seroval 1.5.6 → 1.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. package/dist/dev/index.cjs +2954 -0
  2. package/dist/dev/index.d.cts +761 -0
  3. package/dist/dev/index.d.ts +761 -0
  4. package/dist/dev/index.js +2917 -0
  5. package/dist/index.cjs +2947 -0
  6. package/dist/index.d.cts +761 -0
  7. package/dist/index.d.ts +761 -0
  8. package/dist/index.js +2910 -0
  9. package/package.json +23 -21
  10. package/dist/cjs/development/index.cjs +0 -4232
  11. package/dist/cjs/development/index.cjs.map +0 -7
  12. package/dist/cjs/production/index.cjs +0 -3
  13. package/dist/esm/development/index.mjs +0 -4209
  14. package/dist/esm/development/index.mjs.map +0 -7
  15. package/dist/esm/production/index.mjs +0 -3
  16. package/dist/types/core/Serializer.d.ts +0 -28
  17. package/dist/types/core/Serializer.d.ts.map +0 -1
  18. package/dist/types/core/base-primitives.d.ts +0 -28
  19. package/dist/types/core/base-primitives.d.ts.map +0 -1
  20. package/dist/types/core/compat.d.ts +0 -15
  21. package/dist/types/core/compat.d.ts.map +0 -1
  22. package/dist/types/core/constants.d.ts +0 -105
  23. package/dist/types/core/constants.d.ts.map +0 -1
  24. package/dist/types/core/constructors.d.ts +0 -50
  25. package/dist/types/core/constructors.d.ts.map +0 -1
  26. package/dist/types/core/context/async-parser.d.ts +0 -20
  27. package/dist/types/core/context/async-parser.d.ts.map +0 -1
  28. package/dist/types/core/context/deserializer.d.ts +0 -52
  29. package/dist/types/core/context/deserializer.d.ts.map +0 -1
  30. package/dist/types/core/context/parser.d.ts +0 -62
  31. package/dist/types/core/context/parser.d.ts.map +0 -1
  32. package/dist/types/core/context/serializer.d.ts +0 -103
  33. package/dist/types/core/context/serializer.d.ts.map +0 -1
  34. package/dist/types/core/context/sync-parser.d.ts +0 -61
  35. package/dist/types/core/context/sync-parser.d.ts.map +0 -1
  36. package/dist/types/core/cross/index.d.ts +0 -24
  37. package/dist/types/core/cross/index.d.ts.map +0 -1
  38. package/dist/types/core/errors.d.ts +0 -47
  39. package/dist/types/core/errors.d.ts.map +0 -1
  40. package/dist/types/core/function-string.d.ts +0 -3
  41. package/dist/types/core/function-string.d.ts.map +0 -1
  42. package/dist/types/core/keys.d.ts +0 -4
  43. package/dist/types/core/keys.d.ts.map +0 -1
  44. package/dist/types/core/literals.d.ts +0 -10
  45. package/dist/types/core/literals.d.ts.map +0 -1
  46. package/dist/types/core/node.d.ts +0 -8
  47. package/dist/types/core/node.d.ts.map +0 -1
  48. package/dist/types/core/opaque-reference.d.ts +0 -9
  49. package/dist/types/core/opaque-reference.d.ts.map +0 -1
  50. package/dist/types/core/plugin.d.ts +0 -52
  51. package/dist/types/core/plugin.d.ts.map +0 -1
  52. package/dist/types/core/reference.d.ts +0 -6
  53. package/dist/types/core/reference.d.ts.map +0 -1
  54. package/dist/types/core/sequence.d.ts +0 -11
  55. package/dist/types/core/sequence.d.ts.map +0 -1
  56. package/dist/types/core/special-reference.d.ts +0 -16
  57. package/dist/types/core/special-reference.d.ts.map +0 -1
  58. package/dist/types/core/stream.d.ts +0 -17
  59. package/dist/types/core/stream.d.ts.map +0 -1
  60. package/dist/types/core/string.d.ts +0 -4
  61. package/dist/types/core/string.d.ts.map +0 -1
  62. package/dist/types/core/symbols.d.ts +0 -14
  63. package/dist/types/core/symbols.d.ts.map +0 -1
  64. package/dist/types/core/tree/index.d.ts +0 -21
  65. package/dist/types/core/tree/index.d.ts.map +0 -1
  66. package/dist/types/core/types.d.ts +0 -233
  67. package/dist/types/core/types.d.ts.map +0 -1
  68. package/dist/types/core/utils/error.d.ts +0 -6
  69. package/dist/types/core/utils/error.d.ts.map +0 -1
  70. package/dist/types/core/utils/get-identifier.d.ts +0 -2
  71. package/dist/types/core/utils/get-identifier.d.ts.map +0 -1
  72. package/dist/types/core/utils/get-object-flag.d.ts +0 -3
  73. package/dist/types/core/utils/get-object-flag.d.ts.map +0 -1
  74. package/dist/types/core/utils/is-valid-identifier.d.ts +0 -2
  75. package/dist/types/core/utils/is-valid-identifier.d.ts.map +0 -1
  76. package/dist/types/core/utils/promise-to-result.d.ts +0 -2
  77. package/dist/types/core/utils/promise-to-result.d.ts.map +0 -1
  78. package/dist/types/core/utils/typed-array.d.ts +0 -6
  79. package/dist/types/core/utils/typed-array.d.ts.map +0 -1
  80. package/dist/types/core/utils/valid-properties.d.ts +0 -3
  81. package/dist/types/core/utils/valid-properties.d.ts.map +0 -1
  82. package/dist/types/index.d.ts +0 -18
  83. package/dist/types/index.d.ts.map +0 -1
  84. package/src/core/Serializer.ts +0 -119
  85. package/src/core/base-primitives.ts +0 -537
  86. package/src/core/compat.ts +0 -23
  87. package/src/core/constants.ts +0 -202
  88. package/src/core/constructors.ts +0 -265
  89. package/src/core/context/async-parser.ts +0 -666
  90. package/src/core/context/deserializer.ts +0 -832
  91. package/src/core/context/parser.ts +0 -335
  92. package/src/core/context/serializer.ts +0 -1524
  93. package/src/core/context/sync-parser.ts +0 -943
  94. package/src/core/cross/index.ts +0 -190
  95. package/src/core/errors.ts +0 -196
  96. package/src/core/function-string.ts +0 -31
  97. package/src/core/keys.ts +0 -17
  98. package/src/core/literals.ts +0 -43
  99. package/src/core/node.ts +0 -40
  100. package/src/core/opaque-reference.ts +0 -9
  101. package/src/core/plugin.ts +0 -105
  102. package/src/core/reference.ts +0 -66
  103. package/src/core/sequence.ts +0 -59
  104. package/src/core/special-reference.ts +0 -42
  105. package/src/core/stream.ts +0 -71
  106. package/src/core/string.ts +0 -86
  107. package/src/core/symbols.ts +0 -20
  108. package/src/core/tree/index.ts +0 -134
  109. package/src/core/types.ts +0 -377
  110. package/src/core/utils/error.ts +0 -73
  111. package/src/core/utils/get-identifier.ts +0 -18
  112. package/src/core/utils/get-object-flag.ts +0 -14
  113. package/src/core/utils/is-valid-identifier.ts +0 -12
  114. package/src/core/utils/promise-to-result.ts +0 -9
  115. package/src/core/utils/typed-array.ts +0 -56
  116. package/src/core/utils/valid-properties.ts +0 -34
  117. package/src/index.ts +0 -36
