summaryrefslogtreecommitdiff
path: root/lib/internal/structured_clone.js
blob: 0392232badf9fcab8472e93978b75b2b5c183e05 (plain)
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
'use strict';

const {
  codes: { ERR_MISSING_ARGS },
} = require('internal/errors');

const {
  MessageChannel,
  receiveMessageOnPort,
} = require('internal/worker/io');

let channel;
function structuredClone(value, options = undefined) {
  if (arguments.length === 0) {
    throw new ERR_MISSING_ARGS('value');
  }

  // TODO: Improve this with a more efficient solution that avoids
  // instantiating a MessageChannel
  channel ??= new MessageChannel();
  channel.port1.unref();
  channel.port2.unref();
  channel.port1.postMessage(value, options?.transfer);
  return receiveMessageOnPort(channel.port2).message;
}

module.exports = {
  structuredClone,
};