seroval 1.4.0 → 1.4.1

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 (68) hide show
  1. package/dist/cjs/development/index.cjs +524 -364
  2. package/dist/cjs/development/index.cjs.map +3 -3
  3. package/dist/cjs/production/index.cjs +3 -3
  4. package/dist/esm/development/index.mjs +524 -364
  5. package/dist/esm/development/index.mjs.map +3 -3
  6. package/dist/esm/production/index.mjs +3 -3
  7. package/dist/types/core/base-primitives.d.ts +2 -2
  8. package/dist/types/core/base-primitives.d.ts.map +1 -1
  9. package/dist/types/core/compat.d.ts +2 -1
  10. package/dist/types/core/compat.d.ts.map +1 -1
  11. package/dist/types/core/constants.d.ts +2 -2
  12. package/dist/types/core/constants.d.ts.map +1 -1
  13. package/dist/types/core/constructors.d.ts +1 -1
  14. package/dist/types/core/constructors.d.ts.map +1 -1
  15. package/dist/types/core/context/async-parser.d.ts +5 -4
  16. package/dist/types/core/context/async-parser.d.ts.map +1 -1
  17. package/dist/types/core/context/deserializer.d.ts +7 -2
  18. package/dist/types/core/context/deserializer.d.ts.map +1 -1
  19. package/dist/types/core/context/parser.d.ts +3 -1
  20. package/dist/types/core/context/parser.d.ts.map +1 -1
  21. package/dist/types/core/context/serializer.d.ts.map +1 -1
  22. package/dist/types/core/context/sync-parser.d.ts +5 -5
  23. package/dist/types/core/context/sync-parser.d.ts.map +1 -1
  24. package/dist/types/core/cross/index.d.ts.map +1 -1
  25. package/dist/types/core/errors.d.ts +9 -0
  26. package/dist/types/core/errors.d.ts.map +1 -1
  27. package/dist/types/core/literals.d.ts.map +1 -1
  28. package/dist/types/core/node.d.ts +1 -1
  29. package/dist/types/core/node.d.ts.map +1 -1
  30. package/dist/types/core/string.d.ts.map +1 -1
  31. package/dist/types/core/tree/index.d.ts +4 -1
  32. package/dist/types/core/tree/index.d.ts.map +1 -1
  33. package/dist/types/core/types.d.ts +2 -11
  34. package/dist/types/core/types.d.ts.map +1 -1
  35. package/package.json +2 -3
  36. package/src/core/Serializer.ts +0 -119
  37. package/src/core/base-primitives.ts +0 -515
  38. package/src/core/compat.ts +0 -21
  39. package/src/core/constants.ts +0 -187
  40. package/src/core/constructors.ts +0 -269
  41. package/src/core/context/async-parser.ts +0 -586
  42. package/src/core/context/deserializer.ts +0 -646
  43. package/src/core/context/parser.ts +0 -333
  44. package/src/core/context/serializer.ts +0 -1411
  45. package/src/core/context/sync-parser.ts +0 -833
  46. package/src/core/cross/index.ts +0 -186
  47. package/src/core/errors.ts +0 -163
  48. package/src/core/function-string.ts +0 -32
  49. package/src/core/keys.ts +0 -17
  50. package/src/core/literals.ts +0 -43
  51. package/src/core/node.ts +0 -40
  52. package/src/core/opaque-reference.ts +0 -9
  53. package/src/core/plugin.ts +0 -100
  54. package/src/core/reference.ts +0 -66
  55. package/src/core/special-reference.ts +0 -42
  56. package/src/core/stream.ts +0 -71
  57. package/src/core/string.ts +0 -84
  58. package/src/core/symbols.ts +0 -18
  59. package/src/core/tree/index.ts +0 -125
  60. package/src/core/types.ts +0 -375
  61. package/src/core/utils/error.ts +0 -73
  62. package/src/core/utils/get-identifier.ts +0 -18
  63. package/src/core/utils/get-object-flag.ts +0 -14
  64. package/src/core/utils/is-valid-identifier.ts +0 -12
  65. package/src/core/utils/iterator-to-sequence.ts +0 -44
  66. package/src/core/utils/promise-to-result.ts +0 -9
  67. package/src/core/utils/typed-array.ts +0 -56
  68. package/src/index.ts +0 -36
