seroval 1.0.6 → 1.0.8

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.
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var src_exports = {};
22
22
  __export(src_exports, {
23
23
  Feature: () => Feature,
24
+ OpaqueReference: () => OpaqueReference,
24
25
  Serializer: () => Serializer,
25
26
  SerovalDeserializationError: () => SerovalDeserializationError,
26
27
  SerovalError: () => SerovalError,
@@ -920,6 +921,14 @@ var SerovalUnknownTypedArrayError = class extends Error {
920
921
  }
921
922
  };
922
923
 
924
+ // src/core/opaque-reference.ts
925
+ var OpaqueReference = class {
926
+ constructor(value, replacement) {
927
+ this.value = value;
928
+ this.replacement = replacement;
929
+ }
930
+ };
931
+
923
932
  // src/core/special-reference.ts
924
933
  var ITERATOR = {};
925
934
  var ASYNC_ITERATOR = {};
@@ -1560,6 +1569,7 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1560
1569
  })
1561
1570
  );
1562
1571
  }
1572
+ // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: <explanation>
1563
1573
  async parseObject(id, current) {
1564
1574
  if (Array.isArray(current)) {
1565
1575
  return this.parseArray(id, current);
@@ -1567,11 +1577,16 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1567
1577
  if (isStream(current)) {
1568
1578
  return this.parseStream(id, current);
1569
1579
  }
1580
+ const currentClass = current.constructor;
1581
+ if (currentClass === OpaqueReference) {
1582
+ return this.parse(
1583
+ current.replacement
1584
+ );
1585
+ }
1570
1586
  const parsed = await this.parsePlugin(id, current);
1571
1587
  if (parsed) {
1572
1588
  return parsed;
1573
1589
  }
1574
- const currentClass = current.constructor;
1575
1590
  switch (currentClass) {
1576
1591
  case Object:
1577
1592
  return this.parsePlainObject(
@@ -1683,11 +1698,11 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1683
1698
  }
1684
1699
  };
1685
1700
 
1686
- // src/core/tree/async.ts
1687
- var AsyncParserContext = class extends BaseAsyncParserContext {
1701
+ // src/core/cross/async.ts
1702
+ var CrossAsyncParserContext = class extends BaseAsyncParserContext {
1688
1703
  constructor() {
1689
1704
  super(...arguments);
1690
- this.mode = "vanilla";
1705
+ this.mode = "cross";
1691
1706
  }
1692
1707
  };
1693
1708
 
@@ -2027,15 +2042,14 @@ var BaseDeserializerContext = class {
2027
2042
  }
2028
2043
  };
2029
2044
 
2030
- // src/core/tree/deserializer.ts
2031
- var VanillaDeserializerContext = class extends BaseDeserializerContext {
2032
- constructor(options) {
2033
- super(options);
2034
- this.mode = "vanilla";
2035
- this.marked = new Set(options.markedRefs);
2045
+ // src/core/cross/deserializer.ts
2046
+ var CrossDeserializerContext = class extends BaseDeserializerContext {
2047
+ constructor() {
2048
+ super(...arguments);
2049
+ this.mode = "cross";
2036
2050
  }
2037
2051
  assignIndexedValue(index, value) {
2038
- if (this.marked.has(index)) {
2052
+ if (!this.refs.has(index)) {
2039
2053
  this.refs.set(index, value);
2040
2054
  }
2041
2055
  return value;
@@ -2749,7 +2763,7 @@ var BaseSerializerContext = class {
2749
2763
  "if(t=p.shift())t.s({done:!0,value:v});f(),d=b.length,b.push(v)"
2750
2764
  ) + "}),t={[" + this.getRefParam(symbol.i) + "]:" + this.createFunction([], "t") + ",next:" + this.createEffectfulFunction(
2751
2765
  ["i", "t", "v"],
2752
- "if(d===-1){return((i=c++)>=b.length)?(p.push(t=" + this.getRefParam(promise.i) + "()),t):{done:!0,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}"
2766
+ "if(d===-1){return((i=c++)>=b.length)?(p.push(t=" + this.getRefParam(promise.i) + "()),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}"
2753
2767
  ) + "})"
2754
2768
  )
2755
2769
  )
@@ -2867,89 +2881,34 @@ var BaseSerializerContext = class {
2867
2881
  }
2868
2882
  };
2869
2883
 
2870
- // src/core/utils/get-identifier.ts
2871
- var REF_START_CHARS = "hjkmoquxzABCDEFGHIJKLNPQRTUVWXYZ$_";
2872
- var REF_START_CHARS_LEN = REF_START_CHARS.length;
2873
- var REF_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$_";
2874
- var REF_CHARS_LEN = REF_CHARS.length;
2875
- function getIdentifier(index) {
2876
- let mod = index % REF_START_CHARS_LEN;
2877
- let ref = REF_START_CHARS[mod];
2878
- index = (index - mod) / REF_START_CHARS_LEN;
2879
- while (index > 0) {
2880
- mod = index % REF_CHARS_LEN;
2881
- ref += REF_CHARS[mod];
2882
- index = (index - mod) / REF_CHARS_LEN;
2883
- }
2884
- return ref;
2885
- }
2886
-
2887
- // src/core/tree/serializer.ts
2888
- var VanillaSerializerContext = class extends BaseSerializerContext {
2889
- constructor() {
2890
- super(...arguments);
2891
- this.mode = "vanilla";
2892
- /**
2893
- * Map tree refs to actual refs
2894
- * @private
2895
- */
2896
- this.valid = /* @__PURE__ */ new Map();
2897
- /**
2898
- * Variables
2899
- * @private
2900
- */
2901
- this.vars = [];
2884
+ // src/core/cross/serializer.ts
2885
+ var CrossSerializerContext = class extends BaseSerializerContext {
2886
+ constructor(options) {
2887
+ super(options);
2888
+ this.mode = "cross";
2889
+ this.scopeId = options.scopeId;
2902
2890
  }
2903
- /**
2904
- * Creates the reference param (identifier) from the given reference ID
2905
- * Calling this function means the value has been referenced somewhere
2906
- */
2907
- getRefParam(index) {
2908
- let actualIndex = this.valid.get(index);
2909
- if (actualIndex == null) {
2910
- actualIndex = this.valid.size;
2911
- this.valid.set(index, actualIndex);
2912
- }
2913
- let identifier = this.vars[actualIndex];
2914
- if (identifier == null) {
2915
- identifier = getIdentifier(actualIndex);
2916
- this.vars[actualIndex] = identifier;
2917
- }
2918
- return identifier;
2891
+ getRefParam(id) {
2892
+ return GLOBAL_CONTEXT_REFERENCES + "[" + id + "]";
2919
2893
  }
2920
2894
  assignIndexedValue(index, value) {
2921
- if (this.isMarked(index)) {
2922
- return this.getRefParam(index) + "=" + value;
2923
- }
2924
- return value;
2925
- }
2926
- serializePromiseConstructor(node) {
2927
- throw new SerovalUnsupportedNodeError(node);
2928
- }
2929
- serializePromiseResolve(node) {
2930
- throw new SerovalUnsupportedNodeError(node);
2931
- }
2932
- serializePromiseReject(node) {
2933
- throw new SerovalUnsupportedNodeError(node);
2895
+ return this.getRefParam(index) + "=" + value;
2934
2896
  }
2935
2897
  serializeTop(tree) {
2936
2898
  const result = this.serialize(tree);
2937
- if (tree.i != null && this.vars.length) {
2938
- const patches = this.resolvePatches();
2939
- let body = result;
2940
- if (patches) {
2941
- const index = this.getRefParam(tree.i);
2942
- body = result + "," + patches + index;
2943
- if (!result.startsWith(index + "=")) {
2944
- body = index + "=" + body;
2945
- }
2946
- }
2947
- return "(" + this.createFunction(this.vars, "(" + body + ")") + ")()";
2899
+ const id = tree.i;
2900
+ if (id == null) {
2901
+ return result;
2948
2902
  }
2949
- if (tree.t === 10 /* Object */) {
2950
- return "(" + result + ")";
2903
+ const patches = this.resolvePatches();
2904
+ const ref = this.getRefParam(id);
2905
+ const params = this.scopeId == null ? "" : GLOBAL_CONTEXT_REFERENCES;
2906
+ const body = patches ? result + "," + patches + ref : result;
2907
+ if (params === "") {
2908
+ return patches ? "(" + body + ")" : body;
2951
2909
  }
2952
- return result;
2910
+ const args = this.scopeId == null ? "()" : "(" + GLOBAL_CONTEXT_REFERENCES + '["' + serializeString(this.scopeId) + '"])';
2911
+ return "(" + this.createFunction([params], body) + ")" + args;
2953
2912
  }
2954
2913
  };
2955
2914
 
@@ -3093,6 +3052,7 @@ var BaseSyncParserContext = class extends BaseParserContext {
3093
3052
  parsePromise(id, _current) {
3094
3053
  return this.createPromiseConstructorNode(id);
3095
3054
  }
3055
+ // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: <explanation>
3096
3056
  parseObject(id, current) {
3097
3057
  if (Array.isArray(current)) {
3098
3058
  return this.parseArray(id, current);
@@ -3100,11 +3060,16 @@ var BaseSyncParserContext = class extends BaseParserContext {
3100
3060
  if (isStream(current)) {
3101
3061
  return this.parseStream(id, current);
3102
3062
  }
3063
+ const currentClass = current.constructor;
3064
+ if (currentClass === OpaqueReference) {
3065
+ return this.parse(
3066
+ current.replacement
3067
+ );
3068
+ }
3103
3069
  const parsed = this.parsePlugin(id, current);
3104
3070
  if (parsed) {
3105
3071
  return parsed;
3106
3072
  }
3107
- const currentClass = current.constructor;
3108
3073
  switch (currentClass) {
3109
3074
  case Object:
3110
3075
  return this.parsePlainObject(
@@ -3216,141 +3181,6 @@ var BaseSyncParserContext = class extends BaseParserContext {
3216
3181
  }
3217
3182
  };
3218
3183
 
3219
- // src/core/tree/sync.ts
3220
- var SyncParserContext = class extends BaseSyncParserContext {
3221
- constructor() {
3222
- super(...arguments);
3223
- this.mode = "vanilla";
3224
- }
3225
- };
3226
-
3227
- // src/core/tree/index.ts
3228
- function serialize(source, options = {}) {
3229
- const plugins = resolvePlugins(options.plugins);
3230
- const ctx = new SyncParserContext({
3231
- plugins,
3232
- disabledFeatures: options.disabledFeatures
3233
- });
3234
- const tree = ctx.parse(source);
3235
- const serial = new VanillaSerializerContext({
3236
- plugins,
3237
- features: ctx.features,
3238
- markedRefs: ctx.marked
3239
- });
3240
- return serial.serializeTop(tree);
3241
- }
3242
- async function serializeAsync(source, options = {}) {
3243
- const plugins = resolvePlugins(options.plugins);
3244
- const ctx = new AsyncParserContext({
3245
- plugins,
3246
- disabledFeatures: options.disabledFeatures
3247
- });
3248
- const tree = await ctx.parse(source);
3249
- const serial = new VanillaSerializerContext({
3250
- plugins,
3251
- features: ctx.features,
3252
- markedRefs: ctx.marked
3253
- });
3254
- return serial.serializeTop(tree);
3255
- }
3256
- function deserialize(source) {
3257
- return (0, eval)(source);
3258
- }
3259
- function toJSON(source, options = {}) {
3260
- const plugins = resolvePlugins(options.plugins);
3261
- const ctx = new SyncParserContext({
3262
- plugins,
3263
- disabledFeatures: options.disabledFeatures
3264
- });
3265
- return {
3266
- t: ctx.parse(source),
3267
- f: ctx.features,
3268
- m: Array.from(ctx.marked)
3269
- };
3270
- }
3271
- async function toJSONAsync(source, options = {}) {
3272
- const plugins = resolvePlugins(options.plugins);
3273
- const ctx = new AsyncParserContext({
3274
- plugins,
3275
- disabledFeatures: options.disabledFeatures
3276
- });
3277
- return {
3278
- t: await ctx.parse(source),
3279
- f: ctx.features,
3280
- m: Array.from(ctx.marked)
3281
- };
3282
- }
3283
- function compileJSON(source, options = {}) {
3284
- const plugins = resolvePlugins(options.plugins);
3285
- const ctx = new VanillaSerializerContext({
3286
- plugins,
3287
- features: source.f,
3288
- markedRefs: source.m
3289
- });
3290
- return ctx.serializeTop(source.t);
3291
- }
3292
- function fromJSON(source, options = {}) {
3293
- const plugins = resolvePlugins(options.plugins);
3294
- const ctx = new VanillaDeserializerContext({
3295
- plugins,
3296
- markedRefs: source.m
3297
- });
3298
- return ctx.deserialize(source.t);
3299
- }
3300
-
3301
- // src/core/cross/async.ts
3302
- var CrossAsyncParserContext = class extends BaseAsyncParserContext {
3303
- constructor() {
3304
- super(...arguments);
3305
- this.mode = "cross";
3306
- }
3307
- };
3308
-
3309
- // src/core/cross/deserializer.ts
3310
- var CrossDeserializerContext = class extends BaseDeserializerContext {
3311
- constructor() {
3312
- super(...arguments);
3313
- this.mode = "cross";
3314
- }
3315
- assignIndexedValue(index, value) {
3316
- if (!this.refs.has(index)) {
3317
- this.refs.set(index, value);
3318
- }
3319
- return value;
3320
- }
3321
- };
3322
-
3323
- // src/core/cross/serializer.ts
3324
- var CrossSerializerContext = class extends BaseSerializerContext {
3325
- constructor(options) {
3326
- super(options);
3327
- this.mode = "cross";
3328
- this.scopeId = options.scopeId;
3329
- }
3330
- getRefParam(id) {
3331
- return GLOBAL_CONTEXT_REFERENCES + "[" + id + "]";
3332
- }
3333
- assignIndexedValue(index, value) {
3334
- return this.getRefParam(index) + "=" + value;
3335
- }
3336
- serializeTop(tree) {
3337
- const result = this.serialize(tree);
3338
- const id = tree.i;
3339
- if (id == null) {
3340
- return result;
3341
- }
3342
- const patches = this.resolvePatches();
3343
- const ref = this.getRefParam(id);
3344
- const params = this.scopeId == null ? "" : GLOBAL_CONTEXT_REFERENCES;
3345
- const body = patches ? result + "," + patches + ref : result;
3346
- if (params === "") {
3347
- return patches ? "(" + body + ")" : body;
3348
- }
3349
- const args = this.scopeId == null ? "()" : "(" + GLOBAL_CONTEXT_REFERENCES + '["' + serializeString(this.scopeId) + '"])';
3350
- return "(" + this.createFunction([params], body) + ")" + args;
3351
- }
3352
- };
3353
-
3354
3184
  // src/core/context/parser/stream.ts
3355
3185
  var BaseStreamParserContext = class extends BaseSyncParserContext {
3356
3186
  constructor(options) {
@@ -3722,6 +3552,197 @@ function fromCrossJSON(source, options) {
3722
3552
  return ctx.deserialize(source);
3723
3553
  }
3724
3554
 
3555
+ // src/core/tree/async.ts
3556
+ var AsyncParserContext = class extends BaseAsyncParserContext {
3557
+ constructor() {
3558
+ super(...arguments);
3559
+ this.mode = "vanilla";
3560
+ }
3561
+ };
3562
+
3563
+ // src/core/tree/deserializer.ts
3564
+ var VanillaDeserializerContext = class extends BaseDeserializerContext {
3565
+ constructor(options) {
3566
+ super(options);
3567
+ this.mode = "vanilla";
3568
+ this.marked = new Set(options.markedRefs);
3569
+ }
3570
+ assignIndexedValue(index, value) {
3571
+ if (this.marked.has(index)) {
3572
+ this.refs.set(index, value);
3573
+ }
3574
+ return value;
3575
+ }
3576
+ };
3577
+
3578
+ // src/core/utils/get-identifier.ts
3579
+ var REF_START_CHARS = "hjkmoquxzABCDEFGHIJKLNPQRTUVWXYZ$_";
3580
+ var REF_START_CHARS_LEN = REF_START_CHARS.length;
3581
+ var REF_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$_";
3582
+ var REF_CHARS_LEN = REF_CHARS.length;
3583
+ function getIdentifier(index) {
3584
+ let mod = index % REF_START_CHARS_LEN;
3585
+ let ref = REF_START_CHARS[mod];
3586
+ index = (index - mod) / REF_START_CHARS_LEN;
3587
+ while (index > 0) {
3588
+ mod = index % REF_CHARS_LEN;
3589
+ ref += REF_CHARS[mod];
3590
+ index = (index - mod) / REF_CHARS_LEN;
3591
+ }
3592
+ return ref;
3593
+ }
3594
+
3595
+ // src/core/tree/serializer.ts
3596
+ var VanillaSerializerContext = class extends BaseSerializerContext {
3597
+ constructor() {
3598
+ super(...arguments);
3599
+ this.mode = "vanilla";
3600
+ /**
3601
+ * Map tree refs to actual refs
3602
+ * @private
3603
+ */
3604
+ this.valid = /* @__PURE__ */ new Map();
3605
+ /**
3606
+ * Variables
3607
+ * @private
3608
+ */
3609
+ this.vars = [];
3610
+ }
3611
+ /**
3612
+ * Creates the reference param (identifier) from the given reference ID
3613
+ * Calling this function means the value has been referenced somewhere
3614
+ */
3615
+ getRefParam(index) {
3616
+ let actualIndex = this.valid.get(index);
3617
+ if (actualIndex == null) {
3618
+ actualIndex = this.valid.size;
3619
+ this.valid.set(index, actualIndex);
3620
+ }
3621
+ let identifier = this.vars[actualIndex];
3622
+ if (identifier == null) {
3623
+ identifier = getIdentifier(actualIndex);
3624
+ this.vars[actualIndex] = identifier;
3625
+ }
3626
+ return identifier;
3627
+ }
3628
+ assignIndexedValue(index, value) {
3629
+ if (this.isMarked(index)) {
3630
+ return this.getRefParam(index) + "=" + value;
3631
+ }
3632
+ return value;
3633
+ }
3634
+ serializePromiseConstructor(node) {
3635
+ throw new SerovalUnsupportedNodeError(node);
3636
+ }
3637
+ serializePromiseResolve(node) {
3638
+ throw new SerovalUnsupportedNodeError(node);
3639
+ }
3640
+ serializePromiseReject(node) {
3641
+ throw new SerovalUnsupportedNodeError(node);
3642
+ }
3643
+ serializeTop(tree) {
3644
+ const result = this.serialize(tree);
3645
+ if (tree.i != null && this.vars.length) {
3646
+ const patches = this.resolvePatches();
3647
+ let body = result;
3648
+ if (patches) {
3649
+ const index = this.getRefParam(tree.i);
3650
+ body = result + "," + patches + index;
3651
+ if (!result.startsWith(index + "=")) {
3652
+ body = index + "=" + body;
3653
+ }
3654
+ }
3655
+ return "(" + this.createFunction(this.vars, "(" + body + ")") + ")()";
3656
+ }
3657
+ if (tree.t === 10 /* Object */) {
3658
+ return "(" + result + ")";
3659
+ }
3660
+ return result;
3661
+ }
3662
+ };
3663
+
3664
+ // src/core/tree/sync.ts
3665
+ var SyncParserContext = class extends BaseSyncParserContext {
3666
+ constructor() {
3667
+ super(...arguments);
3668
+ this.mode = "vanilla";
3669
+ }
3670
+ };
3671
+
3672
+ // src/core/tree/index.ts
3673
+ function serialize(source, options = {}) {
3674
+ const plugins = resolvePlugins(options.plugins);
3675
+ const ctx = new SyncParserContext({
3676
+ plugins,
3677
+ disabledFeatures: options.disabledFeatures
3678
+ });
3679
+ const tree = ctx.parse(source);
3680
+ const serial = new VanillaSerializerContext({
3681
+ plugins,
3682
+ features: ctx.features,
3683
+ markedRefs: ctx.marked
3684
+ });
3685
+ return serial.serializeTop(tree);
3686
+ }
3687
+ async function serializeAsync(source, options = {}) {
3688
+ const plugins = resolvePlugins(options.plugins);
3689
+ const ctx = new AsyncParserContext({
3690
+ plugins,
3691
+ disabledFeatures: options.disabledFeatures
3692
+ });
3693
+ const tree = await ctx.parse(source);
3694
+ const serial = new VanillaSerializerContext({
3695
+ plugins,
3696
+ features: ctx.features,
3697
+ markedRefs: ctx.marked
3698
+ });
3699
+ return serial.serializeTop(tree);
3700
+ }
3701
+ function deserialize(source) {
3702
+ return (0, eval)(source);
3703
+ }
3704
+ function toJSON(source, options = {}) {
3705
+ const plugins = resolvePlugins(options.plugins);
3706
+ const ctx = new SyncParserContext({
3707
+ plugins,
3708
+ disabledFeatures: options.disabledFeatures
3709
+ });
3710
+ return {
3711
+ t: ctx.parse(source),
3712
+ f: ctx.features,
3713
+ m: Array.from(ctx.marked)
3714
+ };
3715
+ }
3716
+ async function toJSONAsync(source, options = {}) {
3717
+ const plugins = resolvePlugins(options.plugins);
3718
+ const ctx = new AsyncParserContext({
3719
+ plugins,
3720
+ disabledFeatures: options.disabledFeatures
3721
+ });
3722
+ return {
3723
+ t: await ctx.parse(source),
3724
+ f: ctx.features,
3725
+ m: Array.from(ctx.marked)
3726
+ };
3727
+ }
3728
+ function compileJSON(source, options = {}) {
3729
+ const plugins = resolvePlugins(options.plugins);
3730
+ const ctx = new VanillaSerializerContext({
3731
+ plugins,
3732
+ features: source.f,
3733
+ markedRefs: source.m
3734
+ });
3735
+ return ctx.serializeTop(source.t);
3736
+ }
3737
+ function fromJSON(source, options = {}) {
3738
+ const plugins = resolvePlugins(options.plugins);
3739
+ const ctx = new VanillaDeserializerContext({
3740
+ plugins,
3741
+ markedRefs: source.m
3742
+ });
3743
+ return ctx.deserialize(source.t);
3744
+ }
3745
+
3725
3746
  // src/core/Serializer.ts
3726
3747
  var Serializer = class {
3727
3748
  constructor(options) {