Skip to content

Commit 6778261

Browse files
mcollinaaddaleax
authored andcommitted
doc: make sure that calls to .read() are looped
The 'readable' event assumes that calls to readable.read() happens within that event handler until readable.read() returns null. Fixes: #20503 PR-URL: #25375 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 837ca76 commit 6778261

File tree

3 files changed

+35
-20
lines changed

3 files changed

+35
-20
lines changed

doc/api/buffer.md

+16-14
Original file line numberDiff line numberDiff line change
@@ -641,15 +641,16 @@ then copying out the relevant bits.
641641
const store = [];
642642

643643
socket.on('readable', () => {
644-
const data = socket.read();
644+
let data;
645+
while (null !== (data = readable.read())) {
646+
// Allocate for retained data
647+
const sb = Buffer.allocUnsafeSlow(10);
645648

646-
// Allocate for retained data
647-
const sb = Buffer.allocUnsafeSlow(10);
649+
// Copy the data into the new allocation
650+
data.copy(sb, 0, 0, 10);
648651

649-
// Copy the data into the new allocation
650-
data.copy(sb, 0, 0, 10);
651-
652-
store.push(sb);
652+
store.push(sb);
653+
}
653654
});
654655
```
655656

@@ -2561,15 +2562,16 @@ un-pooled `Buffer` instance using `SlowBuffer` then copy out the relevant bits.
25612562
const store = [];
25622563

25632564
socket.on('readable', () => {
2564-
const data = socket.read();
2565+
let data;
2566+
while (null !== (data = readable.read())) {
2567+
// Allocate for retained data
2568+
const sb = SlowBuffer(10);
25652569

2566-
// Allocate for retained data
2567-
const sb = SlowBuffer(10);
2570+
// Copy the data into the new allocation
2571+
data.copy(sb, 0, 0, 10);
25682572

2569-
// Copy the data into the new allocation
2570-
data.copy(sb, 0, 0, 10);
2571-
2572-
store.push(sb);
2573+
store.push(sb);
2574+
}
25732575
});
25742576
```
25752577

doc/api/crypto.md

+15-6
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,10 @@ const cipher = crypto.createCipheriv(algorithm, key, iv);
200200

201201
let encrypted = '';
202202
cipher.on('readable', () => {
203-
const data = cipher.read();
204-
if (data)
205-
encrypted += data.toString('hex');
203+
let chunk;
204+
while (null !== (chunk = cipher.read())) {
205+
encrypted += chunk.toString('hex');
206+
}
206207
});
207208
cipher.on('end', () => {
208209
console.log(encrypted);
@@ -383,9 +384,9 @@ const decipher = crypto.createDecipheriv(algorithm, key, iv);
383384

384385
let decrypted = '';
385386
decipher.on('readable', () => {
386-
const data = decipher.read();
387-
if (data)
388-
decrypted += data.toString('utf8');
387+
while (null !== (chunk = decipher.read())) {
388+
decrypted += chunk.toString('utf8');
389+
}
389390
});
390391
decipher.on('end', () => {
391392
console.log(decrypted);
@@ -941,6 +942,8 @@ const crypto = require('crypto');
941942
const hash = crypto.createHash('sha256');
942943

943944
hash.on('readable', () => {
945+
// Only one element is going to be produced by the
946+
// hash stream.
944947
const data = hash.read();
945948
if (data) {
946949
console.log(data.toString('hex'));
@@ -1033,6 +1036,8 @@ const crypto = require('crypto');
10331036
const hmac = crypto.createHmac('sha256', 'a secret');
10341037

10351038
hmac.on('readable', () => {
1039+
// Only one element is going to be produced by the
1040+
// hash stream.
10361041
const data = hmac.read();
10371042
if (data) {
10381043
console.log(data.toString('hex'));
@@ -1762,6 +1767,8 @@ const hash = crypto.createHash('sha256');
17621767

17631768
const input = fs.createReadStream(filename);
17641769
input.on('readable', () => {
1770+
// Only one element is going to be produced by the
1771+
// hash stream.
17651772
const data = input.read();
17661773
if (data)
17671774
hash.update(data);
@@ -1807,6 +1814,8 @@ const hmac = crypto.createHmac('sha256', 'a secret');
18071814

18081815
const input = fs.createReadStream(filename);
18091816
input.on('readable', () => {
1817+
// Only one element is going to be produced by the
1818+
// hash stream.
18101819
const data = input.read();
18111820
if (data)
18121821
hmac.update(data);

doc/api/stream.md

+4
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,10 @@ readable.on('readable', () => {
10111011
});
10121012
```
10131013

1014+
Note that the `while` loop is necessary when processing data with
1015+
`readable.read()`. Only after `readable.read()` returns `null`,
1016+
[`'readable'`]() will be emitted.
1017+
10141018
A `Readable` stream in object mode will always return a single item from
10151019
a call to [`readable.read(size)`][stream-read], regardless of the value of the
10161020
`size` argument.

0 commit comments

Comments
 (0)