seroval 1.0.3 → 1.0.5
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.
- package/dist/cjs/development/index.cjs +556 -451
- package/dist/cjs/development/index.cjs.map +3 -3
- package/dist/cjs/production/index.cjs +17 -3
- package/dist/esm/development/index.mjs +556 -451
- package/dist/esm/development/index.mjs.map +3 -3
- package/dist/esm/production/index.mjs +17 -3
- package/dist/types/core/Serializer.d.ts.map +1 -1
- package/dist/types/core/base-primitives.d.ts.map +1 -1
- package/dist/types/core/constants.d.ts +4 -4
- package/dist/types/core/constants.d.ts.map +1 -1
- package/dist/types/core/context/deserializer.d.ts +1 -1
- package/dist/types/core/context/deserializer.d.ts.map +1 -1
- package/dist/types/core/context/parser/async.d.ts +1 -1
- package/dist/types/core/context/parser/async.d.ts.map +1 -1
- package/dist/types/core/context/parser/stream.d.ts +2 -2
- package/dist/types/core/context/parser/stream.d.ts.map +1 -1
- package/dist/types/core/context/parser/sync.d.ts +3 -3
- package/dist/types/core/context/parser/sync.d.ts.map +1 -1
- package/dist/types/core/context/parser.d.ts.map +1 -1
- package/dist/types/core/context/serializer.d.ts +1 -1
- package/dist/types/core/context/serializer.d.ts.map +1 -1
- package/dist/types/core/cross/async.d.ts +1 -1
- package/dist/types/core/cross/async.d.ts.map +1 -1
- package/dist/types/core/cross/deserializer.d.ts +1 -1
- package/dist/types/core/cross/deserializer.d.ts.map +1 -1
- package/dist/types/core/cross/serializer.d.ts +1 -1
- package/dist/types/core/cross/serializer.d.ts.map +1 -1
- package/dist/types/core/errors.d.ts +38 -0
- package/dist/types/core/errors.d.ts.map +1 -0
- package/dist/types/core/plugin.d.ts +1 -1
- package/dist/types/core/plugin.d.ts.map +1 -1
- package/dist/types/core/reference.d.ts.map +1 -1
- package/dist/types/core/tree/async.d.ts +1 -1
- package/dist/types/core/tree/async.d.ts.map +1 -1
- package/dist/types/core/tree/deserializer.d.ts +1 -1
- package/dist/types/core/tree/deserializer.d.ts.map +1 -1
- package/dist/types/core/tree/index.d.ts.map +1 -1
- package/dist/types/core/tree/serializer.d.ts +1 -1
- package/dist/types/core/tree/serializer.d.ts.map +1 -1
- package/dist/types/core/types.d.ts +1 -1
- package/dist/types/core/types.d.ts.map +1 -1
- package/dist/types/core/utils/typed-array.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +4 -5
- package/src/core/Serializer.ts +5 -5
- package/src/core/base-primitives.ts +4 -9
- package/src/core/constants.ts +12 -12
- package/src/core/context/deserializer.ts +100 -90
- package/src/core/context/parser/async.ts +56 -52
- package/src/core/context/parser/stream.ts +5 -5
- package/src/core/context/parser/sync.ts +45 -41
- package/src/core/context/parser.ts +5 -12
- package/src/core/context/serializer.ts +127 -118
- package/src/core/cross/async.ts +1 -1
- package/src/core/cross/deserializer.ts +1 -1
- package/src/core/cross/serializer.ts +2 -2
- package/src/core/errors.ts +102 -0
- package/src/core/literals.ts +4 -4
- package/src/core/plugin.ts +1 -1
- package/src/core/reference.ts +7 -3
- package/src/core/tree/async.ts +1 -1
- package/src/core/tree/deserializer.ts +1 -1
- package/src/core/tree/index.ts +8 -2
- package/src/core/tree/serializer.ts +8 -7
- package/src/core/types.ts +2 -2
- package/src/core/utils/typed-array.ts +3 -1
- package/src/index.ts +1 -0
- package/dist/types/core/UnsupportedTypeError.d.ts +0 -5
- package/dist/types/core/UnsupportedTypeError.d.ts.map +0 -1
- package/src/core/UnsupportedTypeError.ts +0 -7
|
@@ -9,13 +9,6 @@ var Feature = /* @__PURE__ */ ((Feature2) => {
|
|
|
9
9
|
})(Feature || {});
|
|
10
10
|
var ALL_ENABLED = 31;
|
|
11
11
|
|
|
12
|
-
// src/core/utils/assert.ts
|
|
13
|
-
function assert(cond, error) {
|
|
14
|
-
if (!cond) {
|
|
15
|
-
throw error;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
12
|
// src/core/string.ts
|
|
20
13
|
function serializeChar(str) {
|
|
21
14
|
switch (str) {
|
|
@@ -107,6 +100,13 @@ function getCrossReferenceHeader(id) {
|
|
|
107
100
|
)}"]=[]`;
|
|
108
101
|
}
|
|
109
102
|
|
|
103
|
+
// src/core/utils/assert.ts
|
|
104
|
+
function assert(cond, error) {
|
|
105
|
+
if (!cond) {
|
|
106
|
+
throw error;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
110
|
// src/core/reference.ts
|
|
111
111
|
var REFERENCE = /* @__PURE__ */ new Map();
|
|
112
112
|
var INV_REFERENCE = /* @__PURE__ */ new Map();
|
|
@@ -122,11 +122,11 @@ function hasReference(id) {
|
|
|
122
122
|
return INV_REFERENCE.has(id);
|
|
123
123
|
}
|
|
124
124
|
function getReferenceID(value) {
|
|
125
|
-
assert(hasReferenceID(value), new
|
|
125
|
+
assert(hasReferenceID(value), new SerovalMissingReferenceError(value));
|
|
126
126
|
return REFERENCE.get(value);
|
|
127
127
|
}
|
|
128
128
|
function getReference(id) {
|
|
129
|
-
assert(hasReference(id), new
|
|
129
|
+
assert(hasReference(id), new SerovalMissingReferenceForIdError(id));
|
|
130
130
|
return INV_REFERENCE.get(id);
|
|
131
131
|
}
|
|
132
132
|
if (typeof globalThis !== "undefined") {
|
|
@@ -183,15 +183,6 @@ function resolvePlugins(plugins) {
|
|
|
183
183
|
return void 0;
|
|
184
184
|
}
|
|
185
185
|
|
|
186
|
-
// src/core/UnsupportedTypeError.ts
|
|
187
|
-
var { toString: objectToString } = Object.prototype;
|
|
188
|
-
var UnsupportedTypeError = class extends Error {
|
|
189
|
-
constructor(value) {
|
|
190
|
-
super('Unsupported type "' + objectToString.call(value) + '"');
|
|
191
|
-
this.value = value;
|
|
192
|
-
}
|
|
193
|
-
};
|
|
194
|
-
|
|
195
186
|
// src/core/constants.ts
|
|
196
187
|
var SYMBOL_STRING = {
|
|
197
188
|
[0 /* AsyncIterator */]: "Symbol.asyncIterator",
|
|
@@ -243,20 +234,20 @@ var CONSTANT_STRING = {
|
|
|
243
234
|
[3 /* False */]: "!1",
|
|
244
235
|
[1 /* Undefined */]: "void 0",
|
|
245
236
|
[0 /* Null */]: "null",
|
|
246
|
-
[4 /*
|
|
247
|
-
[5 /*
|
|
248
|
-
[6 /*
|
|
249
|
-
[7 /*
|
|
237
|
+
[4 /* NegZero */]: "-0",
|
|
238
|
+
[5 /* Inf */]: "1/0",
|
|
239
|
+
[6 /* NegInf */]: "-1/0",
|
|
240
|
+
[7 /* Nan */]: "0/0"
|
|
250
241
|
};
|
|
251
242
|
var CONSTANT_VAL = {
|
|
252
243
|
[2 /* True */]: true,
|
|
253
244
|
[3 /* False */]: false,
|
|
254
245
|
[1 /* Undefined */]: void 0,
|
|
255
246
|
[0 /* Null */]: null,
|
|
256
|
-
[4 /*
|
|
257
|
-
[5 /*
|
|
258
|
-
[6 /*
|
|
259
|
-
[7 /*
|
|
247
|
+
[4 /* NegZero */]: -0,
|
|
248
|
+
[5 /* Inf */]: Number.POSITIVE_INFINITY,
|
|
249
|
+
[6 /* NegInf */]: Number.NEGATIVE_INFINITY,
|
|
250
|
+
[7 /* Nan */]: Number.NaN
|
|
260
251
|
};
|
|
261
252
|
var ERROR_CONSTRUCTOR_STRING = {
|
|
262
253
|
[0 /* Error */]: "Error",
|
|
@@ -307,15 +298,15 @@ var NULL_NODE = /* @__PURE__ */ createConstantNode(
|
|
|
307
298
|
0 /* Null */
|
|
308
299
|
);
|
|
309
300
|
var NEG_ZERO_NODE = /* @__PURE__ */ createConstantNode(
|
|
310
|
-
4 /*
|
|
301
|
+
4 /* NegZero */
|
|
311
302
|
);
|
|
312
303
|
var INFINITY_NODE = /* @__PURE__ */ createConstantNode(
|
|
313
|
-
5 /*
|
|
304
|
+
5 /* Inf */
|
|
314
305
|
);
|
|
315
306
|
var NEG_INFINITY_NODE = /* @__PURE__ */ createConstantNode(
|
|
316
|
-
6 /*
|
|
307
|
+
6 /* NegInf */
|
|
317
308
|
);
|
|
318
|
-
var NAN_NODE = /* @__PURE__ */ createConstantNode(7 /*
|
|
309
|
+
var NAN_NODE = /* @__PURE__ */ createConstantNode(7 /* Nan */);
|
|
319
310
|
|
|
320
311
|
// src/core/utils/error.ts
|
|
321
312
|
function getErrorConstructor(error) {
|
|
@@ -386,9 +377,9 @@ function getObjectFlag(obj) {
|
|
|
386
377
|
// src/core/base-primitives.ts
|
|
387
378
|
function createNumberNode(value) {
|
|
388
379
|
switch (value) {
|
|
389
|
-
case
|
|
380
|
+
case Number.POSITIVE_INFINITY:
|
|
390
381
|
return INFINITY_NODE;
|
|
391
|
-
case
|
|
382
|
+
case Number.NEGATIVE_INFINITY:
|
|
392
383
|
return NEG_INFINITY_NODE;
|
|
393
384
|
}
|
|
394
385
|
if (value !== value) {
|
|
@@ -515,10 +506,6 @@ function createArrayBufferNode(id, current) {
|
|
|
515
506
|
};
|
|
516
507
|
}
|
|
517
508
|
function createWKSymbolNode(id, current) {
|
|
518
|
-
assert(
|
|
519
|
-
current in INV_SYMBOL_REF,
|
|
520
|
-
new Error("Only well-known symbols are supported.")
|
|
521
|
-
);
|
|
522
509
|
return {
|
|
523
510
|
t: 17 /* WKSymbol */,
|
|
524
511
|
i: id,
|
|
@@ -791,6 +778,288 @@ function createStreamReturnNode(id, parsed) {
|
|
|
791
778
|
};
|
|
792
779
|
}
|
|
793
780
|
|
|
781
|
+
// src/core/errors.ts
|
|
782
|
+
var { toString: objectToString } = Object.prototype;
|
|
783
|
+
function getErrorMessage(type, cause) {
|
|
784
|
+
if (cause instanceof Error) {
|
|
785
|
+
return `Seroval caught an error during the ${type} process.
|
|
786
|
+
|
|
787
|
+
${cause.name}
|
|
788
|
+
${cause.message}
|
|
789
|
+
|
|
790
|
+
- For more information, please check the "cause" property of this error.
|
|
791
|
+
- If you believe this is an error in Seroval, please submit an issue at https://github.com/lxsmnsyc/seroval/issues/new`;
|
|
792
|
+
}
|
|
793
|
+
return `Seroval caught an error during the ${type} process.
|
|
794
|
+
|
|
795
|
+
"${objectToString.call(cause)}"
|
|
796
|
+
|
|
797
|
+
For more information, please check the "cause" property of this error.`;
|
|
798
|
+
}
|
|
799
|
+
var SerovalError = class extends Error {
|
|
800
|
+
constructor(type, cause) {
|
|
801
|
+
super(getErrorMessage(type, cause));
|
|
802
|
+
this.cause = cause;
|
|
803
|
+
}
|
|
804
|
+
};
|
|
805
|
+
var SerovalParserError = class extends SerovalError {
|
|
806
|
+
constructor(cause) {
|
|
807
|
+
super("parsing", cause);
|
|
808
|
+
}
|
|
809
|
+
};
|
|
810
|
+
var SerovalSerializationError = class extends SerovalError {
|
|
811
|
+
constructor(cause) {
|
|
812
|
+
super("serialization", cause);
|
|
813
|
+
}
|
|
814
|
+
};
|
|
815
|
+
var SerovalDeserializationError = class extends SerovalError {
|
|
816
|
+
constructor(cause) {
|
|
817
|
+
super("deserialization", cause);
|
|
818
|
+
}
|
|
819
|
+
};
|
|
820
|
+
var SerovalUnsupportedTypeError = class extends Error {
|
|
821
|
+
constructor(value) {
|
|
822
|
+
super(
|
|
823
|
+
`The value ${objectToString.call(value)} of type "${typeof value}" cannot be parsed/serialized.
|
|
824
|
+
|
|
825
|
+
There are few workarounds for this problem:
|
|
826
|
+
- Transform the value in a way that it can be serialized.
|
|
827
|
+
- If the reference is present on multiple runtimes (isomorphic), you can use the Reference API to map the references.`
|
|
828
|
+
);
|
|
829
|
+
this.value = value;
|
|
830
|
+
}
|
|
831
|
+
};
|
|
832
|
+
var SerovalUnsupportedNodeError = class extends Error {
|
|
833
|
+
constructor(node) {
|
|
834
|
+
super('Unsupported node type "' + node.t + '".');
|
|
835
|
+
}
|
|
836
|
+
};
|
|
837
|
+
var SerovalMissingPluginError = class extends Error {
|
|
838
|
+
constructor(tag) {
|
|
839
|
+
super('Missing plugin for tag "' + tag + '".');
|
|
840
|
+
}
|
|
841
|
+
};
|
|
842
|
+
var SerovalMissingInstanceError = class extends Error {
|
|
843
|
+
constructor(tag) {
|
|
844
|
+
super('Missing "' + tag + '" instance.');
|
|
845
|
+
}
|
|
846
|
+
};
|
|
847
|
+
var SerovalMissingReferenceError = class extends Error {
|
|
848
|
+
constructor(value) {
|
|
849
|
+
super(
|
|
850
|
+
'Missing reference for the value "' + objectToString.call(value) + '" of type "' + typeof value + '"'
|
|
851
|
+
);
|
|
852
|
+
this.value = value;
|
|
853
|
+
}
|
|
854
|
+
};
|
|
855
|
+
var SerovalMissingReferenceForIdError = class extends Error {
|
|
856
|
+
constructor(id) {
|
|
857
|
+
super('Missing reference for id "' + serializeString(id) + '"');
|
|
858
|
+
}
|
|
859
|
+
};
|
|
860
|
+
var SerovalUnknownTypedArrayError = class extends Error {
|
|
861
|
+
constructor(name) {
|
|
862
|
+
super('Unknown TypedArray "' + name + '"');
|
|
863
|
+
}
|
|
864
|
+
};
|
|
865
|
+
|
|
866
|
+
// src/core/special-reference.ts
|
|
867
|
+
var ITERATOR = {};
|
|
868
|
+
var ASYNC_ITERATOR = {};
|
|
869
|
+
var SPECIAL_REFS = {
|
|
870
|
+
[0 /* MapSentinel */]: {},
|
|
871
|
+
[1 /* PromiseConstructor */]: {},
|
|
872
|
+
[2 /* PromiseResolve */]: {},
|
|
873
|
+
[3 /* PromiseReject */]: {},
|
|
874
|
+
[4 /* StreamConstructor */]: {}
|
|
875
|
+
};
|
|
876
|
+
|
|
877
|
+
// src/core/utils/deferred.ts
|
|
878
|
+
function createDeferred() {
|
|
879
|
+
let resolve;
|
|
880
|
+
let reject;
|
|
881
|
+
return {
|
|
882
|
+
promise: new Promise((res, rej) => {
|
|
883
|
+
resolve = res;
|
|
884
|
+
reject = rej;
|
|
885
|
+
}),
|
|
886
|
+
resolve(value) {
|
|
887
|
+
resolve(value);
|
|
888
|
+
},
|
|
889
|
+
reject(value) {
|
|
890
|
+
reject(value);
|
|
891
|
+
}
|
|
892
|
+
};
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
// src/core/stream.ts
|
|
896
|
+
function isStream(value) {
|
|
897
|
+
return "__SEROVAL_STREAM__" in value;
|
|
898
|
+
}
|
|
899
|
+
function createStream() {
|
|
900
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
901
|
+
const buffer = [];
|
|
902
|
+
let alive = true;
|
|
903
|
+
let success = false;
|
|
904
|
+
function flushNext(value) {
|
|
905
|
+
for (const listener of listeners.keys()) {
|
|
906
|
+
listener.next(value);
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
function flushThrow(value) {
|
|
910
|
+
for (const listener of listeners.keys()) {
|
|
911
|
+
listener.throw(value);
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
function flushReturn(value) {
|
|
915
|
+
for (const listener of listeners.keys()) {
|
|
916
|
+
listener.return(value);
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
return {
|
|
920
|
+
__SEROVAL_STREAM__: true,
|
|
921
|
+
on(listener) {
|
|
922
|
+
if (alive) {
|
|
923
|
+
listeners.add(listener);
|
|
924
|
+
}
|
|
925
|
+
for (let i = 0, len = buffer.length; i < len; i++) {
|
|
926
|
+
const value = buffer[i];
|
|
927
|
+
if (i === len - 1) {
|
|
928
|
+
if (success) {
|
|
929
|
+
listener.return(value);
|
|
930
|
+
} else {
|
|
931
|
+
listener.throw(value);
|
|
932
|
+
}
|
|
933
|
+
} else {
|
|
934
|
+
listener.next(value);
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
return () => {
|
|
938
|
+
if (alive) {
|
|
939
|
+
listeners.delete(listener);
|
|
940
|
+
}
|
|
941
|
+
};
|
|
942
|
+
},
|
|
943
|
+
next(value) {
|
|
944
|
+
if (alive) {
|
|
945
|
+
buffer.push(value);
|
|
946
|
+
flushNext(value);
|
|
947
|
+
}
|
|
948
|
+
},
|
|
949
|
+
throw(value) {
|
|
950
|
+
if (alive) {
|
|
951
|
+
buffer.push(value);
|
|
952
|
+
flushThrow(value);
|
|
953
|
+
alive = false;
|
|
954
|
+
success = false;
|
|
955
|
+
listeners.clear();
|
|
956
|
+
}
|
|
957
|
+
},
|
|
958
|
+
return(value) {
|
|
959
|
+
if (alive) {
|
|
960
|
+
buffer.push(value);
|
|
961
|
+
flushReturn(value);
|
|
962
|
+
alive = false;
|
|
963
|
+
success = true;
|
|
964
|
+
listeners.clear();
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
};
|
|
968
|
+
}
|
|
969
|
+
function createStreamFromAsyncIterable(iterable) {
|
|
970
|
+
const stream = createStream();
|
|
971
|
+
const iterator = iterable[Symbol.asyncIterator]();
|
|
972
|
+
async function push() {
|
|
973
|
+
try {
|
|
974
|
+
const value = await iterator.next();
|
|
975
|
+
if (value.done) {
|
|
976
|
+
stream.return(value.value);
|
|
977
|
+
} else {
|
|
978
|
+
stream.next(value.value);
|
|
979
|
+
await push();
|
|
980
|
+
}
|
|
981
|
+
} catch (error) {
|
|
982
|
+
stream.throw(error);
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
push().catch(() => {
|
|
986
|
+
});
|
|
987
|
+
return stream;
|
|
988
|
+
}
|
|
989
|
+
function streamToAsyncIterable(stream) {
|
|
990
|
+
return () => {
|
|
991
|
+
const buffer = [];
|
|
992
|
+
const pending = [];
|
|
993
|
+
let count = 0;
|
|
994
|
+
let doneAt = -1;
|
|
995
|
+
let isThrow = false;
|
|
996
|
+
function resolveAll() {
|
|
997
|
+
for (let i = 0, len = pending.length; i < len; i++) {
|
|
998
|
+
pending[i].resolve({ done: true, value: void 0 });
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
stream.on({
|
|
1002
|
+
next(value) {
|
|
1003
|
+
const current = pending.shift();
|
|
1004
|
+
if (current) {
|
|
1005
|
+
current.resolve({ done: false, value });
|
|
1006
|
+
}
|
|
1007
|
+
buffer.push(value);
|
|
1008
|
+
},
|
|
1009
|
+
throw(value) {
|
|
1010
|
+
const current = pending.shift();
|
|
1011
|
+
if (current) {
|
|
1012
|
+
current.reject(value);
|
|
1013
|
+
}
|
|
1014
|
+
resolveAll();
|
|
1015
|
+
doneAt = buffer.length;
|
|
1016
|
+
buffer.push(value);
|
|
1017
|
+
isThrow = true;
|
|
1018
|
+
},
|
|
1019
|
+
return(value) {
|
|
1020
|
+
const current = pending.shift();
|
|
1021
|
+
if (current) {
|
|
1022
|
+
current.resolve({ done: true, value });
|
|
1023
|
+
}
|
|
1024
|
+
resolveAll();
|
|
1025
|
+
doneAt = buffer.length;
|
|
1026
|
+
buffer.push(value);
|
|
1027
|
+
}
|
|
1028
|
+
});
|
|
1029
|
+
function finalize() {
|
|
1030
|
+
const current = count++;
|
|
1031
|
+
const value = buffer[current];
|
|
1032
|
+
if (current !== doneAt) {
|
|
1033
|
+
return { done: false, value };
|
|
1034
|
+
}
|
|
1035
|
+
if (isThrow) {
|
|
1036
|
+
throw value;
|
|
1037
|
+
}
|
|
1038
|
+
return { done: true, value };
|
|
1039
|
+
}
|
|
1040
|
+
return {
|
|
1041
|
+
[Symbol.asyncIterator]() {
|
|
1042
|
+
return this;
|
|
1043
|
+
},
|
|
1044
|
+
async next() {
|
|
1045
|
+
if (doneAt === -1) {
|
|
1046
|
+
const current = count++;
|
|
1047
|
+
if (current >= buffer.length) {
|
|
1048
|
+
const deferred = createDeferred();
|
|
1049
|
+
pending.push(deferred);
|
|
1050
|
+
return await deferred.promise;
|
|
1051
|
+
}
|
|
1052
|
+
return { done: false, value: buffer[current] };
|
|
1053
|
+
}
|
|
1054
|
+
if (count > doneAt) {
|
|
1055
|
+
return { done: true, value: void 0 };
|
|
1056
|
+
}
|
|
1057
|
+
return finalize();
|
|
1058
|
+
}
|
|
1059
|
+
};
|
|
1060
|
+
};
|
|
1061
|
+
}
|
|
1062
|
+
|
|
794
1063
|
// src/core/utils/iterator-to-sequence.ts
|
|
795
1064
|
function iteratorToSequence(source) {
|
|
796
1065
|
const values = [];
|
|
@@ -844,16 +1113,14 @@ function sequenceToIterator(sequence) {
|
|
|
844
1113
|
};
|
|
845
1114
|
}
|
|
846
1115
|
|
|
847
|
-
// src/core/
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
[4 /* StreamConstructor */]: {}
|
|
856
|
-
};
|
|
1116
|
+
// src/core/utils/promise-to-result.ts
|
|
1117
|
+
async function promiseToResult(current) {
|
|
1118
|
+
try {
|
|
1119
|
+
return [1, await current];
|
|
1120
|
+
} catch (e) {
|
|
1121
|
+
return [0, e];
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
857
1124
|
|
|
858
1125
|
// src/core/context/parser.ts
|
|
859
1126
|
var BaseParserContext = class {
|
|
@@ -899,12 +1166,7 @@ var BaseParserContext = class {
|
|
|
899
1166
|
return indexed;
|
|
900
1167
|
}
|
|
901
1168
|
getStrictReference(current) {
|
|
902
|
-
assert(
|
|
903
|
-
hasReferenceID(current),
|
|
904
|
-
new Error(
|
|
905
|
-
"Cannot serialize " + typeof current + " without reference ID."
|
|
906
|
-
)
|
|
907
|
-
);
|
|
1169
|
+
assert(hasReferenceID(current), new SerovalUnsupportedTypeError(current));
|
|
908
1170
|
const result = this.getIndexedValue(current);
|
|
909
1171
|
if (result.type === 1 /* Indexed */) {
|
|
910
1172
|
return result.value;
|
|
@@ -919,10 +1181,7 @@ var BaseParserContext = class {
|
|
|
919
1181
|
if (ref.type !== 0 /* Fresh */) {
|
|
920
1182
|
return ref.value;
|
|
921
1183
|
}
|
|
922
|
-
assert(
|
|
923
|
-
current in INV_SYMBOL_REF,
|
|
924
|
-
new Error("Cannot serialized unsupported symbol.")
|
|
925
|
-
);
|
|
1184
|
+
assert(current in INV_SYMBOL_REF, new SerovalUnsupportedTypeError(current));
|
|
926
1185
|
return createWKSymbolNode(ref.value, current);
|
|
927
1186
|
}
|
|
928
1187
|
parseSpecialReference(ref) {
|
|
@@ -1038,201 +1297,6 @@ var BaseParserContext = class {
|
|
|
1038
1297
|
}
|
|
1039
1298
|
};
|
|
1040
1299
|
|
|
1041
|
-
// src/core/utils/promise-to-result.ts
|
|
1042
|
-
async function promiseToResult(current) {
|
|
1043
|
-
try {
|
|
1044
|
-
return [1, await current];
|
|
1045
|
-
} catch (e) {
|
|
1046
|
-
return [0, e];
|
|
1047
|
-
}
|
|
1048
|
-
}
|
|
1049
|
-
|
|
1050
|
-
// src/core/utils/deferred.ts
|
|
1051
|
-
function createDeferred() {
|
|
1052
|
-
let resolve;
|
|
1053
|
-
let reject;
|
|
1054
|
-
return {
|
|
1055
|
-
promise: new Promise((res, rej) => {
|
|
1056
|
-
resolve = res;
|
|
1057
|
-
reject = rej;
|
|
1058
|
-
}),
|
|
1059
|
-
resolve(value) {
|
|
1060
|
-
resolve(value);
|
|
1061
|
-
},
|
|
1062
|
-
reject(value) {
|
|
1063
|
-
reject(value);
|
|
1064
|
-
}
|
|
1065
|
-
};
|
|
1066
|
-
}
|
|
1067
|
-
|
|
1068
|
-
// src/core/stream.ts
|
|
1069
|
-
function isStream(value) {
|
|
1070
|
-
return "__SEROVAL_STREAM__" in value;
|
|
1071
|
-
}
|
|
1072
|
-
function createStream() {
|
|
1073
|
-
const listeners = /* @__PURE__ */ new Set();
|
|
1074
|
-
const buffer = [];
|
|
1075
|
-
let alive = true;
|
|
1076
|
-
let success = false;
|
|
1077
|
-
function flushNext(value) {
|
|
1078
|
-
for (const listener of listeners.keys()) {
|
|
1079
|
-
listener.next(value);
|
|
1080
|
-
}
|
|
1081
|
-
}
|
|
1082
|
-
function flushThrow(value) {
|
|
1083
|
-
for (const listener of listeners.keys()) {
|
|
1084
|
-
listener.throw(value);
|
|
1085
|
-
}
|
|
1086
|
-
}
|
|
1087
|
-
function flushReturn(value) {
|
|
1088
|
-
for (const listener of listeners.keys()) {
|
|
1089
|
-
listener.return(value);
|
|
1090
|
-
}
|
|
1091
|
-
}
|
|
1092
|
-
return {
|
|
1093
|
-
__SEROVAL_STREAM__: true,
|
|
1094
|
-
on(listener) {
|
|
1095
|
-
if (alive) {
|
|
1096
|
-
listeners.add(listener);
|
|
1097
|
-
}
|
|
1098
|
-
for (let i = 0, len = buffer.length; i < len; i++) {
|
|
1099
|
-
const value = buffer[i];
|
|
1100
|
-
if (i === len - 1) {
|
|
1101
|
-
if (success) {
|
|
1102
|
-
listener.return(value);
|
|
1103
|
-
} else {
|
|
1104
|
-
listener.throw(value);
|
|
1105
|
-
}
|
|
1106
|
-
} else {
|
|
1107
|
-
listener.next(value);
|
|
1108
|
-
}
|
|
1109
|
-
}
|
|
1110
|
-
return () => {
|
|
1111
|
-
if (alive) {
|
|
1112
|
-
listeners.delete(listener);
|
|
1113
|
-
}
|
|
1114
|
-
};
|
|
1115
|
-
},
|
|
1116
|
-
next(value) {
|
|
1117
|
-
if (alive) {
|
|
1118
|
-
buffer.push(value);
|
|
1119
|
-
flushNext(value);
|
|
1120
|
-
}
|
|
1121
|
-
},
|
|
1122
|
-
throw(value) {
|
|
1123
|
-
if (alive) {
|
|
1124
|
-
buffer.push(value);
|
|
1125
|
-
flushThrow(value);
|
|
1126
|
-
alive = false;
|
|
1127
|
-
success = false;
|
|
1128
|
-
listeners.clear();
|
|
1129
|
-
}
|
|
1130
|
-
},
|
|
1131
|
-
return(value) {
|
|
1132
|
-
if (alive) {
|
|
1133
|
-
buffer.push(value);
|
|
1134
|
-
flushReturn(value);
|
|
1135
|
-
alive = false;
|
|
1136
|
-
success = true;
|
|
1137
|
-
listeners.clear();
|
|
1138
|
-
}
|
|
1139
|
-
}
|
|
1140
|
-
};
|
|
1141
|
-
}
|
|
1142
|
-
function createStreamFromAsyncIterable(iterable) {
|
|
1143
|
-
const stream = createStream();
|
|
1144
|
-
const iterator = iterable[Symbol.asyncIterator]();
|
|
1145
|
-
async function push() {
|
|
1146
|
-
try {
|
|
1147
|
-
const value = await iterator.next();
|
|
1148
|
-
if (value.done) {
|
|
1149
|
-
stream.return(value.value);
|
|
1150
|
-
} else {
|
|
1151
|
-
stream.next(value.value);
|
|
1152
|
-
await push();
|
|
1153
|
-
}
|
|
1154
|
-
} catch (error) {
|
|
1155
|
-
stream.throw(error);
|
|
1156
|
-
}
|
|
1157
|
-
}
|
|
1158
|
-
push().catch(() => {
|
|
1159
|
-
});
|
|
1160
|
-
return stream;
|
|
1161
|
-
}
|
|
1162
|
-
function streamToAsyncIterable(stream) {
|
|
1163
|
-
return () => {
|
|
1164
|
-
const buffer = [];
|
|
1165
|
-
const pending = [];
|
|
1166
|
-
let count = 0;
|
|
1167
|
-
let doneAt = -1;
|
|
1168
|
-
let isThrow = false;
|
|
1169
|
-
function resolveAll() {
|
|
1170
|
-
for (let i = 0, len = pending.length; i < len; i++) {
|
|
1171
|
-
pending[i].resolve({ done: true, value: void 0 });
|
|
1172
|
-
}
|
|
1173
|
-
}
|
|
1174
|
-
stream.on({
|
|
1175
|
-
next(value) {
|
|
1176
|
-
const current = pending.shift();
|
|
1177
|
-
if (current) {
|
|
1178
|
-
current.resolve({ done: false, value });
|
|
1179
|
-
}
|
|
1180
|
-
buffer.push(value);
|
|
1181
|
-
},
|
|
1182
|
-
throw(value) {
|
|
1183
|
-
const current = pending.shift();
|
|
1184
|
-
if (current) {
|
|
1185
|
-
current.reject(value);
|
|
1186
|
-
}
|
|
1187
|
-
resolveAll();
|
|
1188
|
-
doneAt = buffer.length;
|
|
1189
|
-
buffer.push(value);
|
|
1190
|
-
isThrow = true;
|
|
1191
|
-
},
|
|
1192
|
-
return(value) {
|
|
1193
|
-
const current = pending.shift();
|
|
1194
|
-
if (current) {
|
|
1195
|
-
current.resolve({ done: true, value });
|
|
1196
|
-
}
|
|
1197
|
-
resolveAll();
|
|
1198
|
-
doneAt = buffer.length;
|
|
1199
|
-
buffer.push(value);
|
|
1200
|
-
}
|
|
1201
|
-
});
|
|
1202
|
-
function finalize() {
|
|
1203
|
-
const current = count++;
|
|
1204
|
-
const value = buffer[current];
|
|
1205
|
-
if (current !== doneAt) {
|
|
1206
|
-
return { done: false, value };
|
|
1207
|
-
}
|
|
1208
|
-
if (isThrow) {
|
|
1209
|
-
throw value;
|
|
1210
|
-
}
|
|
1211
|
-
return { done: true, value };
|
|
1212
|
-
}
|
|
1213
|
-
return {
|
|
1214
|
-
[Symbol.asyncIterator]() {
|
|
1215
|
-
return this;
|
|
1216
|
-
},
|
|
1217
|
-
async next() {
|
|
1218
|
-
if (doneAt === -1) {
|
|
1219
|
-
const current = count++;
|
|
1220
|
-
if (current >= buffer.length) {
|
|
1221
|
-
const deferred = createDeferred();
|
|
1222
|
-
pending.push(deferred);
|
|
1223
|
-
return await deferred.promise;
|
|
1224
|
-
}
|
|
1225
|
-
return { done: false, value: buffer[current] };
|
|
1226
|
-
}
|
|
1227
|
-
if (count > doneAt) {
|
|
1228
|
-
return { done: true, value: void 0 };
|
|
1229
|
-
}
|
|
1230
|
-
return finalize();
|
|
1231
|
-
}
|
|
1232
|
-
};
|
|
1233
|
-
};
|
|
1234
|
-
}
|
|
1235
|
-
|
|
1236
1300
|
// src/core/context/parser/async.ts
|
|
1237
1301
|
var BaseAsyncParserContext = class extends BaseParserContext {
|
|
1238
1302
|
async parseItems(current) {
|
|
@@ -1527,33 +1591,37 @@ var BaseAsyncParserContext = class extends BaseParserContext {
|
|
|
1527
1591
|
if (Symbol.iterator in current || Symbol.asyncIterator in current) {
|
|
1528
1592
|
return this.parsePlainObject(id, current, !!currentClass);
|
|
1529
1593
|
}
|
|
1530
|
-
throw new
|
|
1594
|
+
throw new SerovalUnsupportedTypeError(current);
|
|
1531
1595
|
}
|
|
1532
1596
|
async parse(current) {
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1597
|
+
try {
|
|
1598
|
+
switch (typeof current) {
|
|
1599
|
+
case "boolean":
|
|
1600
|
+
return current ? TRUE_NODE : FALSE_NODE;
|
|
1601
|
+
case "undefined":
|
|
1602
|
+
return UNDEFINED_NODE;
|
|
1603
|
+
case "string":
|
|
1604
|
+
return createStringNode(current);
|
|
1605
|
+
case "number":
|
|
1606
|
+
return createNumberNode(current);
|
|
1607
|
+
case "bigint":
|
|
1608
|
+
return createBigIntNode(current);
|
|
1609
|
+
case "object": {
|
|
1610
|
+
if (current) {
|
|
1611
|
+
const ref = this.getReference(current);
|
|
1612
|
+
return ref.type === 0 ? await this.parseObject(ref.value, current) : ref.value;
|
|
1613
|
+
}
|
|
1614
|
+
return NULL_NODE;
|
|
1548
1615
|
}
|
|
1549
|
-
|
|
1616
|
+
case "symbol":
|
|
1617
|
+
return this.parseWellKnownSymbol(current);
|
|
1618
|
+
case "function":
|
|
1619
|
+
return this.parseFunction(current);
|
|
1620
|
+
default:
|
|
1621
|
+
throw new SerovalUnsupportedTypeError(current);
|
|
1550
1622
|
}
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
case "function":
|
|
1554
|
-
return this.parseFunction(current);
|
|
1555
|
-
default:
|
|
1556
|
-
throw new UnsupportedTypeError(current);
|
|
1623
|
+
} catch (error) {
|
|
1624
|
+
throw new SerovalParserError(error);
|
|
1557
1625
|
}
|
|
1558
1626
|
}
|
|
1559
1627
|
};
|
|
@@ -1592,7 +1660,7 @@ function getTypedArrayConstructor(name) {
|
|
|
1592
1660
|
case "BigUint64Array":
|
|
1593
1661
|
return BigUint64Array;
|
|
1594
1662
|
default:
|
|
1595
|
-
throw new
|
|
1663
|
+
throw new SerovalUnknownTypedArrayError(name);
|
|
1596
1664
|
}
|
|
1597
1665
|
}
|
|
1598
1666
|
|
|
@@ -1761,20 +1829,20 @@ var BaseDeserializerContext = class {
|
|
|
1761
1829
|
}
|
|
1762
1830
|
}
|
|
1763
1831
|
}
|
|
1764
|
-
throw new
|
|
1832
|
+
throw new SerovalMissingPluginError(node.c);
|
|
1765
1833
|
}
|
|
1766
1834
|
deserializePromiseConstructor(node) {
|
|
1767
1835
|
return this.assignIndexedValue(node.i, createDeferred()).promise;
|
|
1768
1836
|
}
|
|
1769
1837
|
deserializePromiseResolve(node) {
|
|
1770
1838
|
const deferred = this.refs.get(node.i);
|
|
1771
|
-
assert(deferred, new
|
|
1839
|
+
assert(deferred, new SerovalMissingInstanceError("Promise"));
|
|
1772
1840
|
deferred.resolve(this.deserialize(node.a[1]));
|
|
1773
1841
|
return void 0;
|
|
1774
1842
|
}
|
|
1775
1843
|
deserializePromiseReject(node) {
|
|
1776
1844
|
const deferred = this.refs.get(node.i);
|
|
1777
|
-
assert(deferred, new
|
|
1845
|
+
assert(deferred, new SerovalMissingInstanceError("Promise"));
|
|
1778
1846
|
deferred.reject(this.deserialize(node.a[1]));
|
|
1779
1847
|
return void 0;
|
|
1780
1848
|
}
|
|
@@ -1800,19 +1868,19 @@ var BaseDeserializerContext = class {
|
|
|
1800
1868
|
}
|
|
1801
1869
|
deserializeStreamNext(node) {
|
|
1802
1870
|
const deferred = this.refs.get(node.i);
|
|
1803
|
-
assert(deferred, new
|
|
1871
|
+
assert(deferred, new SerovalMissingInstanceError("Stream"));
|
|
1804
1872
|
deferred.next(this.deserialize(node.f));
|
|
1805
1873
|
return void 0;
|
|
1806
1874
|
}
|
|
1807
1875
|
deserializeStreamThrow(node) {
|
|
1808
1876
|
const deferred = this.refs.get(node.i);
|
|
1809
|
-
assert(deferred, new
|
|
1877
|
+
assert(deferred, new SerovalMissingInstanceError("Stream"));
|
|
1810
1878
|
deferred.throw(this.deserialize(node.f));
|
|
1811
1879
|
return void 0;
|
|
1812
1880
|
}
|
|
1813
1881
|
deserializeStreamReturn(node) {
|
|
1814
1882
|
const deferred = this.refs.get(node.i);
|
|
1815
|
-
assert(deferred, new
|
|
1883
|
+
assert(deferred, new SerovalMissingInstanceError("Stream"));
|
|
1816
1884
|
deferred.return(this.deserialize(node.f));
|
|
1817
1885
|
return void 0;
|
|
1818
1886
|
}
|
|
@@ -1825,75 +1893,79 @@ var BaseDeserializerContext = class {
|
|
|
1825
1893
|
return void 0;
|
|
1826
1894
|
}
|
|
1827
1895
|
deserialize(node) {
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1896
|
+
try {
|
|
1897
|
+
switch (node.t) {
|
|
1898
|
+
case 2 /* Constant */:
|
|
1899
|
+
return CONSTANT_VAL[node.s];
|
|
1900
|
+
case 0 /* Number */:
|
|
1901
|
+
return node.s;
|
|
1902
|
+
case 1 /* String */:
|
|
1903
|
+
return deserializeString(node.s);
|
|
1904
|
+
case 3 /* BigInt */:
|
|
1905
|
+
return BigInt(node.s);
|
|
1906
|
+
case 4 /* IndexedValue */:
|
|
1907
|
+
return this.refs.get(node.i);
|
|
1908
|
+
case 18 /* Reference */:
|
|
1909
|
+
return this.deserializeReference(node);
|
|
1910
|
+
case 9 /* Array */:
|
|
1911
|
+
return this.deserializeArray(node);
|
|
1912
|
+
case 10 /* Object */:
|
|
1913
|
+
case 11 /* NullConstructor */:
|
|
1914
|
+
return this.deserializeObject(node);
|
|
1915
|
+
case 5 /* Date */:
|
|
1916
|
+
return this.deserializeDate(node);
|
|
1917
|
+
case 6 /* RegExp */:
|
|
1918
|
+
return this.deserializeRegExp(node);
|
|
1919
|
+
case 7 /* Set */:
|
|
1920
|
+
return this.deserializeSet(node);
|
|
1921
|
+
case 8 /* Map */:
|
|
1922
|
+
return this.deserializeMap(node);
|
|
1923
|
+
case 19 /* ArrayBuffer */:
|
|
1924
|
+
return this.deserializeArrayBuffer(node);
|
|
1925
|
+
case 16 /* BigIntTypedArray */:
|
|
1926
|
+
case 15 /* TypedArray */:
|
|
1927
|
+
return this.deserializeTypedArray(node);
|
|
1928
|
+
case 20 /* DataView */:
|
|
1929
|
+
return this.deserializeDataView(node);
|
|
1930
|
+
case 14 /* AggregateError */:
|
|
1931
|
+
return this.deserializeAggregateError(node);
|
|
1932
|
+
case 13 /* Error */:
|
|
1933
|
+
return this.deserializeError(node);
|
|
1934
|
+
case 12 /* Promise */:
|
|
1935
|
+
return this.deserializePromise(node);
|
|
1936
|
+
case 17 /* WKSymbol */:
|
|
1937
|
+
return SYMBOL_REF[node.s];
|
|
1938
|
+
case 21 /* Boxed */:
|
|
1939
|
+
return this.deserializeBoxed(node);
|
|
1940
|
+
case 25 /* Plugin */:
|
|
1941
|
+
return this.deserializePlugin(node);
|
|
1942
|
+
case 22 /* PromiseConstructor */:
|
|
1943
|
+
return this.deserializePromiseConstructor(node);
|
|
1944
|
+
case 23 /* PromiseResolve */:
|
|
1945
|
+
return this.deserializePromiseResolve(node);
|
|
1946
|
+
case 24 /* PromiseReject */:
|
|
1947
|
+
return this.deserializePromiseReject(node);
|
|
1948
|
+
case 28 /* IteratorFactoryInstance */:
|
|
1949
|
+
return this.deserializeIteratorFactoryInstance(node);
|
|
1950
|
+
case 30 /* AsyncIteratorFactoryInstance */:
|
|
1951
|
+
return this.deserializeAsyncIteratorFactoryInstance(node);
|
|
1952
|
+
case 31 /* StreamConstructor */:
|
|
1953
|
+
return this.deserializeStreamConstructor(node);
|
|
1954
|
+
case 32 /* StreamNext */:
|
|
1955
|
+
return this.deserializeStreamNext(node);
|
|
1956
|
+
case 33 /* StreamThrow */:
|
|
1957
|
+
return this.deserializeStreamThrow(node);
|
|
1958
|
+
case 34 /* StreamReturn */:
|
|
1959
|
+
return this.deserializeStreamReturn(node);
|
|
1960
|
+
case 27 /* IteratorFactory */:
|
|
1961
|
+
return this.deserializeIteratorFactory(node);
|
|
1962
|
+
case 29 /* AsyncIteratorFactory */:
|
|
1963
|
+
return this.deserializeAsyncIteratorFactory(node);
|
|
1964
|
+
default:
|
|
1965
|
+
throw new SerovalUnsupportedNodeError(node);
|
|
1966
|
+
}
|
|
1967
|
+
} catch (error) {
|
|
1968
|
+
throw new SerovalDeserializationError(error);
|
|
1897
1969
|
}
|
|
1898
1970
|
}
|
|
1899
1971
|
};
|
|
@@ -2167,7 +2239,14 @@ var BaseSerializerContext = class {
|
|
|
2167
2239
|
const isIdentifier = (
|
|
2168
2240
|
// Test if key is a valid positive number or JS identifier
|
|
2169
2241
|
// so that we don't have to serialize the key and wrap with brackets
|
|
2170
|
-
check >= 0 &&
|
|
2242
|
+
check >= 0 && // It's also important to consider that if the key is
|
|
2243
|
+
// indeed numeric, we need to make sure that when
|
|
2244
|
+
// converted back into a string, it's still the same
|
|
2245
|
+
// to the original key. This allows us to differentiate
|
|
2246
|
+
// keys that has numeric formats but in a different
|
|
2247
|
+
// format, which can cause unintentional key declaration
|
|
2248
|
+
// Example: { 0x1: 1 } vs { '0x1': 1 }
|
|
2249
|
+
check.toString() === key || isValidIdentifier(key)
|
|
2171
2250
|
);
|
|
2172
2251
|
if (this.isIndexedValueInStack(val)) {
|
|
2173
2252
|
const refParam = this.getRefParam(val.i);
|
|
@@ -2223,7 +2302,14 @@ var BaseSerializerContext = class {
|
|
|
2223
2302
|
const isIdentifier = (
|
|
2224
2303
|
// Test if key is a valid positive number or JS identifier
|
|
2225
2304
|
// so that we don't have to serialize the key and wrap with brackets
|
|
2226
|
-
check >= 0 &&
|
|
2305
|
+
check >= 0 && // It's also important to consider that if the key is
|
|
2306
|
+
// indeed numeric, we need to make sure that when
|
|
2307
|
+
// converted back into a string, it's still the same
|
|
2308
|
+
// to the original key. This allows us to differentiate
|
|
2309
|
+
// keys that has numeric formats but in a different
|
|
2310
|
+
// format, which can cause unintentional key declaration
|
|
2311
|
+
// Example: { 0x1: 1 } vs { '0x1': 1 }
|
|
2312
|
+
check.toString() === key || isValidIdentifier(key)
|
|
2227
2313
|
);
|
|
2228
2314
|
if (this.isIndexedValueInStack(value)) {
|
|
2229
2315
|
if (isIdentifier && check !== check) {
|
|
@@ -2478,7 +2564,7 @@ var BaseSerializerContext = class {
|
|
|
2478
2564
|
}
|
|
2479
2565
|
}
|
|
2480
2566
|
}
|
|
2481
|
-
throw new
|
|
2567
|
+
throw new SerovalMissingPluginError(node.c);
|
|
2482
2568
|
}
|
|
2483
2569
|
getConstructor(node) {
|
|
2484
2570
|
const current = this.serialize(node);
|
|
@@ -2644,78 +2730,82 @@ var BaseSerializerContext = class {
|
|
|
2644
2730
|
return this.getRefParam(node.i) + ".return(" + this.serialize(node.f) + ")";
|
|
2645
2731
|
}
|
|
2646
2732
|
serialize(node) {
|
|
2647
|
-
|
|
2648
|
-
|
|
2649
|
-
|
|
2650
|
-
|
|
2651
|
-
|
|
2652
|
-
|
|
2653
|
-
|
|
2654
|
-
|
|
2655
|
-
|
|
2656
|
-
|
|
2657
|
-
|
|
2658
|
-
|
|
2659
|
-
|
|
2660
|
-
|
|
2661
|
-
|
|
2662
|
-
|
|
2663
|
-
|
|
2664
|
-
|
|
2665
|
-
|
|
2666
|
-
|
|
2667
|
-
|
|
2668
|
-
|
|
2669
|
-
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
|
|
2683
|
-
|
|
2684
|
-
|
|
2685
|
-
|
|
2686
|
-
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
|
|
2696
|
-
|
|
2697
|
-
|
|
2698
|
-
|
|
2699
|
-
|
|
2700
|
-
|
|
2701
|
-
|
|
2702
|
-
|
|
2703
|
-
|
|
2704
|
-
|
|
2705
|
-
|
|
2706
|
-
|
|
2707
|
-
|
|
2708
|
-
|
|
2709
|
-
|
|
2710
|
-
|
|
2711
|
-
|
|
2712
|
-
|
|
2713
|
-
|
|
2714
|
-
|
|
2715
|
-
|
|
2716
|
-
|
|
2717
|
-
|
|
2718
|
-
|
|
2733
|
+
try {
|
|
2734
|
+
switch (node.t) {
|
|
2735
|
+
case 2 /* Constant */:
|
|
2736
|
+
return CONSTANT_STRING[node.s];
|
|
2737
|
+
case 0 /* Number */:
|
|
2738
|
+
return "" + node.s;
|
|
2739
|
+
case 1 /* String */:
|
|
2740
|
+
return '"' + node.s + '"';
|
|
2741
|
+
case 3 /* BigInt */:
|
|
2742
|
+
return node.s + "n";
|
|
2743
|
+
case 4 /* IndexedValue */:
|
|
2744
|
+
return this.getRefParam(node.i);
|
|
2745
|
+
case 18 /* Reference */:
|
|
2746
|
+
return this.serializeReference(node);
|
|
2747
|
+
case 9 /* Array */:
|
|
2748
|
+
return this.serializeArray(node);
|
|
2749
|
+
case 10 /* Object */:
|
|
2750
|
+
return this.serializeObject(node);
|
|
2751
|
+
case 11 /* NullConstructor */:
|
|
2752
|
+
return this.serializeNullConstructor(node);
|
|
2753
|
+
case 5 /* Date */:
|
|
2754
|
+
return this.serializeDate(node);
|
|
2755
|
+
case 6 /* RegExp */:
|
|
2756
|
+
return this.serializeRegExp(node);
|
|
2757
|
+
case 7 /* Set */:
|
|
2758
|
+
return this.serializeSet(node);
|
|
2759
|
+
case 8 /* Map */:
|
|
2760
|
+
return this.serializeMap(node);
|
|
2761
|
+
case 19 /* ArrayBuffer */:
|
|
2762
|
+
return this.serializeArrayBuffer(node);
|
|
2763
|
+
case 16 /* BigIntTypedArray */:
|
|
2764
|
+
case 15 /* TypedArray */:
|
|
2765
|
+
return this.serializeTypedArray(node);
|
|
2766
|
+
case 20 /* DataView */:
|
|
2767
|
+
return this.serializeDataView(node);
|
|
2768
|
+
case 14 /* AggregateError */:
|
|
2769
|
+
return this.serializeAggregateError(node);
|
|
2770
|
+
case 13 /* Error */:
|
|
2771
|
+
return this.serializeError(node);
|
|
2772
|
+
case 12 /* Promise */:
|
|
2773
|
+
return this.serializePromise(node);
|
|
2774
|
+
case 17 /* WKSymbol */:
|
|
2775
|
+
return this.serializeWellKnownSymbol(node);
|
|
2776
|
+
case 21 /* Boxed */:
|
|
2777
|
+
return this.serializeBoxed(node);
|
|
2778
|
+
case 22 /* PromiseConstructor */:
|
|
2779
|
+
return this.serializePromiseConstructor(node);
|
|
2780
|
+
case 23 /* PromiseResolve */:
|
|
2781
|
+
return this.serializePromiseResolve(node);
|
|
2782
|
+
case 24 /* PromiseReject */:
|
|
2783
|
+
return this.serializePromiseReject(node);
|
|
2784
|
+
case 25 /* Plugin */:
|
|
2785
|
+
return this.serializePlugin(node);
|
|
2786
|
+
case 26 /* SpecialReference */:
|
|
2787
|
+
return this.serializeSpecialReference(node);
|
|
2788
|
+
case 27 /* IteratorFactory */:
|
|
2789
|
+
return this.serializeIteratorFactory(node);
|
|
2790
|
+
case 28 /* IteratorFactoryInstance */:
|
|
2791
|
+
return this.serializeIteratorFactoryInstance(node);
|
|
2792
|
+
case 29 /* AsyncIteratorFactory */:
|
|
2793
|
+
return this.serializeAsyncIteratorFactory(node);
|
|
2794
|
+
case 30 /* AsyncIteratorFactoryInstance */:
|
|
2795
|
+
return this.serializeAsyncIteratorFactoryInstance(node);
|
|
2796
|
+
case 31 /* StreamConstructor */:
|
|
2797
|
+
return this.serializeStreamConstructor(node);
|
|
2798
|
+
case 32 /* StreamNext */:
|
|
2799
|
+
return this.serializeStreamNext(node);
|
|
2800
|
+
case 33 /* StreamThrow */:
|
|
2801
|
+
return this.serializeStreamThrow(node);
|
|
2802
|
+
case 34 /* StreamReturn */:
|
|
2803
|
+
return this.serializeStreamReturn(node);
|
|
2804
|
+
default:
|
|
2805
|
+
throw new SerovalUnsupportedNodeError(node);
|
|
2806
|
+
}
|
|
2807
|
+
} catch (error) {
|
|
2808
|
+
throw new SerovalSerializationError(error);
|
|
2719
2809
|
}
|
|
2720
2810
|
}
|
|
2721
2811
|
};
|
|
@@ -2777,13 +2867,13 @@ var VanillaSerializerContext = class extends BaseSerializerContext {
|
|
|
2777
2867
|
return value;
|
|
2778
2868
|
}
|
|
2779
2869
|
serializePromiseConstructor(node) {
|
|
2780
|
-
throw new
|
|
2870
|
+
throw new SerovalUnsupportedNodeError(node);
|
|
2781
2871
|
}
|
|
2782
2872
|
serializePromiseResolve(node) {
|
|
2783
|
-
throw new
|
|
2873
|
+
throw new SerovalUnsupportedNodeError(node);
|
|
2784
2874
|
}
|
|
2785
2875
|
serializePromiseReject(node) {
|
|
2786
|
-
throw new
|
|
2876
|
+
throw new SerovalUnsupportedNodeError(node);
|
|
2787
2877
|
}
|
|
2788
2878
|
serializeTop(tree) {
|
|
2789
2879
|
const result = this.serialize(tree);
|
|
@@ -3034,33 +3124,37 @@ var BaseSyncParserContext = class extends BaseParserContext {
|
|
|
3034
3124
|
if (Symbol.iterator in current || Symbol.asyncIterator in current) {
|
|
3035
3125
|
return this.parsePlainObject(id, current, !!currentClass);
|
|
3036
3126
|
}
|
|
3037
|
-
throw new
|
|
3127
|
+
throw new SerovalUnsupportedTypeError(current);
|
|
3038
3128
|
}
|
|
3039
3129
|
parse(current) {
|
|
3040
|
-
|
|
3041
|
-
|
|
3042
|
-
|
|
3043
|
-
|
|
3044
|
-
|
|
3045
|
-
|
|
3046
|
-
|
|
3047
|
-
|
|
3048
|
-
|
|
3049
|
-
|
|
3050
|
-
|
|
3051
|
-
|
|
3052
|
-
|
|
3053
|
-
|
|
3054
|
-
|
|
3130
|
+
try {
|
|
3131
|
+
switch (typeof current) {
|
|
3132
|
+
case "boolean":
|
|
3133
|
+
return current ? TRUE_NODE : FALSE_NODE;
|
|
3134
|
+
case "undefined":
|
|
3135
|
+
return UNDEFINED_NODE;
|
|
3136
|
+
case "string":
|
|
3137
|
+
return createStringNode(current);
|
|
3138
|
+
case "number":
|
|
3139
|
+
return createNumberNode(current);
|
|
3140
|
+
case "bigint":
|
|
3141
|
+
return createBigIntNode(current);
|
|
3142
|
+
case "object": {
|
|
3143
|
+
if (current) {
|
|
3144
|
+
const ref = this.getReference(current);
|
|
3145
|
+
return ref.type === 0 ? this.parseObject(ref.value, current) : ref.value;
|
|
3146
|
+
}
|
|
3147
|
+
return NULL_NODE;
|
|
3055
3148
|
}
|
|
3056
|
-
|
|
3149
|
+
case "symbol":
|
|
3150
|
+
return this.parseWellKnownSymbol(current);
|
|
3151
|
+
case "function":
|
|
3152
|
+
return this.parseFunction(current);
|
|
3153
|
+
default:
|
|
3154
|
+
throw new SerovalUnsupportedTypeError(current);
|
|
3057
3155
|
}
|
|
3058
|
-
|
|
3059
|
-
|
|
3060
|
-
case "function":
|
|
3061
|
-
return this.parseFunction(current);
|
|
3062
|
-
default:
|
|
3063
|
-
throw new UnsupportedTypeError(current);
|
|
3156
|
+
} catch (error) {
|
|
3157
|
+
throw new SerovalParserError(error);
|
|
3064
3158
|
}
|
|
3065
3159
|
}
|
|
3066
3160
|
};
|
|
@@ -3652,6 +3746,17 @@ var Serializer = class {
|
|
|
3652
3746
|
export {
|
|
3653
3747
|
Feature,
|
|
3654
3748
|
Serializer,
|
|
3749
|
+
SerovalDeserializationError,
|
|
3750
|
+
SerovalError,
|
|
3751
|
+
SerovalMissingInstanceError,
|
|
3752
|
+
SerovalMissingPluginError,
|
|
3753
|
+
SerovalMissingReferenceError,
|
|
3754
|
+
SerovalMissingReferenceForIdError,
|
|
3755
|
+
SerovalParserError,
|
|
3756
|
+
SerovalSerializationError,
|
|
3757
|
+
SerovalUnknownTypedArrayError,
|
|
3758
|
+
SerovalUnsupportedNodeError,
|
|
3759
|
+
SerovalUnsupportedTypeError,
|
|
3655
3760
|
compileJSON,
|
|
3656
3761
|
createPlugin,
|
|
3657
3762
|
createReference,
|