seroval 1.3.2 → 1.4.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/dist/cjs/development/index.cjs +2640 -2683
- package/dist/cjs/development/index.cjs.map +4 -4
- package/dist/cjs/production/index.cjs +3 -17
- package/dist/esm/development/index.mjs +2640 -2683
- 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 +1 -2
- package/dist/types/core/base-primitives.d.ts.map +1 -1
- package/dist/types/core/compat.d.ts +1 -1
- package/dist/types/core/compat.d.ts.map +1 -1
- package/dist/types/core/constants.d.ts +1 -0
- 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 +19 -0
- package/dist/types/core/context/async-parser.d.ts.map +1 -0
- package/dist/types/core/context/deserializer.d.ts +35 -38
- package/dist/types/core/context/deserializer.d.ts.map +1 -1
- package/dist/types/core/context/parser.d.ts +32 -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.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/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/symbols.d.ts +14 -0
- package/dist/types/core/symbols.d.ts.map +1 -0
- package/dist/types/core/tree/index.d.ts +3 -2
- package/dist/types/core/tree/index.d.ts.map +1 -1
- package/dist/types/core/types.d.ts +3 -1
- 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 +17 -19
- package/src/core/base-primitives.ts +0 -27
- package/src/core/compat.ts +2 -1
- package/src/core/constants.ts +28 -26
- package/src/core/constructors.ts +269 -0
- package/src/core/context/async-parser.ts +586 -0
- package/src/core/context/deserializer.ts +506 -316
- package/src/core/context/parser.ts +243 -180
- package/src/core/context/serializer.ts +1058 -827
- package/src/core/context/sync-parser.ts +833 -0
- package/src/core/cross/index.ts +63 -47
- package/src/core/errors.ts +73 -12
- package/src/core/function-string.ts +11 -10
- package/src/core/plugin.ts +20 -11
- package/src/core/reference.ts +8 -5
- package/src/core/special-reference.ts +18 -90
- package/src/core/stream.ts +18 -152
- package/src/core/symbols.ts +18 -0
- package/src/core/tree/index.ts +45 -31
- package/src/core/types.ts +7 -2
- package/src/core/utils/iterator-to-sequence.ts +6 -28
- package/src/index.ts +29 -11
- 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/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/cross/async.ts +0 -9
- package/src/core/cross/deserializer.ts +0 -16
- 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/tree/async.ts +0 -9
- package/src/core/tree/deserializer.ts +0 -26
- package/src/core/tree/serializer.ts +0 -98
- package/src/core/tree/sync.ts +0 -9
- package/src/core/utils/assert.ts +0 -5
- package/src/core/utils/deferred.ts +0 -22
package/src/core/cross/index.ts
CHANGED
|
@@ -1,18 +1,34 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { AsyncParserContextOptions } from '../context/async-parser';
|
|
2
|
+
import {
|
|
3
|
+
createAsyncParserContext,
|
|
4
|
+
parseTopAsync,
|
|
5
|
+
} from '../context/async-parser';
|
|
6
|
+
import type { CrossDeserializerContextOptions } from '../context/deserializer';
|
|
7
|
+
import {
|
|
8
|
+
createCrossDeserializerContext,
|
|
9
|
+
deserializeTop,
|
|
10
|
+
} from '../context/deserializer';
|
|
11
|
+
import type { CrossContextOptions } from '../context/serializer';
|
|
12
|
+
import {
|
|
13
|
+
createCrossSerializerContext,
|
|
14
|
+
serializeTopCross,
|
|
15
|
+
} from '../context/serializer';
|
|
16
|
+
import type {
|
|
17
|
+
StreamParserContextOptions,
|
|
18
|
+
SyncParserContextOptions,
|
|
19
|
+
} from '../context/sync-parser';
|
|
20
|
+
import {
|
|
21
|
+
createStreamParserContext,
|
|
22
|
+
createSyncParserContext,
|
|
23
|
+
destroyStreamParse,
|
|
24
|
+
parseTop,
|
|
25
|
+
startStreamParse,
|
|
26
|
+
} from '../context/sync-parser';
|
|
27
|
+
import { resolvePlugins, SerovalMode } from '../plugin';
|
|
2
28
|
import type { SerovalNode } from '../types';
|
|
3
|
-
import type { CrossAsyncParserContextOptions } from './async';
|
|
4
|
-
import AsyncCrossParserContext from './async';
|
|
5
|
-
import type { CrossDeserializerContextOptions } from './deserializer';
|
|
6
|
-
import CrossDeserializerContext from './deserializer';
|
|
7
|
-
import type { CrossContextOptions, CrossParserContextOptions } from './parser';
|
|
8
|
-
import CrossSerializerContext from './serializer';
|
|
9
|
-
import type { CrossStreamParserContextOptions } from './stream';
|
|
10
|
-
import StreamCrossParserContext from './stream';
|
|
11
|
-
import type { CrossSyncParserContextOptions } from './sync';
|
|
12
|
-
import SyncCrossParserContext from './sync';
|
|
13
29
|
|
|
14
30
|
export interface CrossSerializeOptions
|
|
15
|
-
extends
|
|
31
|
+
extends SyncParserContextOptions,
|
|
16
32
|
CrossContextOptions {}
|
|
17
33
|
|
|
18
34
|
export function crossSerialize<T>(
|
|
@@ -20,23 +36,23 @@ export function crossSerialize<T>(
|
|
|
20
36
|
options: CrossSerializeOptions = {},
|
|
21
37
|
): string {
|
|
22
38
|
const plugins = resolvePlugins(options.plugins);
|
|
23
|
-
const ctx =
|
|
39
|
+
const ctx = createSyncParserContext(SerovalMode.Cross, {
|
|
24
40
|
plugins,
|
|
25
41
|
disabledFeatures: options.disabledFeatures,
|
|
26
42
|
refs: options.refs,
|
|
27
43
|
});
|
|
28
|
-
const tree =
|
|
29
|
-
const serial =
|
|
44
|
+
const tree = parseTop(ctx, source);
|
|
45
|
+
const serial = createCrossSerializerContext({
|
|
30
46
|
plugins,
|
|
31
|
-
features: ctx.features,
|
|
47
|
+
features: ctx.base.features,
|
|
32
48
|
scopeId: options.scopeId,
|
|
33
|
-
markedRefs: ctx.marked,
|
|
49
|
+
markedRefs: ctx.base.marked,
|
|
34
50
|
});
|
|
35
|
-
return serial
|
|
51
|
+
return serializeTopCross(serial, tree);
|
|
36
52
|
}
|
|
37
53
|
|
|
38
54
|
export interface CrossSerializeAsyncOptions
|
|
39
|
-
extends
|
|
55
|
+
extends AsyncParserContextOptions,
|
|
40
56
|
CrossContextOptions {}
|
|
41
57
|
|
|
42
58
|
export async function crossSerializeAsync<T>(
|
|
@@ -44,53 +60,53 @@ export async function crossSerializeAsync<T>(
|
|
|
44
60
|
options: CrossSerializeAsyncOptions = {},
|
|
45
61
|
): Promise<string> {
|
|
46
62
|
const plugins = resolvePlugins(options.plugins);
|
|
47
|
-
const ctx =
|
|
63
|
+
const ctx = createAsyncParserContext(SerovalMode.Cross, {
|
|
48
64
|
plugins,
|
|
49
65
|
disabledFeatures: options.disabledFeatures,
|
|
50
66
|
refs: options.refs,
|
|
51
67
|
});
|
|
52
|
-
const tree = await ctx
|
|
53
|
-
const serial =
|
|
68
|
+
const tree = await parseTopAsync(ctx, source);
|
|
69
|
+
const serial = createCrossSerializerContext({
|
|
54
70
|
plugins,
|
|
55
|
-
features: ctx.features,
|
|
71
|
+
features: ctx.base.features,
|
|
56
72
|
scopeId: options.scopeId,
|
|
57
|
-
markedRefs: ctx.marked,
|
|
73
|
+
markedRefs: ctx.base.marked,
|
|
58
74
|
});
|
|
59
|
-
return serial
|
|
75
|
+
return serializeTopCross(serial, tree);
|
|
60
76
|
}
|
|
61
77
|
|
|
62
|
-
export type ToCrossJSONOptions =
|
|
78
|
+
export type ToCrossJSONOptions = SyncParserContextOptions;
|
|
63
79
|
|
|
64
80
|
export function toCrossJSON<T>(
|
|
65
81
|
source: T,
|
|
66
|
-
options:
|
|
82
|
+
options: ToCrossJSONOptions = {},
|
|
67
83
|
): SerovalNode {
|
|
68
84
|
const plugins = resolvePlugins(options.plugins);
|
|
69
|
-
const ctx =
|
|
85
|
+
const ctx = createSyncParserContext(SerovalMode.Cross, {
|
|
70
86
|
plugins,
|
|
71
87
|
disabledFeatures: options.disabledFeatures,
|
|
72
88
|
refs: options.refs,
|
|
73
89
|
});
|
|
74
|
-
return
|
|
90
|
+
return parseTop(ctx, source);
|
|
75
91
|
}
|
|
76
92
|
|
|
77
|
-
export type ToCrossJSONAsyncOptions =
|
|
93
|
+
export type ToCrossJSONAsyncOptions = AsyncParserContextOptions;
|
|
78
94
|
|
|
79
95
|
export async function toCrossJSONAsync<T>(
|
|
80
96
|
source: T,
|
|
81
|
-
options:
|
|
97
|
+
options: ToCrossJSONAsyncOptions = {},
|
|
82
98
|
): Promise<SerovalNode> {
|
|
83
99
|
const plugins = resolvePlugins(options.plugins);
|
|
84
|
-
const ctx =
|
|
100
|
+
const ctx = createAsyncParserContext(SerovalMode.Cross, {
|
|
85
101
|
plugins,
|
|
86
102
|
disabledFeatures: options.disabledFeatures,
|
|
87
103
|
refs: options.refs,
|
|
88
104
|
});
|
|
89
|
-
return await ctx
|
|
105
|
+
return await parseTopAsync(ctx, source);
|
|
90
106
|
}
|
|
91
107
|
|
|
92
108
|
export interface CrossSerializeStreamOptions
|
|
93
|
-
extends Omit<
|
|
109
|
+
extends Omit<StreamParserContextOptions, 'onParse'>,
|
|
94
110
|
CrossContextOptions {
|
|
95
111
|
onSerialize: (data: string, initial: boolean) => void;
|
|
96
112
|
}
|
|
@@ -100,22 +116,22 @@ export function crossSerializeStream<T>(
|
|
|
100
116
|
options: CrossSerializeStreamOptions,
|
|
101
117
|
): () => void {
|
|
102
118
|
const plugins = resolvePlugins(options.plugins);
|
|
103
|
-
const ctx =
|
|
119
|
+
const ctx = createStreamParserContext({
|
|
104
120
|
plugins,
|
|
105
121
|
refs: options.refs,
|
|
106
122
|
disabledFeatures: options.disabledFeatures,
|
|
107
123
|
onParse(node, initial): void {
|
|
108
|
-
const serial =
|
|
124
|
+
const serial = createCrossSerializerContext({
|
|
109
125
|
plugins,
|
|
110
|
-
features: ctx.features,
|
|
126
|
+
features: ctx.base.features,
|
|
111
127
|
scopeId: options.scopeId,
|
|
112
|
-
markedRefs: ctx.marked,
|
|
128
|
+
markedRefs: ctx.base.marked,
|
|
113
129
|
});
|
|
114
130
|
|
|
115
131
|
let serialized: string;
|
|
116
132
|
|
|
117
133
|
try {
|
|
118
|
-
serialized = serial
|
|
134
|
+
serialized = serializeTopCross(serial, node);
|
|
119
135
|
} catch (err) {
|
|
120
136
|
if (options.onError) {
|
|
121
137
|
options.onError(err);
|
|
@@ -129,19 +145,19 @@ export function crossSerializeStream<T>(
|
|
|
129
145
|
onDone: options.onDone,
|
|
130
146
|
});
|
|
131
147
|
|
|
132
|
-
ctx
|
|
148
|
+
startStreamParse(ctx, source);
|
|
133
149
|
|
|
134
|
-
return
|
|
150
|
+
return destroyStreamParse.bind(null, ctx);
|
|
135
151
|
}
|
|
136
152
|
|
|
137
|
-
export type ToCrossJSONStreamOptions =
|
|
153
|
+
export type ToCrossJSONStreamOptions = StreamParserContextOptions;
|
|
138
154
|
|
|
139
155
|
export function toCrossJSONStream<T>(
|
|
140
156
|
source: T,
|
|
141
157
|
options: ToCrossJSONStreamOptions,
|
|
142
158
|
): () => void {
|
|
143
159
|
const plugins = resolvePlugins(options.plugins);
|
|
144
|
-
const ctx =
|
|
160
|
+
const ctx = createStreamParserContext({
|
|
145
161
|
plugins,
|
|
146
162
|
refs: options.refs,
|
|
147
163
|
disabledFeatures: options.disabledFeatures,
|
|
@@ -150,9 +166,9 @@ export function toCrossJSONStream<T>(
|
|
|
150
166
|
onDone: options.onDone,
|
|
151
167
|
});
|
|
152
168
|
|
|
153
|
-
ctx
|
|
169
|
+
startStreamParse(ctx, source);
|
|
154
170
|
|
|
155
|
-
return
|
|
171
|
+
return destroyStreamParse.bind(null, ctx);
|
|
156
172
|
}
|
|
157
173
|
|
|
158
174
|
export type FromCrossJSONOptions = CrossDeserializerContextOptions;
|
|
@@ -162,9 +178,9 @@ export function fromCrossJSON<T>(
|
|
|
162
178
|
options: FromCrossJSONOptions,
|
|
163
179
|
): T {
|
|
164
180
|
const plugins = resolvePlugins(options.plugins);
|
|
165
|
-
const ctx =
|
|
181
|
+
const ctx = createCrossDeserializerContext({
|
|
166
182
|
plugins,
|
|
167
183
|
refs: options.refs,
|
|
168
184
|
});
|
|
169
|
-
return
|
|
185
|
+
return deserializeTop(ctx, source) as T;
|
|
170
186
|
}
|
package/src/core/errors.ts
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
|
+
/// <reference types="pridepack/env" />
|
|
2
|
+
|
|
1
3
|
import { serializeString } from './string';
|
|
2
4
|
import type { SerovalNode } from './types';
|
|
3
5
|
|
|
4
6
|
const { toString: objectToString } = /* @__PURE__ */ Object.prototype;
|
|
5
7
|
|
|
6
|
-
|
|
8
|
+
const enum StepErrorCodes {
|
|
9
|
+
Parse = 1,
|
|
10
|
+
Serialize = 2,
|
|
11
|
+
Deserialize = 3,
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function getErrorMessageDev(type: string, cause: any): string {
|
|
7
15
|
if (cause instanceof Error) {
|
|
8
16
|
return `Seroval caught an error during the ${type} process.
|
|
9
17
|
|
|
@@ -20,6 +28,21 @@ ${cause.message}
|
|
|
20
28
|
For more information, please check the "cause" property of this error.`;
|
|
21
29
|
}
|
|
22
30
|
|
|
31
|
+
const STEP_ERROR_CODES: Record<string, StepErrorCodes> = {
|
|
32
|
+
parsing: StepErrorCodes.Parse,
|
|
33
|
+
serialization: StepErrorCodes.Serialize,
|
|
34
|
+
deserialization: StepErrorCodes.Deserialize,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
function getErrorMessageProd(type: string): string {
|
|
38
|
+
return `Seroval Error (step: ${STEP_ERROR_CODES[type]})`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const getErrorMessage = (type: string, cause: any) =>
|
|
42
|
+
import.meta.env.PROD
|
|
43
|
+
? getErrorMessageProd(type)
|
|
44
|
+
: getErrorMessageDev(type, cause);
|
|
45
|
+
|
|
23
46
|
export class SerovalError extends Error {
|
|
24
47
|
constructor(
|
|
25
48
|
type: string,
|
|
@@ -47,10 +70,26 @@ export class SerovalDeserializationError extends SerovalError {
|
|
|
47
70
|
}
|
|
48
71
|
}
|
|
49
72
|
|
|
73
|
+
const enum SpecificErrorCodes {
|
|
74
|
+
UnsupportedType = 1,
|
|
75
|
+
UnsupportedNode = 2,
|
|
76
|
+
MissingPlugin = 3,
|
|
77
|
+
MissingInstance = 4,
|
|
78
|
+
MissingReference = 5,
|
|
79
|
+
MissingReferenceForId = 6,
|
|
80
|
+
UnknownTypedArray = 7,
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function getSpecificErrorMessage(code: SpecificErrorCodes): string {
|
|
84
|
+
return `Seroval Error (specific: ${code})`;
|
|
85
|
+
}
|
|
86
|
+
|
|
50
87
|
export class SerovalUnsupportedTypeError extends Error {
|
|
51
88
|
constructor(public value: unknown) {
|
|
52
89
|
super(
|
|
53
|
-
|
|
90
|
+
import.meta.env.PROD
|
|
91
|
+
? getSpecificErrorMessage(SpecificErrorCodes.UnsupportedType)
|
|
92
|
+
: `The value ${objectToString.call(value)} of type "${typeof value}" cannot be parsed/serialized.
|
|
54
93
|
|
|
55
94
|
There are few workarounds for this problem:
|
|
56
95
|
- Transform the value in a way that it can be serialized.
|
|
@@ -61,42 +100,64 @@ There are few workarounds for this problem:
|
|
|
61
100
|
|
|
62
101
|
export class SerovalUnsupportedNodeError extends Error {
|
|
63
102
|
constructor(node: SerovalNode) {
|
|
64
|
-
super(
|
|
103
|
+
super(
|
|
104
|
+
import.meta.env.PROD
|
|
105
|
+
? getSpecificErrorMessage(SpecificErrorCodes.UnsupportedNode)
|
|
106
|
+
: 'Unsupported node type "' + node.t + '".',
|
|
107
|
+
);
|
|
65
108
|
}
|
|
66
109
|
}
|
|
67
110
|
|
|
68
111
|
export class SerovalMissingPluginError extends Error {
|
|
69
112
|
constructor(tag: string) {
|
|
70
|
-
super(
|
|
113
|
+
super(
|
|
114
|
+
import.meta.env.PROD
|
|
115
|
+
? getSpecificErrorMessage(SpecificErrorCodes.MissingPlugin)
|
|
116
|
+
: 'Missing plugin for tag "' + tag + '".',
|
|
117
|
+
);
|
|
71
118
|
}
|
|
72
119
|
}
|
|
73
120
|
|
|
74
121
|
export class SerovalMissingInstanceError extends Error {
|
|
75
122
|
constructor(tag: string) {
|
|
76
|
-
super(
|
|
123
|
+
super(
|
|
124
|
+
import.meta.env.PROD
|
|
125
|
+
? getSpecificErrorMessage(SpecificErrorCodes.MissingInstance)
|
|
126
|
+
: 'Missing "' + tag + '" instance.',
|
|
127
|
+
);
|
|
77
128
|
}
|
|
78
129
|
}
|
|
79
130
|
|
|
80
131
|
export class SerovalMissingReferenceError extends Error {
|
|
81
132
|
constructor(public value: unknown) {
|
|
82
133
|
super(
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
'
|
|
86
|
-
|
|
87
|
-
|
|
134
|
+
import.meta.env.PROD
|
|
135
|
+
? getSpecificErrorMessage(SpecificErrorCodes.MissingReference)
|
|
136
|
+
: 'Missing reference for the value "' +
|
|
137
|
+
objectToString.call(value) +
|
|
138
|
+
'" of type "' +
|
|
139
|
+
typeof value +
|
|
140
|
+
'"',
|
|
88
141
|
);
|
|
89
142
|
}
|
|
90
143
|
}
|
|
91
144
|
|
|
92
145
|
export class SerovalMissingReferenceForIdError extends Error {
|
|
93
146
|
constructor(id: string) {
|
|
94
|
-
super(
|
|
147
|
+
super(
|
|
148
|
+
import.meta.env.PROD
|
|
149
|
+
? getSpecificErrorMessage(SpecificErrorCodes.MissingReferenceForId)
|
|
150
|
+
: 'Missing reference for id "' + serializeString(id) + '"',
|
|
151
|
+
);
|
|
95
152
|
}
|
|
96
153
|
}
|
|
97
154
|
|
|
98
155
|
export class SerovalUnknownTypedArrayError extends Error {
|
|
99
156
|
constructor(name: string) {
|
|
100
|
-
super(
|
|
157
|
+
super(
|
|
158
|
+
import.meta.env.PROD
|
|
159
|
+
? getSpecificErrorMessage(SpecificErrorCodes.UnknownTypedArray)
|
|
160
|
+
: 'Unknown TypedArray "' + name + '"',
|
|
161
|
+
);
|
|
101
162
|
}
|
|
102
163
|
}
|
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import { Feature } from './compat';
|
|
2
1
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
2
|
+
declare const T: unknown;
|
|
3
|
+
|
|
4
|
+
const RETURN = () => T;
|
|
5
|
+
const SERIALIZED_RETURN = /* @__PURE__ */ RETURN.toString();
|
|
6
|
+
|
|
7
|
+
const IS_MODERN = /* @__PURE__ */ /=>/.test(SERIALIZED_RETURN);
|
|
8
|
+
|
|
9
|
+
export function createFunction(parameters: string[], body: string): string {
|
|
10
|
+
if (IS_MODERN) {
|
|
9
11
|
const joined =
|
|
10
12
|
parameters.length === 1
|
|
11
13
|
? parameters[0]
|
|
@@ -16,11 +18,10 @@ export function createFunction(
|
|
|
16
18
|
}
|
|
17
19
|
|
|
18
20
|
export function createEffectfulFunction(
|
|
19
|
-
features: number,
|
|
20
21
|
parameters: string[],
|
|
21
22
|
body: string,
|
|
22
23
|
): string {
|
|
23
|
-
if (
|
|
24
|
+
if (IS_MODERN) {
|
|
24
25
|
const joined =
|
|
25
26
|
parameters.length === 1
|
|
26
27
|
? parameters[0]
|
|
@@ -28,4 +29,4 @@ export function createEffectfulFunction(
|
|
|
28
29
|
return joined + '=>{' + body + '}';
|
|
29
30
|
}
|
|
30
31
|
return 'function(' + parameters.join(',') + '){' + body + '}';
|
|
31
|
-
}
|
|
32
|
+
}
|
package/src/core/plugin.ts
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import type
|
|
3
|
-
import type
|
|
4
|
-
import type
|
|
5
|
-
|
|
1
|
+
import type { AsyncParsePluginContext } from './context/async-parser';
|
|
2
|
+
import type { DeserializePluginContext } from './context/deserializer';
|
|
3
|
+
import type { SerializePluginContext } from './context/serializer';
|
|
4
|
+
import type {
|
|
5
|
+
StreamParsePluginContext,
|
|
6
|
+
SyncParsePluginContext,
|
|
7
|
+
} from './context/sync-parser';
|
|
6
8
|
|
|
7
|
-
export
|
|
9
|
+
export const enum SerovalMode {
|
|
10
|
+
Vanilla = 1,
|
|
11
|
+
Cross = 2,
|
|
12
|
+
}
|
|
8
13
|
|
|
9
14
|
export interface PluginData {
|
|
10
15
|
id: number;
|
|
@@ -28,28 +33,32 @@ export interface Plugin<Value, Node> {
|
|
|
28
33
|
* Parsing modes
|
|
29
34
|
*/
|
|
30
35
|
parse: {
|
|
31
|
-
sync?: (
|
|
36
|
+
sync?: (
|
|
37
|
+
value: Value,
|
|
38
|
+
ctx: SyncParsePluginContext,
|
|
39
|
+
data: PluginData,
|
|
40
|
+
) => Node;
|
|
32
41
|
async?: (
|
|
33
42
|
value: Value,
|
|
34
|
-
ctx:
|
|
43
|
+
ctx: AsyncParsePluginContext,
|
|
35
44
|
data: PluginData,
|
|
36
45
|
) => Promise<Node>;
|
|
37
46
|
stream?: (
|
|
38
47
|
value: Value,
|
|
39
|
-
ctx:
|
|
48
|
+
ctx: StreamParsePluginContext,
|
|
40
49
|
data: PluginData,
|
|
41
50
|
) => Node;
|
|
42
51
|
};
|
|
43
52
|
/**
|
|
44
53
|
* Convert the parsed node into a JS string
|
|
45
54
|
*/
|
|
46
|
-
serialize(node: Node, ctx:
|
|
55
|
+
serialize(node: Node, ctx: SerializePluginContext, data: PluginData): string;
|
|
47
56
|
/**
|
|
48
57
|
* Convert the parsed node into its runtime equivalent.
|
|
49
58
|
*/
|
|
50
59
|
deserialize(
|
|
51
60
|
node: Node,
|
|
52
|
-
ctx:
|
|
61
|
+
ctx: DeserializePluginContext,
|
|
53
62
|
data: PluginData,
|
|
54
63
|
): Value;
|
|
55
64
|
}
|
package/src/core/reference.ts
CHANGED
|
@@ -3,7 +3,6 @@ import {
|
|
|
3
3
|
SerovalMissingReferenceForIdError,
|
|
4
4
|
} from '..';
|
|
5
5
|
import { REFERENCES_KEY } from './keys';
|
|
6
|
-
import assert from './utils/assert';
|
|
7
6
|
|
|
8
7
|
const REFERENCE = new Map<unknown, string>();
|
|
9
8
|
const INV_REFERENCE = new Map<string, unknown>();
|
|
@@ -23,13 +22,17 @@ export function hasReference(id: string): boolean {
|
|
|
23
22
|
}
|
|
24
23
|
|
|
25
24
|
export function getReferenceID<T>(value: T): string {
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
if (hasReferenceID(value)) {
|
|
26
|
+
return REFERENCE.get(value)!;
|
|
27
|
+
}
|
|
28
|
+
throw new SerovalMissingReferenceError(value);
|
|
28
29
|
}
|
|
29
30
|
|
|
30
31
|
export function getReference<T>(id: string): T {
|
|
31
|
-
|
|
32
|
-
|
|
32
|
+
if (hasReference(id)) {
|
|
33
|
+
return INV_REFERENCE.get(id) as T;
|
|
34
|
+
}
|
|
35
|
+
throw new SerovalMissingReferenceForIdError(id);
|
|
33
36
|
}
|
|
34
37
|
|
|
35
38
|
if (typeof globalThis !== 'undefined') {
|
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import {
|
|
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';
|
|
2
8
|
|
|
3
9
|
export const ITERATOR = {};
|
|
4
10
|
|
|
@@ -10,6 +16,7 @@ export const enum SpecialReference {
|
|
|
10
16
|
PromiseSuccess = 2,
|
|
11
17
|
PromiseFailure = 3,
|
|
12
18
|
StreamConstructor = 4,
|
|
19
|
+
ArrayBufferConstructor = 5,
|
|
13
20
|
}
|
|
14
21
|
|
|
15
22
|
/**
|
|
@@ -21,94 +28,15 @@ export const SPECIAL_REFS: Record<SpecialReference, unknown> = {
|
|
|
21
28
|
[SpecialReference.PromiseSuccess]: {},
|
|
22
29
|
[SpecialReference.PromiseFailure]: {},
|
|
23
30
|
[SpecialReference.StreamConstructor]: {},
|
|
31
|
+
[SpecialReference.ArrayBufferConstructor]: {},
|
|
24
32
|
};
|
|
25
33
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
}
|
|
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
|
+
};
|