seroval 0.15.0 → 1.0.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 +3 -3
- package/dist/cjs/development/index.cjs +1278 -1929
- package/dist/cjs/development/index.cjs.map +3 -3
- package/dist/cjs/production/index.cjs +3 -3
- package/dist/esm/development/index.mjs +1278 -1929
- package/dist/esm/development/index.mjs.map +3 -3
- package/dist/esm/production/index.mjs +3 -3
- package/dist/types/core/Serializer.d.ts +2 -1
- package/dist/types/core/Serializer.d.ts.map +1 -1
- package/dist/types/core/UnsupportedTypeError.d.ts.map +1 -1
- package/dist/types/core/base-primitives.d.ts +6 -2
- package/dist/types/core/base-primitives.d.ts.map +1 -1
- package/dist/types/core/compat.d.ts +5 -13
- package/dist/types/core/compat.d.ts.map +1 -1
- package/dist/types/core/constants.d.ts +17 -29
- package/dist/types/core/constants.d.ts.map +1 -1
- package/dist/types/core/context/deserializer.d.ts +6 -16
- package/dist/types/core/context/deserializer.d.ts.map +1 -1
- package/dist/types/core/context/parser/async.d.ts +2 -11
- package/dist/types/core/context/parser/async.d.ts.map +1 -1
- package/dist/types/core/context/parser/stream.d.ts +10 -10
- package/dist/types/core/context/parser/stream.d.ts.map +1 -1
- package/dist/types/core/context/parser/sync.d.ts +5 -7
- package/dist/types/core/context/parser/sync.d.ts.map +1 -1
- package/dist/types/core/context/parser.d.ts +19 -4
- package/dist/types/core/context/parser.d.ts.map +1 -1
- package/dist/types/core/context/serializer.d.ts +11 -25
- package/dist/types/core/context/serializer.d.ts.map +1 -1
- package/dist/types/core/cross/async.d.ts.map +1 -1
- package/dist/types/core/cross/deserializer.d.ts.map +1 -1
- package/dist/types/core/cross/index.d.ts.map +1 -1
- package/dist/types/core/cross/parser.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/cross/stream.d.ts.map +1 -1
- package/dist/types/core/cross/sync.d.ts.map +1 -1
- package/dist/types/core/keys.d.ts.map +1 -1
- package/dist/types/core/literals.d.ts.map +1 -1
- package/dist/types/core/plugin.d.ts +21 -0
- package/dist/types/core/plugin.d.ts.map +1 -1
- package/dist/types/core/reference.d.ts.map +1 -1
- package/dist/types/core/special-reference.d.ts +4 -9
- package/dist/types/core/special-reference.d.ts.map +1 -1
- package/dist/types/core/stream.d.ts +18 -0
- package/dist/types/core/stream.d.ts.map +1 -0
- package/dist/types/core/string.d.ts.map +1 -1
- package/dist/types/core/tree/async.d.ts.map +1 -1
- package/dist/types/core/tree/deserializer.d.ts.map +1 -1
- package/dist/types/core/tree/index.d.ts +1 -1
- package/dist/types/core/tree/index.d.ts.map +1 -1
- package/dist/types/core/tree/serializer.d.ts +3 -11
- package/dist/types/core/tree/serializer.d.ts.map +1 -1
- package/dist/types/core/types.d.ts +28 -122
- package/dist/types/core/types.d.ts.map +1 -1
- package/dist/types/core/utils/deferred.d.ts +0 -7
- package/dist/types/core/utils/deferred.d.ts.map +1 -1
- package/dist/types/core/utils/error.d.ts +2 -1
- package/dist/types/core/utils/error.d.ts.map +1 -1
- package/dist/types/core/utils/get-identifier.d.ts.map +1 -1
- package/dist/types/core/utils/is-valid-identifier.d.ts.map +1 -1
- package/dist/types/core/utils/iterator-to-sequence.d.ts +0 -12
- package/dist/types/core/utils/iterator-to-sequence.d.ts.map +1 -1
- package/dist/types/core/utils/typed-array.d.ts +2 -0
- package/dist/types/core/utils/typed-array.d.ts.map +1 -1
- package/dist/types/index.d.ts +3 -3
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +7 -7
- package/src/core/Serializer.ts +44 -28
- package/src/core/UnsupportedTypeError.ts +2 -3
- package/src/core/base-primitives.ts +124 -38
- package/src/core/compat.ts +5 -14
- package/src/core/constants.ts +28 -39
- package/src/core/context/deserializer.ts +122 -308
- package/src/core/context/parser/async.ts +163 -375
- package/src/core/context/parser/stream.ts +109 -367
- package/src/core/context/parser/sync.ts +139 -266
- package/src/core/context/parser.ts +93 -68
- package/src/core/context/serializer.ts +370 -439
- package/src/core/cross/async.ts +1 -1
- package/src/core/cross/deserializer.ts +1 -4
- package/src/core/cross/index.ts +28 -31
- package/src/core/cross/parser.ts +1 -1
- package/src/core/cross/serializer.ts +30 -20
- package/src/core/cross/stream.ts +1 -1
- package/src/core/cross/sync.ts +1 -1
- package/src/core/keys.ts +3 -1
- package/src/core/literals.ts +22 -8
- package/src/core/plugin.ts +48 -10
- package/src/core/reference.ts +4 -5
- package/src/core/special-reference.ts +5 -15
- package/src/core/stream.ts +205 -0
- package/src/core/string.ts +53 -22
- package/src/core/tree/async.ts +1 -1
- package/src/core/tree/deserializer.ts +4 -9
- package/src/core/tree/index.ts +15 -10
- package/src/core/tree/serializer.ts +6 -50
- package/src/core/types.ts +39 -179
- package/src/core/utils/deferred.ts +0 -27
- package/src/core/utils/error.ts +26 -15
- package/src/core/utils/get-identifier.ts +5 -4
- package/src/core/utils/is-valid-identifier.ts +6 -5
- package/src/core/utils/iterator-to-sequence.ts +0 -174
- package/src/core/utils/typed-array.ts +35 -11
- package/src/index.ts +5 -12
- package/dist/types/core/utils/constructors.d.ts +0 -5
- package/dist/types/core/utils/constructors.d.ts.map +0 -1
- package/dist/types/core/web-api.d.ts +0 -8
- package/dist/types/core/web-api.d.ts.map +0 -1
- package/dist/types/types.d.ts +0 -21
- package/dist/types/types.d.ts.map +0 -1
- package/src/core/utils/constructors.ts +0 -49
- package/src/core/web-api.ts +0 -138
- package/src/example.ts +0 -25
- package/src/types.ts +0 -84
|
@@ -1,16 +1,12 @@
|
|
|
1
|
-
import UnsupportedTypeError from '../UnsupportedTypeError';
|
|
2
1
|
import assert from '../utils/assert';
|
|
3
2
|
import {
|
|
4
3
|
createIndexedValueNode,
|
|
5
4
|
createReferenceNode,
|
|
6
5
|
createWKSymbolNode,
|
|
7
6
|
} from '../base-primitives';
|
|
8
|
-
import { ALL_ENABLED
|
|
7
|
+
import { ALL_ENABLED } from '../compat';
|
|
9
8
|
import type { WellKnownSymbols } from '../constants';
|
|
10
|
-
import {
|
|
11
|
-
INV_SYMBOL_REF,
|
|
12
|
-
SerovalNodeType,
|
|
13
|
-
} from '../constants';
|
|
9
|
+
import { INV_SYMBOL_REF, SerovalNodeType } from '../constants';
|
|
14
10
|
import type { Plugin, PluginAccessOptions, SerovalMode } from '../plugin';
|
|
15
11
|
import { hasReferenceID } from '../reference';
|
|
16
12
|
import {
|
|
@@ -39,6 +35,23 @@ export interface BaseParserContextOptions extends PluginAccessOptions {
|
|
|
39
35
|
refs?: Map<unknown, number>;
|
|
40
36
|
}
|
|
41
37
|
|
|
38
|
+
interface FreshNode {
|
|
39
|
+
type: 0;
|
|
40
|
+
value: number;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface IndexedNode {
|
|
44
|
+
type: 1;
|
|
45
|
+
value: SerovalIndexedValueNode;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
interface ReferencedNode {
|
|
49
|
+
type: 2;
|
|
50
|
+
value: SerovalReferenceNode;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
type ObjectNode = FreshNode | IndexedNode | ReferencedNode;
|
|
54
|
+
|
|
42
55
|
export abstract class BaseParserContext implements PluginAccessOptions {
|
|
43
56
|
abstract readonly mode: SerovalMode;
|
|
44
57
|
|
|
@@ -64,70 +77,83 @@ export abstract class BaseParserContext implements PluginAccessOptions {
|
|
|
64
77
|
return this.marked.has(id);
|
|
65
78
|
}
|
|
66
79
|
|
|
67
|
-
protected
|
|
68
|
-
const
|
|
69
|
-
if (
|
|
70
|
-
this.markRef(
|
|
71
|
-
return
|
|
80
|
+
protected getIndexedValue<T>(current: T): FreshNode | IndexedNode {
|
|
81
|
+
const registeredId = this.refs.get(current);
|
|
82
|
+
if (registeredId != null) {
|
|
83
|
+
this.markRef(registeredId);
|
|
84
|
+
return {
|
|
85
|
+
type: 1,
|
|
86
|
+
value: createIndexedValueNode(registeredId),
|
|
87
|
+
};
|
|
72
88
|
}
|
|
73
89
|
const id = this.refs.size;
|
|
74
90
|
this.refs.set(current, id);
|
|
91
|
+
return {
|
|
92
|
+
type: 0,
|
|
93
|
+
value: id,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
protected getReference<T>(current: T): ObjectNode {
|
|
98
|
+
const indexed = this.getIndexedValue(current);
|
|
99
|
+
if (indexed.type === 1) {
|
|
100
|
+
return indexed;
|
|
101
|
+
}
|
|
75
102
|
if (hasReferenceID(current)) {
|
|
76
|
-
return
|
|
103
|
+
return {
|
|
104
|
+
type: 2,
|
|
105
|
+
value: createReferenceNode(indexed.value, current),
|
|
106
|
+
};
|
|
77
107
|
}
|
|
78
|
-
return
|
|
108
|
+
return indexed;
|
|
79
109
|
}
|
|
80
110
|
|
|
81
|
-
protected getStrictReference<T>(
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
111
|
+
protected getStrictReference<T>(
|
|
112
|
+
current: T,
|
|
113
|
+
): SerovalIndexedValueNode | SerovalReferenceNode {
|
|
114
|
+
assert(
|
|
115
|
+
hasReferenceID(current),
|
|
116
|
+
new Error(
|
|
117
|
+
'Cannot serialize ' + typeof current + ' without reference ID.',
|
|
118
|
+
),
|
|
119
|
+
);
|
|
120
|
+
const result = this.getIndexedValue(current);
|
|
121
|
+
if (result.type === 1) {
|
|
122
|
+
return result.value;
|
|
86
123
|
}
|
|
87
|
-
|
|
88
|
-
this.refs.set(current, id);
|
|
89
|
-
return createReferenceNode(id, current);
|
|
124
|
+
return createReferenceNode(result.value, current);
|
|
90
125
|
}
|
|
91
126
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
127
|
+
protected parseFunction(
|
|
128
|
+
current: (...args: unknown[]) => unknown,
|
|
129
|
+
): SerovalNode {
|
|
95
130
|
return this.getStrictReference(current);
|
|
96
131
|
}
|
|
97
132
|
|
|
98
|
-
protected
|
|
133
|
+
protected parseWellKnownSymbol(
|
|
99
134
|
current: symbol,
|
|
100
135
|
): SerovalIndexedValueNode | SerovalWKSymbolNode | SerovalReferenceNode {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
this.markRef(registeredID);
|
|
105
|
-
return createIndexedValueNode(registeredID);
|
|
106
|
-
}
|
|
107
|
-
const isValid = current in INV_SYMBOL_REF;
|
|
108
|
-
assert(current in INV_SYMBOL_REF || hasReferenceID(current), new Error('Cannot serialize symbol without reference ID.'));
|
|
109
|
-
const id = this.refs.size;
|
|
110
|
-
this.refs.set(current, id);
|
|
111
|
-
if (isValid) {
|
|
112
|
-
return createWKSymbolNode(id, current as WellKnownSymbols);
|
|
136
|
+
const ref = this.getReference(current);
|
|
137
|
+
if (ref.type !== 0) {
|
|
138
|
+
return ref.value;
|
|
113
139
|
}
|
|
114
|
-
|
|
140
|
+
assert(
|
|
141
|
+
current in INV_SYMBOL_REF,
|
|
142
|
+
new Error('Cannot serialized unsupported symbol.'),
|
|
143
|
+
);
|
|
144
|
+
return createWKSymbolNode(ref.value, current as WellKnownSymbols);
|
|
115
145
|
}
|
|
116
146
|
|
|
117
147
|
protected parseSpecialReference(
|
|
118
148
|
ref: SpecialReference,
|
|
119
149
|
): SerovalIndexedValueNode | SerovalSpecialReferenceNode {
|
|
120
|
-
const
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
this.markRef(registeredID);
|
|
124
|
-
return createIndexedValueNode(registeredID);
|
|
150
|
+
const result = this.getIndexedValue(SPECIAL_REFS[ref]);
|
|
151
|
+
if (result.type === 1) {
|
|
152
|
+
return result.value;
|
|
125
153
|
}
|
|
126
|
-
const id = this.refs.size;
|
|
127
|
-
this.refs.set(specialRef, id);
|
|
128
154
|
return {
|
|
129
155
|
t: SerovalNodeType.SpecialReference,
|
|
130
|
-
i:
|
|
156
|
+
i: result.value,
|
|
131
157
|
s: ref,
|
|
132
158
|
l: undefined,
|
|
133
159
|
c: undefined,
|
|
@@ -141,17 +167,16 @@ export abstract class BaseParserContext implements PluginAccessOptions {
|
|
|
141
167
|
};
|
|
142
168
|
}
|
|
143
169
|
|
|
144
|
-
protected parseIteratorFactory():
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
170
|
+
protected parseIteratorFactory():
|
|
171
|
+
| SerovalIndexedValueNode
|
|
172
|
+
| SerovalIteratorFactoryNode {
|
|
173
|
+
const result = this.getIndexedValue(ITERATOR);
|
|
174
|
+
if (result.type === 1) {
|
|
175
|
+
return result.value;
|
|
149
176
|
}
|
|
150
|
-
const id = this.refs.size;
|
|
151
|
-
this.refs.set(ITERATOR, id);
|
|
152
177
|
return {
|
|
153
178
|
t: SerovalNodeType.IteratorFactory,
|
|
154
|
-
i:
|
|
179
|
+
i: result.value,
|
|
155
180
|
s: undefined,
|
|
156
181
|
l: undefined,
|
|
157
182
|
c: undefined,
|
|
@@ -159,33 +184,33 @@ export abstract class BaseParserContext implements PluginAccessOptions {
|
|
|
159
184
|
p: undefined,
|
|
160
185
|
e: undefined,
|
|
161
186
|
a: undefined,
|
|
162
|
-
f: this.
|
|
187
|
+
f: this.parseWellKnownSymbol(Symbol.iterator),
|
|
163
188
|
b: undefined,
|
|
164
189
|
o: undefined,
|
|
165
190
|
};
|
|
166
191
|
}
|
|
167
192
|
|
|
168
|
-
protected parseAsyncIteratorFactory(
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
const
|
|
172
|
-
if (
|
|
173
|
-
|
|
174
|
-
return createIndexedValueNode(registeredID);
|
|
193
|
+
protected parseAsyncIteratorFactory():
|
|
194
|
+
| SerovalIndexedValueNode
|
|
195
|
+
| SerovalAsyncIteratorFactoryNode {
|
|
196
|
+
const result = this.getIndexedValue(ASYNC_ITERATOR);
|
|
197
|
+
if (result.type === 1) {
|
|
198
|
+
return result.value;
|
|
175
199
|
}
|
|
176
|
-
const id = this.refs.size;
|
|
177
|
-
this.refs.set(ASYNC_ITERATOR, id);
|
|
178
200
|
return {
|
|
179
201
|
t: SerovalNodeType.AsyncIteratorFactory,
|
|
180
|
-
i:
|
|
181
|
-
s:
|
|
202
|
+
i: result.value,
|
|
203
|
+
s: undefined,
|
|
182
204
|
l: undefined,
|
|
183
205
|
c: undefined,
|
|
184
206
|
m: undefined,
|
|
185
207
|
p: undefined,
|
|
186
208
|
e: undefined,
|
|
187
|
-
a:
|
|
188
|
-
|
|
209
|
+
a: [
|
|
210
|
+
this.parseSpecialReference(SpecialReference.PromiseConstructor),
|
|
211
|
+
this.parseWellKnownSymbol(Symbol.asyncIterator),
|
|
212
|
+
],
|
|
213
|
+
f: undefined,
|
|
189
214
|
b: undefined,
|
|
190
215
|
o: undefined,
|
|
191
216
|
};
|