Commit f35c0cdd86c for nodejs
commit f35c0cdd86c597d302fe7c3988c83c95038cca62
Author: Matteo Collina <hello@matteocollina.com>
Date: Sun Dec 6 10:36:25 2026 +0100
stream: deprecate piping to emitters without prependListener
The prependListener method has been available on EventEmitter since
Node.js v6.0.0 (April 2016). The internal fallback code in legacy.js
that manually manipulates the `_events` object is only needed to
support versions before that.
This adds a runtime deprecation that fires when piping to an
EventEmitter that lacks the prependListener method, so that users have
time to migrate before the fallback code is removed in a future version.
Signed-off-by: Karan Mangtani <karan.mangtani@lrn.com>
PR-URL: https://github.com/nodejs/node/pull/68009
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: René <contact.9a5d6388@renegade334.me.uk>
diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md
index a87b571a5f3..14b05789727 100644
--- a/doc/api/deprecations.md
+++ b/doc/api/deprecations.md
@@ -4987,6 +4987,22 @@ returns the types of the resources that keep the event loop alive.
`async_hooks.createHook()` may be put behind a command-line flag or removed in
a future version of Node.js.
+### DEP0216: Piping to emitters without `prependListener`
+
+<!-- YAML
+changes:
+ - version: REPLACEME
+ pr-url: https://github.com/nodejs/node/pull/68009
+ description: Runtime deprecation.
+-->
+
+Type: Runtime
+
+Piping to an EventEmitter that does not have a `prependListener` method is
+deprecated. The `prependListener` method has been available on EventEmitter
+since Node.js v6.0.0. The internal fallback code that manually manipulates
+the `_events` object will be removed in a future version.
+
[DEP0142]: #dep0142-repl_builtinlibs
[DEP0156]: #dep0156-aborted-property-and-abort-aborted-event-in-http
[NIST SP 800-38D]: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
diff --git a/lib/internal/streams/legacy.js b/lib/internal/streams/legacy.js
index 8e75dfe6182..c473c01b0f5 100644
--- a/lib/internal/streams/legacy.js
+++ b/lib/internal/streams/legacy.js
@@ -105,6 +105,8 @@ Stream.prototype.eventNames = function eventNames() {
return names;
};
+let emittedPrependListenerDeprecation = false;
+
function prependListener(emitter, event, fn) {
// Sadly this is not cacheable as some libraries bundle their own
// event emitter implementation with them.
@@ -115,6 +117,15 @@ function prependListener(emitter, event, fn) {
// userland ones. NEVER DO THIS. This is here only because this code needs
// to continue to work with older versions of Node.js that do not include
// the prependListener() method. The goal is to eventually remove this hack.
+ if (!emittedPrependListenerDeprecation) {
+ process.emitWarning(
+ 'Piping to an EventEmitter without a prependListener method is deprecated. ' +
+ 'The emitter should have a prependListener method.',
+ 'DeprecationWarning',
+ 'DEP0216',
+ );
+ emittedPrependListenerDeprecation = true;
+ }
if (!emitter._events || !emitter._events[event])
emitter.on(event, fn);
else if (ArrayIsArray(emitter._events[event]))
diff --git a/test/parallel/test-event-emitter-prepend.js b/test/parallel/test-event-emitter-prepend.js
index ffe85449113..e986421651b 100644
--- a/test/parallel/test-event-emitter-prepend.js
+++ b/test/parallel/test-event-emitter-prepend.js
@@ -37,6 +37,14 @@ function Readable() {
Object.setPrototypeOf(Readable.prototype, stream.Stream.prototype);
Object.setPrototypeOf(Readable, stream.Stream);
+// Expect deprecation warning when using fallback path
+common.expectWarning(
+ 'DeprecationWarning',
+ 'Piping to an EventEmitter without a prependListener method is deprecated. ' +
+ 'The emitter should have a prependListener method.',
+ 'DEP0216',
+);
+
const w = new Writable();
const r = new Readable();
r.pipe(w);
diff --git a/test/parallel/test-stream-events-prepend.js b/test/parallel/test-stream-events-prepend.js
index 80fedf8faee..e35ce5126b4 100644
--- a/test/parallel/test-stream-events-prepend.js
+++ b/test/parallel/test-stream-events-prepend.js
@@ -19,6 +19,14 @@ class Readable extends stream.Readable {
}
}
+// Expect deprecation warning when using fallback path
+common.expectWarning(
+ 'DeprecationWarning',
+ 'Piping to an EventEmitter without a prependListener method is deprecated. ' +
+ 'The emitter should have a prependListener method.',
+ 'DEP0216',
+);
+
const w = new Writable();
w.on('pipe', common.mustCall());