Skip to content

Commit b6195e4

Browse files
committed
stream: compare with the original write() in the pipe fast path
Signed-off-by: Nigro Simone <nigro.simone@gmail.com>
1 parent d603f99 commit b6195e4

2 files changed

Lines changed: 29 additions & 8 deletions

File tree

‎lib/internal/streams/writable.js‎

Lines changed: 8 additions & 6 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;
@@ -588,9 +590,9 @@ function writeOrBuffer(stream, state, chunk, encoding, callback) {
588590

589591
// Steady state of a flowing pipe into a byte-mode Writable: one Buffer,
590592
// nothing queued, no user write callback, and write() not replaced. pipe()
591-
// calls dest.write(), so a write() set on the instance or a subclass has to
592-
// run. Returns undefined when the caller must use write() instead. Otherwise
593-
// the same boolean as write().
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().
594596
const kWriteFlowBlock = kObjectMode | kDestroyed | kErrored | kSync |
595597
kEnding | kFinished | kWriting | kCorked | kBuffered | kEnded |
596598
kNeedDrain | kWriteCb | kExpectWriteCb | kBufferProcessing |
@@ -599,7 +601,7 @@ const kWriteFlowBlock = kObjectMode | kDestroyed | kErrored | kSync |
599601
function writeKnownBuffer(stream, chunk) {
600602
const state = stream._writableState;
601603
if (state == null || state.length !== 0 || !(chunk instanceof Buffer) ||
602-
stream.write !== Writable.prototype.write)
604+
stream.write !== write)
603605
return undefined;
604606

605607
const bits = state[kState];

‎test/parallel/test-stream-pipe-replaced-write.js‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict';
22

33
// pipe() calls dest.write(). The fast path of a flowing pipe must not go
4-
// around a write() replaced on the instance or overridden in a subclass,
5-
// as compression middleware does to the response it wraps.
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.
67

78
const common = require('../common');
89
const assert = require('assert');
@@ -60,3 +61,21 @@ function source() {
6061
assert.strictEqual(dest.calls, CHUNKS);
6162
}));
6263
}
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)