seroval 0.11.5 → 0.12.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.
- package/README.md +29 -16
- package/dist/cjs/development/index.cjs +3012 -4790
- package/dist/cjs/development/index.cjs.map +4 -4
- package/dist/cjs/production/index.cjs +3 -3
- package/dist/esm/development/index.mjs +3010 -4790
- package/dist/esm/development/index.mjs.map +4 -4
- package/dist/esm/production/index.mjs +3 -3
- package/dist/types/core/Serializer.d.ts +2 -0
- package/dist/types/core/Serializer.d.ts.map +1 -0
- package/dist/types/core/UnsupportedTypeError.d.ts +1 -0
- package/dist/types/core/UnsupportedTypeError.d.ts.map +1 -0
- package/dist/types/core/assert.d.ts +1 -0
- package/dist/types/core/assert.d.ts.map +1 -0
- package/dist/types/core/base/async.d.ts +30 -0
- package/dist/types/core/base/async.d.ts.map +1 -0
- package/dist/types/core/base/stream.d.ts +40 -0
- package/dist/types/core/base/stream.d.ts.map +1 -0
- package/dist/types/core/base/sync.d.ts +32 -0
- package/dist/types/core/base/sync.d.ts.map +1 -0
- package/dist/types/core/base-primitives.d.ts +3 -1
- package/dist/types/core/base-primitives.d.ts.map +1 -0
- package/dist/types/core/compat.d.ts +1 -0
- package/dist/types/core/compat.d.ts.map +1 -0
- package/dist/types/core/constants.d.ts +19 -4
- package/dist/types/core/constants.d.ts.map +1 -0
- package/dist/types/core/constructors.d.ts +1 -0
- package/dist/types/core/constructors.d.ts.map +1 -0
- package/dist/types/core/cross/async.d.ts +8 -3
- package/dist/types/core/cross/async.d.ts.map +1 -0
- package/dist/types/core/cross/cross-parser.d.ts +8 -0
- package/dist/types/core/cross/cross-parser.d.ts.map +1 -0
- package/dist/types/core/cross/index.d.ts +12 -5
- package/dist/types/core/cross/index.d.ts.map +1 -0
- package/dist/types/core/cross/serialize.d.ts +23 -5
- package/dist/types/core/cross/serialize.d.ts.map +1 -0
- package/dist/types/core/cross/stream.d.ts +8 -3
- package/dist/types/core/cross/stream.d.ts.map +1 -0
- package/dist/types/core/cross/sync.d.ts +8 -3
- package/dist/types/core/cross/sync.d.ts.map +1 -0
- package/dist/types/core/get-identifier.d.ts +1 -0
- package/dist/types/core/get-identifier.d.ts.map +1 -0
- package/dist/types/core/keys.d.ts +1 -0
- package/dist/types/core/keys.d.ts.map +1 -0
- package/dist/types/core/literals.d.ts +1 -0
- package/dist/types/core/literals.d.ts.map +1 -0
- package/dist/types/core/parser-context.d.ts +23 -0
- package/dist/types/core/parser-context.d.ts.map +1 -0
- package/dist/types/core/plugin.d.ts +26 -0
- package/dist/types/core/plugin.d.ts.map +1 -0
- package/dist/types/core/promise-to-result.d.ts +1 -0
- package/dist/types/core/promise-to-result.d.ts.map +1 -0
- package/dist/types/core/reference.d.ts +1 -0
- package/dist/types/core/reference.d.ts.map +1 -0
- package/dist/types/core/serializer-context.old.d.ts +165 -0
- package/dist/types/core/serializer-context.old.d.ts.map +1 -0
- package/dist/types/core/shared.d.ts +4 -7
- package/dist/types/core/shared.d.ts.map +1 -0
- package/dist/types/core/string.d.ts +1 -0
- package/dist/types/core/string.d.ts.map +1 -0
- package/dist/types/core/tree/async.d.ts +8 -3
- package/dist/types/core/tree/async.d.ts.map +1 -0
- package/dist/types/core/tree/deserialize.d.ts +49 -2
- package/dist/types/core/tree/deserialize.d.ts.map +1 -0
- package/dist/types/core/tree/index.d.ts +10 -7
- package/dist/types/core/tree/index.d.ts.map +1 -0
- package/dist/types/core/tree/serialize.d.ts +37 -4
- package/dist/types/core/tree/serialize.d.ts.map +1 -0
- package/dist/types/core/tree/sync.d.ts +8 -3
- package/dist/types/core/tree/sync.d.ts.map +1 -0
- package/dist/types/core/types.d.ts +10 -3
- package/dist/types/core/types.d.ts.map +1 -0
- package/dist/types/core/web-api.d.ts +1 -0
- package/dist/types/core/web-api.d.ts.map +1 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/types.d.ts +1 -0
- package/dist/types/types.d.ts.map +1 -0
- package/package.json +2 -2
- package/src/core/Serializer.ts +2 -0
- package/src/core/base/async.ts +804 -0
- package/src/core/base/stream.ts +496 -0
- package/src/core/base/sync.ts +675 -0
- package/src/core/base-primitives.ts +22 -0
- package/src/core/constants.ts +61 -45
- package/src/core/cross/async.ts +6 -743
- package/src/core/cross/cross-parser.ts +9 -0
- package/src/core/cross/index.ts +62 -84
- package/src/core/cross/serialize.ts +71 -862
- package/src/core/cross/stream.ts +6 -868
- package/src/core/cross/sync.ts +6 -623
- package/src/core/is-serializable.new.ts +208 -0
- package/src/core/keys.ts +1 -1
- package/src/core/parser-context.ts +131 -0
- package/src/core/plugin.ts +55 -0
- package/src/core/serializer-context.new.ts +1168 -0
- package/src/core/serializer-context.old.ts +1126 -0
- package/src/core/shared.ts +46 -124
- package/src/core/tree/async.ts +6 -742
- package/src/core/tree/deserialize.ts +423 -420
- package/src/core/tree/index.ts +49 -91
- package/src/core/tree/parser.ts +45 -0
- package/src/core/tree/serialize.ts +122 -828
- package/src/core/tree/sync.ts +6 -623
- package/src/core/types.ts +13 -2
- package/src/example.ts +28 -0
- package/src/index.ts +3 -0
- package/dist/types/core/assignments.d.ts +0 -33
- package/dist/types/core/context.d.ts +0 -9
- package/dist/types/core/cross/context.d.ts +0 -37
- package/dist/types/core/cross/primitives.d.ts +0 -9
- package/dist/types/core/cross/web-api.d.ts +0 -4
- package/dist/types/core/tree/context.d.ts +0 -44
- package/dist/types/core/tree/primitives.d.ts +0 -9
- package/dist/types/core/tree/web-api.d.ts +0 -4
- package/src/core/assignments.ts +0 -159
- package/src/core/context.ts +0 -15
- package/src/core/cross/context.ts +0 -102
- package/src/core/cross/primitives.ts +0 -128
- package/src/core/cross/web-api.ts +0 -50
- package/src/core/tree/context.ts +0 -125
- package/src/core/tree/primitives.ts +0 -123
- package/src/core/tree/web-api.ts +0 -50
package/src/core/shared.ts
CHANGED
|
@@ -1,143 +1,33 @@
|
|
|
1
|
-
import { BIGINT_FLAG, Feature } from './compat';
|
|
2
1
|
import type {
|
|
3
2
|
ErrorValue,
|
|
4
3
|
} from '../types';
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
4
|
+
import { Feature } from './compat';
|
|
5
|
+
import {
|
|
6
|
+
ERROR_CONSTRUCTOR_STRING,
|
|
7
|
+
ErrorConstructorTag,
|
|
8
|
+
SerovalObjectFlags,
|
|
9
|
+
} from './constants';
|
|
7
10
|
|
|
8
|
-
export function
|
|
11
|
+
export function getErrorConstructor(error: ErrorValue): ErrorConstructorTag {
|
|
9
12
|
if (error instanceof EvalError) {
|
|
10
|
-
return
|
|
13
|
+
return ErrorConstructorTag.EvalError;
|
|
11
14
|
}
|
|
12
15
|
if (error instanceof RangeError) {
|
|
13
|
-
return
|
|
16
|
+
return ErrorConstructorTag.RangeError;
|
|
14
17
|
}
|
|
15
18
|
if (error instanceof ReferenceError) {
|
|
16
|
-
return
|
|
19
|
+
return ErrorConstructorTag.ReferenceError;
|
|
17
20
|
}
|
|
18
21
|
if (error instanceof SyntaxError) {
|
|
19
|
-
return
|
|
22
|
+
return ErrorConstructorTag.SyntaxError;
|
|
20
23
|
}
|
|
21
24
|
if (error instanceof TypeError) {
|
|
22
|
-
return
|
|
25
|
+
return ErrorConstructorTag.TypeError;
|
|
23
26
|
}
|
|
24
27
|
if (error instanceof URIError) {
|
|
25
|
-
return
|
|
28
|
+
return ErrorConstructorTag.URIError;
|
|
26
29
|
}
|
|
27
|
-
return
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
type ErrorConstructors =
|
|
31
|
-
| ErrorConstructor
|
|
32
|
-
| EvalErrorConstructor
|
|
33
|
-
| RangeErrorConstructor
|
|
34
|
-
| ReferenceErrorConstructor
|
|
35
|
-
| SyntaxErrorConstructor
|
|
36
|
-
| TypeErrorConstructor
|
|
37
|
-
| URIErrorConstructor;
|
|
38
|
-
|
|
39
|
-
export function getErrorConstructor(errorName: string): ErrorConstructors {
|
|
40
|
-
switch (errorName) {
|
|
41
|
-
case 'Error': return Error;
|
|
42
|
-
case 'EvalError': return EvalError;
|
|
43
|
-
case 'RangeError': return RangeError;
|
|
44
|
-
case 'ReferenceError': return ReferenceError;
|
|
45
|
-
case 'SyntaxError': return SyntaxError;
|
|
46
|
-
case 'TypeError': return TypeError;
|
|
47
|
-
case 'URIError': return URIError;
|
|
48
|
-
default:
|
|
49
|
-
throw new Error(`Unknown Error constructor "${errorName}"`);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export function getErrorOptions(
|
|
54
|
-
ctx: BaseParserContext,
|
|
55
|
-
error: Error,
|
|
56
|
-
): Record<string, unknown> | undefined {
|
|
57
|
-
let options: Record<string, unknown> | undefined;
|
|
58
|
-
const constructor = getErrorConstructorName(error);
|
|
59
|
-
// Name has been modified
|
|
60
|
-
if (error.name !== constructor) {
|
|
61
|
-
options = { name: error.name };
|
|
62
|
-
} else if (error.constructor.name !== constructor) {
|
|
63
|
-
// Otherwise, name is overriden because
|
|
64
|
-
// the Error class is extended
|
|
65
|
-
options = { name: error.constructor.name };
|
|
66
|
-
}
|
|
67
|
-
const names = Object.getOwnPropertyNames(error);
|
|
68
|
-
for (const name of names) {
|
|
69
|
-
if (name !== 'name' && name !== 'message') {
|
|
70
|
-
if (name === 'stack') {
|
|
71
|
-
if (ctx.features & Feature.ErrorPrototypeStack) {
|
|
72
|
-
options = options || {};
|
|
73
|
-
options[name] = error[name as keyof Error];
|
|
74
|
-
}
|
|
75
|
-
} else {
|
|
76
|
-
options = options || {};
|
|
77
|
-
options[name] = error[name as keyof Error];
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
return options;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export function isIterable(
|
|
85
|
-
ctx: BaseParserContext,
|
|
86
|
-
value: unknown,
|
|
87
|
-
): value is Iterable<unknown> {
|
|
88
|
-
if (!value || typeof value !== 'object') {
|
|
89
|
-
return false;
|
|
90
|
-
}
|
|
91
|
-
if (Array.isArray(value)) {
|
|
92
|
-
return false;
|
|
93
|
-
}
|
|
94
|
-
const currentClass = value.constructor;
|
|
95
|
-
if (ctx.features & Feature.TypedArray) {
|
|
96
|
-
switch (currentClass) {
|
|
97
|
-
case Int8Array:
|
|
98
|
-
case Int16Array:
|
|
99
|
-
case Int32Array:
|
|
100
|
-
case Uint8Array:
|
|
101
|
-
case Uint16Array:
|
|
102
|
-
case Uint32Array:
|
|
103
|
-
case Uint8ClampedArray:
|
|
104
|
-
case Float32Array:
|
|
105
|
-
case Float64Array:
|
|
106
|
-
return false;
|
|
107
|
-
default:
|
|
108
|
-
break;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
// BigInt Typed Arrays
|
|
112
|
-
if ((ctx.features & BIGINT_FLAG) === BIGINT_FLAG) {
|
|
113
|
-
switch (currentClass) {
|
|
114
|
-
case BigInt64Array:
|
|
115
|
-
case BigUint64Array:
|
|
116
|
-
return false;
|
|
117
|
-
default:
|
|
118
|
-
break;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
// ES Collection
|
|
122
|
-
if (ctx.features & Feature.Map && currentClass === Map) {
|
|
123
|
-
return false;
|
|
124
|
-
}
|
|
125
|
-
if (ctx.features & Feature.Set && currentClass === Set) {
|
|
126
|
-
return false;
|
|
127
|
-
}
|
|
128
|
-
if (ctx.features & Feature.WebAPI) {
|
|
129
|
-
switch (currentClass) {
|
|
130
|
-
case Headers:
|
|
131
|
-
case File:
|
|
132
|
-
return false;
|
|
133
|
-
default:
|
|
134
|
-
break;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
if (ctx.features & Feature.Symbol) {
|
|
138
|
-
return Symbol.iterator in value;
|
|
139
|
-
}
|
|
140
|
-
return false;
|
|
30
|
+
return ErrorConstructorTag.Error;
|
|
141
31
|
}
|
|
142
32
|
|
|
143
33
|
type TypedArrayConstructor =
|
|
@@ -195,3 +85,35 @@ export function getObjectFlag(obj: unknown): SerovalObjectFlags {
|
|
|
195
85
|
}
|
|
196
86
|
return SerovalObjectFlags.NonExtensible;
|
|
197
87
|
}
|
|
88
|
+
|
|
89
|
+
export function getErrorOptions(
|
|
90
|
+
error: Error,
|
|
91
|
+
features: number,
|
|
92
|
+
): Record<string, unknown> | undefined {
|
|
93
|
+
let options: Record<string, unknown> | undefined;
|
|
94
|
+
const constructor = ERROR_CONSTRUCTOR_STRING[getErrorConstructor(error)];
|
|
95
|
+
// Name has been modified
|
|
96
|
+
if (error.name !== constructor) {
|
|
97
|
+
options = { name: error.name };
|
|
98
|
+
} else if (error.constructor.name !== constructor) {
|
|
99
|
+
// Otherwise, name is overriden because
|
|
100
|
+
// the Error class is extended
|
|
101
|
+
options = { name: error.constructor.name };
|
|
102
|
+
}
|
|
103
|
+
const names = Object.getOwnPropertyNames(error);
|
|
104
|
+
for (let i = 0, len = names.length, name: string; i < len; i++) {
|
|
105
|
+
name = names[i];
|
|
106
|
+
if (name !== 'name' && name !== 'message') {
|
|
107
|
+
if (name === 'stack') {
|
|
108
|
+
if (features & Feature.ErrorPrototypeStack) {
|
|
109
|
+
options = options || {};
|
|
110
|
+
options[name] = error[name as keyof Error];
|
|
111
|
+
}
|
|
112
|
+
} else {
|
|
113
|
+
options = options || {};
|
|
114
|
+
options[name] = error[name as keyof Error];
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return options;
|
|
119
|
+
}
|