• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const { Error } = primordials;
3const {
4  messaging_deserialize_symbol,
5  messaging_transfer_symbol,
6  messaging_clone_symbol,
7  messaging_transfer_list_symbol
8} = internalBinding('symbols');
9const {
10  JSTransferable,
11  setDeserializerCreateObjectFunction
12} = internalBinding('messaging');
13
14function setup() {
15  // Register the handler that will be used when deserializing JS-based objects
16  // from .postMessage() calls. The format of `deserializeInfo` is generally
17  // 'module:Constructor', e.g. 'internal/fs/promises:FileHandle'.
18  setDeserializerCreateObjectFunction((deserializeInfo) => {
19    const [ module, ctor ] = deserializeInfo.split(':');
20    const Ctor = require(module)[ctor];
21    if (typeof Ctor !== 'function' ||
22        !(Ctor.prototype instanceof JSTransferable)) {
23      // Not one of the official errors because one should not be able to get
24      // here without messing with Node.js internals.
25      // eslint-disable-next-line no-restricted-syntax
26      throw new Error(`Unknown deserialize spec ${deserializeInfo}`);
27    }
28    return new Ctor();
29  });
30}
31
32module.exports = {
33  setup,
34  JSTransferable,
35  kClone: messaging_clone_symbol,
36  kDeserialize: messaging_deserialize_symbol,
37  kTransfer: messaging_transfer_symbol,
38  kTransferList: messaging_transfer_list_symbol
39};
40