@@ -78,39 +78,22 @@ let file;
7878 file = await open (' bonjour.txt' );
7979})();
8080
81- async function readAll (file , bufferSize = 128 ) {
82- // Obtain the size of the file
81+ async function readAll (file ) {
8382 const fileInfo = await file .stat ();
84- const fileSize = fileInfo .size ;
85-
86- // Prepare a buffer to store the file content
87- const fileContent = new Uint8Array (fileInfo .size );
88- let fileOffset = 0 ;
89-
90- // Prepare a buffer to read the chunks of the file into
91- const buffer = new Uint8Array (Math .min (bufferSize, fileSize));
92-
93- while (true ) {
94- // Read a chunk of the file
95- const bytesRead = await file .read (buffer);
96- if (bytesRead == null || bytesRead === 0 ) {
97- // EOF
98- break ;
99- }
100-
101- // Copy the content of the buffer into the file content buffer
102- fileContent .set (buffer .slice (0 , bytesRead), fileOffset);
103-
104- // Do something useful with the content of the buffer
105- fileOffset += bytesRead;
106-
107- // If bytesRead is less than the buffer size, we've read the whole file
108- if (bytesRead < buffer .byteLength ) {
109- break ;
110- }
83+ const buffer = new Uint8Array (fileInfo .size );
84+
85+ const bytesRead = await file .read (buffer);
86+ if (bytesRead !== fileInfo .size ) {
87+ throw new Error (
88+ ' unexpected number of bytes read; expected ' +
89+ fileInfo .size +
90+ ' but got ' +
91+ bytesRead +
92+ ' bytes'
93+ );
11194 }
11295
113- return fileContent ;
96+ return buffer ;
11497}
11598
11699export default async function () {
0 commit comments