seroval 0.12.4 → 0.13.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.
Files changed (137) hide show
  1. package/dist/cjs/development/index.cjs +1431 -958
  2. package/dist/cjs/development/index.cjs.map +3 -3
  3. package/dist/cjs/production/index.cjs +3 -3
  4. package/dist/esm/development/index.mjs +1431 -958
  5. package/dist/esm/development/index.mjs.map +3 -3
  6. package/dist/esm/production/index.mjs +3 -3
  7. package/dist/types/core/base-primitives.d.ts +12 -1
  8. package/dist/types/core/base-primitives.d.ts.map +1 -1
  9. package/dist/types/core/compat.d.ts +0 -2
  10. package/dist/types/core/compat.d.ts.map +1 -1
  11. package/dist/types/core/constants.d.ts +8 -2
  12. package/dist/types/core/constants.d.ts.map +1 -1
  13. package/dist/types/core/{tree/deserialize.d.ts → context/deserializer.d.ts} +19 -13
  14. package/dist/types/core/context/deserializer.d.ts.map +1 -0
  15. package/dist/types/core/{base → context/parser}/async.d.ts +6 -5
  16. package/dist/types/core/context/parser/async.d.ts.map +1 -0
  17. package/dist/types/core/{base → context/parser}/stream.d.ts +2 -1
  18. package/dist/types/core/context/parser/stream.d.ts.map +1 -0
  19. package/dist/types/core/{base → context/parser}/sync.d.ts +7 -9
  20. package/dist/types/core/context/parser/sync.d.ts.map +1 -0
  21. package/dist/types/core/context/parser.d.ts +27 -0
  22. package/dist/types/core/context/parser.d.ts.map +1 -0
  23. package/dist/types/core/{serializer-context.old.d.ts → context/serializer.d.ts} +21 -22
  24. package/dist/types/core/context/serializer.d.ts.map +1 -0
  25. package/dist/types/core/cross/async.d.ts +2 -2
  26. package/dist/types/core/cross/async.d.ts.map +1 -1
  27. package/dist/types/core/cross/deserializer.d.ts +9 -0
  28. package/dist/types/core/cross/deserializer.d.ts.map +1 -0
  29. package/dist/types/core/cross/index.d.ts +11 -1
  30. package/dist/types/core/cross/index.d.ts.map +1 -1
  31. package/dist/types/core/cross/parser.d.ts +6 -0
  32. package/dist/types/core/cross/parser.d.ts.map +1 -0
  33. package/dist/types/core/cross/{serialize.d.ts → serializer.d.ts} +6 -5
  34. package/dist/types/core/cross/serializer.d.ts.map +1 -0
  35. package/dist/types/core/cross/stream.d.ts +2 -2
  36. package/dist/types/core/cross/stream.d.ts.map +1 -1
  37. package/dist/types/core/cross/sync.d.ts +2 -2
  38. package/dist/types/core/cross/sync.d.ts.map +1 -1
  39. package/dist/types/core/keys.d.ts +3 -1
  40. package/dist/types/core/keys.d.ts.map +1 -1
  41. package/dist/types/core/plugin.d.ts +6 -7
  42. package/dist/types/core/plugin.d.ts.map +1 -1
  43. package/dist/types/core/special-reference.d.ts +6 -0
  44. package/dist/types/core/special-reference.d.ts.map +1 -0
  45. package/dist/types/core/tree/async.d.ts +2 -2
  46. package/dist/types/core/tree/async.d.ts.map +1 -1
  47. package/dist/types/core/tree/deserializer.d.ts +13 -0
  48. package/dist/types/core/tree/deserializer.d.ts.map +1 -0
  49. package/dist/types/core/tree/{serialize.d.ts → serializer.d.ts} +4 -4
  50. package/dist/types/core/tree/serializer.d.ts.map +1 -0
  51. package/dist/types/core/tree/sync.d.ts +2 -2
  52. package/dist/types/core/tree/sync.d.ts.map +1 -1
  53. package/dist/types/core/types.d.ts +47 -6
  54. package/dist/types/core/types.d.ts.map +1 -1
  55. package/dist/types/core/utils/assert.d.ts.map +1 -0
  56. package/dist/types/core/utils/constructors.d.ts.map +1 -0
  57. package/dist/types/core/utils/deferred.d.ts +14 -0
  58. package/dist/types/core/utils/deferred.d.ts.map +1 -0
  59. package/dist/types/core/utils/error.d.ts +5 -0
  60. package/dist/types/core/utils/error.d.ts.map +1 -0
  61. package/dist/types/core/utils/get-identifier.d.ts.map +1 -0
  62. package/dist/types/core/utils/get-object-flag.d.ts +3 -0
  63. package/dist/types/core/utils/get-object-flag.d.ts.map +1 -0
  64. package/dist/types/core/utils/is-valid-identifier.d.ts +2 -0
  65. package/dist/types/core/utils/is-valid-identifier.d.ts.map +1 -0
  66. package/dist/types/core/utils/iterator-to-sequence.d.ts +20 -0
  67. package/dist/types/core/utils/iterator-to-sequence.d.ts.map +1 -0
  68. package/dist/types/core/utils/promise-to-result.d.ts.map +1 -0
  69. package/dist/types/core/utils/typed-array.d.ts +4 -0
  70. package/dist/types/core/utils/typed-array.d.ts.map +1 -0
  71. package/dist/types/core/web-api.d.ts +4 -1
  72. package/dist/types/core/web-api.d.ts.map +1 -1
  73. package/package.json +2 -2
  74. package/src/core/base-primitives.ts +229 -1
  75. package/src/core/compat.ts +0 -2
  76. package/src/core/constants.ts +7 -2
  77. package/src/core/{tree/deserialize.ts → context/deserializer.ts} +159 -66
  78. package/src/core/{base → context/parser}/async.ts +181 -309
  79. package/src/core/{base → context/parser}/stream.ts +86 -11
  80. package/src/core/{base → context/parser}/sync.ts +120 -281
  81. package/src/core/context/parser.ts +259 -0
  82. package/src/core/{serializer-context.old.ts → context/serializer.ts} +241 -166
  83. package/src/core/cross/async.ts +2 -2
  84. package/src/core/cross/deserializer.ts +19 -0
  85. package/src/core/cross/index.ts +68 -47
  86. package/src/core/cross/parser.ts +7 -0
  87. package/src/core/cross/{serialize.ts → serializer.ts} +31 -12
  88. package/src/core/cross/stream.ts +2 -2
  89. package/src/core/cross/sync.ts +2 -2
  90. package/src/core/is-serializable.new.ts +1 -1
  91. package/src/core/keys.ts +8 -2
  92. package/src/core/plugin.ts +7 -7
  93. package/src/core/reference.ts +1 -1
  94. package/src/core/special-reference.ts +9 -0
  95. package/src/core/tree/async.ts +2 -2
  96. package/src/core/tree/deserializer.ts +31 -0
  97. package/src/core/tree/index.ts +2 -2
  98. package/src/core/tree/{serialize.ts → serializer.ts} +5 -16
  99. package/src/core/tree/sync.ts +2 -2
  100. package/src/core/types.ts +60 -7
  101. package/src/core/utils/deferred.ts +49 -0
  102. package/src/core/{shared.ts → utils/error.ts} +3 -60
  103. package/src/core/utils/get-object-flag.ts +14 -0
  104. package/src/core/utils/is-valid-identifier.ts +11 -0
  105. package/src/core/utils/iterator-to-sequence.ts +238 -0
  106. package/src/core/utils/typed-array.ts +30 -0
  107. package/src/core/web-api.ts +75 -1
  108. package/src/example.ts +0 -3
  109. package/dist/types/core/assert.d.ts.map +0 -1
  110. package/dist/types/core/base/async.d.ts.map +0 -1
  111. package/dist/types/core/base/stream.d.ts.map +0 -1
  112. package/dist/types/core/base/sync.d.ts.map +0 -1
  113. package/dist/types/core/constructors.d.ts.map +0 -1
  114. package/dist/types/core/cross/cross-parser.d.ts +0 -8
  115. package/dist/types/core/cross/cross-parser.d.ts.map +0 -1
  116. package/dist/types/core/cross/serialize.d.ts.map +0 -1
  117. package/dist/types/core/get-identifier.d.ts.map +0 -1
  118. package/dist/types/core/parser-context.d.ts +0 -23
  119. package/dist/types/core/parser-context.d.ts.map +0 -1
  120. package/dist/types/core/promise-to-result.d.ts.map +0 -1
  121. package/dist/types/core/serializer-context.old.d.ts.map +0 -1
  122. package/dist/types/core/shared.d.ts +0 -10
  123. package/dist/types/core/shared.d.ts.map +0 -1
  124. package/dist/types/core/tree/deserialize.d.ts.map +0 -1
  125. package/dist/types/core/tree/serialize.d.ts.map +0 -1
  126. package/src/core/cross/cross-parser.ts +0 -9
  127. package/src/core/parser-context.ts +0 -131
  128. package/src/core/serializer-context.new.ts +0 -1168
  129. package/src/core/tree/parser.ts +0 -45
  130. /package/dist/types/core/{assert.d.ts → utils/assert.d.ts} +0 -0
  131. /package/dist/types/core/{constructors.d.ts → utils/constructors.d.ts} +0 -0
  132. /package/dist/types/core/{get-identifier.d.ts → utils/get-identifier.d.ts} +0 -0
  133. /package/dist/types/core/{promise-to-result.d.ts → utils/promise-to-result.d.ts} +0 -0
  134. /package/src/core/{assert.ts → utils/assert.ts} +0 -0
  135. /package/src/core/{constructors.ts → utils/constructors.ts} +0 -0
  136. /package/src/core/{get-identifier.ts → utils/get-identifier.ts} +0 -0
  137. /package/src/core/{promise-to-result.ts → utils/promise-to-result.ts} +0 -0
