Skip to content

Commit 49102ae

Browse files
committed
stream: sync writes don't need drain
1 parent 8ef68e6 commit 49102ae

5 files changed

Lines changed: 67 additions & 26 deletions

lib/_stream_writable.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -378,11 +378,6 @@ function writeOrBuffer(stream, state, isBuf, chunk, encoding, cb) {
378378

379379
state.length += len;
380380

381-
const ret = state.length < state.highWaterMark;
382-
// We must ensure that previous needDrain will not be reset to false.
383-
if (!ret)
384-
state.needDrain = true;
385-
386381
if (state.writing || state.corked) {
387382
var last = state.lastBufferedRequest;
388383
state.lastBufferedRequest = {
@@ -402,6 +397,11 @@ function writeOrBuffer(stream, state, isBuf, chunk, encoding, cb) {
402397
doWrite(stream, state, false, len, chunk, encoding, cb);
403398
}
404399

400+
const ret = state.length < state.highWaterMark;
401+
// We must ensure that previous needDrain will not be reset to false.
402+
if (!ret)
403+
state.needDrain = true;
404+
405405
return ret;
406406
}
407407

test/parallel/test-stream-big-packet.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,19 @@ class TestStream extends stream.Transform {
3232
// Char 'a' only exists in the last write
3333
passed = chunk.toString().includes('a');
3434
}
35-
done();
35+
process.nextTick(done);
3636
}
3737
}
3838

39-
const s1 = new stream.PassThrough();
40-
const s2 = new stream.PassThrough();
39+
class PassStream extends stream.Transform {
40+
_transform(chunk, encoding, done) {
41+
this.push(chunk);
42+
process.nextTick(done);
43+
}
44+
}
45+
46+
const s1 = new PassStream();
47+
const s2 = new PassStream();
4148
const s3 = new TestStream();
4249
s1.pipe(s3);
4350
// Don't let s2 auto close which may close s3

test/parallel/test-stream-pipe-await-drain-push-while-write.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const writable = new stream.Writable({
1919
});
2020
}
2121

22-
cb();
22+
process.nextTick(cb);
2323
}, 3)
2424
});
2525

test/parallel/test-stream-pipe-await-drain.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ const stream = require('stream');
44
const assert = require('assert');
55

66
// This is very similar to test-stream-pipe-cleanup-pause.js.
7+
class TestStream extends stream.Writable {
8+
_write(chunk, encoding, done) {
9+
process.nextTick(done);
10+
}
11+
}
712

813
const reader = new stream.Readable();
9-
const writer1 = new stream.Writable();
10-
const writer2 = new stream.Writable();
11-
const writer3 = new stream.Writable();
14+
const writer1 = new TestStream();
15+
const writer2 = new TestStream();
16+
const writer3 = new TestStream();
1217

1318
// 560000 is chosen here because it is larger than the (default) highWaterMark
1419
// and will cause `.write()` to return false
@@ -19,7 +24,7 @@ reader._read = () => {};
1924

2025
writer1._write = common.mustCall(function(chunk, encoding, cb) {
2126
this.emit('chunk-received');
22-
cb();
27+
process.nextTick(cb);
2328
}, 1);
2429

2530
writer1.once('chunk-received', () => {

test/parallel/test-stream-writable-needdrain-state.js

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,49 @@ const common = require('../common');
44
const stream = require('stream');
55
const assert = require('assert');
66

7-
const transform = new stream.Transform({
8-
transform: _transform,
9-
highWaterMark: 1
10-
});
11-
12-
function _transform(chunk, encoding, cb) {
13-
assert.strictEqual(transform._writableState.needDrain, true);
14-
cb();
15-
}
7+
{
8+
const transform = new stream.Transform({
9+
transform: _transform,
10+
highWaterMark: 1
11+
});
12+
13+
function _transform(chunk, encoding, cb) {
14+
assert.strictEqual(transform._writableState.needDrain, false);
15+
cb();
16+
}
17+
18+
assert.strictEqual(transform._writableState.needDrain, false);
1619

17-
assert.strictEqual(transform._writableState.needDrain, false);
20+
transform.write('asdasd', common.mustCall(() => {
21+
assert.strictEqual(transform._writableState.needDrain, false);
22+
}));
1823

19-
transform.write('asdasd', common.mustCall(() => {
2024
assert.strictEqual(transform._writableState.needDrain, false);
21-
}));
25+
}
26+
27+
{
28+
const w = new stream.Writable({
29+
highWaterMark: 1
30+
});
31+
32+
w._write = (chunk, encoding, cb) => {
33+
cb();
34+
};
35+
w.on('drain', common.mustNotCall());
2236

23-
assert.strictEqual(transform._writableState.needDrain, true);
37+
assert.strictEqual(w.write('asd'), true);
38+
}
39+
40+
41+
{
42+
const w = new stream.Writable({
43+
highWaterMark: 1
44+
});
45+
46+
w._write = (chunk, encoding, cb) => {
47+
process.nextTick(cb);
48+
};
49+
w.on('drain', common.mustCall());
50+
51+
assert.strictEqual(w.write('asd'), false);
52+
}

0 commit comments

Comments
 (0)