seroval 0.11.5 → 0.12.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.
- package/README.md +29 -16
- package/dist/cjs/development/index.cjs +3012 -4790
- package/dist/cjs/development/index.cjs.map +4 -4
- package/dist/cjs/production/index.cjs +3 -3
- package/dist/esm/development/index.mjs +3010 -4790
- package/dist/esm/development/index.mjs.map +4 -4
- package/dist/esm/production/index.mjs +3 -3
- package/dist/types/core/Serializer.d.ts +2 -0
- package/dist/types/core/Serializer.d.ts.map +1 -0
- package/dist/types/core/UnsupportedTypeError.d.ts +1 -0
- package/dist/types/core/UnsupportedTypeError.d.ts.map +1 -0
- package/dist/types/core/assert.d.ts +1 -0
- package/dist/types/core/assert.d.ts.map +1 -0
- package/dist/types/core/base/async.d.ts +30 -0
- package/dist/types/core/base/async.d.ts.map +1 -0
- package/dist/types/core/base/stream.d.ts +40 -0
- package/dist/types/core/base/stream.d.ts.map +1 -0
- package/dist/types/core/base/sync.d.ts +32 -0
- package/dist/types/core/base/sync.d.ts.map +1 -0
- package/dist/types/core/base-primitives.d.ts +3 -1
- package/dist/types/core/base-primitives.d.ts.map +1 -0
- package/dist/types/core/compat.d.ts +1 -0
- package/dist/types/core/compat.d.ts.map +1 -0
- package/dist/types/core/constants.d.ts +19 -4
- package/dist/types/core/constants.d.ts.map +1 -0
- package/dist/types/core/constructors.d.ts +1 -0
- package/dist/types/core/constructors.d.ts.map +1 -0
- package/dist/types/core/cross/async.d.ts +8 -3
- package/dist/types/core/cross/async.d.ts.map +1 -0
- package/dist/types/core/cross/cross-parser.d.ts +8 -0
- package/dist/types/core/cross/cross-parser.d.ts.map +1 -0
- package/dist/types/core/cross/index.d.ts +12 -5
- package/dist/types/core/cross/index.d.ts.map +1 -0
- package/dist/types/core/cross/serialize.d.ts +23 -5
- package/dist/types/core/cross/serialize.d.ts.map +1 -0
- package/dist/types/core/cross/stream.d.ts +8 -3
- package/dist/types/core/cross/stream.d.ts.map +1 -0
- package/dist/types/core/cross/sync.d.ts +8 -3
- package/dist/types/core/cross/sync.d.ts.map +1 -0
- package/dist/types/core/get-identifier.d.ts +1 -0
- package/dist/types/core/get-identifier.d.ts.map +1 -0
- package/dist/types/core/keys.d.ts +1 -0
- package/dist/types/core/keys.d.ts.map +1 -0
- package/dist/types/core/literals.d.ts +1 -0
- package/dist/types/core/literals.d.ts.map +1 -0
- package/dist/types/core/parser-context.d.ts +23 -0
- package/dist/types/core/parser-context.d.ts.map +1 -0
- package/dist/types/core/plugin.d.ts +26 -0
- package/dist/types/core/plugin.d.ts.map +1 -0
- package/dist/types/core/promise-to-result.d.ts +1 -0
- package/dist/types/core/promise-to-result.d.ts.map +1 -0
- package/dist/types/core/reference.d.ts +1 -0
- package/dist/types/core/reference.d.ts.map +1 -0
- package/dist/types/core/serializer-context.old.d.ts +165 -0
- package/dist/types/core/serializer-context.old.d.ts.map +1 -0
- package/dist/types/core/shared.d.ts +4 -7
- package/dist/types/core/shared.d.ts.map +1 -0
- package/dist/types/core/string.d.ts +1 -0
- package/dist/types/core/string.d.ts.map +1 -0
- package/dist/types/core/tree/async.d.ts +8 -3
- package/dist/types/core/tree/async.d.ts.map +1 -0
- package/dist/types/core/tree/deserialize.d.ts +49 -2
- package/dist/types/core/tree/deserialize.d.ts.map +1 -0
- package/dist/types/core/tree/index.d.ts +10 -7
- package/dist/types/core/tree/index.d.ts.map +1 -0
- package/dist/types/core/tree/serialize.d.ts +37 -4
- package/dist/types/core/tree/serialize.d.ts.map +1 -0
- package/dist/types/core/tree/sync.d.ts +8 -3
- package/dist/types/core/tree/sync.d.ts.map +1 -0
- package/dist/types/core/types.d.ts +10 -3
- package/dist/types/core/types.d.ts.map +1 -0
- package/dist/types/core/web-api.d.ts +1 -0
- package/dist/types/core/web-api.d.ts.map +1 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/types.d.ts +1 -0
- package/dist/types/types.d.ts.map +1 -0
- package/package.json +2 -2
- package/src/core/Serializer.ts +2 -0
- package/src/core/base/async.ts +804 -0
- package/src/core/base/stream.ts +496 -0
- package/src/core/base/sync.ts +675 -0
- package/src/core/base-primitives.ts +22 -0
- package/src/core/constants.ts +61 -45
- package/src/core/cross/async.ts +6 -743
- package/src/core/cross/cross-parser.ts +9 -0
- package/src/core/cross/index.ts +62 -84
- package/src/core/cross/serialize.ts +71 -862
- package/src/core/cross/stream.ts +6 -868
- package/src/core/cross/sync.ts +6 -623
- package/src/core/is-serializable.new.ts +208 -0
- package/src/core/keys.ts +1 -1
- package/src/core/parser-context.ts +131 -0
- package/src/core/plugin.ts +55 -0
- package/src/core/serializer-context.new.ts +1168 -0
- package/src/core/serializer-context.old.ts +1126 -0
- package/src/core/shared.ts +46 -124
- package/src/core/tree/async.ts +6 -742
- package/src/core/tree/deserialize.ts +423 -420
- package/src/core/tree/index.ts +49 -91
- package/src/core/tree/parser.ts +45 -0
- package/src/core/tree/serialize.ts +122 -828
- package/src/core/tree/sync.ts +6 -623
- package/src/core/types.ts +13 -2
- package/src/example.ts +28 -0
- package/src/index.ts +3 -0
- package/dist/types/core/assignments.d.ts +0 -33
- package/dist/types/core/context.d.ts +0 -9
- package/dist/types/core/cross/context.d.ts +0 -37
- package/dist/types/core/cross/primitives.d.ts +0 -9
- package/dist/types/core/cross/web-api.d.ts +0 -4
- package/dist/types/core/tree/context.d.ts +0 -44
- package/dist/types/core/tree/primitives.d.ts +0 -9
- package/dist/types/core/tree/web-api.d.ts +0 -4
- package/src/core/assignments.ts +0 -159
- package/src/core/context.ts +0 -15
- package/src/core/cross/context.ts +0 -102
- package/src/core/cross/primitives.ts +0 -128
- package/src/core/cross/web-api.ts +0 -50
- package/src/core/tree/context.ts +0 -125
- package/src/core/tree/primitives.ts +0 -123
- package/src/core/tree/web-api.ts +0 -50
package/src/core/tree/context.ts
DELETED
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
import { ALL_ENABLED } from '../compat';
|
|
2
|
-
import type { BaseParserContext, BaseSerializerContext } from '../context';
|
|
3
|
-
import getIdentifier from '../get-identifier';
|
|
4
|
-
|
|
5
|
-
export interface ParserReference {
|
|
6
|
-
ids: Map<unknown, number>;
|
|
7
|
-
marked: Set<number>;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export interface ParserContext extends BaseParserContext {
|
|
11
|
-
reference: ParserReference;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export interface ParserOptions {
|
|
15
|
-
disabledFeatures: number;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export function createParserContext(options: Partial<ParserOptions> = {}): ParserContext {
|
|
19
|
-
return {
|
|
20
|
-
reference: {
|
|
21
|
-
ids: new Map(),
|
|
22
|
-
marked: new Set(),
|
|
23
|
-
},
|
|
24
|
-
features: ALL_ENABLED ^ (options.disabledFeatures || 0),
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export interface SerializerReference {
|
|
29
|
-
size: number;
|
|
30
|
-
// Map tree refs to actual refs
|
|
31
|
-
valid: (number | undefined)[];
|
|
32
|
-
// Refs that are...referenced
|
|
33
|
-
marked: Set<number>;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export interface SerializerContext extends BaseSerializerContext {
|
|
37
|
-
reference: SerializerReference;
|
|
38
|
-
// Variables
|
|
39
|
-
vars: (string | undefined)[];
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export interface SerializerOptions {
|
|
43
|
-
markedRefs: number[] | Set<number>;
|
|
44
|
-
features: number;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export function createSerializerContext(options: SerializerOptions): SerializerContext {
|
|
48
|
-
return {
|
|
49
|
-
stack: [],
|
|
50
|
-
vars: [],
|
|
51
|
-
assignments: [],
|
|
52
|
-
reference: {
|
|
53
|
-
valid: [],
|
|
54
|
-
size: 0,
|
|
55
|
-
marked: new Set(options.markedRefs),
|
|
56
|
-
},
|
|
57
|
-
features: options.features,
|
|
58
|
-
flags: [],
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Increments the number of references the referenced value has
|
|
64
|
-
*/
|
|
65
|
-
export function markRef(
|
|
66
|
-
ctx: ParserContext | SerializerContext,
|
|
67
|
-
current: number,
|
|
68
|
-
): void {
|
|
69
|
-
ctx.reference.marked.add(current);
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Creates the reference param (identifier) from the given reference ID
|
|
73
|
-
* Calling this function means the value has been referenced somewhere
|
|
74
|
-
*/
|
|
75
|
-
export function getRefParam(ctx: SerializerContext, index: number): string {
|
|
76
|
-
/**
|
|
77
|
-
* Creates a new reference ID from a given reference ID
|
|
78
|
-
* This new reference ID means that the reference itself
|
|
79
|
-
* has been referenced at least once, and is used to generate
|
|
80
|
-
* the variables
|
|
81
|
-
*/
|
|
82
|
-
let actualIndex = ctx.reference.valid[index];
|
|
83
|
-
if (actualIndex == null) {
|
|
84
|
-
actualIndex = ctx.reference.size++;
|
|
85
|
-
ctx.reference.valid[index] = actualIndex;
|
|
86
|
-
}
|
|
87
|
-
let identifier = ctx.vars[actualIndex];
|
|
88
|
-
if (identifier == null) {
|
|
89
|
-
identifier = getIdentifier(actualIndex);
|
|
90
|
-
ctx.vars[actualIndex] = identifier;
|
|
91
|
-
}
|
|
92
|
-
return identifier;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export function createIndexedValue<T>(
|
|
96
|
-
ctx: ParserContext,
|
|
97
|
-
current: T,
|
|
98
|
-
): number {
|
|
99
|
-
const ref = ctx.reference.ids.get(current);
|
|
100
|
-
if (ref == null) {
|
|
101
|
-
const id = ctx.reference.ids.size;
|
|
102
|
-
ctx.reference.ids.set(current, id);
|
|
103
|
-
return id;
|
|
104
|
-
}
|
|
105
|
-
markRef(ctx, ref);
|
|
106
|
-
return ref;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
export interface DeserializerContext {
|
|
110
|
-
values: Map<number, unknown>;
|
|
111
|
-
refs: Set<number>;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
export interface DeserializerOptions {
|
|
115
|
-
markedRefs: number[] | Set<number>;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
export function createDeserializerContext(
|
|
119
|
-
options: DeserializerOptions,
|
|
120
|
-
): DeserializerContext {
|
|
121
|
-
return {
|
|
122
|
-
values: new Map(),
|
|
123
|
-
refs: new Set(options.markedRefs),
|
|
124
|
-
};
|
|
125
|
-
}
|
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import assert from '../assert';
|
|
2
|
-
import type { ParserContext } from './context';
|
|
3
|
-
import { createIndexedValue } from './context';
|
|
4
|
-
import type { BigIntTypedArrayValue, TypedArrayValue } from '../../types';
|
|
5
|
-
import { hasReferenceID } from '../reference';
|
|
6
|
-
import type {
|
|
7
|
-
SerovalBigIntTypedArrayNode,
|
|
8
|
-
SerovalTypedArrayNode,
|
|
9
|
-
SerovalWKSymbolNode,
|
|
10
|
-
SerovalReferenceNode,
|
|
11
|
-
SerovalDataViewNode,
|
|
12
|
-
SerovalNode,
|
|
13
|
-
SerovalIndexedValueNode,
|
|
14
|
-
} from '../types';
|
|
15
|
-
import type { WellKnownSymbols } from '../constants';
|
|
16
|
-
import { SerovalNodeType } from '../constants';
|
|
17
|
-
import {
|
|
18
|
-
createArrayBufferNode,
|
|
19
|
-
createIndexedValueNode,
|
|
20
|
-
createReferenceNode,
|
|
21
|
-
createWKSymbolNode,
|
|
22
|
-
} from '../base-primitives';
|
|
23
|
-
|
|
24
|
-
export function serializeArrayBuffer(
|
|
25
|
-
ctx: ParserContext,
|
|
26
|
-
current: ArrayBuffer,
|
|
27
|
-
): SerovalNode {
|
|
28
|
-
const id = createIndexedValue(ctx, current);
|
|
29
|
-
if (ctx.reference.marked.has(id)) {
|
|
30
|
-
return createIndexedValueNode(id);
|
|
31
|
-
}
|
|
32
|
-
return createArrayBufferNode(id, current);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function createTypedArrayNode(
|
|
36
|
-
ctx: ParserContext,
|
|
37
|
-
id: number,
|
|
38
|
-
current: TypedArrayValue,
|
|
39
|
-
): SerovalTypedArrayNode {
|
|
40
|
-
return {
|
|
41
|
-
t: SerovalNodeType.TypedArray,
|
|
42
|
-
i: id,
|
|
43
|
-
s: undefined,
|
|
44
|
-
l: current.length,
|
|
45
|
-
c: current.constructor.name,
|
|
46
|
-
m: undefined,
|
|
47
|
-
p: undefined,
|
|
48
|
-
e: undefined,
|
|
49
|
-
a: undefined,
|
|
50
|
-
f: serializeArrayBuffer(ctx, current.buffer),
|
|
51
|
-
b: current.byteOffset,
|
|
52
|
-
o: undefined,
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export function createBigIntTypedArrayNode(
|
|
57
|
-
ctx: ParserContext,
|
|
58
|
-
id: number,
|
|
59
|
-
current: BigIntTypedArrayValue,
|
|
60
|
-
): SerovalBigIntTypedArrayNode {
|
|
61
|
-
return {
|
|
62
|
-
t: SerovalNodeType.BigIntTypedArray,
|
|
63
|
-
i: id,
|
|
64
|
-
s: undefined,
|
|
65
|
-
l: current.length,
|
|
66
|
-
c: current.constructor.name,
|
|
67
|
-
m: undefined,
|
|
68
|
-
p: undefined,
|
|
69
|
-
e: undefined,
|
|
70
|
-
a: undefined,
|
|
71
|
-
f: serializeArrayBuffer(ctx, current.buffer),
|
|
72
|
-
b: current.byteOffset,
|
|
73
|
-
o: undefined,
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export function createDataViewNode(
|
|
78
|
-
ctx: ParserContext,
|
|
79
|
-
id: number,
|
|
80
|
-
current: DataView,
|
|
81
|
-
): SerovalDataViewNode {
|
|
82
|
-
return {
|
|
83
|
-
t: SerovalNodeType.DataView,
|
|
84
|
-
i: id,
|
|
85
|
-
s: undefined,
|
|
86
|
-
l: current.byteLength,
|
|
87
|
-
c: undefined,
|
|
88
|
-
m: undefined,
|
|
89
|
-
p: undefined,
|
|
90
|
-
e: undefined,
|
|
91
|
-
a: undefined,
|
|
92
|
-
f: serializeArrayBuffer(ctx, current.buffer),
|
|
93
|
-
b: current.byteOffset,
|
|
94
|
-
o: undefined,
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export function createSymbolNode(
|
|
99
|
-
ctx: ParserContext,
|
|
100
|
-
current: symbol,
|
|
101
|
-
): SerovalIndexedValueNode | SerovalReferenceNode | SerovalWKSymbolNode {
|
|
102
|
-
const id = createIndexedValue(ctx, current);
|
|
103
|
-
if (ctx.reference.marked.has(id)) {
|
|
104
|
-
return createIndexedValueNode(id);
|
|
105
|
-
}
|
|
106
|
-
if (hasReferenceID(current)) {
|
|
107
|
-
return createReferenceNode(id, current);
|
|
108
|
-
}
|
|
109
|
-
return createWKSymbolNode(id, current as WellKnownSymbols);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export function createFunctionNode(
|
|
113
|
-
ctx: ParserContext,
|
|
114
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
115
|
-
current: Function,
|
|
116
|
-
): SerovalIndexedValueNode | SerovalReferenceNode {
|
|
117
|
-
assert(hasReferenceID(current), new Error('Cannot serialize function without reference ID.'));
|
|
118
|
-
const id = createIndexedValue(ctx, current);
|
|
119
|
-
if (ctx.reference.marked.has(id)) {
|
|
120
|
-
return createIndexedValueNode(id);
|
|
121
|
-
}
|
|
122
|
-
return createReferenceNode(id, current);
|
|
123
|
-
}
|
package/src/core/tree/web-api.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import type { ParserContext } from './context';
|
|
2
|
-
import { serializeString } from '../string';
|
|
3
|
-
import { serializeArrayBuffer } from './primitives';
|
|
4
|
-
import type {
|
|
5
|
-
SerovalBlobNode,
|
|
6
|
-
SerovalFileNode,
|
|
7
|
-
} from '../types';
|
|
8
|
-
import { SerovalNodeType } from '../constants';
|
|
9
|
-
|
|
10
|
-
export async function createBlobNode(
|
|
11
|
-
ctx: ParserContext,
|
|
12
|
-
id: number,
|
|
13
|
-
current: Blob,
|
|
14
|
-
): Promise<SerovalBlobNode> {
|
|
15
|
-
return {
|
|
16
|
-
t: SerovalNodeType.Blob,
|
|
17
|
-
i: id,
|
|
18
|
-
s: undefined,
|
|
19
|
-
l: undefined,
|
|
20
|
-
c: serializeString(current.type),
|
|
21
|
-
m: undefined,
|
|
22
|
-
p: undefined,
|
|
23
|
-
e: undefined,
|
|
24
|
-
f: serializeArrayBuffer(ctx, await current.arrayBuffer()),
|
|
25
|
-
a: undefined,
|
|
26
|
-
b: undefined,
|
|
27
|
-
o: undefined,
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export async function createFileNode(
|
|
32
|
-
ctx: ParserContext,
|
|
33
|
-
id: number,
|
|
34
|
-
current: File,
|
|
35
|
-
): Promise<SerovalFileNode> {
|
|
36
|
-
return {
|
|
37
|
-
t: SerovalNodeType.File,
|
|
38
|
-
i: id,
|
|
39
|
-
s: undefined,
|
|
40
|
-
l: undefined,
|
|
41
|
-
c: serializeString(current.type),
|
|
42
|
-
m: serializeString(current.name),
|
|
43
|
-
p: undefined,
|
|
44
|
-
e: undefined,
|
|
45
|
-
f: serializeArrayBuffer(ctx, await current.arrayBuffer()),
|
|
46
|
-
a: undefined,
|
|
47
|
-
b: current.lastModified,
|
|
48
|
-
o: undefined,
|
|
49
|
-
};
|
|
50
|
-
}
|