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,42 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SERIALIZED_ARRAY_BUFFER_CONSTRUCTOR,
|
|
3
|
+
SERIALIZED_PROMISE_CONSTRUCTOR,
|
|
4
|
+
SERIALIZED_PROMISE_FAILURE,
|
|
5
|
+
SERIALIZED_PROMISE_SUCCESS,
|
|
6
|
+
SERIALIZED_STREAM_CONSTRUCTOR,
|
|
7
|
+
} from './constructors';
|
|
8
|
+
|
|
9
|
+
export const ITERATOR = {};
|
|
10
|
+
|
|
11
|
+
export const ASYNC_ITERATOR = {};
|
|
12
|
+
|
|
13
|
+
export const enum SpecialReference {
|
|
14
|
+
MapSentinel = 0,
|
|
15
|
+
PromiseConstructor = 1,
|
|
16
|
+
PromiseSuccess = 2,
|
|
17
|
+
PromiseFailure = 3,
|
|
18
|
+
StreamConstructor = 4,
|
|
19
|
+
ArrayBufferConstructor = 5,
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Placeholder references
|
|
24
|
+
*/
|
|
25
|
+
export const SPECIAL_REFS: Record<SpecialReference, unknown> = {
|
|
26
|
+
[SpecialReference.MapSentinel]: {},
|
|
27
|
+
[SpecialReference.PromiseConstructor]: {},
|
|
28
|
+
[SpecialReference.PromiseSuccess]: {},
|
|
29
|
+
[SpecialReference.PromiseFailure]: {},
|
|
30
|
+
[SpecialReference.StreamConstructor]: {},
|
|
31
|
+
[SpecialReference.ArrayBufferConstructor]: {},
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export const SPECIAL_REF_STRING: Record<SpecialReference, string> = {
|
|
35
|
+
[SpecialReference.MapSentinel]: '[]',
|
|
36
|
+
[SpecialReference.PromiseConstructor]: SERIALIZED_PROMISE_CONSTRUCTOR,
|
|
37
|
+
[SpecialReference.PromiseSuccess]: SERIALIZED_PROMISE_SUCCESS,
|
|
38
|
+
[SpecialReference.PromiseFailure]: SERIALIZED_PROMISE_FAILURE,
|
|
39
|
+
[SpecialReference.StreamConstructor]: SERIALIZED_STREAM_CONSTRUCTOR,
|
|
40
|
+
[SpecialReference.ArrayBufferConstructor]:
|
|
41
|
+
SERIALIZED_ARRAY_BUFFER_CONSTRUCTOR,
|
|
42
|
+
};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ASYNC_ITERATOR_CONSTRUCTOR,
|
|
3
|
+
PROMISE_CONSTRUCTOR,
|
|
4
|
+
STREAM_CONSTRUCTOR,
|
|
5
|
+
} from './constructors';
|
|
6
|
+
import { SYM_ASYNC_ITERATOR } from './symbols';
|
|
7
|
+
|
|
8
|
+
export interface StreamListener<T> {
|
|
9
|
+
next(value: T): void;
|
|
10
|
+
throw(value: unknown): void;
|
|
11
|
+
return(value: T): void;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface Stream<T> {
|
|
15
|
+
__SEROVAL_STREAM__: true;
|
|
16
|
+
|
|
17
|
+
on(listener: StreamListener<T>): () => void;
|
|
18
|
+
|
|
19
|
+
next(value: T): void;
|
|
20
|
+
throw(value: unknown): void;
|
|
21
|
+
return(value: T): void;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function isStream<T>(value: object): value is Stream<T> {
|
|
25
|
+
return '__SEROVAL_STREAM__' in value;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function createStream<T>(): Stream<T> {
|
|
29
|
+
return STREAM_CONSTRUCTOR() as unknown as Stream<T>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function createStreamFromAsyncIterable<T>(
|
|
33
|
+
iterable: AsyncIterable<T>,
|
|
34
|
+
): Stream<T> {
|
|
35
|
+
const stream = createStream<T>();
|
|
36
|
+
|
|
37
|
+
const iterator = iterable[SYM_ASYNC_ITERATOR]();
|
|
38
|
+
|
|
39
|
+
async function push(): Promise<void> {
|
|
40
|
+
try {
|
|
41
|
+
const value = await iterator.next();
|
|
42
|
+
if (value.done) {
|
|
43
|
+
stream.return(value.value as T);
|
|
44
|
+
} else {
|
|
45
|
+
stream.next(value.value);
|
|
46
|
+
await push();
|
|
47
|
+
}
|
|
48
|
+
} catch (error) {
|
|
49
|
+
stream.throw(error);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
push().catch(() => {
|
|
54
|
+
// no-op
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
return stream;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const createAsyncIterable = ASYNC_ITERATOR_CONSTRUCTOR(
|
|
61
|
+
SYM_ASYNC_ITERATOR,
|
|
62
|
+
PROMISE_CONSTRUCTOR,
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
export function streamToAsyncIterable<T>(
|
|
66
|
+
stream: Stream<T>,
|
|
67
|
+
): () => AsyncIterableIterator<T> {
|
|
68
|
+
return createAsyncIterable(
|
|
69
|
+
stream,
|
|
70
|
+
) as unknown as () => AsyncIterableIterator<T>;
|
|
71
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { NIL } from './constants';
|
|
2
|
+
|
|
3
|
+
export function serializeChar(str: string): string | undefined {
|
|
4
|
+
switch (str) {
|
|
5
|
+
case '"':
|
|
6
|
+
return '\\"';
|
|
7
|
+
case '\\':
|
|
8
|
+
return '\\\\';
|
|
9
|
+
case '\n':
|
|
10
|
+
return '\\n';
|
|
11
|
+
case '\r':
|
|
12
|
+
return '\\r';
|
|
13
|
+
case '\b':
|
|
14
|
+
return '\\b';
|
|
15
|
+
case '\t':
|
|
16
|
+
return '\\t';
|
|
17
|
+
case '\f':
|
|
18
|
+
return '\\f';
|
|
19
|
+
case '<':
|
|
20
|
+
return '\\x3C';
|
|
21
|
+
case '\u2028':
|
|
22
|
+
return '\\u2028';
|
|
23
|
+
case '\u2029':
|
|
24
|
+
return '\\u2029';
|
|
25
|
+
default:
|
|
26
|
+
return NIL;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Written by https://github.com/DylanPiercey and is distributed under the MIT license.
|
|
31
|
+
// Creates a JavaScript double quoted string and escapes all characters
|
|
32
|
+
// not listed as DoubleStringCharacters on
|
|
33
|
+
// Also includes "<" to escape "</script>" and "\" to avoid invalid escapes in the output.
|
|
34
|
+
// http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4
|
|
35
|
+
export function serializeString(str: string): string {
|
|
36
|
+
let result = '';
|
|
37
|
+
let lastPos = 0;
|
|
38
|
+
let replacement: string | undefined;
|
|
39
|
+
for (let i = 0, len = str.length; i < len; i++) {
|
|
40
|
+
replacement = serializeChar(str[i]);
|
|
41
|
+
if (replacement) {
|
|
42
|
+
result += str.slice(lastPos, i) + replacement;
|
|
43
|
+
lastPos = i + 1;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (lastPos === 0) {
|
|
47
|
+
result = str;
|
|
48
|
+
} else {
|
|
49
|
+
result += str.slice(lastPos);
|
|
50
|
+
}
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function deserializeReplacer(str: string): string {
|
|
55
|
+
switch (str) {
|
|
56
|
+
case '\\\\':
|
|
57
|
+
return '\\';
|
|
58
|
+
case '\\"':
|
|
59
|
+
return '"';
|
|
60
|
+
case '\\n':
|
|
61
|
+
return '\n';
|
|
62
|
+
case '\\r':
|
|
63
|
+
return '\r';
|
|
64
|
+
case '\\b':
|
|
65
|
+
return '\b';
|
|
66
|
+
case '\\t':
|
|
67
|
+
return '\t';
|
|
68
|
+
case '\\f':
|
|
69
|
+
return '\f';
|
|
70
|
+
case '\\x3C':
|
|
71
|
+
return '\x3C';
|
|
72
|
+
case '\\u2028':
|
|
73
|
+
return '\u2028';
|
|
74
|
+
case '\\u2029':
|
|
75
|
+
return '\u2029';
|
|
76
|
+
default:
|
|
77
|
+
return str;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function deserializeString(str: string): string {
|
|
82
|
+
return str.replace(
|
|
83
|
+
/(\\\\|\\"|\\n|\\r|\\b|\\t|\\f|\\u2028|\\u2029|\\x3C)/g,
|
|
84
|
+
deserializeReplacer,
|
|
85
|
+
);
|
|
86
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export const SYM_ASYNC_ITERATOR: typeof Symbol.asyncIterator = Symbol.asyncIterator;
|
|
2
|
+
export const SYM_HAS_INSTANCE: typeof Symbol.hasInstance = Symbol.hasInstance;
|
|
3
|
+
export const SYM_IS_CONCAT_SPREADABLE: typeof Symbol.isConcatSpreadable = Symbol.isConcatSpreadable;
|
|
4
|
+
export const SYM_ITERATOR: typeof Symbol.iterator = Symbol.iterator;
|
|
5
|
+
export const SYM_MATCH: typeof Symbol.match = Symbol.match;
|
|
6
|
+
export const SYM_MATCH_ALL: typeof Symbol.matchAll = Symbol.matchAll;
|
|
7
|
+
export const SYM_REPLACE: typeof Symbol.replace = Symbol.replace;
|
|
8
|
+
export const SYM_SEARCH: typeof Symbol.search = Symbol.search;
|
|
9
|
+
export const SYM_SPECIES: typeof Symbol.species = Symbol.species;
|
|
10
|
+
export const SYM_SPLIT: typeof Symbol.split = Symbol.split;
|
|
11
|
+
export const SYM_TO_PRIMITIVE: typeof Symbol.toPrimitive = Symbol.toPrimitive;
|
|
12
|
+
export const SYM_TO_STRING_TAG: typeof Symbol.toStringTag = Symbol.toStringTag;
|
|
13
|
+
export const SYM_UNSCOPABLES: typeof Symbol.unscopables = Symbol.unscopables;
|
|
14
|
+
|
|
15
|
+
// For the future
|
|
16
|
+
|
|
17
|
+
// export const SYM_DISPOSE = Symbol.dispose;
|
|
18
|
+
// export const SYM_ASYNC_DISPOSE = Symbol.asyncDispose;
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createAsyncParserContext,
|
|
3
|
+
parseTopAsync,
|
|
4
|
+
} from '../context/async-parser';
|
|
5
|
+
import {
|
|
6
|
+
createVanillaDeserializerContext,
|
|
7
|
+
deserializeTop,
|
|
8
|
+
} from '../context/deserializer';
|
|
9
|
+
import type { BaseParserContextOptions } from '../context/parser';
|
|
10
|
+
import {
|
|
11
|
+
createVanillaSerializerContext,
|
|
12
|
+
serializeTopVanilla,
|
|
13
|
+
} from '../context/serializer';
|
|
14
|
+
import { createSyncParserContext, parseTop } from '../context/sync-parser';
|
|
15
|
+
import {
|
|
16
|
+
type PluginAccessOptions,
|
|
17
|
+
resolvePlugins,
|
|
18
|
+
SerovalMode,
|
|
19
|
+
} from '../plugin';
|
|
20
|
+
import type { SerovalNode } from '../types';
|
|
21
|
+
import { ALL_ENABLED } from '../compat';
|
|
22
|
+
export type SyncParserContextOptions = Omit<BaseParserContextOptions, 'refs'>;
|
|
23
|
+
export type AsyncParserContextOptions = Omit<BaseParserContextOptions, 'refs'>;
|
|
24
|
+
|
|
25
|
+
export function serialize<T>(
|
|
26
|
+
source: T,
|
|
27
|
+
options: SyncParserContextOptions = {},
|
|
28
|
+
): string {
|
|
29
|
+
const plugins = resolvePlugins(options.plugins);
|
|
30
|
+
const ctx = createSyncParserContext(SerovalMode.Vanilla, {
|
|
31
|
+
plugins,
|
|
32
|
+
disabledFeatures: options.disabledFeatures,
|
|
33
|
+
});
|
|
34
|
+
const tree = parseTop(ctx, source);
|
|
35
|
+
const serial = createVanillaSerializerContext({
|
|
36
|
+
plugins,
|
|
37
|
+
features: ctx.base.features,
|
|
38
|
+
markedRefs: ctx.base.marked,
|
|
39
|
+
});
|
|
40
|
+
return serializeTopVanilla(serial, tree);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export async function serializeAsync<T>(
|
|
44
|
+
source: T,
|
|
45
|
+
options: AsyncParserContextOptions = {},
|
|
46
|
+
): Promise<string> {
|
|
47
|
+
const plugins = resolvePlugins(options.plugins);
|
|
48
|
+
const ctx = createAsyncParserContext(SerovalMode.Vanilla, {
|
|
49
|
+
plugins,
|
|
50
|
+
disabledFeatures: options.disabledFeatures,
|
|
51
|
+
});
|
|
52
|
+
const tree = await parseTopAsync(ctx, source);
|
|
53
|
+
const serial = createVanillaSerializerContext({
|
|
54
|
+
plugins,
|
|
55
|
+
features: ctx.base.features,
|
|
56
|
+
markedRefs: ctx.base.marked,
|
|
57
|
+
});
|
|
58
|
+
return serializeTopVanilla(serial, tree);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function deserialize<T>(source: string): T {
|
|
62
|
+
return (0, eval)(source) as T;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface SerovalJSON {
|
|
66
|
+
t: SerovalNode;
|
|
67
|
+
f: number;
|
|
68
|
+
m: number[];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface FromJSONOptions extends PluginAccessOptions {
|
|
72
|
+
disabledFeatures?: number;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function toJSON<T>(
|
|
76
|
+
source: T,
|
|
77
|
+
options: SyncParserContextOptions = {},
|
|
78
|
+
): SerovalJSON {
|
|
79
|
+
const plugins = resolvePlugins(options.plugins);
|
|
80
|
+
const ctx = createSyncParserContext(SerovalMode.Vanilla, {
|
|
81
|
+
plugins,
|
|
82
|
+
disabledFeatures: options.disabledFeatures,
|
|
83
|
+
});
|
|
84
|
+
return {
|
|
85
|
+
t: parseTop(ctx, source),
|
|
86
|
+
f: ctx.base.features,
|
|
87
|
+
m: Array.from(ctx.base.marked),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export async function toJSONAsync<T>(
|
|
92
|
+
source: T,
|
|
93
|
+
options: AsyncParserContextOptions = {},
|
|
94
|
+
): Promise<SerovalJSON> {
|
|
95
|
+
const plugins = resolvePlugins(options.plugins);
|
|
96
|
+
const ctx = createAsyncParserContext(SerovalMode.Vanilla, {
|
|
97
|
+
plugins,
|
|
98
|
+
disabledFeatures: options.disabledFeatures,
|
|
99
|
+
});
|
|
100
|
+
return {
|
|
101
|
+
t: await parseTopAsync(ctx, source),
|
|
102
|
+
f: ctx.base.features,
|
|
103
|
+
m: Array.from(ctx.base.marked),
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function compileJSON(
|
|
108
|
+
source: SerovalJSON,
|
|
109
|
+
options: PluginAccessOptions = {},
|
|
110
|
+
): string {
|
|
111
|
+
const plugins = resolvePlugins(options.plugins);
|
|
112
|
+
const ctx = createVanillaSerializerContext({
|
|
113
|
+
plugins,
|
|
114
|
+
features: source.f,
|
|
115
|
+
markedRefs: source.m,
|
|
116
|
+
});
|
|
117
|
+
return serializeTopVanilla(ctx, source.t);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function fromJSON<T>(
|
|
121
|
+
source: SerovalJSON,
|
|
122
|
+
options: FromJSONOptions = {},
|
|
123
|
+
): T {
|
|
124
|
+
const plugins = resolvePlugins(options.plugins);
|
|
125
|
+
const disabledFeatures = options.disabledFeatures || 0;
|
|
126
|
+
const sourceFeatures = source.f ?? ALL_ENABLED;
|
|
127
|
+
const ctx = createVanillaDeserializerContext({
|
|
128
|
+
plugins,
|
|
129
|
+
markedRefs: source.m,
|
|
130
|
+
features: sourceFeatures & ~disabledFeatures,
|
|
131
|
+
disabledFeatures,
|
|
132
|
+
});
|
|
133
|
+
return deserializeTop(ctx, source.t) as T;
|
|
134
|
+
}
|
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ErrorConstructorTag,
|
|
3
|
+
SerovalConstant,
|
|
4
|
+
SerovalNodeType,
|
|
5
|
+
SerovalObjectFlags,
|
|
6
|
+
Symbols,
|
|
7
|
+
} from './constants';
|
|
8
|
+
import type { SpecialReference } from './special-reference';
|
|
9
|
+
|
|
10
|
+
export interface SerovalBaseNode {
|
|
11
|
+
// Type of the node
|
|
12
|
+
t: SerovalNodeType;
|
|
13
|
+
// Reference ID
|
|
14
|
+
i: number | undefined;
|
|
15
|
+
// Serialized value
|
|
16
|
+
s: unknown;
|
|
17
|
+
// Constructor name / RegExp source
|
|
18
|
+
c: string | undefined;
|
|
19
|
+
// message/flags
|
|
20
|
+
m: string | undefined;
|
|
21
|
+
// properties (objects)
|
|
22
|
+
p: SerovalObjectRecordNode | undefined;
|
|
23
|
+
// entries (for Map, etc.)
|
|
24
|
+
e: SerovalMapRecordNode | undefined;
|
|
25
|
+
// array of nodes
|
|
26
|
+
a: (SerovalNode | 0)[] | undefined;
|
|
27
|
+
// fulfilled node
|
|
28
|
+
f: SerovalNode | undefined;
|
|
29
|
+
// byte offset/object flags
|
|
30
|
+
b: number | undefined;
|
|
31
|
+
// object flag
|
|
32
|
+
o: SerovalObjectFlags | undefined;
|
|
33
|
+
// length
|
|
34
|
+
l: number | undefined;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export type SerovalObjectRecordKey = string | SerovalNode;
|
|
38
|
+
|
|
39
|
+
export interface SerovalObjectRecordNode {
|
|
40
|
+
k: SerovalObjectRecordKey[];
|
|
41
|
+
v: SerovalNode[];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface SerovalMapRecordNode {
|
|
45
|
+
k: SerovalNode[];
|
|
46
|
+
v: SerovalNode[];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface SerovalNumberNode extends SerovalBaseNode {
|
|
50
|
+
t: SerovalNodeType.Number;
|
|
51
|
+
s: number;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface SerovalStringNode extends SerovalBaseNode {
|
|
55
|
+
t: SerovalNodeType.String;
|
|
56
|
+
s: string;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface SerovalConstantNode extends SerovalBaseNode {
|
|
60
|
+
t: SerovalNodeType.Constant;
|
|
61
|
+
s: SerovalConstant;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export type SerovalPrimitiveNode =
|
|
65
|
+
| SerovalNumberNode
|
|
66
|
+
| SerovalStringNode
|
|
67
|
+
| SerovalConstantNode;
|
|
68
|
+
|
|
69
|
+
export interface SerovalIndexedValueNode extends SerovalBaseNode {
|
|
70
|
+
t: SerovalNodeType.IndexedValue;
|
|
71
|
+
// id
|
|
72
|
+
i: number;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface SerovalBigIntNode extends SerovalBaseNode {
|
|
76
|
+
t: SerovalNodeType.BigInt;
|
|
77
|
+
// value in string
|
|
78
|
+
s: string;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export interface SerovalDateNode extends SerovalBaseNode {
|
|
82
|
+
t: SerovalNodeType.Date;
|
|
83
|
+
// id (Dates are stateful)
|
|
84
|
+
i: number;
|
|
85
|
+
// value in ISO string
|
|
86
|
+
s: string;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface SerovalRegExpNode extends SerovalBaseNode {
|
|
90
|
+
t: SerovalNodeType.RegExp;
|
|
91
|
+
// id (RegExp are stateful)
|
|
92
|
+
i: number;
|
|
93
|
+
// source
|
|
94
|
+
c: string;
|
|
95
|
+
// flags
|
|
96
|
+
m: string;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export interface SerovalArrayBufferNode extends SerovalBaseNode {
|
|
100
|
+
t: SerovalNodeType.ArrayBuffer;
|
|
101
|
+
// id
|
|
102
|
+
i: number;
|
|
103
|
+
// byte string
|
|
104
|
+
s: string;
|
|
105
|
+
// array buffer constructor
|
|
106
|
+
f: SerovalNodeWithID;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export interface SerovalTypedArrayNode extends SerovalBaseNode {
|
|
110
|
+
t: SerovalNodeType.TypedArray;
|
|
111
|
+
// id
|
|
112
|
+
i: number;
|
|
113
|
+
// TypedArray Constructor
|
|
114
|
+
c: string;
|
|
115
|
+
// ArrayBuffer reference
|
|
116
|
+
f: SerovalNode;
|
|
117
|
+
// Byte Offset
|
|
118
|
+
b: number;
|
|
119
|
+
// length
|
|
120
|
+
l: number;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export interface SerovalBigIntTypedArrayNode extends SerovalBaseNode {
|
|
124
|
+
t: SerovalNodeType.BigIntTypedArray;
|
|
125
|
+
i: number;
|
|
126
|
+
// TypedArray Constructor
|
|
127
|
+
c: string;
|
|
128
|
+
// ArrayBuffer reference
|
|
129
|
+
f: SerovalNode;
|
|
130
|
+
// Byte Offset
|
|
131
|
+
b: number;
|
|
132
|
+
// length
|
|
133
|
+
l: number;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export type SerovalSemiPrimitiveNode =
|
|
137
|
+
| SerovalBigIntNode
|
|
138
|
+
| SerovalDateNode
|
|
139
|
+
| SerovalRegExpNode
|
|
140
|
+
| SerovalTypedArrayNode
|
|
141
|
+
| SerovalBigIntTypedArrayNode;
|
|
142
|
+
|
|
143
|
+
export interface SerovalSetNode extends SerovalBaseNode {
|
|
144
|
+
t: SerovalNodeType.Set;
|
|
145
|
+
// id
|
|
146
|
+
i: number;
|
|
147
|
+
// Items in Set (as array)
|
|
148
|
+
a: SerovalNode[];
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export interface SerovalMapNode extends SerovalBaseNode {
|
|
152
|
+
t: SerovalNodeType.Map;
|
|
153
|
+
i: number;
|
|
154
|
+
// key/value pairs
|
|
155
|
+
e: SerovalMapRecordNode;
|
|
156
|
+
f: SerovalNodeWithID;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export interface SerovalArrayNode extends SerovalBaseNode {
|
|
160
|
+
t: SerovalNodeType.Array;
|
|
161
|
+
// items
|
|
162
|
+
a: (SerovalNode | 0)[];
|
|
163
|
+
i: number;
|
|
164
|
+
o: SerovalObjectFlags;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export interface SerovalObjectNode extends SerovalBaseNode {
|
|
168
|
+
t: SerovalNodeType.Object;
|
|
169
|
+
// key/value pairs
|
|
170
|
+
p: SerovalObjectRecordNode;
|
|
171
|
+
i: number;
|
|
172
|
+
o: SerovalObjectFlags;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export interface SerovalNullConstructorNode extends SerovalBaseNode {
|
|
176
|
+
t: SerovalNodeType.NullConstructor;
|
|
177
|
+
// key/value pairs
|
|
178
|
+
p: SerovalObjectRecordNode;
|
|
179
|
+
i: number;
|
|
180
|
+
o: SerovalObjectFlags;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export interface SerovalPromiseNode extends SerovalBaseNode {
|
|
184
|
+
t: SerovalNodeType.Promise;
|
|
185
|
+
s: 0 | 1;
|
|
186
|
+
// resolved value
|
|
187
|
+
f: SerovalNode;
|
|
188
|
+
i: number;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export interface SerovalErrorNode extends SerovalBaseNode {
|
|
192
|
+
t: SerovalNodeType.Error;
|
|
193
|
+
// constructor name
|
|
194
|
+
s: ErrorConstructorTag;
|
|
195
|
+
// message
|
|
196
|
+
m: string;
|
|
197
|
+
// other properties
|
|
198
|
+
p: SerovalObjectRecordNode | undefined;
|
|
199
|
+
i: number;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export interface SerovalAggregateErrorNode extends SerovalBaseNode {
|
|
203
|
+
t: SerovalNodeType.AggregateError;
|
|
204
|
+
i: number;
|
|
205
|
+
// message
|
|
206
|
+
m: string;
|
|
207
|
+
// other properties
|
|
208
|
+
p: SerovalObjectRecordNode | undefined;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export interface SerovalWKSymbolNode extends SerovalBaseNode {
|
|
212
|
+
t: SerovalNodeType.WKSymbol;
|
|
213
|
+
i: number;
|
|
214
|
+
s: Symbols;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export interface SerovalReferenceNode extends SerovalBaseNode {
|
|
218
|
+
t: SerovalNodeType.Reference;
|
|
219
|
+
i: number;
|
|
220
|
+
// id of the reference in the map
|
|
221
|
+
s: string;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export interface SerovalDataViewNode extends SerovalBaseNode {
|
|
225
|
+
t: SerovalNodeType.DataView;
|
|
226
|
+
i: number;
|
|
227
|
+
// reference to array buffer
|
|
228
|
+
f: SerovalNode;
|
|
229
|
+
// byte offset
|
|
230
|
+
b: number;
|
|
231
|
+
// byte length
|
|
232
|
+
l: number;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export interface SerovalBoxedNode extends SerovalBaseNode {
|
|
236
|
+
t: SerovalNodeType.Boxed;
|
|
237
|
+
i: number;
|
|
238
|
+
f: SerovalNode;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export interface SerovalPromiseConstructorNode extends SerovalBaseNode {
|
|
242
|
+
t: SerovalNodeType.PromiseConstructor;
|
|
243
|
+
i: number;
|
|
244
|
+
s: number;
|
|
245
|
+
f: SerovalNodeWithID;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export interface SerovalPromiseResolveNode extends SerovalBaseNode {
|
|
249
|
+
t: SerovalNodeType.PromiseSuccess;
|
|
250
|
+
i: number;
|
|
251
|
+
a: [resolver: SerovalNodeWithID, resolved: SerovalNode];
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
export interface SerovalPromiseRejectNode extends SerovalBaseNode {
|
|
255
|
+
t: SerovalNodeType.PromiseFailure;
|
|
256
|
+
i: number;
|
|
257
|
+
a: [resolver: SerovalNodeWithID, resolved: SerovalNode];
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export interface SerovalPluginNode extends SerovalBaseNode {
|
|
261
|
+
t: SerovalNodeType.Plugin;
|
|
262
|
+
i: number;
|
|
263
|
+
// value
|
|
264
|
+
s: unknown;
|
|
265
|
+
// tag name
|
|
266
|
+
c: string;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Represents special values as placeholders
|
|
271
|
+
*/
|
|
272
|
+
export interface SerovalSpecialReferenceNode extends SerovalBaseNode {
|
|
273
|
+
t: SerovalNodeType.SpecialReference;
|
|
274
|
+
i: number;
|
|
275
|
+
s: SpecialReference;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
export interface SerovalIteratorFactoryNode extends SerovalBaseNode {
|
|
279
|
+
t: SerovalNodeType.IteratorFactory;
|
|
280
|
+
i: number;
|
|
281
|
+
f: SerovalNodeWithID;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
export interface SerovalIteratorFactoryInstanceNode extends SerovalBaseNode {
|
|
285
|
+
t: SerovalNodeType.IteratorFactoryInstance;
|
|
286
|
+
a: [instance: SerovalNodeWithID, sequence: SerovalNode];
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export interface SerovalAsyncIteratorFactoryNode extends SerovalBaseNode {
|
|
290
|
+
t: SerovalNodeType.AsyncIteratorFactory;
|
|
291
|
+
i: number;
|
|
292
|
+
a: [promise: SerovalNodeWithID, symbol: SerovalNodeWithID];
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
export interface SerovalAsyncIteratorFactoryInstanceNode
|
|
296
|
+
extends SerovalBaseNode {
|
|
297
|
+
t: SerovalNodeType.AsyncIteratorFactoryInstance;
|
|
298
|
+
a: [instance: SerovalNodeWithID, sequence: SerovalNode];
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
export interface SerovalStreamConstructorNode extends SerovalBaseNode {
|
|
302
|
+
t: SerovalNodeType.StreamConstructor;
|
|
303
|
+
i: number;
|
|
304
|
+
a: SerovalNode[];
|
|
305
|
+
// special reference to the constructor
|
|
306
|
+
f: SerovalNodeWithID;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
export interface SerovalStreamNextNode extends SerovalBaseNode {
|
|
310
|
+
t: SerovalNodeType.StreamNext;
|
|
311
|
+
i: number;
|
|
312
|
+
// Next value
|
|
313
|
+
f: SerovalNode;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
export interface SerovalStreamThrowNode extends SerovalBaseNode {
|
|
317
|
+
t: SerovalNodeType.StreamThrow;
|
|
318
|
+
i: number;
|
|
319
|
+
// Throw value
|
|
320
|
+
f: SerovalNode;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
export interface SerovalStreamReturnNode extends SerovalBaseNode {
|
|
324
|
+
t: SerovalNodeType.StreamReturn;
|
|
325
|
+
i: number;
|
|
326
|
+
// Return value
|
|
327
|
+
f: SerovalNode;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
export type SerovalSyncNode =
|
|
331
|
+
| SerovalPrimitiveNode
|
|
332
|
+
| SerovalIndexedValueNode
|
|
333
|
+
| SerovalSemiPrimitiveNode
|
|
334
|
+
| SerovalSetNode
|
|
335
|
+
| SerovalMapNode
|
|
336
|
+
| SerovalArrayNode
|
|
337
|
+
| SerovalObjectNode
|
|
338
|
+
| SerovalNullConstructorNode
|
|
339
|
+
| SerovalPromiseNode
|
|
340
|
+
| SerovalErrorNode
|
|
341
|
+
| SerovalAggregateErrorNode
|
|
342
|
+
| SerovalWKSymbolNode
|
|
343
|
+
| SerovalReferenceNode
|
|
344
|
+
| SerovalArrayBufferNode
|
|
345
|
+
| SerovalDataViewNode
|
|
346
|
+
| SerovalBoxedNode
|
|
347
|
+
| SerovalPluginNode
|
|
348
|
+
| SerovalSpecialReferenceNode
|
|
349
|
+
| SerovalIteratorFactoryNode
|
|
350
|
+
| SerovalIteratorFactoryInstanceNode
|
|
351
|
+
| SerovalAsyncIteratorFactoryNode
|
|
352
|
+
| SerovalAsyncIteratorFactoryInstanceNode;
|
|
353
|
+
|
|
354
|
+
export type SerovalAsyncNode =
|
|
355
|
+
| SerovalPromiseNode
|
|
356
|
+
| SerovalPromiseConstructorNode
|
|
357
|
+
| SerovalPromiseResolveNode
|
|
358
|
+
| SerovalPromiseRejectNode
|
|
359
|
+
| SerovalStreamConstructorNode
|
|
360
|
+
| SerovalStreamNextNode
|
|
361
|
+
| SerovalStreamThrowNode
|
|
362
|
+
| SerovalStreamReturnNode;
|
|
363
|
+
|
|
364
|
+
export type SerovalNode = SerovalSyncNode | SerovalAsyncNode;
|
|
365
|
+
|
|
366
|
+
export type SerovalNodeWithID = Extract<SerovalNode, { i: number }>;
|