@@ -1,7 +1,10 @@
1
+ import type { SerovalNode } from '../types';
1
2
  import type { CrossAsyncParserContextOptions } from './async';
2
3
  import AsyncCrossParserContext from './async';
3
- import type { CrossContextOptions } from './cross-parser';
4
- import CrossSerializerContext from './serialize';
4
+ import type { CrossDeserializerContextOptions } from './deserializer';
5
+ import CrossDeserializerContext from './deserializer';
6
+ import type { CrossContextOptions, CrossParserContextOptions } from './parser';
7
+ import CrossSerializerContext from './serializer';
5
8
  import type { CrossStreamParserContextOptions } from './stream';
6
9
  import StreamCrossParserContext from './stream';
7
10
  import type { CrossSyncParserContextOptions } from './sync';
@@ -53,51 +56,33 @@ export async function crossSerializeAsync<T>(
53
56
  return serial.serializeTop(tree);
54
57
  }
55
58
 
56
- // export interface SerovalCrossJSON {
57
- // t: SerovalNode;
58
- // f: number;
59
- // }
60
-
61
- // export function toCrossJSON<T>(
62
- // source: T,
63
- // options?: CrossParserContextOptions,
64
- // ): SerovalCrossJSON {
65
- // const ctx = createCrossParserContext(options);
66
- // return {
67
- // t: parseSync(ctx, source),
68
- // f: ctx.features,
69
- // };
70
- // }
71
-
72
- // export async function toCrossJSONAsync<T>(
73
- // source: T,
74
- // options?: CrossParserContextOptions,
75
- // ): Promise<SerovalCrossJSON> {
76
- // const ctx = createCrossParserContext(options);
77
- // return {
78
- // t: await parseAsync(ctx, source),
79
- // f: ctx.features,
80
- // };
81
- // }
82
-
83
- // export function compileCrossJSON(source: SerovalCrossJSON): string {
84
- // const serial = createCrossSerializerContext({
85
- // features: source.f,
86
- // });
87
- // const result = crossSerializeTree(serial, source.t);
88
- // return finalize(
89
- // serial,
90
- // source.t.i,
91
- // result,
92
- // );
93
- // }
94
-
95
- // export function fromJSON<T>(source: SerovalJSON): T {
96
- // const serial = createDeserializerContext({
97
- // markedRefs: source.m,
98
- // });
99
- // return deserializeTree(serial, source.t) as T;
100
- // }
59
+ export type ToCrossJSONOptions = CrossParserContextOptions;
60
+
61
+ export function toCrossJSON<T>(
62
+ source: T,
63
+ options: CrossParserContextOptions = {},
64
+ ): SerovalNode {
65
+ const ctx = new SyncCrossParserContext({
66
+ plugins: options.plugins,
67
+ disabledFeatures: options.disabledFeatures,
68
+ refs: options.refs,
69
+ });
70
+ return ctx.parse(source);
71
+ }
72
+
73
+ export type ToCrossJSONAsyncOptions = CrossParserContextOptions;
74
+
75
+ export async function toCrossJSONAsync<T>(
76
+ source: T,
77
+ options: CrossParserContextOptions = {},
78
+ ): Promise<SerovalNode> {
79
+ const ctx = new AsyncCrossParserContext({
80
+ plugins: options.plugins,
81
+ disabledFeatures: options.disabledFeatures,
82
+ refs: options.refs,
83
+ });
84
+ return ctx.parse(source);
85
+ }
101
86
 
