Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b8c6ced

Browse filesBrowse files
ronagRafaelGSS
authored andcommitted
stream: expose stream symbols
This is required for streams interop with e.g. readable-stream. Currently readable-stream helpers will not work with normal node streams which is confusing and bad for the ecosystem. PR-URL: #45671 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Debadree Chatterjee <debadree333@gmail.com>
1 parent dc024d9 commit b8c6ced
Copy full SHA for b8c6ced

File tree

Expand file treeCollapse file tree

3 files changed

+20
-10
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+20
-10
lines changed
Open diff view settings
Collapse file

‎lib/internal/streams/destroy.js‎

Copy file name to clipboardExpand all lines: lib/internal/streams/destroy.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const {
1111
Symbol,
1212
} = primordials;
1313
const {
14-
kDestroyed,
14+
kIsDestroyed,
1515
isDestroyed,
1616
isFinished,
1717
isServerRequest,
@@ -327,7 +327,7 @@ function destroyer(stream, err) {
327327
}
328328

329329
if (!stream.destroyed) {
330-
stream[kDestroyed] = true;
330+
stream[kIsDestroyed] = true;
331331
}
332332
}
333333

Collapse file

‎lib/internal/streams/utils.js‎

Copy file name to clipboardExpand all lines: lib/internal/streams/utils.js
+14-8Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
'use strict';
22

33
const {
4-
Symbol,
54
SymbolAsyncIterator,
65
SymbolIterator,
76
SymbolFor,
87
} = primordials;
98

10-
const kDestroyed = Symbol('kDestroyed');
11-
const kIsErrored = Symbol('kIsErrored');
12-
const kIsReadable = Symbol('kIsReadable');
13-
const kIsDisturbed = Symbol('kIsDisturbed');
9+
// We need to use SymbolFor to make these globally available
10+
// for interopt with readable-stream, i.e. readable-stream
11+
// and node core needs to be able to read/write private state
12+
// from each other for proper interoperability.
13+
const kIsDestroyed = SymbolFor('nodejs.stream.destroyed');
14+
const kIsErrored = SymbolFor('nodejs.stream.errored');
15+
const kIsReadable = SymbolFor('nodejs.stream.readable');
16+
const kIsWritable = SymbolFor('nodejs.stream.writable');
17+
const kIsDisturbed = SymbolFor('nodejs.stream.disturbed');
1418

1519
const kIsClosedPromise = SymbolFor('nodejs.webstream.isClosedPromise');
1620
const kControllerErrorFunction = SymbolFor('nodejs.webstream.controllerErrorFunction');
@@ -104,7 +108,7 @@ function isDestroyed(stream) {
104108
const wState = stream._writableState;
105109
const rState = stream._readableState;
106110
const state = wState || rState;
107-
return !!(stream.destroyed || stream[kDestroyed] || state?.destroyed);
111+
return !!(stream.destroyed || stream[kIsDestroyed] || state?.destroyed);
108112
}
109113

110114
// Have been end():d.
@@ -162,6 +166,7 @@ function isReadable(stream) {
162166
}
163167

164168
function isWritable(stream) {
169+
if (stream && stream[kIsWritable] != null) return stream[kIsWritable];
165170
if (typeof stream?.writable !== 'boolean') return null;
166171
if (isDestroyed(stream)) return false;
167172
return isWritableNodeStream(stream) &&
@@ -298,7 +303,8 @@ function isErrored(stream) {
298303
}
299304

300305
module.exports = {
301-
kDestroyed,
306+
isDestroyed,
307+
kIsDestroyed,
302308
isDisturbed,
303309
kIsDisturbed,
304310
isErrored,
@@ -307,8 +313,8 @@ module.exports = {
307313
kIsReadable,
308314
kIsClosedPromise,
309315
kControllerErrorFunction,
316+
kIsWritable,
310317
isClosed,
311-
isDestroyed,
312318
isDuplexNodeStream,
313319
isFinished,
314320
isIterable,
Collapse file

‎lib/stream.js‎

Copy file name to clipboardExpand all lines: lib/stream.js
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,13 @@ const promises = require('stream/promises');
5151
const utils = require('internal/streams/utils');
5252

5353
const Stream = module.exports = require('internal/streams/legacy').Stream;
54+
55+
Stream.isDestroyed = utils.isDestroyed;
5456
Stream.isDisturbed = utils.isDisturbed;
5557
Stream.isErrored = utils.isErrored;
5658
Stream.isReadable = utils.isReadable;
59+
Stream.isWritable = utils.isWritable;
60+
5761
Stream.Readable = require('internal/streams/readable');
5862
for (const key of ObjectKeys(streamReturningOperators)) {
5963
const op = streamReturningOperators[key];

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.