3
3
// This file contains process bootstrappers that can only be
4
4
// run in the worker thread.
5
5
const {
6
- getEnvMessagePort,
7
- threadId
6
+ getEnvMessagePort
8
7
} = internalBinding ( 'worker' ) ;
9
8
10
9
const {
11
- messageTypes,
12
- kStdioWantsMoreDataCallback,
13
10
kWaitingStreams,
14
11
ReadableWorkerStdio,
15
12
WritableWorkerStdio
@@ -18,15 +15,6 @@ const {
18
15
const {
19
16
codes : { ERR_WORKER_UNSUPPORTED_OPERATION }
20
17
} = require ( 'internal/errors' ) ;
21
-
22
- let debuglog ;
23
- function debug ( ...args ) {
24
- if ( ! debuglog ) {
25
- debuglog = require ( 'util' ) . debuglog ( 'worker' ) ;
26
- }
27
- return debuglog ( ...args ) ;
28
- }
29
-
30
18
const workerStdio = { } ;
31
19
32
20
function initializeWorkerStdio ( ) {
@@ -43,97 +31,6 @@ function initializeWorkerStdio() {
43
31
} ;
44
32
}
45
33
46
- function createMessageHandler ( port , prepareUserCodeExecution ) {
47
- const publicWorker = require ( 'worker_threads' ) ;
48
-
49
- return function ( message ) {
50
- if ( message . type === messageTypes . LOAD_SCRIPT ) {
51
- const {
52
- filename,
53
- doEval,
54
- workerData,
55
- publicPort,
56
- manifestSrc,
57
- manifestURL,
58
- hasStdin
59
- } = message ;
60
- if ( manifestSrc ) {
61
- require ( 'internal/process/policy' ) . setup ( manifestSrc , manifestURL ) ;
62
- }
63
- prepareUserCodeExecution ( ) ;
64
- publicWorker . parentPort = publicPort ;
65
- publicWorker . workerData = workerData ;
66
-
67
- if ( ! hasStdin )
68
- workerStdio . stdin . push ( null ) ;
69
-
70
- debug ( `[${ threadId } ] starts worker script ${ filename } ` +
71
- `(eval = ${ eval } ) at cwd = ${ process . cwd ( ) } ` ) ;
72
- port . unref ( ) ;
73
- port . postMessage ( { type : messageTypes . UP_AND_RUNNING } ) ;
74
- if ( doEval ) {
75
- const { evalScript } = require ( 'internal/process/execution' ) ;
76
- evalScript ( '[worker eval]' , filename ) ;
77
- } else {
78
- process . argv [ 1 ] = filename ; // script filename
79
- require ( 'module' ) . runMain ( ) ;
80
- }
81
- return ;
82
- } else if ( message . type === messageTypes . STDIO_PAYLOAD ) {
83
- const { stream, chunk, encoding } = message ;
84
- workerStdio [ stream ] . push ( chunk , encoding ) ;
85
- return ;
86
- } else if ( message . type === messageTypes . STDIO_WANTS_MORE_DATA ) {
87
- const { stream } = message ;
88
- workerStdio [ stream ] [ kStdioWantsMoreDataCallback ] ( ) ;
89
- return ;
90
- }
91
-
92
- require ( 'assert' ) . fail ( `Unknown worker message type ${ message . type } ` ) ;
93
- } ;
94
- }
95
-
96
- // XXX(joyeecheung): this has to be returned as an anonymous function
97
- // wrapped in a closure, see the comment of the original
98
- // process._fatalException in lib/internal/process/execution.js
99
- function createWorkerFatalExeception ( port ) {
100
- const {
101
- fatalException : originalFatalException
102
- } = require ( 'internal/process/execution' ) ;
103
-
104
- return ( error ) => {
105
- debug ( `[${ threadId } ] gets fatal exception` ) ;
106
- let caught = false ;
107
- try {
108
- caught = originalFatalException . call ( this , error ) ;
109
- } catch ( e ) {
110
- error = e ;
111
- }
112
- debug ( `[${ threadId } ] fatal exception caught = ${ caught } ` ) ;
113
-
114
- if ( ! caught ) {
115
- let serialized ;
116
- try {
117
- const { serializeError } = require ( 'internal/error-serdes' ) ;
118
- serialized = serializeError ( error ) ;
119
- } catch { }
120
- debug ( `[${ threadId } ] fatal exception serialized = ${ ! ! serialized } ` ) ;
121
- if ( serialized )
122
- port . postMessage ( {
123
- type : messageTypes . ERROR_MESSAGE ,
124
- error : serialized
125
- } ) ;
126
- else
127
- port . postMessage ( { type : messageTypes . COULD_NOT_SERIALIZE_ERROR } ) ;
128
-
129
- const { clearAsyncIdStack } = require ( 'internal/async_hooks' ) ;
130
- clearAsyncIdStack ( ) ;
131
-
132
- process . exit ( ) ;
133
- }
134
- } ;
135
- }
136
-
137
34
// The execution of this function itself should not cause any side effects.
138
35
function wrapProcessMethods ( binding ) {
139
36
function umask ( mask ) {
@@ -150,7 +47,5 @@ function wrapProcessMethods(binding) {
150
47
151
48
module . exports = {
152
49
initializeWorkerStdio,
153
- createMessageHandler,
154
- createWorkerFatalExeception,
155
50
wrapProcessMethods
156
51
} ;
0 commit comments