From d603f99c1d1390352c91b9730c1d0d4d21274bba Mon Sep 17 00:00:00 2001 From: Nigro Simone Date: Thu, 8 Oct 2026 10:16:09 +0200 Subject: [PATCH 1/2] 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: https://github.com/nodejs/node/pull/66182 Signed-off-by: Nigro Simone --- lib/internal/streams/writable.js | 9 ++- .../test-stream-pipe-replaced-write.js | 62 +++++++++++++++++++ 2 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 test/parallel/test-stream-pipe-replaced-write.js diff --git a/lib/internal/streams/writable.js b/lib/internal/streams/writable.js index f8c61f819615..d11141cf2dac 100644 --- a/lib/internal/streams/writable.js +++ b/lib/internal/streams/writable.js @@ -587,8 +587,10 @@ function writeOrBuffer(stream, state, chunk, encoding, callback) { } // Steady state of a flowing pipe into a byte-mode Writable: one Buffer, -// nothing queued, and no user write callback. Returns undefined when the -// caller must use write() instead. Otherwise the same boolean as write(). +// nothing queued, no user write callback, and write() not replaced. pipe() +// calls dest.write(), so a write() set on the instance or a subclass has to +// run. Returns undefined when the caller must use write() instead. Otherwise +// the same boolean as write(). const kWriteFlowBlock = kObjectMode | kDestroyed | kErrored | kSync | kEnding | kFinished | kWriting | kCorked | kBuffered | kEnded | kNeedDrain | kWriteCb | kExpectWriteCb | kBufferProcessing | @@ -596,7 +598,8 @@ const kWriteFlowBlock = kObjectMode | kDestroyed | kErrored | kSync | function writeKnownBuffer(stream, chunk) { const state = stream._writableState; - if (state == null || state.length !== 0 || !(chunk instanceof Buffer)) + if (state == null || state.length !== 0 || !(chunk instanceof Buffer) || + stream.write !== Writable.prototype.write) return undefined; const bits = state[kState]; diff --git a/test/parallel/test-stream-pipe-replaced-write.js b/test/parallel/test-stream-pipe-replaced-write.js new file mode 100644 index 000000000000..4b9e3b9b66e7 --- /dev/null +++ b/test/parallel/test-stream-pipe-replaced-write.js @@ -0,0 +1,62 @@ +'use strict'; + +// pipe() calls dest.write(). The fast path of a flowing pipe must not go +// around a write() replaced on the instance or overridden in a subclass, +// as compression middleware does to the response it wraps. + +const common = require('../common'); +const assert = require('assert'); +const { Readable, Writable } = require('stream'); + +const CHUNKS = 64; + +// Pushes from inside _read(), which is what lets the fast path take over. +function source() { + const data = Buffer.alloc(CHUNKS * 1024, 'a'); + let offset = 0; + return new Readable({ + read() { + if (offset >= data.length) { + this.push(null); + return; + } + this.push(data.subarray(offset, offset += 1024)); + }, + }); +} + +{ + const dest = new Writable({ + write(chunk, encoding, callback) { + callback(); + }, + }); + let calls = 0; + const write = dest.write; + dest.write = function(chunk, encoding, callback) { + calls++; + return write.call(this, chunk, encoding, callback); + }; + source().pipe(dest).on('finish', common.mustCall(() => { + assert.strictEqual(calls, CHUNKS); + })); +} + +{ + class Counting extends Writable { + calls = 0; + + write(chunk, encoding, callback) { + this.calls++; + return super.write(chunk, encoding, callback); + } + + _write(chunk, encoding, callback) { + callback(); + } + } + const dest = new Counting(); + source().pipe(dest).on('finish', common.mustCall(() => { + assert.strictEqual(dest.calls, CHUNKS); + })); +} From b6195e48fab413e533d04efd924fcff1c5cefbe8 Mon Sep 17 00:00:00 2001 From: Nigro Simone Date: Thu, 19 Nov 2026 07:31:46 +0100 Subject: [PATCH 2/2] stream: compare with the original write() in the pipe fast path Signed-off-by: Nigro Simone --- lib/internal/streams/writable.js | 14 ++++++----- .../test-stream-pipe-replaced-write.js | 23 +++++++++++++++++-- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/lib/internal/streams/writable.js b/lib/internal/streams/writable.js index d11141cf2dac..3f62489c0f70 100644 --- a/lib/internal/streams/writable.js +++ b/lib/internal/streams/writable.js @@ -503,14 +503,16 @@ function _write(stream, chunk, encoding, cb) { return writeOrBuffer(stream, state, chunk, encoding, cb); } -Writable.prototype.write = function(chunk, encoding, cb) { +function write(chunk, encoding, cb) { if (encoding != null && typeof encoding === 'function') { cb = encoding; encoding = null; } return _write(this, chunk, encoding, cb) === true; -}; +} + +Writable.prototype.write = write; Writable.prototype.cork = function() { const state = this._writableState; @@ -588,9 +590,9 @@ function writeOrBuffer(stream, state, chunk, encoding, callback) { // Steady state of a flowing pipe into a byte-mode Writable: one Buffer, // nothing queued, no user write callback, and write() not replaced. pipe() -// calls dest.write(), so a write() set on the instance or a subclass has to -// run. Returns undefined when the caller must use write() instead. Otherwise -// the same boolean as write(). +// calls dest.write(), so a write() set on the instance, a subclass or the +// prototype has to run. Returns undefined when the caller must use write() +// instead. Otherwise the same boolean as write(). const kWriteFlowBlock = kObjectMode | kDestroyed | kErrored | kSync | kEnding | kFinished | kWriting | kCorked | kBuffered | kEnded | kNeedDrain | kWriteCb | kExpectWriteCb | kBufferProcessing | @@ -599,7 +601,7 @@ const kWriteFlowBlock = kObjectMode | kDestroyed | kErrored | kSync | function writeKnownBuffer(stream, chunk) { const state = stream._writableState; if (state == null || state.length !== 0 || !(chunk instanceof Buffer) || - stream.write !== Writable.prototype.write) + stream.write !== write) return undefined; const bits = state[kState]; diff --git a/test/parallel/test-stream-pipe-replaced-write.js b/test/parallel/test-stream-pipe-replaced-write.js index 4b9e3b9b66e7..ef65d453a486 100644 --- a/test/parallel/test-stream-pipe-replaced-write.js +++ b/test/parallel/test-stream-pipe-replaced-write.js @@ -1,8 +1,9 @@ 'use strict'; // pipe() calls dest.write(). The fast path of a flowing pipe must not go -// around a write() replaced on the instance or overridden in a subclass, -// as compression middleware does to the response it wraps. +// around a write() replaced on the instance, overridden in a subclass, as +// compression middleware does to the response it wraps, or patched on +// Writable.prototype, as APM tools do. const common = require('../common'); const assert = require('assert'); @@ -60,3 +61,21 @@ function source() { assert.strictEqual(dest.calls, CHUNKS); })); } + +{ + const dest = new Writable({ + write(chunk, encoding, callback) { + callback(); + }, + }); + const write = Writable.prototype.write; + let calls = 0; + Writable.prototype.write = function(chunk, encoding, callback) { + if (this === dest) calls++; + return write.call(this, chunk, encoding, callback); + }; + source().pipe(dest).on('finish', common.mustCall(() => { + Writable.prototype.write = write; + assert.strictEqual(calls, CHUNKS); + })); +}