seroval 0.10.1 → 0.10.3

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.
@@ -180,13 +180,6 @@ function getRefParam(ctx, index) {
180
180
  }
181
181
  return identifier;
182
182
  }
183
- function getRootID(ctx, current) {
184
- const ref = ctx.reference.ids.get(current);
185
- if (ref == null) {
186
- return ctx.reference.ids.size;
187
- }
188
- return ref;
189
- }
190
183
  function createIndexedValue(ctx, current) {
191
184
  const ref = ctx.reference.ids.get(current);
192
185
  if (ref == null) {
@@ -290,26 +283,23 @@ var NAN_NODE = createConstantNode(7 /* NaN */);
290
283
 
291
284
  // src/core/keys.ts
292
285
  var REFERENCES_KEY = "__SEROVAL_REFS__";
286
+ var GLOBAL_CONTEXT_API = "_$";
293
287
  var GLOBAL_CONTEXT_REFERENCES = "$R";
294
288
  var LOCAL_CONTEXT_PROMISE_RESOLVE = "s";
295
289
  var LOCAL_CONTEXT_PROMISE_REJECT = "f";
296
- var GLOBAL_CONTEXT_PROMISE_CONSTRUCTOR = "$P";
297
- var GLOBAL_CONTEXT_PROMISE_RESOLVE = "$Ps";
298
- var GLOBAL_CONTEXT_PROMISE_REJECT = "$Pf";
290
+ var GLOBAL_CONTEXT_PROMISE_CONSTRUCTOR = "P";
291
+ var GLOBAL_CONTEXT_PROMISE_RESOLVE = "Ps";
292
+ var GLOBAL_CONTEXT_PROMISE_REJECT = "Pf";
299
293
  var LOCAL_CONTEXT_STREAM_CONTROLLER = "c";
300
- var GLOBAL_CONTEXT_STREAM_CONSTRUCTOR = "$S";
301
- var GLOBAL_CONTEXT_STREAM_EMIT = "$Se";
302
- var ROOT_REFERENCE = "t";
303
- var CROSS_REFERENCE_HEADER = `
304
- var ${GLOBAL_CONTEXT_REFERENCES}=[];
305
- function ${GLOBAL_CONTEXT_PROMISE_CONSTRUCTOR}(s,f,p){return (p=new Promise(function(a,b){s=a,f=b})).${LOCAL_CONTEXT_PROMISE_RESOLVE}=s,p.${LOCAL_CONTEXT_PROMISE_REJECT}=f,p}
306
- function $uP(i,p){delete (p=${GLOBAL_CONTEXT_REFERENCES}[i]).${LOCAL_CONTEXT_PROMISE_RESOLVE};delete p.${LOCAL_CONTEXT_PROMISE_REJECT}}
307
- function ${GLOBAL_CONTEXT_PROMISE_RESOLVE}(i,d){${GLOBAL_CONTEXT_REFERENCES}[i].${LOCAL_CONTEXT_PROMISE_RESOLVE}(d),$uP(i)}
308
- function ${GLOBAL_CONTEXT_PROMISE_REJECT}(i,d){${GLOBAL_CONTEXT_REFERENCES}[i].${LOCAL_CONTEXT_PROMISE_REJECT}(d),$uP(i)}
309
- function $uS(s){delete s.${LOCAL_CONTEXT_STREAM_CONTROLLER}}
310
- function ${GLOBAL_CONTEXT_STREAM_EMIT}(i,t,d,s,c){switch(c=(s=${GLOBAL_CONTEXT_REFERENCES}[i]).${LOCAL_CONTEXT_STREAM_CONTROLLER},t){case 0:return c.enqueue(d);case 1:return c.error(d),$uS(s);case 2:return c.close(),$uS(s)}}
311
- function ${GLOBAL_CONTEXT_STREAM_CONSTRUCTOR}(s,c){return(s=new ReadableStream({start:function(t){c=t}})).c=c,s}
312
- `;
294
+ var GLOBAL_CONTEXT_STREAM_CONSTRUCTOR = "S";
295
+ var GLOBAL_CONTEXT_STREAM_EMIT = "Se";
296
+ var GLOBAL_CONTEXT_API_SCRIPT = `self.${GLOBAL_CONTEXT_API}=self.${GLOBAL_CONTEXT_API}||{${GLOBAL_CONTEXT_PROMISE_CONSTRUCTOR}:function(s,f,p){return(p=new Promise(function(a,b){s=a,f=b})).${LOCAL_CONTEXT_PROMISE_RESOLVE}=s,p.${LOCAL_CONTEXT_PROMISE_REJECT}=f,p},uP:function(p){delete p.${LOCAL_CONTEXT_PROMISE_RESOLVE};delete p.${LOCAL_CONTEXT_PROMISE_REJECT}},${GLOBAL_CONTEXT_PROMISE_RESOLVE}:function(p,d){p.${LOCAL_CONTEXT_PROMISE_RESOLVE}(d),p.value=d,this.uP(p)},${GLOBAL_CONTEXT_PROMISE_REJECT}:function(p,d){p.${LOCAL_CONTEXT_PROMISE_REJECT}(d),this.uP(p)},uS:function(s){delete s.${LOCAL_CONTEXT_STREAM_CONTROLLER}},${GLOBAL_CONTEXT_STREAM_EMIT}:function(s,t,d,c){switch(c=s.${LOCAL_CONTEXT_STREAM_CONTROLLER},t){case 0:return c.enqueue(d);case 1:return(this.uS(s),c.error(d));case 2:return(this.uS(s),c.close())}},${GLOBAL_CONTEXT_STREAM_CONSTRUCTOR}:function(s,c){return(s=new ReadableStream({start:function(x){c=x}})).${LOCAL_CONTEXT_STREAM_CONTROLLER}=c,s},}`;
297
+ function getCrossReferenceHeader(id) {
298
+ if (id == null) {
299
+ return `self.${GLOBAL_CONTEXT_REFERENCES}=self.${GLOBAL_CONTEXT_REFERENCES}||[];`;
300
+ }
301
+ return `(self.${GLOBAL_CONTEXT_REFERENCES}=self.${GLOBAL_CONTEXT_REFERENCES}||{})["${serializeString(id)}"]=[]`;
302
+ }
313
303
 
314
304
  // src/core/reference.ts
315
305
  var REFERENCE = /* @__PURE__ */ new Map();
@@ -2631,7 +2621,7 @@ function parseSync(ctx, current) {
2631
2621
 
2632
2622
  // src/core/tree/index.ts
2633
2623
  function finalize(ctx, rootID, isObject, result) {
2634
- if (ctx.vars.length) {
2624
+ if (rootID != null && ctx.vars.length) {
2635
2625
  const patches = resolvePatches(ctx);
2636
2626
  let body = result;
2637
2627
  if (patches) {
@@ -2663,7 +2653,7 @@ function serialize(source, options) {
2663
2653
  const result = serializeTree(serial, tree);
2664
2654
  return finalize(
2665
2655
  serial,
2666
- getRootID(ctx, source),
2656
+ tree.i,
2667
2657
  tree.t === 10 /* Object */,
2668
2658
  result
2669
2659
  );
@@ -2678,7 +2668,7 @@ async function serializeAsync(source, options) {
2678
2668
  const result = serializeTree(serial, tree);
2679
2669
  return finalize(
2680
2670
  serial,
2681
- getRootID(ctx, source),
2671
+ tree.i,
2682
2672
  tree.t === 10 /* Object */,
2683
2673
  result
2684
2674
  );
@@ -2690,7 +2680,6 @@ function toJSON(source, options) {
2690
2680
  const ctx = createParserContext(options);
2691
2681
  return {
2692
2682
  t: parseSync(ctx, source),
2693
- r: getRootID(ctx, source),
2694
2683
  f: ctx.features,
2695
2684
  m: Array.from(ctx.reference.marked)
2696
2685
  };
@@ -2699,7 +2688,6 @@ async function toJSONAsync(source, options) {
2699
2688
  const ctx = createParserContext(options);
2700
2689
  return {
2701
2690
  t: await parseAsync(ctx, source),
2702
- r: getRootID(ctx, source),
2703
2691
  f: ctx.features,
2704
2692
  m: Array.from(ctx.reference.marked)
2705
2693
  };
@@ -2710,7 +2698,7 @@ function compileJSON(source) {
2710
2698
  markedRefs: source.m
2711
2699
  });
2712
2700
  const result = serializeTree(serial, source.t);
2713
- return finalize(serial, source.r, source.t.i === 10 /* Object */, result);
2701
+ return finalize(serial, source.t.i, source.t.i === 10 /* Object */, result);
2714
2702
  }
2715
2703
  function fromJSON(source) {
2716
2704
  const serial = createDeserializerContext({
@@ -2722,6 +2710,7 @@ function fromJSON(source) {
2722
2710
  // src/core/cross/context.ts
2723
2711
  function createCrossParserContext(options = {}) {
2724
2712
  return {
2713
+ scopeId: options.scopeId,
2725
2714
  refs: options.refs || /* @__PURE__ */ new Map(),
2726
2715
  features: ALL_ENABLED ^ (options.disabledFeatures || 0)
2727
2716
  };
@@ -2738,11 +2727,26 @@ function createCrossIndexedValue(ctx, current) {
2738
2727
  function createStreamingCrossParserContext(options) {
2739
2728
  return {
2740
2729
  alive: true,
2730
+ pending: 0,
2741
2731
  refs: options.refs || /* @__PURE__ */ new Map(),
2742
2732
  features: ALL_ENABLED ^ (options.disabledFeatures || 0),
2743
- onParse: options.onParse
2733
+ onParse: options.onParse,
2734
+ onDone() {
2735
+ if (options.onDone) {
2736
+ options.onDone();
2737
+ }
2738
+ },
2739
+ scopeId: options.scopeId
2744
2740
  };
2745
2741
  }
2742
+ function pushPendingState(ctx) {
2743
+ ctx.pending++;
2744
+ }
2745
+ function popPendingState(ctx) {
2746
+ if (--ctx.pending <= 0) {
2747
+ ctx.onDone();
2748
+ }
2749
+ }
2746
2750
  function createCrossSerializerContext(options) {
2747
2751
  return {
2748
2752
  stack: [],
@@ -3782,25 +3786,25 @@ function serializeWKSymbol2(node) {
3782
3786
  return assignIndexedValue3(node.i, SYMBOL_STRING[node.s]);
3783
3787
  }
3784
3788
  function serializePromiseResolve(ctx, node) {
3785
- return GLOBAL_CONTEXT_PROMISE_RESOLVE + "(" + node.i + "," + crossSerializeTree(ctx, node.f) + ")";
3789
+ return GLOBAL_CONTEXT_API + "." + GLOBAL_CONTEXT_PROMISE_RESOLVE + "(" + getRefExpr(node.i) + "," + crossSerializeTree(ctx, node.f) + ")";
3786
3790
  }
3787
3791
  function serializePromiseReject(ctx, node) {
3788
- return GLOBAL_CONTEXT_PROMISE_REJECT + "(" + node.i + "," + crossSerializeTree(ctx, node.f) + ")";
3792
+ return GLOBAL_CONTEXT_API + "." + GLOBAL_CONTEXT_PROMISE_REJECT + "(" + getRefExpr(node.i) + "," + crossSerializeTree(ctx, node.f) + ")";
3789
3793
  }
3790
3794
  function serializePromiseConstructor(node) {
3791
- return assignIndexedValue3(node.i, GLOBAL_CONTEXT_PROMISE_CONSTRUCTOR + "()");
3795
+ return assignIndexedValue3(node.i, GLOBAL_CONTEXT_API + "." + GLOBAL_CONTEXT_PROMISE_CONSTRUCTOR + "()");
3792
3796
  }
3793
3797
  function serializeReadableStreamClose(node) {
3794
- return GLOBAL_CONTEXT_STREAM_EMIT + "(" + node.i + ",2)";
3798
+ return GLOBAL_CONTEXT_API + "." + GLOBAL_CONTEXT_STREAM_EMIT + "(" + getRefExpr(node.i) + ",2)";
3795
3799
  }
3796
3800
  function serializeReadableStreamEnqueue(ctx, node) {
3797
- return GLOBAL_CONTEXT_STREAM_EMIT + "(" + node.i + ",0," + crossSerializeTree(ctx, node.f) + ")";
3801
+ return GLOBAL_CONTEXT_API + "." + GLOBAL_CONTEXT_STREAM_EMIT + "(" + getRefExpr(node.i) + ",0," + crossSerializeTree(ctx, node.f) + ")";
3798
3802
  }
3799
3803
  function serializeReadableStreamError(ctx, node) {
3800
- return GLOBAL_CONTEXT_STREAM_EMIT + "(" + node.i + ",1," + crossSerializeTree(ctx, node.f) + ")";
3804
+ return GLOBAL_CONTEXT_API + "." + GLOBAL_CONTEXT_STREAM_EMIT + "(" + getRefExpr(node.i) + ",1," + crossSerializeTree(ctx, node.f) + ")";
3801
3805
  }
3802
3806
  function serializeReadableStreamConstructor(node) {
3803
- return assignIndexedValue3(node.i, GLOBAL_CONTEXT_STREAM_CONSTRUCTOR + "()");
3807
+ return assignIndexedValue3(node.i, GLOBAL_CONTEXT_API + "." + GLOBAL_CONTEXT_STREAM_CONSTRUCTOR + "()");
3804
3808
  }
3805
3809
  function crossSerializeTree(ctx, node) {
3806
3810
  switch (node.t) {
@@ -4087,6 +4091,7 @@ function generatePromiseNode3(ctx, id, current) {
4087
4091
  b: void 0,
4088
4092
  o: void 0
4089
4093
  }, false);
4094
+ popPendingState(ctx);
4090
4095
  }
4091
4096
  },
4092
4097
  (data) => {
@@ -4105,9 +4110,11 @@ function generatePromiseNode3(ctx, id, current) {
4105
4110
  b: void 0,
4106
4111
  o: void 0
4107
4112
  }, false);
4113
+ popPendingState(ctx);
4108
4114
  }
4109
4115
  }
4110
4116
  );
4117
+ pushPendingState(ctx);
4111
4118
  return {
4112
4119
  t: 28 /* PromiseConstructor */,
4113
4120
  i: id,
@@ -4236,6 +4243,7 @@ function generateBoxedNode4(ctx, id, current) {
4236
4243
  function generateReadableStreamNode(ctx, id, current) {
4237
4244
  assert(ctx.features & 8192 /* WebAPI */, new UnsupportedTypeError(current));
4238
4245
  const reader = current.getReader();
4246
+ pushPendingState(ctx);
4239
4247
  function push() {
4240
4248
  reader.read().then(
4241
4249
  (data) => {
@@ -4255,6 +4263,7 @@ function generateReadableStreamNode(ctx, id, current) {
4255
4263
  b: void 0,
4256
4264
  o: void 0
4257
4265
  }, false);
4266
+ popPendingState(ctx);
4258
4267
  } else {
4259
4268
  ctx.onParse({
4260
4269
  t: 32 /* ReadableStreamEnqueue */,
@@ -4290,7 +4299,7 @@ function generateReadableStreamNode(ctx, id, current) {
4290
4299
  b: void 0,
4291
4300
  o: void 0
4292
4301
  }, false);
4293
- push();
4302
+ popPendingState(ctx);
4294
4303
  }
4295
4304
  }
4296
4305
  );
@@ -4871,22 +4880,23 @@ function crossParseSync(ctx, current) {
4871
4880
  }
4872
4881
 
4873
4882
  // src/core/cross/index.ts
4874
- function finalize2(ctx, id, result) {
4883
+ function finalize2(ctx, scopeId, id, result) {
4884
+ if (id == null) {
4885
+ return result;
4886
+ }
4875
4887
  const patches = resolvePatches2(ctx);
4876
- if (patches) {
4877
- if (id == null) {
4878
- if (ctx.features & 4 /* ArrowFunction */) {
4879
- const params2 = "(" + ROOT_REFERENCE + ")";
4880
- const body2 = ROOT_REFERENCE + "=" + result + "," + patches + ROOT_REFERENCE;
4881
- return "(" + params2 + "=>(" + body2 + "))()";
4882
- }
4883
- const params = "(" + ROOT_REFERENCE + ")";
4884
- const body = ROOT_REFERENCE + "=" + result + "," + patches + ROOT_REFERENCE;
4885
- return "(function" + params + "{return " + body + "})()";
4886
- }
4887
- return "(" + result + "," + patches + getRefExpr(id) + ")";
4888
+ const ref = getRefExpr(id);
4889
+ const params = scopeId == null ? "" : GLOBAL_CONTEXT_REFERENCES;
4890
+ const mainBody = patches ? result + "," + patches : result;
4891
+ if (params === "") {
4892
+ return patches ? "(" + mainBody + ref + ")" : mainBody;
4893
+ }
4894
+ const args = scopeId == null ? "()" : "(" + GLOBAL_CONTEXT_REFERENCES + '["' + serializeString(scopeId) + '"])';
4895
+ const body = mainBody + (patches ? ref : "");
4896
+ if (ctx.features & 4 /* ArrowFunction */) {
4897
+ return "(" + params + "=>(" + body + "))" + args;
4888
4898
  }
4889
- return result;
4899
+ return "(function(" + params + "){return " + body + "})" + args;
4890
4900
  }
4891
4901
  function crossSerialize(source, options) {
4892
4902
  const ctx = createCrossParserContext(options);
@@ -4897,6 +4907,7 @@ function crossSerialize(source, options) {
4897
4907
  const result = crossSerializeTree(serial, tree);
4898
4908
  return finalize2(
4899
4909
  serial,
4910
+ ctx.scopeId,
4900
4911
  tree.i,
4901
4912
  result
4902
4913
  );
@@ -4910,12 +4921,14 @@ async function crossSerializeAsync(source, options) {
4910
4921
  const result = crossSerializeTree(serial, tree);
4911
4922
  return finalize2(
4912
4923
  serial,
4924
+ ctx.scopeId,
4913
4925
  tree.i,
4914
4926
  result
4915
4927
  );
4916
4928
  }
4917
4929
  function crossSerializeStream(source, options) {
4918
4930
  const ctx = createStreamingCrossParserContext({
4931
+ scopeId: options.scopeId,
4919
4932
  refs: options.refs,
4920
4933
  disabledFeatures: options.disabledFeatures,
4921
4934
  onParse(node, initial) {
@@ -4925,16 +4938,25 @@ function crossSerializeStream(source, options) {
4925
4938
  options.onSerialize(
4926
4939
  finalize2(
4927
4940
  serial,
4941
+ ctx.scopeId,
4928
4942
  node.i,
4929
4943
  crossSerializeTree(serial, node)
4930
4944
  ),
4931
4945
  initial
4932
4946
  );
4933
- }
4947
+ },
4948
+ onDone: options.onDone
4934
4949
  });
4935
4950
  ctx.onParse(crossParseStream(ctx, source), true);
4936
- return () => {
4951
+ if (ctx.pending <= 0) {
4952
+ ctx.onDone();
4937
4953
  ctx.alive = false;
4954
+ }
4955
+ return () => {
4956
+ if (ctx.alive) {
4957
+ ctx.alive = false;
4958
+ ctx.onDone();
4959
+ }
4938
4960
  };
4939
4961
  }
4940
4962
 
@@ -4943,27 +4965,56 @@ var Serializer = class {
4943
4965
  constructor(options) {
4944
4966
  this.options = options;
4945
4967
  __publicField(this, "alive", true);
4968
+ __publicField(this, "flushed", false);
4969
+ __publicField(this, "done", false);
4970
+ __publicField(this, "pending", 0);
4946
4971
  __publicField(this, "cleanups", []);
4947
4972
  __publicField(this, "refs", /* @__PURE__ */ new Map());
4948
4973
  }
4949
4974
  write(key, value) {
4950
- if (this.alive) {
4975
+ if (this.alive && !this.flushed) {
4976
+ this.pending++;
4951
4977
  this.cleanups.push(crossSerializeStream(value, {
4978
+ scopeId: this.options.scopeId,
4952
4979
  refs: this.refs,
4953
4980
  disabledFeatures: this.options.disabledFeatures,
4954
4981
  onSerialize: (data, initial) => {
4955
- this.options.onData(
4956
- initial ? this.options.globalIdentifier + '["' + serializeString(key) + '"]=' + data : data
4957
- );
4982
+ if (this.alive) {
4983
+ this.options.onData(
4984
+ initial ? this.options.globalIdentifier + '["' + serializeString(key) + '"]=' + data : data
4985
+ );
4986
+ }
4987
+ },
4988
+ onDone: () => {
4989
+ if (this.alive) {
4990
+ this.pending--;
4991
+ if (this.pending <= 0 && this.flushed && !this.done && this.options.onDone) {
4992
+ this.options.onDone();
4993
+ this.done = true;
4994
+ }
4995
+ }
4958
4996
  }
4959
4997
  }));
4960
4998
  }
4961
4999
  }
5000
+ flush() {
5001
+ if (this.alive) {
5002
+ this.flushed = true;
5003
+ if (this.pending <= 0 && !this.done && this.options.onDone) {
5004
+ this.options.onDone();
5005
+ this.done = true;
5006
+ }
5007
+ }
5008
+ }
4962
5009
  close() {
4963
5010
  if (this.alive) {
4964
5011
  for (let i = 0, len = this.cleanups.length; i < len; i++) {
4965
5012
  this.cleanups[i]();
4966
5013
  }
5014
+ if (!this.done && this.options.onDone) {
5015
+ this.options.onDone();
5016
+ this.done = true;
5017
+ }
4967
5018
  this.alive = false;
4968
5019
  }
4969
5020
  }
@@ -4972,8 +5023,8 @@ var Serializer = class {
4972
5023
  // src/index.ts
4973
5024
  var src_default = serialize;
4974
5025
  export {
4975
- CROSS_REFERENCE_HEADER,
4976
5026
  Feature,
5027
+ GLOBAL_CONTEXT_API_SCRIPT,
4977
5028
  Serializer,
4978
5029
  compileJSON,
4979
5030
  createReference,
@@ -4983,6 +5034,7 @@ export {
4983
5034
  src_default as default,
4984
5035
  deserialize,
4985
5036
  fromJSON,
5037
+ getCrossReferenceHeader,
4986
5038
  serialize,
4987
5039
  serializeAsync,
4988
5040
  toJSON,