Skip to content

Commit 487a3db

Browse files
committed
Apply suggestions from code review
1 parent 0334473 commit 487a3db

File tree

2 files changed

+13
-31
lines changed

2 files changed

+13
-31
lines changed

docs/sources/next/javascript-api/k6-experimental/fs/FileInfo.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export default async function () {
3333
if (fileinfo.name != 'bonjour.txt') {
3434
throw new Error('Unexpected file name');
3535
}
36-
3736
}
3837
```
3938

docs/sources/next/javascript-api/k6-experimental/fs/file/read.md

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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

11699
export default async function () {

0 commit comments

Comments
 (0)