-
Notifications
You must be signed in to change notification settings - Fork 670
Expand file tree
/
Copy pathsandbox.ts
More file actions
183 lines (174 loc) · 6.25 KB
/
Copy pathsandbox.ts
File metadata and controls
183 lines (174 loc) · 6.25 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
import { ChildCommand, ParentCommand } from '../enums';
import { ChildMessage } from '../interfaces';
import { Child } from './child';
import { ChildPool } from './child-pool';
import { Job } from './job';
const sandbox = <T, R, N extends string>(
processFile: any,
childPool: ChildPool,
) => {
return async function process(
job: Job<T, R, N>,
token?: string,
signal?: AbortSignal,
): Promise<R> {
let child: Child;
let msgHandler: any;
let exitHandler: any;
let abortHandler: (() => void) | undefined;
try {
const done: Promise<R> = new Promise((resolve, reject) => {
const initChild = async () => {
try {
exitHandler = (exitCode: any, signal: any) => {
reject(
new Error(
'Unexpected exit code: ' + exitCode + ' signal: ' + signal,
),
);
};
child = await childPool.retain(processFile);
child.on('exit', exitHandler);
msgHandler = async (msg: ChildMessage) => {
try {
switch (msg.cmd) {
case ParentCommand.Completed:
resolve(msg.value);
break;
case ParentCommand.Failed:
case ParentCommand.Error: {
const err = new Error();
// ParentCommand.Failed carries the error under `value`,
// while ParentCommand.Error carries it under `err`. Read
// from either key so the failure reason is never lost.
Object.assign(err, msg.value ?? msg.err);
reject(err);
break;
}
case ParentCommand.Progress:
await job.updateProgress(msg.value);
break;
case ParentCommand.Log:
await job.log(msg.value);
break;
case ParentCommand.MoveToDelayed:
await job.moveToDelayed(
msg.value?.timestamp,
msg.value?.token,
);
break;
case ParentCommand.MoveToWait:
await job.moveToWait(msg.value?.token);
break;
case ParentCommand.MoveToWaitingChildren:
{
const value = await job.moveToWaitingChildren(
msg.value?.token,
msg.value?.opts,
);
child.send({
requestId: msg.requestId,
cmd: ChildCommand.MoveToWaitingChildrenResponse,
value,
});
}
break;
case ParentCommand.Update:
await job.updateData(msg.value);
break;
case ParentCommand.GetChildrenValues:
{
const value = await job.getChildrenValues();
child.send({
requestId: msg.requestId,
cmd: ChildCommand.GetChildrenValuesResponse,
value,
});
}
break;
case ParentCommand.GetIgnoredChildrenFailures:
{
const value = await job.getIgnoredChildrenFailures();
child.send({
requestId: msg.requestId,
cmd: ChildCommand.GetIgnoredChildrenFailuresResponse,
value,
});
}
break;
case ParentCommand.GetDependenciesCount:
{
const value = await job.getDependenciesCount(msg.value);
child.send({
requestId: msg.requestId,
cmd: ChildCommand.GetDependenciesCountResponse,
value,
});
}
break;
case ParentCommand.GetDependencies:
{
const value = await job.getDependencies(msg.value);
child.send({
requestId: msg.requestId,
cmd: ChildCommand.GetDependenciesResponse,
value,
});
}
break;
}
} catch (err) {
reject(err);
}
};
child.on('message', msgHandler);
child.send({
cmd: ChildCommand.Start,
job: job.asJSONSandbox(),
token,
});
if (signal) {
abortHandler = () => {
try {
child.send({
cmd: ChildCommand.Cancel,
value: signal.reason,
});
} catch {
// Child process may have already exited
}
};
if (signal.aborted) {
abortHandler();
} else {
signal.addEventListener('abort', abortHandler, { once: true });
}
}
} catch (error) {
reject(error);
}
};
initChild();
});
await done;
return done;
} finally {
// Note: There is a potential race where the signal is aborted between
// `await done` and this cleanup. This is safe because:
// 1. abortHandler has a try-catch for child process already exited
// 2. The listener is added with `once: true`, so it fires at most once
// 3. removeEventListener here is defensive cleanup only
if (signal && abortHandler) {
signal.removeEventListener('abort', abortHandler);
}
if (child) {
child.off('message', msgHandler);
child.off('exit', exitHandler);
if (child.exitCode === null && child.signalCode === null) {
childPool.release(child);
}
}
}
};
};
export default sandbox;