seroval 1.7.10 → 1.8.0

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 (73) hide show
  1. package/dist/dev/index.cjs +58 -3893
  2. package/dist/dev/index.d.cts +5 -834
  3. package/dist/dev/index.d.ts +5 -834
  4. package/dist/dev/index.js +4 -3847
  5. package/dist/dev/read-Bn_ffmiS.d.ts +14 -0
  6. package/dist/dev/read-CfSVIjp6.js +455 -0
  7. package/dist/dev/read-CgzYPh_Q.d.cts +14 -0
  8. package/dist/dev/read-DvIuZ2yx.cjs +478 -0
  9. package/dist/dev/read.cjs +45 -0
  10. package/dist/dev/read.d.cts +17 -0
  11. package/dist/dev/read.d.ts +17 -0
  12. package/dist/dev/read.js +3 -0
  13. package/dist/dev/shared-Cf-Bmoy-.js +1010 -0
  14. package/dist/dev/shared-DTMVoS-y.d.ts +749 -0
  15. package/dist/dev/shared-DWx3Bwb6.cjs +1410 -0
  16. package/dist/dev/shared-Dju99SLx.d.cts +749 -0
  17. package/dist/dev/shared.cjs +40 -0
  18. package/dist/dev/shared.d.cts +2 -0
  19. package/dist/dev/shared.d.ts +2 -0
  20. package/dist/dev/shared.js +1 -0
  21. package/dist/dev/typed-array-C3haUQv_.js +72 -0
  22. package/dist/dev/typed-array-DUH_YsNc.cjs +101 -0
  23. package/dist/dev/validator-CxDhXc0k.d.cts +28 -0
  24. package/dist/dev/validator-CxDhXc0k.d.ts +28 -0
  25. package/dist/dev/validator.cjs +54 -0
  26. package/dist/dev/validator.d.cts +2 -0
  27. package/dist/dev/validator.d.ts +2 -0
  28. package/dist/dev/validator.js +45 -0
  29. package/dist/dev/write-BKJsV31u.js +2354 -0
  30. package/dist/dev/write-C0KqlAXN.d.ts +86 -0
  31. package/dist/dev/write-DGOp2jqo.cjs +2425 -0
  32. package/dist/dev/write-KxJKA4_p.d.cts +86 -0
  33. package/dist/dev/write.cjs +54 -0
  34. package/dist/dev/write.d.cts +4 -0
  35. package/dist/dev/write.d.ts +4 -0
  36. package/dist/dev/write.js +3 -0
  37. package/dist/index.cjs +58 -3877
  38. package/dist/index.d.cts +5 -834
  39. package/dist/index.d.ts +5 -834
  40. package/dist/index.js +4 -3831
  41. package/dist/read-Bn_ffmiS.d.ts +14 -0
  42. package/dist/read-CD88YElZ.cjs +478 -0
  43. package/dist/read-CSKXLr9t.js +455 -0
  44. package/dist/read-CgzYPh_Q.d.cts +14 -0
  45. package/dist/read.cjs +45 -0
  46. package/dist/read.d.cts +17 -0
  47. package/dist/read.d.ts +17 -0
  48. package/dist/read.js +3 -0
  49. package/dist/shared-DJIVyqa9.js +994 -0
  50. package/dist/shared-DTMVoS-y.d.ts +749 -0
  51. package/dist/shared-Dju99SLx.d.cts +749 -0
  52. package/dist/shared-u-KYXAax.cjs +1394 -0
  53. package/dist/shared.cjs +40 -0
  54. package/dist/shared.d.cts +2 -0
  55. package/dist/shared.d.ts +2 -0
  56. package/dist/shared.js +1 -0
  57. package/dist/typed-array-BWH0IKxT.js +72 -0
  58. package/dist/typed-array-BkSwWNFI.cjs +101 -0
  59. package/dist/validator-CxDhXc0k.d.cts +28 -0
  60. package/dist/validator-CxDhXc0k.d.ts +28 -0
  61. package/dist/validator.cjs +54 -0
  62. package/dist/validator.d.cts +2 -0
  63. package/dist/validator.d.ts +2 -0
  64. package/dist/validator.js +45 -0
  65. package/dist/write-B_hZaAwC.js +2354 -0
  66. package/dist/write-C0KqlAXN.d.ts +86 -0
  67. package/dist/write-CMi0CxOu.cjs +2425 -0
  68. package/dist/write-KxJKA4_p.d.cts +86 -0
  69. package/dist/write.cjs +54 -0
  70. package/dist/write.d.cts +4 -0
  71. package/dist/write.d.ts +4 -0
  72. package/dist/write.js +3 -0
  73. package/package.json +97 -1
