seroval 1.4.1 → 1.4.2
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.
- package/dist/cjs/development/index.cjs +54 -11
- package/dist/cjs/development/index.cjs.map +2 -2
- package/dist/cjs/production/index.cjs +2 -2
- package/dist/esm/development/index.mjs +54 -11
- package/dist/esm/development/index.mjs.map +2 -2
- package/dist/esm/production/index.mjs +2 -2
- package/dist/types/core/base-primitives.d.ts.map +1 -1
- package/dist/types/core/context/async-parser.d.ts.map +1 -1
- package/dist/types/core/context/deserializer.d.ts.map +1 -1
- package/dist/types/core/context/parser.d.ts.map +1 -1
- package/dist/types/core/context/sync-parser.d.ts.map +1 -1
- package/dist/types/core/literals.d.ts.map +1 -1
- package/dist/types/core/node.d.ts +1 -1
- package/dist/types/core/node.d.ts.map +1 -1
- package/dist/types/core/types.d.ts +4 -0
- package/dist/types/core/types.d.ts.map +1 -1
- package/package.json +3 -2
- package/src/core/Serializer.ts +119 -0
- package/src/core/base-primitives.ts +514 -0
- package/src/core/compat.ts +23 -0
- package/src/core/constants.ts +201 -0
- package/src/core/constructors.ts +270 -0
- package/src/core/context/async-parser.ts +638 -0
- package/src/core/context/deserializer.ts +810 -0
- package/src/core/context/parser.ts +335 -0
- package/src/core/context/serializer.ts +1406 -0
- package/src/core/context/sync-parser.ts +907 -0
- package/src/core/cross/index.ts +188 -0
- package/src/core/errors.ts +196 -0
- package/src/core/function-string.ts +32 -0
- package/src/core/keys.ts +17 -0
- package/src/core/literals.ts +43 -0
- package/src/core/node.ts +40 -0
- package/src/core/opaque-reference.ts +9 -0
- package/src/core/plugin.ts +100 -0
- package/src/core/reference.ts +66 -0
- package/src/core/special-reference.ts +42 -0
- package/src/core/stream.ts +71 -0
- package/src/core/string.ts +86 -0
- package/src/core/symbols.ts +18 -0
- package/src/core/tree/index.ts +134 -0
- package/src/core/types.ts +366 -0
- package/src/core/utils/error.ts +73 -0
- package/src/core/utils/get-identifier.ts +18 -0
- package/src/core/utils/get-object-flag.ts +14 -0
- package/src/core/utils/is-valid-identifier.ts +12 -0
- package/src/core/utils/iterator-to-sequence.ts +44 -0
- package/src/core/utils/promise-to-result.ts +9 -0
- package/src/core/utils/typed-array.ts +56 -0
- package/src/index.ts +36 -0
|
@@ -0,0 +1,270 @@
|
|
|
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 = (b64: string) => {
|
|
260
|
+
const decoded = atob(b64);
|
|
261
|
+
const length = decoded.length;
|
|
262
|
+
const arr = new Uint8Array(length);
|
|
263
|
+
for (let i = 0; i < length; i++) {
|
|
264
|
+
arr[i] = decoded.charCodeAt(i);
|
|
265
|
+
}
|
|
266
|
+
return arr.buffer;
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
export const SERIALIZED_ARRAY_BUFFER_CONSTRUCTOR =
|
|
270
|
+
/* @__PURE__ */ ARRAY_BUFFER_CONSTRUCTOR.toString();
|