seroval 0.11.6 → 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 +3072 -4875
- package/dist/cjs/development/index.cjs.map +4 -4
- package/dist/cjs/production/index.cjs +3 -3
- package/dist/esm/development/index.mjs +3069 -4874
- 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 -751
- 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 -873
- package/src/core/cross/sync.ts +6 -624
- 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 -123
- package/src/core/tree/async.ts +6 -750
- 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 -624
- 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
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { SerovalObjectFlags } from './constants';
|
|
2
|
-
interface IndexAssignment {
|
|
3
|
-
t: 'index';
|
|
4
|
-
s: string;
|
|
5
|
-
k: undefined;
|
|
6
|
-
v: string;
|
|
7
|
-
}
|
|
8
|
-
interface SetAssignment {
|
|
9
|
-
t: 'set';
|
|
10
|
-
s: string;
|
|
11
|
-
k: string;
|
|
12
|
-
v: string;
|
|
13
|
-
}
|
|
14
|
-
interface AddAssignment {
|
|
15
|
-
t: 'add';
|
|
16
|
-
s: string;
|
|
17
|
-
k: undefined;
|
|
18
|
-
v: string;
|
|
19
|
-
}
|
|
20
|
-
interface AppendAssignment {
|
|
21
|
-
t: 'append';
|
|
22
|
-
s: string;
|
|
23
|
-
k: string;
|
|
24
|
-
v: string;
|
|
25
|
-
}
|
|
26
|
-
export type Assignment = IndexAssignment | AddAssignment | SetAssignment | AppendAssignment;
|
|
27
|
-
export interface FlaggedObject {
|
|
28
|
-
type: SerovalObjectFlags;
|
|
29
|
-
value: string;
|
|
30
|
-
}
|
|
31
|
-
export declare function resolveAssignments(assignments: Assignment[]): string | undefined;
|
|
32
|
-
export declare function resolveFlags(flags: FlaggedObject[]): string | undefined;
|
|
33
|
-
export {};
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { Assignment, FlaggedObject } from './assignments';
|
|
2
|
-
export interface BaseParserContext {
|
|
3
|
-
features: number;
|
|
4
|
-
}
|
|
5
|
-
export interface BaseSerializerContext extends BaseParserContext {
|
|
6
|
-
stack: number[];
|
|
7
|
-
flags: FlaggedObject[];
|
|
8
|
-
assignments: Assignment[];
|
|
9
|
-
}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import type { Assignment, FlaggedObject } from '../assignments';
|
|
2
|
-
import type { BaseParserContext } from '../context';
|
|
3
|
-
import type { SerovalNode } from '../types';
|
|
4
|
-
export interface CrossParserContextOptions {
|
|
5
|
-
scopeId?: string;
|
|
6
|
-
refs?: Map<unknown, number>;
|
|
7
|
-
disabledFeatures?: number;
|
|
8
|
-
}
|
|
9
|
-
export interface CrossParserContext {
|
|
10
|
-
scopeId?: string;
|
|
11
|
-
refs: Map<unknown, number>;
|
|
12
|
-
features: number;
|
|
13
|
-
}
|
|
14
|
-
export declare function createCrossParserContext(options?: CrossParserContextOptions): CrossParserContext;
|
|
15
|
-
export declare function createCrossIndexedValue<T>(ctx: CrossParserContext, current: T): number;
|
|
16
|
-
export interface StreamingCrossParserContextOptions extends CrossParserContextOptions {
|
|
17
|
-
onParse: (node: SerovalNode, initial: boolean) => void;
|
|
18
|
-
onDone?: () => void;
|
|
19
|
-
}
|
|
20
|
-
export interface StreamingCrossParserContext extends CrossParserContext {
|
|
21
|
-
alive: boolean;
|
|
22
|
-
pending: number;
|
|
23
|
-
onParse(node: SerovalNode, initial: boolean): void;
|
|
24
|
-
onDone(): void;
|
|
25
|
-
}
|
|
26
|
-
export declare function createStreamingCrossParserContext(options: StreamingCrossParserContextOptions): StreamingCrossParserContext;
|
|
27
|
-
export declare function pushPendingState(ctx: StreamingCrossParserContext): void;
|
|
28
|
-
export declare function popPendingState(ctx: StreamingCrossParserContext): void;
|
|
29
|
-
export interface CrossSerializerContext extends BaseParserContext {
|
|
30
|
-
stack: number[];
|
|
31
|
-
assignments: Assignment[];
|
|
32
|
-
flags: FlaggedObject[];
|
|
33
|
-
}
|
|
34
|
-
export interface CrossSerializerOptions {
|
|
35
|
-
features: number;
|
|
36
|
-
}
|
|
37
|
-
export declare function createCrossSerializerContext(options: CrossSerializerOptions): CrossSerializerContext;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { CrossParserContext } from './context';
|
|
2
|
-
import type { BigIntTypedArrayValue, TypedArrayValue } from '../../types';
|
|
3
|
-
import type { SerovalBigIntTypedArrayNode, SerovalTypedArrayNode, SerovalDataViewNode, SerovalNode, SerovalIndexedValueNode, SerovalReferenceNode, SerovalWKSymbolNode } from '../types';
|
|
4
|
-
export declare function serializeArrayBuffer(ctx: CrossParserContext, current: ArrayBuffer): SerovalNode;
|
|
5
|
-
export declare function createTypedArrayNode(ctx: CrossParserContext, id: number, current: TypedArrayValue): SerovalTypedArrayNode;
|
|
6
|
-
export declare function createBigIntTypedArrayNode(ctx: CrossParserContext, id: number, current: BigIntTypedArrayValue): SerovalBigIntTypedArrayNode;
|
|
7
|
-
export declare function createDataViewNode(ctx: CrossParserContext, id: number, current: DataView): SerovalDataViewNode;
|
|
8
|
-
export declare function createSymbolNode(ctx: CrossParserContext, current: symbol): SerovalIndexedValueNode | SerovalReferenceNode | SerovalWKSymbolNode;
|
|
9
|
-
export declare function createFunctionNode(ctx: CrossParserContext, current: Function): SerovalIndexedValueNode | SerovalReferenceNode;
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import type { CrossParserContext } from './context';
|
|
2
|
-
import type { SerovalBlobNode, SerovalFileNode } from '../types';
|
|
3
|
-
export declare function createBlobNode(ctx: CrossParserContext, id: number, current: Blob): Promise<SerovalBlobNode>;
|
|
4
|
-
export declare function createFileNode(ctx: CrossParserContext, id: number, current: File): Promise<SerovalFileNode>;
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import type { BaseParserContext, BaseSerializerContext } from '../context';
|
|
2
|
-
export interface ParserReference {
|
|
3
|
-
ids: Map<unknown, number>;
|
|
4
|
-
marked: Set<number>;
|
|
5
|
-
}
|
|
6
|
-
export interface ParserContext extends BaseParserContext {
|
|
7
|
-
reference: ParserReference;
|
|
8
|
-
}
|
|
9
|
-
export interface ParserOptions {
|
|
10
|
-
disabledFeatures: number;
|
|
11
|
-
}
|
|
12
|
-
export declare function createParserContext(options?: Partial<ParserOptions>): ParserContext;
|
|
13
|
-
export interface SerializerReference {
|
|
14
|
-
size: number;
|
|
15
|
-
valid: (number | undefined)[];
|
|
16
|
-
marked: Set<number>;
|
|
17
|
-
}
|
|
18
|
-
export interface SerializerContext extends BaseSerializerContext {
|
|
19
|
-
reference: SerializerReference;
|
|
20
|
-
vars: (string | undefined)[];
|
|
21
|
-
}
|
|
22
|
-
export interface SerializerOptions {
|
|
23
|
-
markedRefs: number[] | Set<number>;
|
|
24
|
-
features: number;
|
|
25
|
-
}
|
|
26
|
-
export declare function createSerializerContext(options: SerializerOptions): SerializerContext;
|
|
27
|
-
/**
|
|
28
|
-
* Increments the number of references the referenced value has
|
|
29
|
-
*/
|
|
30
|
-
export declare function markRef(ctx: ParserContext | SerializerContext, current: number): void;
|
|
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
|
-
export declare function getRefParam(ctx: SerializerContext, index: number): string;
|
|
36
|
-
export declare function createIndexedValue<T>(ctx: ParserContext, current: T): number;
|
|
37
|
-
export interface DeserializerContext {
|
|
38
|
-
values: Map<number, unknown>;
|
|
39
|
-
refs: Set<number>;
|
|
40
|
-
}
|
|
41
|
-
export interface DeserializerOptions {
|
|
42
|
-
markedRefs: number[] | Set<number>;
|
|
43
|
-
}
|
|
44
|
-
export declare function createDeserializerContext(options: DeserializerOptions): DeserializerContext;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { ParserContext } from './context';
|
|
2
|
-
import type { BigIntTypedArrayValue, TypedArrayValue } from '../../types';
|
|
3
|
-
import type { SerovalBigIntTypedArrayNode, SerovalTypedArrayNode, SerovalWKSymbolNode, SerovalReferenceNode, SerovalDataViewNode, SerovalNode, SerovalIndexedValueNode } from '../types';
|
|
4
|
-
export declare function serializeArrayBuffer(ctx: ParserContext, current: ArrayBuffer): SerovalNode;
|
|
5
|
-
export declare function createTypedArrayNode(ctx: ParserContext, id: number, current: TypedArrayValue): SerovalTypedArrayNode;
|
|
6
|
-
export declare function createBigIntTypedArrayNode(ctx: ParserContext, id: number, current: BigIntTypedArrayValue): SerovalBigIntTypedArrayNode;
|
|
7
|
-
export declare function createDataViewNode(ctx: ParserContext, id: number, current: DataView): SerovalDataViewNode;
|
|
8
|
-
export declare function createSymbolNode(ctx: ParserContext, current: symbol): SerovalIndexedValueNode | SerovalReferenceNode | SerovalWKSymbolNode;
|
|
9
|
-
export declare function createFunctionNode(ctx: ParserContext, current: Function): SerovalIndexedValueNode | SerovalReferenceNode;
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import type { ParserContext } from './context';
|
|
2
|
-
import type { SerovalBlobNode, SerovalFileNode } from '../types';
|
|
3
|
-
export declare function createBlobNode(ctx: ParserContext, id: number, current: Blob): Promise<SerovalBlobNode>;
|
|
4
|
-
export declare function createFileNode(ctx: ParserContext, id: number, current: File): Promise<SerovalFileNode>;
|
package/src/core/assignments.ts
DELETED
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
import { SerovalObjectFlags } from './constants';
|
|
2
|
-
|
|
3
|
-
interface IndexAssignment {
|
|
4
|
-
t: 'index';
|
|
5
|
-
s: string;
|
|
6
|
-
k: undefined;
|
|
7
|
-
v: string;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
interface SetAssignment {
|
|
11
|
-
t: 'set';
|
|
12
|
-
s: string;
|
|
13
|
-
k: string;
|
|
14
|
-
v: string;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
interface AddAssignment {
|
|
18
|
-
t: 'add';
|
|
19
|
-
s: string;
|
|
20
|
-
k: undefined;
|
|
21
|
-
v: string;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
interface AppendAssignment {
|
|
25
|
-
t: 'append';
|
|
26
|
-
s: string;
|
|
27
|
-
k: string;
|
|
28
|
-
v: string;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// Array of assignments to be done (used for recursion)
|
|
32
|
-
export type Assignment =
|
|
33
|
-
| IndexAssignment
|
|
34
|
-
| AddAssignment
|
|
35
|
-
| SetAssignment
|
|
36
|
-
| AppendAssignment;
|
|
37
|
-
|
|
38
|
-
export interface FlaggedObject {
|
|
39
|
-
type: SerovalObjectFlags;
|
|
40
|
-
value: string;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function getAssignmentExpression(assignment: Assignment): string {
|
|
44
|
-
switch (assignment.t) {
|
|
45
|
-
case 'index':
|
|
46
|
-
return assignment.s + '=' + assignment.v;
|
|
47
|
-
case 'set':
|
|
48
|
-
return assignment.s + '.set(' + assignment.k + ',' + assignment.v + ')';
|
|
49
|
-
case 'add':
|
|
50
|
-
return assignment.s + '.add(' + assignment.v + ')';
|
|
51
|
-
case 'append':
|
|
52
|
-
return assignment.s + '.append(' + assignment.k + ',' + assignment.v + ')';
|
|
53
|
-
default:
|
|
54
|
-
return '';
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const OBJECT_FLAG_CONSTRUCTOR: Record<SerovalObjectFlags, string | undefined> = {
|
|
59
|
-
[SerovalObjectFlags.Frozen]: 'Object.freeze',
|
|
60
|
-
[SerovalObjectFlags.Sealed]: 'Object.seal',
|
|
61
|
-
[SerovalObjectFlags.NonExtensible]: 'Object.preventExtensions',
|
|
62
|
-
[SerovalObjectFlags.None]: undefined,
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
function mergeAssignments(assignments: Assignment[]): Assignment[] {
|
|
66
|
-
const newAssignments: Assignment[] = [];
|
|
67
|
-
let current = assignments[0];
|
|
68
|
-
let prev = current;
|
|
69
|
-
let item: Assignment;
|
|
70
|
-
for (let i = 1, len = assignments.length; i < len; i++) {
|
|
71
|
-
item = assignments[i];
|
|
72
|
-
if (item.t === prev.t) {
|
|
73
|
-
switch (item.t) {
|
|
74
|
-
case 'index':
|
|
75
|
-
if (item.v === prev.v) {
|
|
76
|
-
// Merge if the right-hand value is the same
|
|
77
|
-
// saves at least 2 chars
|
|
78
|
-
current = {
|
|
79
|
-
t: 'index',
|
|
80
|
-
s: item.s,
|
|
81
|
-
k: undefined,
|
|
82
|
-
v: getAssignmentExpression(current),
|
|
83
|
-
};
|
|
84
|
-
} else {
|
|
85
|
-
// Different assignment, push current
|
|
86
|
-
newAssignments.push(current);
|
|
87
|
-
current = item;
|
|
88
|
-
}
|
|
89
|
-
break;
|
|
90
|
-
case 'set':
|
|
91
|
-
if (item.s === prev.s) {
|
|
92
|
-
// Maps has chaining methods, merge if source is the same
|
|
93
|
-
current = {
|
|
94
|
-
t: 'set',
|
|
95
|
-
s: getAssignmentExpression(current),
|
|
96
|
-
k: item.k,
|
|
97
|
-
v: item.v,
|
|
98
|
-
};
|
|
99
|
-
} else {
|
|
100
|
-
// Different assignment, push current
|
|
101
|
-
newAssignments.push(current);
|
|
102
|
-
current = item;
|
|
103
|
-
}
|
|
104
|
-
break;
|
|
105
|
-
case 'add':
|
|
106
|
-
if (item.s === prev.s) {
|
|
107
|
-
// Sets has chaining methods too
|
|
108
|
-
current = {
|
|
109
|
-
t: 'add',
|
|
110
|
-
s: getAssignmentExpression(current),
|
|
111
|
-
k: undefined,
|
|
112
|
-
v: item.v,
|
|
113
|
-
};
|
|
114
|
-
} else {
|
|
115
|
-
// Different assignment, push current
|
|
116
|
-
newAssignments.push(current);
|
|
117
|
-
current = item;
|
|
118
|
-
}
|
|
119
|
-
break;
|
|
120
|
-
case 'append':
|
|
121
|
-
// Different assignment, push current
|
|
122
|
-
newAssignments.push(current);
|
|
123
|
-
current = item;
|
|
124
|
-
break;
|
|
125
|
-
default:
|
|
126
|
-
break;
|
|
127
|
-
}
|
|
128
|
-
} else {
|
|
129
|
-
newAssignments.push(current);
|
|
130
|
-
current = item;
|
|
131
|
-
}
|
|
132
|
-
prev = item;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
newAssignments.push(current);
|
|
136
|
-
|
|
137
|
-
return newAssignments;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
export function resolveAssignments(assignments: Assignment[]): string | undefined {
|
|
141
|
-
if (assignments.length) {
|
|
142
|
-
let result = '';
|
|
143
|
-
const merged = mergeAssignments(assignments);
|
|
144
|
-
for (let i = 0, len = merged.length; i < len; i++) {
|
|
145
|
-
result += getAssignmentExpression(merged[i]) + ',';
|
|
146
|
-
}
|
|
147
|
-
return result;
|
|
148
|
-
}
|
|
149
|
-
return undefined;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
export function resolveFlags(flags: FlaggedObject[]): string | undefined {
|
|
153
|
-
let result = '';
|
|
154
|
-
for (let i = 0, len = flags.length; i < len; i++) {
|
|
155
|
-
const flag = flags[i];
|
|
156
|
-
result += OBJECT_FLAG_CONSTRUCTOR[flag.type] + '(' + flag.value + '),';
|
|
157
|
-
}
|
|
158
|
-
return result;
|
|
159
|
-
}
|
package/src/core/context.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { Assignment, FlaggedObject } from './assignments';
|
|
2
|
-
|
|
3
|
-
export interface BaseParserContext {
|
|
4
|
-
// Supported features
|
|
5
|
-
features: number;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export interface BaseSerializerContext extends BaseParserContext {
|
|
9
|
-
// To check if an object is synchronously referencing itself
|
|
10
|
-
stack: number[];
|
|
11
|
-
// Array of object mutations
|
|
12
|
-
flags: FlaggedObject[];
|
|
13
|
-
// Array of assignments to be done (used for recursion)
|
|
14
|
-
assignments: Assignment[];
|
|
15
|
-
}
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import type { Assignment, FlaggedObject } from '../assignments';
|
|
2
|
-
import { ALL_ENABLED } from '../compat';
|
|
3
|
-
import type { BaseParserContext } from '../context';
|
|
4
|
-
import type { SerovalNode } from '../types';
|
|
5
|
-
|
|
6
|
-
export interface CrossParserContextOptions {
|
|
7
|
-
scopeId?: string;
|
|
8
|
-
refs?: Map<unknown, number>;
|
|
9
|
-
disabledFeatures?: number;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export interface CrossParserContext {
|
|
13
|
-
scopeId?: string;
|
|
14
|
-
refs: Map<unknown, number>;
|
|
15
|
-
features: number;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export function createCrossParserContext(
|
|
19
|
-
options: CrossParserContextOptions = {},
|
|
20
|
-
): CrossParserContext {
|
|
21
|
-
return {
|
|
22
|
-
scopeId: options.scopeId,
|
|
23
|
-
refs: options.refs || new Map<unknown, number>(),
|
|
24
|
-
features: ALL_ENABLED ^ (options.disabledFeatures || 0),
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export function createCrossIndexedValue<T>(
|
|
29
|
-
ctx: CrossParserContext,
|
|
30
|
-
current: T,
|
|
31
|
-
): number {
|
|
32
|
-
const ref = ctx.refs.get(current);
|
|
33
|
-
if (ref == null) {
|
|
34
|
-
const id = ctx.refs.size;
|
|
35
|
-
ctx.refs.set(current, id);
|
|
36
|
-
return id;
|
|
37
|
-
}
|
|
38
|
-
return ref;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export interface StreamingCrossParserContextOptions extends CrossParserContextOptions {
|
|
42
|
-
onParse: (node: SerovalNode, initial: boolean) => void;
|
|
43
|
-
onDone?: () => void;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export interface StreamingCrossParserContext extends CrossParserContext {
|
|
47
|
-
alive: boolean;
|
|
48
|
-
pending: number;
|
|
49
|
-
onParse(node: SerovalNode, initial: boolean): void;
|
|
50
|
-
onDone(): void;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export function createStreamingCrossParserContext(
|
|
54
|
-
options: StreamingCrossParserContextOptions,
|
|
55
|
-
): StreamingCrossParserContext {
|
|
56
|
-
return {
|
|
57
|
-
alive: true,
|
|
58
|
-
pending: 0,
|
|
59
|
-
refs: options.refs || new Map<unknown, number>(),
|
|
60
|
-
features: ALL_ENABLED ^ (options.disabledFeatures || 0),
|
|
61
|
-
onParse: options.onParse,
|
|
62
|
-
onDone(): void {
|
|
63
|
-
if (options.onDone) {
|
|
64
|
-
options.onDone();
|
|
65
|
-
}
|
|
66
|
-
},
|
|
67
|
-
scopeId: options.scopeId,
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export function pushPendingState(ctx: StreamingCrossParserContext): void {
|
|
72
|
-
ctx.pending++;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export function popPendingState(ctx: StreamingCrossParserContext): void {
|
|
76
|
-
if (--ctx.pending <= 0) {
|
|
77
|
-
ctx.onDone();
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export interface CrossSerializerContext extends BaseParserContext {
|
|
82
|
-
stack: number[];
|
|
83
|
-
// Array of assignments to be done (used for recursion)
|
|
84
|
-
assignments: Assignment[];
|
|
85
|
-
// Object flags
|
|
86
|
-
flags: FlaggedObject[];
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export interface CrossSerializerOptions {
|
|
90
|
-
features: number;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export function createCrossSerializerContext(
|
|
94
|
-
options: CrossSerializerOptions,
|
|
95
|
-
): CrossSerializerContext {
|
|
96
|
-
return {
|
|
97
|
-
stack: [],
|
|
98
|
-
assignments: [],
|
|
99
|
-
features: options.features,
|
|
100
|
-
flags: [],
|
|
101
|
-
};
|
|
102
|
-
}
|
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import assert from '../assert';
|
|
2
|
-
import type { CrossParserContext } from './context';
|
|
3
|
-
import type { BigIntTypedArrayValue, TypedArrayValue } from '../../types';
|
|
4
|
-
import { hasReferenceID } from '../reference';
|
|
5
|
-
import type {
|
|
6
|
-
SerovalBigIntTypedArrayNode,
|
|
7
|
-
SerovalTypedArrayNode,
|
|
8
|
-
SerovalDataViewNode,
|
|
9
|
-
SerovalNode,
|
|
10
|
-
SerovalIndexedValueNode,
|
|
11
|
-
SerovalReferenceNode,
|
|
12
|
-
SerovalWKSymbolNode,
|
|
13
|
-
} from '../types';
|
|
14
|
-
import type { WellKnownSymbols } from '../constants';
|
|
15
|
-
import { SerovalNodeType } from '../constants';
|
|
16
|
-
import {
|
|
17
|
-
createIndexedValueNode,
|
|
18
|
-
createArrayBufferNode,
|
|
19
|
-
createWKSymbolNode,
|
|
20
|
-
createReferenceNode,
|
|
21
|
-
} from '../base-primitives';
|
|
22
|
-
|
|
23
|
-
export function serializeArrayBuffer(
|
|
24
|
-
ctx: CrossParserContext,
|
|
25
|
-
current: ArrayBuffer,
|
|
26
|
-
): SerovalNode {
|
|
27
|
-
const id = ctx.refs.get(current);
|
|
28
|
-
if (id != null) {
|
|
29
|
-
return createIndexedValueNode(id);
|
|
30
|
-
}
|
|
31
|
-
const newID = ctx.refs.size;
|
|
32
|
-
ctx.refs.set(current, newID);
|
|
33
|
-
return createArrayBufferNode(newID, current);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export function createTypedArrayNode(
|
|
37
|
-
ctx: CrossParserContext,
|
|
38
|
-
id: number,
|
|
39
|
-
current: TypedArrayValue,
|
|
40
|
-
): SerovalTypedArrayNode {
|
|
41
|
-
return {
|
|
42
|
-
t: SerovalNodeType.TypedArray,
|
|
43
|
-
i: id,
|
|
44
|
-
s: undefined,
|
|
45
|
-
l: current.length,
|
|
46
|
-
c: current.constructor.name,
|
|
47
|
-
m: undefined,
|
|
48
|
-
p: undefined,
|
|
49
|
-
e: undefined,
|
|
50
|
-
a: undefined,
|
|
51
|
-
f: serializeArrayBuffer(ctx, current.buffer),
|
|
52
|
-
b: current.byteOffset,
|
|
53
|
-
o: undefined,
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export function createBigIntTypedArrayNode(
|
|
58
|
-
ctx: CrossParserContext,
|
|
59
|
-
id: number,
|
|
60
|
-
current: BigIntTypedArrayValue,
|
|
61
|
-
): SerovalBigIntTypedArrayNode {
|
|
62
|
-
return {
|
|
63
|
-
t: SerovalNodeType.BigIntTypedArray,
|
|
64
|
-
i: id,
|
|
65
|
-
s: undefined,
|
|
66
|
-
l: current.length,
|
|
67
|
-
c: current.constructor.name,
|
|
68
|
-
m: undefined,
|
|
69
|
-
p: undefined,
|
|
70
|
-
e: undefined,
|
|
71
|
-
a: undefined,
|
|
72
|
-
f: serializeArrayBuffer(ctx, current.buffer),
|
|
73
|
-
b: current.byteOffset,
|
|
74
|
-
o: undefined,
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export function createDataViewNode(
|
|
79
|
-
ctx: CrossParserContext,
|
|
80
|
-
id: number,
|
|
81
|
-
current: DataView,
|
|
82
|
-
): SerovalDataViewNode {
|
|
83
|
-
return {
|
|
84
|
-
t: SerovalNodeType.DataView,
|
|
85
|
-
i: id,
|
|
86
|
-
s: undefined,
|
|
87
|
-
l: current.byteLength,
|
|
88
|
-
c: undefined,
|
|
89
|
-
m: undefined,
|
|
90
|
-
p: undefined,
|
|
91
|
-
e: undefined,
|
|
92
|
-
a: undefined,
|
|
93
|
-
f: serializeArrayBuffer(ctx, current.buffer),
|
|
94
|
-
b: current.byteOffset,
|
|
95
|
-
o: undefined,
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export function createSymbolNode(
|
|
100
|
-
ctx: CrossParserContext,
|
|
101
|
-
current: symbol,
|
|
102
|
-
): SerovalIndexedValueNode | SerovalReferenceNode | SerovalWKSymbolNode {
|
|
103
|
-
const id = ctx.refs.get(current);
|
|
104
|
-
if (id != null) {
|
|
105
|
-
return createIndexedValueNode(id);
|
|
106
|
-
}
|
|
107
|
-
const newID = ctx.refs.size;
|
|
108
|
-
ctx.refs.set(current, newID);
|
|
109
|
-
if (hasReferenceID(current)) {
|
|
110
|
-
return createReferenceNode(newID, current);
|
|
111
|
-
}
|
|
112
|
-
return createWKSymbolNode(newID, current as WellKnownSymbols);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
export function createFunctionNode(
|
|
116
|
-
ctx: CrossParserContext,
|
|
117
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
118
|
-
current: Function,
|
|
119
|
-
): SerovalIndexedValueNode | SerovalReferenceNode {
|
|
120
|
-
assert(hasReferenceID(current), new Error('Cannot serialize function without reference ID.'));
|
|
121
|
-
const id = ctx.refs.get(current);
|
|
122
|
-
if (id != null) {
|
|
123
|
-
return createIndexedValueNode(id);
|
|
124
|
-
}
|
|
125
|
-
const newID = ctx.refs.size;
|
|
126
|
-
ctx.refs.set(current, newID);
|
|
127
|
-
return createReferenceNode(newID, current);
|
|
128
|
-
}
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import type { CrossParserContext } 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: CrossParserContext,
|
|
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: CrossParserContext,
|
|
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
|
-
}
|