Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 3x 1x 2x 1x 1x 1x 1x | import { isJsonRpcMessage } from '@metamask/kernel-utils';
import type { JsonRpcMessage } from '@metamask/kernel-utils';
import { splitLoggerStream } from '@metamask/logger';
import { NodeWorkerDuplexStream } from '@metamask/streams';
import type { DuplexStream } from '@metamask/streams';
import { parentPort } from 'node:worker_threads';
import type { MessagePort as NodePort } from 'node:worker_threads';
/**
* Return the parent port of the Node.js worker if it exists; otherwise throw.
*
* @returns The parent port.
* @throws If not called from within a Node.js worker.
*/
export function getPort(): NodePort {
if (!parentPort) {
throw new Error('Expected to run in a Node.js worker with parentPort.');
}
return parentPort;
}
/**
* When called from within Node.js worker, returns a DuplexStream which
* communicates over the parentPort.
*
* @returns A pair of NodeWorkerDuplexStreams
*/
export async function makeStreams(): Promise<{
kernelStream: DuplexStream<JsonRpcMessage>;
loggerStream: DuplexStream<JsonRpcMessage>;
}> {
const stream = new NodeWorkerDuplexStream<JsonRpcMessage>(
getPort(),
isJsonRpcMessage,
);
const { kernelStream, loggerStream } = splitLoggerStream(stream);
await stream.synchronize();
return { kernelStream, loggerStream };
}
|