Skip to content

Commit dbb2a5f

Browse files
committed
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 that manually manipulates the _events object was intended for pre-v6 compatibility. This adds a runtime deprecation warning (DEP0205) when piping to an EventEmitter that lacks the prependListener method. The fallback behavior is preserved for now but will be removed in a future version. PR-URL: https://github.com/nodejs/node/pull/XXXXX Refs: https://github.com/nodejs/node/issues/XXXXX
1 parent 0d7e4b1 commit dbb2a5f

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

doc/api/deprecations.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4517,6 +4517,22 @@ Type: Documentation-only
45174517
Passing a non-extractable [`CryptoKey`][] to [`KeyObject.from()`][] is
45184518
deprecated and will throw an error in a future version.
45194519
4520+
### DEP0205: Piping to emitters without `prependListener`
4521+
4522+
<!-- YAML
4523+
changes:
4524+
- version: REPLACEME
4525+
pr-url: https://github.com/nodejs/node/pull/XXXXX
4526+
description: Runtime deprecation.
4527+
-->
4528+
4529+
Type: Runtime
4530+
4531+
Piping to an EventEmitter that does not have a `prependListener` method is
4532+
deprecated. The `prependListener` method has been available on EventEmitter
4533+
since Node.js v6.0.0. The internal fallback code that manually manipulates
4534+
the `_events` object will be removed in a future version.
4535+
45204536
[DEP0142]: #dep0142-repl_builtinlibs
45214537
[NIST SP 800-38D]: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
45224538
[RFC 6066]: https://tools.ietf.org/html/rfc6066#section-3

lib/internal/streams/legacy.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ Stream.prototype.eventNames = function eventNames() {
105105
return names;
106106
};
107107

108+
let emittedPrependListenerDeprecation = false;
109+
108110
function prependListener(emitter, event, fn) {
109111
// Sadly this is not cacheable as some libraries bundle their own
110112
// event emitter implementation with them.
@@ -115,6 +117,15 @@ function prependListener(emitter, event, fn) {
115117
// userland ones. NEVER DO THIS. This is here only because this code needs
116118
// to continue to work with older versions of Node.js that do not include
117119
// the prependListener() method. The goal is to eventually remove this hack.
120+
if (!emittedPrependListenerDeprecation) {
121+
process.emitWarning(
122+
'Piping to an EventEmitter without a prependListener method is deprecated. ' +
123+
'The emitter should have a prependListener method.',
124+
'DeprecationWarning',
125+
'DEP0205',
126+
);
127+
emittedPrependListenerDeprecation = true;
128+
}
118129
if (!emitter._events || !emitter._events[event])
119130
emitter.on(event, fn);
120131
else if (ArrayIsArray(emitter._events[event]))

test/parallel/test-event-emitter-prepend.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ function Readable() {
3737
Object.setPrototypeOf(Readable.prototype, stream.Stream.prototype);
3838
Object.setPrototypeOf(Readable, stream.Stream);
3939

40+
// Expect deprecation warning when using fallback path
41+
common.expectWarning(
42+
'DeprecationWarning',
43+
'Piping to an EventEmitter without a prependListener method is deprecated. ' +
44+
'The emitter should have a prependListener method.',
45+
'DEP0205',
46+
);
47+
4048
const w = new Writable();
4149
const r = new Readable();
4250
r.pipe(w);

test/parallel/test-stream-events-prepend.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ class Readable extends stream.Readable {
1919
}
2020
}
2121

22+
// Expect deprecation warning when using fallback path
23+
common.expectWarning(
24+
'DeprecationWarning',
25+
'Piping to an EventEmitter without a prependListener method is deprecated. ' +
26+
'The emitter should have a prependListener method.',
27+
'DEP0205',
28+
);
29+
2230
const w = new Writable();
2331
w.on('pipe', common.mustCall());
2432

0 commit comments

Comments
 (0)