seroval 1.3.2 → 1.4.1
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 +2817 -2700
- package/dist/cjs/development/index.cjs.map +4 -4
- package/dist/cjs/production/index.cjs +3 -17
- package/dist/esm/development/index.mjs +2817 -2700
- package/dist/esm/development/index.mjs.map +4 -4
- package/dist/esm/production/index.mjs +3 -17
- package/dist/types/core/base-primitives.d.ts +3 -4
- package/dist/types/core/base-primitives.d.ts.map +1 -1
- package/dist/types/core/compat.d.ts +3 -2
- package/dist/types/core/compat.d.ts.map +1 -1
- package/dist/types/core/constants.d.ts +2 -1
- package/dist/types/core/constants.d.ts.map +1 -1
- package/dist/types/core/constructors.d.ts +54 -0
- package/dist/types/core/constructors.d.ts.map +1 -0
- package/dist/types/core/context/async-parser.d.ts +20 -0
- package/dist/types/core/context/async-parser.d.ts.map +1 -0
- package/dist/types/core/context/deserializer.d.ts +40 -38
- package/dist/types/core/context/deserializer.d.ts.map +1 -1
- package/dist/types/core/context/parser.d.ts +34 -19
- package/dist/types/core/context/parser.d.ts.map +1 -1
- package/dist/types/core/context/serializer.d.ts +38 -99
- package/dist/types/core/context/serializer.d.ts.map +1 -1
- package/dist/types/core/context/sync-parser.d.ts +59 -0
- package/dist/types/core/context/sync-parser.d.ts.map +1 -0
- package/dist/types/core/cross/index.d.ts +12 -13
- package/dist/types/core/cross/index.d.ts.map +1 -1
- package/dist/types/core/errors.d.ts +9 -0
- package/dist/types/core/errors.d.ts.map +1 -1
- package/dist/types/core/function-string.d.ts +2 -2
- package/dist/types/core/function-string.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/plugin.d.ts +13 -11
- package/dist/types/core/plugin.d.ts.map +1 -1
- package/dist/types/core/reference.d.ts.map +1 -1
- package/dist/types/core/special-reference.d.ts +3 -2
- package/dist/types/core/special-reference.d.ts.map +1 -1
- package/dist/types/core/stream.d.ts +1 -2
- package/dist/types/core/stream.d.ts.map +1 -1
- package/dist/types/core/string.d.ts.map +1 -1
- package/dist/types/core/symbols.d.ts +14 -0
- package/dist/types/core/symbols.d.ts.map +1 -0
- package/dist/types/core/tree/index.d.ts +7 -3
- package/dist/types/core/tree/index.d.ts.map +1 -1
- package/dist/types/core/types.d.ts +4 -11
- package/dist/types/core/types.d.ts.map +1 -1
- package/dist/types/core/utils/iterator-to-sequence.d.ts.map +1 -1
- package/dist/types/index.d.ts +10 -5
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +18 -21
- package/dist/types/core/context/parser/async.d.ts +0 -24
- package/dist/types/core/context/parser/async.d.ts.map +0 -1
- package/dist/types/core/context/parser/stream.d.ts +0 -43
- package/dist/types/core/context/parser/stream.d.ts.map +0 -1
- package/dist/types/core/context/parser/sync.d.ts +0 -30
- package/dist/types/core/context/parser/sync.d.ts.map +0 -1
- package/dist/types/core/cross/async.d.ts +0 -8
- package/dist/types/core/cross/async.d.ts.map +0 -1
- package/dist/types/core/cross/deserializer.d.ts +0 -9
- package/dist/types/core/cross/deserializer.d.ts.map +0 -1
- package/dist/types/core/cross/parser.d.ts +0 -6
- package/dist/types/core/cross/parser.d.ts.map +0 -1
- package/dist/types/core/cross/serializer.d.ts +0 -16
- package/dist/types/core/cross/serializer.d.ts.map +0 -1
- package/dist/types/core/cross/stream.d.ts +0 -8
- package/dist/types/core/cross/stream.d.ts.map +0 -1
- package/dist/types/core/cross/sync.d.ts +0 -8
- package/dist/types/core/cross/sync.d.ts.map +0 -1
- package/dist/types/core/tree/async.d.ts +0 -8
- package/dist/types/core/tree/async.d.ts.map +0 -1
- package/dist/types/core/tree/deserializer.d.ts +0 -13
- package/dist/types/core/tree/deserializer.d.ts.map +0 -1
- package/dist/types/core/tree/serializer.d.ts +0 -29
- package/dist/types/core/tree/serializer.d.ts.map +0 -1
- package/dist/types/core/tree/sync.d.ts +0 -8
- package/dist/types/core/tree/sync.d.ts.map +0 -1
- package/dist/types/core/utils/assert.d.ts +0 -2
- package/dist/types/core/utils/assert.d.ts.map +0 -1
- package/dist/types/core/utils/deferred.d.ts +0 -7
- package/dist/types/core/utils/deferred.d.ts.map +0 -1
- package/src/core/Serializer.ts +0 -119
- package/src/core/base-primitives.ts +0 -542
- package/src/core/compat.ts +0 -20
- package/src/core/constants.ts +0 -185
- package/src/core/context/deserializer.ts +0 -456
- package/src/core/context/parser/async.ts +0 -495
- package/src/core/context/parser/stream.ts +0 -323
- package/src/core/context/parser/sync.ts +0 -404
- package/src/core/context/parser.ts +0 -270
- package/src/core/context/serializer.ts +0 -1180
- package/src/core/cross/async.ts +0 -9
- package/src/core/cross/deserializer.ts +0 -16
- package/src/core/cross/index.ts +0 -170
- package/src/core/cross/parser.ts +0 -7
- package/src/core/cross/serializer.ts +0 -72
- package/src/core/cross/stream.ts +0 -9
- package/src/core/cross/sync.ts +0 -9
- package/src/core/errors.ts +0 -102
- package/src/core/function-string.ts +0 -31
- package/src/core/keys.ts +0 -17
- package/src/core/literals.ts +0 -43
- package/src/core/node.ts +0 -40
- package/src/core/opaque-reference.ts +0 -9
- package/src/core/plugin.ts +0 -91
- package/src/core/reference.ts +0 -63
- package/src/core/special-reference.ts +0 -114
- package/src/core/stream.ts +0 -205
- package/src/core/string.ts +0 -84
- package/src/core/tree/async.ts +0 -9
- package/src/core/tree/deserializer.ts +0 -26
- package/src/core/tree/index.ts +0 -111
- package/src/core/tree/serializer.ts +0 -98
- package/src/core/tree/sync.ts +0 -9
- package/src/core/types.ts +0 -370
- package/src/core/utils/assert.ts +0 -5
- package/src/core/utils/deferred.ts +0 -22
- package/src/core/utils/error.ts +0 -73
- package/src/core/utils/get-identifier.ts +0 -18
- package/src/core/utils/get-object-flag.ts +0 -14
- package/src/core/utils/is-valid-identifier.ts +0 -12
- package/src/core/utils/iterator-to-sequence.ts +0 -66
- package/src/core/utils/promise-to-result.ts +0 -9
- package/src/core/utils/typed-array.ts +0 -56
- package/src/index.ts +0 -18
package/src/core/reference.ts
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
SerovalMissingReferenceError,
|
|
3
|
-
SerovalMissingReferenceForIdError,
|
|
4
|
-
} from '..';
|
|
5
|
-
import { REFERENCES_KEY } from './keys';
|
|
6
|
-
import assert from './utils/assert';
|
|
7
|
-
|
|
8
|
-
const REFERENCE = new Map<unknown, string>();
|
|
9
|
-
const INV_REFERENCE = new Map<string, unknown>();
|
|
10
|
-
|
|
11
|
-
export function createReference<T>(id: string, value: T): T {
|
|
12
|
-
REFERENCE.set(value, id);
|
|
13
|
-
INV_REFERENCE.set(id, value);
|
|
14
|
-
return value;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export function hasReferenceID<T>(value: T): boolean {
|
|
18
|
-
return REFERENCE.has(value);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function hasReference(id: string): boolean {
|
|
22
|
-
return INV_REFERENCE.has(id);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export function getReferenceID<T>(value: T): string {
|
|
26
|
-
assert(hasReferenceID(value), new SerovalMissingReferenceError(value));
|
|
27
|
-
return REFERENCE.get(value)!;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export function getReference<T>(id: string): T {
|
|
31
|
-
assert(hasReference(id), new SerovalMissingReferenceForIdError(id));
|
|
32
|
-
return INV_REFERENCE.get(id) as T;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
if (typeof globalThis !== 'undefined') {
|
|
36
|
-
Object.defineProperty(globalThis, REFERENCES_KEY, {
|
|
37
|
-
value: INV_REFERENCE,
|
|
38
|
-
configurable: true,
|
|
39
|
-
writable: false,
|
|
40
|
-
enumerable: false,
|
|
41
|
-
});
|
|
42
|
-
} else if (typeof window !== 'undefined') {
|
|
43
|
-
Object.defineProperty(window, REFERENCES_KEY, {
|
|
44
|
-
value: INV_REFERENCE,
|
|
45
|
-
configurable: true,
|
|
46
|
-
writable: false,
|
|
47
|
-
enumerable: false,
|
|
48
|
-
});
|
|
49
|
-
} else if (typeof self !== 'undefined') {
|
|
50
|
-
Object.defineProperty(self, REFERENCES_KEY, {
|
|
51
|
-
value: INV_REFERENCE,
|
|
52
|
-
configurable: true,
|
|
53
|
-
writable: false,
|
|
54
|
-
enumerable: false,
|
|
55
|
-
});
|
|
56
|
-
} else if (typeof global !== 'undefined') {
|
|
57
|
-
Object.defineProperty(global, REFERENCES_KEY, {
|
|
58
|
-
value: INV_REFERENCE,
|
|
59
|
-
configurable: true,
|
|
60
|
-
writable: false,
|
|
61
|
-
enumerable: false,
|
|
62
|
-
});
|
|
63
|
-
}
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
import { createEffectfulFunction, createFunction } from './function-string';
|
|
2
|
-
|
|
3
|
-
export const ITERATOR = {};
|
|
4
|
-
|
|
5
|
-
export const ASYNC_ITERATOR = {};
|
|
6
|
-
|
|
7
|
-
export const enum SpecialReference {
|
|
8
|
-
MapSentinel = 0,
|
|
9
|
-
PromiseConstructor = 1,
|
|
10
|
-
PromiseSuccess = 2,
|
|
11
|
-
PromiseFailure = 3,
|
|
12
|
-
StreamConstructor = 4,
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Placeholder references
|
|
17
|
-
*/
|
|
18
|
-
export const SPECIAL_REFS: Record<SpecialReference, unknown> = {
|
|
19
|
-
[SpecialReference.MapSentinel]: {},
|
|
20
|
-
[SpecialReference.PromiseConstructor]: {},
|
|
21
|
-
[SpecialReference.PromiseSuccess]: {},
|
|
22
|
-
[SpecialReference.PromiseFailure]: {},
|
|
23
|
-
[SpecialReference.StreamConstructor]: {},
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
function serializePromiseConstructor(features: number): string {
|
|
27
|
-
return createFunction(
|
|
28
|
-
features,
|
|
29
|
-
['r'],
|
|
30
|
-
'(r.p=new Promise(' +
|
|
31
|
-
createEffectfulFunction(features, ['s', 'f'], 'r.s=s,r.f=f') +
|
|
32
|
-
'))',
|
|
33
|
-
);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function serializePromiseSuccess(features: number): string {
|
|
37
|
-
return createEffectfulFunction(
|
|
38
|
-
features,
|
|
39
|
-
['r', 'd'],
|
|
40
|
-
'r.s(d),r.p.s=1,r.p.v=d',
|
|
41
|
-
);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function serializePromiseFailure(features: number): string {
|
|
45
|
-
return createEffectfulFunction(
|
|
46
|
-
features,
|
|
47
|
-
['r', 'd'],
|
|
48
|
-
'r.f(d),r.p.s=2,r.p.v=d',
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function serializeStreamConstructor(features: number): string {
|
|
53
|
-
return createFunction(
|
|
54
|
-
features,
|
|
55
|
-
['b', 'a', 's', 'l', 'p', 'f', 'e', 'n'],
|
|
56
|
-
'(b=[],a=!0,s=!1,l=[],p=0,f=' +
|
|
57
|
-
createEffectfulFunction(
|
|
58
|
-
features,
|
|
59
|
-
['v', 'm', 'x'],
|
|
60
|
-
'for(x=0;x<p;x++)l[x]&&l[x][m](v)',
|
|
61
|
-
) +
|
|
62
|
-
',n=' +
|
|
63
|
-
createEffectfulFunction(
|
|
64
|
-
features,
|
|
65
|
-
['o', 'x', 'z', 'c'],
|
|
66
|
-
'for(x=0,z=b.length;x<z;x++)(c=b[x],(!a&&x===z-1)?o[s?"return":"throw"](c):o.next(c))',
|
|
67
|
-
) +
|
|
68
|
-
',e=' +
|
|
69
|
-
createFunction(
|
|
70
|
-
features,
|
|
71
|
-
['o', 't'],
|
|
72
|
-
'(a&&(l[t=p++]=o),n(o),' +
|
|
73
|
-
createEffectfulFunction(features, [], 'a&&(l[t]=void 0)') +
|
|
74
|
-
')',
|
|
75
|
-
) +
|
|
76
|
-
',{__SEROVAL_STREAM__:!0,on:' +
|
|
77
|
-
createFunction(features, ['o'], 'e(o)') +
|
|
78
|
-
',next:' +
|
|
79
|
-
createEffectfulFunction(features, ['v'], 'a&&(b.push(v),f(v,"next"))') +
|
|
80
|
-
',throw:' +
|
|
81
|
-
createEffectfulFunction(
|
|
82
|
-
features,
|
|
83
|
-
['v'],
|
|
84
|
-
'a&&(b.push(v),f(v,"throw"),a=s=!1,l.length=0)',
|
|
85
|
-
) +
|
|
86
|
-
',return:' +
|
|
87
|
-
createEffectfulFunction(
|
|
88
|
-
features,
|
|
89
|
-
['v'],
|
|
90
|
-
'a&&(b.push(v),f(v,"return"),a=!1,s=!0,l.length=0)',
|
|
91
|
-
) +
|
|
92
|
-
'})',
|
|
93
|
-
);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export function serializeSpecialReferenceValue(
|
|
97
|
-
features: number,
|
|
98
|
-
ref: SpecialReference,
|
|
99
|
-
): string {
|
|
100
|
-
switch (ref) {
|
|
101
|
-
case SpecialReference.MapSentinel:
|
|
102
|
-
return '[]';
|
|
103
|
-
case SpecialReference.PromiseConstructor:
|
|
104
|
-
return serializePromiseConstructor(features);
|
|
105
|
-
case SpecialReference.PromiseSuccess:
|
|
106
|
-
return serializePromiseSuccess(features);
|
|
107
|
-
case SpecialReference.PromiseFailure:
|
|
108
|
-
return serializePromiseFailure(features);
|
|
109
|
-
case SpecialReference.StreamConstructor:
|
|
110
|
-
return serializeStreamConstructor(features);
|
|
111
|
-
default:
|
|
112
|
-
return '';
|
|
113
|
-
}
|
|
114
|
-
}
|
package/src/core/stream.ts
DELETED
|
@@ -1,205 +0,0 @@
|
|
|
1
|
-
import type { Deferred } from './utils/deferred';
|
|
2
|
-
import { createDeferred } from './utils/deferred';
|
|
3
|
-
|
|
4
|
-
interface StreamListener<T> {
|
|
5
|
-
next(value: T): void;
|
|
6
|
-
throw(value: unknown): void;
|
|
7
|
-
return(value: T): void;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export interface Stream<T> {
|
|
11
|
-
__SEROVAL_STREAM__: true;
|
|
12
|
-
|
|
13
|
-
on(listener: StreamListener<T>): () => void;
|
|
14
|
-
|
|
15
|
-
next(value: T): void;
|
|
16
|
-
throw(value: unknown): void;
|
|
17
|
-
return(value: T): void;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function isStream<T>(value: object): value is Stream<T> {
|
|
21
|
-
return '__SEROVAL_STREAM__' in value;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function createStream<T>(): Stream<T> {
|
|
25
|
-
const listeners = new Set<StreamListener<T>>();
|
|
26
|
-
const buffer: unknown[] = [];
|
|
27
|
-
let alive = true;
|
|
28
|
-
let success = true;
|
|
29
|
-
|
|
30
|
-
function flushNext(value: T): void {
|
|
31
|
-
for (const listener of listeners.keys()) {
|
|
32
|
-
listener.next(value);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function flushThrow(value: unknown): void {
|
|
37
|
-
for (const listener of listeners.keys()) {
|
|
38
|
-
listener.throw(value);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function flushReturn(value: T): void {
|
|
43
|
-
for (const listener of listeners.keys()) {
|
|
44
|
-
listener.return(value);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return {
|
|
49
|
-
__SEROVAL_STREAM__: true,
|
|
50
|
-
on(listener: StreamListener<T>): () => void {
|
|
51
|
-
if (alive) {
|
|
52
|
-
listeners.add(listener);
|
|
53
|
-
}
|
|
54
|
-
for (let i = 0, len = buffer.length; i < len; i++) {
|
|
55
|
-
const value = buffer[i];
|
|
56
|
-
if (i === len - 1 && !alive) {
|
|
57
|
-
if (success) {
|
|
58
|
-
listener.return(value as T);
|
|
59
|
-
} else {
|
|
60
|
-
listener.throw(value);
|
|
61
|
-
}
|
|
62
|
-
} else {
|
|
63
|
-
listener.next(value as T);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
return () => {
|
|
67
|
-
if (alive) {
|
|
68
|
-
listeners.delete(listener);
|
|
69
|
-
}
|
|
70
|
-
};
|
|
71
|
-
},
|
|
72
|
-
next(value): void {
|
|
73
|
-
if (alive) {
|
|
74
|
-
buffer.push(value);
|
|
75
|
-
flushNext(value);
|
|
76
|
-
}
|
|
77
|
-
},
|
|
78
|
-
throw(value): void {
|
|
79
|
-
if (alive) {
|
|
80
|
-
buffer.push(value);
|
|
81
|
-
flushThrow(value);
|
|
82
|
-
alive = false;
|
|
83
|
-
success = false;
|
|
84
|
-
listeners.clear();
|
|
85
|
-
}
|
|
86
|
-
},
|
|
87
|
-
return(value): void {
|
|
88
|
-
if (alive) {
|
|
89
|
-
buffer.push(value);
|
|
90
|
-
flushReturn(value);
|
|
91
|
-
alive = false;
|
|
92
|
-
success = true;
|
|
93
|
-
listeners.clear();
|
|
94
|
-
}
|
|
95
|
-
},
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export function createStreamFromAsyncIterable<T>(
|
|
100
|
-
iterable: AsyncIterable<T>,
|
|
101
|
-
): Stream<T> {
|
|
102
|
-
const stream = createStream<T>();
|
|
103
|
-
|
|
104
|
-
const iterator = iterable[Symbol.asyncIterator]();
|
|
105
|
-
|
|
106
|
-
async function push(): Promise<void> {
|
|
107
|
-
try {
|
|
108
|
-
const value = await iterator.next();
|
|
109
|
-
if (value.done) {
|
|
110
|
-
stream.return(value.value as T);
|
|
111
|
-
} else {
|
|
112
|
-
stream.next(value.value);
|
|
113
|
-
await push();
|
|
114
|
-
}
|
|
115
|
-
} catch (error) {
|
|
116
|
-
stream.throw(error);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
push().catch(() => {
|
|
121
|
-
// no-op
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
return stream;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
export function streamToAsyncIterable<T>(
|
|
128
|
-
stream: Stream<T>,
|
|
129
|
-
): () => AsyncIterableIterator<T> {
|
|
130
|
-
return (): AsyncIterableIterator<T> => {
|
|
131
|
-
const buffer: T[] = [];
|
|
132
|
-
const pending: Deferred[] = [];
|
|
133
|
-
let count = 0;
|
|
134
|
-
let doneAt = -1;
|
|
135
|
-
let isThrow = false;
|
|
136
|
-
|
|
137
|
-
function resolveAll(): void {
|
|
138
|
-
for (let i = 0, len = pending.length; i < len; i++) {
|
|
139
|
-
pending[i].resolve({ done: true, value: undefined });
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
stream.on({
|
|
144
|
-
next(value) {
|
|
145
|
-
const current = pending.shift();
|
|
146
|
-
if (current) {
|
|
147
|
-
current.resolve({ done: false, value });
|
|
148
|
-
}
|
|
149
|
-
buffer.push(value);
|
|
150
|
-
},
|
|
151
|
-
throw(value) {
|
|
152
|
-
const current = pending.shift();
|
|
153
|
-
if (current) {
|
|
154
|
-
current.reject(value);
|
|
155
|
-
}
|
|
156
|
-
resolveAll();
|
|
157
|
-
doneAt = buffer.length;
|
|
158
|
-
buffer.push(value as T);
|
|
159
|
-
isThrow = true;
|
|
160
|
-
},
|
|
161
|
-
return(value) {
|
|
162
|
-
const current = pending.shift();
|
|
163
|
-
if (current) {
|
|
164
|
-
current.resolve({ done: true, value });
|
|
165
|
-
}
|
|
166
|
-
resolveAll();
|
|
167
|
-
doneAt = buffer.length;
|
|
168
|
-
buffer.push(value);
|
|
169
|
-
},
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
function finalize() {
|
|
173
|
-
const current = count++;
|
|
174
|
-
const value = buffer[current];
|
|
175
|
-
if (current !== doneAt) {
|
|
176
|
-
return { done: false, value };
|
|
177
|
-
}
|
|
178
|
-
if (isThrow) {
|
|
179
|
-
throw value;
|
|
180
|
-
}
|
|
181
|
-
return { done: true, value };
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
return {
|
|
185
|
-
[Symbol.asyncIterator](): AsyncIterableIterator<T> {
|
|
186
|
-
return this;
|
|
187
|
-
},
|
|
188
|
-
async next(): Promise<IteratorResult<T>> {
|
|
189
|
-
if (doneAt === -1) {
|
|
190
|
-
const current = count++;
|
|
191
|
-
if (current >= buffer.length) {
|
|
192
|
-
const deferred = createDeferred();
|
|
193
|
-
pending.push(deferred);
|
|
194
|
-
return (await deferred.promise) as Promise<IteratorResult<T>>;
|
|
195
|
-
}
|
|
196
|
-
return { done: false, value: buffer[current] };
|
|
197
|
-
}
|
|
198
|
-
if (count > doneAt) {
|
|
199
|
-
return { done: true, value: undefined };
|
|
200
|
-
}
|
|
201
|
-
return finalize();
|
|
202
|
-
},
|
|
203
|
-
};
|
|
204
|
-
};
|
|
205
|
-
}
|
package/src/core/string.ts
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
export function serializeChar(str: string): string | undefined {
|
|
2
|
-
switch (str) {
|
|
3
|
-
case '"':
|
|
4
|
-
return '\\"';
|
|
5
|
-
case '\\':
|
|
6
|
-
return '\\\\';
|
|
7
|
-
case '\n':
|
|
8
|
-
return '\\n';
|
|
9
|
-
case '\r':
|
|
10
|
-
return '\\r';
|
|
11
|
-
case '\b':
|
|
12
|
-
return '\\b';
|
|
13
|
-
case '\t':
|
|
14
|
-
return '\\t';
|
|
15
|
-
case '\f':
|
|
16
|
-
return '\\f';
|
|
17
|
-
case '<':
|
|
18
|
-
return '\\x3C';
|
|
19
|
-
case '\u2028':
|
|
20
|
-
return '\\u2028';
|
|
21
|
-
case '\u2029':
|
|
22
|
-
return '\\u2029';
|
|
23
|
-
default:
|
|
24
|
-
return undefined;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// Written by https://github.com/DylanPiercey and is distributed under the MIT license.
|
|
29
|
-
// Creates a JavaScript double quoted string and escapes all characters
|
|
30
|
-
// not listed as DoubleStringCharacters on
|
|
31
|
-
// Also includes "<" to escape "</script>" and "\" to avoid invalid escapes in the output.
|
|
32
|
-
// http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4
|
|
33
|
-
export function serializeString(str: string): string {
|
|
34
|
-
let result = '';
|
|
35
|
-
let lastPos = 0;
|
|
36
|
-
let replacement: string | undefined;
|
|
37
|
-
for (let i = 0, len = str.length; i < len; i++) {
|
|
38
|
-
replacement = serializeChar(str[i]);
|
|
39
|
-
if (replacement) {
|
|
40
|
-
result += str.slice(lastPos, i) + replacement;
|
|
41
|
-
lastPos = i + 1;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
if (lastPos === 0) {
|
|
45
|
-
result = str;
|
|
46
|
-
} else {
|
|
47
|
-
result += str.slice(lastPos);
|
|
48
|
-
}
|
|
49
|
-
return result;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function deserializeReplacer(str: string): string {
|
|
53
|
-
switch (str) {
|
|
54
|
-
case '\\\\':
|
|
55
|
-
return '\\';
|
|
56
|
-
case '\\"':
|
|
57
|
-
return '"';
|
|
58
|
-
case '\\n':
|
|
59
|
-
return '\n';
|
|
60
|
-
case '\\r':
|
|
61
|
-
return '\r';
|
|
62
|
-
case '\\b':
|
|
63
|
-
return '\b';
|
|
64
|
-
case '\\t':
|
|
65
|
-
return '\t';
|
|
66
|
-
case '\\f':
|
|
67
|
-
return '\f';
|
|
68
|
-
case '\\x3C':
|
|
69
|
-
return '\x3C';
|
|
70
|
-
case '\\u2028':
|
|
71
|
-
return '\u2028';
|
|
72
|
-
case '\\u2029':
|
|
73
|
-
return '\u2029';
|
|
74
|
-
default:
|
|
75
|
-
return str;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export function deserializeString(str: string): string {
|
|
80
|
-
return str.replace(
|
|
81
|
-
/(\\\\|\\"|\\n|\\r|\\b|\\t|\\f|\\u2028|\\u2029|\\x3C)/g,
|
|
82
|
-
deserializeReplacer,
|
|
83
|
-
);
|
|
84
|
-
}
|
package/src/core/tree/async.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { BaseParserContextOptions } from '../context/parser';
|
|
2
|
-
import BaseAsyncParserContext from '../context/parser/async';
|
|
3
|
-
import type { SerovalMode } from '../plugin';
|
|
4
|
-
|
|
5
|
-
export type AsyncParserContextOptions = Omit<BaseParserContextOptions, 'refs'>;
|
|
6
|
-
|
|
7
|
-
export default class AsyncParserContext extends BaseAsyncParserContext {
|
|
8
|
-
readonly mode: SerovalMode = 'vanilla';
|
|
9
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import type { BaseDeserializerOptions } from '../context/deserializer';
|
|
2
|
-
import BaseDeserializerContext from '../context/deserializer';
|
|
3
|
-
import type { SerovalMode } from '../plugin';
|
|
4
|
-
|
|
5
|
-
export interface VanillaDeserializerContextOptions
|
|
6
|
-
extends Omit<BaseDeserializerOptions, 'refs'> {
|
|
7
|
-
markedRefs: number[] | Set<number>;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export default class VanillaDeserializerContext extends BaseDeserializerContext {
|
|
11
|
-
readonly mode: SerovalMode = 'vanilla';
|
|
12
|
-
|
|
13
|
-
marked: Set<number>;
|
|
14
|
-
|
|
15
|
-
constructor(options: VanillaDeserializerContextOptions) {
|
|
16
|
-
super(options);
|
|
17
|
-
this.marked = new Set(options.markedRefs);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
assignIndexedValue<T>(index: number, value: T): T {
|
|
21
|
-
if (this.marked.has(index)) {
|
|
22
|
-
this.refs.set(index, value);
|
|
23
|
-
}
|
|
24
|
-
return value;
|
|
25
|
-
}
|
|
26
|
-
}
|
package/src/core/tree/index.ts
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import { type PluginAccessOptions, resolvePlugins } from '../plugin';
|
|
2
|
-
import type { SerovalNode } from '../types';
|
|
3
|
-
import type { AsyncParserContextOptions } from './async';
|
|
4
|
-
import AsyncParserContext from './async';
|
|
5
|
-
import VanillaDeserializerContext from './deserializer';
|
|
6
|
-
import VanillaSerializerContext from './serializer';
|
|
7
|
-
import type { SyncParserContextOptions } from './sync';
|
|
8
|
-
import SyncParserContext from './sync';
|
|
9
|
-
|
|
10
|
-
export function serialize<T>(
|
|
11
|
-
source: T,
|
|
12
|
-
options: SyncParserContextOptions = {},
|
|
13
|
-
): string {
|
|
14
|
-
const plugins = resolvePlugins(options.plugins);
|
|
15
|
-
const ctx = new SyncParserContext({
|
|
16
|
-
plugins,
|
|
17
|
-
disabledFeatures: options.disabledFeatures,
|
|
18
|
-
});
|
|
19
|
-
const tree = ctx.parseTop(source);
|
|
20
|
-
const serial = new VanillaSerializerContext({
|
|
21
|
-
plugins,
|
|
22
|
-
features: ctx.features,
|
|
23
|
-
markedRefs: ctx.marked,
|
|
24
|
-
});
|
|
25
|
-
return serial.serializeTop(tree);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export async function serializeAsync<T>(
|
|
29
|
-
source: T,
|
|
30
|
-
options: AsyncParserContextOptions = {},
|
|
31
|
-
): Promise<string> {
|
|
32
|
-
const plugins = resolvePlugins(options.plugins);
|
|
33
|
-
const ctx = new AsyncParserContext({
|
|
34
|
-
plugins,
|
|
35
|
-
disabledFeatures: options.disabledFeatures,
|
|
36
|
-
});
|
|
37
|
-
const tree = await ctx.parseTop(source);
|
|
38
|
-
const serial = new VanillaSerializerContext({
|
|
39
|
-
plugins,
|
|
40
|
-
features: ctx.features,
|
|
41
|
-
markedRefs: ctx.marked,
|
|
42
|
-
});
|
|
43
|
-
return serial.serializeTop(tree);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export function deserialize<T>(source: string): T {
|
|
47
|
-
return (0, eval)(source) as T;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export interface SerovalJSON {
|
|
51
|
-
t: SerovalNode;
|
|
52
|
-
f: number;
|
|
53
|
-
m: number[];
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export function toJSON<T>(
|
|
57
|
-
source: T,
|
|
58
|
-
options: SyncParserContextOptions = {},
|
|
59
|
-
): SerovalJSON {
|
|
60
|
-
const plugins = resolvePlugins(options.plugins);
|
|
61
|
-
const ctx = new SyncParserContext({
|
|
62
|
-
plugins,
|
|
63
|
-
disabledFeatures: options.disabledFeatures,
|
|
64
|
-
});
|
|
65
|
-
return {
|
|
66
|
-
t: ctx.parseTop(source),
|
|
67
|
-
f: ctx.features,
|
|
68
|
-
m: Array.from(ctx.marked),
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export async function toJSONAsync<T>(
|
|
73
|
-
source: T,
|
|
74
|
-
options: AsyncParserContextOptions = {},
|
|
75
|
-
): Promise<SerovalJSON> {
|
|
76
|
-
const plugins = resolvePlugins(options.plugins);
|
|
77
|
-
const ctx = new AsyncParserContext({
|
|
78
|
-
plugins,
|
|
79
|
-
disabledFeatures: options.disabledFeatures,
|
|
80
|
-
});
|
|
81
|
-
return {
|
|
82
|
-
t: await ctx.parseTop(source),
|
|
83
|
-
f: ctx.features,
|
|
84
|
-
m: Array.from(ctx.marked),
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
export function compileJSON(
|
|
89
|
-
source: SerovalJSON,
|
|
90
|
-
options: PluginAccessOptions = {},
|
|
91
|
-
): string {
|
|
92
|
-
const plugins = resolvePlugins(options.plugins);
|
|
93
|
-
const ctx = new VanillaSerializerContext({
|
|
94
|
-
plugins,
|
|
95
|
-
features: source.f,
|
|
96
|
-
markedRefs: source.m,
|
|
97
|
-
});
|
|
98
|
-
return ctx.serializeTop(source.t);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export function fromJSON<T>(
|
|
102
|
-
source: SerovalJSON,
|
|
103
|
-
options: PluginAccessOptions = {},
|
|
104
|
-
): T {
|
|
105
|
-
const plugins = resolvePlugins(options.plugins);
|
|
106
|
-
const ctx = new VanillaDeserializerContext({
|
|
107
|
-
plugins,
|
|
108
|
-
markedRefs: source.m,
|
|
109
|
-
});
|
|
110
|
-
return ctx.deserializeTop(source.t) as T;
|
|
111
|
-
}
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import { SerovalNodeType } from '../constants';
|
|
2
|
-
import type { BaseSerializerContextOptions } from '../context/serializer';
|
|
3
|
-
import BaseSerializerContext from '../context/serializer';
|
|
4
|
-
import { SerovalUnsupportedNodeError } from '../errors';
|
|
5
|
-
import type { SerovalMode } from '../plugin';
|
|
6
|
-
import type {
|
|
7
|
-
SerovalNode,
|
|
8
|
-
SerovalPromiseConstructorNode,
|
|
9
|
-
SerovalPromiseRejectNode,
|
|
10
|
-
SerovalPromiseResolveNode,
|
|
11
|
-
} from '../types';
|
|
12
|
-
import getIdentifier from '../utils/get-identifier';
|
|
13
|
-
|
|
14
|
-
export type VanillaSerializerContextOptions = BaseSerializerContextOptions;
|
|
15
|
-
|
|
16
|
-
export default class VanillaSerializerContext extends BaseSerializerContext {
|
|
17
|
-
readonly mode: SerovalMode = 'vanilla';
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Map tree refs to actual refs
|
|
21
|
-
* @private
|
|
22
|
-
*/
|
|
23
|
-
valid = new Map<number, number>();
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Variables
|
|
27
|
-
* @private
|
|
28
|
-
*/
|
|
29
|
-
vars: string[] = [];
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Creates the reference param (identifier) from the given reference ID
|
|
33
|
-
* Calling this function means the value has been referenced somewhere
|
|
34
|
-
*/
|
|
35
|
-
getRefParam(index: number): string {
|
|
36
|
-
/**
|
|
37
|
-
* Creates a new reference ID from a given reference ID
|
|
38
|
-
* This new reference ID means that the reference itself
|
|
39
|
-
* has been referenced at least once, and is used to generate
|
|
40
|
-
* the variables
|
|
41
|
-
*/
|
|
42
|
-
let actualIndex = this.valid.get(index);
|
|
43
|
-
if (actualIndex == null) {
|
|
44
|
-
actualIndex = this.valid.size;
|
|
45
|
-
this.valid.set(index, actualIndex);
|
|
46
|
-
}
|
|
47
|
-
let identifier = this.vars[actualIndex];
|
|
48
|
-
if (identifier == null) {
|
|
49
|
-
identifier = getIdentifier(actualIndex);
|
|
50
|
-
this.vars[actualIndex] = identifier;
|
|
51
|
-
}
|
|
52
|
-
return identifier;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
protected assignIndexedValue(index: number, value: string): string {
|
|
56
|
-
if (this.isMarked(index)) {
|
|
57
|
-
return this.getRefParam(index) + '=' + value;
|
|
58
|
-
}
|
|
59
|
-
return value;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
protected serializePromiseConstructor(
|
|
63
|
-
node: SerovalPromiseConstructorNode,
|
|
64
|
-
): string {
|
|
65
|
-
throw new SerovalUnsupportedNodeError(node);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
protected serializePromiseResolve(node: SerovalPromiseResolveNode): string {
|
|
69
|
-
throw new SerovalUnsupportedNodeError(node);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
protected serializePromiseReject(node: SerovalPromiseRejectNode): string {
|
|
73
|
-
throw new SerovalUnsupportedNodeError(node);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
serializeTop(tree: SerovalNode): string {
|
|
77
|
-
const result = this.serialize(tree);
|
|
78
|
-
// Shared references detected
|
|
79
|
-
if (tree.i != null && this.vars.length) {
|
|
80
|
-
const patches = this.resolvePatches();
|
|
81
|
-
let body = result;
|
|
82
|
-
if (patches) {
|
|
83
|
-
// Get (or create) a ref from the source
|
|
84
|
-
const index = this.getRefParam(tree.i);
|
|
85
|
-
body = result + ',' + patches + index;
|
|
86
|
-
if (!result.startsWith(index + '=')) {
|
|
87
|
-
body = index + '=' + body;
|
|
88
|
-
}
|
|
89
|
-
body = '(' + body + ')';
|
|
90
|
-
}
|
|
91
|
-
return '(' + this.createFunction(this.vars, body) + ')()';
|
|
92
|
-
}
|
|
93
|
-
if (tree.t === SerovalNodeType.Object) {
|
|
94
|
-
return '(' + result + ')';
|
|
95
|
-
}
|
|
96
|
-
return result;
|
|
97
|
-
}
|
|
98
|
-
}
|