Skip to content

Commit d32f1bd

Browse files
authored
stream: run a replaced write() in the flowing pipe
pipe() calls dest.write(). The fast path of a flowing pipe hands the chunks of a source that pushes from _read() to dest._write() directly, so a write() replaced on the instance or overridden in a subclass no longer runs. A response that is a Writable, wrapped by compression middleware, sent a gzip body with uncompressed chunks in it. Take the fast path only when write() is Writable.prototype.write. Refs: #66182 Signed-off-by: Nigro Simone <nigro.simone@gmail.com> PR-URL: #66596 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 660c104 commit d32f1bd

2 files changed

Lines changed: 91 additions & 5 deletions

File tree

‎lib/internal/streams/writable.js‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -503,14 +503,16 @@ function _write(stream, chunk, encoding, cb) {
503503
return writeOrBuffer(stream, state, chunk, encoding, cb);
504504
}
505505

506-
Writable.prototype.write = function(chunk, encoding, cb) {
506+
function write(chunk, encoding, cb) {
507507
if (encoding != null && typeof encoding === 'function') {
508508
cb = encoding;
509509
encoding = null;
510510
}
511511

512512
return _write(this, chunk, encoding, cb) === true;
513-
};
513+
}
514+
515+
Writable.prototype.write = write;
514516

515517
Writable.prototype.cork = function() {
516518
const state = this._writableState;
@@ -587,16 +589,19 @@ function writeOrBuffer(stream, state, chunk, encoding, callback) {
587589
}
588590

589591
// Steady state of a flowing pipe into a byte-mode Writable: one Buffer,
590-
// nothing queued, and no user write callback. Returns undefined when the
591-
// caller must use write() instead. Otherwise the same boolean as write().
592+
// nothing queued, no user write callback, and write() not replaced. pipe()
593+
// calls dest.write(), so a write() set on the instance, a subclass or the
594+
// prototype has to run. Returns undefined when the caller must use write()
595+
// instead. Otherwise the same boolean as write().
592596
const kWriteFlowBlock = kObjectMode | kDestroyed | kErrored | kSync |
593597
kEnding | kFinished | kWriting | kCorked | kBuffered | kEnded |
594598
kNeedDrain | kWriteCb | kExpectWriteCb | kBufferProcessing |
595599
kFinalCalled | kPrefinished | kOnFinished | kErrorEmitted;
596600

597601
function writeKnownBuffer(stream, chunk) {
598602
const state = stream._writableState;
599-
if (state == null || state.length !== 0 || !(chunk instanceof Buffer))
603+
if (state == null || state.length !== 0 || !(chunk instanceof Buffer) ||
604+
stream.write !== write)
600605
return undefined;
601606

602607
const bits = state[kState];
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
'use strict';
2+
3+
// pipe() calls dest.write(). The fast path of a flowing pipe must not go
4+
// around a write() replaced on the instance, overridden in a subclass, as
5+
// compression middleware does to the response it wraps, or patched on
6+
// Writable.prototype, as APM tools do.
7+
8+
const common = require('../common');
9+
const assert = require('assert');
10+
const { Readable, Writable } = require('stream');
11+
12+
const CHUNKS = 64;
13+
14+
// Pushes from inside _read(), which is what lets the fast path take over.
15+
function source() {
16+
const data = Buffer.alloc(CHUNKS * 1024, 'a');
17+
let offset = 0;
18+
return new Readable({
19+
read() {
20+
if (offset >= data.length) {
21+
this.push(null);
22+
return;
23+
}
24+
this.push(data.subarray(offset, offset += 1024));
25+
},
26+
});
27+
}
28+
29+
{
30+
const dest = new Writable({
31+
write(chunk, encoding, callback) {
32+
callback();
33+
},
34+
});
35+
let calls = 0;
36+
const write = dest.write;
37+
dest.write = function(chunk, encoding, callback) {
38+
calls++;
39+
return write.call(this, chunk, encoding, callback);
40+
};
41+
source().pipe(dest).on('finish', common.mustCall(() => {
42+
assert.strictEqual(calls, CHUNKS);
43+
}));
44+
}
45+
46+
{
47+
class Counting extends Writable {
48+
calls = 0;
49+
50+
write(chunk, encoding, callback) {
51+
this.calls++;
52+
return super.write(chunk, encoding, callback);
53+
}
54+
55+
_write(chunk, encoding, callback) {
56+
callback();
57+
}
58+
}
59+
const dest = new Counting();
60+
source().pipe(dest).on('finish', common.mustCall(() => {
61+
assert.strictEqual(dest.calls, CHUNKS);
62+
}));
63+
}
64+
65+
{
66+
const dest = new Writable({
67+
write(chunk, encoding, callback) {
68+
callback();
69+
},
70+
});
71+
const write = Writable.prototype.write;
72+
let calls = 0;
73+
Writable.prototype.write = function(chunk, encoding, callback) {
74+
if (this === dest) calls++;
75+
return write.call(this, chunk, encoding, callback);
76+
};
77+
source().pipe(dest).on('finish', common.mustCall(() => {
78+
Writable.prototype.write = write;
79+
assert.strictEqual(calls, CHUNKS);
80+
}));
81+
}

0 commit comments

Comments
 (0)