seroval 1.0.7 → 1.1.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.
@@ -863,6 +863,14 @@ var SerovalUnknownTypedArrayError = class extends Error {
863
863
  }
864
864
  };
865
865
 
866
+ // src/core/opaque-reference.ts
867
+ var OpaqueReference = class {
868
+ constructor(value, replacement) {
869
+ this.value = value;
870
+ this.replacement = replacement;
871
+ }
872
+ };
873
+
866
874
  // src/core/special-reference.ts
867
875
  var ITERATOR = {};
868
876
  var ASYNC_ITERATOR = {};
@@ -1503,6 +1511,7 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1503
1511
  })
1504
1512
  );
1505
1513
  }
1514
+ // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: <explanation>
1506
1515
  async parseObject(id, current) {
1507
1516
  if (Array.isArray(current)) {
1508
1517
  return this.parseArray(id, current);
@@ -1510,11 +1519,16 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1510
1519
  if (isStream(current)) {
1511
1520
  return this.parseStream(id, current);
1512
1521
  }
1522
+ const currentClass = current.constructor;
1523
+ if (currentClass === OpaqueReference) {
1524
+ return this.parse(
1525
+ current.replacement
1526
+ );
1527
+ }
1513
1528
  const parsed = await this.parsePlugin(id, current);
1514
1529
  if (parsed) {
1515
1530
  return parsed;
1516
1531
  }
1517
- const currentClass = current.constructor;
1518
1532
  switch (currentClass) {
1519
1533
  case Object:
1520
1534
  return this.parsePlainObject(
@@ -1626,11 +1640,11 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1626
1640
  }
1627
1641
  };
1628
1642
 
1629
- // src/core/tree/async.ts
1630
- var AsyncParserContext = class extends BaseAsyncParserContext {
1643
+ // src/core/cross/async.ts
1644
+ var CrossAsyncParserContext = class extends BaseAsyncParserContext {
1631
1645
  constructor() {
1632
1646
  super(...arguments);
1633
- this.mode = "vanilla";
1647
+ this.mode = "cross";
1634
1648
  }
1635
1649
  };
1636
1650
 
@@ -1970,15 +1984,14 @@ var BaseDeserializerContext = class {
1970
1984
  }
1971
1985
  };
1972
1986
 
1973
- // src/core/tree/deserializer.ts
1974
- var VanillaDeserializerContext = class extends BaseDeserializerContext {
1975
- constructor(options) {
1976
- super(options);
1977
- this.mode = "vanilla";
1978
- this.marked = new Set(options.markedRefs);
1987
+ // src/core/cross/deserializer.ts
1988
+ var CrossDeserializerContext = class extends BaseDeserializerContext {
1989
+ constructor() {
1990
+ super(...arguments);
1991
+ this.mode = "cross";
1979
1992
  }
1980
1993
  assignIndexedValue(index, value) {
1981
- if (this.marked.has(index)) {
1994
+ if (!this.refs.has(index)) {
1982
1995
  this.refs.set(index, value);
1983
1996
  }
1984
1997
  return value;
@@ -2810,89 +2823,34 @@ var BaseSerializerContext = class {
2810
2823
  }
2811
2824
  };
2812
2825
 
2813
- // src/core/utils/get-identifier.ts
2814
- var REF_START_CHARS = "hjkmoquxzABCDEFGHIJKLNPQRTUVWXYZ$_";
2815
- var REF_START_CHARS_LEN = REF_START_CHARS.length;
2816
- var REF_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$_";
2817
- var REF_CHARS_LEN = REF_CHARS.length;
2818
- function getIdentifier(index) {
2819
- let mod = index % REF_START_CHARS_LEN;
2820
- let ref = REF_START_CHARS[mod];
2821
- index = (index - mod) / REF_START_CHARS_LEN;
2822
- while (index > 0) {
2823
- mod = index % REF_CHARS_LEN;
2824
- ref += REF_CHARS[mod];
2825
- index = (index - mod) / REF_CHARS_LEN;
2826
- }
2827
- return ref;
2828
- }
2829
-
2830
- // src/core/tree/serializer.ts
2831
- var VanillaSerializerContext = class extends BaseSerializerContext {
2832
- constructor() {
2833
- super(...arguments);
2834
- this.mode = "vanilla";
2835
- /**
2836
- * Map tree refs to actual refs
2837
- * @private
2838
- */
2839
- this.valid = /* @__PURE__ */ new Map();
2840
- /**
2841
- * Variables
2842
- * @private
2843
- */
2844
- this.vars = [];
2826
+ // src/core/cross/serializer.ts
2827
+ var CrossSerializerContext = class extends BaseSerializerContext {
2828
+ constructor(options) {
2829
+ super(options);
2830
+ this.mode = "cross";
2831
+ this.scopeId = options.scopeId;
2845
2832
  }
2846
- /**
2847
- * Creates the reference param (identifier) from the given reference ID
2848
- * Calling this function means the value has been referenced somewhere
2849
- */
2850
- getRefParam(index) {
2851
- let actualIndex = this.valid.get(index);
2852
- if (actualIndex == null) {
2853
- actualIndex = this.valid.size;
2854
- this.valid.set(index, actualIndex);
2855
- }
2856
- let identifier = this.vars[actualIndex];
2857
- if (identifier == null) {
2858
- identifier = getIdentifier(actualIndex);
2859
- this.vars[actualIndex] = identifier;
2860
- }
2861
- return identifier;
2833
+ getRefParam(id) {
2834
+ return GLOBAL_CONTEXT_REFERENCES + "[" + id + "]";
2862
2835
  }
2863
2836
  assignIndexedValue(index, value) {
2864
- if (this.isMarked(index)) {
2865
- return this.getRefParam(index) + "=" + value;
2866
- }
2867
- return value;
2868
- }
2869
- serializePromiseConstructor(node) {
2870
- throw new SerovalUnsupportedNodeError(node);
2871
- }
2872
- serializePromiseResolve(node) {
2873
- throw new SerovalUnsupportedNodeError(node);
2874
- }
2875
- serializePromiseReject(node) {
2876
- throw new SerovalUnsupportedNodeError(node);
2837
+ return this.getRefParam(index) + "=" + value;
2877
2838
  }
2878
2839
  serializeTop(tree) {
2879
2840
  const result = this.serialize(tree);
2880
- if (tree.i != null && this.vars.length) {
2881
- const patches = this.resolvePatches();
2882
- let body = result;
2883
- if (patches) {
2884
- const index = this.getRefParam(tree.i);
2885
- body = result + "," + patches + index;
2886
- if (!result.startsWith(index + "=")) {
2887
- body = index + "=" + body;
2888
- }
2889
- }
2890
- return "(" + this.createFunction(this.vars, "(" + body + ")") + ")()";
2841
+ const id = tree.i;
2842
+ if (id == null) {
2843
+ return result;
2891
2844
  }
2892
- if (tree.t === 10 /* Object */) {
2893
- return "(" + result + ")";
2845
+ const patches = this.resolvePatches();
2846
+ const ref = this.getRefParam(id);
2847
+ const params = this.scopeId == null ? "" : GLOBAL_CONTEXT_REFERENCES;
2848
+ const body = patches ? result + "," + patches + ref : result;
2849
+ if (params === "") {
2850
+ return patches ? "(" + body + ")" : body;
2894
2851
  }
2895
- return result;
2852
+ const args = this.scopeId == null ? "()" : "(" + GLOBAL_CONTEXT_REFERENCES + '["' + serializeString(this.scopeId) + '"])';
2853
+ return "(" + this.createFunction([params], body) + ")" + args;
2896
2854
  }
2897
2855
  };
2898
2856
 
@@ -3036,6 +2994,7 @@ var BaseSyncParserContext = class extends BaseParserContext {
3036
2994
  parsePromise(id, _current) {
3037
2995
  return this.createPromiseConstructorNode(id);
3038
2996
  }
2997
+ // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: <explanation>
3039
2998
  parseObject(id, current) {
3040
2999
  if (Array.isArray(current)) {
3041
3000
  return this.parseArray(id, current);
@@ -3043,11 +3002,16 @@ var BaseSyncParserContext = class extends BaseParserContext {
3043
3002
  if (isStream(current)) {
3044
3003
  return this.parseStream(id, current);
3045
3004
  }
3005
+ const currentClass = current.constructor;
3006
+ if (currentClass === OpaqueReference) {
3007
+ return this.parse(
3008
+ current.replacement
3009
+ );
3010
+ }
3046
3011
  const parsed = this.parsePlugin(id, current);
3047
3012
  if (parsed) {
3048
3013
  return parsed;
3049
3014
  }
3050
- const currentClass = current.constructor;
3051
3015
  switch (currentClass) {
3052
3016
  case Object:
3053
3017
  return this.parsePlainObject(
@@ -3159,141 +3123,6 @@ var BaseSyncParserContext = class extends BaseParserContext {
3159
3123
  }
3160
3124
  };
3161
3125
 
3162
- // src/core/tree/sync.ts
3163
- var SyncParserContext = class extends BaseSyncParserContext {
3164
- constructor() {
3165
- super(...arguments);
3166
- this.mode = "vanilla";
3167
- }
3168
- };
3169
-
3170
- // src/core/tree/index.ts
3171
- function serialize(source, options = {}) {
3172
- const plugins = resolvePlugins(options.plugins);
3173
- const ctx = new SyncParserContext({
3174
- plugins,
3175
- disabledFeatures: options.disabledFeatures
3176
- });
3177
- const tree = ctx.parse(source);
3178
- const serial = new VanillaSerializerContext({
3179
- plugins,
3180
- features: ctx.features,
3181
- markedRefs: ctx.marked
3182
- });
3183
- return serial.serializeTop(tree);
3184
- }
3185
- async function serializeAsync(source, options = {}) {
3186
- const plugins = resolvePlugins(options.plugins);
3187
- const ctx = new AsyncParserContext({
3188
- plugins,
3189
- disabledFeatures: options.disabledFeatures
3190
- });
3191
- const tree = await ctx.parse(source);
3192
- const serial = new VanillaSerializerContext({
3193
- plugins,
3194
- features: ctx.features,
3195
- markedRefs: ctx.marked
3196
- });
3197
- return serial.serializeTop(tree);
3198
- }
3199
- function deserialize(source) {
3200
- return (0, eval)(source);
3201
- }
3202
- function toJSON(source, options = {}) {
3203
- const plugins = resolvePlugins(options.plugins);
3204
- const ctx = new SyncParserContext({
3205
- plugins,
3206
- disabledFeatures: options.disabledFeatures
3207
- });
3208
- return {
3209
- t: ctx.parse(source),
3210
- f: ctx.features,
3211
- m: Array.from(ctx.marked)
3212
- };
3213
- }
3214
- async function toJSONAsync(source, options = {}) {
3215
- const plugins = resolvePlugins(options.plugins);
3216
- const ctx = new AsyncParserContext({
3217
- plugins,
3218
- disabledFeatures: options.disabledFeatures
3219
- });
3220
- return {
3221
- t: await ctx.parse(source),
3222
- f: ctx.features,
3223
- m: Array.from(ctx.marked)
3224
- };
3225
- }
3226
- function compileJSON(source, options = {}) {
3227
- const plugins = resolvePlugins(options.plugins);
3228
- const ctx = new VanillaSerializerContext({
3229
- plugins,
3230
- features: source.f,
3231
- markedRefs: source.m
3232
- });
3233
- return ctx.serializeTop(source.t);
3234
- }
3235
- function fromJSON(source, options = {}) {
3236
- const plugins = resolvePlugins(options.plugins);
3237
- const ctx = new VanillaDeserializerContext({
3238
- plugins,
3239
- markedRefs: source.m
3240
- });
3241
- return ctx.deserialize(source.t);
3242
- }
3243
-
3244
- // src/core/cross/async.ts
3245
- var CrossAsyncParserContext = class extends BaseAsyncParserContext {
3246
- constructor() {
3247
- super(...arguments);
3248
- this.mode = "cross";
3249
- }
3250
- };
3251
-
3252
- // src/core/cross/deserializer.ts
3253
- var CrossDeserializerContext = class extends BaseDeserializerContext {
3254
- constructor() {
3255
- super(...arguments);
3256
- this.mode = "cross";
3257
- }
3258
- assignIndexedValue(index, value) {
3259
- if (!this.refs.has(index)) {
3260
- this.refs.set(index, value);
3261
- }
3262
- return value;
3263
- }
3264
- };
3265
-
3266
- // src/core/cross/serializer.ts
3267
- var CrossSerializerContext = class extends BaseSerializerContext {
3268
- constructor(options) {
3269
- super(options);
3270
- this.mode = "cross";
3271
- this.scopeId = options.scopeId;
3272
- }
3273
- getRefParam(id) {
3274
- return GLOBAL_CONTEXT_REFERENCES + "[" + id + "]";
3275
- }
3276
- assignIndexedValue(index, value) {
3277
- return this.getRefParam(index) + "=" + value;
3278
- }
3279
- serializeTop(tree) {
3280
- const result = this.serialize(tree);
3281
- const id = tree.i;
3282
- if (id == null) {
3283
- return result;
3284
- }
3285
- const patches = this.resolvePatches();
3286
- const ref = this.getRefParam(id);
3287
- const params = this.scopeId == null ? "" : GLOBAL_CONTEXT_REFERENCES;
3288
- const body = patches ? result + "," + patches + ref : result;
3289
- if (params === "") {
3290
- return patches ? "(" + body + ")" : body;
3291
- }
3292
- const args = this.scopeId == null ? "()" : "(" + GLOBAL_CONTEXT_REFERENCES + '["' + serializeString(this.scopeId) + '"])';
3293
- return "(" + this.createFunction([params], body) + ")" + args;
3294
- }
3295
- };
3296
-
3297
3126
  // src/core/context/parser/stream.ts
3298
3127
  var BaseStreamParserContext = class extends BaseSyncParserContext {
3299
3128
  constructor(options) {
@@ -3665,6 +3494,197 @@ function fromCrossJSON(source, options) {
3665
3494
  return ctx.deserialize(source);
3666
3495
  }
3667
3496
 
3497
+ // src/core/tree/async.ts
3498
+ var AsyncParserContext = class extends BaseAsyncParserContext {
3499
+ constructor() {
3500
+ super(...arguments);
3501
+ this.mode = "vanilla";
3502
+ }
3503
+ };
3504
+
3505
+ // src/core/tree/deserializer.ts
3506
+ var VanillaDeserializerContext = class extends BaseDeserializerContext {
3507
+ constructor(options) {
3508
+ super(options);
3509
+ this.mode = "vanilla";
3510
+ this.marked = new Set(options.markedRefs);
3511
+ }
3512
+ assignIndexedValue(index, value) {
3513
+ if (this.marked.has(index)) {
3514
+ this.refs.set(index, value);
3515
+ }
3516
+ return value;
3517
+ }
3518
+ };
3519
+
3520
+ // src/core/utils/get-identifier.ts
3521
+ var REF_START_CHARS = "hjkmoquxzABCDEFGHIJKLNPQRTUVWXYZ$_";
3522
+ var REF_START_CHARS_LEN = REF_START_CHARS.length;
3523
+ var REF_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$_";
3524
+ var REF_CHARS_LEN = REF_CHARS.length;
3525
+ function getIdentifier(index) {
3526
+ let mod = index % REF_START_CHARS_LEN;
3527
+ let ref = REF_START_CHARS[mod];
3528
+ index = (index - mod) / REF_START_CHARS_LEN;
3529
+ while (index > 0) {
3530
+ mod = index % REF_CHARS_LEN;
3531
+ ref += REF_CHARS[mod];
3532
+ index = (index - mod) / REF_CHARS_LEN;
3533
+ }
3534
+ return ref;
3535
+ }
3536
+
3537
+ // src/core/tree/serializer.ts
3538
+ var VanillaSerializerContext = class extends BaseSerializerContext {
3539
+ constructor() {
3540
+ super(...arguments);
3541
+ this.mode = "vanilla";
3542
+ /**
3543
+ * Map tree refs to actual refs
3544
+ * @private
3545
+ */
3546
+ this.valid = /* @__PURE__ */ new Map();
3547
+ /**
3548
+ * Variables
3549
+ * @private
3550
+ */
3551
+ this.vars = [];
3552
+ }
3553
+ /**
3554
+ * Creates the reference param (identifier) from the given reference ID
3555
+ * Calling this function means the value has been referenced somewhere
3556
+ */
3557
+ getRefParam(index) {
3558
+ let actualIndex = this.valid.get(index);
3559
+ if (actualIndex == null) {
3560
+ actualIndex = this.valid.size;
3561
+ this.valid.set(index, actualIndex);
3562
+ }
3563
+ let identifier = this.vars[actualIndex];
3564
+ if (identifier == null) {
3565
+ identifier = getIdentifier(actualIndex);
3566
+ this.vars[actualIndex] = identifier;
3567
+ }
3568
+ return identifier;
3569
+ }
3570
+ assignIndexedValue(index, value) {
3571
+ if (this.isMarked(index)) {
3572
+ return this.getRefParam(index) + "=" + value;
3573
+ }
3574
+ return value;
3575
+ }
3576
+ serializePromiseConstructor(node) {
3577
+ throw new SerovalUnsupportedNodeError(node);
3578
+ }
3579
+ serializePromiseResolve(node) {
3580
+ throw new SerovalUnsupportedNodeError(node);
3581
+ }
3582
+ serializePromiseReject(node) {
3583
+ throw new SerovalUnsupportedNodeError(node);
3584
+ }
3585
+ serializeTop(tree) {
3586
+ const result = this.serialize(tree);
3587
+ if (tree.i != null && this.vars.length) {
3588
+ const patches = this.resolvePatches();
3589
+ let body = result;
3590
+ if (patches) {
3591
+ const index = this.getRefParam(tree.i);
3592
+ body = result + "," + patches + index;
3593
+ if (!result.startsWith(index + "=")) {
3594
+ body = index + "=" + body;
3595
+ }
3596
+ }
3597
+ return "(" + this.createFunction(this.vars, "(" + body + ")") + ")()";
3598
+ }
3599
+ if (tree.t === 10 /* Object */) {
3600
+ return "(" + result + ")";
3601
+ }
3602
+ return result;
3603
+ }
3604
+ };
3605
+
3606
+ // src/core/tree/sync.ts
3607
+ var SyncParserContext = class extends BaseSyncParserContext {
3608
+ constructor() {
3609
+ super(...arguments);
3610
+ this.mode = "vanilla";
3611
+ }
3612
+ };
3613
+
3614
+ // src/core/tree/index.ts
3615
+ function serialize(source, options = {}) {
3616
+ const plugins = resolvePlugins(options.plugins);
3617
+ const ctx = new SyncParserContext({
3618
+ plugins,
3619
+ disabledFeatures: options.disabledFeatures
3620
+ });
3621
+ const tree = ctx.parse(source);
3622
+ const serial = new VanillaSerializerContext({
3623
+ plugins,
3624
+ features: ctx.features,
3625
+ markedRefs: ctx.marked
3626
+ });
3627
+ return serial.serializeTop(tree);
3628
+ }
3629
+ async function serializeAsync(source, options = {}) {
3630
+ const plugins = resolvePlugins(options.plugins);
3631
+ const ctx = new AsyncParserContext({
3632
+ plugins,
3633
+ disabledFeatures: options.disabledFeatures
3634
+ });
3635
+ const tree = await ctx.parse(source);
3636
+ const serial = new VanillaSerializerContext({
3637
+ plugins,
3638
+ features: ctx.features,
3639
+ markedRefs: ctx.marked
3640
+ });
3641
+ return serial.serializeTop(tree);
3642
+ }
3643
+ function deserialize(source) {
3644
+ return (0, eval)(source);
3645
+ }
3646
+ function toJSON(source, options = {}) {
3647
+ const plugins = resolvePlugins(options.plugins);
3648
+ const ctx = new SyncParserContext({
3649
+ plugins,
3650
+ disabledFeatures: options.disabledFeatures
3651
+ });
3652
+ return {
3653
+ t: ctx.parse(source),
3654
+ f: ctx.features,
3655
+ m: Array.from(ctx.marked)
3656
+ };
3657
+ }
3658
+ async function toJSONAsync(source, options = {}) {
3659
+ const plugins = resolvePlugins(options.plugins);
3660
+ const ctx = new AsyncParserContext({
3661
+ plugins,
3662
+ disabledFeatures: options.disabledFeatures
3663
+ });
3664
+ return {
3665
+ t: await ctx.parse(source),
3666
+ f: ctx.features,
3667
+ m: Array.from(ctx.marked)
3668
+ };
3669
+ }
3670
+ function compileJSON(source, options = {}) {
3671
+ const plugins = resolvePlugins(options.plugins);
3672
+ const ctx = new VanillaSerializerContext({
3673
+ plugins,
3674
+ features: source.f,
3675
+ markedRefs: source.m
3676
+ });
3677
+ return ctx.serializeTop(source.t);
3678
+ }
3679
+ function fromJSON(source, options = {}) {
3680
+ const plugins = resolvePlugins(options.plugins);
3681
+ const ctx = new VanillaDeserializerContext({
3682
+ plugins,
3683
+ markedRefs: source.m
3684
+ });
3685
+ return ctx.deserialize(source.t);
3686
+ }
3687
+
3668
3688
  // src/core/Serializer.ts
3669
3689
  var Serializer = class {
3670
3690
  constructor(options) {
@@ -3745,6 +3765,7 @@ var Serializer = class {
3745
3765
  };
3746
3766
  export {
3747
3767
  Feature,
3768
+ OpaqueReference,
3748
3769
  Serializer,
3749
3770
  SerovalDeserializationError,
3750
3771
  SerovalError,