From e12a7e359da5817ff33cc043c6b19e3478834adb Mon Sep 17 00:00:00 2001 From: Karan Mangtani Date: Thu, 2 Apr 2026 19:50:30 +0530 Subject: [PATCH] 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 --- doc/api/deprecations.md | 16 ++++++++++++++++ lib/internal/streams/legacy.js | 11 +++++++++++ test/parallel/test-event-emitter-prepend.js | 8 ++++++++ test/parallel/test-stream-events-prepend.js | 8 ++++++++ 4 files changed, 43 insertions(+) diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index a87b571a5f3a..14b05789727c 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` + + + +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 8e75dfe61822..c473c01b0f53 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 ffe854491136..e986421651b7 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 80fedf8faee5..e35ce5126b42 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());