Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions doc/api/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -4517,6 +4517,22 @@ Type: Documentation-only
Passing a non-extractable [`CryptoKey`][] to [`KeyObject.from()`][] is
deprecated and will throw an error in a future version.

### DEP0205: Piping to emitters without `prependListener`

<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/62435
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
[NIST SP 800-38D]: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
[RFC 6066]: https://tools.ietf.org/html/rfc6066#section-3
Expand Down
11 changes: 11 additions & 0 deletions lib/internal/streams/legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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',
'DEP0205',
);
emittedPrependListenerDeprecation = true;
}
if (!emitter._events || !emitter._events[event])
emitter.on(event, fn);
else if (ArrayIsArray(emitter._events[event]))
Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-event-emitter-prepend.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
'DEP0205',
);

const w = new Writable();
const r = new Readable();
r.pipe(w);
8 changes: 8 additions & 0 deletions test/parallel/test-stream-events-prepend.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
'DEP0205',
);

const w = new Writable();
w.on('pipe', common.mustCall());

Expand Down
Loading