All files / lib/internal structured_clone.js

100% Statements 29/29
80% Branches 4/5
100% Functions 1/1
100% Lines 29/29

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 3031x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 20x 20x 1x 1x 19x 19x 19x 19x 19x 19x 20x 20x 20x 31x 31x 31x 31x  
'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,
};