@@ -0,0 +1,1010 @@
1
+ import * as v from "./validator.js";
2
+ //#region src/core/constructors.ts
3
+ const PROMISE_CONSTRUCTOR = () => {
4
+ const resolver = {
5
+ p: 0,
6
+ s: 0,
7
+ f: 0
8
+ };
9
+ resolver.p = new Promise((resolve, reject) => {
10
+ resolver.s = resolve;
11
+ resolver.f = reject;
12
+ });
13
+ return resolver;
14
+ };
15
+ const PROMISE_SUCCESS = (resolver, data) => {
16
+ resolver.s(data);
17
+ resolver.p.s = 1;
18
+ resolver.p.v = data;
19
+ };
20
+ const PROMISE_FAILURE = (resolver, data) => {
21
+ resolver.f(data);
22
+ resolver.p.s = 2;
23
+ resolver.p.v = data;
24
+ };
25
+ const SERIALIZED_PROMISE_CONSTRUCTOR = /* @__PURE__ */ PROMISE_CONSTRUCTOR.toString();
26
+ const SERIALIZED_PROMISE_SUCCESS = /* @__PURE__ */ PROMISE_SUCCESS.toString();
27
+ const SERIALIZED_PROMISE_FAILURE = /* @__PURE__ */ PROMISE_FAILURE.toString();
28
+ const STREAM_CONSTRUCTOR = (live) => {
29
+ const buffer = [];
30
+ const listeners = [];
31
+ let alive = true;
32
+ let success = false;
33
+ let count = 0;
34
+ const internal = {
35
+ flush(value, mode, x) {
36
+ live > 1 || buffer.push(value);
37
+ for (x = 0; x < count; x++) {
38
+ const listener = listeners[x];
39
+ if (listener) listener[mode](value);
40
+ }
41
+ },
42
+ up(listener, x, z, current) {
43
+ for (x = 0, z = buffer.length; x < z; x++) {
44
+ current = buffer[x];
45
+ if (!alive && x === z - 1) listener[success ? "return" : "throw"](current);
46
+ else listener.next(current);
47
+ }
48
+ },
49
+ on(listener, temp = 0) {
50
+ if (live > 1) throw new Error("Stream consumed");
51
+ let subscribed = alive;
52
+ if (alive) {
53
+ for (temp = 0; temp < count; temp++) if (!listeners[temp]) break;
54
+ if (temp === count) count++;
55
+ listeners[temp] = listener;
56
+ }
57
+ internal.up(listener);
58
+ if (live) {
59
+ live = 2;
60
+ buffer.length = 0;
61
+ }
62
+ return () => {
63
+ if (alive && subscribed) {
64
+ subscribed = false;
65
+ listeners[temp] = void 0;
66
+ while (count > 0 && !listeners[count - 1]) count--;
67
+ listeners.length = count;
68
+ }
69
+ };
70
+ }
71
+ };
72
+ return {
73
+ __SEROVAL_STREAM__: true,
74
+ on(listener) {
75
+ return internal.on(listener);
76
+ },
77
+ next(value) {
78
+ if (alive) internal.flush(value, "next");
79
+ },
80
+ throw(value) {
81
+ if (alive) {
82
+ internal.flush(value, "throw");
83
+ alive = false;
84
+ success = false;
85
+ listeners.length = 0;
86
+ }
87
+ },
88
+ return(value) {
89
+ if (alive) {
90
+ internal.flush(value, "return");
91
+ alive = false;
92
+ success = true;
93
+ listeners.length = 0;
94
+ }
95
+ }
96
+ };
97
+ };
98
+ const SERIALIZED_STREAM_CONSTRUCTOR = /* @__PURE__ */ STREAM_CONSTRUCTOR.toString();
99
+ const ITERATOR_CONSTRUCTOR = (symbol) => (sequence) => () => {
100
+ let index = 0;
101
+ const instance = {
102
+ [symbol]() {
103
+ return instance;
104
+ },
105
+ next() {
106
+ if (index > sequence.d) return {
107
+ done: true,
108
+ value: void 0
109
+ };
110
+ const currentIndex = index++;
111
+ const data = sequence.v[currentIndex];
112
+ if (currentIndex === sequence.t) throw data;
113
+ return {
114
+ done: currentIndex === sequence.d,
115
+ value: data
116
+ };
117
+ }
118
+ };
119
+ return instance;
120
+ };
121
+ const SERIALIZED_ITERATOR_CONSTRUCTOR = /* @__PURE__ */ ITERATOR_CONSTRUCTOR.toString();
122
+ const ASYNC_ITERATOR_CONSTRUCTOR = (symbol, createPromise) => (stream) => () => {
123
+ let count = 0;
124
+ let doneAt = -1;
125
+ let isThrow = false;
126
+ const buffer = [];
127
+ const pending = [];
128
+ const internal = { finalize(i = 0, len = pending.length) {
129
+ for (; i < len; i++) pending[i].s({
130
+ done: true,
131
+ value: void 0
132
+ });
133
+ } };
134
+ stream.on({
135
+ next(value) {
136
+ const temp = pending.shift();
137
+ if (temp) temp.s({
138
+ done: false,
139
+ value
140
+ });
141
+ buffer.push(value);
142
+ },
143
+ throw(value) {
144
+ const temp = pending.shift();
145
+ if (temp) temp.f(value);
146
+ internal.finalize();
147
+ doneAt = buffer.length;
148
+ isThrow = true;
149
+ buffer.push(value);
150
+ },
151
+ return(value) {
152
+ const temp = pending.shift();
153
+ if (temp) temp.s({
154
+ done: true,
155
+ value
156
+ });
157
+ internal.finalize();
158
+ doneAt = buffer.length;
159
+ buffer.push(value);
160
+ }
161
+ });
162
+ const instance = {
163
+ [symbol]() {
164
+ return instance;
165
+ },
166
+ next() {
167
+ if (doneAt === -1) {
168
+ const index = count++;
169
+ if (index >= buffer.length) {
170
+ const temp = createPromise();
171
+ pending.push(temp);
172
+ return temp.p;
173
+ }
174
+ return {
175
+ done: false,
176
+ value: buffer[index]
177
+ };
178
+ }
179
+ if (count > doneAt) return {
180
+ done: true,
181
+ value: void 0
182
+ };
183
+ const index = count++;
184
+ const value = buffer[index];
185
+ if (index !== doneAt) return {
186
+ done: false,
187
+ value
188
+ };
189
+ if (isThrow) throw value;
190
+ return {
191
+ done: true,
192
+ value
193
+ };
194
+ }
195
+ };
196
+ return instance;
197
+ };
198
+ const SERIALIZED_ASYNC_ITERATOR_CONSTRUCTOR = /* @__PURE__ */ ASYNC_ITERATOR_CONSTRUCTOR.toString();
199
+ const ARRAY_BUFFER_CONSTRUCTOR = (b64) => {
200
+ const decoded = atob(b64);
201
+ const length = decoded.length;
202
+ const arr = new Uint8Array(length);
203
+ for (let i = 0; i < length; i++) arr[i] = decoded.charCodeAt(i);
204
+ return arr.buffer;
205
+ };
206
+ const SERIALIZED_ARRAY_BUFFER_CONSTRUCTOR = /* @__PURE__ */ ARRAY_BUFFER_CONSTRUCTOR.toString();
207
+ //#endregion
208
+ //#region src/core/compat.ts
209
+ /**
210
+ * Public feature bits for `disabledFeatures`. Stays a runtime `enum` so the
211
+ * published declaration is unchanged for consumers. Its values must match
212
+ * the internal bits (checked by test/feature.test.ts).
213
+ */
214
+ let Feature = /* @__PURE__ */ function(Feature) {
215
+ Feature[Feature["AggregateError"] = 1] = "AggregateError";
216
+ /**
217
+ * @deprecated Has no effect. Output follows the syntax of the build seroval
218
+ * runs in: arrow functions, or `function` expressions in an ES5 build.
219
+ */
220
+ Feature[Feature["ArrowFunction"] = 2] = "ArrowFunction";
221
+ Feature[Feature["ErrorPrototypeStack"] = 4] = "ErrorPrototypeStack";
222
+ Feature[Feature["ObjectAssign"] = 8] = "ObjectAssign";
223
+ Feature[Feature["BigIntTypedArray"] = 16] = "BigIntTypedArray";
224
+ Feature[Feature["RegExp"] = 32] = "RegExp";
225
+ Feature[Feature["Temporal"] = 64] = "Temporal";
226
+ return Feature;
227
+ }({});
228
+ //#endregion
229
+ //#region src/core/symbols.ts
230
+ const SYM_ASYNC_ITERATOR = Symbol.asyncIterator;
231
+ const SYM_HAS_INSTANCE = Symbol.hasInstance;
232
+ const SYM_IS_CONCAT_SPREADABLE = Symbol.isConcatSpreadable;
233
+ const SYM_ITERATOR = Symbol.iterator;
234
+ const SYM_MATCH = Symbol.match;
235
+ const SYM_MATCH_ALL = Symbol.matchAll;
236
+ const SYM_REPLACE = Symbol.replace;
237
+ const SYM_SEARCH = Symbol.search;
238
+ const SYM_SPECIES = Symbol.species;
239
+ const SYM_SPLIT = Symbol.split;
240
+ const SYM_TO_PRIMITIVE = Symbol.toPrimitive;
241
+ const SYM_TO_STRING_TAG = Symbol.toStringTag;
242
+ const SYM_UNSCOPABLES = Symbol.unscopables;
243
+ //#endregion
244
+ //#region src/core/constants.ts
245
+ const TEMPORAL_TYPE_NAME = {
246
+ [0]: "Instant",
247
+ [1]: "Duration",
248
+ [2]: "PlainDate",
249
+ [3]: "PlainDateTime",
250
+ [4]: "PlainMonthDay",
251
+ [5]: "PlainTime",
252
+ [6]: "PlainYearMonth",
253
+ [7]: "ZonedDateTime"
254
+ };
255
+ const SYMBOL_REF = {
256
+ [0]: SYM_ASYNC_ITERATOR,
257
+ [1]: SYM_HAS_INSTANCE,
258
+ [2]: SYM_IS_CONCAT_SPREADABLE,
259
+ [3]: SYM_ITERATOR,
260
+ [4]: SYM_MATCH,
261
+ [5]: SYM_MATCH_ALL,
262
+ [6]: SYM_REPLACE,
263
+ [7]: SYM_SEARCH,
264
+ [8]: SYM_SPECIES,
265
+ [9]: SYM_SPLIT,
266
+ [10]: SYM_TO_PRIMITIVE,
267
+ [11]: SYM_TO_STRING_TAG,
268
+ [12]: SYM_UNSCOPABLES
269
+ };
270
+ function createSymbolStringTable() {
271
+ const result = {};
272
+ for (let i = 0; i <= 12; i++) result[i] = SYMBOL_REF[i].description;
273
+ return result;
274
+ }
275
+ const SYMBOL_STRING = /* @__PURE__ */ createSymbolStringTable();
276
+ function createInverseSymbolTable() {
277
+ const result = {};
278
+ for (let i = 0; i <= 12; i++) result[SYMBOL_REF[i]] = i;
279
+ return result;
280
+ }
281
+ const INV_SYMBOL_REF = /* @__PURE__ */ createInverseSymbolTable();
282
+ const CONSTANT_STRING = {
283
+ [2]: "!0",
284
+ [3]: "!1",
285
+ [1]: "void 0",
286
+ [0]: "null",
287
+ [4]: "-0",
288
+ [5]: "1/0",
289
+ [6]: "-1/0",
290
+ [7]: "0/0"
291
+ };
292
+ const CONSTANT_VAL = {
293
+ [2]: true,
294
+ [3]: false,
295
+ [1]: void 0,
296
+ [0]: null,
297
+ [4]: -0,
298
+ [5]: Infinity,
299
+ [6]: -Infinity,
300
+ [7]: NaN
301
+ };
302
+ const ERROR_CONSTRUCTOR = {
303
+ [0]: Error,
304
+ [1]: EvalError,
305
+ [2]: RangeError,
306
+ [3]: ReferenceError,
307
+ [4]: SyntaxError,
308
+ [5]: TypeError,
309
+ [6]: URIError
310
+ };
311
+ function createErrorConstructorStringTable() {
312
+ const result = {};
313
+ for (let i = 0; i <= 6; i++) result[i] = ERROR_CONSTRUCTOR[i].name;
314
+ return result;
315
+ }
316
+ const ERROR_CONSTRUCTOR_STRING = /* @__PURE__ */ createErrorConstructorStringTable();
317
+ //#endregion
318
+ //#region src/core/string.ts
319
+ const MIN_JSON_STRINGIFY_LENGTH = 64;
320
+ const NEEDS_ESCAPE = /["\\\n\r\b\t\f<\u2028\u2029]/;
321
+ const JSON_ESCAPE_DIFFERENCES = /[\x00-\x07\x0b\x0e-\x1f\ud800-\udfff]/;
322
+ const LESS_THAN = /</g;
323
+ const LINE_SEPARATOR = /\u2028/g;
324
+ const PARAGRAPH_SEPARATOR = /\u2029/g;
325
+ function escapeCharCode(code) {
326
+ switch (code) {
327
+ case 34: return "\\\"";
328
+ case 92: return "\\\\";
329
+ case 10: return "\\n";
330
+ case 13: return "\\r";
331
+ case 8: return "\\b";
332
+ case 9: return "\\t";
333
+ case 12: return "\\f";
334
+ case 60: return "\\x3C";
335
+ case 8232: return "\\u2028";
336
+ case 8233: return "\\u2029";
337
+ default: return;
338
+ }
339
+ }
340
+ function serializeString(input) {
341
+ const str = "" + input;
342
+ if (!NEEDS_ESCAPE.test(str)) return str;
343
+ if (str.length >= MIN_JSON_STRINGIFY_LENGTH && !JSON_ESCAPE_DIFFERENCES.test(str)) {
344
+ let result = JSON.stringify(str).slice(1, -1);
345
+ if (result.includes("<")) result = result.replace(LESS_THAN, "\\x3C");
346
+ if (result.includes("\u2028")) result = result.replace(LINE_SEPARATOR, "\\u2028");
347
+ if (result.includes("\u2029")) result = result.replace(PARAGRAPH_SEPARATOR, "\\u2029");
348
+ return result;
349
+ }
350
+ let result = "";
351
+ let lastPos = 0;
352
+ let replacement;
353
+ for (let i = 0, len = str.length; i < len; i++) {
354
+ replacement = escapeCharCode(str.charCodeAt(i));
355
+ if (replacement) {
356
+ result += str.slice(lastPos, i) + replacement;
357
+ lastPos = i + 1;
358
+ }
359
+ }
360
+ return result + str.slice(lastPos);
361
+ }
362
+ const JSON_COMPATIBLE = /^(?:[^"\\\x00-\x1f]|\\["\\nrbtf])*$/;
363
+ const ESCAPE_SEQUENCE = /(\\\\|\\"|\\n|\\r|\\b|\\t|\\f|\\u2028|\\u2029|\\x3C)/g;
364
+ function deserializeString(str) {
365
+ if (typeof str !== "string") return str.replace(ESCAPE_SEQUENCE, deserializeString);
366
+ let index = str.indexOf("\\");
367
+ if (index === -1) return str;
368
+ if (JSON_COMPATIBLE.test(str)) return JSON.parse("\"" + str + "\"");
369
+ let result = "";
370
+ let lastPos = 0;
371
+ const last = str.length - 1;
372
+ while (index !== -1 && index < last) {
373
+ let replacement;
374
+ let length = 2;
375
+ switch (str.charCodeAt(index + 1)) {
376
+ case 92:
377
+ replacement = "\\";
378
+ break;
379
+ case 34:
380
+ replacement = "\"";
381
+ break;
382
+ case 110:
383
+ replacement = "\n";
384
+ break;
385
+ case 114:
386
+ replacement = "\r";
387
+ break;
388
+ case 98:
389
+ replacement = "\b";
390
+ break;
391
+ case 116:
392
+ replacement = " ";
393
+ break;
394
+ case 102:
395
+ replacement = "\f";
396
+ break;
397
+ case 120:
398
+ if (str.startsWith("3C", index + 2)) {
399
+ replacement = "<";
400
+ length = 4;
401
+ }
402
+ break;
403
+ case 117: if (str.startsWith("2028", index + 2)) {
404
+ replacement = "\u2028";
405
+ length = 6;
406
+ } else if (str.startsWith("2029", index + 2)) {
407
+ replacement = "\u2029";
408
+ length = 6;
409
+ }
410
+ }
411
+ if (replacement) {
412
+ result += str.slice(lastPos, index) + replacement;
413
+ lastPos = index + length;
414
+ index = str.indexOf("\\", lastPos);
415
+ } else index = str.indexOf("\\", index + 1);
416
+ }
417
+ return result + str.slice(lastPos);
418
+ }
419
+ //#endregion
420
+ //#region src/core/errors.ts
421
+ function objectToString(value) {
422
+ return Object.prototype.toString.call(value);
423
+ }
424
+ function getErrorMessageDev(type, cause) {
425
+ if (cause instanceof Error) return `Seroval caught an error during the ${type} process.
426
+
427
+ ${cause.name}
428
+ ${cause.message}
429
+
430
+ - For more information, please check the "cause" property of this error.
431
+ - If you believe this is an error in Seroval, please submit an issue at https://github.com/lxsmnsyc/seroval/issues/new`;
432
+ return `Seroval caught an error during the ${type} process.
433
+
434
+ "${objectToString(cause)}"
435
+
436
+ For more information, please check the "cause" property of this error.`;
437
+ }
438
+ var SerovalError = class extends Error {
439
+ constructor(type, cause) {
440
+ super(getErrorMessageDev(type, cause));
441
+ this.cause = cause;
442
+ }
443
+ };
444
+ var SerovalParserError = class extends SerovalError {
445
+ constructor(cause) {
446
+ super("parsing", cause);
447
+ }
448
+ };
449
+ var SerovalSerializationError = class extends SerovalError {
450
+ constructor(cause) {
451
+ super("serialization", cause);
452
+ }
453
+ };
454
+ var SerovalDeserializationError = class extends SerovalError {
455
+ constructor(cause) {
456
+ super("deserialization", cause);
457
+ }
458
+ };
459
+ var SerovalUnsupportedTypeError = class extends Error {
460
+ constructor(value) {
461
+ super(`The value ${objectToString(value)} of type "${typeof value}" cannot be parsed/serialized.
462
+
463
+ There are few workarounds for this problem:
464
+ - Transform the value in a way that it can be serialized.
465
+ - If the reference is present on multiple runtimes (isomorphic), you can use the Reference API to map the references.`);
466
+ this.value = value;
467
+ }
468
+ };
469
+ var SerovalUnsupportedNodeError = class extends Error {
470
+ constructor(node) {
471
+ super("Unsupported node type \"" + node.t + "\".");
472
+ }
473
+ };
474
+ var SerovalMissingPluginError = class extends Error {
475
+ constructor(tag) {
476
+ super("Missing plugin for tag \"" + tag + "\".");
477
+ }
478
+ };
479
+ var SerovalPluginValidationError = class extends Error {
480
+ constructor(tag, field) {
481
+ super("Plugin \"" + tag + "\" received a value for \"" + field + "\" that failed its validator.");
482
+ this.tag = tag;
483
+ this.field = field;
484
+ }
485
+ };
486
+ var SerovalMissingInstanceError = class extends Error {
487
+ constructor(tag) {
488
+ super("Missing \"" + tag + "\" instance.");
489
+ }
490
+ };
491
+ /**
492
+ * @deprecated Not thrown by seroval. A missing `createReference` key throws
493
+ * {@link SerovalMissingReferenceForIdError} instead.
494
+ */
495
+ var SerovalMissingReferenceError = class extends Error {
496
+ constructor(value) {
497
+ super("Missing reference for the value \"" + objectToString(value) + "\" of type \"" + typeof value + "\"");
498
+ this.value = value;
499
+ }
500
+ };
501
+ var SerovalMissingReferenceForIdError = class extends Error {
502
+ constructor(id) {
503
+ super("Missing reference for id \"" + serializeString(id) + "\"");
504
+ }
505
+ };
506
+ var SerovalUnknownTypedArrayError = class extends Error {
507
+ constructor(name) {
508
+ super("Unknown TypedArray \"" + name + "\"");
509
+ }
510
+ };
511
+ var SerovalMalformedNodeError = class extends Error {
512
+ constructor(node) {
513
+ super("Malformed node type \"" + node.t + "\".");
514
+ }
515
+ };
516
+ var SerovalConflictedNodeIdError = class extends Error {
517
+ constructor(node) {
518
+ super("Conflicted node id \"" + node.i + "\".");
519
+ }
520
+ };
521
+ var SerovalDepthLimitError = class extends Error {
522
+ constructor(limit) {
523
+ super("Depth limit of " + limit + " reached");
524
+ }
525
+ };
526
+ var SerovalAbortedError = class extends Error {
527
+ constructor() {
528
+ super("Cross deserializer has been aborted.");
529
+ }
530
+ };
531
+ const LIVE_STREAM_ERROR_MESSAGES = {
532
+ consumed: "Live stream already has a consumer",
533
+ pending: "Live stream already has a pending event",
534
+ reading: "Live stream already has a pending read",
535
+ closed: "Live stream is closed"
536
+ };
537
+ var SerovalLiveStreamError = class extends Error {
538
+ constructor(reason) {
539
+ super(LIVE_STREAM_ERROR_MESSAGES[reason]);
540
+ this.reason = reason;
541
+ }
542
+ };
543
+ //#endregion
544
+ //#region src/core/keys.ts
545
+ const REFERENCES_KEY = "__SEROVAL_REFS__";
546
+ const GLOBAL_CONTEXT_R = `self.\$R`;
547
+ function getCrossReferenceHeader(id) {
548
+ if (id == null) return `${GLOBAL_CONTEXT_R}=${GLOBAL_CONTEXT_R}||[]`;
549
+ return `(${GLOBAL_CONTEXT_R}=${GLOBAL_CONTEXT_R}||{})["${serializeString(id)}"]=[]`;
550
+ }
551
+ //#endregion
552
+ //#region src/core/reference.ts
553
+ const REFERENCE = /* @__PURE__ */ new Map();
554
+ const INV_REFERENCE = /* @__PURE__ */ new Map();
555
+ let installed = false;
556
+ function getGlobalObject() {
557
+ if (typeof globalThis !== "undefined") return globalThis;
558
+ if (typeof window !== "undefined") return window;
559
+ if (typeof self !== "undefined") return self;
560
+ if (typeof global !== "undefined") return global;
561
+ }
562
+ function installReferenceStore() {
563
+ if (!installed) {
564
+ installed = true;
565
+ const globalObject = getGlobalObject();
566
+ if (globalObject) Object.defineProperty(globalObject, REFERENCES_KEY, {
567
+ value: INV_REFERENCE,
568
+ configurable: true,
569
+ writable: false,
570
+ enumerable: false
571
+ });
572
+ }
573
+ }
574
+ function createReference(id, value) {
575
+ installReferenceStore();
576
+ REFERENCE.set(value, id);
577
+ INV_REFERENCE.set(id, value);
578
+ return value;
579
+ }
580
+ function hasReference(id) {
581
+ return INV_REFERENCE.has(id);
582
+ }
583
+ function getReferenceID(value) {
584
+ return REFERENCE.get(value);
585
+ }
586
+ function getReference(id) {
587
+ if (hasReference(id)) return INV_REFERENCE.get(id);
588
+ throw new SerovalMissingReferenceForIdError(id);
589
+ }
590
+ //#endregion
591
+ //#region src/core/sequence.ts
592
+ /**
593
+ * An internal class rather than a tagged POJO: identity is checked with
594
+ * `instanceof`, which untrusted input cannot forge. The class is exported
595
+ * because bundlers cannot drop an unexported top-level class.
596
+ * The eval-based `deserialize` path still rebuilds a `{__SEROVAL_SEQUENCE__…}`
597
+ * POJO from embedded source - it has no access to this class - so a value read
598
+ * back through `deserialize` is not an instance and, by design, is not treated
599
+ * as a genuine Sequence on re-serialization.
600
+ */
601
+ var Sequence = class {
602
+ constructor(values, throwAt, doneAt) {
603
+ this.v = values;
604
+ this.t = throwAt;
605
+ this.d = doneAt;
606
+ }
607
+ };
608
+ function isSequence(value) {
609
+ return value instanceof Sequence;
610
+ }
611
+ function createSequence(values, throwAt, doneAt) {
612
+ return new Sequence(values, throwAt, doneAt);
613
+ }
614
+ function createSequenceFromIterable(source) {
615
+ const values = [];
616
+ let throwsAt = -1;
617
+ let doneAt = -1;
618
+ const iterator = source[SYM_ITERATOR]();
619
+ for (;;) try {
620
+ const value = iterator.next();
621
+ values.push(value.value);
622
+ if (value.done) {
623
+ doneAt = values.length - 1;
624
+ break;
625
+ }
626
+ } catch (error) {
627
+ throwsAt = values.length;
628
+ doneAt = throwsAt;
629
+ values.push(error);
630
+ break;
631
+ }
632
+ return createSequence(values, throwsAt, doneAt);
633
+ }
634
+ const createIterator = /* @__PURE__ */ ITERATOR_CONSTRUCTOR(SYM_ITERATOR);
635
+ function sequenceToIterator(sequence) {
636
+ return createIterator(sequence);
637
+ }
638
+ //#endregion
639
+ //#region src/core/live-stream.ts
640
+ const BRAND = /* @__PURE__ */ Symbol("LiveStream");
641
+ function isLiveStream(value) {
642
+ return value != null && value[BRAND] === true;
643
+ }
644
+ /**
645
+ * A single-consumer stream for incremental serialization. Unlike
646
+ * `createStream`, it keeps no history: at most one event is held, and the
647
+ * producer operation that sent it settles only when the consumer accepts it.
648
+ *
649
+ * An internal class rather than a tagged POJO: identity is checked with
650
+ * `isLiveStream`, which untrusted input cannot forge. The class is exported
651
+ * because bundlers cannot drop an unexported top-level class.
652
+ */
653
+ var LiveStream = class {
654
+ constructor(options) {
655
+ this[BRAND] = true;
656
+ this.consumed = false;
657
+ this.closed = false;
658
+ this.cancelled = false;
659
+ this.options = options;
660
+ }
661
+ deliver(reader, record) {
662
+ record.delivered = true;
663
+ reader.resolve({
664
+ event: record.event,
665
+ accept: () => {
666
+ if (this.pending === record && record.delivered) {
667
+ const terminal = record.event.type !== "next";
668
+ record.event = void 0;
669
+ this.pending = void 0;
670
+ const resolve = record.resolve;
671
+ record.resolve = void 0;
672
+ record.reject = void 0;
673
+ if (terminal) {
674
+ this.closed = true;
675
+ this.options = void 0;
676
+ const waiter = this.waiting;
677
+ this.waiting = void 0;
678
+ waiter?.reject(new SerovalLiveStreamError("closed"));
679
+ }
680
+ resolve?.();
681
+ }
682
+ }
683
+ });
684
+ }
685
+ /** @internal */
686
+ push(event) {
687
+ if (this.cancelled) return Promise.reject(this.cancelReason);
688
+ if (this.closed) return Promise.reject(new SerovalLiveStreamError("closed"));
689
+ if (this.pending) return Promise.reject(new SerovalLiveStreamError("pending"));
690
+ return new Promise((resolve, reject) => {
691
+ const record = {
692
+ event,
693
+ delivered: false,
694
+ resolve,
695
+ reject
696
+ };
697
+ this.pending = record;
698
+ if (this.waiting) {
699
+ const waiter = this.waiting;
700
+ this.waiting = void 0;
701
+ this.deliver(waiter, record);
702
+ }
703
+ });
704
+ }
705
+ cancel(reason) {
706
+ if (this.cancelled || this.closed) return;
707
+ this.cancelled = true;
708
+ this.cancelReason = reason;
709
+ const record = this.pending;
710
+ const waiter = this.waiting;
711
+ this.pending = void 0;
712
+ this.waiting = void 0;
713
+ const callbackOwner = this.options;
714
+ this.options = void 0;
715
+ if (record) {
716
+ record.event = void 0;
717
+ const reject = record.reject;
718
+ record.resolve = void 0;
719
+ record.reject = void 0;
720
+ reject?.(reason);
721
+ }
722
+ if (waiter) waiter.reject(reason);
723
+ callbackOwner?.onCancel?.(reason);
724
+ }
725
+ read() {
726
+ if (this.cancelled) return Promise.reject(this.cancelReason);
727
+ if (this.closed) return Promise.reject(new SerovalLiveStreamError("closed"));
728
+ if (this.waiting) return Promise.reject(new SerovalLiveStreamError("reading"));
729
+ return new Promise((resolve, reject) => {
730
+ const next = {
731
+ resolve,
732
+ reject
733
+ };
734
+ if (this.pending && !this.pending.delivered) this.deliver(next, this.pending);
735
+ else this.waiting = next;
736
+ });
737
+ }
738
+ consume() {
739
+ if (this.consumed) throw new SerovalLiveStreamError("consumed");
740
+ this.consumed = true;
741
+ return {
742
+ read: () => this.read(),
743
+ cancel: (reason) => this.cancel(reason)
744
+ };
745
+ }
746
+ /**
747
+ * Consumes the stream through a replay-style listener, accepting each
748
+ * event as soon as it has been delivered. Use `consume` or `pump` when
749
+ * acceptance must wait for processing.
750
+ */
751
+ on(listener) {
752
+ const wrap = (key) => (value, accept) => {
753
+ listener[key](value);
754
+ accept();
755
+ };
756
+ return this.pump({
757
+ next: wrap("next"),
758
+ throw: wrap("throw"),
759
+ return: wrap("return")
760
+ });
761
+ }
762
+ /**
763
+ * Consumes the stream into a sink that controls acceptance. Returns the
764
+ * cancel function.
765
+ * @internal
766
+ */
767
+ pump(sink) {
768
+ const consumer = this.consume();
769
+ let current = sink;
770
+ function next() {
771
+ consumer.read().then((delivery) => {
772
+ const target = current;
773
+ if (!target) return;
774
+ const event = delivery.event;
775
+ let failure;
776
+ let failed = false;
777
+ try {
778
+ failure = target[event.type](event.type === "throw" ? event.error : event.value, delivery.accept);
779
+ } catch (error) {
780
+ failed = true;
781
+ failure = error;
782
+ }
783
+ if (failed || failure !== void 0) {
784
+ current = void 0;
785
+ try {
786
+ consumer.cancel(failure);
787
+ } finally {
788
+ target.done?.();
789
+ }
790
+ return;
791
+ }
792
+ if (event.type === "next") {
793
+ next();
794
+ return;
795
+ }
796
+ current = void 0;
797
+ target.done?.();
798
+ }, (reason) => {
799
+ const target = current;
800
+ current = void 0;
801
+ target?.error?.(reason);
802
+ });
803
+ }
804
+ next();
805
+ return consumer.cancel;
806
+ }
807
+ };
808
+ /**
809
+ * Creates a live stream and the producer that writes to it.
810
+ */
811
+ function createLiveStream(options) {
812
+ const stream = new LiveStream(options);
813
+ return {
814
+ stream,
815
+ producer: {
816
+ write(value) {
817
+ return stream.push({
818
+ type: "next",
819
+ value
820
+ });
821
+ },
822
+ close(value) {
823
+ return stream.push({
824
+ type: "return",
825
+ value
826
+ });
827
+ },
828
+ fail(error) {
829
+ return stream.push({
830
+ type: "throw",
831
+ error
832
+ });
833
+ }
834
+ }
835
+ };
836
+ }
837
+ //#endregion
838
+ //#region src/core/stream.ts
839
+ /**
840
+ * An internal class rather than a tagged POJO: identity is checked with
841
+ * `instanceof`, which untrusted input cannot forge. The class is exported
842
+ * because bundlers cannot drop an unexported top-level class.
843
+ *
844
+ * The behavior is intentionally duplicated from `STREAM_CONSTRUCTOR`. That
845
+ * constructor's source is embedded verbatim into the eval-based `deserialize`
846
+ * output, which has no access to this class, so the two cannot be shared. A
847
+ * stream read back through `deserialize` is therefore a plain POJO and, by
848
+ * design, is not treated as a genuine Stream on re-serialization. Keep the two
849
+ * implementations in sync.
850
+ */
851
+ var Stream = class {
852
+ constructor(live) {
853
+ this.buffer = [];
854
+ this.listeners = [];
855
+ this.alive = true;
856
+ this.success = false;
857
+ this.count = 0;
858
+ this.live = live;
859
+ }
860
+ flush(value, mode) {
861
+ this.live > 1 || this.buffer.push(value);
862
+ for (let x = 0; x < this.count; x++) this.listeners[x]?.[mode](value);
863
+ }
864
+ replay(listener) {
865
+ for (let x = 0, z = this.buffer.length; x < z; x++) {
866
+ const current = this.buffer[x];
867
+ if (!this.alive && x === z - 1) listener[this.success ? "return" : "throw"](current);
868
+ else listener.next(current);
869
+ }
870
+ }
871
+ on(listener) {
872
+ if (this.live > 1) throw new Error("Stream consumed");
873
+ let subscribed = this.alive;
874
+ let temp = 0;
875
+ if (subscribed) {
876
+ for (; temp < this.count; temp++) if (!this.listeners[temp]) break;
877
+ if (temp === this.count) this.count++;
878
+ this.listeners[temp] = listener;
879
+ }
880
+ this.replay(listener);
881
+ if (this.live) {
882
+ this.live = 2;
883
+ this.buffer.length = 0;
884
+ }
885
+ return () => {
886
+ if (this.alive && subscribed) {
887
+ subscribed = false;
888
+ this.listeners[temp] = void 0;
889
+ while (this.count > 0 && !this.listeners[this.count - 1]) this.count--;
890
+ this.listeners.length = this.count;
891
+ }
892
+ };
893
+ }
894
+ /**
895
+ * Returns the events of a stream that has ended, without subscribing, so a
896
+ * live receiver is not consumed. The last value is the `return` or `throw`
897
+ * value. Returns `undefined` while the stream is open, or when a live
898
+ * receiver was already consumed and has no events left.
899
+ * @internal
900
+ */
901
+ endedEvents() {
902
+ if (this.alive || this.live > 1) return void 0;
903
+ return {
904
+ values: this.buffer.slice(),
905
+ success: this.success
906
+ };
907
+ }
908
+ next(value) {
909
+ if (this.alive) this.flush(value, "next");
910
+ }
911
+ throw(value) {
912
+ if (this.alive) {
913
+ this.flush(value, "throw");
914
+ this.alive = false;
915
+ this.success = false;
916
+ this.listeners.length = 0;
917
+ }
918
+ }
919
+ return(value) {
920
+ if (this.alive) {
921
+ this.flush(value, "return");
922
+ this.alive = false;
923
+ this.success = true;
924
+ this.listeners.length = 0;
925
+ }
926
+ }
927
+ };
928
+ function isStream(value) {
929
+ return value instanceof Stream || isLiveStream(value);
930
+ }
931
+ function createStream() {
932
+ return new Stream();
933
+ }
934
+ const createAsyncIterable = /* @__PURE__ */ ASYNC_ITERATOR_CONSTRUCTOR(SYM_ASYNC_ITERATOR, PROMISE_CONSTRUCTOR);
935
+ function streamToAsyncIterable(stream) {
936
+ return createAsyncIterable(stream);
937
+ }
938
+ //#endregion
939
+ //#region src/core/plugin.ts
940
+ let SerovalMode = /* @__PURE__ */ function(SerovalMode) {
941
+ SerovalMode[SerovalMode["Vanilla"] = 1] = "Vanilla";
942
+ SerovalMode[SerovalMode["Cross"] = 2] = "Cross";
943
+ return SerovalMode;
944
+ }({});
945
+ function createPlugin(plugin) {
946
+ return plugin;
947
+ }
948
+ function dedupePlugins(deduped, plugins) {
949
+ for (let i = 0, len = plugins.length; i < len; i++) {
950
+ const current = plugins[i];
951
+ if (!deduped.has(current)) {
952
+ deduped.add(current);
953
+ if (current.extends) dedupePlugins(deduped, current.extends);
954
+ }
955
+ }
956
+ }
957
+ const RESOLVED_PLUGINS = /* @__PURE__ */ new WeakMap();
958
+ function matchResolved(resolved, plugins, added) {
959
+ for (let i = 0, len = plugins.length; i < len; i++) {
960
+ const current = plugins[i];
961
+ if (current === resolved[added]) {
962
+ added++;
963
+ if (current.extends) {
964
+ added = matchResolved(resolved, current.extends, added);
965
+ if (added === -1) return -1;
966
+ }
967
+ } else {
968
+ const index = resolved.indexOf(current);
969
+ if (index === -1 || index >= added) return -1;
970
+ }
971
+ }
972
+ return added;
973
+ }
974
+ function resolvePlugins(plugins) {
975
+ if (plugins) {
976
+ const cached = RESOLVED_PLUGINS.get(plugins);
977
+ if (cached && matchResolved(cached, plugins, 0) === cached.length) return cached;
978
+ const deduped = /* @__PURE__ */ new Set();
979
+ dedupePlugins(deduped, plugins);
980
+ const resolved = [...deduped];
981
+ RESOLVED_PLUGINS.set(plugins, resolved);
982
+ return resolved;
983
+ }
984
+ }
985
+ /**
986
+ * Returns the plugins that are tested for plain objects. It is the same array
987
+ * when no plugin sets `skipPlainObjects`.
988
+ */
989
+ function getPlainObjectPlugins(plugins) {
990
+ if (plugins) {
991
+ for (let i = 0, len = plugins.length; i < len; i++) if (plugins[i].skipPlainObjects) {
992
+ const result = plugins.filter((plugin) => !plugin.skipPlainObjects);
993
+ return result.length ? result : void 0;
994
+ }
995
+ }
996
+ return plugins;
997
+ }
998
+ //#endregion
999
+ //#region src/core/opaque-reference.ts
1000
+ /**
1001
+ * An opaque reference allows hiding values from the serializer.
1002
+ */
1003
+ var OpaqueReference = class {
1004
+ constructor(value, replacement) {
1005
+ this.value = value;
1006
+ this.replacement = replacement;
1007
+ }
1008
+ };
1009
+ //#endregion
1010
+ export { SYM_ITERATOR as $, SerovalMalformedNodeError as A, SerovalUnsupportedTypeError as B, getCrossReferenceHeader as C, SerovalDeserializationError as D, SerovalDepthLimitError as E, SerovalParserError as F, ERROR_CONSTRUCTOR as G, serializeString as H, SerovalPluginValidationError as I, SYMBOL_REF as J, ERROR_CONSTRUCTOR_STRING as K, SerovalSerializationError as L, SerovalMissingPluginError as M, SerovalMissingReferenceError as N, SerovalError as O, SerovalMissingReferenceForIdError as P, SYM_IS_CONCAT_SPREADABLE as Q, SerovalUnknownTypedArrayError as R, REFERENCES_KEY as S, SerovalConflictedNodeIdError as T, CONSTANT_STRING as U, deserializeString as V, CONSTANT_VAL as W, TEMPORAL_TYPE_NAME as X, SYMBOL_STRING as Y, SYM_ASYNC_ITERATOR as Z, isSequence as _, getPlainObjectPlugins as a, SERIALIZED_ASYNC_ITERATOR_CONSTRUCTOR as at, getReference as b, createStream as c, SERIALIZED_PROMISE_FAILURE as ct, LiveStream as d, SYM_TO_STRING_TAG as et, createLiveStream as f, createSequenceFromIterable as g, createSequence as h, createPlugin as i, SERIALIZED_ARRAY_BUFFER_CONSTRUCTOR as it, SerovalMissingInstanceError as j, SerovalLiveStreamError as k, isStream as l, SERIALIZED_PROMISE_SUCCESS as lt, Sequence as m, OpaqueReference as n, ARRAY_BUFFER_CONSTRUCTOR as nt, resolvePlugins as o, SERIALIZED_ITERATOR_CONSTRUCTOR as ot, isLiveStream as p, INV_SYMBOL_REF as q, SerovalMode as r, PROMISE_CONSTRUCTOR as rt, Stream as s, SERIALIZED_PROMISE_CONSTRUCTOR as st, v as t, Feature as tt, streamToAsyncIterable as u, SERIALIZED_STREAM_CONSTRUCTOR as ut, sequenceToIterator as v, SerovalAbortedError as w, getReferenceID as x, createReference as y, SerovalUnsupportedNodeError as z };