Skip to content

Commit 9480e1b

Browse files
joyeecheungBridgeAR
authored andcommitted
process: split worker IO into internal/worker/io.js
- Move `setupProcessStdio` which contains write access to the process object into `bootstrap/node.js` - Move `MessagePort`, `MessageChannel`, `ReadableWorkerStdio`, and `WritableWorkerStdio` into `internal/worker/io.js` - Move more worker-specific bootstrap code into `internal/process/worker_thread_only` from `setupChild` in `internal/worker.js`, and move the `process._fatalException` overwrite into `bootstrap/node.js` for clarity. PR-URL: #25199 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 456b1b5 commit 9480e1b

8 files changed

Lines changed: 358 additions & 299 deletions

File tree

‎lib/internal/bootstrap/node.js‎

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,13 @@ function startup() {
140140
}
141141

142142
if(isMainThread){
143-
mainThreadSetup.setupStdio();
143+
const{ getStdout, getStdin, getStderr }=
144+
NativeModule.require('internal/process/stdio').getMainThreadStdio();
145+
setupProcessStdio(getStdout,getStdin,getStderr);
144146
}else{
145-
workerThreadSetup.setupStdio();
147+
const{ getStdout, getStdin, getStderr }=
148+
workerThreadSetup.initializeWorkerStdio();
149+
setupProcessStdio(getStdout,getStdin,getStderr);
146150
}
147151

148152
if(global.__coverage__)
@@ -318,8 +322,14 @@ function startup() {
318322
functionstartExecution(){
319323
// This means we are in a Worker context, and any script execution
320324
// will be directed by the worker module.
321-
if(internalBinding('worker').getEnvMessagePort()!==undefined){
322-
NativeModule.require('internal/worker').setupChild();
325+
if(!isMainThread){
326+
constworkerThreadSetup=NativeModule.require(
327+
'internal/process/worker_thread_only'
328+
);
329+
// Set up the message port and start listening
330+
const{ workerFatalExeception }=workerThreadSetup.setup();
331+
// Overwrite fatalException
332+
process._fatalException=workerFatalExeception;
323333
return;
324334
}
325335

@@ -511,6 +521,31 @@ function setupProcessObject() {
511521
EventEmitter.call(process);
512522
}
513523

524+
functionsetupProcessStdio(getStdout,getStdin,getStderr){
525+
Object.defineProperty(process,'stdout',{
526+
configurable: true,
527+
enumerable: true,
528+
get: getStdout
529+
});
530+
531+
Object.defineProperty(process,'stderr',{
532+
configurable: true,
533+
enumerable: true,
534+
get: getStderr
535+
});
536+
537+
Object.defineProperty(process,'stdin',{
538+
configurable: true,
539+
enumerable: true,
540+
get: getStdin
541+
});
542+
543+
process.openStdin=function(){
544+
process.stdin.resume();
545+
returnprocess.stdin;
546+
};
547+
}
548+
514549
functionsetupGlobalVariables(){
515550
Object.defineProperty(global,Symbol.toStringTag,{
516551
value: 'global',

‎lib/internal/process/main_thread_only.js‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,6 @@ const {
1616
validateString
1717
}=require('internal/validators');
1818

19-
const{
20-
setupProcessStdio,
21-
getMainThreadStdio
22-
}=require('internal/process/stdio');
23-
24-
functionsetupStdio(){
25-
setupProcessStdio(getMainThreadStdio());
26-
}
27-
2819
// The execution of this function itself should not cause any side effects.
2920
functionwrapProcessMethods(binding){
3021
functionchdir(directory){
@@ -174,7 +165,6 @@ function setupChildProcessIpcChannel() {
174165
}
175166

176167
module.exports={
177-
setupStdio,
178168
wrapProcessMethods,
179169
setupSignalHandlers,
180170
setupChildProcessIpcChannel,

‎lib/internal/process/stdio.js‎

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
exports.setupProcessStdio=setupProcessStdio;
43
exports.getMainThreadStdio=getMainThreadStdio;
54

65
functiondummyDestroy(err,cb){cb(err);}
@@ -134,31 +133,6 @@ function getMainThreadStdio() {
134133
};
135134
}
136135

137-
functionsetupProcessStdio({ getStdout, getStdin, getStderr }){
138-
Object.defineProperty(process,'stdout',{
139-
configurable: true,
140-
enumerable: true,
141-
get: getStdout
142-
});
143-
144-
Object.defineProperty(process,'stderr',{
145-
configurable: true,
146-
enumerable: true,
147-
get: getStderr
148-
});
149-
150-
Object.defineProperty(process,'stdin',{
151-
configurable: true,
152-
enumerable: true,
153-
get: getStdin
154-
});
155-
156-
process.openStdin=function(){
157-
process.stdin.resume();
158-
returnprocess.stdin;
159-
};
160-
}
161-
162136
functioncreateWritableStdioStream(fd){
163137
varstream;
164138
consttty_wrap=internalBinding('tty_wrap');

‎lib/internal/process/worker_thread_only.js‎

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,54 @@
22

33
// This file contains process bootstrappers that can only be
44
// run in the worker thread.
5+
const{
6+
getEnvMessagePort,
7+
threadId
8+
}=internalBinding('worker');
9+
10+
constdebug=require('util').debuglog('worker');
511

612
const{
7-
setupProcessStdio
8-
}=require('internal/process/stdio');
13+
kWaitingStreams,
14+
ReadableWorkerStdio,
15+
WritableWorkerStdio
16+
}=require('internal/worker/io');
917

1018
const{
11-
workerStdio
19+
createMessageHandler,
20+
createWorkerFatalExeception
1221
}=require('internal/worker');
1322

14-
functionsetupStdio(){
15-
setupProcessStdio({
16-
getStdout: ()=>workerStdio.stdout,
17-
getStderr: ()=>workerStdio.stderr,
18-
getStdin: ()=>workerStdio.stdin
19-
});
23+
constworkerStdio={};
24+
25+
functioninitializeWorkerStdio(){
26+
constport=getEnvMessagePort();
27+
port[kWaitingStreams]=0;
28+
workerStdio.stdin=newReadableWorkerStdio(port,'stdin');
29+
workerStdio.stdout=newWritableWorkerStdio(port,'stdout');
30+
workerStdio.stderr=newWritableWorkerStdio(port,'stderr');
31+
32+
return{
33+
getStdout(){returnworkerStdio.stdout;},
34+
getStderr(){returnworkerStdio.stderr;},
35+
getStdin(){returnworkerStdio.stdin;}
36+
};
37+
}
38+
39+
functionsetup(){
40+
debug(`[${threadId}] is setting up worker child environment`);
41+
42+
constport=getEnvMessagePort();
43+
constpublicWorker=require('worker_threads');
44+
port.on('message',createMessageHandler(publicWorker,port,workerStdio));
45+
port.start();
46+
47+
return{
48+
workerFatalExeception: createWorkerFatalExeception(port)
49+
};
2050
}
2151

2252
module.exports={
23-
setupStdio
53+
initializeWorkerStdio,
54+
setup
2455
};

0 commit comments

Comments
 (0)