|
2 | 2 |
|
3 | 3 | const { getOptionValue } = require('internal/options'); |
4 | 4 |
|
| 5 | +function prepareMainThreadExecution() { |
| 6 | + // If the process is spawned with env NODE_CHANNEL_FD, it's probably |
| 7 | + // spawned by our child_process module, then initialize IPC. |
| 8 | + // This attaches some internal event listeners and creates: |
| 9 | + // process.send(), process.channel, process.connected, |
| 10 | + // process.disconnect(). |
| 11 | + setupChildProcessIpcChannel(); |
| 12 | + |
| 13 | + // Load policy from disk and parse it. |
| 14 | + initializePolicy(); |
| 15 | + |
| 16 | + // If this is a worker in cluster mode, start up the communication |
| 17 | + // channel. This needs to be done before any user code gets executed |
| 18 | + // (including preload modules). |
| 19 | + initializeClusterIPC(); |
| 20 | + |
| 21 | + initializeDeprecations(); |
| 22 | + initializeESMLoader(); |
| 23 | + loadPreloadModules(); |
| 24 | +} |
| 25 | + |
5 | 26 | // In general deprecations are intialized wherever the APIs are implemented, |
6 | 27 | // this is used to deprecate APIs implemented in C++ where the deprecation |
7 | 28 | // utitlities are not easily accessible. |
@@ -41,10 +62,22 @@ function initializeDeprecations() { |
41 | 62 | } |
42 | 63 | } |
43 | 64 |
|
| 65 | +function setupChildProcessIpcChannel() { |
| 66 | + if (process.env.NODE_CHANNEL_FD) { |
| 67 | + const assert = require('internal/assert'); |
| 68 | + |
| 69 | + const fd = parseInt(process.env.NODE_CHANNEL_FD, 10); |
| 70 | + assert(fd >= 0); |
| 71 | + |
| 72 | + // Make sure it's not accidentally inherited by child processes. |
| 73 | + delete process.env.NODE_CHANNEL_FD; |
| 74 | + |
| 75 | + require('child_process')._forkChild(fd); |
| 76 | + assert(process.send); |
| 77 | + } |
| 78 | +} |
| 79 | + |
44 | 80 | function initializeClusterIPC() { |
45 | | - // If this is a worker in cluster mode, start up the communication |
46 | | - // channel. This needs to be done before any user code gets executed |
47 | | - // (including preload modules). |
48 | 81 | if (process.argv[1] && process.env.NODE_UNIQUE_ID) { |
49 | 82 | const cluster = require('cluster'); |
50 | 83 | cluster._setupWorker(); |
@@ -114,9 +147,8 @@ function loadPreloadModules() { |
114 | 147 | } |
115 | 148 |
|
116 | 149 | module.exports = { |
| 150 | + prepareMainThreadExecution, |
117 | 151 | initializeDeprecations, |
118 | | - initializeClusterIPC, |
119 | | - initializePolicy, |
120 | 152 | initializeESMLoader, |
121 | 153 | loadPreloadModules |
122 | 154 | }; |
0 commit comments