Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ const {

const { errorOrDestroy } = destroyImpl;

const kAfterWriteTickInfo = Symbol('kAfterWriteTickInfo');

ObjectSetPrototypeOf(Writable.prototype, Stream.prototype);
ObjectSetPrototypeOf(Writable, Stream);

Expand Down Expand Up @@ -148,7 +150,7 @@ function WritableState(options, stream, isDuplex) {

// Storage for data passed to the afterWrite() callback in case of
// synchronous _write() completion.
this.afterWriteTickInfo = null;
this[kAfterWriteTickInfo] = null;

this.bufferedRequest = null;
this.lastBufferedRequest = null;
Expand Down Expand Up @@ -185,6 +187,11 @@ function WritableState(options, stream, isDuplex) {
this.corkedRequestsFree = corkReq;
}

ObjectDefineProperty(WritableState.prototype, 'afterWriteTickInfo', {
get() { return this[kAfterWriteTickInfo]; },
set(val) { this[kAfterWriteTickInfo] = val; },
});

WritableState.prototype.getBuffer = function getBuffer() {
var current = this.bufferedRequest;
const out = [];
Expand Down Expand Up @@ -487,12 +494,12 @@ function onwrite(stream, er) {
// the same. In that case, we do not schedule a new nextTick(), but rather
// just increase a counter, to improve performance and avoid memory
// allocations.
if (state.afterWriteTickInfo !== null &&
state.afterWriteTickInfo.cb === cb) {
state.afterWriteTickInfo.count++;
if (state[kAfterWriteTickInfo] !== null &&
state[kAfterWriteTickInfo].cb === cb) {
state[kAfterWriteTickInfo].count++;
} else {
state.afterWriteTickInfo = { count: 1, cb, stream, state };
process.nextTick(afterWriteTick, state.afterWriteTickInfo);
state[kAfterWriteTickInfo] = { count: 1, cb, stream, state };
process.nextTick(afterWriteTick, state[kAfterWriteTickInfo]);
}
} else {
afterWrite(stream, state, 1, cb);
Expand All @@ -501,7 +508,7 @@ function onwrite(stream, er) {
}

function afterWriteTick({ stream, state, count, cb }) {
state.afterWriteTickInfo = null;
state[kAfterWriteTickInfo] = null;
return afterWrite(stream, state, count, cb);
}

Expand Down
9 changes: 9 additions & 0 deletions test/parallel/test-json-stringify-stdout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

// Make sure stdout does not contain circular structures.
// Regression test for https://github.com/nodejs/node/issues/31277

require('../common');

process.stdout.write('');
JSON.stringify(process.stdout);