seroval 0.4.0-alpha.8 → 0.4.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 +81 -55
- package/dist/cjs/development/index.cjs +999 -831
- package/dist/cjs/development/index.cjs.map +4 -4
- package/dist/cjs/production/index.cjs +2 -2
- package/dist/esm/development/index.mjs +999 -831
- package/dist/esm/development/index.mjs.map +4 -4
- package/dist/esm/production/index.mjs +2 -2
- package/dist/types/compat.d.ts +10 -25
- package/dist/types/context.d.ts +17 -15
- package/dist/types/index.d.ts +2 -1
- package/dist/types/tree/primitives.d.ts +17 -0
- package/dist/types/tree/serialize.d.ts +1 -1
- package/dist/types/tree/shared.d.ts +4 -8
- package/dist/types/tree/types.d.ts +108 -59
- package/dist/types/types.d.ts +3 -2
- package/package.json +2 -2
- package/src/compat.ts +10 -246
- package/src/context.ts +37 -26
- package/src/index.ts +33 -41
- package/src/quote.ts +1 -1
- package/src/tree/async.ts +362 -163
- package/src/tree/primitives.ts +150 -0
- package/src/tree/serialize.ts +308 -282
- package/src/tree/shared.ts +43 -119
- package/src/tree/sync.ts +295 -146
- package/src/tree/types.ts +143 -65
- package/src/types.ts +4 -1
- package/dist/types/checks.d.ts +0 -5
- package/src/checks.ts +0 -40
package/src/tree/shared.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import assert from '../assert';
|
|
2
|
-
import { constructorCheck } from '../checks';
|
|
3
1
|
import { Feature } from '../compat';
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
import { ParserContext } from '../context';
|
|
3
|
+
import {
|
|
4
|
+
AsyncServerValue,
|
|
5
|
+
ErrorValue,
|
|
6
|
+
} from '../types';
|
|
8
7
|
|
|
9
8
|
export function getErrorConstructor(error: ErrorValue) {
|
|
10
9
|
if (error instanceof EvalError) {
|
|
@@ -29,6 +28,7 @@ export function getErrorConstructor(error: ErrorValue) {
|
|
|
29
28
|
}
|
|
30
29
|
|
|
31
30
|
export function getErrorOptions(
|
|
31
|
+
ctx: ParserContext,
|
|
32
32
|
error: Error,
|
|
33
33
|
) {
|
|
34
34
|
let options: Record<string, any> | undefined;
|
|
@@ -44,8 +44,15 @@ export function getErrorOptions(
|
|
|
44
44
|
const names = Object.getOwnPropertyNames(error);
|
|
45
45
|
for (const name of names) {
|
|
46
46
|
if (name !== 'name' && name !== 'message') {
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
if (name === 'stack') {
|
|
48
|
+
if (ctx.features & Feature.ErrorPrototypeStack) {
|
|
49
|
+
options = options || {};
|
|
50
|
+
options[name] = error[name as keyof Error];
|
|
51
|
+
}
|
|
52
|
+
} else {
|
|
53
|
+
options = options || {};
|
|
54
|
+
options[name] = error[name as keyof Error];
|
|
55
|
+
}
|
|
49
56
|
}
|
|
50
57
|
}
|
|
51
58
|
return options;
|
|
@@ -63,115 +70,32 @@ export function getIterableOptions(obj: Iterable<any>) {
|
|
|
63
70
|
return undefined;
|
|
64
71
|
}
|
|
65
72
|
|
|
66
|
-
export function
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
ctx: ParserContext,
|
|
95
|
-
current: unknown,
|
|
96
|
-
id: number,
|
|
97
|
-
): SerovalNode | undefined {
|
|
98
|
-
if (typeof current === 'bigint') {
|
|
99
|
-
assert(ctx.features & Feature.BigInt, 'Unsupported type "BigInt"');
|
|
100
|
-
return [SerovalNodeType.BigInt, `${current}n`];
|
|
101
|
-
}
|
|
102
|
-
if (constructorCheck<Date>(current, Date)) {
|
|
103
|
-
return [SerovalNodeType.Date, current.toISOString(), id];
|
|
104
|
-
}
|
|
105
|
-
if (constructorCheck<RegExp>(current, RegExp)) {
|
|
106
|
-
return [SerovalNodeType.RegExp, String(current), id];
|
|
107
|
-
}
|
|
108
|
-
if (
|
|
109
|
-
constructorCheck<Int8Array>(current, Int8Array)
|
|
110
|
-
|| constructorCheck<Int16Array>(current, Int16Array)
|
|
111
|
-
|| constructorCheck<Int32Array>(current, Int32Array)
|
|
112
|
-
|| constructorCheck<Uint8Array>(current, Uint8Array)
|
|
113
|
-
|| constructorCheck<Uint16Array>(current, Uint16Array)
|
|
114
|
-
|| constructorCheck<Uint32Array>(current, Uint32Array)
|
|
115
|
-
|| constructorCheck<Uint8ClampedArray>(current, Uint8ClampedArray)
|
|
116
|
-
|| constructorCheck<Float32Array>(current, Float32Array)
|
|
117
|
-
|| constructorCheck<Float64Array>(current, Float64Array)
|
|
118
|
-
) {
|
|
119
|
-
const constructor = current.constructor.name;
|
|
120
|
-
assert(ctx.features & Feature.TypedArray, `Unsupported value type "${constructor}"`);
|
|
121
|
-
return [SerovalNodeType.TypedArray, [constructor, current.toString(), current.byteOffset], id];
|
|
122
|
-
}
|
|
123
|
-
if (
|
|
124
|
-
constructorCheck<BigInt64Array>(current, BigInt64Array)
|
|
125
|
-
|| constructorCheck<BigUint64Array>(current, BigUint64Array)
|
|
126
|
-
) {
|
|
127
|
-
const constructor = current.constructor.name;
|
|
128
|
-
assert(
|
|
129
|
-
ctx.features & (Feature.BigIntTypedArray),
|
|
130
|
-
`Unsupported value type "${constructor}"`,
|
|
131
|
-
);
|
|
132
|
-
let result = '';
|
|
133
|
-
const cap = current.length - 1;
|
|
134
|
-
for (let i = 0; i < cap; i++) {
|
|
135
|
-
result += `${current[i]}n,`;
|
|
136
|
-
}
|
|
137
|
-
result += `"${current[cap]}"`;
|
|
138
|
-
return [
|
|
139
|
-
SerovalNodeType.BigIntTypedArray,
|
|
140
|
-
[constructor, result, current.byteOffset],
|
|
141
|
-
id,
|
|
142
|
-
];
|
|
143
|
-
}
|
|
144
|
-
return undefined;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
export function serializePrimitive(
|
|
148
|
-
value: PrimitiveValue,
|
|
149
|
-
): string | number | null {
|
|
150
|
-
// Shortened forms
|
|
151
|
-
if (value === true) {
|
|
152
|
-
return '!0';
|
|
153
|
-
}
|
|
154
|
-
if (value === false) {
|
|
155
|
-
return '!1';
|
|
156
|
-
}
|
|
157
|
-
if (value === undefined) {
|
|
158
|
-
return 'void 0';
|
|
159
|
-
}
|
|
160
|
-
if (value === null) {
|
|
161
|
-
return null;
|
|
162
|
-
}
|
|
163
|
-
if (typeof value === 'string') {
|
|
164
|
-
return quote(value);
|
|
165
|
-
}
|
|
166
|
-
// negative 0 isn't the same as 0
|
|
167
|
-
if (Object.is(value, -0)) {
|
|
168
|
-
return '-0';
|
|
169
|
-
}
|
|
170
|
-
if (Object.is(value, Infinity)) {
|
|
171
|
-
return '1/0';
|
|
172
|
-
}
|
|
173
|
-
if (Object.is(value, -Infinity)) {
|
|
174
|
-
return '-1/0';
|
|
175
|
-
}
|
|
176
|
-
return value;
|
|
73
|
+
export function isIterable(
|
|
74
|
+
value: unknown,
|
|
75
|
+
): value is Iterable<AsyncServerValue> {
|
|
76
|
+
if (!value || typeof value !== 'object') {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
if (Array.isArray(value)) {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
switch (value.constructor) {
|
|
83
|
+
case Map:
|
|
84
|
+
case Set:
|
|
85
|
+
case Int8Array:
|
|
86
|
+
case Int16Array:
|
|
87
|
+
case Int32Array:
|
|
88
|
+
case Uint8Array:
|
|
89
|
+
case Uint16Array:
|
|
90
|
+
case Uint32Array:
|
|
91
|
+
case Uint8ClampedArray:
|
|
92
|
+
case Float32Array:
|
|
93
|
+
case Float64Array:
|
|
94
|
+
case BigInt64Array:
|
|
95
|
+
case BigUint64Array:
|
|
96
|
+
return false;
|
|
97
|
+
default:
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
return Symbol.iterator in value;
|
|
177
101
|
}
|