All files / core/src/thread transfer.ts

100% Statements 26/26
100% Branches 3/3
100% Functions 2/2
100% Lines 26/26

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 271x 1x 1x 1x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 81x 81x 62x 81x 81x 81x 81x 62x 62x  
import { expose, ProxyMarked, transferHandlers, wrap } from 'comlink';
import { TypeOf } from '../utility/type-of';
 
if (TypeOf.process(globalThis.process)) {
  const nodeEndpoint = require('comlink/dist/umd/node-adapter');
  const { MessageChannel } = require('worker_threads');
 
  /**
   * Alteration of the {@link transferHandlers}, allowing values to be proxied
   * between endpoints under NodeJS.
   *
   * @remarks https://github.com/GoogleChromeLabs/comlink/issues/313
   */
  transferHandlers.set('proxy', {
    // eslint-disable-next-line @typescript-eslint/unbound-method
    canHandle: transferHandlers.get('proxy')!.canHandle,
    deserialize: (value: MessagePort) => {
      return wrap(nodeEndpoint(value));
    },
    serialize: (value: ProxyMarked) => {
      const { port1, port2 } = new MessageChannel();
      expose(value, nodeEndpoint(port1));
      return [port2, [port2]];
    }
  });
}