@@ -1,333 +0,0 @@
1
- import {
2
- createIndexedValueNode,
3
- createReferenceNode,
4
- createWKSymbolNode,
5
- } from '../base-primitives';
6
- import { ALL_ENABLED } from '../compat';
7
- import type { WellKnownSymbols } from '../constants';
8
- import { INV_SYMBOL_REF, NIL, SerovalNodeType } from '../constants';
9
- import { SerovalUnsupportedTypeError } from '../errors';
10
- import { createSerovalNode } from '../node';
11
- import type { PluginAccessOptions, SerovalMode } from '../plugin';
12
- import { hasReferenceID } from '../reference';
13
- import {
14
- ASYNC_ITERATOR,
15
- ITERATOR,
16
- SPECIAL_REFS,
17
- SpecialReference,
18
- } from '../special-reference';
19
- import { serializeString } from '../string';
20
- import { SYM_ASYNC_ITERATOR, SYM_ITERATOR } from '../symbols';
21
- import type {
22
- SerovalArrayBufferNode,
23
- SerovalAsyncIteratorFactoryNode,
24
- SerovalIndexedValueNode,
25
- SerovalIteratorFactoryNode,
26
- SerovalMapNode,
27
- SerovalNode,
28
- SerovalNullConstructorNode,
29
- SerovalObjectNode,
30
- SerovalObjectRecordNode,
31
- SerovalPromiseConstructorNode,
32
- SerovalReferenceNode,
33
- SerovalSpecialReferenceNode,
34
- SerovalWKSymbolNode,
35
- } from '../types';
36
- import { getObjectFlag } from '../utils/get-object-flag';
37
-
38
- export interface BaseParserContextOptions extends PluginAccessOptions {
39
- disabledFeatures?: number;
40
- refs?: Map<unknown, number>;
41
- }
42
-
43
- export const enum ParserNodeType {
44
- Fresh = 0,
45
- Indexed = 1,
46
- Referenced = 2,
47
- }
48
-
49
- export interface FreshNode {
50
- type: ParserNodeType.Fresh;
51
- value: number;
52
- }
53
-
54
- export interface IndexedNode {
55
- type: ParserNodeType.Indexed;
56
- value: SerovalIndexedValueNode;
57
- }
58
-
59
- export interface ReferencedNode {
60
- type: ParserNodeType.Referenced;
61
- value: SerovalReferenceNode;
62
- }
63
-
64
- type ObjectNode = FreshNode | IndexedNode | ReferencedNode;
65
-
66
- export interface BaseParserContext extends PluginAccessOptions {
67
- readonly mode: SerovalMode;
68
-
69
- marked: Set<number>;
70
-
71
- refs: Map<unknown, number>;
72
-
73
- features: number;
74
- }
75
-
76
- export function createBaseParserContext(
77
- mode: SerovalMode,
78
- options: BaseParserContextOptions,
79
- ): BaseParserContext {
80
- return {
81
- plugins: options.plugins,
82
- mode,
83
- marked: new Set(),
84
- features: ALL_ENABLED ^ (options.disabledFeatures || 0),
85
- refs: options.refs || new Map(),
86
- };
87
- }
88
-
89
- /**
90
- * Ensures that the value (based on an identifier) has been visited by the parser.
91
- * @param ctx
92
- * @param id
93
- */
94
- export function markParserRef(ctx: BaseParserContext, id: number): void {
95
- ctx.marked.add(id);
96
- }
97
-
98
- export function isParserRefMarked(ctx: BaseParserContext, id: number): boolean {
99
- return ctx.marked.has(id);
100
- }
101
-
102
- /**
103
- * Creates an identifier for a value
104
- * @param ctx
105
- * @param current
106
- */
107
- export function createIndexForValue<T>(
108
- ctx: BaseParserContext,
109
- current: T,
110
- ): number {
111
- const id = ctx.refs.size;
112
- ctx.refs.set(current, id);
113
- return id;
114
- }
115
-
116
- export function getNodeForIndexedValue<T>(
117
- ctx: BaseParserContext,
118
- current: T,
119
- ): FreshNode | IndexedNode {
120
- const registeredId = ctx.refs.get(current);
121
- if (registeredId != null) {
122
- markParserRef(ctx, registeredId);
123
- return {
124
- type: ParserNodeType.Indexed,
125
- value: createIndexedValueNode(registeredId),
126
- };
127
- }
128
- return {
129
- type: ParserNodeType.Fresh,
130
- value: createIndexForValue(ctx, current),
131
- };
132
- }
133
-
134
- export function getReferenceNode<T>(
135
- ctx: BaseParserContext,
136
- current: T,
137
- ): ObjectNode {
138
- const indexed = getNodeForIndexedValue(ctx, current);
139
- if (indexed.type === ParserNodeType.Indexed) {
140
- return indexed;
141
- }
142
- // Special references are special ;)
143
- if (hasReferenceID(current)) {
144
- return {
145
- type: ParserNodeType.Referenced,
146
- value: createReferenceNode(indexed.value, current),
147
- };
148
- }
149
- return indexed;
150
- }
151
-
152
- /**
153
- * Parsing methods
154
- */
155
- export function parseWellKnownSymbol(
156
- ctx: BaseParserContext,
157
- current: symbol,
158
- ): SerovalIndexedValueNode | SerovalWKSymbolNode | SerovalReferenceNode {
159
- const ref = getReferenceNode(ctx, current);
160
- if (ref.type !== ParserNodeType.Fresh) {
161
- return ref.value;
162
- }
163
- if (current in INV_SYMBOL_REF) {
164
- return createWKSymbolNode(ref.value, current as WellKnownSymbols);
165
- }
166
- throw new SerovalUnsupportedTypeError(current);
167
- }
168
-
169
- export function parseSpecialReference(
170
- ctx: BaseParserContext,
171
- ref: SpecialReference,
172
- ): SerovalIndexedValueNode | SerovalSpecialReferenceNode {
173
- const result = getNodeForIndexedValue(ctx, SPECIAL_REFS[ref]);
174
- if (result.type === ParserNodeType.Indexed) {
175
- return result.value;
176
- }
177
- return createSerovalNode(
178
- SerovalNodeType.SpecialReference,
179
- result.value,
180
- ref,
181
- NIL,
182
- NIL,
183
- NIL,
184
- NIL,
185
- NIL,
186
- NIL,
187
- NIL,
188
- NIL,
189
- NIL,
190
- );
191
- }
192
-
193
- export function parseIteratorFactory(
194
- ctx: BaseParserContext,
195
- ): SerovalIndexedValueNode | SerovalIteratorFactoryNode {
196
- const result = getNodeForIndexedValue(ctx, ITERATOR);
197
- if (result.type === ParserNodeType.Indexed) {
198
- return result.value;
199
- }
200
- return createSerovalNode(
201
- SerovalNodeType.IteratorFactory,
202
- result.value,
203
- NIL,
204
- NIL,
205
- NIL,
206
- NIL,
207
- NIL,
208
- NIL,
209
- NIL,
210
- parseWellKnownSymbol(ctx, SYM_ITERATOR),
211
- NIL,
212
- NIL,
213
- );
214
- }
215
-
216
- export function parseAsyncIteratorFactory(
217
- ctx: BaseParserContext,
218
- ): SerovalIndexedValueNode | SerovalAsyncIteratorFactoryNode {
219
- const result = getNodeForIndexedValue(ctx, ASYNC_ITERATOR);
220
- if (result.type === ParserNodeType.Indexed) {
221
- return result.value;
222
- }
223
- return createSerovalNode(
224
- SerovalNodeType.AsyncIteratorFactory,
225
- result.value,
226
- NIL,
227
- NIL,
228
- NIL,
229
- NIL,
230
- NIL,
231
- NIL,
232
- [
233
- parseSpecialReference(ctx, SpecialReference.PromiseConstructor),
234
- parseWellKnownSymbol(ctx, SYM_ASYNC_ITERATOR),
235
- ],
236
- NIL,
237
- NIL,
238
- NIL,
239
- );
240
- }
241
-
242
- export function createObjectNode(
243
- id: number,
244
- current: Record<string, unknown>,
245
- empty: boolean,
246
- record: SerovalObjectRecordNode,
247
- ): SerovalObjectNode | SerovalNullConstructorNode {
248
- return createSerovalNode(
249
- empty ? SerovalNodeType.NullConstructor : SerovalNodeType.Object,
250
- id,
251
- NIL,
252
- NIL,
253
- NIL,
254
- NIL,
255
- record,
256
- NIL,
257
- NIL,
258
- NIL,
259
- NIL,
260
- getObjectFlag(current),
261
- );
262
- }
263
-
264
- export function createMapNode(
265
- ctx: BaseParserContext,
266
- id: number,
267
- k: SerovalNode[],
268
- v: SerovalNode[],
269
- s: number,
270
- ): SerovalMapNode {
271
- return createSerovalNode(
272
- SerovalNodeType.Map,
273
- id,
274
- NIL,
275
- NIL,
276
- NIL,
277
- NIL,
278
- NIL,
279
- { k, v, s },
280
- NIL,
281
- parseSpecialReference(ctx, SpecialReference.MapSentinel),
282
- NIL,
283
- NIL,
284
- );
285
- }
286
-
287
- export function createPromiseConstructorNode(
288
- ctx: BaseParserContext,
289
- id: number,
290
- resolver: number,
291
- ): SerovalPromiseConstructorNode {
292
- return createSerovalNode(
293
- SerovalNodeType.PromiseConstructor,
294
- id,
295
- resolver,
296
- NIL,
297
- NIL,
298
- NIL,
299
- NIL,
300
- NIL,
301
- NIL,
302
- parseSpecialReference(ctx, SpecialReference.PromiseConstructor),
303
- NIL,
304
- NIL,
305
- );
306
- }
307
-
308
- export function createArrayBufferNode(
309
- ctx: BaseParserContext,
310
- id: number,
311
- current: ArrayBuffer,
312
- ): SerovalArrayBufferNode {
313
- const bytes = new Uint8Array(current);
314
- const len = bytes.length;
315
- let result = '';
316
- for (let i = 0; i < len; i++) {
317
- result += String.fromCharCode(bytes[i]);
318
- }
319
- return createSerovalNode(
320
- SerovalNodeType.ArrayBuffer,
321
- id,
322
- serializeString(btoa(result)),
323
- len,
324
- NIL,
325
- NIL,
326
- NIL,
327
- NIL,
328
- NIL,
329
- parseSpecialReference(ctx, SpecialReference.ArrayBufferConstructor),
330
- NIL,
331
- NIL,
332
- );
333
- }