source: frontend/node_modules/@ungap/structured-clone/cjs/deserialize.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 2 weeks ago

Fix frontend appearance

  • Property mode set to 100644
File size: 2.4 KB
Line 
1'use strict';
2const {
3 VOID, PRIMITIVE, ARRAY, OBJECT, DATE, REGEXP, MAP, SET, ERROR, BIGINT
4} = require('./types.js');
5
6const env = typeof self === 'object' ? self : globalThis;
7
8const guard = (name, init) => {
9 switch (name) {
10 case 'Function':
11 case 'SharedWorker':
12 case 'Worker':
13 case 'eval':
14 case 'setInterval':
15 case 'setTimeout':
16 throw new TypeError('unable to deserialize ' + name);
17 }
18 return new env[name](init);
19};
20
21const deserializer = ($, _) => {
22 const as = (out, index) => {
23 $.set(index, out);
24 return out;
25 };
26
27 const unpair = index => {
28 if ($.has(index))
29 return $.get(index);
30
31 const [type, value] = _[index];
32 switch (type) {
33 case PRIMITIVE:
34 case VOID:
35 return as(value, index);
36 case ARRAY: {
37 const arr = as([], index);
38 for (const index of value)
39 arr.push(unpair(index));
40 return arr;
41 }
42 case OBJECT: {
43 const object = as({}, index);
44 for (const [key, index] of value)
45 object[unpair(key)] = unpair(index);
46 return object;
47 }
48 case DATE:
49 return as(new Date(value), index);
50 case REGEXP: {
51 const {source, flags} = value;
52 return as(new RegExp(source, flags), index);
53 }
54 case MAP: {
55 const map = as(new Map, index);
56 for (const [key, index] of value)
57 map.set(unpair(key), unpair(index));
58 return map;
59 }
60 case SET: {
61 const set = as(new Set, index);
62 for (const index of value)
63 set.add(unpair(index));
64 return set;
65 }
66 case ERROR: {
67 const {name, message} = value;
68 return as(guard(name, message), index);
69 }
70 case BIGINT:
71 return as(BigInt(value), index);
72 case 'BigInt':
73 return as(Object(BigInt(value)), index);
74 case 'ArrayBuffer':
75 return as(new Uint8Array(value).buffer, value);
76 case 'DataView': {
77 const { buffer } = new Uint8Array(value);
78 return as(new DataView(buffer), value);
79 }
80 }
81 return as(guard(type, value), index);
82 };
83
84 return unpair;
85};
86
87/**
88 * @typedef {Array<string,any>} Record a type representation
89 */
90
91/**
92 * Returns a deserialized value from a serialized array of Records.
93 * @param {Record[]} serialized a previously serialized value.
94 * @returns {any}
95 */
96const deserialize = serialized => deserializer(new Map, serialized)(0);
97exports.deserialize = deserialize;
Note: See TracBrowser for help on using the repository browser.