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: https://github.com/nodejs/node/issues/20503 PR-URL: https://github.com/nodejs/node/pull/25375 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
parent
842a35fbac
commit
2d2f82c413
@ -641,15 +641,16 @@ then copying out the relevant bits.
|
|||||||
const store = [];
|
const store = [];
|
||||||
|
|
||||||
socket.on('readable', () => {
|
socket.on('readable', () => {
|
||||||
const data = socket.read();
|
let data;
|
||||||
|
while (null !== (data = readable.read())) {
|
||||||
|
// Allocate for retained data
|
||||||
|
const sb = Buffer.allocUnsafeSlow(10);
|
||||||
|
|
||||||
// Allocate for retained data
|
// Copy the data into the new allocation
|
||||||
const sb = Buffer.allocUnsafeSlow(10);
|
data.copy(sb, 0, 0, 10);
|
||||||
|
|
||||||
// Copy the data into the new allocation
|
store.push(sb);
|
||||||
data.copy(sb, 0, 0, 10);
|
}
|
||||||
|
|
||||||
store.push(sb);
|
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -2561,15 +2562,16 @@ un-pooled `Buffer` instance using `SlowBuffer` then copy out the relevant bits.
|
|||||||
const store = [];
|
const store = [];
|
||||||
|
|
||||||
socket.on('readable', () => {
|
socket.on('readable', () => {
|
||||||
const data = socket.read();
|
let data;
|
||||||
|
while (null !== (data = readable.read())) {
|
||||||
|
// Allocate for retained data
|
||||||
|
const sb = SlowBuffer(10);
|
||||||
|
|
||||||
// Allocate for retained data
|
// Copy the data into the new allocation
|
||||||
const sb = SlowBuffer(10);
|
data.copy(sb, 0, 0, 10);
|
||||||
|
|
||||||
// Copy the data into the new allocation
|
store.push(sb);
|
||||||
data.copy(sb, 0, 0, 10);
|
}
|
||||||
|
|
||||||
store.push(sb);
|
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -200,9 +200,10 @@ const cipher = crypto.createCipheriv(algorithm, key, iv);
|
|||||||
|
|
||||||
let encrypted = '';
|
let encrypted = '';
|
||||||
cipher.on('readable', () => {
|
cipher.on('readable', () => {
|
||||||
const data = cipher.read();
|
let chunk;
|
||||||
if (data)
|
while (null !== (chunk = cipher.read())) {
|
||||||
encrypted += data.toString('hex');
|
encrypted += chunk.toString('hex');
|
||||||
|
}
|
||||||
});
|
});
|
||||||
cipher.on('end', () => {
|
cipher.on('end', () => {
|
||||||
console.log(encrypted);
|
console.log(encrypted);
|
||||||
@ -383,9 +384,9 @@ const decipher = crypto.createDecipheriv(algorithm, key, iv);
|
|||||||
|
|
||||||
let decrypted = '';
|
let decrypted = '';
|
||||||
decipher.on('readable', () => {
|
decipher.on('readable', () => {
|
||||||
const data = decipher.read();
|
while (null !== (chunk = decipher.read())) {
|
||||||
if (data)
|
decrypted += chunk.toString('utf8');
|
||||||
decrypted += data.toString('utf8');
|
}
|
||||||
});
|
});
|
||||||
decipher.on('end', () => {
|
decipher.on('end', () => {
|
||||||
console.log(decrypted);
|
console.log(decrypted);
|
||||||
@ -941,6 +942,8 @@ const crypto = require('crypto');
|
|||||||
const hash = crypto.createHash('sha256');
|
const hash = crypto.createHash('sha256');
|
||||||
|
|
||||||
hash.on('readable', () => {
|
hash.on('readable', () => {
|
||||||
|
// Only one element is going to be produced by the
|
||||||
|
// hash stream.
|
||||||
const data = hash.read();
|
const data = hash.read();
|
||||||
if (data) {
|
if (data) {
|
||||||
console.log(data.toString('hex'));
|
console.log(data.toString('hex'));
|
||||||
@ -1033,6 +1036,8 @@ const crypto = require('crypto');
|
|||||||
const hmac = crypto.createHmac('sha256', 'a secret');
|
const hmac = crypto.createHmac('sha256', 'a secret');
|
||||||
|
|
||||||
hmac.on('readable', () => {
|
hmac.on('readable', () => {
|
||||||
|
// Only one element is going to be produced by the
|
||||||
|
// hash stream.
|
||||||
const data = hmac.read();
|
const data = hmac.read();
|
||||||
if (data) {
|
if (data) {
|
||||||
console.log(data.toString('hex'));
|
console.log(data.toString('hex'));
|
||||||
@ -1762,6 +1767,8 @@ const hash = crypto.createHash('sha256');
|
|||||||
|
|
||||||
const input = fs.createReadStream(filename);
|
const input = fs.createReadStream(filename);
|
||||||
input.on('readable', () => {
|
input.on('readable', () => {
|
||||||
|
// Only one element is going to be produced by the
|
||||||
|
// hash stream.
|
||||||
const data = input.read();
|
const data = input.read();
|
||||||
if (data)
|
if (data)
|
||||||
hash.update(data);
|
hash.update(data);
|
||||||
@ -1807,6 +1814,8 @@ const hmac = crypto.createHmac('sha256', 'a secret');
|
|||||||
|
|
||||||
const input = fs.createReadStream(filename);
|
const input = fs.createReadStream(filename);
|
||||||
input.on('readable', () => {
|
input.on('readable', () => {
|
||||||
|
// Only one element is going to be produced by the
|
||||||
|
// hash stream.
|
||||||
const data = input.read();
|
const data = input.read();
|
||||||
if (data)
|
if (data)
|
||||||
hmac.update(data);
|
hmac.update(data);
|
||||||
|
@ -1011,6 +1011,10 @@ readable.on('readable', () => {
|
|||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Note that the `while` loop is necessary when processing data with
|
||||||
|
`readable.read()`. Only after `readable.read()` returns `null`,
|
||||||
|
[`'readable'`]() will be emitted.
|
||||||
|
|
||||||
A `Readable` stream in object mode will always return a single item from
|
A `Readable` stream in object mode will always return a single item from
|
||||||
a call to [`readable.read(size)`][stream-read], regardless of the value of the
|
a call to [`readable.read(size)`][stream-read], regardless of the value of the
|
||||||
`size` argument.
|
`size` argument.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user