GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — stream: fix readable behavior for highWaterMark === 0 · nodejs/node@31e3e6f

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib
    • _stream_readable.js
  • test
    • parallel
      • test-readable-single-end.js
      • test-stream-readable-hwm-0.js
    • pseudo-tty
      • test-readable-tty-keepalive.js
      • test-readable-tty-keepalive.out
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/_stream_readable.js‎

Copy file name to clipboardExpand all lines: lib/_stream_readable.js
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,10 @@ Readable.prototype.read = function(n) {
383383
// the 'readable' event and move on.
384384
if (n === 0 &&
385385
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)) {
387390
debug('read: emitReadable', state.length, state.ended);
388391
if (state.length === 0 && state.ended)
389392
endReadable(this);
@@ -808,6 +811,7 @@ Readable.prototype.on = function(ev, fn) {
808811
if (!state.endEmitted && !state.readableListening) {
809812
state.readableListening = state.needReadable = true;
810813
state.emittedReadable = false;
814+
debug('on readable', state.length, state.reading);
811815
if (state.length) {
812816
emitReadable(this);
813817
} else if (!state.reading) {
Collapse file

‎test/parallel/test-readable-single-end.js‎

Copy file name to clipboard
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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

‎test/parallel/test-stream-readable-hwm-0.js‎

Copy file name to clipboard
+30Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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

‎test/pseudo-tty/test-readable-tty-keepalive.js‎

Copy file name to clipboard
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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

‎test/pseudo-tty/test-readable-tty-keepalive.out‎

Copy file name to clipboardExpand all lines: test/pseudo-tty/test-readable-tty-keepalive.out
Whitespace-only changes.

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.