|
1 | | -import { realpathSync } from 'node:fs' |
| 1 | +import { realpathSync, writeFileSync } from 'node:fs' |
2 | 2 | import path from 'node:path' |
3 | 3 | import process from 'node:process' |
4 | 4 |
|
@@ -103,27 +103,32 @@ const LAZY_IPC = (() => { |
103 | 103 | _ipc[key] = false |
104 | 104 | defineGetter(ipc, key, () => _ipc[key]) |
105 | 105 | } |
106 | | - void new Promise<void>(resolve => { |
107 | | - const onmessage = (ipcData_: Serializable) => { |
108 | | - const ipcData: { [key: string]: any } = { |
109 | | - __proto__: null, |
110 | | - ...(isObject(ipcData_) ? ipcData_ : {}) |
| 106 | + // A forked subprocess will have the 'send' method. |
| 107 | + // https://nodejs.org/api/child_process.html#subprocesssendmessage-sendhandle-options-callback |
| 108 | + if (typeof process.send === 'function') { |
| 109 | + void new Promise<void>(resolve => { |
| 110 | + const onmessage = (ipcData_: Serializable) => { |
| 111 | + finish() |
| 112 | + const ipcData: { [key: string]: any } = { |
| 113 | + __proto__: null, |
| 114 | + ...(isObject(ipcData_) ? ipcData_ : {}) |
| 115 | + } |
| 116 | + for (const key of keys) { |
| 117 | + _ipc[key] = ipcData[key] |
| 118 | + } |
111 | 119 | } |
112 | | - for (const key of keys) { |
113 | | - _ipc[key] = ipcData[key] |
114 | | - } |
115 | | - resolve() |
116 | | - } |
117 | | - process.once('message', onmessage) |
118 | | - abortSignal.addEventListener( |
119 | | - 'abort', |
120 | | - () => { |
| 120 | + const finish = () => { |
| 121 | + abortSignal.removeEventListener('abort', finish) |
121 | 122 | process.removeListener('message', onmessage) |
122 | 123 | resolve() |
123 | | - }, |
124 | | - { once: true } |
125 | | - ) |
126 | | - }) |
| 124 | + } |
| 125 | + abortSignal.addEventListener('abort', finish, { once: true }) |
| 126 | + process.on('message', onmessage) |
| 127 | + // The timeout of 100ms is to prevent an unresolved promised. It should be |
| 128 | + // more than enough time for the IPC handshake. |
| 129 | + setTimeout(finish, 100) |
| 130 | + }) |
| 131 | + } |
127 | 132 | return () => Object.freeze(ipc) |
128 | 133 | })() |
129 | 134 |
|
|
0 commit comments