-
Notifications
You must be signed in to change notification settings - Fork 652
Expand file tree
/
Copy pathNodeProcessExecutionService.ts
More file actions
42 lines (36 loc) · 1.36 KB
/
NodeProcessExecutionService.ts
File metadata and controls
42 lines (36 loc) · 1.36 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
import type { BasePostMessageStream } from '@metamask/post-message-stream';
import { ProcessParentMessageStream } from '@metamask/post-message-stream/node';
import type { ChildProcess } from 'child_process';
import { fork } from 'child_process';
import type { TerminateJobArgs } from '..';
import { AbstractExecutionService } from '..';
export class NodeProcessExecutionService extends AbstractExecutionService<ChildProcess> {
protected async initEnvStream(): Promise<{
worker: ChildProcess;
stream: BasePostMessageStream;
}> {
const worker = fork(
require.resolve(
'@metamask/snaps-execution-environments/dist/browserify/node-process/bundle.js',
),
{
stdio: 'pipe',
},
);
// Capturing `stdout` and `stderr` from the worker prevents the worker from
// writing to them directly, making it easier to capture them Jest.
worker.stdout?.on('data', (data) => {
// eslint-disable-next-line no-console
console.log(data.toString());
});
worker.stderr?.on('data', (data) => {
// eslint-disable-next-line no-console
console.error(data.toString());
});
const stream = new ProcessParentMessageStream({ process: worker });
return Promise.resolve({ worker, stream });
}
protected terminateJob(jobWrapper: TerminateJobArgs<ChildProcess>): void {
jobWrapper.worker?.kill();
}
}