seroval 1.3.2 → 1.4.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 (112) hide show
  1. package/dist/cjs/development/index.cjs +2640 -2683
  2. package/dist/cjs/development/index.cjs.map +4 -4
  3. package/dist/cjs/production/index.cjs +3 -17
  4. package/dist/esm/development/index.mjs +2640 -2683
  5. package/dist/esm/development/index.mjs.map +4 -4
  6. package/dist/esm/production/index.mjs +3 -17
  7. package/dist/types/core/base-primitives.d.ts +1 -2
  8. package/dist/types/core/base-primitives.d.ts.map +1 -1
  9. package/dist/types/core/compat.d.ts +1 -1
  10. package/dist/types/core/compat.d.ts.map +1 -1
  11. package/dist/types/core/constants.d.ts +1 -0
  12. package/dist/types/core/constants.d.ts.map +1 -1
  13. package/dist/types/core/constructors.d.ts +54 -0
  14. package/dist/types/core/constructors.d.ts.map +1 -0
  15. package/dist/types/core/context/async-parser.d.ts +19 -0
  16. package/dist/types/core/context/async-parser.d.ts.map +1 -0
  17. package/dist/types/core/context/deserializer.d.ts +35 -38
  18. package/dist/types/core/context/deserializer.d.ts.map +1 -1
  19. package/dist/types/core/context/parser.d.ts +32 -19
  20. package/dist/types/core/context/parser.d.ts.map +1 -1
  21. package/dist/types/core/context/serializer.d.ts +38 -99
  22. package/dist/types/core/context/serializer.d.ts.map +1 -1
  23. package/dist/types/core/context/sync-parser.d.ts +59 -0
  24. package/dist/types/core/context/sync-parser.d.ts.map +1 -0
  25. package/dist/types/core/cross/index.d.ts +12 -13
  26. package/dist/types/core/cross/index.d.ts.map +1 -1
  27. package/dist/types/core/errors.d.ts.map +1 -1
  28. package/dist/types/core/function-string.d.ts +2 -2
  29. package/dist/types/core/function-string.d.ts.map +1 -1
  30. package/dist/types/core/plugin.d.ts +13 -11
  31. package/dist/types/core/plugin.d.ts.map +1 -1
  32. package/dist/types/core/reference.d.ts.map +1 -1
  33. package/dist/types/core/special-reference.d.ts +3 -2
  34. package/dist/types/core/special-reference.d.ts.map +1 -1
  35. package/dist/types/core/stream.d.ts +1 -2
  36. package/dist/types/core/stream.d.ts.map +1 -1
  37. package/dist/types/core/symbols.d.ts +14 -0
  38. package/dist/types/core/symbols.d.ts.map +1 -0
  39. package/dist/types/core/tree/index.d.ts +3 -2
  40. package/dist/types/core/tree/index.d.ts.map +1 -1
  41. package/dist/types/core/types.d.ts +3 -1
  42. package/dist/types/core/types.d.ts.map +1 -1
  43. package/dist/types/core/utils/iterator-to-sequence.d.ts.map +1 -1
  44. package/dist/types/index.d.ts +10 -5
  45. package/dist/types/index.d.ts.map +1 -1
  46. package/package.json +17 -19
  47. package/src/core/base-primitives.ts +0 -27
  48. package/src/core/compat.ts +2 -1
  49. package/src/core/constants.ts +28 -26
  50. package/src/core/constructors.ts +269 -0
  51. package/src/core/context/async-parser.ts +586 -0
  52. package/src/core/context/deserializer.ts +506 -316
  53. package/src/core/context/parser.ts +243 -180
  54. package/src/core/context/serializer.ts +1058 -827
  55. package/src/core/context/sync-parser.ts +833 -0
  56. package/src/core/cross/index.ts +63 -47
  57. package/src/core/errors.ts +73 -12
  58. package/src/core/function-string.ts +11 -10
  59. package/src/core/plugin.ts +20 -11
  60. package/src/core/reference.ts +8 -5
  61. package/src/core/special-reference.ts +18 -90
  62. package/src/core/stream.ts +18 -152
  63. package/src/core/symbols.ts +18 -0
  64. package/src/core/tree/index.ts +45 -31
  65. package/src/core/types.ts +7 -2
  66. package/src/core/utils/iterator-to-sequence.ts +6 -28
  67. package/src/index.ts +29 -11
  68. package/dist/types/core/context/parser/async.d.ts +0 -24
  69. package/dist/types/core/context/parser/async.d.ts.map +0 -1
  70. package/dist/types/core/context/parser/stream.d.ts +0 -43
  71. package/dist/types/core/context/parser/stream.d.ts.map +0 -1
  72. package/dist/types/core/context/parser/sync.d.ts +0 -30
  73. package/dist/types/core/context/parser/sync.d.ts.map +0 -1
  74. package/dist/types/core/cross/async.d.ts +0 -8
  75. package/dist/types/core/cross/async.d.ts.map +0 -1
  76. package/dist/types/core/cross/deserializer.d.ts +0 -9
  77. package/dist/types/core/cross/deserializer.d.ts.map +0 -1
  78. package/dist/types/core/cross/parser.d.ts +0 -6
  79. package/dist/types/core/cross/parser.d.ts.map +0 -1
  80. package/dist/types/core/cross/serializer.d.ts +0 -16
  81. package/dist/types/core/cross/serializer.d.ts.map +0 -1
  82. package/dist/types/core/cross/stream.d.ts +0 -8
  83. package/dist/types/core/cross/stream.d.ts.map +0 -1
  84. package/dist/types/core/cross/sync.d.ts +0 -8
  85. package/dist/types/core/cross/sync.d.ts.map +0 -1
  86. package/dist/types/core/tree/async.d.ts +0 -8
  87. package/dist/types/core/tree/async.d.ts.map +0 -1
  88. package/dist/types/core/tree/deserializer.d.ts +0 -13
  89. package/dist/types/core/tree/deserializer.d.ts.map +0 -1
  90. package/dist/types/core/tree/serializer.d.ts +0 -29
  91. package/dist/types/core/tree/serializer.d.ts.map +0 -1
  92. package/dist/types/core/tree/sync.d.ts +0 -8
  93. package/dist/types/core/tree/sync.d.ts.map +0 -1
  94. package/dist/types/core/utils/assert.d.ts +0 -2
  95. package/dist/types/core/utils/assert.d.ts.map +0 -1
  96. package/dist/types/core/utils/deferred.d.ts +0 -7
  97. package/dist/types/core/utils/deferred.d.ts.map +0 -1
  98. package/src/core/context/parser/async.ts +0 -495
  99. package/src/core/context/parser/stream.ts +0 -323
  100. package/src/core/context/parser/sync.ts +0 -404
  101. package/src/core/cross/async.ts +0 -9
  102. package/src/core/cross/deserializer.ts +0 -16
  103. package/src/core/cross/parser.ts +0 -7
  104. package/src/core/cross/serializer.ts +0 -72
  105. package/src/core/cross/stream.ts +0 -9
  106. package/src/core/cross/sync.ts +0 -9
  107. package/src/core/tree/async.ts +0 -9
  108. package/src/core/tree/deserializer.ts +0 -26
  109. package/src/core/tree/serializer.ts +0 -98
  110. package/src/core/tree/sync.ts +0 -9
  111. package/src/core/utils/assert.ts +0 -5
  112. package/src/core/utils/deferred.ts +0 -22
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "seroval",
3
3
  "type": "module",
