seroval 0.4.0-alpha.9 → 0.4.1
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 +81 -55
- package/dist/cjs/development/index.cjs +894 -934
- package/dist/cjs/development/index.cjs.map +4 -4
- package/dist/cjs/production/index.cjs +2 -2
- package/dist/esm/development/index.mjs +894 -934
- package/dist/esm/development/index.mjs.map +4 -4
- package/dist/esm/production/index.mjs +2 -2
- package/dist/types/compat.d.ts +10 -25
- package/dist/types/context.d.ts +17 -15
- package/dist/types/index.d.ts +14 -5
- package/dist/types/tree/primitives.d.ts +17 -0
- package/dist/types/tree/shared.d.ts +4 -8
- package/dist/types/types.d.ts +3 -2
- package/package.json +2 -2
- package/src/compat.ts +10 -246
- package/src/context.ts +37 -26
- package/src/index.ts +37 -46
- package/src/quote.ts +1 -1
- package/src/tree/async.ts +319 -197
- package/src/tree/primitives.ts +150 -0
- package/src/tree/serialize.ts +266 -235
- package/src/tree/shared.ts +43 -175
- package/src/tree/sync.ts +255 -180
- package/src/types.ts +4 -1
- package/dist/types/checks.d.ts +0 -5
- package/src/checks.ts +0 -40
package/src/index.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
/* eslint-disable no-await-in-loop */
|
|
2
|
-
import { isPrimitive } from './checks';
|
|
3
2
|
import { Feature } from './compat';
|
|
4
3
|
import {
|
|
5
4
|
SerializationContext,
|
|
@@ -10,7 +9,6 @@ import {
|
|
|
10
9
|
} from './context';
|
|
11
10
|
import parseAsync from './tree/async';
|
|
12
11
|
import serializeTree, { resolvePatches } from './tree/serialize';
|
|
13
|
-
import { serializePrimitive } from './tree/shared';
|
|
14
12
|
import parseSync from './tree/sync';
|
|
15
13
|
import { SerovalNode } from './tree/types';
|
|
16
14
|
import {
|
|
@@ -29,6 +27,7 @@ export {
|
|
|
29
27
|
CommonServerValue,
|
|
30
28
|
SemiPrimitiveValue,
|
|
31
29
|
ErrorValue,
|
|
30
|
+
Feature,
|
|
32
31
|
};
|
|
33
32
|
|
|
34
33
|
function finalize(
|
|
@@ -44,10 +43,9 @@ function finalize(
|
|
|
44
43
|
if (patches) {
|
|
45
44
|
// Get (or create) a ref from the source
|
|
46
45
|
const index = getRefParam(ctx, rootID);
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
body = `${index}=${result},${patches}${index}`;
|
|
46
|
+
body = result + ',' + patches + index;
|
|
47
|
+
if (!result.startsWith(index + '=')) {
|
|
48
|
+
body = index + '=' + body;
|
|
51
49
|
}
|
|
52
50
|
}
|
|
53
51
|
let params = ctx.vars.length > 1
|
|
@@ -56,14 +54,14 @@ function finalize(
|
|
|
56
54
|
// Source is probably already assigned
|
|
57
55
|
if (ctx.features & Feature.ArrowFunction) {
|
|
58
56
|
params = ctx.vars.length > 1 || ctx.vars.length === 0
|
|
59
|
-
?
|
|
57
|
+
? '(' + params + ')'
|
|
60
58
|
: params;
|
|
61
|
-
return
|
|
59
|
+
return '(' + params + '=>(' + body + '))()';
|
|
62
60
|
}
|
|
63
|
-
return
|
|
61
|
+
return '(function(' + params + '){return ' + body + '})()';
|
|
64
62
|
}
|
|
65
63
|
if (isObject) {
|
|
66
|
-
return
|
|
64
|
+
return '(' + result + ')';
|
|
67
65
|
}
|
|
68
66
|
return result;
|
|
69
67
|
}
|
|
@@ -73,9 +71,6 @@ export function serialize<T extends ServerValue>(
|
|
|
73
71
|
options?: Partial<Options>,
|
|
74
72
|
) {
|
|
75
73
|
const ctx = createParserContext(options);
|
|
76
|
-
if (isPrimitive(source)) {
|
|
77
|
-
return String(serializePrimitive(source));
|
|
78
|
-
}
|
|
79
74
|
const [tree, rootID, isObject] = parseSync(ctx, source);
|
|
80
75
|
const serial = createSerializationContext(ctx);
|
|
81
76
|
const result = serializeTree(serial, tree);
|
|
@@ -87,9 +82,6 @@ export async function serializeAsync<T extends AsyncServerValue>(
|
|
|
87
82
|
options?: Partial<Options>,
|
|
88
83
|
) {
|
|
89
84
|
const ctx = createParserContext(options);
|
|
90
|
-
if (isPrimitive(source)) {
|
|
91
|
-
return String(serializePrimitive(source));
|
|
92
|
-
}
|
|
93
85
|
const [tree, rootID, isObject] = await parseAsync(ctx, source);
|
|
94
86
|
const serial = createSerializationContext(ctx);
|
|
95
87
|
const result = serializeTree(serial, tree);
|
|
@@ -101,55 +93,54 @@ export function deserialize<T extends AsyncServerValue>(source: string): T {
|
|
|
101
93
|
return (0, eval)(source) as T;
|
|
102
94
|
}
|
|
103
95
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
96
|
+
export interface SerovalJSON {
|
|
97
|
+
t: SerovalNode,
|
|
98
|
+
r: number,
|
|
99
|
+
i: boolean,
|
|
100
|
+
f: number,
|
|
101
|
+
m: number[],
|
|
102
|
+
}
|
|
111
103
|
|
|
112
104
|
export function toJSON<T extends ServerValue>(
|
|
113
105
|
source: T,
|
|
114
106
|
options?: Partial<Options>,
|
|
115
|
-
) {
|
|
107
|
+
): SerovalJSON {
|
|
116
108
|
const ctx = createParserContext(options);
|
|
117
109
|
const [tree, root, isObject] = parseSync(ctx, source);
|
|
118
|
-
return
|
|
119
|
-
tree,
|
|
120
|
-
root,
|
|
121
|
-
isObject,
|
|
122
|
-
ctx.features,
|
|
123
|
-
ctx.markedRefs,
|
|
124
|
-
|
|
110
|
+
return {
|
|
111
|
+
t: tree,
|
|
112
|
+
r: root,
|
|
113
|
+
i: isObject,
|
|
114
|
+
f: ctx.features,
|
|
115
|
+
m: Array.from(ctx.markedRefs),
|
|
116
|
+
};
|
|
125
117
|
}
|
|
126
118
|
|
|
127
119
|
export async function toJSONAsync<T extends AsyncServerValue>(
|
|
128
120
|
source: T,
|
|
129
121
|
options?: Partial<Options>,
|
|
130
|
-
) {
|
|
122
|
+
): Promise<SerovalJSON> {
|
|
131
123
|
const ctx = createParserContext(options);
|
|
132
124
|
const [tree, root, isObject] = await parseAsync(ctx, source);
|
|
133
|
-
return
|
|
134
|
-
tree,
|
|
135
|
-
root,
|
|
136
|
-
isObject,
|
|
137
|
-
ctx.features,
|
|
138
|
-
ctx.markedRefs,
|
|
139
|
-
|
|
125
|
+
return {
|
|
126
|
+
t: tree,
|
|
127
|
+
r: root,
|
|
128
|
+
i: isObject,
|
|
129
|
+
f: ctx.features,
|
|
130
|
+
m: Array.from(ctx.markedRefs),
|
|
131
|
+
};
|
|
140
132
|
}
|
|
141
133
|
|
|
142
|
-
export function compileJSON(source:
|
|
143
|
-
const parsed = JSON.parse(source) as SerovalJSON;
|
|
134
|
+
export function compileJSON(source: SerovalJSON): string {
|
|
144
135
|
const serial = createSerializationContext({
|
|
145
|
-
features:
|
|
146
|
-
markedRefs:
|
|
136
|
+
features: source.f,
|
|
137
|
+
markedRefs: source.m,
|
|
147
138
|
});
|
|
148
|
-
const result = serializeTree(serial,
|
|
149
|
-
return finalize(serial,
|
|
139
|
+
const result = serializeTree(serial, source.t);
|
|
140
|
+
return finalize(serial, source.r, source.i, result);
|
|
150
141
|
}
|
|
151
142
|
|
|
152
|
-
export function fromJSON<T extends AsyncServerValue>(source:
|
|
143
|
+
export function fromJSON<T extends AsyncServerValue>(source: SerovalJSON): T {
|
|
153
144
|
return deserialize<T>(compileJSON(source));
|
|
154
145
|
}
|
|
155
146
|
|
package/src/quote.ts
CHANGED