GeezerVison is showing github.com — stream: fix readable behavior for highWaterMark === 0 · nodejs/node@31e3e6f
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/_stream_readable.js +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -383,7 +383,10 @@ Readable.prototype.read = function(n) { | 383 | 383 | // the 'readable' event and move on. | 384 | 384 | if (n === 0 && | 385 | 385 | state.needReadable && | 386 | | - (state.length >= state.highWaterMark || state.ended)) { | | 386 | + ((state.highWaterMark !== 0 ? | | 387 | + state.length >= state.highWaterMark : | | 388 | + state.length > 0) || | | 389 | + state.ended)) { | 387 | 390 | debug('read: emitReadable', state.length, state.ended); | 388 | 391 | if (state.length === 0 && state.ended) | 389 | 392 | endReadable(this); | @@ -808,6 +811,7 @@ Readable.prototype.on = function(ev, fn) { | 808 | 811 | if (!state.endEmitted && !state.readableListening) { | 809 | 812 | state.readableListening = state.needReadable = true; | 810 | 813 | state.emittedReadable = false; | | 814 | + debug('on readable', state.length, state.reading); | 811 | 815 | if (state.length) { | 812 | 816 | emitReadable(this); | 813 | 817 | } else if (!state.reading) { | |
Collapse file Copy file name to clipboard +16Lines changed: 16 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common'); | | 4 | +const { Readable } = require('stream'); | | 5 | + | | 6 | +// This test ensures that there will not be an additional empty 'readable' | | 7 | +// event when stream has ended (only 1 event signalling about end) | | 8 | + | | 9 | +const r = new Readable({ | | 10 | + read: () => {}, | | 11 | +}); | | 12 | + | | 13 | +r.push(null); | | 14 | + | | 15 | +r.on('readable', common.mustCall()); | | 16 | +r.on('end', common.mustCall()); |
Collapse file Copy file name to clipboard +30Lines changed: 30 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common'); | | 4 | + | | 5 | +// This test ensures that Readable stream will call _read() for streams | | 6 | +// with highWaterMark === 0 upon .read(0) instead of just trying to | | 7 | +// emit 'readable' event. | | 8 | + | | 9 | +const assert = require('assert'); | | 10 | +const { Readable } = require('stream'); | | 11 | + | | 12 | +const r = new Readable({ | | 13 | + // must be called only once upon setting 'readable' listener | | 14 | + read: common.mustCall(), | | 15 | + highWaterMark: 0, | | 16 | +}); | | 17 | + | | 18 | +let pushedNull = false; | | 19 | +// this will trigger read(0) but must only be called after push(null) | | 20 | +// because the we haven't pushed any data | | 21 | +r.on('readable', common.mustCall(() => { | | 22 | + assert.strictEqual(r.read(), null); | | 23 | + assert.strictEqual(pushedNull, true); | | 24 | +})); | | 25 | +r.on('end', common.mustCall()); | | 26 | +process.nextTick(() => { | | 27 | + assert.strictEqual(r.read(), null); | | 28 | + pushedNull = true; | | 29 | + r.push(null); | | 30 | +}); |
Collapse file Copy file name to clipboard +29Lines changed: 29 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common'); | | 4 | +const assert = require('assert'); | | 5 | + | | 6 | +// This test ensures that Node.js will not ignore tty 'readable' subscribers | | 7 | +// when it's the only tty subscriber and the only thing keeping event loop alive | | 8 | +// https://github.com/nodejs/node/issues/20503 | | 9 | + | | 10 | +process.stdin.setEncoding('utf8'); | | 11 | + | | 12 | +const expectedInput = ['foo', 'bar', null]; | | 13 | + | | 14 | +process.stdin.on('readable', common.mustCall(function() { | | 15 | + const data = process.stdin.read(); | | 16 | + assert.strictEqual(data, expectedInput.shift()); | | 17 | +}, 3)); // first 2 data, then end | | 18 | + | | 19 | +process.stdin.on('end', common.mustCall()); | | 20 | + | | 21 | +setTimeout(() => { | | 22 | + process.stdin.push('foo'); | | 23 | + process.nextTick(() => { | | 24 | + process.stdin.push('bar'); | | 25 | + process.nextTick(() => { | | 26 | + process.stdin.push(null); | | 27 | + }); | | 28 | + }); | | 29 | +}, 1); |
Collapse file Copy file name to clipboardExpand all lines: test/pseudo-tty/test-readable-tty-keepalive.out
|