Skip to content

Commit a603461

Browse files
committed
stream: deprecate piping to emitters without prependListener
1 parent 4f6e602 commit a603461

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
@@ -4548,6 +4548,22 @@ that have proven unresolveable. See [caveats of asynchronous customization hooks
45484548
`module.registerHooks()` as soon as possible as `module.register()` will be
45494549
removed in a future version of Node.js.
45504550
4551+
### DEP0206: Piping to emitters without `prependListener`
4552+
4553+
<!-- YAML
4554+
changes:
4555+
- version: REPLACEME
4556+
pr-url: https://github.com/nodejs/node/pull/62435
4557+
description: Runtime deprecation.
4558+
-->
4559+
4560+
Type: Runtime
4561+
4562+
Piping to an EventEmitter that does not have a `prependListener` method is
4563+
deprecated. The `prependListener` method has been available on EventEmitter
4564+
since Node.js v6.0.0. The internal fallback code that manually manipulates
4565+
the `_events` object will be removed in a future version.
4566+
45514567
[DEP0142]: #dep0142-repl_builtinlibs
45524568
[NIST SP 800-38D]: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
45534569
[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+
'DEP0206',
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+
'DEP0206',
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+
'DEP0206',
28+
);
29+
2230
const w = new Writable();
2331
w.on('pipe', common.mustCall());
2432

0 commit comments

Comments
 (0)