Skip to content

Commit fbbd482

Browse files
committed
Add example implementation of readAll
1 parent 1e28d34 commit fbbd482

File tree

1 file changed

+64
-10
lines changed
  • docs/sources/next/javascript-api/k6-experimental/fs/file

1 file changed

+64
-10
lines changed

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

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ A [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl
2222

2323
## Example
2424

25+
### Reading a file
26+
27+
In the following example, we open a file and read it in chunks of 128 bytes until we reach the end of the file.
28+
2529
{{< code >}}
2630

2731
```javascript
@@ -33,13 +37,7 @@ let file;
3337
})();
3438

3539
export default async function () {
36-
// About information about the file
37-
const fileinfo = await file.stat();
38-
if (fileinfo.name != 'bonjour.txt') {
39-
throw new Error('Unexpected file name');
40-
}
41-
42-
const buffer = new Uint8Array(4);
40+
const buffer = new Uint8Array(128);
4341

4442
let totalBytesRead = 0;
4543
while (true) {
@@ -59,11 +57,67 @@ export default async function () {
5957
}
6058
}
6159

62-
// Check that we read the expected number of bytes
63-
if (totalBytesRead != fileinfo.size) {
64-
throw new Error('Unexpected number of bytes read');
60+
// Seek back to the beginning of the file
61+
await file.seek(0, SeekMode.Start);
62+
}
63+
```
64+
65+
{{< /code >}}
66+
67+
### `readAll` helper function
68+
69+
The following helper function can be used to read the entire contents of a file into a [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) buffer.
70+
71+
{{< code >}}
72+
73+
```javascript
74+
import { open, SeekMode } from 'k6/experimental/fs';
75+
76+
let file;
77+
(async function () {
78+
file = await open('bonjour.txt');
79+
})();
80+
81+
async function readAll(file, bufferSize = 128) {
82+
// Obtain the size of the file
83+
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+
}
65111
}
66112

113+
return fileContent;
114+
}
115+
116+
export default async function () {
117+
// Read the whole file
118+
const fileContent = await readAll(file);
119+
console.log(JSON.stringify(fileContent));
120+
67121
// Seek back to the beginning of the file
68122
await file.seek(0, SeekMode.Start);
69123
}

0 commit comments

Comments
 (0)