Skip to content

Commit 530f005

Browse files
mcollinaMylesBorins
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 82dd321 commit 530f005

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

@@ -2558,15 +2559,16 @@ un-pooled `Buffer` instance using `SlowBuffer` then copy out the relevant bits.
25582559
const store = [];
25592560

25602561
socket.on('readable', () => {
2561-
const data = socket.read();
2562+
let data;
2563+
while (null !== (data = readable.read())) {
2564+
// Allocate for retained data
2565+
const sb = SlowBuffer(10);
25622566

2563-
// Allocate for retained data
2564-
const sb = SlowBuffer(10);
2567+
// Copy the data into the new allocation
2568+
data.copy(sb, 0, 0, 10);
25652569

2566-
// Copy the data into the new allocation
2567-
data.copy(sb, 0, 0, 10);
2568-
2569-
store.push(sb);
2570+
store.push(sb);
2571+
}
25702572
});
25712573
```
25722574

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);
@@ -944,6 +945,8 @@ const crypto = require('crypto');
944945
const hash = crypto.createHash('sha256');
945946

946947
hash.on('readable', () => {
948+
// Only one element is going to be produced by the
949+
// hash stream.
947950
const data = hash.read();
948951
if (data) {
949952
console.log(data.toString('hex'));
@@ -1036,6 +1039,8 @@ const crypto = require('crypto');
10361039
const hmac = crypto.createHmac('sha256', 'a secret');
10371040

10381041
hmac.on('readable', () => {
1042+
// Only one element is going to be produced by the
1043+
// hash stream.
10391044
const data = hmac.read();
10401045
if (data) {
10411046
console.log(data.toString('hex'));
@@ -1678,6 +1683,8 @@ const hash = crypto.createHash('sha256');
16781683

16791684
const input = fs.createReadStream(filename);
16801685
input.on('readable', () => {
1686+
// Only one element is going to be produced by the
1687+
// hash stream.
16811688
const data = input.read();
16821689
if (data)
16831690
hash.update(data);
@@ -1718,6 +1725,8 @@ const hmac = crypto.createHmac('sha256', 'a secret');
17181725

17191726
const input = fs.createReadStream(filename);
17201727
input.on('readable', () => {
1728+
// Only one element is going to be produced by the
1729+
// hash stream.
17211730
const data = input.read();
17221731
if (data)
17231732
hmac.update(data);

doc/api/stream.md

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

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

0 commit comments

Comments
 (0)