-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.ts
More file actions
64 lines (59 loc) · 2.24 KB
/
Copy pathrunner.ts
File metadata and controls
64 lines (59 loc) · 2.24 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
import { runApplicationPluginRunner } from './runner-host.js';
import type { PluginProcessEnvelope } from './protocol.js';
import { normalizePluginProcessError } from './error.js';
const IPC_SEND_TIMEOUT_MS = 2_000;
if (typeof process.send !== 'function') {
process.exit(1);
}
const listeners = new Set<(input: unknown) => void>();
const onMessage = (message: unknown) => {
for (const listener of [...listeners]) listener(message);
};
process.on('message', onMessage);
const runner = runApplicationPluginRunner({
transport: {
send(envelope: PluginProcessEnvelope) {
return new Promise<void>((resolve, reject) => {
if (!process.connected || typeof process.send !== 'function') {
reject(new Error('Application plugin IPC parent is disconnected'));
return;
}
let settled = false;
const timeout = setTimeout(() => {
if (settled) return;
settled = true;
reject(new Error('Application plugin IPC send timed out'));
}, IPC_SEND_TIMEOUT_MS);
const complete = (error: Error | null): void => {
if (settled) return;
settled = true;
clearTimeout(timeout);
if (error) reject(error);
else resolve();
};
try {
process.send(envelope, complete);
} catch (error) {
complete(normalizePluginProcessError(error));
}
});
},
subscribe(listener) {
listeners.add(listener);
return () => listeners.delete(listener);
},
close() {
if (process.connected) process.disconnect();
},
},
exit: ({ failed }) => process.exit(failed ? 1 : 0),
timers: {
setTimeout: (callback, milliseconds) => setTimeout(callback, milliseconds),
clearTimeout: (handle) => clearTimeout(handle as ReturnType<typeof setTimeout>),
},
});
process.on('uncaughtException', (error) => { void runner.fatal(error); });
process.on('unhandledRejection', (reason) => { void runner.fatal(reason); });
process.on('disconnect', () => { void runner.disconnect(); });
process.on('SIGINT', () => { void runner.fatal(new Error('Application plugin runner received SIGINT')); });
process.on('SIGTERM', () => { void runner.fatal(new Error('Application plugin runner received SIGTERM')); });