web.structured-clone.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. var IS_PURE = require('../internals/is-pure');
  2. var $ = require('../internals/export');
  3. var global = require('../internals/global');
  4. var getBuiltin = require('../internals/get-built-in');
  5. var uncurryThis = require('../internals/function-uncurry-this');
  6. var fails = require('../internals/fails');
  7. var uid = require('../internals/uid');
  8. var isCallable = require('../internals/is-callable');
  9. var isConstructor = require('../internals/is-constructor');
  10. var isNullOrUndefined = require('../internals/is-null-or-undefined');
  11. var isObject = require('../internals/is-object');
  12. var isSymbol = require('../internals/is-symbol');
  13. var iterate = require('../internals/iterate');
  14. var anObject = require('../internals/an-object');
  15. var classof = require('../internals/classof');
  16. var hasOwn = require('../internals/has-own-property');
  17. var createProperty = require('../internals/create-property');
  18. var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
  19. var lengthOfArrayLike = require('../internals/length-of-array-like');
  20. var validateArgumentsLength = require('../internals/validate-arguments-length');
  21. var getRegExpFlags = require('../internals/regexp-get-flags');
  22. var ERROR_STACK_INSTALLABLE = require('../internals/error-stack-installable');
  23. var V8 = require('../internals/engine-v8-version');
  24. var IS_BROWSER = require('../internals/engine-is-browser');
  25. var IS_DENO = require('../internals/engine-is-deno');
  26. var IS_NODE = require('../internals/engine-is-node');
  27. var Object = global.Object;
  28. var Date = global.Date;
  29. var Error = global.Error;
  30. var EvalError = global.EvalError;
  31. var RangeError = global.RangeError;
  32. var ReferenceError = global.ReferenceError;
  33. var SyntaxError = global.SyntaxError;
  34. var TypeError = global.TypeError;
  35. var URIError = global.URIError;
  36. var PerformanceMark = global.PerformanceMark;
  37. var WebAssembly = global.WebAssembly;
  38. var CompileError = WebAssembly && WebAssembly.CompileError || Error;
  39. var LinkError = WebAssembly && WebAssembly.LinkError || Error;
  40. var RuntimeError = WebAssembly && WebAssembly.RuntimeError || Error;
  41. var DOMException = getBuiltin('DOMException');
  42. var Set = getBuiltin('Set');
  43. var Map = getBuiltin('Map');
  44. var MapPrototype = Map.prototype;
  45. var mapHas = uncurryThis(MapPrototype.has);
  46. var mapGet = uncurryThis(MapPrototype.get);
  47. var mapSet = uncurryThis(MapPrototype.set);
  48. var setAdd = uncurryThis(Set.prototype.add);
  49. var objectKeys = getBuiltin('Object', 'keys');
  50. var push = uncurryThis([].push);
  51. var thisBooleanValue = uncurryThis(true.valueOf);
  52. var thisNumberValue = uncurryThis(1.0.valueOf);
  53. var thisStringValue = uncurryThis(''.valueOf);
  54. var thisTimeValue = uncurryThis(Date.prototype.getTime);
  55. var PERFORMANCE_MARK = uid('structuredClone');
  56. var DATA_CLONE_ERROR = 'DataCloneError';
  57. var TRANSFERRING = 'Transferring';
  58. var checkBasicSemantic = function (structuredCloneImplementation) {
  59. return !fails(function () {
  60. var set1 = new global.Set([7]);
  61. var set2 = structuredCloneImplementation(set1);
  62. var number = structuredCloneImplementation(Object(7));
  63. return set2 == set1 || !set2.has(7) || typeof number != 'object' || number != 7;
  64. }) && structuredCloneImplementation;
  65. };
  66. var checkErrorsCloning = function (structuredCloneImplementation, $Error) {
  67. return !fails(function () {
  68. var error = new $Error();
  69. var test = structuredCloneImplementation({ a: error, b: error });
  70. return !(test && test.a === test.b && test.a instanceof $Error && test.a.stack === error.stack);
  71. });
  72. };
  73. // https://github.com/whatwg/html/pull/5749
  74. var checkNewErrorsCloningSemantic = function (structuredCloneImplementation) {
  75. return !fails(function () {
  76. var test = structuredCloneImplementation(new global.AggregateError([1], PERFORMANCE_MARK, { cause: 3 }));
  77. return test.name != 'AggregateError' || test.errors[0] != 1 || test.message != PERFORMANCE_MARK || test.cause != 3;
  78. });
  79. };
  80. // FF94+, Safari 15.4+, Chrome 98+, NodeJS 17.0+, Deno 1.13+
  81. // FF<103 and Safari implementations can't clone errors
  82. // https://bugzilla.mozilla.org/show_bug.cgi?id=1556604
  83. // FF103 can clone errors, but `.stack` of clone is an empty string
  84. // https://bugzilla.mozilla.org/show_bug.cgi?id=1778762
  85. // FF104+ fixed it on usual errors, but not on DOMExceptions
  86. // https://bugzilla.mozilla.org/show_bug.cgi?id=1777321
  87. // Chrome <102 returns `null` if cloned object contains multiple references to one error
  88. // https://bugs.chromium.org/p/v8/issues/detail?id=12542
  89. // NodeJS implementation can't clone DOMExceptions
  90. // https://github.com/nodejs/node/issues/41038
  91. // only FF103+ supports new (html/5749) error cloning semantic
  92. var nativeStructuredClone = global.structuredClone;
  93. var FORCED_REPLACEMENT = IS_PURE
  94. || !checkErrorsCloning(nativeStructuredClone, Error)
  95. || !checkErrorsCloning(nativeStructuredClone, DOMException)
  96. || !checkNewErrorsCloningSemantic(nativeStructuredClone);
  97. // Chrome 82+, Safari 14.1+, Deno 1.11+
  98. // Chrome 78-81 implementation swaps `.name` and `.message` of cloned `DOMException`
  99. // Chrome returns `null` if cloned object contains multiple references to one error
  100. // Safari 14.1 implementation doesn't clone some `RegExp` flags, so requires a workaround
  101. // Safari implementation can't clone errors
  102. // Deno 1.2-1.10 implementations too naive
  103. // NodeJS 16.0+ does not have `PerformanceMark` constructor
  104. // NodeJS <17.2 structured cloning implementation from `performance.mark` is too naive
  105. // and can't clone, for example, `RegExp` or some boxed primitives
  106. // https://github.com/nodejs/node/issues/40840
  107. // no one of those implementations supports new (html/5749) error cloning semantic
  108. var structuredCloneFromMark = !nativeStructuredClone && checkBasicSemantic(function (value) {
  109. return new PerformanceMark(PERFORMANCE_MARK, { detail: value }).detail;
  110. });
  111. var nativeRestrictedStructuredClone = checkBasicSemantic(nativeStructuredClone) || structuredCloneFromMark;
  112. var throwUncloneable = function (type) {
  113. throw new DOMException('Uncloneable type: ' + type, DATA_CLONE_ERROR);
  114. };
  115. var throwUnpolyfillable = function (type, action) {
  116. throw new DOMException((action || 'Cloning') + ' of ' + type + ' cannot be properly polyfilled in this engine', DATA_CLONE_ERROR);
  117. };
  118. var createDataTransfer = function () {
  119. var dataTransfer;
  120. try {
  121. dataTransfer = new global.DataTransfer();
  122. } catch (error) {
  123. try {
  124. dataTransfer = new global.ClipboardEvent('').clipboardData;
  125. } catch (error2) { /* empty */ }
  126. }
  127. return dataTransfer && dataTransfer.items && dataTransfer.files ? dataTransfer : null;
  128. };
  129. var structuredCloneInternal = function (value, map) {
  130. if (isSymbol(value)) throwUncloneable('Symbol');
  131. if (!isObject(value)) return value;
  132. // effectively preserves circular references
  133. if (map) {
  134. if (mapHas(map, value)) return mapGet(map, value);
  135. } else map = new Map();
  136. var type = classof(value);
  137. var deep = false;
  138. var C, name, cloned, dataTransfer, i, length, keys, key, source, target;
  139. switch (type) {
  140. case 'Array':
  141. cloned = [];
  142. deep = true;
  143. break;
  144. case 'Object':
  145. cloned = {};
  146. deep = true;
  147. break;
  148. case 'Map':
  149. cloned = new Map();
  150. deep = true;
  151. break;
  152. case 'Set':
  153. cloned = new Set();
  154. deep = true;
  155. break;
  156. case 'RegExp':
  157. // in this block because of a Safari 14.1 bug
  158. // old FF does not clone regexes passed to the constructor, so get the source and flags directly
  159. cloned = new RegExp(value.source, getRegExpFlags(value));
  160. break;
  161. case 'Error':
  162. name = value.name;
  163. switch (name) {
  164. case 'AggregateError':
  165. cloned = getBuiltin('AggregateError')([]);
  166. break;
  167. case 'EvalError':
  168. cloned = EvalError();
  169. break;
  170. case 'RangeError':
  171. cloned = RangeError();
  172. break;
  173. case 'ReferenceError':
  174. cloned = ReferenceError();
  175. break;
  176. case 'SyntaxError':
  177. cloned = SyntaxError();
  178. break;
  179. case 'TypeError':
  180. cloned = TypeError();
  181. break;
  182. case 'URIError':
  183. cloned = URIError();
  184. break;
  185. case 'CompileError':
  186. cloned = CompileError();
  187. break;
  188. case 'LinkError':
  189. cloned = LinkError();
  190. break;
  191. case 'RuntimeError':
  192. cloned = RuntimeError();
  193. break;
  194. default:
  195. cloned = Error();
  196. }
  197. deep = true;
  198. break;
  199. case 'DOMException':
  200. cloned = new DOMException(value.message, value.name);
  201. deep = true;
  202. break;
  203. case 'DataView':
  204. case 'Int8Array':
  205. case 'Uint8Array':
  206. case 'Uint8ClampedArray':
  207. case 'Int16Array':
  208. case 'Uint16Array':
  209. case 'Int32Array':
  210. case 'Uint32Array':
  211. case 'Float32Array':
  212. case 'Float64Array':
  213. case 'BigInt64Array':
  214. case 'BigUint64Array':
  215. C = global[type];
  216. // in some old engines like Safari 9, typeof C is 'object'
  217. // on Uint8ClampedArray or some other constructors
  218. if (!isObject(C)) throwUnpolyfillable(type);
  219. cloned = new C(
  220. // this is safe, since arraybuffer cannot have circular references
  221. structuredCloneInternal(value.buffer, map),
  222. value.byteOffset,
  223. type === 'DataView' ? value.byteLength : value.length
  224. );
  225. break;
  226. case 'DOMQuad':
  227. try {
  228. cloned = new DOMQuad(
  229. structuredCloneInternal(value.p1, map),
  230. structuredCloneInternal(value.p2, map),
  231. structuredCloneInternal(value.p3, map),
  232. structuredCloneInternal(value.p4, map)
  233. );
  234. } catch (error) {
  235. if (nativeRestrictedStructuredClone) {
  236. cloned = nativeRestrictedStructuredClone(value);
  237. } else throwUnpolyfillable(type);
  238. }
  239. break;
  240. case 'FileList':
  241. dataTransfer = createDataTransfer();
  242. if (dataTransfer) {
  243. for (i = 0, length = lengthOfArrayLike(value); i < length; i++) {
  244. dataTransfer.items.add(structuredCloneInternal(value[i], map));
  245. }
  246. cloned = dataTransfer.files;
  247. } else if (nativeRestrictedStructuredClone) {
  248. cloned = nativeRestrictedStructuredClone(value);
  249. } else throwUnpolyfillable(type);
  250. break;
  251. case 'ImageData':
  252. // Safari 9 ImageData is a constructor, but typeof ImageData is 'object'
  253. try {
  254. cloned = new ImageData(
  255. structuredCloneInternal(value.data, map),
  256. value.width,
  257. value.height,
  258. { colorSpace: value.colorSpace }
  259. );
  260. } catch (error) {
  261. if (nativeRestrictedStructuredClone) {
  262. cloned = nativeRestrictedStructuredClone(value);
  263. } else throwUnpolyfillable(type);
  264. } break;
  265. default:
  266. if (nativeRestrictedStructuredClone) {
  267. cloned = nativeRestrictedStructuredClone(value);
  268. } else switch (type) {
  269. case 'BigInt':
  270. // can be a 3rd party polyfill
  271. cloned = Object(value.valueOf());
  272. break;
  273. case 'Boolean':
  274. cloned = Object(thisBooleanValue(value));
  275. break;
  276. case 'Number':
  277. cloned = Object(thisNumberValue(value));
  278. break;
  279. case 'String':
  280. cloned = Object(thisStringValue(value));
  281. break;
  282. case 'Date':
  283. cloned = new Date(thisTimeValue(value));
  284. break;
  285. case 'ArrayBuffer':
  286. C = global.DataView;
  287. // `ArrayBuffer#slice` is not available in IE10
  288. // `ArrayBuffer#slice` and `DataView` are not available in old FF
  289. if (!C && typeof value.slice != 'function') throwUnpolyfillable(type);
  290. // detached buffers throws in `DataView` and `.slice`
  291. try {
  292. if (typeof value.slice == 'function') {
  293. cloned = value.slice(0);
  294. } else {
  295. length = value.byteLength;
  296. cloned = new ArrayBuffer(length);
  297. source = new C(value);
  298. target = new C(cloned);
  299. for (i = 0; i < length; i++) {
  300. target.setUint8(i, source.getUint8(i));
  301. }
  302. }
  303. } catch (error) {
  304. throw new DOMException('ArrayBuffer is detached', DATA_CLONE_ERROR);
  305. } break;
  306. case 'SharedArrayBuffer':
  307. // SharedArrayBuffer should use shared memory, we can't polyfill it, so return the original
  308. cloned = value;
  309. break;
  310. case 'Blob':
  311. try {
  312. cloned = value.slice(0, value.size, value.type);
  313. } catch (error) {
  314. throwUnpolyfillable(type);
  315. } break;
  316. case 'DOMPoint':
  317. case 'DOMPointReadOnly':
  318. C = global[type];
  319. try {
  320. cloned = C.fromPoint
  321. ? C.fromPoint(value)
  322. : new C(value.x, value.y, value.z, value.w);
  323. } catch (error) {
  324. throwUnpolyfillable(type);
  325. } break;
  326. case 'DOMRect':
  327. case 'DOMRectReadOnly':
  328. C = global[type];
  329. try {
  330. cloned = C.fromRect
  331. ? C.fromRect(value)
  332. : new C(value.x, value.y, value.width, value.height);
  333. } catch (error) {
  334. throwUnpolyfillable(type);
  335. } break;
  336. case 'DOMMatrix':
  337. case 'DOMMatrixReadOnly':
  338. C = global[type];
  339. try {
  340. cloned = C.fromMatrix
  341. ? C.fromMatrix(value)
  342. : new C(value);
  343. } catch (error) {
  344. throwUnpolyfillable(type);
  345. } break;
  346. case 'AudioData':
  347. case 'VideoFrame':
  348. if (!isCallable(value.clone)) throwUnpolyfillable(type);
  349. try {
  350. cloned = value.clone();
  351. } catch (error) {
  352. throwUncloneable(type);
  353. } break;
  354. case 'File':
  355. try {
  356. cloned = new File([value], value.name, value);
  357. } catch (error) {
  358. throwUnpolyfillable(type);
  359. } break;
  360. case 'CropTarget':
  361. case 'CryptoKey':
  362. case 'FileSystemDirectoryHandle':
  363. case 'FileSystemFileHandle':
  364. case 'FileSystemHandle':
  365. case 'GPUCompilationInfo':
  366. case 'GPUCompilationMessage':
  367. case 'ImageBitmap':
  368. case 'RTCCertificate':
  369. case 'WebAssembly.Module':
  370. throwUnpolyfillable(type);
  371. // break omitted
  372. default:
  373. throwUncloneable(type);
  374. }
  375. }
  376. mapSet(map, value, cloned);
  377. if (deep) switch (type) {
  378. case 'Array':
  379. case 'Object':
  380. keys = objectKeys(value);
  381. for (i = 0, length = lengthOfArrayLike(keys); i < length; i++) {
  382. key = keys[i];
  383. createProperty(cloned, key, structuredCloneInternal(value[key], map));
  384. } break;
  385. case 'Map':
  386. value.forEach(function (v, k) {
  387. mapSet(cloned, structuredCloneInternal(k, map), structuredCloneInternal(v, map));
  388. });
  389. break;
  390. case 'Set':
  391. value.forEach(function (v) {
  392. setAdd(cloned, structuredCloneInternal(v, map));
  393. });
  394. break;
  395. case 'Error':
  396. createNonEnumerableProperty(cloned, 'message', structuredCloneInternal(value.message, map));
  397. if (hasOwn(value, 'cause')) {
  398. createNonEnumerableProperty(cloned, 'cause', structuredCloneInternal(value.cause, map));
  399. }
  400. if (name == 'AggregateError') {
  401. cloned.errors = structuredCloneInternal(value.errors, map);
  402. } // break omitted
  403. case 'DOMException':
  404. if (ERROR_STACK_INSTALLABLE) {
  405. createNonEnumerableProperty(cloned, 'stack', structuredCloneInternal(value.stack, map));
  406. }
  407. }
  408. return cloned;
  409. };
  410. var PROPER_TRANSFER = nativeStructuredClone && !fails(function () {
  411. // prevent V8 ArrayBufferDetaching protector cell invalidation and performance degradation
  412. // https://github.com/zloirock/core-js/issues/679
  413. if ((IS_DENO && V8 > 92) || (IS_NODE && V8 > 94) || (IS_BROWSER && V8 > 97)) return false;
  414. var buffer = new ArrayBuffer(8);
  415. var clone = nativeStructuredClone(buffer, { transfer: [buffer] });
  416. return buffer.byteLength != 0 || clone.byteLength != 8;
  417. });
  418. var tryToTransfer = function (rawTransfer, map) {
  419. if (!isObject(rawTransfer)) throw TypeError('Transfer option cannot be converted to a sequence');
  420. var transfer = [];
  421. iterate(rawTransfer, function (value) {
  422. push(transfer, anObject(value));
  423. });
  424. var i = 0;
  425. var length = lengthOfArrayLike(transfer);
  426. var value, type, C, transferredArray, transferred, canvas, context;
  427. if (PROPER_TRANSFER) {
  428. transferredArray = nativeStructuredClone(transfer, { transfer: transfer });
  429. while (i < length) mapSet(map, transfer[i], transferredArray[i++]);
  430. } else while (i < length) {
  431. value = transfer[i++];
  432. if (mapHas(map, value)) throw new DOMException('Duplicate transferable', DATA_CLONE_ERROR);
  433. type = classof(value);
  434. switch (type) {
  435. case 'ImageBitmap':
  436. C = global.OffscreenCanvas;
  437. if (!isConstructor(C)) throwUnpolyfillable(type, TRANSFERRING);
  438. try {
  439. canvas = new C(value.width, value.height);
  440. context = canvas.getContext('bitmaprenderer');
  441. context.transferFromImageBitmap(value);
  442. transferred = canvas.transferToImageBitmap();
  443. } catch (error) { /* empty */ }
  444. break;
  445. case 'AudioData':
  446. case 'VideoFrame':
  447. if (!isCallable(value.clone) || !isCallable(value.close)) throwUnpolyfillable(type, TRANSFERRING);
  448. try {
  449. transferred = value.clone();
  450. value.close();
  451. } catch (error) { /* empty */ }
  452. break;
  453. case 'ArrayBuffer':
  454. case 'MediaSourceHandle':
  455. case 'MessagePort':
  456. case 'OffscreenCanvas':
  457. case 'ReadableStream':
  458. case 'TransformStream':
  459. case 'WritableStream':
  460. throwUnpolyfillable(type, TRANSFERRING);
  461. }
  462. if (transferred === undefined) throw new DOMException('This object cannot be transferred: ' + type, DATA_CLONE_ERROR);
  463. mapSet(map, value, transferred);
  464. }
  465. };
  466. // `structuredClone` method
  467. // https://html.spec.whatwg.org/multipage/structured-data.html#dom-structuredclone
  468. $({ global: true, enumerable: true, sham: !PROPER_TRANSFER, forced: FORCED_REPLACEMENT }, {
  469. structuredClone: function structuredClone(value /* , { transfer } */) {
  470. var options = validateArgumentsLength(arguments.length, 1) > 1 && !isNullOrUndefined(arguments[1]) ? anObject(arguments[1]) : undefined;
  471. var transfer = options ? options.transfer : undefined;
  472. var map;
  473. if (transfer !== undefined) {
  474. map = new Map();
  475. tryToTransfer(transfer, map);
  476. }
  477. return structuredCloneInternal(value, map);
  478. }
  479. });