forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstructured_clone.js
48 lines (40 loc) · 1.07 KB
/
structured_clone.js
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'use strict';
const {
codes: { ERR_MISSING_ARGS },
} = require('internal/errors');
const {
lazyDOMException,
} = require('internal/util');
const {
MessageChannel,
receiveMessageOnPort,
} = require('internal/worker/io');
const {
isURL,
isURLSearchParams,
} = require('internal/url');
let channel;
function structuredClone(value, options = undefined) {
if (arguments.length === 0) {
throw new ERR_MISSING_ARGS('value');
}
if (isURL(value)) {
throw new lazyDOMException(
'URL: no structured serialize/deserialize support',
'DataCloneError');
} else if (isURLSearchParams(value)) {
throw new lazyDOMException(
'URLSearchParams: no structured serialize/deserialize support',
'DataCloneError');
}
// 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,
};