102
87
  export interface CrossSerializeStreamOptions
103
88
  extends Omit<CrossStreamParserContextOptions, 'onParse'>, CrossContextOptions {
@@ -109,6 +94,7 @@ export function crossSerializeStream<T>(
109
94
  options: CrossSerializeStreamOptions,
110
95
  ): () => void {
111
96
  const ctx = new StreamCrossParserContext({
97
+ plugins: options.plugins,
112
98
  refs: options.refs,
113
99
  disabledFeatures: options.disabledFeatures,
114
100
  onParse(node, initial): void {
@@ -145,3 +131,38 @@ export function crossSerializeStream<T>(
145
131
  ctx.destroy();
146
132
  };
147
133
  }
134
+
135
+ export type ToCrossJSONStreamOptions = CrossStreamParserContextOptions;
136
+
137
+ export function toCrossJSONStream<T>(
138
+ source: T,
139
+ options: ToCrossJSONStreamOptions,
140
+ ): () => void {
141
+ const ctx = new StreamCrossParserContext({
142
+ plugins: options.plugins,
143
+ refs: options.refs,
144
+ disabledFeatures: options.disabledFeatures,
145
+ onParse: options.onParse,
146
+ onError: options.onError,
147
+ onDone: options.onDone,
148
+ });
149
+
150
+ ctx.start(source);
151
+
152
+ return () => {
153
+ ctx.destroy();
154
+ };
155
+ }
156
+
157
+ export type FromCrossJSONOptions = CrossDeserializerContextOptions;
158
+
159
+ export function fromCrossJSON<T>(
160
+ source: SerovalNode,
161
+ options: FromCrossJSONOptions,
162
+ ): T {
163
+ const ctx = new CrossDeserializerContext({
164
+ plugins: options.plugins,
165
+ refs: options.refs,
166
+ });
167
+ return ctx.deserialize(source) as T;
168
+ }
@@ -0,0 +1,7 @@
1
+ import type { BaseParserContextOptions } from '../context/parser';
2
+
3
+ export type CrossParserContextOptions = BaseParserContextOptions
4
+
5
+ export interface CrossContextOptions {
6
+ scopeId?: string;
7
+ }
@@ -7,6 +7,7 @@ import type {
7
7
  SerovalReadableStreamErrorNode,
8
8
  SerovalReadableStreamConstructorNode,
9
9
  SerovalNode,
10
+ SerovalAsyncIteratorFactoryNode,
10
11
  } from '../types';
11
12
  import {
12
13
  GLOBAL_CONTEXT_PROMISE_REJECT,
@@ -14,15 +15,16 @@ import {
14
15
  GLOBAL_CONTEXT_PROMISE_CONSTRUCTOR,
15
16
  GLOBAL_CONTEXT_REFERENCES,
16
17
  GLOBAL_CONTEXT_STREAM_CONSTRUCTOR,
17
- GLOBAL_CONTEXT_STREAM_EMIT,
18
+ GLOBAL_CONTEXT_STREAM_ENQUEUE,
19
+ GLOBAL_CONTEXT_STREAM_CLOSE,
20
+ GLOBAL_CONTEXT_STREAM_ERROR,
18
21
  GLOBAL_CONTEXT_API,
19
22
  } from '../keys';
20
- import type { BaseSerializerContextOptions } from '../serializer-context.old';
21
- import BaseSerializerContext from '../serializer-context.old';
23
+ import type { BaseSerializerContextOptions } from '../context/serializer';
24
+ import BaseSerializerContext from '../context/serializer';
22
25
  import type { SerovalMode } from '../plugin';
23
- import { Feature } from '../compat';
24
26
  import { serializeString } from '../string';
25
- import type { CrossContextOptions } from './cross-parser';
27
+ import type { CrossContextOptions } from './parser';
26
28
 
27
29
  export interface CrossSerializerContextOptions
28
30
  extends BaseSerializerContextOptions, CrossContextOptions {
@@ -79,19 +81,39 @@ export default class CrossSerializerContext extends BaseSerializerContext {
79
81
  protected serializeReadableStreamEnqueue(
80
82
  node: SerovalReadableStreamEnqueueNode,
81
83
  ): string {
82
- return GLOBAL_CONTEXT_API + '.' + GLOBAL_CONTEXT_STREAM_EMIT + '(' + this.getRefParam(node.i) + ',0,' + this.serialize(node.f) + ')';
84
+ return GLOBAL_CONTEXT_API + '.' + GLOBAL_CONTEXT_STREAM_ENQUEUE + '(' + this.getRefParam(node.i) + ',' + this.serialize(node.f) + ')';
83
85
  }
84
86
 
85
87
  protected serializeReadableStreamError(
86
88
  node: SerovalReadableStreamErrorNode,
87
89
  ): string {
88
- return GLOBAL_CONTEXT_API + '.' + GLOBAL_CONTEXT_STREAM_EMIT + '(' + this.getRefParam(node.i) + ',1,' + this.serialize(node.f) + ')';
90
+ return GLOBAL_CONTEXT_API + '.' + GLOBAL_CONTEXT_STREAM_ERROR + '(' + this.getRefParam(node.i) + ',' + this.serialize(node.f) + ')';
89
91
  }
90
92
 
91
93
  protected serializeReadableStreamClose(
92
94
  node: SerovalReadableStreamCloseNode,
93
95
  ): string {
94
- return GLOBAL_CONTEXT_API + '.' + GLOBAL_CONTEXT_STREAM_EMIT + '(' + this.getRefParam(node.i) + ',2)';
96
+ return GLOBAL_CONTEXT_API + '.' + GLOBAL_CONTEXT_STREAM_CLOSE + '(' + this.getRefParam(node.i) + ')';
97
+ }
98
+
99
+ protected serializeAsyncIteratorFactory(node: SerovalAsyncIteratorFactoryNode): string {
100
+ return this.assignIndexedValue(
101
+ node.i,
102
+ this.createFunction(
103
+ ['s'],
104
+ this.createFunction(
105
+ ['b', 't'],
106
+ '(b=s.tee(),s=b[0],b=b[1].getReader(),t={[' + this.serialize(node.f) + ']:' + this.createFunction([], 't') + ','
107
+ + 'next:' + this.createFunction(
108
+ [],
109
+ 'b.read().then(' + this.createEffectfulFunction(
110
+ ['d'],
111
+ 'if(d.done)return{done:!0,value:void 0};d=d.value;if(d[0]===1)throw d[1];return{done:d[0]===2,value:d[1]}',
112
+ ) + ')',
113
+ ) + '})',
114
+ ),
115
+ ),
116
+ );
95
117
  }
96
118
 
97
119
  serializeTop(tree: SerovalNode): string {
@@ -109,9 +131,6 @@ export default class CrossSerializerContext extends BaseSerializerContext {
109
131
  }
110
132
  const args = this.scopeId == null ? '()' : '(' + GLOBAL_CONTEXT_REFERENCES + '["' + serializeString(this.scopeId) + '"])';
111
133
  const body = mainBody + (patches ? ref : '');
112
- if (this.features & Feature.ArrowFunction) {
113
- return '(' + params + '=>(' + body + '))' + args;
114
- }
115
- return '(function(' + params + '){return ' + body + '})' + args;
134
+ return '(' + this.createFunction([params], body) + ')' + args;
116
135
  }
117
136
  }
@@ -1,5 +1,5 @@
1
- import type { BaseStreamParserContextOptions } from '../base/stream';
2
- import BaseStreamParserContext from '../base/stream';
1
+ import type { BaseStreamParserContextOptions } from '../context/parser/stream';
2
+ import BaseStreamParserContext from '../context/parser/stream';
3
3
  import type { SerovalMode } from '../plugin';
4
4
 
5
5
  export type CrossStreamParserContextOptions = BaseStreamParserContextOptions
@@ -1,6 +1,6 @@
1
- import BaseSyncParserContext from '../base/sync';
1
+ import BaseSyncParserContext from '../context/parser/sync';
2
2
  import type { SerovalMode } from '../plugin';
3
- import type { CrossParserContextOptions } from './cross-parser';
3
+ import type { CrossParserContextOptions } from './parser';
4
4
 
5
5
  export type CrossSyncParserContextOptions = CrossParserContextOptions
6
6
 
@@ -2,7 +2,7 @@ import { ALL_ENABLED, BIGINT_FLAG, Feature } from './compat';
2
2
  import { INV_SYMBOL_REF, UNIVERSAL_SENTINEL } from './constants';
3
3
  import type { Plugin, PluginAccessOptions } from './plugin';
4
4
  import { hasReferenceID } from './reference';
5
- import { getErrorOptions } from './shared';
5
+ import { getErrorOptions } from './utils/error';
6
6
 
7
7
  export type SerializationMode = 'sync' | 'async' | 'stream';
8
8
 
package/src/core/keys.ts CHANGED
@@ -21,7 +21,11 @@ export const LOCAL_CONTEXT_STREAM_CONTROLLER = 'c';
21
21
 
22
22
  export const GLOBAL_CONTEXT_STREAM_CONSTRUCTOR = 'S';
23
23
 
24
- export const GLOBAL_CONTEXT_STREAM_EMIT = 'Se';
24
+ export const GLOBAL_CONTEXT_STREAM_ENQUEUE = 'Se';
25
+
26
+ export const GLOBAL_CONTEXT_STREAM_ERROR = 'St';
27
+
28
+ export const GLOBAL_CONTEXT_STREAM_CLOSE = 'Sc';
25
29
 
26
30
  const GLOBAL_CONTEXT_API_REF = `self.${GLOBAL_CONTEXT_API}`;
27
31
 
@@ -31,7 +35,9 @@ export const GLOBAL_CONTEXT_API_SCRIPT = `${GLOBAL_CONTEXT_API_REF}=${GLOBAL_CON
31
35
  + `${GLOBAL_CONTEXT_PROMISE_RESOLVE}:function(p,d){p.${LOCAL_CONTEXT_PROMISE_RESOLVE}(d),p.status="success",p.value=d,this.uP(p)},`
32
36
  + `${GLOBAL_CONTEXT_PROMISE_REJECT}:function(p,d){p.${LOCAL_CONTEXT_PROMISE_REJECT}(d),p.status="failure",p.value=d,this.uP(p)},`
33
37
  + `uS:function(s){delete s.${LOCAL_CONTEXT_STREAM_CONTROLLER}},`
34
- + `${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())}},`
38
+ + `${GLOBAL_CONTEXT_STREAM_CLOSE}:function(s){s.${LOCAL_CONTEXT_STREAM_CONTROLLER}.close(),this.uS(s)},`
39
+ + `${GLOBAL_CONTEXT_STREAM_ERROR}:function(s,e){s.${LOCAL_CONTEXT_STREAM_CONTROLLER}.error(e),this.uS(s)},`
40
+ + `${GLOBAL_CONTEXT_STREAM_ENQUEUE}:function(s,d){s.${LOCAL_CONTEXT_STREAM_CONTROLLER}.enqueue(d)},`
35
41
  + `${GLOBAL_CONTEXT_STREAM_CONSTRUCTOR}:function(s,c){return(s=new ReadableStream({start:function(x){c=x}})).${LOCAL_CONTEXT_STREAM_CONTROLLER}=c,s}`
36
42
  + '}';
37
43
 
@@ -1,8 +1,8 @@
1
- import type BaseAsyncParserContext from './base/async';
2
- import type BaseStreamParserContext from './base/stream';
3
- import type BaseSyncParserContext from './base/sync';
4
- import type BaseSerializerContext from './serializer-context.old';
5
- import type VanillaDeserializerContext from './tree/deserialize';
1
+ import type BaseAsyncParserContext from './context/parser/async';
2
+ import type BaseStreamParserContext from './context/parser/stream';
3
+ import type BaseSyncParserContext from './context/parser/sync';
4
+ import type BaseSerializerContext from './context/serializer';
5
+ import type BaseDeserializerContext from './context/deserializer';
6
6
 
7
7
  export type SerovalMode = 'vanilla' | 'cross';
8
8
 
@@ -37,10 +37,10 @@ export interface Plugin<Value, Node> {
37
37
  ): string;
38
38
  deserialize(
39
39
  node: Node,
40
- ctx: VanillaDeserializerContext,
40
+ ctx: BaseDeserializerContext,
41
41
  data: PluginData,
42
42
  ): Value;
43
- isIterable?: (value: Value) => boolean;
43
+ // isIterable?: (value: Value) => boolean;
44
44
  // isSerializable?: (value: Value) => boolean;
45
45
  }
46
46
 
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable no-restricted-globals */
2
- import assert from './assert';
2
+ import assert from './utils/assert';
3
3
  import { REFERENCES_KEY } from './keys';
4
4
 
5
5
  const REFERENCE = new Map<unknown, string>();
@@ -0,0 +1,9 @@
1
+ export const MAP_SENTINEL = {};
2
+
3
+ export const ITERATOR = {};
4
+
5
+ export const ASYNC_ITERATOR = {};
6
+
7
+ export const READABLE_STREAM = {};
8
+
9
+ export const UNIVERSAL_SENTINEL = {};
@@ -1,5 +1,5 @@
1
- import BaseAsyncParserContext from '../base/async';
2
- import type { BaseParserContextOptions } from '../parser-context';
1
+ import BaseAsyncParserContext from '../context/parser/async';
2
+ import type { BaseParserContextOptions } from '../context/parser';
3
3
  import type { SerovalMode } from '../plugin';
4
4
 
5
5
  export type AsyncParserContextOptions = Omit<BaseParserContextOptions, 'refs'>
@@ -0,0 +1,31 @@
1
+ import BaseDeserializerContext from '../context/deserializer';
2
+ import type { BaseDeserializerOptions } from '../context/deserializer';
3
+ import type { SerovalMode } from '../plugin';
4
+
5
+ export interface VanillaDeserializerContextOptions extends Omit<BaseDeserializerOptions, 'refs'> {
6
+ markedRefs: number[] | Set<number>;
7
+ }
8
+
9
+ export default class VanillaDeserializerContext extends BaseDeserializerContext {
10
+ readonly mode: SerovalMode = 'vanilla';
11
+
12
+ marked: Set<number>;
13
+
14
+ constructor(options: VanillaDeserializerContextOptions) {
15
+ super({
16
+ plugins: options.plugins,
17
+ refs: undefined,
18
+ });
19
+ this.marked = new Set(options.markedRefs);
20
+ }
21
+
22
+ assignIndexedValue<T>(
23
+ index: number,
24
+ value: T,
25
+ ): T {
26
+ if (this.marked.has(index)) {
27
+ this.refs.set(index, value);
28
+ }
29
+ return value;
30
+ }
31
+ }
@@ -2,8 +2,8 @@ import type { PluginAccessOptions } from '../plugin';
2
2
  import type { SerovalNode } from '../types';
3
3
  import type { AsyncParserContextOptions } from './async';
4
4
  import AsyncParserContext from './async';
5
- import VanillaDeserializerContext from './deserialize';
6
- import VanillaSerializerContext from './serialize';
5
+ import VanillaDeserializerContext from './deserializer';
6
+ import VanillaSerializerContext from './serializer';
7
7
  import type { SyncParserContextOptions } from './sync';
8
8
  import SyncParserContext from './sync';
9
9
 
@@ -8,11 +8,10 @@ import type {
8
8
  SerovalReadableStreamEnqueueNode,
9
9
  SerovalReadableStreamErrorNode,
10
10
  } from '../types';
11
- import type { BaseSerializerContextOptions } from '../serializer-context.old';
12
- import BaseSerializerContext from '../serializer-context.old';
13
- import getIdentifier from '../get-identifier';
11
+ import type { BaseSerializerContextOptions } from '../context/serializer';
12
+ import BaseSerializerContext from '../context/serializer';
13
+ import getIdentifier from '../utils/get-identifier';
14
14
  import type { SerovalMode } from '../plugin';
15
- import { Feature } from '../compat';
16
15
  import { SerovalNodeType } from '../constants';
17
16
 
18
17
  export type VanillaSerializerContextOptions = BaseSerializerContextOptions
@@ -30,7 +29,7 @@ export default class VanillaSerializerContext extends BaseSerializerContext {
30
29
  * Variables
31
30
  * @private
32
31
  */
33
- vars: (string | undefined)[] = [];
32
+ vars: string[] = [];
34
33
 
35
34
  /**
36
35
  * Increments the number of references the referenced value has
@@ -131,17 +130,7 @@ export default class VanillaSerializerContext extends BaseSerializerContext {
131
130
  body = index + '=' + body;
132
131
  }
133
132
  }
134
- let params = this.vars.length > 1
135
- ? this.vars.join(',')
136
- : this.vars[0];
137
- // Source is probably already assigned
138
- if (this.features & Feature.ArrowFunction) {
139
- params = this.vars.length > 1 || this.vars.length === 0
140
- ? '(' + params + ')'
141
- : params;
142
- return '(' + params + '=>(' + body + '))()';
143
- }
144
- return '(function(' + params + '){return ' + body + '})()';
133
+ return '(' + this.createFunction(this.vars, '(' + body + ')') + ')()';
145
134
  }
146
135
  if (tree.t === SerovalNodeType.Object) {
147
136
  return '(' + result + ')';
@@ -1,5 +1,5 @@
1
- import BaseSyncParserContext from '../base/sync';
2
- import type { BaseParserContextOptions } from '../parser-context';
1
+ import BaseSyncParserContext from '../context/parser/sync';
2
+ import type { BaseParserContextOptions } from '../context/parser';
3
3
  import type { SerovalMode } from '../plugin';
4
4
 
5
5
  export type SyncParserContextOptions = Omit<BaseParserContextOptions, 'refs'>;
package/src/core/types.ts CHANGED
@@ -33,13 +33,9 @@ export interface SerovalBaseNode {
33
33
  o: SerovalObjectFlags | undefined;
34
34
  }
35
35
 
36
- export const enum SerovalObjectRecordSpecialKey {
37
- SymbolIterator = 0,
38
- }
39
-
40
36
  export type SerovalObjectRecordKey =
41
37
  | string
42
- | SerovalObjectRecordSpecialKey;
38
+ | SerovalNode;
43
39
 
44
40
  export interface SerovalPlainRecordNode {
45
41
  k: string[];
@@ -166,6 +162,7 @@ export interface SerovalMapNode extends SerovalBaseNode {
166
162
  i: number;
167
163
  // key/value pairs
168
164
  e: SerovalMapRecordNode;
165
+ f: SerovalMapSentinelNode | SerovalIndexedValueNode;
169
166
  }
170
167
 
171
168
  export interface SerovalArrayNode extends SerovalBaseNode {
@@ -392,6 +389,53 @@ export interface SerovalPluginNode extends SerovalBaseNode {
392
389
  c: string;
393
390
  }
394
391
 
392
+ export interface SerovalMapSentinelNode extends SerovalBaseNode {
393
+ t: SerovalNodeType.MapSentinel;
394
+ i: number;
395
+ }
396
+
397
+ export interface SerovalIteratorFactoryNode extends SerovalBaseNode {
398
+ t: SerovalNodeType.IteratorFactory;
399
+ i: number;
400
+ f: SerovalNode;
401
+ }
402
+
403
+ export interface SerovalIteratorFactoryInstanceNode extends SerovalBaseNode {
404
+ t: SerovalNodeType.IteratorFactoryInstance;
405
+ a: [
406
+ instance: SerovalNode,
407
+ sequence: SerovalNode,
408
+ ];
409
+ }
410
+
411
+ export interface SerovalAsyncIteratorFactoryNode extends SerovalBaseNode {
412
+ t: SerovalNodeType.AsyncIteratorFactory;
413
+ i: number;
414
+ f: SerovalNode;
415
+ }
416
+
417
+ export interface SerovalAsyncIteratorFactoryInstanceNode extends SerovalBaseNode {
418
+ t: SerovalNodeType.AsyncIteratorFactoryInstance;
419
+ a: [
420
+ instance: SerovalNode,
421
+ sequence: SerovalNode,
422
+ ];
423
+ }
424
+
425
+ export interface SerovalReadableStreamNode extends SerovalBaseNode {
426
+ t: SerovalNodeType.ReadableStream;
427
+ i: number;
428
+ a: [
429
+ instance: SerovalNode,
430
+ sequence: SerovalNode,
431
+ ];
432
+ }
433
+
434
+ export interface SerovalReadableStreamFactoryNode extends SerovalBaseNode {
435
+ t: SerovalNodeType.ReadableStreamFactory;
436
+ i: number;
437
+ }
438
+
395
439
  export type SerovalSyncNode =
396
440
  | SerovalPrimitiveNode
397
441
  | SerovalIndexedValueNode
@@ -418,7 +462,12 @@ export type SerovalSyncNode =
418
462
  | SerovalEventNode
419
463
  | SerovalCustomEventNode
420
464
  | SerovalDOMExceptionNode
421
- | SerovalPluginNode;
465
+ | SerovalPluginNode
466
+ | SerovalMapSentinelNode
467
+ | SerovalIteratorFactoryNode
468
+ | SerovalIteratorFactoryInstanceNode
469
+ | SerovalAsyncIteratorFactoryNode
470
+ | SerovalAsyncIteratorFactoryInstanceNode;
422
471
 
423
472
  export type SerovalAsyncNode =
424
473
  | SerovalPromiseNode
@@ -432,8 +481,12 @@ export type SerovalAsyncNode =
432
481
  | SerovalReadableStreamCloseNode
433
482
  | SerovalReadableStreamErrorNode
434
483
  | SerovalRequestNode
435
- | SerovalResponseNode;
484
+ | SerovalResponseNode
485
+ | SerovalReadableStreamNode
486
+ | SerovalReadableStreamFactoryNode;
436
487
 
437
488
  export type SerovalNode =
438
489
  | SerovalSyncNode
439
490
  | SerovalAsyncNode;
491
+
492
+ export type SerovalNodeWithID = Extract<SerovalNode, { i: number }>;
@@ -0,0 +1,49 @@
1
+ export interface Deferred {
2
+ promise: Promise<unknown>;
3
+ resolve(value: unknown): void;
4
+ reject(value: unknown): void;
5
+ }
6
+
7
+ export function createDeferred(): Deferred {
8
+ let resolve: Deferred['resolve'];
9
+ let reject: Deferred['reject'];
10
+ return {
11
+ promise: new Promise<unknown>((res, rej) => {
12
+ resolve = res;
13
+ reject = rej;
14
+ }),
15
+ resolve(value): void {
16
+ resolve(value);
17
+ },
18
+ reject(value): void {
19
+ reject(value);
20
+ },
21
+ };
22
+ }
23
+
24
+ export interface DeferredStream {
25
+ stream: ReadableStream;
26
+ close(): void;
27
+ enqueue(chunk?: unknown): void;
28
+ error(e?: any): void;
29
+ }
30
+
31
+ export function createDeferredStream(): DeferredStream {
32
+ let controller: ReadableStreamDefaultController;
33
+ return {
34
+ stream: new ReadableStream({
35
+ start(current): void {
36
+ controller = current;
37
+ },
38
+ }),
39
+ close(): void {
40
+ controller.close();
41
+ },
42
+ enqueue(data): void {
43
+ controller.enqueue(data);
44
+ },
45
+ error(e): void {
46
+ controller.error(e);
47
+ },
48
+ };
49
+ }
@@ -1,12 +1,11 @@
1
1
  import type {
2
2
  ErrorValue,
3
- } from '../types';
4
- import { Feature } from './compat';
3
+ } from '../../types';
4
+ import { Feature } from '../compat';
5
5
  import {
6
6
  ERROR_CONSTRUCTOR_STRING,
7
7
  ErrorConstructorTag,
8
- SerovalObjectFlags,
9
- } from './constants';
8
+ } from '../constants';
10
9
 
11
10
  export function getErrorConstructor(error: ErrorValue): ErrorConstructorTag {
12
11
  if (error instanceof EvalError) {
@@ -30,62 +29,6 @@ export function getErrorConstructor(error: ErrorValue): ErrorConstructorTag {
30
29
  return ErrorConstructorTag.Error;
31
30
  }
32
31
 
33
- type TypedArrayConstructor =
34
- | Int8ArrayConstructor
35
- | Int16ArrayConstructor
36
- | Int32ArrayConstructor
37
- | Uint8ArrayConstructor
38
- | Uint16ArrayConstructor
39
- | Uint32ArrayConstructor
40
- | Uint8ClampedArrayConstructor
41
- | Float32ArrayConstructor
42
- | Float64ArrayConstructor
43
- | BigInt64ArrayConstructor
44
- | BigUint64ArrayConstructor;
45
-
46
- export function getTypedArrayConstructor(name: string): TypedArrayConstructor {
47
- switch (name) {
48
- case 'Int8Array': return Int8Array;
49
- case 'Int16Array': return Int16Array;
50
- case 'Int32Array': return Int32Array;
51
- case 'Uint8Array': return Uint8Array;
52
- case 'Uint16Array': return Uint16Array;
53
- case 'Uint32Array': return Uint32Array;
54
- case 'Uint8ClampedArray': return Uint8ClampedArray;
55
- case 'Float32Array': return Float32Array;
56
- case 'Float64Array': return Float64Array;
57
- case 'BigInt64Array': return BigInt64Array;
58
- case 'BigUint64Array': return BigUint64Array;
59
- default:
60
- throw new Error(`Unknown TypedArray "${name}"`);
61
- }
62
- }
63
-
64
- const IDENTIFIER_CHECK = /^[$A-Z_][0-9A-Z_$]*$/i;
65
-
66
- export function isValidIdentifier(name: string): boolean {
67
- const char = name[0];
68
- return (
69
- char === '$'
70
- || char === '_'
71
- || (char >= 'A' && char <= 'Z')
72
- || (char >= 'a' && char <= 'z')
73
- ) && IDENTIFIER_CHECK.test(name);
74
- }
75
-
76
- export function getObjectFlag(obj: unknown): SerovalObjectFlags {
77
- if (Object.isFrozen(obj)) {
78
- return SerovalObjectFlags.Frozen;
79
- }
80
- if (Object.isSealed(obj)) {
81
- return SerovalObjectFlags.Sealed;
82
- }
83
- if (Object.isExtensible(obj)) {
84
- return SerovalObjectFlags.None;
85
- }
86
- return SerovalObjectFlags.NonExtensible;
87
- }
88
-
89
32
  export function getErrorOptions(
90
33
  error: Error,
91
34
  features: number,