forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchild_process.ts
More file actions
339 lines (275 loc) · 7.71 KB
/
child_process.ts
File metadata and controls
339 lines (275 loc) · 7.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// eslint-disable-next-line max-classes-per-file
import { EventEmitter } from 'events';
import { protocolAndHost } from '@codesandbox/common/lib/utils/url-generator';
import { commonPostMessage } from '@codesandbox/common/lib/utils/global';
import _debug from '@codesandbox/common/lib/utils/debug';
const debug = _debug('cs:node:child_process');
const isSafari =
typeof navigator !== 'undefined' &&
/^((?!chrome|android).)*safari/i.test(navigator.userAgent);
let DefaultWorker: false | (() => Worker);
const workerMap: Map<string, false | (() => Worker)> = new Map();
function addDefaultForkHandler(worker: false | (() => Worker)) {
DefaultWorker = worker;
}
function addForkHandler(path: string, worker: false | (() => Worker)) {
workerMap.set(path, worker);
}
interface IProcessOpts {
silent?: boolean;
detached?: boolean;
execArgv?: string[];
cwd?: string;
env?: {
[key: string]: any;
};
}
class Stream extends EventEmitter {
constructor(private worker: Worker) {
super();
}
setEncoding() {}
write(message: string) {
this.worker.postMessage({ $type: 'input-write', $data: message });
}
}
class NullStream extends EventEmitter {
setEncoding() {}
}
class NullChildProcess extends EventEmitter {
public stdout: NullStream = new NullStream();
public stderr: NullStream = new NullStream();
public stdin: NullStream = new NullStream();
public kill() {}
}
class ChildProcess extends EventEmitter {
public stdout: Stream;
public stderr: Stream;
public stdin: Stream;
private destroyed = false;
constructor(private worker: Worker) {
super();
this.stdout = new Stream(worker);
this.stderr = new Stream(worker);
this.stdin = new Stream(worker);
this.listen();
}
public send(message: any, _a: any, _b: any, callback: (arg: any) => void) {
if (this.destroyed) {
callback(new Error('This connection has been killed'));
return;
}
const m = {
$type: 'message',
$data: JSON.stringify(message),
};
this.worker.postMessage(m);
if (typeof _a === 'function') {
_a(null);
} else if (typeof _b === 'function') {
_b(null);
} else if (typeof callback === 'function') {
callback(null);
}
}
public kill() {
this.destroyed = true;
this.worker.removeEventListener('message', this.listener.bind(this));
this.worker.terminate();
}
private listener(message: MessageEvent) {
const data = message.data.$data;
if (data) {
switch (message.data.$type) {
case 'stdout':
this.stdout.emit('data', data);
break;
case 'message':
this.emit('message', JSON.parse(data));
break;
default:
break;
}
}
}
private listen() {
this.worker.addEventListener('message', this.listener.bind(this));
}
}
const cachedWorkers: { [path: string]: Array<Worker | false> } = {};
const cachedDefaultWorkers: Array<Worker | false> = [];
function getWorker(path: string) {
let WorkerConstructor = workerMap.get(path);
if (!WorkerConstructor) {
WorkerConstructor = DefaultWorker;
// Explicitly ignore
if (WorkerConstructor === false) {
return false;
}
if (WorkerConstructor == null) {
throw new Error('No worker set for path: ' + path);
}
}
const worker = WorkerConstructor();
// Register file system that syncs with filesystem in manager
// BrowserFS.FileSystem.WorkerFS.attachRemoteListener(worker);
return worker;
}
function getWorkerFromCache(path: string, isDefaultWorker: boolean) {
if (isDefaultWorker) {
const cachedDefaultWorker = cachedDefaultWorkers.pop();
if (cachedDefaultWorker) {
return cachedDefaultWorker;
}
} else if (cachedWorkers[path]) {
return cachedWorkers[path].pop();
}
return undefined;
}
const sentBroadcasts: Map<string, number[]> = new Map();
/**
* Broadcasts a message if it hasn't been sent by this worker/window before
*/
function handleBroadcast(
path: string,
target: Worker | Window,
data: {
$id?: number;
$data: object;
$type: string;
}
) {
const sentBroadcastsForPath = sentBroadcasts.get(path) || [];
if (data.$id && sentBroadcastsForPath.indexOf(data.$id) > -1) {
return;
}
data.$id = data.$id || Math.floor(Math.random() * 100000000);
if (sentBroadcastsForPath.length > 100) {
sentBroadcastsForPath.shift();
}
sentBroadcastsForPath.push(data.$id);
if (
// @ts-ignore This check is for the subworker polyfill, if it has an id it's polyfilled by subworkers and indeed a worker
target.id ||
target.constructor.name === 'Worker' ||
// @ts-ignore Unknown to TS
(typeof DedicatedWorkerGlobalScope !== 'undefined' &&
// @ts-ignore Unknown to TS
target instanceof DedicatedWorkerGlobalScope)
) {
// @ts-ignore
target.postMessage(data);
} else {
(target as Window).postMessage(data, protocolAndHost());
}
sentBroadcasts.set(path, sentBroadcastsForPath);
}
function fork(path: string, argv?: string[], processOpts?: IProcessOpts) {
const WorkerConstructor = workerMap.get(path);
const isDefaultWorker = !WorkerConstructor;
const worker = getWorkerFromCache(path, isDefaultWorker) || getWorker(path);
if (worker === false) {
return new NullChildProcess();
}
debug('Forking', path);
const WORKER_ID = path + '-' + Math.floor(Math.random() * 100000);
self.addEventListener('message', ((e: MessageEvent) => {
const { data } = e;
if (data.$broadcast) {
handleBroadcast(WORKER_ID, worker, data);
return;
}
if (!data.$sang && data.$type) {
const newData = {
$sang: true,
$data: data,
};
worker.postMessage(newData);
}
}) as EventListener);
worker.addEventListener('message', e => {
const { data } = e;
if (data.$broadcast) {
handleBroadcast(WORKER_ID, self, data);
return;
}
if (!data.$sang && data.$type) {
const newData = {
$sang: true,
$data: data,
};
commonPostMessage(newData);
}
});
const data: any = {
entry: path,
argv: argv || [],
};
if (processOpts) {
data.env = processOpts.env;
data.cwd = processOpts.cwd;
data.execArgv = processOpts.execArgv;
}
if (isSafari) {
// For Safari it takes a while until the worker started, so we listen for ready message
// and send a message anyway if a second passes
let sentReady = false;
const timeout = setTimeout(() => {
if (!sentReady) {
worker.postMessage({
$type: 'worker-manager',
$event: 'init',
data,
});
sentReady = true;
}
}, 1500);
worker.addEventListener('message', e => {
if (!sentReady && e.data && e.data.$type === 'ready') {
worker.postMessage({
$type: 'worker-manager',
$event: 'init',
data,
});
clearTimeout(timeout);
sentReady = true;
}
});
} else {
worker.postMessage({
$type: 'worker-manager',
$event: 'init',
data,
});
}
return new ChildProcess(worker);
}
function preloadWorker(path: string) {
const WorkerConstructor = workerMap.get(path);
const isDefaultWorker = !WorkerConstructor;
const worker = getWorker(path);
if (isDefaultWorker) {
cachedDefaultWorkers.push(worker);
} else {
cachedWorkers[path] = cachedWorkers[path] || [];
cachedWorkers[path].push(worker);
}
}
function execFileSync(path: string) {
if (process.env.NODE_ENV === 'development') {
debug('EXEC_FILE_SYNC', path);
}
}
function execSync(path: string) {
if (process.env.NODE_ENV === 'development') {
debug('EXEC_SYNC', path);
}
}
export {
addForkHandler,
addDefaultForkHandler,
preloadWorker,
fork,
execSync,
execFileSync,
};