4
- "version": "1.3.2",
4
+ "version": "1.4.0",
5
5
  "files": [
6
6
  "dist",
7
7
  "src"
@@ -14,25 +14,13 @@
14
14
  "pridepack"
15
15
  ],
16
16
  "devDependencies": {
17
- "@types/node": "^22.15.12",
18
- "@vitest/ui": "^3.1.3",
17
+ "@types/node": "^24.10.0",
18
+ "@vitest/ui": "^4.0.6",
19
19
  "pridepack": "2.6.4",
20
20
  "ts-prune": "^0.10.3",
21
21
  "tslib": "^2.8.1",
22
- "typescript": "^5.8.3",
23
- "vitest": "^3.1.3"
24
- },
25
- "scripts": {
26
- "prepublishOnly": "pridepack clean && pridepack build",
27
- "build": "pridepack build",
28
- "type-check": "pridepack check",
29
- "clean": "pridepack clean",
30
- "watch": "pridepack watch",
31
- "start": "pridepack start",
32
- "dev": "pridepack dev",
33
- "test": "vitest",
34
- "test:ui": "vitest --ui",
35
- "prune": "ts-prune"
22
+ "typescript": "^5.9.3",
23
+ "vitest": "^4.0.6"
36
24
  },
37
25
  "private": false,
