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
package/src/core/tree/index.ts
CHANGED
|
@@ -1,93 +1,44 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { SerovalNodeType } from '../constants';
|
|
1
|
+
import type { PluginAccessOptions } from '../plugin';
|
|
3
2
|
import type { SerovalNode } from '../types';
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
} from './
|
|
9
|
-
import
|
|
10
|
-
createDeserializerContext,
|
|
11
|
-
createParserContext,
|
|
12
|
-
createSerializerContext,
|
|
13
|
-
getRefParam,
|
|
14
|
-
} from './context';
|
|
15
|
-
import deserializeTree from './deserialize';
|
|
16
|
-
import serializeTree, { resolvePatches } from './serialize';
|
|
17
|
-
import parseSync from './sync';
|
|
18
|
-
|
|
19
|
-
function finalize(
|
|
20
|
-
ctx: SerializerContext,
|
|
21
|
-
rootID: number | undefined,
|
|
22
|
-
isObject: boolean,
|
|
23
|
-
result: string,
|
|
24
|
-
): string {
|
|
25
|
-
// Shared references detected
|
|
26
|
-
if (rootID != null && ctx.vars.length) {
|
|
27
|
-
const patches = resolvePatches(ctx);
|
|
28
|
-
let body = result;
|
|
29
|
-
if (patches) {
|
|
30
|
-
// Get (or create) a ref from the source
|
|
31
|
-
const index = getRefParam(ctx, rootID);
|
|
32
|
-
body = result + ',' + patches + index;
|
|
33
|
-
if (!result.startsWith(index + '=')) {
|
|
34
|
-
body = index + '=' + body;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
let params = ctx.vars.length > 1
|
|
38
|
-
? ctx.vars.join(',')
|
|
39
|
-
: ctx.vars[0];
|
|
40
|
-
// Source is probably already assigned
|
|
41
|
-
if (ctx.features & Feature.ArrowFunction) {
|
|
42
|
-
params = ctx.vars.length > 1 || ctx.vars.length === 0
|
|
43
|
-
? '(' + params + ')'
|
|
44
|
-
: params;
|
|
45
|
-
return '(' + params + '=>(' + body + '))()';
|
|
46
|
-
}
|
|
47
|
-
return '(function(' + params + '){return ' + body + '})()';
|
|
48
|
-
}
|
|
49
|
-
if (isObject) {
|
|
50
|
-
return '(' + result + ')';
|
|
51
|
-
}
|
|
52
|
-
return result;
|
|
53
|
-
}
|
|
3
|
+
import type { AsyncParserContextOptions } from './async';
|
|
4
|
+
import AsyncParserContext from './async';
|
|
5
|
+
import VanillaDeserializerContext from './deserialize';
|
|
6
|
+
import VanillaSerializerContext from './serialize';
|
|
7
|
+
import type { SyncParserContextOptions } from './sync';
|
|
8
|
+
import SyncParserContext from './sync';
|
|
54
9
|
|
|
55
10
|
export function serialize<T>(
|
|
56
11
|
source: T,
|
|
57
|
-
options
|
|
12
|
+
options: SyncParserContextOptions = {},
|
|
58
13
|
): string {
|
|
59
|
-
const ctx =
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
14
|
+
const ctx = new SyncParserContext({
|
|
15
|
+
plugins: options.plugins,
|
|
16
|
+
disabledFeatures: options.disabledFeatures,
|
|
17
|
+
});
|
|
18
|
+
const tree = ctx.parse(source);
|
|
19
|
+
const serial = new VanillaSerializerContext({
|
|
20
|
+
plugins: options.plugins,
|
|
63
21
|
features: ctx.features,
|
|
22
|
+
markedRefs: ctx.marked,
|
|
64
23
|
});
|
|
65
|
-
|
|
66
|
-
return finalize(
|
|
67
|
-
serial,
|
|
68
|
-
tree.i,
|
|
69
|
-
tree.t === SerovalNodeType.Object,
|
|
70
|
-
result,
|
|
71
|
-
);
|
|
24
|
+
return serial.serializeTop(tree);
|
|
72
25
|
}
|
|
73
26
|
|
|
74
27
|
export async function serializeAsync<T>(
|
|
75
28
|
source: T,
|
|
76
|
-
options
|
|
29
|
+
options: AsyncParserContextOptions = {},
|
|
77
30
|
): Promise<string> {
|
|
78
|
-
const ctx =
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
31
|
+
const ctx = new AsyncParserContext({
|
|
32
|
+
plugins: options.plugins,
|
|
33
|
+
disabledFeatures: options.disabledFeatures,
|
|
34
|
+
});
|
|
35
|
+
const tree = await ctx.parse(source);
|
|
36
|
+
const serial = new VanillaSerializerContext({
|
|
37
|
+
plugins: options.plugins,
|
|
82
38
|
features: ctx.features,
|
|
39
|
+
markedRefs: ctx.marked,
|
|
83
40
|
});
|
|
84
|
-
|
|
85
|
-
return finalize(
|
|
86
|
-
serial,
|
|
87
|
-
tree.i,
|
|
88
|
-
tree.t === SerovalNodeType.Object,
|
|
89
|
-
result,
|
|
90
|
-
);
|
|
41
|
+
return serial.serializeTop(tree);
|
|
91
42
|
}
|
|
92
43
|
|
|
93
44
|
export function deserialize<T>(source: string): T {
|
|
@@ -103,40 +54,47 @@ export interface SerovalJSON {
|
|
|
103
54
|
|
|
104
55
|
export function toJSON<T>(
|
|
105
56
|
source: T,
|
|
106
|
-
options
|
|
57
|
+
options: SyncParserContextOptions = {},
|
|
107
58
|
): SerovalJSON {
|
|
108
|
-
const ctx =
|
|
59
|
+
const ctx = new SyncParserContext({
|
|
60
|
+
plugins: options.plugins,
|
|
61
|
+
disabledFeatures: options.disabledFeatures,
|
|
62
|
+
});
|
|
109
63
|
return {
|
|
110
|
-
t:
|
|
64
|
+
t: ctx.parse(source),
|
|
111
65
|
f: ctx.features,
|
|
112
|
-
m: Array.from(ctx.
|
|
66
|
+
m: Array.from(ctx.marked),
|
|
113
67
|
};
|
|
114
68
|
}
|
|
115
69
|
|
|
116
70
|
export async function toJSONAsync<T>(
|
|
117
71
|
source: T,
|
|
118
|
-
options
|
|
72
|
+
options: AsyncParserContextOptions = {},
|
|
119
73
|
): Promise<SerovalJSON> {
|
|
120
|
-
const ctx =
|
|
74
|
+
const ctx = new AsyncParserContext({
|
|
75
|
+
plugins: options.plugins,
|
|
76
|
+
disabledFeatures: options.disabledFeatures,
|
|
77
|
+
});
|
|
121
78
|
return {
|
|
122
|
-
t: await
|
|
79
|
+
t: await ctx.parse(source),
|
|
123
80
|
f: ctx.features,
|
|
124
|
-
m: Array.from(ctx.
|
|
81
|
+
m: Array.from(ctx.marked),
|
|
125
82
|
};
|
|
126
83
|
}
|
|
127
84
|
|
|
128
|
-
export function compileJSON(source: SerovalJSON): string {
|
|
129
|
-
const
|
|
85
|
+
export function compileJSON(source: SerovalJSON, options: PluginAccessOptions = {}): string {
|
|
86
|
+
const ctx = new VanillaSerializerContext({
|
|
87
|
+
plugins: options.plugins,
|
|
130
88
|
features: source.f,
|
|
131
89
|
markedRefs: source.m,
|
|
132
90
|
});
|
|
133
|
-
|
|
134
|
-
return finalize(serial, source.t.i, source.t.i === SerovalNodeType.Object, result);
|
|
91
|
+
return ctx.serializeTop(source.t);
|
|
135
92
|
}
|
|
136
93
|
|
|
137
|
-
export function fromJSON<T>(source: SerovalJSON): T {
|
|
138
|
-
const
|
|
94
|
+
export function fromJSON<T>(source: SerovalJSON, options: PluginAccessOptions = {}): T {
|
|
95
|
+
const ctx = new VanillaDeserializerContext({
|
|
96
|
+
plugins: options.plugins,
|
|
139
97
|
markedRefs: source.m,
|
|
140
98
|
});
|
|
141
|
-
return
|
|
99
|
+
return ctx.deserialize(source.t) as T;
|
|
142
100
|
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { createIndexedValueNode, createReferenceNode } from '../base-primitives';
|
|
2
|
+
import { hasReferenceID } from '../reference';
|
|
3
|
+
import type { SerovalIndexedValueNode, SerovalReferenceNode } from '../types';
|
|
4
|
+
|
|
5
|
+
function createIndexedValue<T>(
|
|
6
|
+
ids: Map<unknown, number>,
|
|
7
|
+
marked: Set<number>,
|
|
8
|
+
current: T,
|
|
9
|
+
): number {
|
|
10
|
+
const ref = ids.get(current);
|
|
11
|
+
if (ref == null) {
|
|
12
|
+
const id = ids.size;
|
|
13
|
+
ids.set(current, id);
|
|
14
|
+
return id;
|
|
15
|
+
}
|
|
16
|
+
marked.add(ref);
|
|
17
|
+
return ref;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function getVanillaReference<T>(
|
|
21
|
+
ids: Map<unknown, number>,
|
|
22
|
+
marked: Set<number>,
|
|
23
|
+
current: T,
|
|
24
|
+
): number | SerovalIndexedValueNode | SerovalReferenceNode {
|
|
25
|
+
const id = createIndexedValue(ids, marked, current);
|
|
26
|
+
if (marked.has(id)) {
|
|
27
|
+
return createIndexedValueNode(id);
|
|
28
|
+
}
|
|
29
|
+
if (hasReferenceID(current)) {
|
|
30
|
+
return createReferenceNode(id, current);
|
|
31
|
+
}
|
|
32
|
+
return id;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function getStrictVanillaReference<T>(
|
|
36
|
+
ids: Map<unknown, number>,
|
|
37
|
+
marked: Set<number>,
|
|
38
|
+
current: T,
|
|
39
|
+
): SerovalIndexedValueNode | SerovalReferenceNode {
|
|
40
|
+
const id = createIndexedValue(ids, marked, current);
|
|
41
|
+
if (marked.has(id)) {
|
|
42
|
+
return createIndexedValueNode(id);
|
|
43
|
+
}
|
|
44
|
+
return createReferenceNode(id, current);
|
|
45
|
+
}
|