1'use strict'; 2 3const { 4 codes: { ERR_MISSING_ARGS }, 5} = require('internal/errors'); 6 7const { 8 MessageChannel, 9 receiveMessageOnPort, 10} = require('internal/worker/io'); 11 12let channel; 13function structuredClone(value, options = undefined) { 14 if (arguments.length === 0) { 15 throw new ERR_MISSING_ARGS('value'); 16 } 17 18 // TODO: Improve this with a more efficient solution that avoids 19 // instantiating a MessageChannel 20 channel ??= new MessageChannel(); 21 channel.port1.unref(); 22 channel.port2.unref(); 23 channel.port1.postMessage(value, options?.transfer); 24 return receiveMessageOnPort(channel.port2).message; 25} 26 27module.exports = { 28 structuredClone, 29}; 30