38
26
  "description": "Stringify JS values",
@@ -65,5 +53,15 @@
65
53
  "typesVersions": {
66
54
  "*": {}
67
55
  },
68
- "gitHead": "c1e47dd1233aaec1d4fae82c5776c31d5850def9"
69
- }
56
+ "scripts": {
57
+ "build": "pridepack build",
58
+ "type-check": "pridepack check",
59
+ "clean": "pridepack clean",
60
+ "watch": "pridepack watch",
61
+ "start": "pridepack start",
62
+ "dev": "pridepack dev",
63
+ "test": "vitest",
64
+ "test:ui": "vitest --ui",
65
+ "prune": "ts-prune"
66
+ }
67
+ }
@@ -11,7 +11,6 @@ import { getReferenceID } from './reference';
11
11
  import { serializeString } from './string';
12
12
  import type {
13
13
  SerovalAggregateErrorNode,
14
- SerovalArrayBufferNode,
15
14
  SerovalArrayNode,
16
15
  SerovalAsyncIteratorFactoryInstanceNode,
17
16
  SerovalBigIntNode,
@@ -166,32 +165,6 @@ export function createRegExpNode(
166
165
  );
167
166
  }
168
167
 
169
- export function createArrayBufferNode(
170
- id: number,
171
- current: ArrayBuffer,
172
- ): SerovalArrayBufferNode {
173
- const bytes = new Uint8Array(current);
174
- const len = bytes.length;
175
- const values = new Array<number>(len);
176
- for (let i = 0; i < len; i++) {
177
- values[i] = bytes[i];
178
- }
179
- return createSerovalNode(
180
- SerovalNodeType.ArrayBuffer,
181
- id,
182
- values,
183
- NIL,
184
- NIL,
185
- NIL,
186
- NIL,
187
- NIL,
188
- NIL,
189
- NIL,
190
- NIL,
191
- NIL,
192
- );
193
- }
194
-
195
168
  export function createWKSymbolNode(
196
169
  id: number,
197
170
  current: WellKnownSymbols,
@@ -4,8 +4,9 @@
4
4
  * - MDN
5
5
  */
6
6
 
7
- export const enum Feature {
7
+ export enum Feature {
8
8
  AggregateError = 0x01,
9
+ // @deprecated
9
10
  ArrowFunction = 0x02,
10
11
  ErrorPrototypeStack = 0x04,
11
12
  ObjectAssign = 0x08,
@@ -1,3 +1,5 @@
1
+ import { SYM_ASYNC_ITERATOR, SYM_HAS_INSTANCE, SYM_IS_CONCAT_SPREADABLE, SYM_ITERATOR, SYM_MATCH, SYM_MATCH_ALL, SYM_REPLACE, SYM_SEARCH, SYM_SPECIES, SYM_SPLIT, SYM_TO_PRIMITIVE, SYM_TO_STRING_TAG, SYM_UNSCOPABLES } from "./symbols";
2
+
1
3
  export const enum SerovalConstant {
2
4
  Null = 0,
3
5
  Undefined = 1,
@@ -87,37 +89,37 @@ export const SYMBOL_STRING: Record<Symbols, string> = {
87
89
  };
88
90
 
89
91
  export const INV_SYMBOL_REF = /* @__PURE__ */ {
90
- [Symbol.asyncIterator]: Symbols.AsyncIterator,
91
- [Symbol.hasInstance]: Symbols.HasInstance,
92
- [Symbol.isConcatSpreadable]: Symbols.IsConcatSpreadable,
93
- [Symbol.iterator]: Symbols.Iterator,
94
- [Symbol.match]: Symbols.Match,
95
- [Symbol.matchAll]: Symbols.MatchAll,
96
- [Symbol.replace]: Symbols.Replace,
97
- [Symbol.search]: Symbols.Search,
98
- [Symbol.species]: Symbols.Species,
99
- [Symbol.split]: Symbols.Split,
100
- [Symbol.toPrimitive]: Symbols.ToPrimitive,
101
- [Symbol.toStringTag]: Symbols.ToStringTag,
102
- [Symbol.unscopables]: Symbols.Unscopables,
92
+ [SYM_ASYNC_ITERATOR]: Symbols.AsyncIterator,
93
+ [SYM_HAS_INSTANCE]: Symbols.HasInstance,
94
+ [SYM_IS_CONCAT_SPREADABLE]: Symbols.IsConcatSpreadable,
95
+ [SYM_ITERATOR]: Symbols.Iterator,
96
+ [SYM_MATCH]: Symbols.Match,
97
+ [SYM_MATCH_ALL]: Symbols.MatchAll,
98
+ [SYM_REPLACE]: Symbols.Replace,
99
+ [SYM_SEARCH]: Symbols.Search,
100
+ [SYM_SPECIES]: Symbols.Species,
101
+ [SYM_SPLIT]: Symbols.Split,
102
+ [SYM_TO_PRIMITIVE]: Symbols.ToPrimitive,
103
+ [SYM_TO_STRING_TAG]: Symbols.ToStringTag,
104
+ [SYM_UNSCOPABLES]: Symbols.Unscopables,
103
105
  };
104
106
 
105
107
  export type WellKnownSymbols = keyof typeof INV_SYMBOL_REF;
106
108
 
107
109
  export const SYMBOL_REF: Record<Symbols, WellKnownSymbols> = {
108
- [Symbols.AsyncIterator]: Symbol.asyncIterator,
109
- [Symbols.HasInstance]: Symbol.hasInstance,
110
- [Symbols.IsConcatSpreadable]: Symbol.isConcatSpreadable,
111
- [Symbols.Iterator]: Symbol.iterator,
112
- [Symbols.Match]: Symbol.match,
113
- [Symbols.MatchAll]: Symbol.matchAll,
114
- [Symbols.Replace]: Symbol.replace,
115
- [Symbols.Search]: Symbol.search,
116
- [Symbols.Species]: Symbol.species,
117
- [Symbols.Split]: Symbol.split,
118
- [Symbols.ToPrimitive]: Symbol.toPrimitive,
119
- [Symbols.ToStringTag]: Symbol.toStringTag,
120
- [Symbols.Unscopables]: Symbol.unscopables,
110
+ [Symbols.AsyncIterator]: SYM_ASYNC_ITERATOR,
111
+ [Symbols.HasInstance]: SYM_HAS_INSTANCE,
112
+ [Symbols.IsConcatSpreadable]: SYM_IS_CONCAT_SPREADABLE,
113
+ [Symbols.Iterator]: SYM_ITERATOR,
114
+ [Symbols.Match]: SYM_MATCH,
115
+ [Symbols.MatchAll]: SYM_MATCH_ALL,
116
+ [Symbols.Replace]: SYM_REPLACE,
117
+ [Symbols.Search]: SYM_SEARCH,
118
+ [Symbols.Species]: SYM_SPECIES,
119
+ [Symbols.Split]: SYM_SPLIT,
120
+ [Symbols.ToPrimitive]: SYM_TO_PRIMITIVE,
121
+ [Symbols.ToStringTag]: SYM_TO_STRING_TAG,
122
+ [Symbols.Unscopables]: SYM_UNSCOPABLES,
121
123
  };
122
124
 
123
125
  export const CONSTANT_STRING: Record<SerovalConstant, string> = {
@@ -0,0 +1,269 @@
1
+ import type { Stream } from './stream';
2
+
3
+ type SpecialPromise = Promise<unknown> & { s?: 1 | 2; v?: unknown };
4
+
5
+ export interface PromiseConstructorResolver {
6
+ p: SpecialPromise;
7
+ s: (value: unknown) => void;
8
+ f: (value: unknown) => void;
9
+ }
10
+
11
+ export const PROMISE_CONSTRUCTOR = (): PromiseConstructorResolver => {
12
+ const resolver = {
13
+ p: 0,
14
+ s: 0,
15
+ f: 0,
16
+ } as unknown as PromiseConstructorResolver;
17
+ resolver.p = new Promise((resolve, reject) => {
18
+ resolver.s = resolve;
19
+ resolver.f = reject;
20
+ });
21
+ return resolver;
22
+ };
23
+
24
+ export const PROMISE_SUCCESS = (
25
+ resolver: PromiseConstructorResolver,
26
+ data: unknown,
27
+ ): void => {
28
+ resolver.s(data);
29
+ resolver.p.s = 1;
30
+ resolver.p.v = data;
31
+ };
32
+
33
+ export const PROMISE_FAILURE = (
34
+ resolver: PromiseConstructorResolver,
35
+ data: unknown,
36
+ ): void => {
37
+ resolver.f(data);
38
+ resolver.p.s = 2;
39
+ resolver.p.v = data;
40
+ };
41
+
42
+ export const SERIALIZED_PROMISE_CONSTRUCTOR =
43
+ /* @__PURE__ */ PROMISE_CONSTRUCTOR.toString();
44
+ export const SERIALIZED_PROMISE_SUCCESS =
45
+ /* @__PURE__ */ PROMISE_SUCCESS.toString();
46
+ export const SERIALIZED_PROMISE_FAILURE =
47
+ /* @__PURE__ */ PROMISE_FAILURE.toString();
48
+
49
+ interface StreamListener<T> {
50
+ next(value: T): void;
51
+ throw(value: unknown): void;
52
+ return(value: T): void;
53
+ }
54
+
55
+ export const STREAM_CONSTRUCTOR = () => {
56
+ const buffer: unknown[] = [];
57
+ const listeners: StreamListener<unknown>[] = [];
58
+ let alive = true;
59
+ let success = false;
60
+ let count = 0;
61
+ const flush = (
62
+ value: unknown,
63
+ mode: keyof StreamListener<unknown>,
64
+ x?: number,
65
+ ) => {
66
+ for (x = 0; x < count; x++) {
67
+ if (listeners[x]) {
68
+ listeners[x][mode](value);
69
+ }
70
+ }
71
+ };
72
+ const up = (
73
+ listener: StreamListener<unknown>,
74
+ x?: number,
75
+ z?: number,
76
+ current?: unknown,
77
+ ) => {
78
+ for (x = 0, z = buffer.length; x < z; x++) {
79
+ current = buffer[x];
80
+ if (!alive && x === z - 1) {
81
+ listener[success ? 'return' : 'throw'](current);
82
+ } else {
83
+ listener.next(current);
84
+ }
85
+ }
86
+ };
87
+ const on = (listener: StreamListener<unknown>, temp?: number) => {
88
+ if (alive) {
89
+ temp = count++;
90
+ listeners[temp] = listener;
91
+ }
92
+ up(listener);
93
+ return () => {
94
+ if (alive) {
95
+ listeners[temp!] = listeners[count];
96
+ listeners[count--] = undefined as any;
97
+ }
98
+ };
99
+ };
100
+ return {
101
+ __SEROVAL_STREAM__: true,
102
+ on: (listener: StreamListener<unknown>) => on(listener),
103
+ next: (value: unknown) => {
104
+ if (alive) {
105
+ buffer.push(value);
106
+ flush(value, 'next');
107
+ }
108
+ },
109
+ throw: (value: unknown) => {
110
+ if (alive) {
111
+ buffer.push(value);
112
+ flush(value, 'throw');
113
+ alive = false;
114
+ success = false;
115
+ listeners.length = 0;
116
+ }
117
+ },
118
+ return: (value: unknown) => {
119
+ if (alive) {
120
+ buffer.push(value);
121
+ flush(value, 'return');
122
+ alive = false;
123
+ success = true;
124
+ listeners.length = 0;
125
+ }
126
+ },
127
+ };
128
+ };
129
+
130
+ export const SERIALIZED_STREAM_CONSTRUCTOR =
131
+ /* @__PURE__ */ STREAM_CONSTRUCTOR.toString();
132
+
133
+ export interface Sequence {
134
+ v: unknown[];
135
+ t: number;
136
+ d: number;
137
+ }
138
+
139
+ export const ITERATOR_CONSTRUCTOR =
140
+ (symbol: symbol) => (sequence: Sequence) => () => {
141
+ let index = 0;
142
+ const instance = {
143
+ [symbol]: () => instance,
144
+ next: () => {
145
+ if (index > sequence.d) {
146
+ return {
147
+ done: true,
148
+ value: undefined,
149
+ };
150
+ }
151
+ const currentIndex = index++;
152
+ const data = sequence.v[currentIndex];
153
+ if (currentIndex === sequence.t) {
154
+ throw data;
155
+ }
156
+ return {
157
+ done: currentIndex === sequence.d,
158
+ value: data,
159
+ };
160
+ },
161
+ };
162
+ return instance;
163
+ };
164
+
165
+ export const SERIALIZED_ITERATOR_CONSTRUCTOR =
166
+ /* @__PURE__ */ ITERATOR_CONSTRUCTOR.toString();
167
+
168
+ export const ASYNC_ITERATOR_CONSTRUCTOR =
169
+ (symbol: symbol, createPromise: typeof PROMISE_CONSTRUCTOR) =>
170
+ (stream: Stream<unknown>) =>
171
+ () => {
172
+ let count = 0;
173
+ let doneAt = -1;
174
+ let isThrow = false;
175
+ const buffer: unknown[] = [];
176
+ const pending: PromiseConstructorResolver[] = [];
177
+ const finalize = (i = 0, len = pending.length) => {
178
+ for (; i < len; i++) {
179
+ pending[i].s({
180
+ done: true,
181
+ value: undefined,
182
+ });
183
+ }
184
+ };
185
+ stream.on({
186
+ next: value => {
187
+ const temp = pending.shift();
188
+ if (temp) {
189
+ temp.s({ done: false, value });
190
+ }
191
+ buffer.push(value);
192
+ },
193
+ throw: value => {
194
+ const temp = pending.shift();
195
+ if (temp) {
196
+ temp.f(value);
197
+ }
198
+ finalize();
199
+ doneAt = buffer.length;
200
+ isThrow = true;
201
+ buffer.push(value);
202
+ },
203
+ return: value => {
204
+ const temp = pending.shift();
205
+ if (temp) {
206
+ temp.s({ done: true, value });
207
+ }
208
+ finalize();
209
+ doneAt = buffer.length;
210
+ buffer.push(value);
211
+ },
212
+ });
213
+
214
+ const instance = {
215
+ [symbol]: () => instance,
216
+ next: () => {
217
+ if (doneAt === -1) {
218
+ const index = count++;
219
+ if (index >= buffer.length) {
220
+ const temp = createPromise();
221
+ pending.push(temp);
222
+ return temp.p;
223
+ }
224
+ return {
225
+ done: false,
226
+ value: buffer[index],
227
+ };
228
+ }
229
+ if (count > doneAt) {
230
+ return {
231
+ done: true,
232
+ value: undefined,
233
+ };
234
+ }
235
+ const index = count++;
236
+ const value = buffer[index];
237
+ if (index !== doneAt) {
238
+ return {
239
+ done: false,
240
+ value,
241
+ };
242
+ }
243
+ if (isThrow) {
244
+ throw value;
245
+ }
246
+ return {
247
+ done: true,
248
+ value,
249
+ };
250
+ },
251
+ };
252
+
253
+ return instance;
254
+ };
255
+
256
+ export const SERIALIZED_ASYNC_ITERATOR_CONSTRUCTOR =
257
+ /* @__PURE__ */ ASYNC_ITERATOR_CONSTRUCTOR.toString();
258
+
259
+ export const ARRAY_BUFFER_CONSTRUCTOR = (length: number, b64: string) => {
260
+ const decoded = atob(b64);
261
+ const arr = new Uint8Array(length);
262
+ for (let i = 0; i < length; i++) {
263
+ arr[i] = decoded.charCodeAt(i);
264
+ }
265
+ return arr.buffer;
266
+ };
267
+
268
+ export const SERIALIZED_ARRAY_BUFFER_CONSTRUCTOR =
269
+ /* @__PURE__ */ ARRAY_BUFFER_CONSTRUCTOR.toString();