eliscript 0.0.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/LICENSE +674 -0
- package/README.md +135 -0
- package/bin/eliscript +16 -0
- package/bin/eliscript-bootstrap +15 -0
- package/bin/eliscript-build +15 -0
- package/bin/eliscript-check +15 -0
- package/bin/eliscript-eval +15 -0
- package/bin/eliscript-format +15 -0
- package/bin/eliscript-org +10 -0
- package/bin/eliscript-portable +17 -0
- package/bin/eliscript-seed +10 -0
- package/bin/eliscript-seed-build +10 -0
- package/bin/eliscript-watch +15 -0
- package/bootstrap/README.md +100 -0
- package/bootstrap/compiler/analyzer.eli +1426 -0
- package/bootstrap/compiler/compiler.eli +472 -0
- package/bootstrap/compiler/emitter.eli +1745 -0
- package/bootstrap/compiler/expander.eli +2522 -0
- package/bootstrap/compiler/formatter.eli +359 -0
- package/bootstrap/compiler/ir.eli +297 -0
- package/bootstrap/compiler/lower.eli +661 -0
- package/bootstrap/compiler/project.eli +1120 -0
- package/bootstrap/compiler/reader.eli +321 -0
- package/bootstrap/compiler/source-map.eli +165 -0
- package/bootstrap/compiler/symbol.eli +92 -0
- package/bootstrap/compiler/syntax.eli +54 -0
- package/bootstrap/compiler/transient-analysis.eli +825 -0
- package/bootstrap/host/build.mjs +28 -0
- package/bootstrap/host/bun.mjs +282 -0
- package/bootstrap/host/check-cli.mjs +163 -0
- package/bootstrap/host/eliscript-cli.mjs +36 -0
- package/bootstrap/host/evaluation-cli.mjs +401 -0
- package/bootstrap/host/evaluation.mjs +560 -0
- package/bootstrap/host/format-cli.mjs +139 -0
- package/bootstrap/host/project-cli.mjs +295 -0
- package/bootstrap/host/project.mjs +1080 -0
- package/bootstrap/host/watch-cli.mjs +129 -0
- package/bootstrap/host/watch.mjs +267 -0
- package/browser/host.mjs +357 -0
- package/browser/worker.mjs +60 -0
- package/compiler/README.md +118 -0
- package/compiler/eliscript-analyzer.el +716 -0
- package/compiler/eliscript-cli.el +108 -0
- package/compiler/eliscript-diagnostic.el +158 -0
- package/compiler/eliscript-emitter.el +1351 -0
- package/compiler/eliscript-evaluation.el +259 -0
- package/compiler/eliscript-expander.el +1630 -0
- package/compiler/eliscript-form.el +79 -0
- package/compiler/eliscript-ir-emitter.el +1460 -0
- package/compiler/eliscript-ir.el +356 -0
- package/compiler/eliscript-lower.el +463 -0
- package/compiler/eliscript-macro-eval.el +811 -0
- package/compiler/eliscript-parameters.el +324 -0
- package/compiler/eliscript-portable.el +492 -0
- package/compiler/eliscript-project-cli.el +156 -0
- package/compiler/eliscript-project.el +1676 -0
- package/compiler/eliscript-reader.el +421 -0
- package/compiler/eliscript-source-map.el +192 -0
- package/compiler/eliscript-symbol.el +101 -0
- package/compiler/eliscript-transient-analysis.el +817 -0
- package/compiler/eliscript.el +239 -0
- package/dist/bootstrap/analyzer.mjs +968 -0
- package/dist/bootstrap/analyzer.mjs.map +1 -0
- package/dist/bootstrap/compiler.mjs +228 -0
- package/dist/bootstrap/compiler.mjs.map +1 -0
- package/dist/bootstrap/emitter.mjs +1173 -0
- package/dist/bootstrap/emitter.mjs.map +1 -0
- package/dist/bootstrap/expander.mjs +1469 -0
- package/dist/bootstrap/expander.mjs.map +1 -0
- package/dist/bootstrap/formatter.mjs +304 -0
- package/dist/bootstrap/formatter.mjs.map +1 -0
- package/dist/bootstrap/ir.mjs +193 -0
- package/dist/bootstrap/ir.mjs.map +1 -0
- package/dist/bootstrap/lower.mjs +344 -0
- package/dist/bootstrap/lower.mjs.map +1 -0
- package/dist/bootstrap/project.mjs +585 -0
- package/dist/bootstrap/project.mjs.map +1 -0
- package/dist/bootstrap/reader.mjs +259 -0
- package/dist/bootstrap/reader.mjs.map +1 -0
- package/dist/bootstrap/source-map.mjs +154 -0
- package/dist/bootstrap/source-map.mjs.map +1 -0
- package/dist/bootstrap/symbol.mjs +74 -0
- package/dist/bootstrap/symbol.mjs.map +1 -0
- package/dist/bootstrap/syntax.mjs +38 -0
- package/dist/bootstrap/syntax.mjs.map +1 -0
- package/dist/bootstrap/transient-analysis.mjs +660 -0
- package/dist/bootstrap/transient-analysis.mjs.map +1 -0
- package/dist/browser/compiler.js +6491 -0
- package/editor/README.md +83 -0
- package/editor/eliscript-mode.el +878 -0
- package/editor/eliscript-repl.el +501 -0
- package/package.json +163 -0
- package/platform/README.md +48 -0
- package/platform/browser.mjs +204 -0
- package/platform/worker.mjs +166 -0
- package/runtime/README.md +296 -0
- package/runtime/core/collection-internals.mjs +512 -0
- package/runtime/core/collection.mjs +645 -0
- package/runtime/core/data-text.mjs +532 -0
- package/runtime/core/data.mjs +268 -0
- package/runtime/core/identifier.mjs +314 -0
- package/runtime/core/lazy-sequence.mjs +244 -0
- package/runtime/core/list-internals.mjs +18 -0
- package/runtime/core/list.mjs +273 -0
- package/runtime/core/map-internals.mjs +793 -0
- package/runtime/core/map.mjs +394 -0
- package/runtime/core/metadata-internals.mjs +14 -0
- package/runtime/core/metadata.mjs +44 -0
- package/runtime/core/object-impl.mjs +65 -0
- package/runtime/core/object-impl.mjs.map +1 -0
- package/runtime/core/object.mjs +13 -0
- package/runtime/core/order.mjs +234 -0
- package/runtime/core/persistent-kind.mjs +43 -0
- package/runtime/core/protocol-error.mjs +36 -0
- package/runtime/core/protocol-impl.mjs +326 -0
- package/runtime/core/protocol-impl.mjs.map +1 -0
- package/runtime/core/protocol.mjs +14 -0
- package/runtime/core/queue-internals.mjs +27 -0
- package/runtime/core/queue.mjs +238 -0
- package/runtime/core/record.mjs +346 -0
- package/runtime/core/regex.mjs +184 -0
- package/runtime/core/sequence.mjs +632 -0
- package/runtime/core/set-algebra.mjs +287 -0
- package/runtime/core/set-internals.mjs +33 -0
- package/runtime/core/set.mjs +378 -0
- package/runtime/core/sorted-internals.mjs +215 -0
- package/runtime/core/sorted.mjs +598 -0
- package/runtime/core/text-impl.mjs +95 -0
- package/runtime/core/text-impl.mjs.map +1 -0
- package/runtime/core/text.mjs +15 -0
- package/runtime/core/transducer.mjs +668 -0
- package/runtime/core/transient-internals.mjs +38 -0
- package/runtime/core/transient.mjs +53 -0
- package/runtime/core/truth.mjs +3 -0
- package/runtime/core/type.mjs +262 -0
- package/runtime/core/value-internals.mjs +188 -0
- package/runtime/core/value.mjs +93 -0
- package/runtime/core/vector-internals.mjs +102 -0
- package/runtime/core/vector.mjs +951 -0
- package/runtime/literals.mjs +34 -0
- package/runtime/source-mapping.mjs +220 -0
- package/runtime/testing/list.mjs +52 -0
- package/runtime/testing/map.mjs +145 -0
- package/runtime/testing/queue.mjs +49 -0
- package/runtime/testing/set.mjs +61 -0
- package/runtime/testing/sorted.mjs +105 -0
- package/runtime/testing/value.mjs +17 -0
- package/runtime/testing/vector.mjs +102 -0
- package/runtime/worker-value-codec.mjs +565 -0
- package/runtime/worker-value-stream.mjs +786 -0
- package/runtime/worker.mjs +975 -0
- package/stdlib/README.md +774 -0
- package/stdlib/bit.eli +36 -0
- package/stdlib/core/collection.eli +134 -0
- package/stdlib/core/data-text.eli +16 -0
- package/stdlib/core/data.eli +306 -0
- package/stdlib/core/identifier.eli +32 -0
- package/stdlib/core/lazy-sequence.eli +63 -0
- package/stdlib/core/metadata.eli +20 -0
- package/stdlib/core/object.eli +89 -0
- package/stdlib/core/order.eli +133 -0
- package/stdlib/core/protocol.eli +387 -0
- package/stdlib/core/record.eli +16 -0
- package/stdlib/core/regex.eli +17 -0
- package/stdlib/core/seq.eli +701 -0
- package/stdlib/core/set.eli +312 -0
- package/stdlib/core/sorted.eli +53 -0
- package/stdlib/core/text.eli +103 -0
- package/stdlib/core/transducer.eli +59 -0
- package/stdlib/core/transient.eli +24 -0
- package/stdlib/core/type.eli +19 -0
- package/stdlib/core/walk.eli +195 -0
- package/stdlib/core/zipper.eli +508 -0
- package/stdlib/data-text.eli +1092 -0
- package/stdlib/data.eli +42 -0
- package/stdlib/deferred.eli +70 -0
- package/stdlib/function.eli +120 -0
- package/stdlib/hierarchy.eli +245 -0
- package/stdlib/identifier.eli +147 -0
- package/stdlib/interop/js.eli +616 -0
- package/stdlib/json.eli +870 -0
- package/stdlib/metadata.eli +56 -0
- package/stdlib/multimethod.eli +464 -0
- package/stdlib/numeric.eli +193 -0
- package/stdlib/object.eli +96 -0
- package/stdlib/persistent-list.eli +145 -0
- package/stdlib/persistent-map.eli +724 -0
- package/stdlib/persistent-queue.eli +32 -0
- package/stdlib/persistent-set.eli +236 -0
- package/stdlib/persistent-vector.eli +342 -0
- package/stdlib/result.eli +141 -0
- package/stdlib/sequence.eli +106 -0
- package/stdlib/state/atom.eli +265 -0
- package/stdlib/text.eli +88 -0
- package/stdlib/value.eli +325 -0
|
@@ -0,0 +1,786 @@
|
|
|
1
|
+
import { setImmediate as yieldToEventLoop } from "node:timers/promises";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
eliscriptSymbol,
|
|
5
|
+
isEliscriptSymbol,
|
|
6
|
+
isKeyword,
|
|
7
|
+
keyword,
|
|
8
|
+
} from "./core/identifier.mjs";
|
|
9
|
+
import { EMPTY_LIST, isPersistentList } from "./core/list.mjs";
|
|
10
|
+
import {
|
|
11
|
+
isPersistentHashMap,
|
|
12
|
+
persistentHashMap,
|
|
13
|
+
} from "./core/map.mjs";
|
|
14
|
+
import { meta, withMeta } from "./core/metadata.mjs";
|
|
15
|
+
import {
|
|
16
|
+
isPersistentHashSet,
|
|
17
|
+
persistentHashSet,
|
|
18
|
+
} from "./core/set.mjs";
|
|
19
|
+
import {
|
|
20
|
+
assocBang,
|
|
21
|
+
conjBang,
|
|
22
|
+
persistentBang,
|
|
23
|
+
transient,
|
|
24
|
+
} from "./core/transient.mjs";
|
|
25
|
+
import {
|
|
26
|
+
isPersistentVector,
|
|
27
|
+
persistentVector,
|
|
28
|
+
} from "./core/vector.mjs";
|
|
29
|
+
|
|
30
|
+
export const workerValueFraming = "eliscript-value-chunks-v1";
|
|
31
|
+
|
|
32
|
+
export const workerValueStreamLimits = Object.freeze({
|
|
33
|
+
maxDepth: 64,
|
|
34
|
+
maxNodes: 8_000_000,
|
|
35
|
+
maxCollectionLength: 4_000_000,
|
|
36
|
+
maxStringUnits: 268_435_456,
|
|
37
|
+
maxTotalStringUnits: 536_870_912,
|
|
38
|
+
maxChunkBytes: 256 * 1024,
|
|
39
|
+
maxEventsPerChunk: 512,
|
|
40
|
+
maxTextPartUnits: 8_192,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const SPECIAL_NUMBERS = new Map([
|
|
44
|
+
["nan", Number.NaN],
|
|
45
|
+
["positive-infinity", Number.POSITIVE_INFINITY],
|
|
46
|
+
["negative-infinity", Number.NEGATIVE_INFINITY],
|
|
47
|
+
["negative-zero", -0],
|
|
48
|
+
]);
|
|
49
|
+
|
|
50
|
+
export class WorkerValueStreamError extends TypeError {
|
|
51
|
+
constructor(code, message, path = "$") {
|
|
52
|
+
super(`${message} at ${path}`);
|
|
53
|
+
this.name = "WorkerValueStreamError";
|
|
54
|
+
this.code = code;
|
|
55
|
+
this.path = path;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function normalizedLimits(options = {}) {
|
|
60
|
+
const limits = { ...workerValueStreamLimits, ...options };
|
|
61
|
+
for (const name of Object.keys(workerValueStreamLimits)) {
|
|
62
|
+
if (!Number.isSafeInteger(limits[name]) || limits[name] < 1) {
|
|
63
|
+
throw new TypeError(`${name} must be a positive safe integer`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (limits.maxTextPartUnits < 2) {
|
|
67
|
+
throw new TypeError("maxTextPartUnits must be at least 2");
|
|
68
|
+
}
|
|
69
|
+
if (limits.maxTextPartUnits > limits.maxStringUnits) {
|
|
70
|
+
throw new TypeError("maxTextPartUnits cannot exceed maxStringUnits");
|
|
71
|
+
}
|
|
72
|
+
return limits;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function createState(mode, options) {
|
|
76
|
+
return {
|
|
77
|
+
active: new WeakSet(),
|
|
78
|
+
limits: normalizedLimits(options),
|
|
79
|
+
mode,
|
|
80
|
+
nodes: 0,
|
|
81
|
+
stringUnits: 0,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function streamError(state, suffix, message, path) {
|
|
86
|
+
throw new WorkerValueStreamError(
|
|
87
|
+
`value-stream-${state.mode}-${suffix}`,
|
|
88
|
+
message,
|
|
89
|
+
path,
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function checkAbort(signal, path) {
|
|
94
|
+
if (signal?.aborted) {
|
|
95
|
+
throw new WorkerValueStreamError(
|
|
96
|
+
"value-stream-cancelled",
|
|
97
|
+
"value stream traversal was cancelled",
|
|
98
|
+
path,
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function visit(state, depth, path) {
|
|
104
|
+
state.nodes += 1;
|
|
105
|
+
if (state.nodes > state.limits.maxNodes) {
|
|
106
|
+
streamError(
|
|
107
|
+
state,
|
|
108
|
+
"limit",
|
|
109
|
+
`value stream exceeds ${state.limits.maxNodes} nodes`,
|
|
110
|
+
path,
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
if (depth > state.limits.maxDepth) {
|
|
114
|
+
streamError(
|
|
115
|
+
state,
|
|
116
|
+
"limit",
|
|
117
|
+
`value stream exceeds depth ${state.limits.maxDepth}`,
|
|
118
|
+
path,
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function collectionLength(state, length, path) {
|
|
124
|
+
if (!Number.isSafeInteger(length) || length < 0) {
|
|
125
|
+
streamError(state, "invalid", "collection length must be a safe integer", path);
|
|
126
|
+
}
|
|
127
|
+
if (length > state.limits.maxCollectionLength) {
|
|
128
|
+
streamError(
|
|
129
|
+
state,
|
|
130
|
+
"limit",
|
|
131
|
+
`collection exceeds ${state.limits.maxCollectionLength} values`,
|
|
132
|
+
path,
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function stringLength(state, length, path) {
|
|
138
|
+
if (!Number.isSafeInteger(length) || length < 0) {
|
|
139
|
+
streamError(state, "invalid", "string length must be a safe integer", path);
|
|
140
|
+
}
|
|
141
|
+
if (length > state.limits.maxStringUnits) {
|
|
142
|
+
streamError(
|
|
143
|
+
state,
|
|
144
|
+
"limit",
|
|
145
|
+
`string exceeds ${state.limits.maxStringUnits} UTF-16 units`,
|
|
146
|
+
path,
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
state.stringUnits += length;
|
|
150
|
+
if (state.stringUnits > state.limits.maxTotalStringUnits) {
|
|
151
|
+
streamError(
|
|
152
|
+
state,
|
|
153
|
+
"limit",
|
|
154
|
+
`value stream exceeds ${state.limits.maxTotalStringUnits} string units`,
|
|
155
|
+
path,
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function activate(state, value, path) {
|
|
161
|
+
if (state.active.has(value)) {
|
|
162
|
+
streamError(state, "cycle", "cyclic values are not transportable", path);
|
|
163
|
+
}
|
|
164
|
+
state.active.add(value);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function textPartEnd(value, start, maximumUnits) {
|
|
168
|
+
let end = Math.min(value.length, start + maximumUnits);
|
|
169
|
+
if (end < value.length && end > start) {
|
|
170
|
+
const previous = value.charCodeAt(end - 1);
|
|
171
|
+
const next = value.charCodeAt(end);
|
|
172
|
+
if (previous >= 0xd800 && previous <= 0xdbff &&
|
|
173
|
+
next >= 0xdc00 && next <= 0xdfff) {
|
|
174
|
+
end -= 1;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return end === start ? Math.min(value.length, start + 1) : end;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async function* encodeText(value, state, path, signal) {
|
|
181
|
+
stringLength(state, value.length, path);
|
|
182
|
+
yield ["text", value.length];
|
|
183
|
+
for (let start = 0; start < value.length;) {
|
|
184
|
+
checkAbort(signal, path);
|
|
185
|
+
const end = textPartEnd(value, start, state.limits.maxTextPartUnits);
|
|
186
|
+
yield ["text-part", value.slice(start, end)];
|
|
187
|
+
start = end;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function plainObjectEntries(value, state, path) {
|
|
192
|
+
const prototype = Object.getPrototypeOf(value);
|
|
193
|
+
if (prototype !== Object.prototype && prototype !== null) {
|
|
194
|
+
streamError(
|
|
195
|
+
state,
|
|
196
|
+
"unsupported",
|
|
197
|
+
`unsupported object ${Object.prototype.toString.call(value)}`,
|
|
198
|
+
path,
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
const symbolKeys = Object.getOwnPropertySymbols(value).filter(
|
|
202
|
+
(key) => Object.prototype.propertyIsEnumerable.call(value, key),
|
|
203
|
+
);
|
|
204
|
+
if (symbolKeys.length > 0) {
|
|
205
|
+
streamError(
|
|
206
|
+
state,
|
|
207
|
+
"unsupported",
|
|
208
|
+
"host objects with enumerable Symbol keys are not transportable",
|
|
209
|
+
path,
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
const descriptors = Object.getOwnPropertyDescriptors(value);
|
|
213
|
+
const keys = Object.keys(descriptors).filter((key) => descriptors[key].enumerable);
|
|
214
|
+
collectionLength(state, keys.length, path);
|
|
215
|
+
return keys.map((key) => {
|
|
216
|
+
const descriptor = descriptors[key];
|
|
217
|
+
if (!("value" in descriptor)) {
|
|
218
|
+
streamError(
|
|
219
|
+
state,
|
|
220
|
+
"unsupported",
|
|
221
|
+
"host object accessors are not transportable",
|
|
222
|
+
`${path}.${key}`,
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
return [key, descriptor.value];
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
async function* encodeValue(value, state, depth, path, signal) {
|
|
230
|
+
checkAbort(signal, path);
|
|
231
|
+
visit(state, depth, path);
|
|
232
|
+
if (value === null || typeof value === "boolean") {
|
|
233
|
+
yield ["value", value];
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
if (typeof value === "string") {
|
|
237
|
+
yield* encodeText(value, state, path, signal);
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
if (value === undefined) {
|
|
241
|
+
yield ["undefined"];
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
if (typeof value === "number") {
|
|
245
|
+
if (Number.isNaN(value)) yield ["number", "nan"];
|
|
246
|
+
else if (value === Number.POSITIVE_INFINITY) {
|
|
247
|
+
yield ["number", "positive-infinity"];
|
|
248
|
+
} else if (value === Number.NEGATIVE_INFINITY) {
|
|
249
|
+
yield ["number", "negative-infinity"];
|
|
250
|
+
} else if (Object.is(value, -0)) yield ["number", "negative-zero"];
|
|
251
|
+
else yield ["value", value];
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
if (isKeyword(value)) {
|
|
255
|
+
yield ["open", "keyword", 2];
|
|
256
|
+
yield* encodeValue(value.namespace, state, depth + 1, `${path}.namespace`, signal);
|
|
257
|
+
yield* encodeValue(value.name, state, depth + 1, `${path}.name`, signal);
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
if (isEliscriptSymbol(value)) {
|
|
261
|
+
activate(state, value, path);
|
|
262
|
+
try {
|
|
263
|
+
yield ["open", "symbol", 3];
|
|
264
|
+
yield* encodeValue(value.namespace, state, depth + 1, `${path}.namespace`, signal);
|
|
265
|
+
yield* encodeValue(value.name, state, depth + 1, `${path}.name`, signal);
|
|
266
|
+
yield* encodeValue(meta(value), state, depth + 1, `${path}.metadata`, signal);
|
|
267
|
+
} finally {
|
|
268
|
+
state.active.delete(value);
|
|
269
|
+
}
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
if (isPersistentList(value) || isPersistentVector(value)) {
|
|
273
|
+
activate(state, value, path);
|
|
274
|
+
try {
|
|
275
|
+
collectionLength(state, value.count, path);
|
|
276
|
+
yield ["open", isPersistentList(value) ? "list" : "vector", value.count];
|
|
277
|
+
let index = 0;
|
|
278
|
+
for (const item of value) {
|
|
279
|
+
yield* encodeValue(item, state, depth + 1, `${path}[${index}]`, signal);
|
|
280
|
+
index += 1;
|
|
281
|
+
}
|
|
282
|
+
yield* encodeValue(meta(value), state, depth + 1, `${path}.metadata`, signal);
|
|
283
|
+
} finally {
|
|
284
|
+
state.active.delete(value);
|
|
285
|
+
}
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
if (isPersistentHashMap(value)) {
|
|
289
|
+
activate(state, value, path);
|
|
290
|
+
try {
|
|
291
|
+
collectionLength(state, value.count, path);
|
|
292
|
+
yield ["open", "map", value.count];
|
|
293
|
+
let index = 0;
|
|
294
|
+
for (const [key, item] of value) {
|
|
295
|
+
yield* encodeValue(key, state, depth + 1, `${path}.keys[${index}]`, signal);
|
|
296
|
+
yield* encodeValue(item, state, depth + 1, `${path}.values[${index}]`, signal);
|
|
297
|
+
index += 1;
|
|
298
|
+
}
|
|
299
|
+
yield* encodeValue(meta(value), state, depth + 1, `${path}.metadata`, signal);
|
|
300
|
+
} finally {
|
|
301
|
+
state.active.delete(value);
|
|
302
|
+
}
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
if (isPersistentHashSet(value)) {
|
|
306
|
+
activate(state, value, path);
|
|
307
|
+
try {
|
|
308
|
+
collectionLength(state, value.count, path);
|
|
309
|
+
yield ["open", "set", value.count];
|
|
310
|
+
let index = 0;
|
|
311
|
+
for (const item of value) {
|
|
312
|
+
yield* encodeValue(item, state, depth + 1, `${path}[${index}]`, signal);
|
|
313
|
+
index += 1;
|
|
314
|
+
}
|
|
315
|
+
yield* encodeValue(meta(value), state, depth + 1, `${path}.metadata`, signal);
|
|
316
|
+
} finally {
|
|
317
|
+
state.active.delete(value);
|
|
318
|
+
}
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
if (Array.isArray(value)) {
|
|
322
|
+
activate(state, value, path);
|
|
323
|
+
try {
|
|
324
|
+
collectionLength(state, value.length, path);
|
|
325
|
+
yield ["open", "array", value.length];
|
|
326
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
327
|
+
yield* encodeValue(value[index], state, depth + 1, `${path}[${index}]`, signal);
|
|
328
|
+
}
|
|
329
|
+
} finally {
|
|
330
|
+
state.active.delete(value);
|
|
331
|
+
}
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
if (typeof value === "object") {
|
|
335
|
+
activate(state, value, path);
|
|
336
|
+
try {
|
|
337
|
+
const entries = plainObjectEntries(value, state, path);
|
|
338
|
+
yield ["open", "object", entries.length];
|
|
339
|
+
for (let index = 0; index < entries.length; index += 1) {
|
|
340
|
+
const [key, item] = entries[index];
|
|
341
|
+
yield* encodeValue(key, state, depth + 1, `${path}.keys[${index}]`, signal);
|
|
342
|
+
yield* encodeValue(item, state, depth + 1, `${path}.${key}`, signal);
|
|
343
|
+
}
|
|
344
|
+
} finally {
|
|
345
|
+
state.active.delete(value);
|
|
346
|
+
}
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
streamError(state, "unsupported", `unsupported ${typeof value} value`, path);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
export async function* encodeWorkerValueEvents(value, options = {}) {
|
|
353
|
+
const { signal, ...limitOptions } = options;
|
|
354
|
+
const state = createState("encoding", limitOptions);
|
|
355
|
+
yield* encodeValue(value, state, 0, "$", signal);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
function eventBytes(event) {
|
|
359
|
+
return Buffer.byteLength(JSON.stringify(event), "utf8");
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
export async function* encodeWorkerValueChunks(value, options = {}) {
|
|
363
|
+
const limits = normalizedLimits(options);
|
|
364
|
+
let events = [];
|
|
365
|
+
let bytes = 2;
|
|
366
|
+
for await (const event of encodeWorkerValueEvents(value, options)) {
|
|
367
|
+
const size = eventBytes(event);
|
|
368
|
+
if (size + 2 > limits.maxChunkBytes) {
|
|
369
|
+
throw new WorkerValueStreamError(
|
|
370
|
+
"value-stream-encoding-limit",
|
|
371
|
+
`one value event exceeds ${limits.maxChunkBytes} bytes`,
|
|
372
|
+
);
|
|
373
|
+
}
|
|
374
|
+
const added = size + (events.length === 0 ? 0 : 1);
|
|
375
|
+
if (events.length > 0 &&
|
|
376
|
+
(bytes + added > limits.maxChunkBytes ||
|
|
377
|
+
events.length >= limits.maxEventsPerChunk)) {
|
|
378
|
+
yield events;
|
|
379
|
+
await yieldToEventLoop();
|
|
380
|
+
events = [];
|
|
381
|
+
bytes = 2;
|
|
382
|
+
}
|
|
383
|
+
events.push(event);
|
|
384
|
+
bytes += size + (events.length === 1 ? 0 : 1);
|
|
385
|
+
}
|
|
386
|
+
if (events.length > 0) yield events;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function requireEvent(event, length, tag, path) {
|
|
390
|
+
if (!Array.isArray(event) || event.length !== length || event[0] !== tag) {
|
|
391
|
+
throw new WorkerValueStreamError(
|
|
392
|
+
"value-stream-decoding-invalid",
|
|
393
|
+
`${tag} event must contain ${length} fields`,
|
|
394
|
+
path,
|
|
395
|
+
);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
function requireName(value, label, path, nullable = false) {
|
|
400
|
+
if (nullable && value === null) return null;
|
|
401
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
402
|
+
throw new WorkerValueStreamError(
|
|
403
|
+
"value-stream-decoding-invalid",
|
|
404
|
+
`${label} must be a non-empty string${nullable ? " or null" : ""}`,
|
|
405
|
+
path,
|
|
406
|
+
);
|
|
407
|
+
}
|
|
408
|
+
return value;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
function metadata(value, path) {
|
|
412
|
+
if (value === null) return null;
|
|
413
|
+
if (!isPersistentHashMap(value)) {
|
|
414
|
+
throw new WorkerValueStreamError(
|
|
415
|
+
"value-stream-decoding-invalid",
|
|
416
|
+
"metadata must decode to a persistent Map",
|
|
417
|
+
path,
|
|
418
|
+
);
|
|
419
|
+
}
|
|
420
|
+
return value;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
function childPath(frame) {
|
|
424
|
+
const index = frame.received;
|
|
425
|
+
switch (frame.tag) {
|
|
426
|
+
case "keyword":
|
|
427
|
+
case "symbol":
|
|
428
|
+
return `${frame.path}.${["namespace", "name", "metadata"][index]}`;
|
|
429
|
+
case "list":
|
|
430
|
+
case "vector":
|
|
431
|
+
case "set":
|
|
432
|
+
return index < frame.length
|
|
433
|
+
? `${frame.path}[${index}]`
|
|
434
|
+
: `${frame.path}.metadata`;
|
|
435
|
+
case "array":
|
|
436
|
+
return `${frame.path}[${index}]`;
|
|
437
|
+
case "map":
|
|
438
|
+
if (index === frame.length * 2) return `${frame.path}.metadata`;
|
|
439
|
+
return index % 2 === 0
|
|
440
|
+
? `${frame.path}.keys[${index / 2}]`
|
|
441
|
+
: `${frame.path}.values[${(index - 1) / 2}]`;
|
|
442
|
+
case "object":
|
|
443
|
+
return index % 2 === 0
|
|
444
|
+
? `${frame.path}.keys[${index / 2}]`
|
|
445
|
+
: `${frame.path}.values[${(index - 1) / 2}]`;
|
|
446
|
+
default:
|
|
447
|
+
return frame.path;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
function makeFrame(tag, length, path, state) {
|
|
452
|
+
if (typeof tag !== "string" || !Number.isSafeInteger(length) || length < 0) {
|
|
453
|
+
streamError(state, "invalid", "open event tag and length are invalid", path);
|
|
454
|
+
}
|
|
455
|
+
if (tag === "keyword" && length !== 2) {
|
|
456
|
+
streamError(state, "invalid", "keyword open event must declare arity 2", path);
|
|
457
|
+
}
|
|
458
|
+
if (tag === "symbol" && length !== 3) {
|
|
459
|
+
streamError(state, "invalid", "symbol open event must declare arity 3", path);
|
|
460
|
+
}
|
|
461
|
+
if (!["keyword", "symbol", "list", "vector", "map", "set", "array", "object"].includes(tag)) {
|
|
462
|
+
streamError(state, "invalid", `unknown open event tag ${tag}`, path);
|
|
463
|
+
}
|
|
464
|
+
if (!["keyword", "symbol"].includes(tag)) collectionLength(state, length, path);
|
|
465
|
+
const expected = tag === "keyword" || tag === "symbol"
|
|
466
|
+
? length
|
|
467
|
+
: tag === "map" || tag === "object"
|
|
468
|
+
? length * 2 + (tag === "map" ? 1 : 0)
|
|
469
|
+
: length + (["list", "vector", "set"].includes(tag) ? 1 : 0);
|
|
470
|
+
const frame = { tag, length, path, expected, received: 0 };
|
|
471
|
+
if (["keyword", "symbol"].includes(tag)) frame.values = [];
|
|
472
|
+
else if (tag === "list" || tag === "array") frame.values = [];
|
|
473
|
+
else if (tag === "vector") frame.builder = transient(persistentVector());
|
|
474
|
+
else if (tag === "map") frame.builder = transient(persistentHashMap());
|
|
475
|
+
else if (tag === "set") frame.builder = transient(persistentHashSet());
|
|
476
|
+
else if (tag === "object") {
|
|
477
|
+
frame.value = {};
|
|
478
|
+
frame.keys = new Set();
|
|
479
|
+
}
|
|
480
|
+
return frame;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
function consumeFrame(frame, value) {
|
|
484
|
+
const index = frame.received;
|
|
485
|
+
switch (frame.tag) {
|
|
486
|
+
case "keyword":
|
|
487
|
+
case "symbol":
|
|
488
|
+
frame.values.push(value);
|
|
489
|
+
break;
|
|
490
|
+
case "list":
|
|
491
|
+
case "array":
|
|
492
|
+
if (index < frame.length) frame.values.push(value);
|
|
493
|
+
else frame.metadata = value;
|
|
494
|
+
break;
|
|
495
|
+
case "vector":
|
|
496
|
+
if (index < frame.length) frame.builder = conjBang(frame.builder, value);
|
|
497
|
+
else frame.metadata = value;
|
|
498
|
+
break;
|
|
499
|
+
case "map":
|
|
500
|
+
if (index === frame.length * 2) frame.metadata = value;
|
|
501
|
+
else if (index % 2 === 0) frame.key = value;
|
|
502
|
+
else frame.builder = assocBang(frame.builder, frame.key, value);
|
|
503
|
+
break;
|
|
504
|
+
case "set":
|
|
505
|
+
if (index < frame.length) frame.builder = conjBang(frame.builder, value);
|
|
506
|
+
else frame.metadata = value;
|
|
507
|
+
break;
|
|
508
|
+
case "object":
|
|
509
|
+
if (index % 2 === 0) {
|
|
510
|
+
const key = requireName(value, "object key", childPath(frame));
|
|
511
|
+
if (frame.keys.has(key)) {
|
|
512
|
+
throw new WorkerValueStreamError(
|
|
513
|
+
"value-stream-decoding-invalid",
|
|
514
|
+
`object contains duplicate key ${key}`,
|
|
515
|
+
childPath(frame),
|
|
516
|
+
);
|
|
517
|
+
}
|
|
518
|
+
frame.keys.add(key);
|
|
519
|
+
frame.key = key;
|
|
520
|
+
} else {
|
|
521
|
+
Object.defineProperty(frame.value, frame.key, {
|
|
522
|
+
configurable: true,
|
|
523
|
+
enumerable: true,
|
|
524
|
+
value,
|
|
525
|
+
writable: true,
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
break;
|
|
529
|
+
}
|
|
530
|
+
frame.received += 1;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
function finalizeFrame(frame) {
|
|
534
|
+
switch (frame.tag) {
|
|
535
|
+
case "keyword": {
|
|
536
|
+
const namespace = requireName(
|
|
537
|
+
frame.values[0],
|
|
538
|
+
"keyword namespace",
|
|
539
|
+
`${frame.path}.namespace`,
|
|
540
|
+
true,
|
|
541
|
+
);
|
|
542
|
+
const name = requireName(frame.values[1], "keyword name", `${frame.path}.name`);
|
|
543
|
+
return namespace === null ? keyword(name) : keyword(namespace, name);
|
|
544
|
+
}
|
|
545
|
+
case "symbol": {
|
|
546
|
+
const namespace = requireName(
|
|
547
|
+
frame.values[0],
|
|
548
|
+
"symbol namespace",
|
|
549
|
+
`${frame.path}.namespace`,
|
|
550
|
+
true,
|
|
551
|
+
);
|
|
552
|
+
const name = requireName(frame.values[1], "symbol name", `${frame.path}.name`);
|
|
553
|
+
const result = namespace === null
|
|
554
|
+
? eliscriptSymbol(name)
|
|
555
|
+
: eliscriptSymbol(namespace, name);
|
|
556
|
+
const valueMetadata = metadata(frame.values[2], `${frame.path}.metadata`);
|
|
557
|
+
return valueMetadata === null ? result : withMeta(result, valueMetadata);
|
|
558
|
+
}
|
|
559
|
+
case "list": {
|
|
560
|
+
let result = EMPTY_LIST;
|
|
561
|
+
for (let index = frame.values.length - 1; index >= 0; index -= 1) {
|
|
562
|
+
result = result.conj(frame.values[index]);
|
|
563
|
+
}
|
|
564
|
+
const valueMetadata = metadata(frame.metadata, `${frame.path}.metadata`);
|
|
565
|
+
return valueMetadata === null ? result : withMeta(result, valueMetadata);
|
|
566
|
+
}
|
|
567
|
+
case "vector": {
|
|
568
|
+
const result = persistentBang(frame.builder);
|
|
569
|
+
const valueMetadata = metadata(frame.metadata, `${frame.path}.metadata`);
|
|
570
|
+
return valueMetadata === null ? result : withMeta(result, valueMetadata);
|
|
571
|
+
}
|
|
572
|
+
case "map": {
|
|
573
|
+
const result = persistentBang(frame.builder);
|
|
574
|
+
if (result.count !== frame.length) {
|
|
575
|
+
throw new WorkerValueStreamError(
|
|
576
|
+
"value-stream-decoding-invalid",
|
|
577
|
+
"map contains an equivalent duplicate key",
|
|
578
|
+
frame.path,
|
|
579
|
+
);
|
|
580
|
+
}
|
|
581
|
+
const valueMetadata = metadata(frame.metadata, `${frame.path}.metadata`);
|
|
582
|
+
return valueMetadata === null ? result : withMeta(result, valueMetadata);
|
|
583
|
+
}
|
|
584
|
+
case "set": {
|
|
585
|
+
const result = persistentBang(frame.builder);
|
|
586
|
+
if (result.count !== frame.length) {
|
|
587
|
+
throw new WorkerValueStreamError(
|
|
588
|
+
"value-stream-decoding-invalid",
|
|
589
|
+
"set contains an equivalent duplicate value",
|
|
590
|
+
frame.path,
|
|
591
|
+
);
|
|
592
|
+
}
|
|
593
|
+
const valueMetadata = metadata(frame.metadata, `${frame.path}.metadata`);
|
|
594
|
+
return valueMetadata === null ? result : withMeta(result, valueMetadata);
|
|
595
|
+
}
|
|
596
|
+
case "array":
|
|
597
|
+
return frame.values;
|
|
598
|
+
case "object":
|
|
599
|
+
return frame.value;
|
|
600
|
+
default:
|
|
601
|
+
throw new WorkerValueStreamError(
|
|
602
|
+
"value-stream-decoding-invalid",
|
|
603
|
+
`cannot finalize unknown container ${frame.tag}`,
|
|
604
|
+
frame.path,
|
|
605
|
+
);
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
export class WorkerValueStreamDecoder {
|
|
610
|
+
constructor(options = {}) {
|
|
611
|
+
this.state = createState("decoding", options);
|
|
612
|
+
this.signal = options.signal;
|
|
613
|
+
this.stack = [];
|
|
614
|
+
this.text = undefined;
|
|
615
|
+
this.hasRoot = false;
|
|
616
|
+
this.root = undefined;
|
|
617
|
+
this.finished = false;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
currentPath() {
|
|
621
|
+
const frame = this.stack.at(-1);
|
|
622
|
+
return frame ? childPath(frame) : "$";
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
accept(value) {
|
|
626
|
+
let completed = value;
|
|
627
|
+
while (true) {
|
|
628
|
+
const frame = this.stack.at(-1);
|
|
629
|
+
if (!frame) {
|
|
630
|
+
if (this.hasRoot) {
|
|
631
|
+
streamError(this.state, "invalid", "value stream contains multiple roots", "$");
|
|
632
|
+
}
|
|
633
|
+
this.root = completed;
|
|
634
|
+
this.hasRoot = true;
|
|
635
|
+
return;
|
|
636
|
+
}
|
|
637
|
+
consumeFrame(frame, completed);
|
|
638
|
+
if (frame.received < frame.expected) return;
|
|
639
|
+
this.stack.pop();
|
|
640
|
+
completed = finalizeFrame(frame);
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
startText(length, path) {
|
|
645
|
+
stringLength(this.state, length, path);
|
|
646
|
+
if (length === 0) {
|
|
647
|
+
this.accept("");
|
|
648
|
+
return;
|
|
649
|
+
}
|
|
650
|
+
this.text = { path, remaining: length, parts: [] };
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
writeTextPart(event) {
|
|
654
|
+
requireEvent(event, 2, "text-part", this.text.path);
|
|
655
|
+
const part = event[1];
|
|
656
|
+
if (typeof part !== "string" || part.length === 0) {
|
|
657
|
+
streamError(this.state, "invalid", "text-part payload must be non-empty", this.text.path);
|
|
658
|
+
}
|
|
659
|
+
if (part.length > this.text.remaining) {
|
|
660
|
+
streamError(this.state, "invalid", "text parts exceed declared string length", this.text.path);
|
|
661
|
+
}
|
|
662
|
+
this.text.parts.push(part);
|
|
663
|
+
this.text.remaining -= part.length;
|
|
664
|
+
if (this.text.remaining === 0) {
|
|
665
|
+
const value = this.text.parts.join("");
|
|
666
|
+
this.text = undefined;
|
|
667
|
+
this.accept(value);
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
writeEvent(event) {
|
|
672
|
+
checkAbort(this.signal, this.currentPath());
|
|
673
|
+
if (this.finished) {
|
|
674
|
+
streamError(this.state, "invalid", "cannot write after stream completion", "$");
|
|
675
|
+
}
|
|
676
|
+
if (this.text) {
|
|
677
|
+
this.writeTextPart(event);
|
|
678
|
+
return;
|
|
679
|
+
}
|
|
680
|
+
if (!Array.isArray(event) || event.length === 0 || typeof event[0] !== "string") {
|
|
681
|
+
streamError(this.state, "invalid", "value event must be a tagged array", this.currentPath());
|
|
682
|
+
}
|
|
683
|
+
const path = this.currentPath();
|
|
684
|
+
switch (event[0]) {
|
|
685
|
+
case "value":
|
|
686
|
+
requireEvent(event, 2, "value", path);
|
|
687
|
+
if (!(event[1] === null || typeof event[1] === "boolean" ||
|
|
688
|
+
(typeof event[1] === "number" && Number.isFinite(event[1])))) {
|
|
689
|
+
streamError(this.state, "invalid", "value event contains an invalid scalar", path);
|
|
690
|
+
}
|
|
691
|
+
visit(this.state, this.stack.length, path);
|
|
692
|
+
this.accept(event[1]);
|
|
693
|
+
break;
|
|
694
|
+
case "undefined":
|
|
695
|
+
requireEvent(event, 1, "undefined", path);
|
|
696
|
+
visit(this.state, this.stack.length, path);
|
|
697
|
+
this.accept(undefined);
|
|
698
|
+
break;
|
|
699
|
+
case "number":
|
|
700
|
+
requireEvent(event, 2, "number", path);
|
|
701
|
+
if (!SPECIAL_NUMBERS.has(event[1])) {
|
|
702
|
+
streamError(this.state, "invalid", `unknown special number ${String(event[1])}`, path);
|
|
703
|
+
}
|
|
704
|
+
visit(this.state, this.stack.length, path);
|
|
705
|
+
this.accept(SPECIAL_NUMBERS.get(event[1]));
|
|
706
|
+
break;
|
|
707
|
+
case "text":
|
|
708
|
+
requireEvent(event, 2, "text", path);
|
|
709
|
+
visit(this.state, this.stack.length, path);
|
|
710
|
+
this.startText(event[1], path);
|
|
711
|
+
break;
|
|
712
|
+
case "open": {
|
|
713
|
+
requireEvent(event, 3, "open", path);
|
|
714
|
+
visit(this.state, this.stack.length, path);
|
|
715
|
+
const frame = makeFrame(event[1], event[2], path, this.state);
|
|
716
|
+
this.stack.push(frame);
|
|
717
|
+
if (frame.expected === 0) {
|
|
718
|
+
this.stack.pop();
|
|
719
|
+
this.accept(finalizeFrame(frame));
|
|
720
|
+
}
|
|
721
|
+
break;
|
|
722
|
+
}
|
|
723
|
+
default:
|
|
724
|
+
streamError(this.state, "invalid", `unknown value event ${event[0]}`, path);
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
write(events) {
|
|
729
|
+
if (!Array.isArray(events)) {
|
|
730
|
+
streamError(this.state, "invalid", "value chunk events must be an array", this.currentPath());
|
|
731
|
+
}
|
|
732
|
+
let bytes;
|
|
733
|
+
try {
|
|
734
|
+
bytes = Buffer.byteLength(JSON.stringify(events), "utf8");
|
|
735
|
+
} catch (error) {
|
|
736
|
+
streamError(
|
|
737
|
+
this.state,
|
|
738
|
+
"invalid",
|
|
739
|
+
`value chunk is not JSON-serializable: ${error.message}`,
|
|
740
|
+
this.currentPath(),
|
|
741
|
+
);
|
|
742
|
+
}
|
|
743
|
+
if (bytes > this.state.limits.maxChunkBytes) {
|
|
744
|
+
streamError(
|
|
745
|
+
this.state,
|
|
746
|
+
"limit",
|
|
747
|
+
`value chunk exceeds ${this.state.limits.maxChunkBytes} bytes`,
|
|
748
|
+
this.currentPath(),
|
|
749
|
+
);
|
|
750
|
+
}
|
|
751
|
+
if (events.length > this.state.limits.maxEventsPerChunk) {
|
|
752
|
+
streamError(
|
|
753
|
+
this.state,
|
|
754
|
+
"limit",
|
|
755
|
+
`value chunk exceeds ${this.state.limits.maxEventsPerChunk} events`,
|
|
756
|
+
this.currentPath(),
|
|
757
|
+
);
|
|
758
|
+
}
|
|
759
|
+
for (const event of events) this.writeEvent(event);
|
|
760
|
+
return this;
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
finish() {
|
|
764
|
+
checkAbort(this.signal, this.currentPath());
|
|
765
|
+
if (this.finished) {
|
|
766
|
+
streamError(this.state, "invalid", "value stream was already completed", "$");
|
|
767
|
+
}
|
|
768
|
+
if (this.text) {
|
|
769
|
+
streamError(this.state, "truncated", "value stream ended inside a string", this.text.path);
|
|
770
|
+
}
|
|
771
|
+
if (this.stack.length > 0) {
|
|
772
|
+
streamError(this.state, "truncated", "value stream ended inside a container", this.currentPath());
|
|
773
|
+
}
|
|
774
|
+
if (!this.hasRoot) {
|
|
775
|
+
streamError(this.state, "truncated", "value stream did not contain a root", "$");
|
|
776
|
+
}
|
|
777
|
+
this.finished = true;
|
|
778
|
+
return this.root;
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
export async function decodeWorkerValueChunks(chunks, options = {}) {
|
|
783
|
+
const decoder = new WorkerValueStreamDecoder(options);
|
|
784
|
+
for await (const events of chunks) decoder.write(events);
|
|
785
|
+
return decoder.finish();
|
|
786
|
+
}
|