File tree 2 files changed +46
-0
lines changed
2 files changed +46
-0
lines changed Original file line number Diff line number Diff line change @@ -184,6 +184,8 @@ class Worker extends EventEmitter {
184
184
}
185
185
186
186
postMessage ( ...args ) {
187
+ if ( this [ kPublicPort ] === null ) return ;
188
+
187
189
this [ kPublicPort ] . postMessage ( ...args ) ;
188
190
}
189
191
@@ -219,14 +221,20 @@ class Worker extends EventEmitter {
219
221
}
220
222
221
223
get stdin ( ) {
224
+ if ( this [ kParentSideStdio ] === null ) return null ;
225
+
222
226
return this [ kParentSideStdio ] . stdin ;
223
227
}
224
228
225
229
get stdout ( ) {
230
+ if ( this [ kParentSideStdio ] === null ) return null ;
231
+
226
232
return this [ kParentSideStdio ] . stdout ;
227
233
}
228
234
229
235
get stderr ( ) {
236
+ if ( this [ kParentSideStdio ] === null ) return null ;
237
+
230
238
return this [ kParentSideStdio ] . stderr ;
231
239
}
232
240
}
Original file line number Diff line number Diff line change
1
+ 'use strict' ;
2
+
3
+ const common = require ( '../common' ) ;
4
+
5
+ const assert = require ( 'assert' ) ;
6
+ const { Worker, isMainThread } = require ( 'worker_threads' ) ;
7
+
8
+ if ( isMainThread ) {
9
+ const w = new Worker ( __filename , {
10
+ stdin : true ,
11
+ stdout : true ,
12
+ stderr : true
13
+ } ) ;
14
+
15
+ w . on ( 'exit' , common . mustCall ( ( code ) => {
16
+ assert . strictEqual ( code , 0 ) ;
17
+
18
+ // `postMessage` should not throw after termination
19
+ // (this mimics the browser behavior).
20
+ w . postMessage ( 'foobar' ) ;
21
+ w . ref ( ) ;
22
+ w . unref ( ) ;
23
+
24
+ // Although not browser specific, probably wise to
25
+ // make sure the stream getters don't throw either.
26
+ w . stdin ;
27
+ w . stdout ;
28
+ w . stderr ;
29
+
30
+ // Sanity check.
31
+ assert . strictEqual ( w . threadId , - 1 ) ;
32
+ assert . strictEqual ( w . stdin , null ) ;
33
+ assert . strictEqual ( w . stdout , null ) ;
34
+ assert . strictEqual ( w . stderr , null ) ;
35
+ } ) ) ;
36
+ } else {
37
+ process . exit ( 0 ) ;
38
+ }
You can’t perform that action at this time.
0 commit comments