@@ -1,4209 +0,0 @@
1
- // src/core/compat.ts
2
- var Feature = /* @__PURE__ */ ((Feature2) => {
3
- Feature2[Feature2["AggregateError"] = 1] = "AggregateError";
4
- Feature2[Feature2["ArrowFunction"] = 2] = "ArrowFunction";
5
- Feature2[Feature2["ErrorPrototypeStack"] = 4] = "ErrorPrototypeStack";
6
- Feature2[Feature2["ObjectAssign"] = 8] = "ObjectAssign";
7
- Feature2[Feature2["BigIntTypedArray"] = 16] = "BigIntTypedArray";
8
- Feature2[Feature2["RegExp"] = 32] = "RegExp";
9
- return Feature2;
10
- })(Feature || {});
11
- var ALL_ENABLED = 1 /* AggregateError */ | 2 /* ArrowFunction */ | 4 /* ErrorPrototypeStack */ | 8 /* ObjectAssign */ | 16 /* BigIntTypedArray */ | 32 /* RegExp */;
12
-
13
- // src/core/symbols.ts
14
- var SYM_ASYNC_ITERATOR = Symbol.asyncIterator;
15
- var SYM_HAS_INSTANCE = Symbol.hasInstance;
16
- var SYM_IS_CONCAT_SPREADABLE = Symbol.isConcatSpreadable;
17
- var SYM_ITERATOR = Symbol.iterator;
18
- var SYM_MATCH = Symbol.match;
19
- var SYM_MATCH_ALL = Symbol.matchAll;
20
- var SYM_REPLACE = Symbol.replace;
21
- var SYM_SEARCH = Symbol.search;
22
- var SYM_SPECIES = Symbol.species;
23
- var SYM_SPLIT = Symbol.split;
24
- var SYM_TO_PRIMITIVE = Symbol.toPrimitive;
25
- var SYM_TO_STRING_TAG = Symbol.toStringTag;
26
- var SYM_UNSCOPABLES = Symbol.unscopables;
27
-
28
- // src/core/constants.ts
29
- var SYMBOL_STRING = {
30
- [0 /* AsyncIterator */]: "Symbol.asyncIterator",
31
- [1 /* HasInstance */]: "Symbol.hasInstance",
32
- [2 /* IsConcatSpreadable */]: "Symbol.isConcatSpreadable",
33
- [3 /* Iterator */]: "Symbol.iterator",
34
- [4 /* Match */]: "Symbol.match",
35
- [5 /* MatchAll */]: "Symbol.matchAll",
36
- [6 /* Replace */]: "Symbol.replace",
37
- [7 /* Search */]: "Symbol.search",
38
- [8 /* Species */]: "Symbol.species",
39
- [9 /* Split */]: "Symbol.split",
40
- [10 /* ToPrimitive */]: "Symbol.toPrimitive",
41
- [11 /* ToStringTag */]: "Symbol.toStringTag",
42
- [12 /* Unscopables */]: "Symbol.unscopables"
43
- };
44
- var INV_SYMBOL_REF = {
45
- [SYM_ASYNC_ITERATOR]: 0 /* AsyncIterator */,
46
- [SYM_HAS_INSTANCE]: 1 /* HasInstance */,
47
- [SYM_IS_CONCAT_SPREADABLE]: 2 /* IsConcatSpreadable */,
48
- [SYM_ITERATOR]: 3 /* Iterator */,
49
- [SYM_MATCH]: 4 /* Match */,
50
- [SYM_MATCH_ALL]: 5 /* MatchAll */,
51
- [SYM_REPLACE]: 6 /* Replace */,
52
- [SYM_SEARCH]: 7 /* Search */,
53
- [SYM_SPECIES]: 8 /* Species */,
54
- [SYM_SPLIT]: 9 /* Split */,
55
- [SYM_TO_PRIMITIVE]: 10 /* ToPrimitive */,
56
- [SYM_TO_STRING_TAG]: 11 /* ToStringTag */,
57
- [SYM_UNSCOPABLES]: 12 /* Unscopables */
58
- };
59
- var SYMBOL_REF = {
60
- [0 /* AsyncIterator */]: SYM_ASYNC_ITERATOR,
61
- [1 /* HasInstance */]: SYM_HAS_INSTANCE,
62
- [2 /* IsConcatSpreadable */]: SYM_IS_CONCAT_SPREADABLE,
63
- [3 /* Iterator */]: SYM_ITERATOR,
64
- [4 /* Match */]: SYM_MATCH,
65
- [5 /* MatchAll */]: SYM_MATCH_ALL,
66
- [6 /* Replace */]: SYM_REPLACE,
67
- [7 /* Search */]: SYM_SEARCH,
68
- [8 /* Species */]: SYM_SPECIES,
69
- [9 /* Split */]: SYM_SPLIT,
70
- [10 /* ToPrimitive */]: SYM_TO_PRIMITIVE,
71
- [11 /* ToStringTag */]: SYM_TO_STRING_TAG,
72
- [12 /* Unscopables */]: SYM_UNSCOPABLES
73
- };
74
- var CONSTANT_STRING = {
75
- [2 /* True */]: "!0",
76
- [3 /* False */]: "!1",
77
- [1 /* Undefined */]: "void 0",
78
- [0 /* Null */]: "null",
79
- [4 /* NegZero */]: "-0",
80
- [5 /* Inf */]: "1/0",
81
- [6 /* NegInf */]: "-1/0",
82
- [7 /* Nan */]: "0/0"
83
- };
84
- var NIL = void 0;
85
- var CONSTANT_VAL = {
86
- [2 /* True */]: true,
87
- [3 /* False */]: false,
88
- [1 /* Undefined */]: NIL,
89
- [0 /* Null */]: null,
90
- [4 /* NegZero */]: -0,
91
- [5 /* Inf */]: Number.POSITIVE_INFINITY,
92
- [6 /* NegInf */]: Number.NEGATIVE_INFINITY,
93
- [7 /* Nan */]: Number.NaN
94
- };
95
- var ERROR_CONSTRUCTOR_STRING = {
96
- [0 /* Error */]: "Error",
97
- [1 /* EvalError */]: "EvalError",
98
- [2 /* RangeError */]: "RangeError",
99
- [3 /* ReferenceError */]: "ReferenceError",
100
- [4 /* SyntaxError */]: "SyntaxError",
101
- [5 /* TypeError */]: "TypeError",
102
- [6 /* URIError */]: "URIError"
103
- };
104
- var ERROR_CONSTRUCTOR = {
105
- [0 /* Error */]: Error,
106
- [1 /* EvalError */]: EvalError,
107
- [2 /* RangeError */]: RangeError,
108
- [3 /* ReferenceError */]: ReferenceError,
109
- [4 /* SyntaxError */]: SyntaxError,
110
- [5 /* TypeError */]: TypeError,
111
- [6 /* URIError */]: URIError
112
- };
113
-
114
- // src/core/node.ts
115
- function createSerovalNode(t, i, s, c, m, p, e, a, f, b, o, l) {
116
- return {
117
- t,
118
- i,
119
- s,
120
- c,
121
- m,
122
- p,
123
- e,
124
- a,
125
- f,
126
- b,
127
- o,
128
- l
129
- };
130
- }
131
-
132
- // src/core/literals.ts
133
- function createConstantNode(value) {
134
- return createSerovalNode(
135
- 2 /* Constant */,
136
- NIL,
137
- value,
138
- NIL,
139
- NIL,
140
- NIL,
141
- NIL,
142
- NIL,
143
- NIL,
144
- NIL,
145
- NIL,
146
- NIL
147
- );
148
- }
149
- var TRUE_NODE = /* @__PURE__ */ createConstantNode(
150
- 2 /* True */
151
- );
152
- var FALSE_NODE = /* @__PURE__ */ createConstantNode(
153
- 3 /* False */
154
- );
155
- var UNDEFINED_NODE = /* @__PURE__ */ createConstantNode(
156
- 1 /* Undefined */
157
- );
158
- var NULL_NODE = /* @__PURE__ */ createConstantNode(
159
- 0 /* Null */
160
- );
161
- var NEG_ZERO_NODE = /* @__PURE__ */ createConstantNode(
162
- 4 /* NegZero */
163
- );
164
- var INFINITY_NODE = /* @__PURE__ */ createConstantNode(
165
- 5 /* Inf */
166
- );
167
- var NEG_INFINITY_NODE = /* @__PURE__ */ createConstantNode(
168
- 6 /* NegInf */
169
- );
170
- var NAN_NODE = /* @__PURE__ */ createConstantNode(7 /* Nan */);
171
-
172
- // src/core/string.ts
173
- function serializeChar(str) {
174
- switch (str) {
175
- case '"':
176
- return '\\"';
177
- case "\\":
178
- return "\\\\";
179
- case "\n":
180
- return "\\n";
181
- case "\r":
182
- return "\\r";
183
- case "\b":
184
- return "\\b";
185
- case " ":
186
- return "\\t";
187
- case "\f":
188
- return "\\f";
189
- case "<":
190
- return "\\x3C";
191
- case "\u2028":
192
- return "\\u2028";
193
- case "\u2029":
194
- return "\\u2029";
195
- default:
196
- return NIL;
197
- }
198
- }
199
- function serializeString(str) {
200
- let result = "";
201
- let lastPos = 0;
202
- let replacement;
203
- for (let i = 0, len = str.length; i < len; i++) {
204
- replacement = serializeChar(str[i]);
205
- if (replacement) {
206
- result += str.slice(lastPos, i) + replacement;
207
- lastPos = i + 1;
208
- }
209
- }
210
- if (lastPos === 0) {
211
- result = str;
212
- } else {
213
- result += str.slice(lastPos);
214
- }
215
- return result;
216
- }
217
- function deserializeReplacer(str) {
218
- switch (str) {
219
- case "\\\\":
220
- return "\\";
221
- case '\\"':
222
- return '"';
223
- case "\\n":
224
- return "\n";
225
- case "\\r":
226
- return "\r";
227
- case "\\b":
228
- return "\b";
229
- case "\\t":
230
- return " ";
231
- case "\\f":
232
- return "\f";
233
- case "\\x3C":
234
- return "<";
235
- case "\\u2028":
236
- return "\u2028";
237
- case "\\u2029":
238
- return "\u2029";
239
- default:
240
- return str;
241
- }
242
- }
243
- function deserializeString(str) {
244
- return str.replace(
245
- /(\\\\|\\"|\\n|\\r|\\b|\\t|\\f|\\u2028|\\u2029|\\x3C)/g,
246
- deserializeReplacer
247
- );
248
- }
249
-
250
- // src/core/keys.ts
251
- var REFERENCES_KEY = "__SEROVAL_REFS__";
252
- var GLOBAL_CONTEXT_REFERENCES = "$R";
253
- var GLOBAL_CONTEXT_R = `self.${GLOBAL_CONTEXT_REFERENCES}`;
254
- function getCrossReferenceHeader(id) {
255
- if (id == null) {
256
- return `${GLOBAL_CONTEXT_R}=${GLOBAL_CONTEXT_R}||[]`;
257
- }
258
- return `(${GLOBAL_CONTEXT_R}=${GLOBAL_CONTEXT_R}||{})["${serializeString(
259
- id
260
- )}"]=[]`;
261
- }
262
-
263
- // src/core/reference.ts
264
- var REFERENCE = /* @__PURE__ */ new Map();
265
- var INV_REFERENCE = /* @__PURE__ */ new Map();
266
- function createReference(id, value) {
267
- REFERENCE.set(value, id);
268
- INV_REFERENCE.set(id, value);
269
- return value;
270
- }
271
- function hasReferenceID(value) {
272
- return REFERENCE.has(value);
273
- }
274
- function hasReference(id) {
275
- return INV_REFERENCE.has(id);
276
- }
277
- function getReferenceID(value) {
278
- if (hasReferenceID(value)) {
279
- return REFERENCE.get(value);
280
- }
281
- throw new SerovalMissingReferenceError(value);
282
- }
283
- function getReference(id) {
284
- if (hasReference(id)) {
285
- return INV_REFERENCE.get(id);
286
- }
287
- throw new SerovalMissingReferenceForIdError(id);
288
- }
289
- if (typeof globalThis !== "undefined") {
290
- Object.defineProperty(globalThis, REFERENCES_KEY, {
291
- value: INV_REFERENCE,
292
- configurable: true,
293
- writable: false,
294
- enumerable: false
295
- });
296
- } else if (typeof window !== "undefined") {
297
- Object.defineProperty(window, REFERENCES_KEY, {
298
- value: INV_REFERENCE,
299
- configurable: true,
300
- writable: false,
301
- enumerable: false
302
- });
303
- } else if (typeof self !== "undefined") {
304
- Object.defineProperty(self, REFERENCES_KEY, {
305
- value: INV_REFERENCE,
306
- configurable: true,
307
- writable: false,
308
- enumerable: false
309
- });
310
- } else if (typeof global !== "undefined") {
311
- Object.defineProperty(global, REFERENCES_KEY, {
312
- value: INV_REFERENCE,
313
- configurable: true,
314
- writable: false,
315
- enumerable: false
316
- });
317
- }
318
-
319
- // src/core/utils/error.ts
320
- function getErrorConstructor(error) {
321
- if (error instanceof EvalError) {
322
- return 1 /* EvalError */;
323
- }
324
- if (error instanceof RangeError) {
325
- return 2 /* RangeError */;
326
- }
327
- if (error instanceof ReferenceError) {
328
- return 3 /* ReferenceError */;
329
- }
330
- if (error instanceof SyntaxError) {
331
- return 4 /* SyntaxError */;
332
- }
333
- if (error instanceof TypeError) {
334
- return 5 /* TypeError */;
335
- }
336
- if (error instanceof URIError) {
337
- return 6 /* URIError */;
338
- }
339
- return 0 /* Error */;
340
- }
341
- function getInitialErrorOptions(error) {
342
- const construct = ERROR_CONSTRUCTOR_STRING[getErrorConstructor(error)];
343
- if (error.name !== construct) {
344
- return { name: error.name };
345
- }
346
- if (error.constructor.name !== construct) {
347
- return { name: error.constructor.name };
348
- }
349
- return {};
350
- }
351
- function getErrorOptions(error, features) {
352
- let options = getInitialErrorOptions(error);
353
- const names = Object.getOwnPropertyNames(error);
354
- for (let i = 0, len = names.length, name; i < len; i++) {
355
- name = names[i];
356
- if (name !== "name" && name !== "message") {
357
- if (name === "stack") {
358
- if (features & 4 /* ErrorPrototypeStack */) {
359
- options = options || {};
360
- options[name] = error[name];
361
- }
362
- } else {
363
- options = options || {};
364
- options[name] = error[name];
365
- }
366
- }
367
- }
368
- return options;
369
- }
370
-
371
- // src/core/utils/get-object-flag.ts
372
- function getObjectFlag(obj) {
373
- if (Object.isFrozen(obj)) {
374
- return 3 /* Frozen */;
375
- }
376
- if (Object.isSealed(obj)) {
377
- return 2 /* Sealed */;
378
- }
379
- if (Object.isExtensible(obj)) {
380
- return 0 /* None */;
381
- }
382
- return 1 /* NonExtensible */;
383
- }
384
-
385
- // src/core/base-primitives.ts
386
- function createNumberNode(value) {
387
- switch (value) {
388
- case Number.POSITIVE_INFINITY:
389
- return INFINITY_NODE;
390
- case Number.NEGATIVE_INFINITY:
391
- return NEG_INFINITY_NODE;
392
- }
393
- if (value !== value) {
394
- return NAN_NODE;
395
- }
396
- if (Object.is(value, -0)) {
397
- return NEG_ZERO_NODE;
398
- }
399
- return createSerovalNode(
400
- 0 /* Number */,
401
- NIL,
402
- value,
403
- NIL,
404
- NIL,
405
- NIL,
406
- NIL,
407
- NIL,
408
- NIL,
409
- NIL,
410
- NIL,
411
- NIL
412
- );
413
- }
414
- function createStringNode(value) {
415
- return createSerovalNode(
416
- 1 /* String */,
417
- NIL,
418
- serializeString(value),
419
- NIL,
420
- NIL,
421
- NIL,
422
- NIL,
423
- NIL,
424
- NIL,
425
- NIL,
426
- NIL,
427
- NIL
428
- );
429
- }
430
- function createBigIntNode(current) {
431
- return createSerovalNode(
432
- 3 /* BigInt */,
433
- NIL,
434
- "" + current,
435
- NIL,
436
- NIL,
437
- NIL,
438
- NIL,
439
- NIL,
440
- NIL,
441
- NIL,
442
- NIL,
443
- NIL
444
- );
445
- }
446
- function createIndexedValueNode(id) {
447
- return createSerovalNode(
448
- 4 /* IndexedValue */,
449
- id,
450
- NIL,
451
- NIL,
452
- NIL,
453
- NIL,
454
- NIL,
455
- NIL,
456
- NIL,
457
- NIL,
458
- NIL,
459
- NIL
460
- );
461
- }
462
- function createDateNode(id, current) {
463
- const timestamp = current.valueOf();
464
- return createSerovalNode(
465
- 5 /* Date */,
466
- id,
467
- timestamp !== timestamp ? "" : current.toISOString(),
468
- NIL,
469
- NIL,
470
- NIL,
471
- NIL,
472
- NIL,
473
- NIL,
474
- NIL,
475
- NIL,
476
- NIL
477
- );
478
- }
479
- function createRegExpNode(id, current) {
480
- return createSerovalNode(
481
- 6 /* RegExp */,
482
- id,
483
- NIL,
484
- serializeString(current.source),
485
- current.flags,
486
- NIL,
487
- NIL,
488
- NIL,
489
- NIL,
490
- NIL,
491
- NIL,
492
- NIL
493
- );
494
- }
495
- function createWKSymbolNode(id, current) {
496
- return createSerovalNode(
497
- 17 /* WKSymbol */,
498
- id,
499
- INV_SYMBOL_REF[current],
500
- NIL,
501
- NIL,
502
- NIL,
503
- NIL,
504
- NIL,
505
- NIL,
506
- NIL,
507
- NIL,
508
- NIL
509
- );
510
- }
511
- function createReferenceNode(id, ref) {
512
- return createSerovalNode(
513
- 18 /* Reference */,
514
- id,
515
- serializeString(getReferenceID(ref)),
516
- NIL,
517
- NIL,
518
- NIL,
519
- NIL,
520
- NIL,
521
- NIL,
522
- NIL,
523
- NIL,
524
- NIL
525
- );
526
- }
527
- function createPluginNode(id, tag, value) {
528
- return createSerovalNode(
529
- 25 /* Plugin */,
530
- id,
531
- value,
532
- serializeString(tag),
533
- NIL,
534
- NIL,
535
- NIL,
536
- NIL,
537
- NIL,
538
- NIL,
539
- NIL,
540
- NIL
541
- );
542
- }
543
- function createArrayNode(id, current, parsedItems) {
544
- return createSerovalNode(
545
- 9 /* Array */,
546
- id,
547
- NIL,
548
- NIL,
549
- NIL,
550
- NIL,
551
- NIL,
552
- parsedItems,
553
- NIL,
554
- NIL,
555
- getObjectFlag(current),
556
- NIL
557
- );
558
- }
559
- function createBoxedNode(id, boxed) {
560
- return createSerovalNode(
561
- 21 /* Boxed */,
562
- id,
563
- NIL,
564
- NIL,
565
- NIL,
566
- NIL,
567
- NIL,
568
- NIL,
569
- boxed,
570
- NIL,
571
- NIL,
572
- NIL
573
- );
574
- }
575
- function createTypedArrayNode(id, current, buffer) {
576
- return createSerovalNode(
577
- 15 /* TypedArray */,
578
- id,
579
- NIL,
580
- current.constructor.name,
581
- NIL,
582
- NIL,
583
- NIL,
584
- NIL,
585
- buffer,
586
- current.byteOffset,
587
- NIL,
588
- current.length
589
- );
590
- }
591
- function createBigIntTypedArrayNode(id, current, buffer) {
592
- return createSerovalNode(
593
- 16 /* BigIntTypedArray */,
594
- id,
595
- NIL,
596
- current.constructor.name,
597
- NIL,
598
- NIL,
599
- NIL,
600
- NIL,
601
- buffer,
602
- current.byteOffset,
603
- NIL,
604
- current.length
605
- );
606
- }
607
- function createDataViewNode(id, current, buffer) {
608
- return createSerovalNode(
609
- 20 /* DataView */,
610
- id,
611
- NIL,
612
- NIL,
613
- NIL,
614
- NIL,
615
- NIL,
616
- NIL,
617
- buffer,
618
- current.byteOffset,
619
- NIL,
620
- current.byteLength
621
- );
622
- }
623
- function createErrorNode(id, current, options) {
624
- return createSerovalNode(
625
- 13 /* Error */,
626
- id,
627
- getErrorConstructor(current),
628
- NIL,
629
- serializeString(current.message),
630
- options,
631
- NIL,
632
- NIL,
633
- NIL,
634
- NIL,
635
- NIL,
636
- NIL
637
- );
638
- }
639
- function createAggregateErrorNode(id, current, options) {
640
- return createSerovalNode(
641
- 14 /* AggregateError */,
642
- id,
643
- getErrorConstructor(current),
644
- NIL,
645
- serializeString(current.message),
646
- options,
647
- NIL,
648
- NIL,
649
- NIL,
650
- NIL,
651
- NIL,
652
- NIL
653
- );
654
- }
655
- function createSetNode(id, items) {
656
- return createSerovalNode(
657
- 7 /* Set */,
658
- id,
659
- NIL,
660
- NIL,
661
- NIL,
662
- NIL,
663
- NIL,
664
- items,
665
- NIL,
666
- NIL,
667
- NIL,
668
- NIL
669
- );
670
- }
671
- function createIteratorFactoryInstanceNode(factory, items) {
672
- return createSerovalNode(
673
- 28 /* IteratorFactoryInstance */,
674
- NIL,
675
- NIL,
676
- NIL,
677
- NIL,
678
- NIL,
679
- NIL,
680
- [factory, items],
681
- NIL,
682
- NIL,
683
- NIL,
684
- NIL
685
- );
686
- }
687
- function createAsyncIteratorFactoryInstanceNode(factory, items) {
688
- return createSerovalNode(
689
- 30 /* AsyncIteratorFactoryInstance */,
690
- NIL,
691
- NIL,
692
- NIL,
693
- NIL,
694
- NIL,
695
- NIL,
696
- [factory, items],
697
- NIL,
698
- NIL,
699
- NIL,
700
- NIL
701
- );
702
- }
703
- function createStreamConstructorNode(id, factory, sequence) {
704
- return createSerovalNode(
705
- 31 /* StreamConstructor */,
706
- id,
707
- NIL,
708
- NIL,
709
- NIL,
710
- NIL,
711
- NIL,
712
- sequence,
713
- factory,
714
- NIL,
715
- NIL,
716
- NIL
717
- );
718
- }
719
- function createStreamNextNode(id, parsed) {
720
- return createSerovalNode(
721
- 32 /* StreamNext */,
722
- id,
723
- NIL,
724
- NIL,
725
- NIL,
726
- NIL,
727
- NIL,
728
- NIL,
729
- parsed,
730
- NIL,
731
- NIL,
732
- NIL
733
- );
734
- }
735
- function createStreamThrowNode(id, parsed) {
736
- return createSerovalNode(
737
- 33 /* StreamThrow */,
738
- id,
739
- NIL,
740
- NIL,
741
- NIL,
742
- NIL,
743
- NIL,
744
- NIL,
745
- parsed,
746
- NIL,
747
- NIL,
748
- NIL
749
- );
750
- }
751
- function createStreamReturnNode(id, parsed) {
752
- return createSerovalNode(
753
- 34 /* StreamReturn */,
754
- id,
755
- NIL,
756
- NIL,
757
- NIL,
758
- NIL,
759
- NIL,
760
- NIL,
761
- parsed,
762
- NIL,
763
- NIL,
764
- NIL
765
- );
766
- }
767
- function createSequenceNode(id, sequence, throwAt, doneAt) {
768
- return createSerovalNode(
769
- 35 /* Sequence */,
770
- id,
771
- throwAt,
772
- NIL,
773
- NIL,
774
- NIL,
775
- NIL,
776
- sequence,
777
- NIL,
778
- NIL,
779
- NIL,
780
- doneAt
781
- );
782
- }
783
-
784
- // src/core/errors.ts
785
- var { toString: objectToString } = Object.prototype;
786
- function getErrorMessageDev(type, cause) {
787
- if (cause instanceof Error) {
788
- return `Seroval caught an error during the ${type} process.
789
-
790
- ${cause.name}
791
- ${cause.message}
792
-
793
- - For more information, please check the "cause" property of this error.
794
- - If you believe this is an error in Seroval, please submit an issue at https://github.com/lxsmnsyc/seroval/issues/new`;
795
- }
796
- return `Seroval caught an error during the ${type} process.
797
-
798
- "${objectToString.call(cause)}"
799
-
800
- For more information, please check the "cause" property of this error.`;
801
- }
802
- var getErrorMessage = (type, cause) => false ? getErrorMessageProd(type) : getErrorMessageDev(type, cause);
803
- var SerovalError = class extends Error {
804
- constructor(type, cause) {
805
- super(getErrorMessage(type, cause));
806
- this.cause = cause;
807
- }
808
- };
809
- var SerovalParserError = class extends SerovalError {
810
- constructor(cause) {
811
- super("parsing", cause);
812
- }
813
- };
814
- var SerovalSerializationError = class extends SerovalError {
815
- constructor(cause) {
816
- super("serialization", cause);
817
- }
818
- };
819
- var SerovalDeserializationError = class extends SerovalError {
820
- constructor(cause) {
821
- super("deserialization", cause);
822
- }
823
- };
824
- var SerovalUnsupportedTypeError = class extends Error {
825
- constructor(value) {
826
- super(
827
- false ? getSpecificErrorMessage(1 /* UnsupportedType */) : `The value ${objectToString.call(value)} of type "${typeof value}" cannot be parsed/serialized.
828
-
829
- There are few workarounds for this problem:
830
- - Transform the value in a way that it can be serialized.
831
- - If the reference is present on multiple runtimes (isomorphic), you can use the Reference API to map the references.`
832
- );
833
- this.value = value;
834
- }
835
- };
836
- var SerovalUnsupportedNodeError = class extends Error {
837
- constructor(node) {
838
- super(
839
- false ? getSpecificErrorMessage(2 /* UnsupportedNode */) : 'Unsupported node type "' + node.t + '".'
840
- );
841
- }
842
- };
843
- var SerovalMissingPluginError = class extends Error {
844
- constructor(tag) {
845
- super(
846
- false ? getSpecificErrorMessage(3 /* MissingPlugin */) : 'Missing plugin for tag "' + tag + '".'
847
- );
848
- }
849
- };
850
- var SerovalMissingInstanceError = class extends Error {
851
- constructor(tag) {
852
- super(
853
- false ? getSpecificErrorMessage(4 /* MissingInstance */) : 'Missing "' + tag + '" instance.'
854
- );
855
- }
856
- };
857
- var SerovalMissingReferenceError = class extends Error {
858
- constructor(value) {
859
- super(
860
- false ? getSpecificErrorMessage(5 /* MissingReference */) : 'Missing reference for the value "' + objectToString.call(value) + '" of type "' + typeof value + '"'
861
- );
862
- this.value = value;
863
- }
864
- };
865
- var SerovalMissingReferenceForIdError = class extends Error {
866
- constructor(id) {
867
- super(
868
- false ? getSpecificErrorMessage(6 /* MissingReferenceForId */) : 'Missing reference for id "' + serializeString(id) + '"'
869
- );
870
- }
871
- };
872
- var SerovalUnknownTypedArrayError = class extends Error {
873
- constructor(name) {
874
- super(
875
- false ? getSpecificErrorMessage(7 /* UnknownTypedArray */) : 'Unknown TypedArray "' + name + '"'
876
- );
877
- }
878
- };
879
- var SerovalMalformedNodeError = class extends Error {
880
- constructor(node) {
881
- super(
882
- false ? getSpecificErrorMessage(8 /* MalformedNode */) : 'Malformed node type "' + node.t + '".'
883
- );
884
- }
885
- };
886
- var SerovalConflictedNodeIdError = class extends Error {
887
- constructor(node) {
888
- super(
889
- false ? getSpecificErrorMessage(9 /* ConflictedNodeId */) : 'Conflicted node id "' + node.i + '".'
890
- );
891
- }
892
- };
893
- var SerovalDepthLimitError = class extends Error {
894
- constructor(limit) {
895
- super(
896
- false ? getSpecificErrorMessage(9 /* ConflictedNodeId */) : "Depth limit of " + limit + " reached"
897
- );
898
- }
899
- };
900
-
901
- // src/core/opaque-reference.ts
902
- var OpaqueReference = class {
903
- constructor(value, replacement) {
904
- this.value = value;
905
- this.replacement = replacement;
906
- }
907
- };
908
-
909
- // src/core/constructors.ts
910
- var PROMISE_CONSTRUCTOR = () => {
911
- const resolver = {
912
- p: 0,
913
- s: 0,
914
- f: 0
915
- };
916
- resolver.p = new Promise((resolve, reject) => {
917
- resolver.s = resolve;
918
- resolver.f = reject;
919
- });
920
- return resolver;
921
- };
922
- var PROMISE_SUCCESS = (resolver, data) => {
923
- resolver.s(data);
924
- resolver.p.s = 1;
925
- resolver.p.v = data;
926
- };
927
- var PROMISE_FAILURE = (resolver, data) => {
928
- resolver.f(data);
929
- resolver.p.s = 2;
930
- resolver.p.v = data;
931
- };
932
- var SERIALIZED_PROMISE_CONSTRUCTOR = /* @__PURE__ */ PROMISE_CONSTRUCTOR.toString();
933
- var SERIALIZED_PROMISE_SUCCESS = /* @__PURE__ */ PROMISE_SUCCESS.toString();
934
- var SERIALIZED_PROMISE_FAILURE = /* @__PURE__ */ PROMISE_FAILURE.toString();
935
- var STREAM_CONSTRUCTOR = () => {
936
- const buffer = [];
937
- const listeners = [];
938
- let alive = true;
939
- let success = false;
940
- let count = 0;
941
- const flush = (value, mode, x) => {
942
- for (x = 0; x < count; x++) {
943
- if (listeners[x]) {
944
- listeners[x][mode](value);
945
- }
946
- }
947
- };
948
- const up = (listener, x, z, current) => {
949
- for (x = 0, z = buffer.length; x < z; x++) {
950
- current = buffer[x];
951
- if (!alive && x === z - 1) {
952
- listener[success ? "return" : "throw"](current);
953
- } else {
954
- listener.next(current);
955
- }
956
- }
957
- };
958
- const on = (listener, temp) => {
959
- if (alive) {
960
- temp = count++;
961
- listeners[temp] = listener;
962
- }
963
- up(listener);
964
- return () => {
965
- if (alive) {
966
- listeners[temp] = listeners[count];
967
- listeners[count--] = void 0;
968
- }
969
- };
970
- };
971
- return {
972
- __SEROVAL_STREAM__: true,
973
- on: (listener) => on(listener),
974
- next: (value) => {
975
- if (alive) {
976
- buffer.push(value);
977
- flush(value, "next");
978
- }
979
- },
980
- throw: (value) => {
981
- if (alive) {
982
- buffer.push(value);
983
- flush(value, "throw");
984
- alive = false;
985
- success = false;
986
- listeners.length = 0;
987
- }
988
- },
989
- return: (value) => {
990
- if (alive) {
991
- buffer.push(value);
992
- flush(value, "return");
993
- alive = false;
994
- success = true;
995
- listeners.length = 0;
996
- }
997
- }
998
- };
999
- };
1000
- var SERIALIZED_STREAM_CONSTRUCTOR = /* @__PURE__ */ STREAM_CONSTRUCTOR.toString();
1001
- var ITERATOR_CONSTRUCTOR = (symbol) => (sequence) => () => {
1002
- let index = 0;
1003
- const instance = {
1004
- [symbol]: () => instance,
1005
- next: () => {
1006
- if (index > sequence.d) {
1007
- return {
1008
- done: true,
1009
- value: void 0
1010
- };
1011
- }
1012
- const currentIndex = index++;
1013
- const data = sequence.v[currentIndex];
1014
- if (currentIndex === sequence.t) {
1015
- throw data;
1016
- }
1017
- return {
1018
- done: currentIndex === sequence.d,
1019
- value: data
1020
- };
1021
- }
1022
- };
1023
- return instance;
1024
- };
1025
- var SERIALIZED_ITERATOR_CONSTRUCTOR = /* @__PURE__ */ ITERATOR_CONSTRUCTOR.toString();
1026
- var ASYNC_ITERATOR_CONSTRUCTOR = (symbol, createPromise) => (stream) => () => {
1027
- let count = 0;
1028
- let doneAt = -1;
1029
- let isThrow = false;
1030
- const buffer = [];
1031
- const pending = [];
1032
- const finalize = (i = 0, len = pending.length) => {
1033
- for (; i < len; i++) {
1034
- pending[i].s({
1035
- done: true,
1036
- value: void 0
1037
- });
1038
- }
1039
- };
1040
- stream.on({
1041
- next: (value) => {
1042
- const temp = pending.shift();
1043
- if (temp) {
1044
- temp.s({ done: false, value });
1045
- }
1046
- buffer.push(value);
1047
- },
1048
- throw: (value) => {
1049
- const temp = pending.shift();
1050
- if (temp) {
1051
- temp.f(value);
1052
- }
1053
- finalize();
1054
- doneAt = buffer.length;
1055
- isThrow = true;
1056
- buffer.push(value);
1057
- },
1058
- return: (value) => {
1059
- const temp = pending.shift();
1060
- if (temp) {
1061
- temp.s({ done: true, value });
1062
- }
1063
- finalize();
1064
- doneAt = buffer.length;
1065
- buffer.push(value);
1066
- }
1067
- });
1068
- const instance = {
1069
- [symbol]: () => instance,
1070
- next: () => {
1071
- if (doneAt === -1) {
1072
- const index2 = count++;
1073
- if (index2 >= buffer.length) {
1074
- const temp = createPromise();
1075
- pending.push(temp);
1076
- return temp.p;
1077
- }
1078
- return {
1079
- done: false,
1080
- value: buffer[index2]
1081
- };
1082
- }
1083
- if (count > doneAt) {
1084
- return {
1085
- done: true,
1086
- value: void 0
1087
- };
1088
- }
1089
- const index = count++;
1090
- const value = buffer[index];
1091
- if (index !== doneAt) {
1092
- return {
1093
- done: false,
1094
- value
1095
- };
1096
- }
1097
- if (isThrow) {
1098
- throw value;
1099
- }
1100
- return {
1101
- done: true,
1102
- value
1103
- };
1104
- }
1105
- };
1106
- return instance;
1107
- };
1108
- var SERIALIZED_ASYNC_ITERATOR_CONSTRUCTOR = /* @__PURE__ */ ASYNC_ITERATOR_CONSTRUCTOR.toString();
1109
- var ARRAY_BUFFER_CONSTRUCTOR = (b64) => {
1110
- const decoded = atob(b64);
1111
- const length = decoded.length;
1112
- const arr = new Uint8Array(length);
1113
- for (let i = 0; i < length; i++) {
1114
- arr[i] = decoded.charCodeAt(i);
1115
- }
1116
- return arr.buffer;
1117
- };
1118
- var SERIALIZED_ARRAY_BUFFER_CONSTRUCTOR = /* @__PURE__ */ ARRAY_BUFFER_CONSTRUCTOR.toString();
1119
-
1120
- // src/core/sequence.ts
1121
- function isSequence(value) {
1122
- return "__SEROVAL_SEQUENCE__" in value;
1123
- }
1124
- function createSequence(values, throwAt, doneAt) {
1125
- return {
1126
- __SEROVAL_SEQUENCE__: true,
1127
- v: values,
1128
- t: throwAt,
1129
- d: doneAt
1130
- };
1131
- }
1132
- function createSequenceFromIterable(source) {
1133
- const values = [];
1134
- let throwsAt = -1;
1135
- let doneAt = -1;
1136
- const iterator = source[SYM_ITERATOR]();
1137
- while (true) {
1138
- try {
1139
- const value = iterator.next();
1140
- values.push(value.value);
1141
- if (value.done) {
1142
- doneAt = values.length - 1;
1143
- break;
1144
- }
1145
- } catch (error) {
1146
- throwsAt = values.length;
1147
- values.push(error);
1148
- }
1149
- }
1150
- return createSequence(values, throwsAt, doneAt);
1151
- }
1152
- var createIterator = ITERATOR_CONSTRUCTOR(SYM_ITERATOR);
1153
- function sequenceToIterator(sequence) {
1154
- return createIterator(sequence);
1155
- }
1156
-
1157
- // src/core/special-reference.ts
1158
- var ITERATOR = {};
1159
- var ASYNC_ITERATOR = {};
1160
- var SPECIAL_REFS = {
1161
- [0 /* MapSentinel */]: {},
1162
- [1 /* PromiseConstructor */]: {},
1163
- [2 /* PromiseSuccess */]: {},
1164
- [3 /* PromiseFailure */]: {},
1165
- [4 /* StreamConstructor */]: {},
1166
- [5 /* ArrayBufferConstructor */]: {}
1167
- };
1168
- var SPECIAL_REF_STRING = {
1169
- [0 /* MapSentinel */]: "[]",
1170
- [1 /* PromiseConstructor */]: SERIALIZED_PROMISE_CONSTRUCTOR,
1171
- [2 /* PromiseSuccess */]: SERIALIZED_PROMISE_SUCCESS,
1172
- [3 /* PromiseFailure */]: SERIALIZED_PROMISE_FAILURE,
1173
- [4 /* StreamConstructor */]: SERIALIZED_STREAM_CONSTRUCTOR,
1174
- [5 /* ArrayBufferConstructor */]: SERIALIZED_ARRAY_BUFFER_CONSTRUCTOR
1175
- };
1176
-
1177
- // src/core/stream.ts
1178
- function isStream(value) {
1179
- return "__SEROVAL_STREAM__" in value;
1180
- }
1181
- function createStream() {
1182
- return STREAM_CONSTRUCTOR();
1183
- }
1184
- function createStreamFromAsyncIterable(iterable) {
1185
- const stream = createStream();
1186
- const iterator = iterable[SYM_ASYNC_ITERATOR]();
1187
- async function push() {
1188
- try {
1189
- const value = await iterator.next();
1190
- if (value.done) {
1191
- stream.return(value.value);
1192
- } else {
1193
- stream.next(value.value);
1194
- await push();
1195
- }
1196
- } catch (error) {
1197
- stream.throw(error);
1198
- }
1199
- }
1200
- push().catch(() => {
1201
- });
1202
- return stream;
1203
- }
1204
- var createAsyncIterable = ASYNC_ITERATOR_CONSTRUCTOR(
1205
- SYM_ASYNC_ITERATOR,
1206
- PROMISE_CONSTRUCTOR
1207
- );
1208
- function streamToAsyncIterable(stream) {
1209
- return createAsyncIterable(
1210
- stream
1211
- );
1212
- }
1213
-
1214
- // src/core/utils/promise-to-result.ts
1215
- async function promiseToResult(current) {
1216
- try {
1217
- return [1, await current];
1218
- } catch (e) {
1219
- return [0, e];
1220
- }
1221
- }
1222
-
1223
- // src/core/context/parser.ts
1224
- function createBaseParserContext(mode, options) {
1225
- return {
1226
- plugins: options.plugins,
1227
- mode,
1228
- marked: /* @__PURE__ */ new Set(),
1229
- features: ALL_ENABLED ^ (options.disabledFeatures || 0),
1230
- refs: options.refs || /* @__PURE__ */ new Map(),
1231
- depthLimit: options.depthLimit || 1e3
1232
- };
1233
- }
1234
- function markParserRef(ctx, id) {
1235
- ctx.marked.add(id);
1236
- }
1237
- function createIndexForValue(ctx, current) {
1238
- const id = ctx.refs.size;
1239
- ctx.refs.set(current, id);
1240
- return id;
1241
- }
1242
- function getNodeForIndexedValue(ctx, current) {
1243
- const registeredId = ctx.refs.get(current);
1244
- if (registeredId != null) {
1245
- markParserRef(ctx, registeredId);
1246
- return {
1247
- type: 1 /* Indexed */,
1248
- value: createIndexedValueNode(registeredId)
1249
- };
1250
- }
1251
- return {
1252
- type: 0 /* Fresh */,
1253
- value: createIndexForValue(ctx, current)
1254
- };
1255
- }
1256
- function getReferenceNode(ctx, current) {
1257
- const indexed = getNodeForIndexedValue(ctx, current);
1258
- if (indexed.type === 1 /* Indexed */) {
1259
- return indexed;
1260
- }
1261
- if (hasReferenceID(current)) {
1262
- return {
1263
- type: 2 /* Referenced */,
1264
- value: createReferenceNode(indexed.value, current)
1265
- };
1266
- }
1267
- return indexed;
1268
- }
1269
- function parseWellKnownSymbol(ctx, current) {
1270
- const ref = getReferenceNode(ctx, current);
1271
- if (ref.type !== 0 /* Fresh */) {
1272
- return ref.value;
1273
- }
1274
- if (current in INV_SYMBOL_REF) {
1275
- return createWKSymbolNode(ref.value, current);
1276
- }
1277
- throw new SerovalUnsupportedTypeError(current);
1278
- }
1279
- function parseSpecialReference(ctx, ref) {
1280
- const result = getNodeForIndexedValue(ctx, SPECIAL_REFS[ref]);
1281
- if (result.type === 1 /* Indexed */) {
1282
- return result.value;
1283
- }
1284
- return createSerovalNode(
1285
- 26 /* SpecialReference */,
1286
- result.value,
1287
- ref,
1288
- NIL,
1289
- NIL,
1290
- NIL,
1291
- NIL,
1292
- NIL,
1293
- NIL,
1294
- NIL,
1295
- NIL,
1296
- NIL
1297
- );
1298
- }
1299
- function parseIteratorFactory(ctx) {
1300
- const result = getNodeForIndexedValue(ctx, ITERATOR);
1301
- if (result.type === 1 /* Indexed */) {
1302
- return result.value;
1303
- }
1304
- return createSerovalNode(
1305
- 27 /* IteratorFactory */,
1306
- result.value,
1307
- NIL,
1308
- NIL,
1309
- NIL,
1310
- NIL,
1311
- NIL,
1312
- NIL,
1313
- parseWellKnownSymbol(ctx, SYM_ITERATOR),
1314
- NIL,
1315
- NIL,
1316
- NIL
1317
- );
1318
- }
1319
- function parseAsyncIteratorFactory(ctx) {
1320
- const result = getNodeForIndexedValue(ctx, ASYNC_ITERATOR);
1321
- if (result.type === 1 /* Indexed */) {
1322
- return result.value;
1323
- }
1324
- return createSerovalNode(
1325
- 29 /* AsyncIteratorFactory */,
1326
- result.value,
1327
- NIL,
1328
- NIL,
1329
- NIL,
1330
- NIL,
1331
- NIL,
1332
- [
1333
- parseSpecialReference(ctx, 1 /* PromiseConstructor */),
1334
- parseWellKnownSymbol(ctx, SYM_ASYNC_ITERATOR)
1335
- ],
1336
- NIL,
1337
- NIL,
1338
- NIL,
1339
- NIL
1340
- );
1341
- }
1342
- function createObjectNode(id, current, empty, record) {
1343
- return createSerovalNode(
1344
- empty ? 11 /* NullConstructor */ : 10 /* Object */,
1345
- id,
1346
- NIL,
1347
- NIL,
1348
- NIL,
1349
- record,
1350
- NIL,
1351
- NIL,
1352
- NIL,
1353
- NIL,
1354
- getObjectFlag(current),
1355
- NIL
1356
- );
1357
- }
1358
- function createMapNode(ctx, id, k, v) {
1359
- return createSerovalNode(
1360
- 8 /* Map */,
1361
- id,
1362
- NIL,
1363
- NIL,
1364
- NIL,
1365
- NIL,
1366
- { k, v },
1367
- NIL,
1368
- parseSpecialReference(ctx, 0 /* MapSentinel */),
1369
- NIL,
1370
- NIL,
1371
- NIL
1372
- );
1373
- }
1374
- function createPromiseConstructorNode(ctx, id, resolver) {
1375
- return createSerovalNode(
1376
- 22 /* PromiseConstructor */,
1377
- id,
1378
- resolver,
1379
- NIL,
1380
- NIL,
1381
- NIL,
1382
- NIL,
1383
- NIL,
1384
- parseSpecialReference(ctx, 1 /* PromiseConstructor */),
1385
- NIL,
1386
- NIL,
1387
- NIL
1388
- );
1389
- }
1390
- function createArrayBufferNode(ctx, id, current) {
1391
- const bytes = new Uint8Array(current);
1392
- let result = "";
1393
- for (let i = 0, len = bytes.length; i < len; i++) {
1394
- result += String.fromCharCode(bytes[i]);
1395
- }
1396
- return createSerovalNode(
1397
- 19 /* ArrayBuffer */,
1398
- id,
1399
- serializeString(btoa(result)),
1400
- NIL,
1401
- NIL,
1402
- NIL,
1403
- NIL,
1404
- NIL,
1405
- parseSpecialReference(ctx, 5 /* ArrayBufferConstructor */),
1406
- NIL,
1407
- NIL,
1408
- NIL
1409
- );
1410
- }
1411
-
1412
- // src/core/context/async-parser.ts
1413
- function createAsyncParserContext(mode, options) {
1414
- return {
1415
- base: createBaseParserContext(mode, options),
1416
- child: void 0
1417
- };
1418
- }
1419
- var AsyncParsePluginContext = class {
1420
- constructor(_p, depth) {
1421
- this._p = _p;
1422
- this.depth = depth;
1423
- }
1424
- parse(current) {
1425
- return parseAsync(this._p, this.depth, current);
1426
- }
1427
- };
1428
- async function parseItems(ctx, depth, current) {
1429
- const nodes = [];
1430
- for (let i = 0, len = current.length; i < len; i++) {
1431
- if (i in current) {
1432
- nodes[i] = await parseAsync(ctx, depth, current[i]);
1433
- } else {
1434
- nodes[i] = 0;
1435
- }
1436
- }
1437
- return nodes;
1438
- }
1439
- async function parseArray(ctx, depth, id, current) {
1440
- return createArrayNode(id, current, await parseItems(ctx, depth, current));
1441
- }
1442
- async function parseProperties(ctx, depth, properties) {
1443
- const entries = Object.entries(properties);
1444
- const keyNodes = [];
1445
- const valueNodes = [];
1446
- for (let i = 0, len = entries.length; i < len; i++) {
1447
- keyNodes.push(serializeString(entries[i][0]));
1448
- valueNodes.push(await parseAsync(ctx, depth, entries[i][1]));
1449
- }
1450
- if (SYM_ITERATOR in properties) {
1451
- keyNodes.push(parseWellKnownSymbol(ctx.base, SYM_ITERATOR));
1452
- valueNodes.push(
1453
- createIteratorFactoryInstanceNode(
1454
- parseIteratorFactory(ctx.base),
1455
- await parseAsync(
1456
- ctx,
1457
- depth,
1458
- createSequenceFromIterable(
1459
- properties
1460
- )
1461
- )
1462
- )
1463
- );
1464
- }
1465
- if (SYM_ASYNC_ITERATOR in properties) {
1466
- keyNodes.push(parseWellKnownSymbol(ctx.base, SYM_ASYNC_ITERATOR));
1467
- valueNodes.push(
1468
- createAsyncIteratorFactoryInstanceNode(
1469
- parseAsyncIteratorFactory(ctx.base),
1470
- await parseAsync(
1471
- ctx,
1472
- depth,
1473
- createStreamFromAsyncIterable(
1474
- properties
1475
- )
1476
- )
1477
- )
1478
- );
1479
- }
1480
- if (SYM_TO_STRING_TAG in properties) {
1481
- keyNodes.push(parseWellKnownSymbol(ctx.base, SYM_TO_STRING_TAG));
1482
- valueNodes.push(createStringNode(properties[SYM_TO_STRING_TAG]));
1483
- }
1484
- if (SYM_IS_CONCAT_SPREADABLE in properties) {
1485
- keyNodes.push(parseWellKnownSymbol(ctx.base, SYM_IS_CONCAT_SPREADABLE));
1486
- valueNodes.push(
1487
- properties[SYM_IS_CONCAT_SPREADABLE] ? TRUE_NODE : FALSE_NODE
1488
- );
1489
- }
1490
- return {
1491
- k: keyNodes,
1492
- v: valueNodes
1493
- };
1494
- }
1495
- async function parsePlainObject(ctx, depth, id, current, empty) {
1496
- return createObjectNode(
1497
- id,
1498
- current,
1499
- empty,
1500
- await parseProperties(ctx, depth, current)
1501
- );
1502
- }
1503
- async function parseBoxed(ctx, depth, id, current) {
1504
- return createBoxedNode(id, await parseAsync(ctx, depth, current.valueOf()));
1505
- }
1506
- async function parseTypedArray(ctx, depth, id, current) {
1507
- return createTypedArrayNode(
1508
- id,
1509
- current,
1510
- await parseAsync(ctx, depth, current.buffer)
1511
- );
1512
- }
1513
- async function parseBigIntTypedArray(ctx, depth, id, current) {
1514
- return createBigIntTypedArrayNode(
1515
- id,
1516
- current,
1517
- await parseAsync(ctx, depth, current.buffer)
1518
- );
1519
- }
1520
- async function parseDataView(ctx, depth, id, current) {
1521
- return createDataViewNode(
1522
- id,
1523
- current,
1524
- await parseAsync(ctx, depth, current.buffer)
1525
- );
1526
- }
1527
- async function parseError(ctx, depth, id, current) {
1528
- const options = getErrorOptions(current, ctx.base.features);
1529
- return createErrorNode(
1530
- id,
1531
- current,
1532
- options ? await parseProperties(ctx, depth, options) : NIL
1533
- );
1534
- }
1535
- async function parseAggregateError(ctx, depth, id, current) {
1536
- const options = getErrorOptions(current, ctx.base.features);
1537
- return createAggregateErrorNode(
1538
- id,
1539
- current,
1540
- options ? await parseProperties(ctx, depth, options) : NIL
1541
- );
1542
- }
1543
- async function parseMap(ctx, depth, id, current) {
1544
- const keyNodes = [];
1545
- const valueNodes = [];
1546
- for (const [key, value] of current.entries()) {
1547
- keyNodes.push(await parseAsync(ctx, depth, key));
1548
- valueNodes.push(await parseAsync(ctx, depth, value));
1549
- }
1550
- return createMapNode(ctx.base, id, keyNodes, valueNodes);
1551
- }
1552
- async function parseSet(ctx, depth, id, current) {
1553
- const items = [];
1554
- for (const item of current.keys()) {
1555
- items.push(await parseAsync(ctx, depth, item));
1556
- }
1557
- return createSetNode(id, items);
1558
- }
1559
- async function parsePlugin(ctx, depth, id, current) {
1560
- const currentPlugins = ctx.base.plugins;
1561
- if (currentPlugins) {
1562
- for (let i = 0, len = currentPlugins.length; i < len; i++) {
1563
- const plugin = currentPlugins[i];
1564
- if (plugin.parse.async && plugin.test(current)) {
1565
- return createPluginNode(
1566
- id,
1567
- plugin.tag,
1568
- await plugin.parse.async(
1569
- current,
1570
- new AsyncParsePluginContext(ctx, depth),
1571
- {
1572
- id
1573
- }
1574
- )
1575
- );
1576
- }
1577
- }
1578
- }
1579
- return NIL;
1580
- }
1581
- async function parsePromise(ctx, depth, id, current) {
1582
- const [status, result] = await promiseToResult(current);
1583
- return createSerovalNode(
1584
- 12 /* Promise */,
1585
- id,
1586
- status,
1587
- NIL,
1588
- NIL,
1589
- NIL,
1590
- NIL,
1591
- NIL,
1592
- await parseAsync(ctx, depth, result),
1593
- NIL,
1594
- NIL,
1595
- NIL
1596
- );
1597
- }
1598
- function parseStreamHandle(depth, id, current, resolve, reject) {
1599
- const sequence = [];
1600
- const cleanup = current.on({
1601
- next: (value) => {
1602
- markParserRef(this.base, id);
1603
- parseAsync(this, depth, value).then(
1604
- (data) => {
1605
- sequence.push(createStreamNextNode(id, data));
1606
- },
1607
- (data) => {
1608
- reject(data);
1609
- cleanup();
1610
- }
1611
- );
1612
- },
1613
- throw: (value) => {
1614
- markParserRef(this.base, id);
1615
- parseAsync(this, depth, value).then(
1616
- (data) => {
1617
- sequence.push(createStreamThrowNode(id, data));
1618
- resolve(sequence);
1619
- cleanup();
1620
- },
1621
- (data) => {
1622
- reject(data);
1623
- cleanup();
1624
- }
1625
- );
1626
- },
1627
- return: (value) => {
1628
- markParserRef(this.base, id);
1629
- parseAsync(this, depth, value).then(
1630
- (data) => {
1631
- sequence.push(createStreamReturnNode(id, data));
1632
- resolve(sequence);
1633
- cleanup();
1634
- },
1635
- (data) => {
1636
- reject(data);
1637
- cleanup();
1638
- }
1639
- );
1640
- }
1641
- });
1642
- }
1643
- async function parseStream(ctx, depth, id, current) {
1644
- return createStreamConstructorNode(
1645
- id,
1646
- parseSpecialReference(ctx.base, 4 /* StreamConstructor */),
1647
- await new Promise(
1648
- parseStreamHandle.bind(ctx, depth, id, current)
1649
- )
1650
- );
1651
- }
1652
- async function parseSequence(ctx, depth, id, current) {
1653
- const nodes = [];
1654
- for (let i = 0, len = current.v.length; i < len; i++) {
1655
- nodes[i] = await parseAsync(ctx, depth, current.v[i]);
1656
- }
1657
- return createSequenceNode(id, nodes, current.t, current.d);
1658
- }
1659
- async function parseObjectAsync(ctx, depth, id, current) {
1660
- if (Array.isArray(current)) {
1661
- return parseArray(ctx, depth, id, current);
1662
- }
1663
- if (isStream(current)) {
1664
- return parseStream(ctx, depth, id, current);
1665
- }
1666
- if (isSequence(current)) {
1667
- return parseSequence(ctx, depth, id, current);
1668
- }
1669
- const currentClass = current.constructor;
1670
- if (currentClass === OpaqueReference) {
1671
- return parseAsync(
1672
- ctx,
1673
- depth,
1674
- current.replacement
1675
- );
1676
- }
1677
- const parsed = await parsePlugin(ctx, depth, id, current);
1678
- if (parsed) {
1679
- return parsed;
1680
- }
1681
- switch (currentClass) {
1682
- case Object:
1683
- return parsePlainObject(
1684
- ctx,
1685
- depth,
1686
- id,
1687
- current,
1688
- false
1689
- );
1690
- case NIL:
1691
- return parsePlainObject(
1692
- ctx,
1693
- depth,
1694
- id,
1695
- current,
1696
- true
1697
- );
1698
- case Date:
1699
- return createDateNode(id, current);
1700
- case Error:
1701
- case EvalError:
1702
- case RangeError:
1703
- case ReferenceError:
1704
- case SyntaxError:
1705
- case TypeError:
1706
- case URIError:
1707
- return parseError(ctx, depth, id, current);
1708
- case Number:
1709
- case Boolean:
1710
- case String:
1711
- case BigInt:
1712
- return parseBoxed(ctx, depth, id, current);
1713
- case ArrayBuffer:
1714
- return createArrayBufferNode(
1715
- ctx.base,
1716
- id,
1717
- current
1718
- );
1719
- case Int8Array:
1720
- case Int16Array:
1721
- case Int32Array:
1722
- case Uint8Array:
1723
- case Uint16Array:
1724
- case Uint32Array:
1725
- case Uint8ClampedArray:
1726
- case Float32Array:
1727
- case Float64Array:
1728
- return parseTypedArray(
1729
- ctx,
1730
- depth,
1731
- id,
1732
- current
1733
- );
1734
- case DataView:
1735
- return parseDataView(ctx, depth, id, current);
1736
- case Map:
1737
- return parseMap(
1738
- ctx,
1739
- depth,
1740
- id,
1741
- current
1742
- );
1743
- case Set:
1744
- return parseSet(ctx, depth, id, current);
1745
- default:
1746
- break;
1747
- }
1748
- if (currentClass === Promise || current instanceof Promise) {
1749
- return parsePromise(ctx, depth, id, current);
1750
- }
1751
- const currentFeatures = ctx.base.features;
1752
- if (currentFeatures & 32 /* RegExp */ && currentClass === RegExp) {
1753
- return createRegExpNode(id, current);
1754
- }
1755
- if (currentFeatures & 16 /* BigIntTypedArray */) {
1756
- switch (currentClass) {
1757
- case BigInt64Array:
1758
- case BigUint64Array:
1759
- return parseBigIntTypedArray(
1760
- ctx,
1761
- depth,
1762
- id,
1763
- current
1764
- );
1765
- default:
1766
- break;
1767
- }
1768
- }
1769
- if (currentFeatures & 1 /* AggregateError */ && typeof AggregateError !== "undefined" && (currentClass === AggregateError || current instanceof AggregateError)) {
1770
- return parseAggregateError(
1771
- ctx,
1772
- depth,
1773
- id,
1774
- current
1775
- );
1776
- }
1777
- if (current instanceof Error) {
1778
- return parseError(ctx, depth, id, current);
1779
- }
1780
- if (SYM_ITERATOR in current || SYM_ASYNC_ITERATOR in current) {
1781
- return parsePlainObject(ctx, depth, id, current, !!currentClass);
1782
- }
1783
- throw new SerovalUnsupportedTypeError(current);
1784
- }
1785
- async function parseFunctionAsync(ctx, depth, current) {
1786
- const ref = getReferenceNode(ctx.base, current);
1787
- if (ref.type !== 0 /* Fresh */) {
1788
- return ref.value;
1789
- }
1790
- const plugin = await parsePlugin(ctx, depth, ref.value, current);
1791
- if (plugin) {
1792
- return plugin;
1793
- }
1794
- throw new SerovalUnsupportedTypeError(current);
1795
- }
1796
- async function parseAsync(ctx, depth, current) {
1797
- if (depth >= ctx.base.depthLimit) {
1798
- throw new SerovalDepthLimitError(ctx.base.depthLimit);
1799
- }
1800
- switch (typeof current) {
1801
- case "boolean":
1802
- return current ? TRUE_NODE : FALSE_NODE;
1803
- case "undefined":
1804
- return UNDEFINED_NODE;
1805
- case "string":
1806
- return createStringNode(current);
1807
- case "number":
1808
- return createNumberNode(current);
1809
- case "bigint":
1810
- return createBigIntNode(current);
1811
- case "object": {
1812
- if (current) {
1813
- const ref = getReferenceNode(ctx.base, current);
1814
- return ref.type === 0 ? await parseObjectAsync(ctx, depth + 1, ref.value, current) : ref.value;
1815
- }
1816
- return NULL_NODE;
1817
- }
1818
- case "symbol":
1819
- return parseWellKnownSymbol(ctx.base, current);
1820
- case "function":
1821
- return parseFunctionAsync(ctx, depth, current);
1822
- default:
1823
- throw new SerovalUnsupportedTypeError(current);
1824
- }
1825
- }
1826
- async function parseTopAsync(ctx, current) {
1827
- try {
1828
- return await parseAsync(ctx, 0, current);
1829
- } catch (error) {
1830
- throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
1831
- }
1832
- }
1833
-
1834
- // src/core/plugin.ts
1835
- var SerovalMode = /* @__PURE__ */ ((SerovalMode2) => {
1836
- SerovalMode2[SerovalMode2["Vanilla"] = 1] = "Vanilla";
1837
- SerovalMode2[SerovalMode2["Cross"] = 2] = "Cross";
1838
- return SerovalMode2;
1839
- })(SerovalMode || {});
1840
- function createPlugin(plugin) {
1841
- return plugin;
1842
- }
1843
- function dedupePlugins(deduped, plugins) {
1844
- for (let i = 0, len = plugins.length; i < len; i++) {
1845
- const current = plugins[i];
1846
- if (!deduped.has(current)) {
1847
- deduped.add(current);
1848
- if (current.extends) {
1849
- dedupePlugins(deduped, current.extends);
1850
- }
1851
- }
1852
- }
1853
- }
1854
- function resolvePlugins(plugins) {
1855
- if (plugins) {
1856
- const deduped = /* @__PURE__ */ new Set();
1857
- dedupePlugins(deduped, plugins);
1858
- return [...deduped];
1859
- }
1860
- return void 0;
1861
- }
1862
-
1863
- // src/core/utils/typed-array.ts
1864
- function getTypedArrayConstructor(name) {
1865
- switch (name) {
1866
- case "Int8Array":
1867
- return Int8Array;
1868
- case "Int16Array":
1869
- return Int16Array;
1870
- case "Int32Array":
1871
- return Int32Array;
1872
- case "Uint8Array":
1873
- return Uint8Array;
1874
- case "Uint16Array":
1875
- return Uint16Array;
1876
- case "Uint32Array":
1877
- return Uint32Array;
1878
- case "Uint8ClampedArray":
1879
- return Uint8ClampedArray;
1880
- case "Float32Array":
1881
- return Float32Array;
1882
- case "Float64Array":
1883
- return Float64Array;
1884
- case "BigInt64Array":
1885
- return BigInt64Array;
1886
- case "BigUint64Array":
1887
- return BigUint64Array;
1888
- default:
1889
- throw new SerovalUnknownTypedArrayError(name);
1890
- }
1891
- }
1892
-
1893
- // src/core/utils/valid-properties.ts
1894
- function isValidKey(key) {
1895
- switch (key) {
1896
- case "constructor":
1897
- case "__proto__":
1898
- case "prototype":
1899
- case "__defineGetter__":
1900
- case "__defineSetter__":
1901
- case "__lookupGetter__":
1902
- case "__lookupSetter__":
1903
- return false;
1904
- default:
1905
- return true;
1906
- }
1907
- }
1908
- function isValidSymbol(symbol) {
1909
- switch (symbol) {
1910
- case SYM_ASYNC_ITERATOR:
1911
- case SYM_IS_CONCAT_SPREADABLE:
1912
- case SYM_TO_STRING_TAG:
1913
- case SYM_ITERATOR:
1914
- return true;
1915
- default:
1916
- return false;
1917
- }
1918
- }
1919
-
1920
- // src/core/context/deserializer.ts
1921
- var MAX_BASE64_LENGTH = 1e6;
1922
- var MAX_BIGINT_LENGTH = 1e4;
1923
- var MAX_REGEXP_SOURCE_LENGTH = 2e4;
1924
- function applyObjectFlag(obj, flag) {
1925
- switch (flag) {
1926
- case 3 /* Frozen */:
1927
- return Object.freeze(obj);
1928
- case 1 /* NonExtensible */:
1929
- return Object.preventExtensions(obj);
1930
- case 2 /* Sealed */:
1931
- return Object.seal(obj);
1932
- default:
1933
- return obj;
1934
- }
1935
- }
1936
- var DEFAULT_DEPTH_LIMIT = 1e3;
1937
- function createBaseDeserializerContext(mode, options) {
1938
- var _a;
1939
- const refs = options.refs || /* @__PURE__ */ new Map();
1940
- if (!("types" in refs)) {
1941
- Object.assign(refs, {
1942
- types: /* @__PURE__ */ new Map()
1943
- });
1944
- }
1945
- return {
1946
- mode,
1947
- plugins: options.plugins,
1948
- refs,
1949
- features: (_a = options.features) != null ? _a : ALL_ENABLED ^ (options.disabledFeatures || 0),
1950
- depthLimit: options.depthLimit || DEFAULT_DEPTH_LIMIT
1951
- };
1952
- }
1953
- function createVanillaDeserializerContext(options) {
1954
- return {
1955
- mode: 1 /* Vanilla */,
1956
- base: createBaseDeserializerContext(1 /* Vanilla */, options),
1957
- child: NIL,
1958
- state: {
1959
- marked: new Set(options.markedRefs)
1960
- }
1961
- };
1962
- }
1963
- function createCrossDeserializerContext(options) {
1964
- return {
1965
- mode: 2 /* Cross */,
1966
- base: createBaseDeserializerContext(2 /* Cross */, options),
1967
- child: NIL
1968
- };
1969
- }
1970
- var DeserializePluginContext = class {
1971
- constructor(_p, depth) {
1972
- this._p = _p;
1973
- this.depth = depth;
1974
- }
1975
- deserialize(node) {
1976
- return deserialize(this._p, this.depth, node);
1977
- }
1978
- };
1979
- function guardIndexedValue(ctx, id) {
1980
- if (id < 0 || !Number.isFinite(id) || !Number.isInteger(id)) {
1981
- throw new SerovalMalformedNodeError({
1982
- t: 4 /* IndexedValue */,
1983
- i: id
1984
- });
1985
- }
1986
- if (ctx.refs.has(id)) {
1987
- throw new Error("Conflicted ref id: " + id);
1988
- }
1989
- }
1990
- function assignIndexedValueVanilla(ctx, id, value) {
1991
- guardIndexedValue(ctx.base, id);
1992
- if (ctx.state.marked.has(id)) {
1993
- ctx.base.refs.set(id, value);
1994
- }
1995
- return value;
1996
- }
1997
- function assignIndexedValueCross(ctx, id, value) {
1998
- guardIndexedValue(ctx.base, id);
1999
- ctx.base.refs.set(id, value);
2000
- return value;
2001
- }
2002
- function assignIndexedValue(ctx, id, value) {
2003
- return ctx.mode === 1 /* Vanilla */ ? assignIndexedValueVanilla(ctx, id, value) : assignIndexedValueCross(ctx, id, value);
2004
- }
2005
- function deserializeKnownValue(node, record, key) {
2006
- if (Object.hasOwn(record, key)) {
2007
- return record[key];
2008
- }
2009
- throw new SerovalMalformedNodeError(node);
2010
- }
2011
- function deserializeReference(ctx, node) {
2012
- return assignIndexedValue(
2013
- ctx,
2014
- node.i,
2015
- getReference(deserializeString(node.s))
2016
- );
2017
- }
2018
- function deserializeArray(ctx, depth, node) {
2019
- const items = node.a;
2020
- const len = items.length;
2021
- const result = assignIndexedValue(
2022
- ctx,
2023
- node.i,
2024
- new Array(len)
2025
- );
2026
- for (let i = 0, item; i < len; i++) {
2027
- item = items[i];
2028
- if (item) {
2029
- result[i] = deserialize(ctx, depth, item);
2030
- }
2031
- }
2032
- applyObjectFlag(result, node.o);
2033
- return result;
2034
- }
2035
- function assignStringProperty(object, key, value) {
2036
- if (isValidKey(key)) {
2037
- object[key] = value;
2038
- } else {
2039
- Object.defineProperty(object, key, {
2040
- value,
2041
- configurable: true,
2042
- enumerable: true,
2043
- writable: true
2044
- });
2045
- }
2046
- }
2047
- function assignProperty(ctx, depth, object, key, value) {
2048
- if (typeof key === "string") {
2049
- assignStringProperty(
2050
- object,
2051
- deserializeString(key),
2052
- deserialize(ctx, depth, value)
2053
- );
2054
- } else {
2055
- const actual = deserialize(ctx, depth, key);
2056
- switch (typeof actual) {
2057
- case "string":
2058
- assignStringProperty(object, actual, deserialize(ctx, depth, value));
2059
- break;
2060
- case "symbol":
2061
- if (isValidSymbol(actual)) {
2062
- object[actual] = deserialize(ctx, depth, value);
2063
- }
2064
- break;
2065
- default:
2066
- throw new SerovalMalformedNodeError(key);
2067
- }
2068
- }
2069
- }
2070
- function assignNodeType(ctx, id, type) {
2071
- ctx.base.refs.types.set(id, type);
2072
- }
2073
- function validateNodeType(ctx, node, id, type) {
2074
- if (ctx.base.refs.types.get(id) !== type) {
2075
- throw new SerovalMalformedNodeError(node);
2076
- }
2077
- }
2078
- function deserializeProperties(ctx, depth, node, result) {
2079
- const keys = node.k;
2080
- const len = keys.length;
2081
- if (len > 0) {
2082
- for (let i = 0, vals = node.v, len2 = keys.length; i < len2; i++) {
2083
- assignProperty(ctx, depth, result, keys[i], vals[i]);
2084
- }
2085
- }
2086
- return result;
2087
- }
2088
- function deserializeObject(ctx, depth, node) {
2089
- const result = assignIndexedValue(
2090
- ctx,
2091
- node.i,
2092
- node.t === 10 /* Object */ ? {} : /* @__PURE__ */ Object.create(null)
2093
- );
2094
- deserializeProperties(ctx, depth, node.p, result);
2095
- applyObjectFlag(result, node.o);
2096
- return result;
2097
- }
2098
- function deserializeDate(ctx, node) {
2099
- return assignIndexedValue(ctx, node.i, new Date(node.s));
2100
- }
2101
- function deserializeRegExp(ctx, node) {
2102
- if (ctx.base.features & 32 /* RegExp */) {
2103
- const source = deserializeString(node.c);
2104
- if (source.length > MAX_REGEXP_SOURCE_LENGTH) {
2105
- throw new SerovalMalformedNodeError(node);
2106
- }
2107
- return assignIndexedValue(ctx, node.i, new RegExp(source, node.m));
2108
- }
2109
- throw new SerovalUnsupportedNodeError(node);
2110
- }
2111
- function deserializeSet(ctx, depth, node) {
2112
- const result = assignIndexedValue(ctx, node.i, /* @__PURE__ */ new Set());
2113
- for (let i = 0, items = node.a, len = items.length; i < len; i++) {
2114
- result.add(deserialize(ctx, depth, items[i]));
2115
- }
2116
- return result;
2117
- }
2118
- function deserializeMap(ctx, depth, node) {
2119
- const result = assignIndexedValue(ctx, node.i, /* @__PURE__ */ new Map());
2120
- for (let i = 0, keys = node.e.k, vals = node.e.v, len = keys.length; i < len; i++) {
2121
- result.set(
2122
- deserialize(ctx, depth, keys[i]),
2123
- deserialize(ctx, depth, vals[i])
2124
- );
2125
- }
2126
- return result;
2127
- }
2128
- function deserializeArrayBuffer(ctx, node) {
2129
- if (node.s.length > MAX_BASE64_LENGTH) {
2130
- throw new SerovalMalformedNodeError(node);
2131
- }
2132
- const result = assignIndexedValue(
2133
- ctx,
2134
- node.i,
2135
- ARRAY_BUFFER_CONSTRUCTOR(deserializeString(node.s))
2136
- );
2137
- return result;
2138
- }
2139
- function deserializeTypedArray(ctx, depth, node) {
2140
- var _a;
2141
- const construct = getTypedArrayConstructor(node.c);
2142
- const source = deserialize(ctx, depth, node.f);
2143
- const offset = (_a = node.b) != null ? _a : 0;
2144
- if (offset < 0 || offset > source.byteLength) {
2145
- throw new SerovalMalformedNodeError(node);
2146
- }
2147
- const result = assignIndexedValue(
2148
- ctx,
2149
- node.i,
2150
- new construct(source, offset, node.l)
2151
- );
2152
- return result;
2153
- }
2154
- function deserializeDataView(ctx, depth, node) {
2155
- var _a;
2156
- const source = deserialize(ctx, depth, node.f);
2157
- const offset = (_a = node.b) != null ? _a : 0;
2158
- if (offset < 0 || offset > source.byteLength) {
2159
- throw new SerovalMalformedNodeError(node);
2160
- }
2161
- const result = assignIndexedValue(
2162
- ctx,
2163
- node.i,
2164
- new DataView(source, offset, node.l)
2165
- );
2166
- return result;
2167
- }
2168
- function deserializeDictionary(ctx, depth, node, result) {
2169
- if (node.p) {
2170
- const fields = deserializeProperties(ctx, depth, node.p, {});
2171
- Object.defineProperties(result, Object.getOwnPropertyDescriptors(fields));
2172
- }
2173
- return result;
2174
- }
2175
- function deserializeAggregateError(ctx, depth, node) {
2176
- const result = assignIndexedValue(
2177
- ctx,
2178
- node.i,
2179
- new AggregateError([], deserializeString(node.m))
2180
- );
2181
- return deserializeDictionary(ctx, depth, node, result);
2182
- }
2183
- function deserializeError(ctx, depth, node) {
2184
- const construct = deserializeKnownValue(node, ERROR_CONSTRUCTOR, node.s);
2185
- const result = assignIndexedValue(
2186
- ctx,
2187
- node.i,
2188
- new construct(deserializeString(node.m))
2189
- );
2190
- return deserializeDictionary(ctx, depth, node, result);
2191
- }
2192
- function deserializePromise(ctx, depth, node) {
2193
- const deferred = PROMISE_CONSTRUCTOR();
2194
- const result = assignIndexedValue(ctx, node.i, deferred.p);
2195
- const deserialized = deserialize(ctx, depth, node.f);
2196
- if (node.s) {
2197
- deferred.s(deserialized);
2198
- } else {
2199
- deferred.f(deserialized);
2200
- }
2201
- return result;
2202
- }
2203
- function deserializeBoxed(ctx, depth, node) {
2204
- return assignIndexedValue(
2205
- ctx,
2206
- node.i,
2207
- // biome-ignore lint/style/useConsistentBuiltinInstantiation: intended
2208
- Object(deserialize(ctx, depth, node.f))
2209
- );
2210
- }
2211
- function deserializePlugin(ctx, depth, node) {
2212
- const currentPlugins = ctx.base.plugins;
2213
- if (currentPlugins) {
2214
- const tag = deserializeString(node.c);
2215
- for (let i = 0, len = currentPlugins.length; i < len; i++) {
2216
- const plugin = currentPlugins[i];
2217
- if (plugin.tag === tag) {
2218
- return assignIndexedValue(
2219
- ctx,
2220
- node.i,
2221
- plugin.deserialize(node.s, new DeserializePluginContext(ctx, depth), {
2222
- id: node.i
2223
- })
2224
- );
2225
- }
2226
- }
2227
- }
2228
- throw new SerovalMissingPluginError(node.c);
2229
- }
2230
- function deserializePromiseConstructor(ctx, node) {
2231
- const value = assignIndexedValue(
2232
- ctx,
2233
- node.i,
2234
- assignIndexedValue(ctx, node.s, PROMISE_CONSTRUCTOR()).p
2235
- );
2236
- assignNodeType(ctx, node.s, 22 /* PromiseConstructor */);
2237
- return value;
2238
- }
2239
- function deserializePromiseResolve(ctx, depth, node) {
2240
- const deferred = ctx.base.refs.get(node.i);
2241
- if (deferred) {
2242
- validateNodeType(ctx, node, node.i, 22 /* PromiseConstructor */);
2243
- deferred.s(deserialize(ctx, depth, node.a[1]));
2244
- return NIL;
2245
- }
2246
- throw new SerovalMissingInstanceError("Promise");
2247
- }
2248
- function deserializePromiseReject(ctx, depth, node) {
2249
- const deferred = ctx.base.refs.get(node.i);
2250
- if (deferred) {
2251
- validateNodeType(ctx, node, node.i, 22 /* PromiseConstructor */);
2252
- deferred.f(deserialize(ctx, depth, node.a[1]));
2253
- return NIL;
2254
- }
2255
- throw new SerovalMissingInstanceError("Promise");
2256
- }
2257
- function deserializeIteratorFactoryInstance(ctx, depth, node) {
2258
- deserialize(ctx, depth, node.a[0]);
2259
- const source = deserialize(ctx, depth, node.a[1]);
2260
- return sequenceToIterator(source);
2261
- }
2262
- function deserializeAsyncIteratorFactoryInstance(ctx, depth, node) {
2263
- deserialize(ctx, depth, node.a[0]);
2264
- const source = deserialize(ctx, depth, node.a[1]);
2265
- return streamToAsyncIterable(source);
2266
- }
2267
- function deserializeStreamConstructor(ctx, depth, node) {
2268
- const result = assignIndexedValue(ctx, node.i, createStream());
2269
- assignNodeType(ctx, node.i, 31 /* StreamConstructor */);
2270
- const items = node.a;
2271
- const len = items.length;
2272
- if (len) {
2273
- for (let i = 0; i < len; i++) {
2274
- deserialize(ctx, depth, items[i]);
2275
- }
2276
- }
2277
- return result;
2278
- }
2279
- function deserializeStreamNext(ctx, depth, node) {
2280
- const deferred = ctx.base.refs.get(node.i);
2281
- if (deferred) {
2282
- validateNodeType(ctx, node, node.i, 31 /* StreamConstructor */);
2283
- deferred.next(deserialize(ctx, depth, node.f));
2284
- return NIL;
2285
- }
2286
- throw new SerovalMissingInstanceError("Stream");
2287
- }
2288
- function deserializeStreamThrow(ctx, depth, node) {
2289
- const deferred = ctx.base.refs.get(node.i);
2290
- if (deferred) {
2291
- validateNodeType(ctx, node, node.i, 31 /* StreamConstructor */);
2292
- deferred.throw(deserialize(ctx, depth, node.f));
2293
- return NIL;
2294
- }
2295
- throw new SerovalMissingInstanceError("Stream");
2296
- }
2297
- function deserializeStreamReturn(ctx, depth, node) {
2298
- const deferred = ctx.base.refs.get(node.i);
2299
- if (deferred) {
2300
- validateNodeType(ctx, node, node.i, 31 /* StreamConstructor */);
2301
- deferred.return(deserialize(ctx, depth, node.f));
2302
- return NIL;
2303
- }
2304
- throw new SerovalMissingInstanceError("Stream");
2305
- }
2306
- function deserializeIteratorFactory(ctx, depth, node) {
2307
- deserialize(ctx, depth, node.f);
2308
- return NIL;
2309
- }
2310
- function deserializeAsyncIteratorFactory(ctx, depth, node) {
2311
- deserialize(ctx, depth, node.a[1]);
2312
- return NIL;
2313
- }
2314
- function deserializeSequence(ctx, depth, node) {
2315
- const result = assignIndexedValue(
2316
- ctx,
2317
- node.i,
2318
- createSequence([], node.s, node.l)
2319
- );
2320
- for (let i = 0, len = node.a.length; i < len; i++) {
2321
- result.v[i] = deserialize(ctx, depth, node.a[i]);
2322
- }
2323
- return result;
2324
- }
2325
- function deserialize(ctx, depth, node) {
2326
- if (depth > ctx.base.depthLimit) {
2327
- throw new SerovalDepthLimitError(ctx.base.depthLimit);
2328
- }
2329
- depth += 1;
2330
- switch (node.t) {
2331
- case 2 /* Constant */:
2332
- return deserializeKnownValue(node, CONSTANT_VAL, node.s);
2333
- case 0 /* Number */:
2334
- return Number(node.s);
2335
- case 1 /* String */:
2336
- return deserializeString(String(node.s));
2337
- case 3 /* BigInt */:
2338
- if (String(node.s).length > MAX_BIGINT_LENGTH) {
2339
- throw new SerovalMalformedNodeError(node);
2340
- }
2341
- return BigInt(node.s);
2342
- case 4 /* IndexedValue */:
2343
- return ctx.base.refs.get(node.i);
2344
- case 18 /* Reference */:
2345
- return deserializeReference(ctx, node);
2346
- case 9 /* Array */:
2347
- return deserializeArray(ctx, depth, node);
2348
- case 10 /* Object */:
2349
- case 11 /* NullConstructor */:
2350
- return deserializeObject(ctx, depth, node);
2351
- case 5 /* Date */:
2352
- return deserializeDate(ctx, node);
2353
- case 6 /* RegExp */:
2354
- return deserializeRegExp(ctx, node);
2355
- case 7 /* Set */:
2356
- return deserializeSet(ctx, depth, node);
2357
- case 8 /* Map */:
2358
- return deserializeMap(ctx, depth, node);
2359
- case 19 /* ArrayBuffer */:
2360
- return deserializeArrayBuffer(ctx, node);
2361
- case 16 /* BigIntTypedArray */:
2362
- case 15 /* TypedArray */:
2363
- return deserializeTypedArray(ctx, depth, node);
2364
- case 20 /* DataView */:
2365
- return deserializeDataView(ctx, depth, node);
2366
- case 14 /* AggregateError */:
2367
- return deserializeAggregateError(ctx, depth, node);
2368
- case 13 /* Error */:
2369
- return deserializeError(ctx, depth, node);
2370
- case 12 /* Promise */:
2371
- return deserializePromise(ctx, depth, node);
2372
- case 17 /* WKSymbol */:
2373
- return deserializeKnownValue(node, SYMBOL_REF, node.s);
2374
- case 21 /* Boxed */:
2375
- return deserializeBoxed(ctx, depth, node);
2376
- case 25 /* Plugin */:
2377
- return deserializePlugin(ctx, depth, node);
2378
- case 22 /* PromiseConstructor */:
2379
- return deserializePromiseConstructor(ctx, node);
2380
- case 23 /* PromiseSuccess */:
2381
- return deserializePromiseResolve(ctx, depth, node);
2382
- case 24 /* PromiseFailure */:
2383
- return deserializePromiseReject(ctx, depth, node);
2384
- case 28 /* IteratorFactoryInstance */:
2385
- return deserializeIteratorFactoryInstance(ctx, depth, node);
2386
- case 30 /* AsyncIteratorFactoryInstance */:
2387
- return deserializeAsyncIteratorFactoryInstance(ctx, depth, node);
2388
- case 31 /* StreamConstructor */:
2389
- return deserializeStreamConstructor(ctx, depth, node);
2390
- case 32 /* StreamNext */:
2391
- return deserializeStreamNext(ctx, depth, node);
2392
- case 33 /* StreamThrow */:
2393
- return deserializeStreamThrow(ctx, depth, node);
2394
- case 34 /* StreamReturn */:
2395
- return deserializeStreamReturn(ctx, depth, node);
2396
- case 27 /* IteratorFactory */:
2397
- return deserializeIteratorFactory(ctx, depth, node);
2398
- case 29 /* AsyncIteratorFactory */:
2399
- return deserializeAsyncIteratorFactory(ctx, depth, node);
2400
- case 35 /* Sequence */:
2401
- return deserializeSequence(ctx, depth, node);
2402
- default:
2403
- throw new SerovalUnsupportedNodeError(node);
2404
- }
2405
- }
2406
- function deserializeTop(ctx, node) {
2407
- try {
2408
- return deserialize(ctx, 0, node);
2409
- } catch (error) {
2410
- throw new SerovalDeserializationError(error);
2411
- }
2412
- }
2413
-
2414
- // src/core/function-string.ts
2415
- var RETURN = () => T;
2416
- var SERIALIZED_RETURN = /* @__PURE__ */ RETURN.toString();
2417
- var IS_MODERN = /* @__PURE__ */ /=>/.test(SERIALIZED_RETURN);
2418
- function createFunction(parameters, body) {
2419
- if (IS_MODERN) {
2420
- const joined = parameters.length === 1 ? parameters[0] : "(" + parameters.join(",") + ")";
2421
- return joined + "=>" + (body.startsWith("{") ? "(" + body + ")" : body);
2422
- }
2423
- return "function(" + parameters.join(",") + "){return " + body + "}";
2424
- }
2425
- function createEffectfulFunction(parameters, body) {
2426
- if (IS_MODERN) {
2427
- const joined = parameters.length === 1 ? parameters[0] : "(" + parameters.join(",") + ")";
2428
- return joined + "=>{" + body + "}";
2429
- }
2430
- return "function(" + parameters.join(",") + "){" + body + "}";
2431
- }
2432
-
2433
- // src/core/utils/get-identifier.ts
2434
- var REF_START_CHARS = "hjkmoquxzABCDEFGHIJKLNPQRTUVWXYZ$_";
2435
- var REF_START_CHARS_LEN = REF_START_CHARS.length;
2436
- var REF_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$_";
2437
- var REF_CHARS_LEN = REF_CHARS.length;
2438
- function getIdentifier(index) {
2439
- let mod = index % REF_START_CHARS_LEN;
2440
- let ref = REF_START_CHARS[mod];
2441
- index = (index - mod) / REF_START_CHARS_LEN;
2442
- while (index > 0) {
2443
- mod = index % REF_CHARS_LEN;
2444
- ref += REF_CHARS[mod];
2445
- index = (index - mod) / REF_CHARS_LEN;
2446
- }
2447
- return ref;
2448
- }
2449
-
2450
- // src/core/utils/is-valid-identifier.ts
2451
- var IDENTIFIER_CHECK = /^[$A-Z_][0-9A-Z_$]*$/i;
2452
- function isValidIdentifier(name) {
2453
- const char = name[0];
2454
- return (char === "$" || char === "_" || char >= "A" && char <= "Z" || char >= "a" && char <= "z") && IDENTIFIER_CHECK.test(name);
2455
- }
2456
-
2457
- // src/core/context/serializer.ts
2458
- function getAssignmentExpression(assignment) {
2459
- switch (assignment.t) {
2460
- case 0 /* Index */:
2461
- return assignment.s + "=" + assignment.v;
2462
- case 2 /* Set */:
2463
- return assignment.s + ".set(" + assignment.k + "," + assignment.v + ")";
2464
- case 1 /* Add */:
2465
- return assignment.s + ".add(" + assignment.v + ")";
2466
- case 3 /* Delete */:
2467
- return assignment.s + ".delete(" + assignment.k + ")";
2468
- case 4 /* Define */:
2469
- return "Object.defineProperty(" + assignment.s + ',"__proto__",{value:' + assignment.k + ",configurable:!0,enumerable:!0,writable:!0})";
2470
- }
2471
- }
2472
- function mergeAssignments(assignments) {
2473
- const newAssignments = [];
2474
- let current = assignments[0];
2475
- for (let i = 1, len = assignments.length, item, prev = current; i < len; i++) {
2476
- item = assignments[i];
2477
- if (item.t === 0 /* Index */ && item.v === prev.v) {
2478
- current = {
2479
- t: 0 /* Index */,
2480
- s: item.s,
2481
- k: NIL,
2482
- v: getAssignmentExpression(current)
2483
- };
2484
- } else if (item.t === 2 /* Set */ && item.s === prev.s) {
2485
- current = {
2486
- t: 2 /* Set */,
2487
- s: getAssignmentExpression(current),
2488
- k: item.k,
2489
- v: item.v
2490
- };
2491
- } else if (item.t === 1 /* Add */ && item.s === prev.s) {
2492
- current = {
2493
- t: 1 /* Add */,
2494
- s: getAssignmentExpression(current),
2495
- k: NIL,
2496
- v: item.v
2497
- };
2498
- } else if (item.t === 3 /* Delete */ && item.s === prev.s) {
2499
- current = {
2500
- t: 3 /* Delete */,
2501
- s: getAssignmentExpression(current),
2502
- k: item.k,
2503
- v: NIL
2504
- };
2505
- } else {
2506
- newAssignments.push(current);
2507
- current = item;
2508
- }
2509
- prev = item;
2510
- }
2511
- newAssignments.push(current);
2512
- return newAssignments;
2513
- }
2514
- function resolveAssignments(assignments) {
2515
- if (assignments.length) {
2516
- let result = "";
2517
- const merged = mergeAssignments(assignments);
2518
- for (let i = 0, len = merged.length; i < len; i++) {
2519
- result += getAssignmentExpression(merged[i]) + ",";
2520
- }
2521
- return result;
2522
- }
2523
- return NIL;
2524
- }
2525
- var NULL_CONSTRUCTOR = "Object.create(null)";
2526
- var SET_CONSTRUCTOR = "new Set";
2527
- var MAP_CONSTRUCTOR = "new Map";
2528
- var PROMISE_RESOLVE = "Promise.resolve";
2529
- var PROMISE_REJECT = "Promise.reject";
2530
- var OBJECT_FLAG_CONSTRUCTOR = {
2531
- [3 /* Frozen */]: "Object.freeze",
2532
- [2 /* Sealed */]: "Object.seal",
2533
- [1 /* NonExtensible */]: "Object.preventExtensions",
2534
- [0 /* None */]: NIL
2535
- };
2536
- function createBaseSerializerContext(mode, options) {
2537
- return {
2538
- mode,
2539
- plugins: options.plugins,
2540
- features: options.features,
2541
- marked: new Set(options.markedRefs),
2542
- stack: [],
2543
- flags: [],
2544
- assignments: []
2545
- };
2546
- }
2547
- function createVanillaSerializerState() {
2548
- return {
2549
- valid: /* @__PURE__ */ new Map(),
2550
- vars: []
2551
- };
2552
- }
2553
- function createVanillaSerializerContext(options) {
2554
- return {
2555
- mode: 1 /* Vanilla */,
2556
- base: createBaseSerializerContext(1 /* Vanilla */, options),
2557
- state: createVanillaSerializerState(),
2558
- child: NIL
2559
- };
2560
- }
2561
- function createCrossSerializerContext(options) {
2562
- return {
2563
- mode: 2 /* Cross */,
2564
- base: createBaseSerializerContext(2 /* Cross */, options),
2565
- state: options,
2566
- child: NIL
2567
- };
2568
- }
2569
- var SerializePluginContext = class {
2570
- constructor(_p) {
2571
- this._p = _p;
2572
- }
2573
- serialize(node) {
2574
- return serialize(this._p, node);
2575
- }
2576
- };
2577
- function getVanillaRefParam(state, index) {
2578
- let actualIndex = state.valid.get(index);
2579
- if (actualIndex == null) {
2580
- actualIndex = state.valid.size;
2581
- state.valid.set(index, actualIndex);
2582
- }
2583
- let identifier = state.vars[actualIndex];
2584
- if (identifier == null) {
2585
- identifier = getIdentifier(actualIndex);
2586
- state.vars[actualIndex] = identifier;
2587
- }
2588
- return identifier;
2589
- }
2590
- function getCrossRefParam(id) {
2591
- return GLOBAL_CONTEXT_REFERENCES + "[" + id + "]";
2592
- }
2593
- function getRefParam(ctx, id) {
2594
- return ctx.mode === 1 /* Vanilla */ ? getVanillaRefParam(ctx.state, id) : getCrossRefParam(id);
2595
- }
2596
- function markSerializerRef(ctx, id) {
2597
- ctx.marked.add(id);
2598
- }
2599
- function isSerializerRefMarked(ctx, id) {
2600
- return ctx.marked.has(id);
2601
- }
2602
- function pushObjectFlag(ctx, flag, id) {
2603
- if (flag !== 0 /* None */) {
2604
- markSerializerRef(ctx.base, id);
2605
- ctx.base.flags.push({
2606
- type: flag,
2607
- value: getRefParam(ctx, id)
2608
- });
2609
- }
2610
- }
2611
- function resolveFlags(ctx) {
2612
- let result = "";
2613
- for (let i = 0, current = ctx.flags, len = current.length; i < len; i++) {
2614
- const flag = current[i];
2615
- result += OBJECT_FLAG_CONSTRUCTOR[flag.type] + "(" + flag.value + "),";
2616
- }
2617
- return result;
2618
- }
2619
- function resolvePatches(ctx) {
2620
- const assignments = resolveAssignments(ctx.assignments);
2621
- const flags = resolveFlags(ctx);
2622
- if (assignments) {
2623
- if (flags) {
2624
- return assignments + flags;
2625
- }
2626
- return assignments;
2627
- }
2628
- return flags;
2629
- }
2630
- function createAssignment(ctx, source, value) {
2631
- ctx.assignments.push({
2632
- t: 0 /* Index */,
2633
- s: source,
2634
- k: NIL,
2635
- v: value
2636
- });
2637
- }
2638
- function createAddAssignment(ctx, ref, value) {
2639
- ctx.base.assignments.push({
2640
- t: 1 /* Add */,
2641
- s: getRefParam(ctx, ref),
2642
- k: NIL,
2643
- v: value
2644
- });
2645
- }
2646
- function createSetAssignment(ctx, ref, key, value) {
2647
- ctx.base.assignments.push({
2648
- t: 2 /* Set */,
2649
- s: getRefParam(ctx, ref),
2650
- k: key,
2651
- v: value
2652
- });
2653
- }
2654
- function createDeleteAssignment(ctx, ref, key) {
2655
- ctx.base.assignments.push({
2656
- t: 3 /* Delete */,
2657
- s: getRefParam(ctx, ref),
2658
- k: key,
2659
- v: NIL
2660
- });
2661
- }
2662
- function createArrayAssign(ctx, ref, index, value) {
2663
- createAssignment(ctx.base, getRefParam(ctx, ref) + "[" + index + "]", value);
2664
- }
2665
- function createObjectAssign(ctx, ref, key, value) {
2666
- if (!isValidKey(key)) {
2667
- ctx.base.assignments.push({
2668
- t: 4 /* Define */,
2669
- s: getRefParam(ctx, ref),
2670
- k: value,
2671
- v: NIL
2672
- });
2673
- return;
2674
- }
2675
- createAssignment(ctx.base, getRefParam(ctx, ref) + "." + key, value);
2676
- }
2677
- function createSequenceAssign(ctx, ref, index, value) {
2678
- createAssignment(
2679
- ctx.base,
2680
- getRefParam(ctx, ref) + ".v[" + index + "]",
2681
- value
2682
- );
2683
- }
2684
- function isIndexedValueInStack(ctx, node) {
2685
- return node.t === 4 /* IndexedValue */ && ctx.stack.includes(node.i);
2686
- }
2687
- function assignIndexedValue2(ctx, index, value) {
2688
- if (ctx.mode === 1 /* Vanilla */ && !isSerializerRefMarked(ctx.base, index)) {
2689
- return value;
2690
- }
2691
- return getRefParam(ctx, index) + "=" + value;
2692
- }
2693
- function serializeReference(node) {
2694
- return REFERENCES_KEY + '.get("' + node.s + '")';
2695
- }
2696
- function serializeArrayItem(ctx, id, item, index) {
2697
- if (item) {
2698
- if (isIndexedValueInStack(ctx.base, item)) {
2699
- markSerializerRef(ctx.base, id);
2700
- createArrayAssign(
2701
- ctx,
2702
- id,
2703
- index,
2704
- getRefParam(ctx, item.i)
2705
- );
2706
- return "";
2707
- }
2708
- return serialize(ctx, item);
2709
- }
2710
- return "";
2711
- }
2712
- function serializeArray(ctx, node) {
2713
- const id = node.i;
2714
- const list = node.a;
2715
- const len = list.length;
2716
- if (len > 0) {
2717
- ctx.base.stack.push(id);
2718
- let values = serializeArrayItem(ctx, id, list[0], 0);
2719
- let isHoley = values === "";
2720
- for (let i = 1, item; i < len; i++) {
2721
- item = serializeArrayItem(ctx, id, list[i], i);
2722
- values += "," + item;
2723
- isHoley = item === "";
2724
- }
2725
- ctx.base.stack.pop();
2726
- pushObjectFlag(ctx, node.o, node.i);
2727
- return "[" + values + (isHoley ? ",]" : "]");
2728
- }
2729
- return "[]";
2730
- }
2731
- function serializeProperty(ctx, source, key, val) {
2732
- if (typeof key === "string") {
2733
- const check = Number(key);
2734
- const isIdentifier = (
2735
- // Test if key is a valid positive number or JS identifier
2736
- // so that we don't have to serialize the key and wrap with brackets
2737
- check >= 0 && // It's also important to consider that if the key is
2738
- // indeed numeric, we need to make sure that when
2739
- // converted back into a string, it's still the same
2740
- // to the original key. This allows us to differentiate
2741
- // keys that has numeric formats but in a different
2742
- // format, which can cause unintentional key declaration
2743
- // Example: { 0x1: 1 } vs { '0x1': 1 }
2744
- check.toString() === key || isValidIdentifier(key)
2745
- );
2746
- if (isIndexedValueInStack(ctx.base, val)) {
2747
- const refParam = getRefParam(ctx, val.i);
2748
- markSerializerRef(ctx.base, source.i);
2749
- if (isIdentifier && check !== check) {
2750
- createObjectAssign(ctx, source.i, key, refParam);
2751
- } else {
2752
- createArrayAssign(
2753
- ctx,
2754
- source.i,
2755
- isIdentifier ? key : '"' + key + '"',
2756
- refParam
2757
- );
2758
- }
2759
- return "";
2760
- }
2761
- if (isValidKey(key)) {
2762
- return (isIdentifier ? key : '"' + key + '"') + ":" + serialize(ctx, val);
2763
- }
2764
- return '["' + key + '"]:' + serialize(ctx, val);
2765
- }
2766
- return "[" + serialize(ctx, key) + "]:" + serialize(ctx, val);
2767
- }
2768
- function serializeProperties(ctx, source, record) {
2769
- const keys = record.k;
2770
- const len = keys.length;
2771
- if (len > 0) {
2772
- const values = record.v;
2773
- ctx.base.stack.push(source.i);
2774
- let result = serializeProperty(ctx, source, keys[0], values[0]);
2775
- for (let i = 1, item = result; i < len; i++) {
2776
- item = serializeProperty(ctx, source, keys[i], values[i]);
2777
- result += (item && result && ",") + item;
2778
- }
2779
- ctx.base.stack.pop();
2780
- return "{" + result + "}";
2781
- }
2782
- return "{}";
2783
- }
2784
- function serializeObject(ctx, node) {
2785
- pushObjectFlag(ctx, node.o, node.i);
2786
- return serializeProperties(ctx, node, node.p);
2787
- }
2788
- function serializeWithObjectAssign(ctx, source, value, serialized) {
2789
- const fields = serializeProperties(ctx, source, value);
2790
- if (fields !== "{}") {
2791
- return "Object.assign(" + serialized + "," + fields + ")";
2792
- }
2793
- return serialized;
2794
- }
2795
- function serializeStringKeyAssignment(ctx, source, mainAssignments, key, value) {
2796
- const base = ctx.base;
2797
- const serialized = serialize(ctx, value);
2798
- const check = Number(key);
2799
- const isIdentifier = (
2800
- // Test if key is a valid positive number or JS identifier
2801
- // so that we don't have to serialize the key and wrap with brackets
2802
- check >= 0 && // It's also important to consider that if the key is
2803
- // indeed numeric, we need to make sure that when
2804
- // converted back into a string, it's still the same
2805
- // to the original key. This allows us to differentiate
2806
- // keys that has numeric formats but in a different
2807
- // format, which can cause unintentional key declaration
2808
- // Example: { 0x1: 1 } vs { '0x1': 1 }
2809
- check.toString() === key || isValidIdentifier(key)
2810
- );
2811
- if (isIndexedValueInStack(base, value)) {
2812
- if (isIdentifier && check !== check) {
2813
- createObjectAssign(ctx, source.i, key, serialized);
2814
- } else {
2815
- createArrayAssign(
2816
- ctx,
2817
- source.i,
2818
- isIdentifier ? key : '"' + key + '"',
2819
- serialized
2820
- );
2821
- }
2822
- } else {
2823
- const parentAssignment = base.assignments;
2824
- base.assignments = mainAssignments;
2825
- if (isIdentifier && check !== check) {
2826
- createObjectAssign(ctx, source.i, key, serialized);
2827
- } else {
2828
- createArrayAssign(
2829
- ctx,
2830
- source.i,
2831
- isIdentifier ? key : '"' + key + '"',
2832
- serialized
2833
- );
2834
- }
2835
- base.assignments = parentAssignment;
2836
- }
2837
- }
2838
- function serializeAssignment(ctx, source, mainAssignments, key, value) {
2839
- if (typeof key === "string") {
2840
- serializeStringKeyAssignment(ctx, source, mainAssignments, key, value);
2841
- } else {
2842
- const base = ctx.base;
2843
- const parent = base.stack;
2844
- base.stack = [];
2845
- const serialized = serialize(ctx, value);
2846
- base.stack = parent;
2847
- const parentAssignment = base.assignments;
2848
- base.assignments = mainAssignments;
2849
- createArrayAssign(ctx, source.i, serialize(ctx, key), serialized);
2850
- base.assignments = parentAssignment;
2851
- }
2852
- }
2853
- function serializeAssignments(ctx, source, node) {
2854
- const keys = node.k;
2855
- const len = keys.length;
2856
- if (len > 0) {
2857
- const mainAssignments = [];
2858
- const values = node.v;
2859
- ctx.base.stack.push(source.i);
2860
- for (let i = 0; i < len; i++) {
2861
- serializeAssignment(ctx, source, mainAssignments, keys[i], values[i]);
2862
- }
2863
- ctx.base.stack.pop();
2864
- return resolveAssignments(mainAssignments);
2865
- }
2866
- return NIL;
2867
- }
2868
- function serializeDictionary(ctx, node, init) {
2869
- if (node.p) {
2870
- const base = ctx.base;
2871
- if (base.features & 8 /* ObjectAssign */) {
2872
- init = serializeWithObjectAssign(ctx, node, node.p, init);
2873
- } else {
2874
- markSerializerRef(base, node.i);
2875
- const assignments = serializeAssignments(ctx, node, node.p);
2876
- if (assignments) {
2877
- return "(" + assignIndexedValue2(ctx, node.i, init) + "," + assignments + getRefParam(ctx, node.i) + ")";
2878
- }
2879
- }
2880
- }
2881
- return init;
2882
- }
2883
- function serializeNullConstructor(ctx, node) {
2884
- pushObjectFlag(ctx, node.o, node.i);
2885
- return serializeDictionary(ctx, node, NULL_CONSTRUCTOR);
2886
- }
2887
- function serializeDate(node) {
2888
- return 'new Date("' + node.s + '")';
2889
- }
2890
- function serializeRegExp(ctx, node) {
2891
- if (ctx.base.features & 32 /* RegExp */) {
2892
- return "/" + deserializeString(node.c) + "/" + node.m;
2893
- }
2894
- throw new SerovalUnsupportedNodeError(node);
2895
- }
2896
- function serializeSetItem(ctx, id, item) {
2897
- const base = ctx.base;
2898
- if (isIndexedValueInStack(base, item)) {
2899
- markSerializerRef(base, id);
2900
- createAddAssignment(
2901
- ctx,
2902
- id,
2903
- getRefParam(ctx, item.i)
2904
- );
2905
- return "";
2906
- }
2907
- return serialize(ctx, item);
2908
- }
2909
- function serializeSet(ctx, node) {
2910
- let serialized = SET_CONSTRUCTOR;
2911
- const items = node.a;
2912
- const size = items.length;
2913
- const id = node.i;
2914
- if (size > 0) {
2915
- ctx.base.stack.push(id);
2916
- let result = serializeSetItem(ctx, id, items[0]);
2917
- for (let i = 1, item = result; i < size; i++) {
2918
- item = serializeSetItem(ctx, id, items[i]);
2919
- result += (item && result && ",") + item;
2920
- }
2921
- ctx.base.stack.pop();
2922
- if (result) {
2923
- serialized += "([" + result + "])";
2924
- }
2925
- }
2926
- return serialized;
2927
- }
2928
- function serializeMapEntry(ctx, id, key, val, sentinel) {
2929
- const base = ctx.base;
2930
- if (isIndexedValueInStack(base, key)) {
2931
- const keyRef = getRefParam(ctx, key.i);
2932
- markSerializerRef(base, id);
2933
- if (isIndexedValueInStack(base, val)) {
2934
- const valueRef = getRefParam(ctx, val.i);
2935
- createSetAssignment(ctx, id, keyRef, valueRef);
2936
- return "";
2937
- }
2938
- if (val.t !== 4 /* IndexedValue */ && val.i != null && isSerializerRefMarked(base, val.i)) {
2939
- const serialized = "(" + serialize(ctx, val) + ",[" + sentinel + "," + sentinel + "])";
2940
- createSetAssignment(ctx, id, keyRef, getRefParam(ctx, val.i));
2941
- createDeleteAssignment(ctx, id, sentinel);
2942
- return serialized;
2943
- }
2944
- const parent = base.stack;
2945
- base.stack = [];
2946
- createSetAssignment(ctx, id, keyRef, serialize(ctx, val));
2947
- base.stack = parent;
2948
- return "";
2949
- }
2950
- if (isIndexedValueInStack(base, val)) {
2951
- const valueRef = getRefParam(ctx, val.i);
2952
- markSerializerRef(base, id);
2953
- if (key.t !== 4 /* IndexedValue */ && key.i != null && isSerializerRefMarked(base, key.i)) {
2954
- const serialized = "(" + serialize(ctx, key) + ",[" + sentinel + "," + sentinel + "])";
2955
- createSetAssignment(ctx, id, getRefParam(ctx, key.i), valueRef);
2956
- createDeleteAssignment(ctx, id, sentinel);
2957
- return serialized;
2958
- }
2959
- const parent = base.stack;
2960
- base.stack = [];
2961
- createSetAssignment(ctx, id, serialize(ctx, key), valueRef);
2962
- base.stack = parent;
2963
- return "";
2964
- }
2965
- return "[" + serialize(ctx, key) + "," + serialize(ctx, val) + "]";
2966
- }
2967
- function serializeMap(ctx, node) {
2968
- let serialized = MAP_CONSTRUCTOR;
2969
- const keys = node.e.k;
2970
- const size = keys.length;
2971
- const id = node.i;
2972
- const sentinel = node.f;
2973
- const sentinelId = getRefParam(ctx, sentinel.i);
2974
- const base = ctx.base;
2975
- if (size > 0) {
2976
- const vals = node.e.v;
2977
- base.stack.push(id);
2978
- let result = serializeMapEntry(ctx, id, keys[0], vals[0], sentinelId);
2979
- for (let i = 1, item = result; i < size; i++) {
2980
- item = serializeMapEntry(ctx, id, keys[i], vals[i], sentinelId);
2981
- result += (item && result && ",") + item;
2982
- }
2983
- base.stack.pop();
2984
- if (result) {
2985
- serialized += "([" + result + "])";
2986
- }
2987
- }
2988
- if (sentinel.t === 26 /* SpecialReference */) {
2989
- markSerializerRef(base, sentinel.i);
2990
- serialized = "(" + serialize(ctx, sentinel) + "," + serialized + ")";
2991
- }
2992
- return serialized;
2993
- }
2994
- function serializeArrayBuffer(ctx, node) {
2995
- return getConstructor(ctx, node.f) + '("' + node.s + '")';
2996
- }
2997
- function serializeTypedArray(ctx, node) {
2998
- return "new " + node.c + "(" + serialize(ctx, node.f) + "," + node.b + "," + node.l + ")";
2999
- }
3000
- function serializeDataView(ctx, node) {
3001
- return "new DataView(" + serialize(ctx, node.f) + "," + node.b + "," + node.l + ")";
3002
- }
3003
- function serializeAggregateError(ctx, node) {
3004
- const id = node.i;
3005
- ctx.base.stack.push(id);
3006
- const serialized = serializeDictionary(
3007
- ctx,
3008
- node,
3009
- 'new AggregateError([],"' + node.m + '")'
3010
- );
3011
- ctx.base.stack.pop();
3012
- return serialized;
3013
- }
3014
- function serializeError(ctx, node) {
3015
- return serializeDictionary(
3016
- ctx,
3017
- node,
3018
- "new " + ERROR_CONSTRUCTOR_STRING[node.s] + '("' + node.m + '")'
3019
- );
3020
- }
3021
- function serializePromise(ctx, node) {
3022
- let serialized;
3023
- const fulfilled = node.f;
3024
- const id = node.i;
3025
- const promiseConstructor = node.s ? PROMISE_RESOLVE : PROMISE_REJECT;
3026
- const base = ctx.base;
3027
- if (isIndexedValueInStack(base, fulfilled)) {
3028
- const ref = getRefParam(ctx, fulfilled.i);
3029
- serialized = promiseConstructor + (node.s ? "().then(" + createFunction([], ref) + ")" : "().catch(" + createEffectfulFunction([], "throw " + ref) + ")");
3030
- } else {
3031
- base.stack.push(id);
3032
- const result = serialize(ctx, fulfilled);
3033
- base.stack.pop();
3034
- serialized = promiseConstructor + "(" + result + ")";
3035
- }
3036
- return serialized;
3037
- }
3038
- function serializeBoxed(ctx, node) {
3039
- return "Object(" + serialize(ctx, node.f) + ")";
3040
- }
3041
- function getConstructor(ctx, node) {
3042
- const current = serialize(ctx, node);
3043
- return node.t === 4 /* IndexedValue */ ? current : "(" + current + ")";
3044
- }
3045
- function serializePromiseConstructor(ctx, node) {
3046
- if (ctx.mode === 1 /* Vanilla */) {
3047
- throw new SerovalUnsupportedNodeError(node);
3048
- }
3049
- const resolver = assignIndexedValue2(
3050
- ctx,
3051
- node.s,
3052
- getConstructor(ctx, node.f) + "()"
3053
- );
3054
- return "(" + resolver + ").p";
3055
- }
3056
- function serializePromiseResolve(ctx, node) {
3057
- if (ctx.mode === 1 /* Vanilla */) {
3058
- throw new SerovalUnsupportedNodeError(node);
3059
- }
3060
- return getConstructor(ctx, node.a[0]) + "(" + getRefParam(ctx, node.i) + "," + serialize(ctx, node.a[1]) + ")";
3061
- }
3062
- function serializePromiseReject(ctx, node) {
3063
- if (ctx.mode === 1 /* Vanilla */) {
3064
- throw new SerovalUnsupportedNodeError(node);
3065
- }
3066
- return getConstructor(ctx, node.a[0]) + "(" + getRefParam(ctx, node.i) + "," + serialize(ctx, node.a[1]) + ")";
3067
- }
3068
- function serializePlugin(ctx, node) {
3069
- const currentPlugins = ctx.base.plugins;
3070
- if (currentPlugins) {
3071
- for (let i = 0, len = currentPlugins.length; i < len; i++) {
3072
- const plugin = currentPlugins[i];
3073
- if (plugin.tag === node.c) {
3074
- if (ctx.child == null) {
3075
- ctx.child = new SerializePluginContext(ctx);
3076
- }
3077
- return plugin.serialize(node.s, ctx.child, {
3078
- id: node.i
3079
- });
3080
- }
3081
- }
3082
- }
3083
- throw new SerovalMissingPluginError(node.c);
3084
- }
3085
- function serializeIteratorFactory(ctx, node) {
3086
- let result = "";
3087
- let initialized = false;
3088
- if (node.f.t !== 4 /* IndexedValue */) {
3089
- markSerializerRef(ctx.base, node.f.i);
3090
- result = "(" + serialize(ctx, node.f) + ",";
3091
- initialized = true;
3092
- }
3093
- result += assignIndexedValue2(
3094
- ctx,
3095
- node.i,
3096
- "(" + SERIALIZED_ITERATOR_CONSTRUCTOR + ")(" + getRefParam(ctx, node.f.i) + ")"
3097
- );
3098
- if (initialized) {
3099
- result += ")";
3100
- }
3101
- return result;
3102
- }
3103
- function serializeIteratorFactoryInstance(ctx, node) {
3104
- return getConstructor(ctx, node.a[0]) + "(" + serialize(ctx, node.a[1]) + ")";
3105
- }
3106
- function serializeAsyncIteratorFactory(ctx, node) {
3107
- const promise = node.a[0];
3108
- const symbol = node.a[1];
3109
- const base = ctx.base;
3110
- let result = "";
3111
- if (promise.t !== 4 /* IndexedValue */) {
3112
- markSerializerRef(base, promise.i);
3113
- result += "(" + serialize(ctx, promise);
3114
- }
3115
- if (symbol.t !== 4 /* IndexedValue */) {
3116
- markSerializerRef(base, symbol.i);
3117
- result += (result ? "," : "(") + serialize(ctx, symbol);
3118
- }
3119
- if (result) {
3120
- result += ",";
3121
- }
3122
- const iterator = assignIndexedValue2(
3123
- ctx,
3124
- node.i,
3125
- "(" + SERIALIZED_ASYNC_ITERATOR_CONSTRUCTOR + ")(" + getRefParam(ctx, symbol.i) + "," + getRefParam(ctx, promise.i) + ")"
3126
- );
3127
- if (result) {
3128
- return result + iterator + ")";
3129
- }
3130
- return iterator;
3131
- }
3132
- function serializeAsyncIteratorFactoryInstance(ctx, node) {
3133
- return getConstructor(ctx, node.a[0]) + "(" + serialize(ctx, node.a[1]) + ")";
3134
- }
3135
- function serializeStreamConstructor(ctx, node) {
3136
- const result = assignIndexedValue2(
3137
- ctx,
3138
- node.i,
3139
- getConstructor(ctx, node.f) + "()"
3140
- );
3141
- const len = node.a.length;
3142
- if (len) {
3143
- let values = serialize(ctx, node.a[0]);
3144
- for (let i = 1; i < len; i++) {
3145
- values += "," + serialize(ctx, node.a[i]);
3146
- }
3147
- return "(" + result + "," + values + "," + getRefParam(ctx, node.i) + ")";
3148
- }
3149
- return result;
3150
- }
3151
- function serializeStreamNext(ctx, node) {
3152
- return getRefParam(ctx, node.i) + ".next(" + serialize(ctx, node.f) + ")";
3153
- }
3154
- function serializeStreamThrow(ctx, node) {
3155
- return getRefParam(ctx, node.i) + ".throw(" + serialize(ctx, node.f) + ")";
3156
- }
3157
- function serializeStreamReturn(ctx, node) {
3158
- return getRefParam(ctx, node.i) + ".return(" + serialize(ctx, node.f) + ")";
3159
- }
3160
- function serializeSequenceItem(ctx, id, index, item) {
3161
- const base = ctx.base;
3162
- if (isIndexedValueInStack(base, item)) {
3163
- markSerializerRef(base, id);
3164
- createSequenceAssign(
3165
- ctx,
3166
- id,
3167
- index,
3168
- getRefParam(ctx, item.i)
3169
- );
3170
- return "";
3171
- }
3172
- return serialize(ctx, item);
3173
- }
3174
- function serializeSequence(ctx, node) {
3175
- const items = node.a;
3176
- const size = items.length;
3177
- const id = node.i;
3178
- if (size > 0) {
3179
- ctx.base.stack.push(id);
3180
- let result = serializeSequenceItem(ctx, id, 0, items[0]);
3181
- for (let i = 1, item = result; i < size; i++) {
3182
- item = serializeSequenceItem(ctx, id, i, items[i]);
3183
- result += (item && result && ",") + item;
3184
- }
3185
- ctx.base.stack.pop();
3186
- if (result) {
3187
- return "{__SEROVAL_SEQUENCE__:!0,v:[" + result + "],t:" + node.s + ",d:" + node.l + "}";
3188
- }
3189
- }
3190
- return "{__SEROVAL_SEQUENCE__:!0,v:[],t:-1,d:0}";
3191
- }
3192
- function serializeAssignable(ctx, node) {
3193
- switch (node.t) {
3194
- case 17 /* WKSymbol */:
3195
- return SYMBOL_STRING[node.s];
3196
- case 18 /* Reference */:
3197
- return serializeReference(node);
3198
- case 9 /* Array */:
3199
- return serializeArray(ctx, node);
3200
- case 10 /* Object */:
3201
- return serializeObject(ctx, node);
3202
- case 11 /* NullConstructor */:
3203
- return serializeNullConstructor(ctx, node);
3204
- case 5 /* Date */:
3205
- return serializeDate(node);
3206
- case 6 /* RegExp */:
3207
- return serializeRegExp(ctx, node);
3208
- case 7 /* Set */:
3209
- return serializeSet(ctx, node);
3210
- case 8 /* Map */:
3211
- return serializeMap(ctx, node);
3212
- case 19 /* ArrayBuffer */:
3213
- return serializeArrayBuffer(ctx, node);
3214
- case 16 /* BigIntTypedArray */:
3215
- case 15 /* TypedArray */:
3216
- return serializeTypedArray(ctx, node);
3217
- case 20 /* DataView */:
3218
- return serializeDataView(ctx, node);
3219
- case 14 /* AggregateError */:
3220
- return serializeAggregateError(ctx, node);
3221
- case 13 /* Error */:
3222
- return serializeError(ctx, node);
3223
- case 12 /* Promise */:
3224
- return serializePromise(ctx, node);
3225
- case 21 /* Boxed */:
3226
- return serializeBoxed(ctx, node);
3227
- case 22 /* PromiseConstructor */:
3228
- return serializePromiseConstructor(ctx, node);
3229
- case 25 /* Plugin */:
3230
- return serializePlugin(ctx, node);
3231
- case 26 /* SpecialReference */:
3232
- return SPECIAL_REF_STRING[node.s];
3233
- case 35 /* Sequence */:
3234
- return serializeSequence(ctx, node);
3235
- default:
3236
- throw new SerovalUnsupportedNodeError(node);
3237
- }
3238
- }
3239
- function serialize(ctx, node) {
3240
- switch (node.t) {
3241
- case 2 /* Constant */:
3242
- return CONSTANT_STRING[node.s];
3243
- case 0 /* Number */:
3244
- return "" + node.s;
3245
- case 1 /* String */:
3246
- return '"' + node.s + '"';
3247
- case 3 /* BigInt */:
3248
- return node.s + "n";
3249
- case 4 /* IndexedValue */:
3250
- return getRefParam(ctx, node.i);
3251
- case 23 /* PromiseSuccess */:
3252
- return serializePromiseResolve(ctx, node);
3253
- case 24 /* PromiseFailure */:
3254
- return serializePromiseReject(ctx, node);
3255
- case 27 /* IteratorFactory */:
3256
- return serializeIteratorFactory(ctx, node);
3257
- case 28 /* IteratorFactoryInstance */:
3258
- return serializeIteratorFactoryInstance(ctx, node);
3259
- case 29 /* AsyncIteratorFactory */:
3260
- return serializeAsyncIteratorFactory(ctx, node);
3261
- case 30 /* AsyncIteratorFactoryInstance */:
3262
- return serializeAsyncIteratorFactoryInstance(ctx, node);
3263
- case 31 /* StreamConstructor */:
3264
- return serializeStreamConstructor(ctx, node);
3265
- case 32 /* StreamNext */:
3266
- return serializeStreamNext(ctx, node);
3267
- case 33 /* StreamThrow */:
3268
- return serializeStreamThrow(ctx, node);
3269
- case 34 /* StreamReturn */:
3270
- return serializeStreamReturn(ctx, node);
3271
- default:
3272
- return assignIndexedValue2(ctx, node.i, serializeAssignable(ctx, node));
3273
- }
3274
- }
3275
- function serializeTopVanilla(ctx, tree) {
3276
- const result = serialize(ctx, tree);
3277
- if (tree.i != null && ctx.state.vars.length) {
3278
- const patches = resolvePatches(ctx.base);
3279
- let body = result;
3280
- if (patches) {
3281
- const index = getRefParam(ctx, tree.i);
3282
- body = result + "," + patches + index;
3283
- if (!result.startsWith(index + "=")) {
3284
- body = index + "=" + body;
3285
- }
3286
- body = "(" + body + ")";
3287
- }
3288
- return "(" + createFunction(ctx.state.vars, body) + ")()";
3289
- }
3290
- if (tree.t === 10 /* Object */) {
3291
- return "(" + result + ")";
3292
- }
3293
- return result;
3294
- }
3295
- function serializeTopCross(ctx, tree) {
3296
- const result = serialize(ctx, tree);
3297
- const id = tree.i;
3298
- if (id == null) {
3299
- return result;
3300
- }
3301
- const patches = resolvePatches(ctx.base);
3302
- const ref = getRefParam(ctx, id);
3303
- const scopeId = ctx.state.scopeId;
3304
- const params = scopeId == null ? "" : GLOBAL_CONTEXT_REFERENCES;
3305
- const body = patches ? "(" + result + "," + patches + ref + ")" : result;
3306
- if (params === "") {
3307
- if (tree.t === 10 /* Object */ && !patches) {
3308
- return "(" + body + ")";
3309
- }
3310
- return body;
3311
- }
3312
- const args = scopeId == null ? "()" : "(" + GLOBAL_CONTEXT_REFERENCES + '["' + serializeString(scopeId) + '"])';
3313
- return "(" + createFunction([params], body) + ")" + args;
3314
- }
3315
-
3316
- // src/core/context/sync-parser.ts
3317
- function createSyncParserContext(mode, options) {
3318
- return {
3319
- type: 1 /* Sync */,
3320
- base: createBaseParserContext(mode, options),
3321
- child: NIL
3322
- };
3323
- }
3324
- var SyncParsePluginContext = class {
3325
- constructor(_p, depth) {
3326
- this._p = _p;
3327
- this.depth = depth;
3328
- }
3329
- parse(current) {
3330
- return parseSOS(this._p, this.depth, current);
3331
- }
3332
- };
3333
- var StreamParsePluginContext = class {
3334
- constructor(_p, depth) {
3335
- this._p = _p;
3336
- this.depth = depth;
3337
- }
3338
- parse(current) {
3339
- return parseSOS(this._p, this.depth, current);
3340
- }
3341
- parseWithError(current) {
3342
- return parseWithError(this._p, this.depth, current);
3343
- }
3344
- isAlive() {
3345
- return this._p.state.alive;
3346
- }
3347
- pushPendingState() {
3348
- pushPendingState(this._p);
3349
- }
3350
- popPendingState() {
3351
- popPendingState(this._p);
3352
- }
3353
- onParse(node) {
3354
- onParse(this._p, node);
3355
- }
3356
- onError(error) {
3357
- onError(this._p, error);
3358
- }
3359
- addCleanup(callback) {
3360
- this._p.state.cleanups.push(callback);
3361
- }
3362
- };
3363
- function createStreamParserState(options) {
3364
- return {
3365
- alive: true,
3366
- pending: 0,
3367
- initial: true,
3368
- buffer: [],
3369
- onParse: options.onParse,
3370
- onError: options.onError,
3371
- onDone: options.onDone,
3372
- cleanups: []
3373
- };
3374
- }
3375
- function createStreamParserContext(options) {
3376
- return {
3377
- type: 2 /* Stream */,
3378
- base: createBaseParserContext(2 /* Cross */, options),
3379
- state: createStreamParserState(options)
3380
- };
3381
- }
3382
- function parseItems2(ctx, depth, current) {
3383
- const nodes = [];
3384
- for (let i = 0, len = current.length; i < len; i++) {
3385
- if (i in current) {
3386
- nodes[i] = parseSOS(ctx, depth, current[i]);
3387
- } else {
3388
- nodes[i] = 0;
3389
- }
3390
- }
3391
- return nodes;
3392
- }
3393
- function parseArray2(ctx, depth, id, current) {
3394
- return createArrayNode(id, current, parseItems2(ctx, depth, current));
3395
- }
3396
- function parseProperties2(ctx, depth, properties) {
3397
- const entries = Object.entries(properties);
3398
- const keyNodes = [];
3399
- const valueNodes = [];
3400
- for (let i = 0, len = entries.length; i < len; i++) {
3401
- keyNodes.push(serializeString(entries[i][0]));
3402
- valueNodes.push(parseSOS(ctx, depth, entries[i][1]));
3403
- }
3404
- if (SYM_ITERATOR in properties) {
3405
- keyNodes.push(parseWellKnownSymbol(ctx.base, SYM_ITERATOR));
3406
- valueNodes.push(
3407
- createIteratorFactoryInstanceNode(
3408
- parseIteratorFactory(ctx.base),
3409
- parseSOS(
3410
- ctx,
3411
- depth,
3412
- createSequenceFromIterable(
3413
- properties
3414
- )
3415
- )
3416
- )
3417
- );
3418
- }
3419
- if (SYM_ASYNC_ITERATOR in properties) {
3420
- keyNodes.push(parseWellKnownSymbol(ctx.base, SYM_ASYNC_ITERATOR));
3421
- valueNodes.push(
3422
- createAsyncIteratorFactoryInstanceNode(
3423
- parseAsyncIteratorFactory(ctx.base),
3424
- parseSOS(
3425
- ctx,
3426
- depth,
3427
- ctx.type === 1 /* Sync */ ? createStream() : createStreamFromAsyncIterable(
3428
- properties
3429
- )
3430
- )
3431
- )
3432
- );
3433
- }
3434
- if (SYM_TO_STRING_TAG in properties) {
3435
- keyNodes.push(parseWellKnownSymbol(ctx.base, SYM_TO_STRING_TAG));
3436
- valueNodes.push(createStringNode(properties[SYM_TO_STRING_TAG]));
3437
- }
3438
- if (SYM_IS_CONCAT_SPREADABLE in properties) {
3439
- keyNodes.push(parseWellKnownSymbol(ctx.base, SYM_IS_CONCAT_SPREADABLE));
3440
- valueNodes.push(
3441
- properties[SYM_IS_CONCAT_SPREADABLE] ? TRUE_NODE : FALSE_NODE
3442
- );
3443
- }
3444
- return {
3445
- k: keyNodes,
3446
- v: valueNodes
3447
- };
3448
- }
3449
- function parsePlainObject2(ctx, depth, id, current, empty) {
3450
- return createObjectNode(
3451
- id,
3452
- current,
3453
- empty,
3454
- parseProperties2(ctx, depth, current)
3455
- );
3456
- }
3457
- function parseBoxed2(ctx, depth, id, current) {
3458
- return createBoxedNode(id, parseSOS(ctx, depth, current.valueOf()));
3459
- }
3460
- function parseTypedArray2(ctx, depth, id, current) {
3461
- return createTypedArrayNode(
3462
- id,
3463
- current,
3464
- parseSOS(ctx, depth, current.buffer)
3465
- );
3466
- }
3467
- function parseBigIntTypedArray2(ctx, depth, id, current) {
3468
- return createBigIntTypedArrayNode(
3469
- id,
3470
- current,
3471
- parseSOS(ctx, depth, current.buffer)
3472
- );
3473
- }
3474
- function parseDataView2(ctx, depth, id, current) {
3475
- return createDataViewNode(id, current, parseSOS(ctx, depth, current.buffer));
3476
- }
3477
- function parseError2(ctx, depth, id, current) {
3478
- const options = getErrorOptions(current, ctx.base.features);
3479
- return createErrorNode(
3480
- id,
3481
- current,
3482
- options ? parseProperties2(ctx, depth, options) : NIL
3483
- );
3484
- }
3485
- function parseAggregateError2(ctx, depth, id, current) {
3486
- const options = getErrorOptions(current, ctx.base.features);
3487
- return createAggregateErrorNode(
3488
- id,
3489
- current,
3490
- options ? parseProperties2(ctx, depth, options) : NIL
3491
- );
3492
- }
3493
- function parseMap2(ctx, depth, id, current) {
3494
- const keyNodes = [];
3495
- const valueNodes = [];
3496
- for (const [key, value] of current.entries()) {
3497
- keyNodes.push(parseSOS(ctx, depth, key));
3498
- valueNodes.push(parseSOS(ctx, depth, value));
3499
- }
3500
- return createMapNode(ctx.base, id, keyNodes, valueNodes);
3501
- }
3502
- function parseSet2(ctx, depth, id, current) {
3503
- const items = [];
3504
- for (const item of current.keys()) {
3505
- items.push(parseSOS(ctx, depth, item));
3506
- }
3507
- return createSetNode(id, items);
3508
- }
3509
- function parseStream2(ctx, depth, id, current) {
3510
- const result = createStreamConstructorNode(
3511
- id,
3512
- parseSpecialReference(ctx.base, 4 /* StreamConstructor */),
3513
- []
3514
- );
3515
- if (ctx.type === 1 /* Sync */) {
3516
- return result;
3517
- }
3518
- pushPendingState(ctx);
3519
- current.on({
3520
- next: (value) => {
3521
- if (ctx.state.alive) {
3522
- const parsed = parseWithError(ctx, depth, value);
3523
- if (parsed) {
3524
- onParse(ctx, createStreamNextNode(id, parsed));
3525
- }
3526
- }
3527
- },
3528
- throw: (value) => {
3529
- if (ctx.state.alive) {
3530
- const parsed = parseWithError(ctx, depth, value);
3531
- if (parsed) {
3532
- onParse(ctx, createStreamThrowNode(id, parsed));
3533
- }
3534
- }
3535
- popPendingState(ctx);
3536
- },
3537
- return: (value) => {
3538
- if (ctx.state.alive) {
3539
- const parsed = parseWithError(ctx, depth, value);
3540
- if (parsed) {
3541
- onParse(ctx, createStreamReturnNode(id, parsed));
3542
- }
3543
- }
3544
- popPendingState(ctx);
3545
- }
3546
- });
3547
- return result;
3548
- }
3549
- function handlePromiseSuccess(id, depth, data) {
3550
- if (this.state.alive) {
3551
- const parsed = parseWithError(this, depth, data);
3552
- if (parsed) {
3553
- onParse(
3554
- this,
3555
- createSerovalNode(
3556
- 23 /* PromiseSuccess */,
3557
- id,
3558
- NIL,
3559
- NIL,
3560
- NIL,
3561
- NIL,
3562
- NIL,
3563
- [
3564
- parseSpecialReference(this.base, 2 /* PromiseSuccess */),
3565
- parsed
3566
- ],
3567
- NIL,
3568
- NIL,
3569
- NIL,
3570
- NIL
3571
- )
3572
- );
3573
- }
3574
- popPendingState(this);
3575
- }
3576
- }
3577
- function handlePromiseFailure(id, depth, data) {
3578
- if (this.state.alive) {
3579
- const parsed = parseWithError(this, depth, data);
3580
- if (parsed) {
3581
- onParse(
3582
- this,
3583
- createSerovalNode(
3584
- 24 /* PromiseFailure */,
3585
- id,
3586
- NIL,
3587
- NIL,
3588
- NIL,
3589
- NIL,
3590
- NIL,
3591
- [
3592
- parseSpecialReference(this.base, 3 /* PromiseFailure */),
3593
- parsed
3594
- ],
3595
- NIL,
3596
- NIL,
3597
- NIL,
3598
- NIL
3599
- )
3600
- );
3601
- }
3602
- }
3603
- popPendingState(this);
3604
- }
3605
- function parsePromise2(ctx, depth, id, current) {
3606
- const resolver = createIndexForValue(ctx.base, {});
3607
- if (ctx.type === 2 /* Stream */) {
3608
- pushPendingState(ctx);
3609
- current.then(
3610
- handlePromiseSuccess.bind(ctx, resolver, depth),
3611
- handlePromiseFailure.bind(ctx, resolver, depth)
3612
- );
3613
- }
3614
- return createPromiseConstructorNode(ctx.base, id, resolver);
3615
- }
3616
- function parsePluginSync(ctx, depth, id, current, currentPlugins) {
3617
- for (let i = 0, len = currentPlugins.length; i < len; i++) {
3618
- const plugin = currentPlugins[i];
3619
- if (plugin.parse.sync && plugin.test(current)) {
3620
- return createPluginNode(
3621
- id,
3622
- plugin.tag,
3623
- plugin.parse.sync(current, new SyncParsePluginContext(ctx, depth), {
3624
- id
3625
- })
3626
- );
3627
- }
3628
- }
3629
- return NIL;
3630
- }
3631
- function parsePluginStream(ctx, depth, id, current, currentPlugins) {
3632
- for (let i = 0, len = currentPlugins.length; i < len; i++) {
3633
- const plugin = currentPlugins[i];
3634
- if (plugin.parse.stream && plugin.test(current)) {
3635
- return createPluginNode(
3636
- id,
3637
- plugin.tag,
3638
- plugin.parse.stream(current, new StreamParsePluginContext(ctx, depth), {
3639
- id
3640
- })
3641
- );
3642
- }
3643
- }
3644
- return NIL;
3645
- }
3646
- function parsePlugin2(ctx, depth, id, current) {
3647
- const currentPlugins = ctx.base.plugins;
3648
- if (currentPlugins) {
3649
- return ctx.type === 1 /* Sync */ ? parsePluginSync(ctx, depth, id, current, currentPlugins) : parsePluginStream(ctx, depth, id, current, currentPlugins);
3650
- }
3651
- return NIL;
3652
- }
3653
- function parseSequence2(ctx, depth, id, current) {
3654
- const nodes = [];
3655
- for (let i = 0, len = current.v.length; i < len; i++) {
3656
- nodes[i] = parseSOS(ctx, depth, current.v[i]);
3657
- }
3658
- return createSequenceNode(id, nodes, current.t, current.d);
3659
- }
3660
- function parseObjectPhase2(ctx, depth, id, current, currentClass) {
3661
- switch (currentClass) {
3662
- case Object:
3663
- return parsePlainObject2(
3664
- ctx,
3665
- depth,
3666
- id,
3667
- current,
3668
- false
3669
- );
3670
- case NIL:
3671
- return parsePlainObject2(
3672
- ctx,
3673
- depth,
3674
- id,
3675
- current,
3676
- true
3677
- );
3678
- case Date:
3679
- return createDateNode(id, current);
3680
- case Error:
3681
- case EvalError:
3682
- case RangeError:
3683
- case ReferenceError:
3684
- case SyntaxError:
3685
- case TypeError:
3686
- case URIError:
3687
- return parseError2(ctx, depth, id, current);
3688
- case Number:
3689
- case Boolean:
3690
- case String:
3691
- case BigInt:
3692
- return parseBoxed2(ctx, depth, id, current);
3693
- case ArrayBuffer:
3694
- return createArrayBufferNode(
3695
- ctx.base,
3696
- id,
3697
- current
3698
- );
3699
- case Int8Array:
3700
- case Int16Array:
3701
- case Int32Array:
3702
- case Uint8Array:
3703
- case Uint16Array:
3704
- case Uint32Array:
3705
- case Uint8ClampedArray:
3706
- case Float32Array:
3707
- case Float64Array:
3708
- return parseTypedArray2(
3709
- ctx,
3710
- depth,
3711
- id,
3712
- current
3713
- );
3714
- case DataView:
3715
- return parseDataView2(ctx, depth, id, current);
3716
- case Map:
3717
- return parseMap2(
3718
- ctx,
3719
- depth,
3720
- id,
3721
- current
3722
- );
3723
- case Set:
3724
- return parseSet2(ctx, depth, id, current);
3725
- default:
3726
- break;
3727
- }
3728
- if (currentClass === Promise || current instanceof Promise) {
3729
- return parsePromise2(ctx, depth, id, current);
3730
- }
3731
- const currentFeatures = ctx.base.features;
3732
- if (currentFeatures & 32 /* RegExp */ && currentClass === RegExp) {
3733
- return createRegExpNode(id, current);
3734
- }
3735
- if (currentFeatures & 16 /* BigIntTypedArray */) {
3736
- switch (currentClass) {
3737
- case BigInt64Array:
3738
- case BigUint64Array:
3739
- return parseBigIntTypedArray2(
3740
- ctx,
3741
- depth,
3742
- id,
3743
- current
3744
- );
3745
- default:
3746
- break;
3747
- }
3748
- }
3749
- if (currentFeatures & 1 /* AggregateError */ && typeof AggregateError !== "undefined" && (currentClass === AggregateError || current instanceof AggregateError)) {
3750
- return parseAggregateError2(
3751
- ctx,
3752
- depth,
3753
- id,
3754
- current
3755
- );
3756
- }
3757
- if (current instanceof Error) {
3758
- return parseError2(ctx, depth, id, current);
3759
- }
3760
- if (SYM_ITERATOR in current || SYM_ASYNC_ITERATOR in current) {
3761
- return parsePlainObject2(ctx, depth, id, current, !!currentClass);
3762
- }
3763
- throw new SerovalUnsupportedTypeError(current);
3764
- }
3765
- function parseObject(ctx, depth, id, current) {
3766
- if (Array.isArray(current)) {
3767
- return parseArray2(ctx, depth, id, current);
3768
- }
3769
- if (isStream(current)) {
3770
- return parseStream2(ctx, depth, id, current);
3771
- }
3772
- if (isSequence(current)) {
3773
- return parseSequence2(ctx, depth, id, current);
3774
- }
3775
- const currentClass = current.constructor;
3776
- if (currentClass === OpaqueReference) {
3777
- return parseSOS(
3778
- ctx,
3779
- depth,
3780
- current.replacement
3781
- );
3782
- }
3783
- const parsed = parsePlugin2(ctx, depth, id, current);
3784
- if (parsed) {
3785
- return parsed;
3786
- }
3787
- return parseObjectPhase2(ctx, depth, id, current, currentClass);
3788
- }
3789
- function parseFunction(ctx, depth, current) {
3790
- const ref = getReferenceNode(ctx.base, current);
3791
- if (ref.type !== 0 /* Fresh */) {
3792
- return ref.value;
3793
- }
3794
- const plugin = parsePlugin2(ctx, depth, ref.value, current);
3795
- if (plugin) {
3796
- return plugin;
3797
- }
3798
- throw new SerovalUnsupportedTypeError(current);
3799
- }
3800
- function parseSOS(ctx, depth, current) {
3801
- if (depth >= ctx.base.depthLimit) {
3802
- throw new SerovalDepthLimitError(ctx.base.depthLimit);
3803
- }
3804
- switch (typeof current) {
3805
- case "boolean":
3806
- return current ? TRUE_NODE : FALSE_NODE;
3807
- case "undefined":
3808
- return UNDEFINED_NODE;
3809
- case "string":
3810
- return createStringNode(current);
3811
- case "number":
3812
- return createNumberNode(current);
3813
- case "bigint":
3814
- return createBigIntNode(current);
3815
- case "object": {
3816
- if (current) {
3817
- const ref = getReferenceNode(ctx.base, current);
3818
- return ref.type === 0 /* Fresh */ ? parseObject(ctx, depth + 1, ref.value, current) : ref.value;
3819
- }
3820
- return NULL_NODE;
3821
- }
3822
- case "symbol":
3823
- return parseWellKnownSymbol(ctx.base, current);
3824
- case "function": {
3825
- return parseFunction(ctx, depth, current);
3826
- }
3827
- default:
3828
- throw new SerovalUnsupportedTypeError(current);
3829
- }
3830
- }
3831
- function parseTop(ctx, current) {
3832
- try {
3833
- return parseSOS(ctx, 0, current);
3834
- } catch (error) {
3835
- throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
3836
- }
3837
- }
3838
- function onParse(ctx, node) {
3839
- if (ctx.state.initial) {
3840
- ctx.state.buffer.push(node);
3841
- } else {
3842
- onParseInternal(ctx, node, false);
3843
- }
3844
- }
3845
- function onError(ctx, error) {
3846
- if (ctx.state.onError) {
3847
- ctx.state.onError(error);
3848
- } else {
3849
- throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
3850
- }
3851
- }
3852
- function onDone(ctx) {
3853
- if (ctx.state.onDone) {
3854
- ctx.state.onDone();
3855
- }
3856
- for (let i = 0, len = ctx.state.cleanups.length; i < len; i++) {
3857
- ctx.state.cleanups[i]();
3858
- }
3859
- }
3860
- function onParseInternal(ctx, node, initial) {
3861
- try {
3862
- ctx.state.onParse(node, initial);
3863
- } catch (error) {
3864
- onError(ctx, error);
3865
- }
3866
- }
3867
- function pushPendingState(ctx) {
3868
- ctx.state.pending++;
3869
- }
3870
- function popPendingState(ctx) {
3871
- if (--ctx.state.pending <= 0) {
3872
- onDone(ctx);
3873
- }
3874
- }
3875
- function parseWithError(ctx, depth, current) {
3876
- try {
3877
- return parseSOS(ctx, depth, current);
3878
- } catch (err) {
3879
- onError(ctx, err);
3880
- return NIL;
3881
- }
3882
- }
3883
- function startStreamParse(ctx, current) {
3884
- const parsed = parseWithError(ctx, 0, current);
3885
- if (parsed) {
3886
- onParseInternal(ctx, parsed, true);
3887
- ctx.state.initial = false;
3888
- flushStreamParse(ctx, ctx.state);
3889
- if (ctx.state.pending <= 0) {
3890
- destroyStreamParse(ctx);
3891
- }
3892
- }
3893
- }
3894
- function flushStreamParse(ctx, state) {
3895
- for (let i = 0, len = state.buffer.length; i < len; i++) {
3896
- onParseInternal(ctx, state.buffer[i], false);
3897
- }
3898
- }
3899
- function destroyStreamParse(ctx) {
3900
- if (ctx.state.alive) {
3901
- onDone(ctx);
3902
- ctx.state.alive = false;
3903
- }
3904
- }
3905
-
3906
- // src/core/cross/index.ts
3907
- function crossSerialize(source, options = {}) {
3908
- const plugins = resolvePlugins(options.plugins);
3909
- const ctx = createSyncParserContext(2 /* Cross */, {
3910
- plugins,
3911
- disabledFeatures: options.disabledFeatures,
3912
- refs: options.refs
3913
- });
3914
- const tree = parseTop(ctx, source);
3915
- const serial = createCrossSerializerContext({
3916
- plugins,
3917
- features: ctx.base.features,
3918
- scopeId: options.scopeId,
3919
- markedRefs: ctx.base.marked
3920
- });
3921
- return serializeTopCross(serial, tree);
3922
- }
3923
- async function crossSerializeAsync(source, options = {}) {
3924
- const plugins = resolvePlugins(options.plugins);
3925
- const ctx = createAsyncParserContext(2 /* Cross */, {
3926
- plugins,
3927
- disabledFeatures: options.disabledFeatures,
3928
- refs: options.refs
3929
- });
3930
- const tree = await parseTopAsync(ctx, source);
3931
- const serial = createCrossSerializerContext({
3932
- plugins,
3933
- features: ctx.base.features,
3934
- scopeId: options.scopeId,
3935
- markedRefs: ctx.base.marked
3936
- });
3937
- return serializeTopCross(serial, tree);
3938
- }
3939
- function toCrossJSON(source, options = {}) {
3940
- const plugins = resolvePlugins(options.plugins);
3941
- const ctx = createSyncParserContext(2 /* Cross */, {
3942
- plugins,
3943
- disabledFeatures: options.disabledFeatures,
3944
- refs: options.refs
3945
- });
3946
- return parseTop(ctx, source);
3947
- }
3948
- async function toCrossJSONAsync(source, options = {}) {
3949
- const plugins = resolvePlugins(options.plugins);
3950
- const ctx = createAsyncParserContext(2 /* Cross */, {
3951
- plugins,
3952
- disabledFeatures: options.disabledFeatures,
3953
- refs: options.refs
3954
- });
3955
- return await parseTopAsync(ctx, source);
3956
- }
3957
- function crossSerializeStream(source, options) {
3958
- const plugins = resolvePlugins(options.plugins);
3959
- const ctx = createStreamParserContext({
3960
- plugins,
3961
- refs: options.refs,
3962
- disabledFeatures: options.disabledFeatures,
3963
- onParse(node, initial) {
3964
- const serial = createCrossSerializerContext({
3965
- plugins,
3966
- features: ctx.base.features,
3967
- scopeId: options.scopeId,
3968
- markedRefs: ctx.base.marked
3969
- });
3970
- let serialized;
3971
- try {
3972
- serialized = serializeTopCross(serial, node);
3973
- } catch (err) {
3974
- if (options.onError) {
3975
- options.onError(err);
3976
- }
3977
- return;
3978
- }
3979
- options.onSerialize(serialized, initial);
3980
- },
3981
- onError: options.onError,
3982
- onDone: options.onDone
3983
- });
3984
- startStreamParse(ctx, source);
3985
- return destroyStreamParse.bind(null, ctx);
3986
- }
3987
- function toCrossJSONStream(source, options) {
3988
- const plugins = resolvePlugins(options.plugins);
3989
- const ctx = createStreamParserContext({
3990
- plugins,
3991
- refs: options.refs,
3992
- disabledFeatures: options.disabledFeatures,
3993
- depthLimit: options.depthLimit,
3994
- onParse: options.onParse,
3995
- onError: options.onError,
3996
- onDone: options.onDone
3997
- });
3998
- startStreamParse(ctx, source);
3999
- return destroyStreamParse.bind(null, ctx);
4000
- }
4001
- function fromCrossJSON(source, options) {
4002
- const plugins = resolvePlugins(options.plugins);
4003
- const ctx = createCrossDeserializerContext({
4004
- plugins,
4005
- refs: options.refs,
4006
- features: options.features,
4007
- disabledFeatures: options.disabledFeatures,
4008
- depthLimit: options.depthLimit
4009
- });
4010
- return deserializeTop(ctx, source);
4011
- }
4012
-
4013
- // src/core/Serializer.ts
4014
- var Serializer = class {
4015
- constructor(options) {
4016
- this.options = options;
4017
- this.alive = true;
4018
- this.flushed = false;
4019
- this.done = false;
4020
- this.pending = 0;
4021
- this.cleanups = [];
4022
- this.refs = /* @__PURE__ */ new Map();
4023
- this.keys = /* @__PURE__ */ new Set();
4024
- this.ids = 0;
4025
- this.plugins = resolvePlugins(options.plugins);
4026
- }
4027
- write(key, value) {
4028
- if (this.alive && !this.flushed) {
4029
- this.pending++;
4030
- this.keys.add(key);
4031
- this.cleanups.push(
4032
- crossSerializeStream(value, {
4033
- plugins: this.plugins,
4034
- scopeId: this.options.scopeId,
4035
- refs: this.refs,
4036
- disabledFeatures: this.options.disabledFeatures,
4037
- onError: this.options.onError,
4038
- onSerialize: (data, initial) => {
4039
- if (this.alive) {
4040
- this.options.onData(
4041
- initial ? this.options.globalIdentifier + '["' + serializeString(key) + '"]=' + data : data
4042
- );
4043
- }
4044
- },
4045
- onDone: () => {
4046
- if (this.alive) {
4047
- this.pending--;
4048
- if (this.pending <= 0 && this.flushed && !this.done && this.options.onDone) {
4049
- this.options.onDone();
4050
- this.done = true;
4051
- }
4052
- }
4053
- }
4054
- })
4055
- );
4056
- }
4057
- }
4058
- getNextID() {
4059
- while (this.keys.has("" + this.ids)) {
4060
- this.ids++;
4061
- }
4062
- return "" + this.ids;
4063
- }
4064
- push(value) {
4065
- const newID = this.getNextID();
4066
- this.write(newID, value);
4067
- return newID;
4068
- }
4069
- flush() {
4070
- if (this.alive) {
4071
- this.flushed = true;
4072
- if (this.pending <= 0 && !this.done && this.options.onDone) {
4073
- this.options.onDone();
4074
- this.done = true;
4075
- }
4076
- }
4077
- }
4078
- close() {
4079
- if (this.alive) {
4080
- for (let i = 0, len = this.cleanups.length; i < len; i++) {
4081
- this.cleanups[i]();
4082
- }
4083
- if (!this.done && this.options.onDone) {
4084
- this.options.onDone();
4085
- this.done = true;
4086
- }
4087
- this.alive = false;
4088
- }
4089
- }
4090
- };
4091
-
4092
- // src/core/tree/index.ts
4093
- function serialize2(source, options = {}) {
4094
- const plugins = resolvePlugins(options.plugins);
4095
- const ctx = createSyncParserContext(1 /* Vanilla */, {
4096
- plugins,
4097
- disabledFeatures: options.disabledFeatures
4098
- });
4099
- const tree = parseTop(ctx, source);
4100
- const serial = createVanillaSerializerContext({
4101
- plugins,
4102
- features: ctx.base.features,
4103
- markedRefs: ctx.base.marked
4104
- });
4105
- return serializeTopVanilla(serial, tree);
4106
- }
4107
- async function serializeAsync(source, options = {}) {
4108
- const plugins = resolvePlugins(options.plugins);
4109
- const ctx = createAsyncParserContext(1 /* Vanilla */, {
4110
- plugins,
4111
- disabledFeatures: options.disabledFeatures
4112
- });
4113
- const tree = await parseTopAsync(ctx, source);
4114
- const serial = createVanillaSerializerContext({
4115
- plugins,
4116
- features: ctx.base.features,
4117
- markedRefs: ctx.base.marked
4118
- });
4119
- return serializeTopVanilla(serial, tree);
4120
- }
4121
- function deserialize2(source) {
4122
- return (0, eval)(source);
4123
- }
4124
- function toJSON(source, options = {}) {
4125
- const plugins = resolvePlugins(options.plugins);
4126
- const ctx = createSyncParserContext(1 /* Vanilla */, {
4127
- plugins,
4128
- disabledFeatures: options.disabledFeatures
4129
- });
4130
- return {
4131
- t: parseTop(ctx, source),
4132
- f: ctx.base.features,
4133
- m: Array.from(ctx.base.marked)
4134
- };
4135
- }
4136
- async function toJSONAsync(source, options = {}) {
4137
- const plugins = resolvePlugins(options.plugins);
4138
- const ctx = createAsyncParserContext(1 /* Vanilla */, {
4139
- plugins,
4140
- disabledFeatures: options.disabledFeatures
4141
- });
4142
- return {
4143
- t: await parseTopAsync(ctx, source),
4144
- f: ctx.base.features,
4145
- m: Array.from(ctx.base.marked)
4146
- };
4147
- }
4148
- function compileJSON(source, options = {}) {
4149
- const plugins = resolvePlugins(options.plugins);
4150
- const ctx = createVanillaSerializerContext({
4151
- plugins,
4152
- features: source.f,
4153
- markedRefs: source.m
4154
- });
4155
- return serializeTopVanilla(ctx, source.t);
4156
- }
4157
- function fromJSON(source, options = {}) {
4158
- var _a;
4159
- const plugins = resolvePlugins(options.plugins);
4160
- const disabledFeatures = options.disabledFeatures || 0;
4161
- const sourceFeatures = (_a = source.f) != null ? _a : ALL_ENABLED;
4162
- const ctx = createVanillaDeserializerContext({
4163
- plugins,
4164
- markedRefs: source.m,
4165
- features: sourceFeatures & ~disabledFeatures,
4166
- disabledFeatures
4167
- });
4168
- return deserializeTop(ctx, source.t);
4169
- }
4170
- export {
4171
- Feature,
4172
- OpaqueReference,
4173
- Serializer,
4174
- SerovalConflictedNodeIdError,
4175
- SerovalDepthLimitError,
4176
- SerovalDeserializationError,
4177
- SerovalError,
4178
- SerovalMalformedNodeError,
4179
- SerovalMissingInstanceError,
4180
- SerovalMissingPluginError,
4181
- SerovalMissingReferenceError,
4182
- SerovalMissingReferenceForIdError,
4183
- SerovalMode,
4184
- SerovalParserError,
4185
- SerovalSerializationError,
4186
- SerovalUnknownTypedArrayError,
4187
- SerovalUnsupportedNodeError,
4188
- SerovalUnsupportedTypeError,
4189
- compileJSON,
4190
- createPlugin,
4191
- createReference,
4192
- createStream,
4193
- crossSerialize,
4194
- crossSerializeAsync,
4195
- crossSerializeStream,
4196
- deserialize2 as deserialize,
4197
- fromCrossJSON,
4198
- fromJSON,
4199
- getCrossReferenceHeader,
4200
- resolvePlugins,
4201
- serialize2 as serialize,
4202
- serializeAsync,
4203
- toCrossJSON,
4204
- toCrossJSONAsync,
4205
- toCrossJSONStream,
4206
- toJSON,
4207
- toJSONAsync
4208
- };
4209
- //# sourceMappingURL=index.mjs.map