Skip to content

Commit c598877

Browse files
ronagdanielleadams
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 3b524cb commit c598877

3 files changed

Lines changed: 20 additions & 10 deletions

File tree

‎lib/internal/streams/destroy.js‎

Lines 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

‎lib/internal/streams/utils.js‎

Lines 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-
constkDestroyed=Symbol('kDestroyed');
11-
constkIsErrored=Symbol('kIsErrored');
12-
constkIsReadable=Symbol('kIsReadable');
13-
constkIsDisturbed=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+
constkIsDestroyed=SymbolFor('nodejs.stream.destroyed');
14+
constkIsErrored=SymbolFor('nodejs.stream.errored');
15+
constkIsReadable=SymbolFor('nodejs.stream.readable');
16+
constkIsWritable=SymbolFor('nodejs.stream.writable');
17+
constkIsDisturbed=SymbolFor('nodejs.stream.disturbed');
1418

1519
constkIsClosedPromise=SymbolFor('nodejs.webstream.isClosedPromise');
1620
constkControllerErrorFunction=SymbolFor('nodejs.webstream.controllerErrorFunction');
@@ -104,7 +108,7 @@ function isDestroyed(stream) {
104108
constwState=stream._writableState;
105109
constrState=stream._readableState;
106110
conststate=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
functionisWritable(stream){
169+
if(stream&&stream[kIsWritable]!=null)returnstream[kIsWritable];
165170
if(typeofstream?.writable!=='boolean')returnnull;
166171
if(isDestroyed(stream))returnfalse;
167172
returnisWritableNodeStream(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,

‎lib/stream.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,13 @@ const promises = require('stream/promises');
5151
constutils=require('internal/streams/utils');
5252

5353
constStream=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(constkeyofObjectKeys(streamReturningOperators)){
5963
constop=streamReturningOperators[key];

0 commit comments

Comments
 (0)