seroval 1.2.0 → 1.2.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.
@@ -243,19 +243,17 @@ function resolvePlugins(plugins) {
243
243
  }
244
244
 
245
245
  // src/core/abort-signal.ts
246
- function abortSignalToPromise(signal) {
247
- return new Promise((resolve) => {
248
- signal.addEventListener(
249
- "abort",
250
- () => {
251
- resolve(signal.reason);
252
- },
253
- {
254
- once: true
255
- }
256
- );
246
+ function resolveAbortSignalResult(resolve) {
247
+ resolve(this.reason);
248
+ }
249
+ function resolveAbortSignal(resolve) {
250
+ this.addEventListener("abort", resolveAbortSignalResult.bind(this, resolve), {
251
+ once: true
257
252
  });
258
253
  }
254
+ function abortSignalToPromise(signal) {
255
+ return new Promise(resolveAbortSignal.bind(signal));
256
+ }
259
257
 
260
258
  // src/core/constants.ts
261
259
  var SYMBOL_STRING = {
@@ -1268,17 +1266,6 @@ var BaseParserContext = class {
1268
1266
  }
1269
1267
  return indexed;
1270
1268
  }
1271
- getStrictReference(current) {
1272
- assert(hasReferenceID(current), new SerovalUnsupportedTypeError(current));
1273
- const result = this.getIndexedValue(current);
1274
- if (result.type === 1 /* Indexed */) {
1275
- return result.value;
1276
- }
1277
- return createReferenceNode(result.value, current);
1278
- }
1279
- parseFunction(current) {
1280
- return this.getStrictReference(current);
1281
- }
1282
1269
  parseWellKnownSymbol(current) {
1283
1270
  const ref = this.getReference(current);
1284
1271
  if (ref.type !== 0 /* Fresh */) {
@@ -1743,6 +1730,17 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1743
1730
  }
1744
1731
  throw new SerovalUnsupportedTypeError(current);
1745
1732
  }
1733
+ async parseFunction(current) {
1734
+ const ref = this.getReference(current);
1735
+ if (ref.type !== 0 /* Fresh */) {
1736
+ return ref.value;
1737
+ }
1738
+ const plugin = await this.parsePlugin(ref.value, current);
1739
+ if (plugin) {
1740
+ return plugin;
1741
+ }
1742
+ throw new SerovalUnsupportedTypeError(current);
1743
+ }
1746
1744
  async parse(current) {
1747
1745
  try {
1748
1746
  switch (typeof current) {
@@ -3304,6 +3302,17 @@ var BaseSyncParserContext = class extends BaseParserContext {
3304
3302
  }
3305
3303
  throw new SerovalUnsupportedTypeError(current);
3306
3304
  }
3305
+ parseFunction(current) {
3306
+ const ref = this.getReference(current);
3307
+ if (ref.type !== 0 /* Fresh */) {
3308
+ return ref.value;
3309
+ }
3310
+ const plugin = this.parsePlugin(ref.value, current);
3311
+ if (plugin) {
3312
+ return plugin;
3313
+ }
3314
+ throw new SerovalUnsupportedTypeError(current);
3315
+ }
3307
3316
  parse(current) {
3308
3317
  try {
3309
3318
  switch (typeof current) {
@@ -3320,14 +3329,15 @@ var BaseSyncParserContext = class extends BaseParserContext {
3320
3329
  case "object": {
3321
3330
  if (current) {
3322
3331
  const ref = this.getReference(current);
3323
- return ref.type === 0 ? this.parseObject(ref.value, current) : ref.value;
3332
+ return ref.type === 0 /* Fresh */ ? this.parseObject(ref.value, current) : ref.value;
3324
3333
  }
3325
3334
  return NULL_NODE;
3326
3335
  }
3327
3336
  case "symbol":
3328
3337
  return this.parseWellKnownSymbol(current);
3329
- case "function":
3338
+ case "function": {
3330
3339
  return this.parseFunction(current);
3340
+ }
3331
3341
  default:
3332
3342
  throw new SerovalUnsupportedTypeError(current);
3333
3343
  }
@@ -3553,6 +3563,33 @@ var BaseStreamParserContext = class extends BaseSyncParserContext {
3553
3563
  });
3554
3564
  return result;
3555
3565
  }
3566
+ handleAbortSignal(id, current) {
3567
+ if (this.alive) {
3568
+ const parsed = this.parseWithError(current.reason);
3569
+ if (parsed) {
3570
+ this.onParse(
3571
+ createSerovalNode(
3572
+ 36 /* AbortSignalAbort */,
3573
+ id,
3574
+ NIL,
3575
+ NIL,
3576
+ NIL,
3577
+ NIL,
3578
+ NIL,
3579
+ NIL,
3580
+ [
3581
+ this.parseSpecialReference(6 /* AbortSignalAbort */),
3582
+ parsed
3583
+ ],
3584
+ NIL,
3585
+ NIL,
3586
+ NIL
3587
+ )
3588
+ );
3589
+ }
3590
+ }
3591
+ this.popPendingState();
3592
+ }
3556
3593
  parseAbortSignal(id, current) {
3557
3594
  if (current.aborted) {
3558
3595
  return this.parseAbortSignalSync(id, current);
@@ -3560,33 +3597,7 @@ var BaseStreamParserContext = class extends BaseSyncParserContext {
3560
3597
  this.pushPendingState();
3561
3598
  current.addEventListener(
3562
3599
  "abort",
3563
- () => {
3564
- if (this.alive) {
3565
- const parsed = this.parseWithError(current.reason);
3566
- if (parsed) {
3567
- this.onParse(
3568
- createSerovalNode(
3569
- 36 /* AbortSignalAbort */,
3570
- id,
3571
- NIL,
3572
- NIL,
3573
- NIL,
3574
- NIL,
3575
- NIL,
3576
- NIL,
3577
- [
3578
- this.parseSpecialReference(6 /* AbortSignalAbort */),
3579
- parsed
3580
- ],
3581
- NIL,
3582
- NIL,
3583
- NIL
3584
- )
3585
- );
3586
- }
3587
- }
3588
- this.popPendingState();
3589
- },
3600
+ this.handleAbortSignal.bind(this, id, current),
3590
3601
  { once: true }
3591
3602
  );
3592
3603
  return this.createAbortSignalConstructorNode(id);
@@ -3722,9 +3733,7 @@ function crossSerializeStream(source, options) {
3722
3733
  onDone: options.onDone
3723
3734
  });
3724
3735
  ctx.start(source);
3725
- return () => {
3726
- ctx.destroy();
3727
- };
3736
+ return ctx.destroy.bind(ctx);
3728
3737
  }
3729
3738
  function toCrossJSONStream(source, options) {
3730
3739
  const plugins = resolvePlugins(options.plugins);
@@ -3737,9 +3746,7 @@ function toCrossJSONStream(source, options) {
3737
3746
  onDone: options.onDone
3738
3747
  });
3739
3748
  ctx.start(source);
3740
- return () => {
3741
- ctx.destroy();
3742
- };
3749
+ return ctx.destroy.bind(ctx);
3743
3750
  }
3744
3751
  function fromCrossJSON(source, options) {
3745
3752
  const plugins = resolvePlugins(options.plugins);