seroval 1.3.0 → 1.3.2

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.
@@ -75,7 +75,7 @@ export default abstract class BaseAsyncParserContext extends BaseParserContext {
75
75
  for (let i = 0, len = current.length; i < len; i++) {
76
76
  // For consistency in holes
77
77
  if (i in current) {
78
- nodes[i] = await this.parseTop(current[i]);
78
+ nodes[i] = await this.parse(current[i]);
79
79
  }
80
80
  }
81
81
  return nodes;
@@ -96,7 +96,7 @@ export default abstract class BaseAsyncParserContext extends BaseParserContext {
96
96
  const valueNodes: SerovalNode[] = [];
97
97
  for (let i = 0, len = entries.length; i < len; i++) {
98
98
  keyNodes.push(serializeString(entries[i][0]));
99
- valueNodes.push(await this.parseTop(entries[i][1]));
99
+ valueNodes.push(await this.parse(entries[i][1]));
100
100
  }
101
101
  // Check special properties
102
102
  let symbol = Symbol.iterator;
@@ -105,7 +105,7 @@ export default abstract class BaseAsyncParserContext extends BaseParserContext {
105
105
  valueNodes.push(
106
106
  createIteratorFactoryInstanceNode(
107
107
  this.parseIteratorFactory(),
108
- await this.parseTop(
108
+ await this.parse(
109
109
  iteratorToSequence(properties as unknown as Iterable<unknown>),
110
110
  ),
111
111
  ),
@@ -117,7 +117,7 @@ export default abstract class BaseAsyncParserContext extends BaseParserContext {
117
117
  valueNodes.push(
118
118
  createAsyncIteratorFactoryInstanceNode(
119
119
  this.parseAsyncIteratorFactory(),
120
- await this.parseTop(
120
+ await this.parse(
121
121
  createStreamFromAsyncIterable(
122
122
  properties as unknown as AsyncIterable<unknown>,
123
123
  ),
@@ -159,18 +159,14 @@ export default abstract class BaseAsyncParserContext extends BaseParserContext {
159
159
  id: number,
160
160
  current: object,
161
161
  ): Promise<SerovalBoxedNode> {
162
- return createBoxedNode(id, await this.parseTop(current.valueOf()));
162
+ return createBoxedNode(id, await this.parse(current.valueOf()));
163
163
  }
164
164
 
165
165
  private async parseTypedArray(
166
166
  id: number,
167
167
  current: TypedArrayValue,
168
168
  ): Promise<SerovalTypedArrayNode> {
169
- return createTypedArrayNode(
170
- id,
171
- current,
172
- await this.parseTop(current.buffer),
173
- );
169
+ return createTypedArrayNode(id, current, await this.parse(current.buffer));
174
170
  }
175
171
 
176
172
  private async parseBigIntTypedArray(
@@ -180,7 +176,7 @@ export default abstract class BaseAsyncParserContext extends BaseParserContext {
180
176
  return createBigIntTypedArrayNode(
181
177
  id,
182
178
  current,
183
- await this.parseTop(current.buffer),
179
+ await this.parse(current.buffer),
184
180
  );
185
181
  }
186
182
 
@@ -188,7 +184,7 @@ export default abstract class BaseAsyncParserContext extends BaseParserContext {
188
184
  id: number,
189
185
  current: DataView,
190
186
  ): Promise<SerovalDataViewNode> {
191
- return createDataViewNode(id, current, await this.parseTop(current.buffer));
187
+ return createDataViewNode(id, current, await this.parse(current.buffer));
192
188
  }
193
189
 
194
190
  private async parseError(
@@ -222,8 +218,8 @@ export default abstract class BaseAsyncParserContext extends BaseParserContext {
222
218
  const keyNodes: SerovalNode[] = [];
223
219
  const valueNodes: SerovalNode[] = [];
224
220
  for (const [key, value] of current.entries()) {
225
- keyNodes.push(await this.parseTop(key));
226
- valueNodes.push(await this.parseTop(value));
221
+ keyNodes.push(await this.parse(key));
222
+ valueNodes.push(await this.parse(value));
227
223
  }
228
224
  return this.createMapNode(id, keyNodes, valueNodes, current.size);
229
225
  }
@@ -234,7 +230,7 @@ export default abstract class BaseAsyncParserContext extends BaseParserContext {
234
230
  ): Promise<SerovalSetNode> {
235
231
  const items: SerovalNode[] = [];
236
232
  for (const item of current.keys()) {
237
- items.push(await this.parseTop(item));
233
+ items.push(await this.parse(item));
238
234
  }
239
235
  return createSetNode(id, current.size, items);
240
236
  }
@@ -255,7 +251,7 @@ export default abstract class BaseAsyncParserContext extends BaseParserContext {
255
251
  NIL,
256
252
  NIL,
257
253
  NIL,
258
- await this.parseTop(result),
254
+ await this.parse(result),
259
255
  NIL,
260
256
  NIL,
261
257
  );
@@ -295,7 +291,7 @@ export default abstract class BaseAsyncParserContext extends BaseParserContext {
295
291
  const cleanup = current.on({
296
292
  next: value => {
297
293
  this.markRef(id);
298
- this.parseTop(value).then(
294
+ this.parse(value).then(
299
295
  data => {
300
296
  sequence.push(createStreamNextNode(id, data));
301
297
  },
@@ -307,7 +303,7 @@ export default abstract class BaseAsyncParserContext extends BaseParserContext {
307
303
  },
308
304
  throw: value => {
309
305
  this.markRef(id);
310
- this.parseTop(value).then(
306
+ this.parse(value).then(
311
307
  data => {
312
308
  sequence.push(createStreamThrowNode(id, data));
313
309
  resolve(sequence);
@@ -321,7 +317,7 @@ export default abstract class BaseAsyncParserContext extends BaseParserContext {
321
317
  },
322
318
  return: value => {
323
319
  this.markRef(id);
324
- this.parseTop(value).then(
320
+ this.parse(value).then(
325
321
  data => {
326
322
  sequence.push(createStreamReturnNode(id, data));
327
323
  resolve(sequence);
@@ -348,7 +344,7 @@ export default abstract class BaseAsyncParserContext extends BaseParserContext {
348
344
  }
349
345
  const currentClass = current.constructor;
350
346
  if (currentClass === OpaqueReference) {
351
- return this.parseTop(
347
+ return this.parse(
352
348
  (current as OpaqueReference<unknown, unknown>).replacement,
353
349
  );
354
350
  }
@@ -457,7 +453,7 @@ export default abstract class BaseAsyncParserContext extends BaseParserContext {
457
453
  throw new SerovalUnsupportedTypeError(current);
458
454
  }
459
455
 
460
- async parseTop<T>(current: T): Promise<SerovalNode> {
456
+ async parse<T>(current: T): Promise<SerovalNode> {
461
457
  switch (typeof current) {
462
458
  case 'boolean':
463
459
  return current ? TRUE_NODE : FALSE_NODE;
@@ -487,9 +483,9 @@ export default abstract class BaseAsyncParserContext extends BaseParserContext {
487
483
  }
488
484
  }
489
485
 
490
- async parse<T>(current: T): Promise<SerovalNode> {
486
+ async parseTop<T>(current: T): Promise<SerovalNode> {
491
487
  try {
492
- return await this.parseTop(current);
488
+ return await this.parse(current);
493
489
  } catch (error) {
494
490
  throw error instanceof SerovalParserError
495
491
  ? error
@@ -111,7 +111,7 @@ export default abstract class BaseStreamParserContext extends BaseSyncParserCont
111
111
  const valueNodes: SerovalNode[] = [];
112
112
  for (let i = 0, len = entries.length; i < len; i++) {
113
113
  keyNodes.push(serializeString(entries[i][0]));
114
- valueNodes.push(this.parseTop(entries[i][1]));
114
+ valueNodes.push(this.parse(entries[i][1]));
115
115
  }
116
116
  // Check special properties, symbols in this case
117
117
  let symbol = Symbol.iterator;
@@ -120,7 +120,7 @@ export default abstract class BaseStreamParserContext extends BaseSyncParserCont
120
120
  valueNodes.push(
121
121
  createIteratorFactoryInstanceNode(
122
122
  this.parseIteratorFactory(),
123
- this.parseTop(
123
+ this.parse(
124
124
  iteratorToSequence(properties as unknown as Iterable<unknown>),
125
125
  ),
126
126
  ),
@@ -132,7 +132,7 @@ export default abstract class BaseStreamParserContext extends BaseSyncParserCont
132
132
  valueNodes.push(
133
133
  createAsyncIteratorFactoryInstanceNode(
134
134
  this.parseAsyncIteratorFactory(),
135
- this.parseTop(
135
+ this.parse(
136
136
  createStreamFromAsyncIterable(
137
137
  properties as unknown as AsyncIterable<unknown>,
138
138
  ),
@@ -283,7 +283,7 @@ export default abstract class BaseStreamParserContext extends BaseSyncParserCont
283
283
 
284
284
  parseWithError<T>(current: T): SerovalNode | undefined {
285
285
  try {
286
- return this.parseTop(current);
286
+ return this.parse(current);
287
287
  } catch (err) {
288
288
  this.onError(err);
289
289
  return NIL;
@@ -68,7 +68,7 @@ export default abstract class BaseSyncParserContext extends BaseParserContext {
68
68
  const nodes = [];
69
69
  for (let i = 0, len = current.length; i < len; i++) {
70
70
  if (i in current) {
71
- nodes[i] = this.parseTop(current[i]);
71
+ nodes[i] = this.parse(current[i]);
72
72
  }
73
73
  }
74
74
  return nodes;
@@ -86,7 +86,7 @@ export default abstract class BaseSyncParserContext extends BaseParserContext {
86
86
  const valueNodes: SerovalNode[] = [];
87
87
  for (let i = 0, len = entries.length; i < len; i++) {
88
88
  keyNodes.push(serializeString(entries[i][0]));
89
- valueNodes.push(this.parseTop(entries[i][1]));
89
+ valueNodes.push(this.parse(entries[i][1]));
90
90
  }
91
91
  // Check special properties, symbols in this case
92
92
  let symbol = Symbol.iterator;
@@ -95,7 +95,7 @@ export default abstract class BaseSyncParserContext extends BaseParserContext {
95
95
  valueNodes.push(
96
96
  createIteratorFactoryInstanceNode(
97
97
  this.parseIteratorFactory(),
98
- this.parseTop(
98
+ this.parse(
99
99
  iteratorToSequence(properties as unknown as Iterable<unknown>),
100
100
  ),
101
101
  ),
@@ -107,7 +107,7 @@ export default abstract class BaseSyncParserContext extends BaseParserContext {
107
107
  valueNodes.push(
108
108
  createAsyncIteratorFactoryInstanceNode(
109
109
  this.parseAsyncIteratorFactory(),
110
- this.parseTop(createStream()),
110
+ this.parse(createStream()),
111
111
  ),
112
112
  );
113
113
  }
@@ -142,29 +142,25 @@ export default abstract class BaseSyncParserContext extends BaseParserContext {
142
142
  }
143
143
 
144
144
  protected parseBoxed(id: number, current: object): SerovalBoxedNode {
145
- return createBoxedNode(id, this.parseTop(current.valueOf()));
145
+ return createBoxedNode(id, this.parse(current.valueOf()));
146
146
  }
147
147
 
148
148
  protected parseTypedArray(
149
149
  id: number,
150
150
  current: TypedArrayValue,
151
151
  ): SerovalTypedArrayNode {
152
- return createTypedArrayNode(id, current, this.parseTop(current.buffer));
152
+ return createTypedArrayNode(id, current, this.parse(current.buffer));
153
153
  }
154
154
 
155
155
  protected parseBigIntTypedArray(
156
156
  id: number,
157
157
  current: BigIntTypedArrayValue,
158
158
  ): SerovalBigIntTypedArrayNode {
159
- return createBigIntTypedArrayNode(
160
- id,
161
- current,
162
- this.parseTop(current.buffer),
163
- );
159
+ return createBigIntTypedArrayNode(id, current, this.parse(current.buffer));
164
160
  }
165
161
 
166
162
  protected parseDataView(id: number, current: DataView): SerovalDataViewNode {
167
- return createDataViewNode(id, current, this.parseTop(current.buffer));
163
+ return createDataViewNode(id, current, this.parse(current.buffer));
168
164
  }
169
165
 
170
166
  protected parseError(id: number, current: Error): SerovalErrorNode {
@@ -195,8 +191,8 @@ export default abstract class BaseSyncParserContext extends BaseParserContext {
195
191
  const keyNodes: SerovalNode[] = [];
196
192
  const valueNodes: SerovalNode[] = [];
197
193
  for (const [key, value] of current.entries()) {
198
- keyNodes.push(this.parseTop(key));
199
- valueNodes.push(this.parseTop(value));
194
+ keyNodes.push(this.parse(key));
195
+ valueNodes.push(this.parse(value));
200
196
  }
201
197
  return this.createMapNode(id, keyNodes, valueNodes, current.size);
202
198
  }
@@ -204,7 +200,7 @@ export default abstract class BaseSyncParserContext extends BaseParserContext {
204
200
  protected parseSet(id: number, current: Set<unknown>): SerovalSetNode {
205
201
  const items: SerovalNode[] = [];
206
202
  for (const item of current.keys()) {
207
- items.push(this.parseTop(item));
203
+ items.push(this.parse(item));
208
204
  }
209
205
  return createSetNode(id, current.size, items);
210
206
  }
@@ -256,7 +252,7 @@ export default abstract class BaseSyncParserContext extends BaseParserContext {
256
252
  }
257
253
  const currentClass = current.constructor;
258
254
  if (currentClass === OpaqueReference) {
259
- return this.parseTop(
255
+ return this.parse(
260
256
  (current as OpaqueReference<unknown, unknown>).replacement,
261
257
  );
262
258
  }
@@ -365,7 +361,7 @@ export default abstract class BaseSyncParserContext extends BaseParserContext {
365
361
  throw new SerovalUnsupportedTypeError(current);
366
362
  }
367
363
 
368
- parseTop<T>(current: T): SerovalNode {
364
+ parse<T>(current: T): SerovalNode {
369
365
  switch (typeof current) {
370
366
  case 'boolean':
371
367
  return current ? TRUE_NODE : FALSE_NODE;
@@ -396,9 +392,9 @@ export default abstract class BaseSyncParserContext extends BaseParserContext {
396
392
  }
397
393
  }
398
394
 
399
- parse<T>(current: T): SerovalNode {
395
+ parseTop<T>(current: T): SerovalNode {
400
396
  try {
401
- return this.parseTop(current);
397
+ return this.parse(current);
402
398
  } catch (error) {
403
399
  throw error instanceof SerovalParserError
404
400
  ? error
@@ -1008,6 +1008,15 @@ export default abstract class BaseSerializerContext
1008
1008
  ['s'],
1009
1009
  this.createFunction(
1010
1010
  ['b', 'c', 'p', 'd', 'e', 't', 'f'],
1011
+ /**
1012
+ * b = resolved values
1013
+ * c = b size
1014
+ * p = pending promises
1015
+ * d = index where the resolved value stops
1016
+ * e = if the last value is a throw
1017
+ * t = placeholder variable
1018
+ * f = finalize
1019
+ */
1011
1020
  '(b=[],c=0,p=[],d=-1,e=!1,f=' +
1012
1021
  this.createEffectfulFunction(
1013
1022
  ['i', 'l'],
@@ -1031,13 +1040,13 @@ export default abstract class BaseSerializerContext
1031
1040
  '}),t={[' +
1032
1041
  this.getRefParam(symbol.i) +
1033
1042
  ']:' +
1034
- this.createFunction([], 't') +
1043
+ this.createFunction([], 't.p') +
1035
1044
  ',next:' +
1036
1045
  this.createEffectfulFunction(
1037
1046
  ['i', 't', 'v'],
1038
- 'if(d===-1){return((i=c++)>=b.length)?(p.push(t=' +
1047
+ 'if(d===-1){return((i=c++)>=b.length)?(' +
1039
1048
  this.getRefParam(promise.i) +
1040
- '()),t):{done:!1,value:b[i]}}if(c>d)return{done:!0,value:void 0};if(v=b[i=c++],i!==d)return{done:!1,value:v};if(e)throw v;return{done:!0,value:v}',
1049
+ '(t={p:0,s:0,f:0}),p.push(t),t.p):{done:!1,value:b[i]}}if(c>d)return{done:!0,value:void 0};if(v=b[i=c++],i!==d)return{done:!1,value:v};if(e)throw v;return{done:!0,value:v}',
1041
1050
  ) +
1042
1051
  '})',
1043
1052
  ),
@@ -25,7 +25,7 @@ export function crossSerialize<T>(
25
25
  disabledFeatures: options.disabledFeatures,
26
26
  refs: options.refs,
27
27
  });
28
- const tree = ctx.parse(source);
28
+ const tree = ctx.parseTop(source);
29
29
  const serial = new CrossSerializerContext({
30
30
  plugins,
31
31
  features: ctx.features,
@@ -49,7 +49,7 @@ export async function crossSerializeAsync<T>(
49
49
  disabledFeatures: options.disabledFeatures,
50
50
  refs: options.refs,
51
51
  });
52
- const tree = await ctx.parse(source);
52
+ const tree = await ctx.parseTop(source);
53
53
  const serial = new CrossSerializerContext({
54
54
  plugins,
55
55
  features: ctx.features,
@@ -71,7 +71,7 @@ export function toCrossJSON<T>(
71
71
  disabledFeatures: options.disabledFeatures,
72
72
  refs: options.refs,
73
73
  });
74
- return ctx.parse(source);
74
+ return ctx.parseTop(source);
75
75
  }
76
76
 
77
77
  export type ToCrossJSONAsyncOptions = CrossParserContextOptions;
@@ -86,7 +86,7 @@ export async function toCrossJSONAsync<T>(
86
86
  disabledFeatures: options.disabledFeatures,
87
87
  refs: options.refs,
88
88
  });
89
- return await ctx.parse(source);
89
+ return await ctx.parseTop(source);
90
90
  }
91
91
 
92
92
  export interface CrossSerializeStreamOptions
@@ -166,5 +166,5 @@ export function fromCrossJSON<T>(
166
166
  plugins,
167
167
  refs: options.refs,
168
168
  });
169
- return ctx.deserialize(source) as T;
169
+ return ctx.deserializeTop(source) as T;
170
170
  }
@@ -1,4 +1,4 @@
1
- import { resolvePlugins, type PluginAccessOptions } from '../plugin';
1
+ import { type PluginAccessOptions, resolvePlugins } from '../plugin';
2
2
  import type { SerovalNode } from '../types';
3
3
  import type { AsyncParserContextOptions } from './async';
4
4
  import AsyncParserContext from './async';
@@ -16,7 +16,7 @@ export function serialize<T>(
16
16
  plugins,
17
17
  disabledFeatures: options.disabledFeatures,
18
18
  });
19
- const tree = ctx.parse(source);
19
+ const tree = ctx.parseTop(source);
20
20
  const serial = new VanillaSerializerContext({
21
21
  plugins,
22
22
  features: ctx.features,
@@ -34,7 +34,7 @@ export async function serializeAsync<T>(
34
34
  plugins,
35
35
  disabledFeatures: options.disabledFeatures,
36
36
  });
37
- const tree = await ctx.parse(source);
37
+ const tree = await ctx.parseTop(source);
38
38
  const serial = new VanillaSerializerContext({
39
39
  plugins,
40
40
  features: ctx.features,
@@ -63,7 +63,7 @@ export function toJSON<T>(
63
63
  disabledFeatures: options.disabledFeatures,
64
64
  });
65
65
  return {
66
- t: ctx.parse(source),
66
+ t: ctx.parseTop(source),
67
67
  f: ctx.features,
68
68
  m: Array.from(ctx.marked),
69
69
  };
@@ -79,7 +79,7 @@ export async function toJSONAsync<T>(
79
79
  disabledFeatures: options.disabledFeatures,
80
80
  });
81
81
  return {
82
- t: await ctx.parse(source),
82
+ t: await ctx.parseTop(source),
83
83
  f: ctx.features,
84
84
  m: Array.from(ctx.marked),
85
85
  };
@@ -107,5 +107,5 @@ export function fromJSON<T>(
107
107
  plugins,
108
108
  markedRefs: source.m,
109
109
  });
110
- return ctx.deserialize(source.t) as T;
110
+ return ctx.deserializeTop(source.t) as T;
111
111
  }