-
Notifications
You must be signed in to change notification settings - Fork 652
Expand file tree
/
Copy pathNativeExecutionService.ts
More file actions
47 lines (42 loc) · 1.41 KB
/
NativeExecutionService.ts
File metadata and controls
47 lines (42 loc) · 1.41 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
import { NativeSnapExecutor } from '@metamask/snaps-execution-environments';
import type { Json } from '@metamask/utils';
import { Duplex } from 'readable-stream';
import {
AbstractExecutionService,
type TerminateJobArgs,
} from '../AbstractExecutionService';
export class NativeExecutionService extends AbstractExecutionService<NativeSnapExecutor> {
protected terminateJob(_job: TerminateJobArgs<NativeSnapExecutor>): void {
// no-op
}
protected async initEnvStream(
_snapId: string,
): Promise<{ worker: NativeSnapExecutor; stream: Duplex }> {
// TODO: Sanity check this.
const workerStream = new Duplex({
objectMode: true,
read() {
return undefined;
},
write(chunk: Json, encoding: BufferEncoding, callback) {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
stream.push(chunk, encoding);
callback();
},
});
const stream = new Duplex({
objectMode: true,
read() {
return undefined;
},
write(chunk: Json, encoding: BufferEncoding, callback) {
workerStream.push(chunk, encoding);
callback();
},
});
// NOTE: Initializes a Snap executor that runs in the same JS thread as the execution service.
// Does not provide process isolation.
const worker = NativeSnapExecutor.initialize(workerStream);
return Promise.resolve({ worker, stream });
}
}