@niscorp/solid 0.1.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/DESIGN.md +320 -0
- package/LICENSE +202 -0
- package/README.md +184 -0
- package/dist/index.cjs +1269 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +34 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +1267 -0
- package/dist/index.js.map +1 -0
- package/package.json +69 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1267 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
// src/types.ts
|
|
4
|
+
var createPendingFinal = () => {
|
|
5
|
+
let resolve = () => {
|
|
6
|
+
};
|
|
7
|
+
let reject = () => {
|
|
8
|
+
};
|
|
9
|
+
const promise = new Promise((res, rej) => {
|
|
10
|
+
resolve = res;
|
|
11
|
+
reject = rej;
|
|
12
|
+
});
|
|
13
|
+
return { resolved: false, promise, resolve, reject };
|
|
14
|
+
};
|
|
15
|
+
var deriveDefaults = (schema) => {
|
|
16
|
+
return deriveFromType(schema);
|
|
17
|
+
};
|
|
18
|
+
var deriveFromType = (schema) => {
|
|
19
|
+
if (schema instanceof z.ZodDefault) {
|
|
20
|
+
return schema._zod.def.defaultValue;
|
|
21
|
+
}
|
|
22
|
+
if (schema instanceof z.ZodOptional) {
|
|
23
|
+
return void 0;
|
|
24
|
+
}
|
|
25
|
+
if (schema instanceof z.ZodNullable) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
if (schema instanceof z.ZodObject) {
|
|
29
|
+
const shape = schema.shape;
|
|
30
|
+
const result = {};
|
|
31
|
+
for (const [key, fieldSchema] of Object.entries(shape)) {
|
|
32
|
+
result[key] = deriveFromType(fieldSchema);
|
|
33
|
+
}
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
if (schema instanceof z.ZodArray) {
|
|
37
|
+
return [];
|
|
38
|
+
}
|
|
39
|
+
if (schema instanceof z.ZodString) {
|
|
40
|
+
return "";
|
|
41
|
+
}
|
|
42
|
+
if (schema instanceof z.ZodNumber) {
|
|
43
|
+
return 0;
|
|
44
|
+
}
|
|
45
|
+
if (schema instanceof z.ZodBoolean) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
if (schema instanceof z.ZodEnum) {
|
|
49
|
+
const values = Object.values(schema._zod.def.entries);
|
|
50
|
+
if (values.length > 0) return values[0];
|
|
51
|
+
}
|
|
52
|
+
if (schema instanceof z.ZodLiteral) {
|
|
53
|
+
const values = [...schema._zod.def.values];
|
|
54
|
+
if (values.length > 0) return values[0];
|
|
55
|
+
}
|
|
56
|
+
if (schema instanceof z.ZodUnion) {
|
|
57
|
+
const options = schema._zod.def.options ?? [];
|
|
58
|
+
const first = options[0];
|
|
59
|
+
if (first) return deriveFromType(first);
|
|
60
|
+
}
|
|
61
|
+
return void 0;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// src/incremental-parser.ts
|
|
65
|
+
var createIncrementalParser = (base, opts = {}) => {
|
|
66
|
+
const valueOpenHook = opts.valueOpenHook;
|
|
67
|
+
const root = structuredClone(base);
|
|
68
|
+
const containerStack = [];
|
|
69
|
+
const containerTypeStack = [];
|
|
70
|
+
const arrayIndexStack = [];
|
|
71
|
+
let currentKey = "";
|
|
72
|
+
const pathStack = [];
|
|
73
|
+
let hasUnpoppedValue = false;
|
|
74
|
+
const dirtyParents = /* @__PURE__ */ new Map();
|
|
75
|
+
let isDirty = false;
|
|
76
|
+
let afterColon = false;
|
|
77
|
+
let expectingValue = false;
|
|
78
|
+
let collectingKey = false;
|
|
79
|
+
let accumulatingString = false;
|
|
80
|
+
let accumulatingLiteral = false;
|
|
81
|
+
let valueBuffer = "";
|
|
82
|
+
let stringEscapeNext = false;
|
|
83
|
+
let unicodeDigits = "";
|
|
84
|
+
let inUnicodeEscape = false;
|
|
85
|
+
let skipping = false;
|
|
86
|
+
let skipKind = "string";
|
|
87
|
+
let skipDepth = 0;
|
|
88
|
+
let skipInString = false;
|
|
89
|
+
let skipStringEscape = false;
|
|
90
|
+
const currentPath = () => pathStack.join(".");
|
|
91
|
+
const topContainerType = () => containerTypeStack[containerTypeStack.length - 1];
|
|
92
|
+
const topArrayIndex = () => arrayIndexStack[arrayIndexStack.length - 1] ?? 0;
|
|
93
|
+
const topContainer = () => containerStack[containerStack.length - 1];
|
|
94
|
+
const markDirty = () => {
|
|
95
|
+
isDirty = true;
|
|
96
|
+
for (let i = 0; i < pathStack.length; i++) {
|
|
97
|
+
const parentPath = i === 0 ? "" : pathStack.slice(0, i).join(".");
|
|
98
|
+
const key = pathStack[i];
|
|
99
|
+
if (!key) continue;
|
|
100
|
+
let keys = dirtyParents.get(parentPath);
|
|
101
|
+
if (!keys) {
|
|
102
|
+
keys = /* @__PURE__ */ new Set();
|
|
103
|
+
dirtyParents.set(parentPath, keys);
|
|
104
|
+
}
|
|
105
|
+
keys.add(key);
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
const setValue = (value) => {
|
|
109
|
+
const container = topContainer();
|
|
110
|
+
let existing;
|
|
111
|
+
if (topContainerType() === "object") {
|
|
112
|
+
const obj = container;
|
|
113
|
+
existing = obj[currentKey];
|
|
114
|
+
obj[currentKey] = value;
|
|
115
|
+
} else {
|
|
116
|
+
const arr = container;
|
|
117
|
+
const idx = topArrayIndex();
|
|
118
|
+
existing = arr[idx];
|
|
119
|
+
arr[idx] = value;
|
|
120
|
+
}
|
|
121
|
+
if (value !== existing) {
|
|
122
|
+
markDirty();
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
const enterContainer = (type) => {
|
|
126
|
+
if (containerStack.length === 0) {
|
|
127
|
+
containerStack.push(root);
|
|
128
|
+
containerTypeStack.push(type);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
const container = topContainer();
|
|
132
|
+
let existing;
|
|
133
|
+
if (topContainerType() === "object") {
|
|
134
|
+
existing = container[currentKey];
|
|
135
|
+
} else {
|
|
136
|
+
existing = container[topArrayIndex()];
|
|
137
|
+
}
|
|
138
|
+
if (type === "object") {
|
|
139
|
+
if (existing && typeof existing === "object" && !Array.isArray(existing)) {
|
|
140
|
+
containerStack.push(existing);
|
|
141
|
+
} else {
|
|
142
|
+
const obj = {};
|
|
143
|
+
setValue(obj);
|
|
144
|
+
containerStack.push(obj);
|
|
145
|
+
}
|
|
146
|
+
} else {
|
|
147
|
+
if (Array.isArray(existing)) {
|
|
148
|
+
containerStack.push(existing);
|
|
149
|
+
} else {
|
|
150
|
+
const arr = [];
|
|
151
|
+
setValue(arr);
|
|
152
|
+
containerStack.push(arr);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
containerTypeStack.push(type);
|
|
156
|
+
};
|
|
157
|
+
const checkValueOpen = (path, kind) => {
|
|
158
|
+
if (!valueOpenHook) return true;
|
|
159
|
+
const decision = valueOpenHook(path, kind);
|
|
160
|
+
if (decision === "accept") return true;
|
|
161
|
+
return false;
|
|
162
|
+
};
|
|
163
|
+
const enterSkipString = () => {
|
|
164
|
+
skipping = true;
|
|
165
|
+
skipKind = "string";
|
|
166
|
+
skipStringEscape = false;
|
|
167
|
+
};
|
|
168
|
+
const enterSkipObject = () => {
|
|
169
|
+
skipping = true;
|
|
170
|
+
skipKind = "object";
|
|
171
|
+
skipDepth = 1;
|
|
172
|
+
skipInString = false;
|
|
173
|
+
skipStringEscape = false;
|
|
174
|
+
};
|
|
175
|
+
const enterSkipArray = () => {
|
|
176
|
+
skipping = true;
|
|
177
|
+
skipKind = "array";
|
|
178
|
+
skipDepth = 1;
|
|
179
|
+
skipInString = false;
|
|
180
|
+
skipStringEscape = false;
|
|
181
|
+
};
|
|
182
|
+
const enterSkipLiteral = () => {
|
|
183
|
+
skipping = true;
|
|
184
|
+
skipKind = "literal";
|
|
185
|
+
};
|
|
186
|
+
const write = (chunk) => {
|
|
187
|
+
const events = [];
|
|
188
|
+
for (const ch of chunk) {
|
|
189
|
+
if (skipping) {
|
|
190
|
+
switch (skipKind) {
|
|
191
|
+
case "string": {
|
|
192
|
+
if (skipStringEscape) {
|
|
193
|
+
skipStringEscape = false;
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
if (ch === "\\") {
|
|
197
|
+
skipStringEscape = true;
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
if (ch === '"') {
|
|
201
|
+
skipping = false;
|
|
202
|
+
}
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
case "object":
|
|
206
|
+
case "array": {
|
|
207
|
+
if (skipInString) {
|
|
208
|
+
if (skipStringEscape) {
|
|
209
|
+
skipStringEscape = false;
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
if (ch === "\\") {
|
|
213
|
+
skipStringEscape = true;
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
if (ch === '"') {
|
|
217
|
+
skipInString = false;
|
|
218
|
+
}
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
if (ch === '"') {
|
|
222
|
+
skipInString = true;
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
const opener = skipKind === "object" ? "{" : "[";
|
|
226
|
+
const closer = skipKind === "object" ? "}" : "]";
|
|
227
|
+
if (ch === opener) {
|
|
228
|
+
skipDepth++;
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
if (ch === closer) {
|
|
232
|
+
skipDepth--;
|
|
233
|
+
if (skipDepth === 0) {
|
|
234
|
+
skipping = false;
|
|
235
|
+
}
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
case "literal": {
|
|
241
|
+
if (isLiteralChar(ch)) continue;
|
|
242
|
+
skipping = false;
|
|
243
|
+
break;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
if (skipping) continue;
|
|
247
|
+
}
|
|
248
|
+
if (accumulatingString) {
|
|
249
|
+
if (inUnicodeEscape) {
|
|
250
|
+
unicodeDigits += ch;
|
|
251
|
+
if (unicodeDigits.length === 4) {
|
|
252
|
+
valueBuffer += String.fromCharCode(parseInt(unicodeDigits, 16));
|
|
253
|
+
inUnicodeEscape = false;
|
|
254
|
+
}
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
if (stringEscapeNext) {
|
|
258
|
+
if (ch === "u") {
|
|
259
|
+
inUnicodeEscape = true;
|
|
260
|
+
unicodeDigits = "";
|
|
261
|
+
} else {
|
|
262
|
+
valueBuffer += unescapeChar(ch);
|
|
263
|
+
}
|
|
264
|
+
stringEscapeNext = false;
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
if (ch === "\\") {
|
|
268
|
+
stringEscapeNext = true;
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
if (ch === '"') {
|
|
272
|
+
accumulatingString = false;
|
|
273
|
+
if (collectingKey) {
|
|
274
|
+
collectingKey = false;
|
|
275
|
+
currentKey = valueBuffer;
|
|
276
|
+
} else {
|
|
277
|
+
setValue(valueBuffer);
|
|
278
|
+
}
|
|
279
|
+
valueBuffer = "";
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
valueBuffer += ch;
|
|
283
|
+
if (!collectingKey) {
|
|
284
|
+
setValue(valueBuffer);
|
|
285
|
+
}
|
|
286
|
+
continue;
|
|
287
|
+
}
|
|
288
|
+
if (accumulatingLiteral) {
|
|
289
|
+
if (isLiteralChar(ch)) {
|
|
290
|
+
valueBuffer += ch;
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
const parsed = parseLiteral(valueBuffer);
|
|
294
|
+
if (parsed.ok) {
|
|
295
|
+
setValue(parsed.value);
|
|
296
|
+
}
|
|
297
|
+
valueBuffer = "";
|
|
298
|
+
accumulatingLiteral = false;
|
|
299
|
+
}
|
|
300
|
+
switch (ch) {
|
|
301
|
+
case "{": {
|
|
302
|
+
const isRoot = containerStack.length === 0;
|
|
303
|
+
if (topContainerType() === "array" && expectingValue) {
|
|
304
|
+
const idx = topArrayIndex();
|
|
305
|
+
events.push({ type: "enterIndex", path: currentPath(), index: idx });
|
|
306
|
+
pathStack.push(String(idx));
|
|
307
|
+
hasUnpoppedValue = true;
|
|
308
|
+
}
|
|
309
|
+
const path = currentPath();
|
|
310
|
+
if (!isRoot && !checkValueOpen(path, "object")) {
|
|
311
|
+
enterSkipObject();
|
|
312
|
+
afterColon = false;
|
|
313
|
+
expectingValue = false;
|
|
314
|
+
break;
|
|
315
|
+
}
|
|
316
|
+
enterContainer("object");
|
|
317
|
+
events.push({ type: "enterObject", path });
|
|
318
|
+
afterColon = false;
|
|
319
|
+
expectingValue = false;
|
|
320
|
+
hasUnpoppedValue = false;
|
|
321
|
+
break;
|
|
322
|
+
}
|
|
323
|
+
case "}": {
|
|
324
|
+
if (hasUnpoppedValue) {
|
|
325
|
+
events.push({ type: "valueComplete", path: currentPath() });
|
|
326
|
+
pathStack.pop();
|
|
327
|
+
hasUnpoppedValue = false;
|
|
328
|
+
}
|
|
329
|
+
const closePath = currentPath();
|
|
330
|
+
events.push({ type: "leaveObject", path: closePath });
|
|
331
|
+
containerStack.pop();
|
|
332
|
+
containerTypeStack.pop();
|
|
333
|
+
if (pathStack.length > 0) pathStack.pop();
|
|
334
|
+
afterColon = false;
|
|
335
|
+
expectingValue = false;
|
|
336
|
+
hasUnpoppedValue = false;
|
|
337
|
+
break;
|
|
338
|
+
}
|
|
339
|
+
case "[": {
|
|
340
|
+
const isRootArr = containerStack.length === 0;
|
|
341
|
+
if (topContainerType() === "array" && expectingValue) {
|
|
342
|
+
const idx = topArrayIndex();
|
|
343
|
+
events.push({ type: "enterIndex", path: currentPath(), index: idx });
|
|
344
|
+
pathStack.push(String(idx));
|
|
345
|
+
hasUnpoppedValue = true;
|
|
346
|
+
}
|
|
347
|
+
const arrPath = currentPath();
|
|
348
|
+
if (!isRootArr && !checkValueOpen(arrPath, "array")) {
|
|
349
|
+
enterSkipArray();
|
|
350
|
+
afterColon = false;
|
|
351
|
+
expectingValue = false;
|
|
352
|
+
break;
|
|
353
|
+
}
|
|
354
|
+
enterContainer("array");
|
|
355
|
+
events.push({ type: "enterArray", path: arrPath });
|
|
356
|
+
arrayIndexStack.push(0);
|
|
357
|
+
expectingValue = true;
|
|
358
|
+
afterColon = false;
|
|
359
|
+
hasUnpoppedValue = false;
|
|
360
|
+
break;
|
|
361
|
+
}
|
|
362
|
+
case "]": {
|
|
363
|
+
if (hasUnpoppedValue) {
|
|
364
|
+
events.push({ type: "valueComplete", path: currentPath() });
|
|
365
|
+
pathStack.pop();
|
|
366
|
+
hasUnpoppedValue = false;
|
|
367
|
+
}
|
|
368
|
+
const arrClosePath = currentPath();
|
|
369
|
+
events.push({ type: "leaveArray", path: arrClosePath });
|
|
370
|
+
containerStack.pop();
|
|
371
|
+
containerTypeStack.pop();
|
|
372
|
+
arrayIndexStack.pop();
|
|
373
|
+
if (pathStack.length > 0) pathStack.pop();
|
|
374
|
+
expectingValue = false;
|
|
375
|
+
afterColon = false;
|
|
376
|
+
hasUnpoppedValue = false;
|
|
377
|
+
break;
|
|
378
|
+
}
|
|
379
|
+
case '"': {
|
|
380
|
+
if (topContainerType() === "object" && !afterColon) {
|
|
381
|
+
accumulatingString = true;
|
|
382
|
+
valueBuffer = "";
|
|
383
|
+
collectingKey = true;
|
|
384
|
+
break;
|
|
385
|
+
}
|
|
386
|
+
if (topContainerType() === "array" && expectingValue) {
|
|
387
|
+
const idx = topArrayIndex();
|
|
388
|
+
events.push({ type: "enterIndex", path: currentPath(), index: idx });
|
|
389
|
+
pathStack.push(String(idx));
|
|
390
|
+
expectingValue = false;
|
|
391
|
+
hasUnpoppedValue = true;
|
|
392
|
+
}
|
|
393
|
+
if (!checkValueOpen(currentPath(), "string")) {
|
|
394
|
+
enterSkipString();
|
|
395
|
+
break;
|
|
396
|
+
}
|
|
397
|
+
accumulatingString = true;
|
|
398
|
+
valueBuffer = "";
|
|
399
|
+
collectingKey = false;
|
|
400
|
+
break;
|
|
401
|
+
}
|
|
402
|
+
case ":": {
|
|
403
|
+
if (topContainerType() === "object") {
|
|
404
|
+
afterColon = true;
|
|
405
|
+
events.push({ type: "enterKey", path: currentPath(), key: currentKey });
|
|
406
|
+
pathStack.push(currentKey);
|
|
407
|
+
expectingValue = true;
|
|
408
|
+
hasUnpoppedValue = true;
|
|
409
|
+
}
|
|
410
|
+
break;
|
|
411
|
+
}
|
|
412
|
+
case ",": {
|
|
413
|
+
if (hasUnpoppedValue) {
|
|
414
|
+
events.push({ type: "valueComplete", path: currentPath() });
|
|
415
|
+
pathStack.pop();
|
|
416
|
+
hasUnpoppedValue = false;
|
|
417
|
+
}
|
|
418
|
+
if (topContainerType() === "object") {
|
|
419
|
+
afterColon = false;
|
|
420
|
+
expectingValue = false;
|
|
421
|
+
} else if (topContainerType() === "array") {
|
|
422
|
+
const lastIdx = arrayIndexStack.length - 1;
|
|
423
|
+
if (lastIdx >= 0) {
|
|
424
|
+
arrayIndexStack[lastIdx] = (arrayIndexStack[lastIdx] ?? 0) + 1;
|
|
425
|
+
}
|
|
426
|
+
expectingValue = true;
|
|
427
|
+
}
|
|
428
|
+
break;
|
|
429
|
+
}
|
|
430
|
+
default: {
|
|
431
|
+
if (ch === " " || ch === "\n" || ch === "\r" || ch === " ") break;
|
|
432
|
+
if (!expectingValue) break;
|
|
433
|
+
if (topContainerType() === "array") {
|
|
434
|
+
const idx = topArrayIndex();
|
|
435
|
+
events.push({ type: "enterIndex", path: currentPath(), index: idx });
|
|
436
|
+
pathStack.push(String(idx));
|
|
437
|
+
hasUnpoppedValue = true;
|
|
438
|
+
}
|
|
439
|
+
const litKind = literalKindFromChar(ch);
|
|
440
|
+
if (!checkValueOpen(currentPath(), litKind)) {
|
|
441
|
+
enterSkipLiteral();
|
|
442
|
+
expectingValue = false;
|
|
443
|
+
break;
|
|
444
|
+
}
|
|
445
|
+
accumulatingLiteral = true;
|
|
446
|
+
valueBuffer = ch;
|
|
447
|
+
expectingValue = false;
|
|
448
|
+
break;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
if (accumulatingLiteral && valueBuffer.length > 0) {
|
|
453
|
+
const parsed = parseLiteral(valueBuffer);
|
|
454
|
+
if (parsed.ok) {
|
|
455
|
+
setValue(parsed.value);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
return events;
|
|
459
|
+
};
|
|
460
|
+
const snapshot = (previous) => {
|
|
461
|
+
if (!isDirty) return { changed: false };
|
|
462
|
+
const value = shareStructure(root, previous, "");
|
|
463
|
+
dirtyParents.clear();
|
|
464
|
+
isDirty = false;
|
|
465
|
+
return { changed: true, value };
|
|
466
|
+
};
|
|
467
|
+
const shareStructure = (source, previous, path) => {
|
|
468
|
+
const dirtyKeys = dirtyParents.get(path);
|
|
469
|
+
if (!dirtyKeys) return previous;
|
|
470
|
+
if (Array.isArray(source)) {
|
|
471
|
+
const prevArr = Array.isArray(previous) ? previous : [];
|
|
472
|
+
const result = new Array(source.length);
|
|
473
|
+
for (let i = 0; i < source.length; i++) {
|
|
474
|
+
const key = String(i);
|
|
475
|
+
if (dirtyKeys.has(key)) {
|
|
476
|
+
const childPath = path === "" ? key : `${path}.${key}`;
|
|
477
|
+
result[i] = dirtyParents.has(childPath) ? shareStructure(source[i], prevArr[i], childPath) : source[i];
|
|
478
|
+
} else {
|
|
479
|
+
result[i] = i < prevArr.length ? prevArr[i] : source[i];
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
return result;
|
|
483
|
+
}
|
|
484
|
+
if (typeof source === "object" && source !== null) {
|
|
485
|
+
const sObj = source;
|
|
486
|
+
const pObj = typeof previous === "object" && previous !== null && !Array.isArray(previous) ? previous : {};
|
|
487
|
+
const result = {};
|
|
488
|
+
for (const key of Object.keys(sObj)) {
|
|
489
|
+
if (dirtyKeys.has(key)) {
|
|
490
|
+
const childPath = path === "" ? key : `${path}.${key}`;
|
|
491
|
+
result[key] = dirtyParents.has(childPath) ? shareStructure(sObj[key], pObj[key], childPath) : sObj[key];
|
|
492
|
+
} else {
|
|
493
|
+
result[key] = key in pObj ? pObj[key] : sObj[key];
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
return result;
|
|
497
|
+
}
|
|
498
|
+
return source;
|
|
499
|
+
};
|
|
500
|
+
return { write, snapshot };
|
|
501
|
+
};
|
|
502
|
+
var unescapeChar = (ch) => {
|
|
503
|
+
switch (ch) {
|
|
504
|
+
case '"':
|
|
505
|
+
return '"';
|
|
506
|
+
case "\\":
|
|
507
|
+
return "\\";
|
|
508
|
+
case "/":
|
|
509
|
+
return "/";
|
|
510
|
+
case "b":
|
|
511
|
+
return "\b";
|
|
512
|
+
case "f":
|
|
513
|
+
return "\f";
|
|
514
|
+
case "n":
|
|
515
|
+
return "\n";
|
|
516
|
+
case "r":
|
|
517
|
+
return "\r";
|
|
518
|
+
case "t":
|
|
519
|
+
return " ";
|
|
520
|
+
default:
|
|
521
|
+
return ch;
|
|
522
|
+
}
|
|
523
|
+
};
|
|
524
|
+
var isLiteralChar = (ch) => ch >= "0" && ch <= "9" || ch === "." || ch === "-" || ch === "+" || ch === "e" || ch === "E" || ch >= "a" && ch <= "z";
|
|
525
|
+
var parseLiteral = (buf) => {
|
|
526
|
+
if (buf === "true") return { ok: true, value: true };
|
|
527
|
+
if (buf === "false") return { ok: true, value: false };
|
|
528
|
+
if (buf === "null") return { ok: true, value: null };
|
|
529
|
+
const num = Number(buf);
|
|
530
|
+
if (!Number.isNaN(num)) return { ok: true, value: num };
|
|
531
|
+
return { ok: false };
|
|
532
|
+
};
|
|
533
|
+
var literalKindFromChar = (ch) => {
|
|
534
|
+
if (ch === "t" || ch === "f") return "boolean";
|
|
535
|
+
if (ch === "n") return "null";
|
|
536
|
+
return "number";
|
|
537
|
+
};
|
|
538
|
+
|
|
539
|
+
// src/finalization-tracker.ts
|
|
540
|
+
var createFinalizationTracker = () => {
|
|
541
|
+
const finalized = /* @__PURE__ */ new Set();
|
|
542
|
+
let isRootFinal = false;
|
|
543
|
+
const keysPerContainer = /* @__PURE__ */ new Map();
|
|
544
|
+
const finalizePath = (path) => {
|
|
545
|
+
if (finalized.has(path)) return;
|
|
546
|
+
finalized.add(path);
|
|
547
|
+
};
|
|
548
|
+
const finalizePreviousSiblings = (containerPath, newKey) => {
|
|
549
|
+
const keys = keysPerContainer.get(containerPath);
|
|
550
|
+
if (!keys) return;
|
|
551
|
+
for (const key of keys) {
|
|
552
|
+
if (key === newKey) break;
|
|
553
|
+
const siblingPath = containerPath === "" ? key : `${containerPath}.${key}`;
|
|
554
|
+
finalizePath(siblingPath);
|
|
555
|
+
finalizeDescendants(siblingPath);
|
|
556
|
+
}
|
|
557
|
+
};
|
|
558
|
+
const finalizeDescendants = (path) => {
|
|
559
|
+
const prefix = path + ".";
|
|
560
|
+
for (const [containerPath, keys] of keysPerContainer) {
|
|
561
|
+
if (containerPath === path || containerPath.startsWith(prefix)) {
|
|
562
|
+
for (const key of keys) {
|
|
563
|
+
const childPath = containerPath === "" ? key : `${containerPath}.${key}`;
|
|
564
|
+
finalizePath(childPath);
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
};
|
|
569
|
+
const process = (events) => {
|
|
570
|
+
for (const event of events) {
|
|
571
|
+
switch (event.type) {
|
|
572
|
+
case "enterObject":
|
|
573
|
+
case "enterArray": {
|
|
574
|
+
if (!keysPerContainer.has(event.path)) {
|
|
575
|
+
keysPerContainer.set(event.path, []);
|
|
576
|
+
}
|
|
577
|
+
break;
|
|
578
|
+
}
|
|
579
|
+
case "enterKey": {
|
|
580
|
+
const keys = keysPerContainer.get(event.path);
|
|
581
|
+
if (keys && !keys.includes(event.key)) {
|
|
582
|
+
finalizePreviousSiblings(event.path, event.key);
|
|
583
|
+
keys.push(event.key);
|
|
584
|
+
}
|
|
585
|
+
break;
|
|
586
|
+
}
|
|
587
|
+
case "enterIndex": {
|
|
588
|
+
const keys = keysPerContainer.get(event.path);
|
|
589
|
+
const indexStr = String(event.index);
|
|
590
|
+
if (keys && !keys.includes(indexStr)) {
|
|
591
|
+
finalizePreviousSiblings(event.path, indexStr);
|
|
592
|
+
keys.push(indexStr);
|
|
593
|
+
}
|
|
594
|
+
break;
|
|
595
|
+
}
|
|
596
|
+
case "leaveObject":
|
|
597
|
+
case "leaveArray": {
|
|
598
|
+
const keys = keysPerContainer.get(event.path);
|
|
599
|
+
if (keys) {
|
|
600
|
+
for (const key of keys) {
|
|
601
|
+
const childPath = event.path === "" ? key : `${event.path}.${key}`;
|
|
602
|
+
finalizePath(childPath);
|
|
603
|
+
finalizeDescendants(childPath);
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
if (event.path === "") {
|
|
607
|
+
isRootFinal = true;
|
|
608
|
+
}
|
|
609
|
+
break;
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
};
|
|
614
|
+
return {
|
|
615
|
+
process,
|
|
616
|
+
isFinal: (path) => finalized.has(path) || isRootFinal,
|
|
617
|
+
isRootFinal: () => isRootFinal,
|
|
618
|
+
getContainerKeys: (path) => keysPerContainer.get(path)
|
|
619
|
+
};
|
|
620
|
+
};
|
|
621
|
+
|
|
622
|
+
// src/path.ts
|
|
623
|
+
var splitPath = (path) => {
|
|
624
|
+
if (path === "") return [];
|
|
625
|
+
return path.split(".");
|
|
626
|
+
};
|
|
627
|
+
var resolvePath = (base, relative) => {
|
|
628
|
+
if (base === "") return relative;
|
|
629
|
+
if (relative === "") return base;
|
|
630
|
+
return `${base}.${relative}`;
|
|
631
|
+
};
|
|
632
|
+
var getByPath = (obj, segments) => {
|
|
633
|
+
let current = obj;
|
|
634
|
+
for (const segment of segments) {
|
|
635
|
+
if (current === null || current === void 0) return void 0;
|
|
636
|
+
if (Array.isArray(current)) {
|
|
637
|
+
const index = Number(segment);
|
|
638
|
+
if (!Number.isInteger(index) || index < 0) return void 0;
|
|
639
|
+
current = current[index];
|
|
640
|
+
} else if (typeof current === "object") {
|
|
641
|
+
current = current[segment];
|
|
642
|
+
} else {
|
|
643
|
+
return void 0;
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
return current;
|
|
647
|
+
};
|
|
648
|
+
|
|
649
|
+
// src/selected-stream.ts
|
|
650
|
+
var createSelectedStream = (deps) => {
|
|
651
|
+
const segments = splitPath(deps.path);
|
|
652
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
653
|
+
const finalListeners = /* @__PURE__ */ new Set();
|
|
654
|
+
const errorListeners = /* @__PURE__ */ new Set();
|
|
655
|
+
let finalState = createPendingFinal();
|
|
656
|
+
let lastEmitted = { hasValue: false };
|
|
657
|
+
let isDestroyed = false;
|
|
658
|
+
let isNotifying = false;
|
|
659
|
+
const pendingImmediateFires = [];
|
|
660
|
+
const project = () => getByPath(deps.getRootValue(), segments);
|
|
661
|
+
const notifyListeners = (value) => {
|
|
662
|
+
isNotifying = true;
|
|
663
|
+
for (const listener of listeners) {
|
|
664
|
+
listener(value);
|
|
665
|
+
}
|
|
666
|
+
isNotifying = false;
|
|
667
|
+
while (pendingImmediateFires.length > 0) {
|
|
668
|
+
const fire = pendingImmediateFires.shift();
|
|
669
|
+
if (fire) fire();
|
|
670
|
+
}
|
|
671
|
+
};
|
|
672
|
+
const resolveFinal = (value) => {
|
|
673
|
+
if (finalState.resolved) return;
|
|
674
|
+
const { resolve } = finalState;
|
|
675
|
+
finalState = { resolved: true, value };
|
|
676
|
+
resolve(value);
|
|
677
|
+
for (const listener of finalListeners) {
|
|
678
|
+
listener(value);
|
|
679
|
+
}
|
|
680
|
+
finalListeners.clear();
|
|
681
|
+
};
|
|
682
|
+
const unsubChange = deps.onRootChange(() => {
|
|
683
|
+
if (isDestroyed) return;
|
|
684
|
+
const projected = project();
|
|
685
|
+
if (!lastEmitted.hasValue || lastEmitted.value !== projected) {
|
|
686
|
+
lastEmitted = { hasValue: true, value: projected };
|
|
687
|
+
notifyListeners(projected);
|
|
688
|
+
}
|
|
689
|
+
if (!finalState.resolved && deps.isPathFinal()) {
|
|
690
|
+
resolveFinal(projected);
|
|
691
|
+
}
|
|
692
|
+
});
|
|
693
|
+
const unsubFinalize = deps.onRootFinalize(() => {
|
|
694
|
+
if (isDestroyed) return;
|
|
695
|
+
if (!finalState.resolved) {
|
|
696
|
+
resolveFinal(project());
|
|
697
|
+
}
|
|
698
|
+
});
|
|
699
|
+
const unsubError = deps.onRootError((err) => {
|
|
700
|
+
if (isDestroyed) return;
|
|
701
|
+
if (!coversPath(err.path, deps.path)) return;
|
|
702
|
+
for (const listener of errorListeners) listener(err);
|
|
703
|
+
});
|
|
704
|
+
const current = () => project();
|
|
705
|
+
const final = () => {
|
|
706
|
+
if (finalState.resolved) return Promise.resolve(finalState.value);
|
|
707
|
+
if (deps.isPathFinal() || deps.isTerminal()) {
|
|
708
|
+
const value = project();
|
|
709
|
+
resolveFinal(value);
|
|
710
|
+
return Promise.resolve(value);
|
|
711
|
+
}
|
|
712
|
+
return finalState.promise;
|
|
713
|
+
};
|
|
714
|
+
const on = (listener) => {
|
|
715
|
+
if (isDestroyed) return () => {
|
|
716
|
+
};
|
|
717
|
+
listeners.add(listener);
|
|
718
|
+
const projected = project();
|
|
719
|
+
lastEmitted = { hasValue: true, value: projected };
|
|
720
|
+
if (isNotifying) {
|
|
721
|
+
pendingImmediateFires.push(() => listener(projected));
|
|
722
|
+
} else {
|
|
723
|
+
listener(projected);
|
|
724
|
+
}
|
|
725
|
+
return () => {
|
|
726
|
+
listeners.delete(listener);
|
|
727
|
+
};
|
|
728
|
+
};
|
|
729
|
+
const onFinal = (listener) => {
|
|
730
|
+
if (isDestroyed) return () => {
|
|
731
|
+
};
|
|
732
|
+
if (finalState.resolved) {
|
|
733
|
+
listener(finalState.value);
|
|
734
|
+
return () => {
|
|
735
|
+
};
|
|
736
|
+
}
|
|
737
|
+
finalListeners.add(listener);
|
|
738
|
+
return () => {
|
|
739
|
+
finalListeners.delete(listener);
|
|
740
|
+
};
|
|
741
|
+
};
|
|
742
|
+
const onError = (listener) => {
|
|
743
|
+
if (isDestroyed) return () => {
|
|
744
|
+
};
|
|
745
|
+
errorListeners.add(listener);
|
|
746
|
+
return () => {
|
|
747
|
+
errorListeners.delete(listener);
|
|
748
|
+
};
|
|
749
|
+
};
|
|
750
|
+
const select = (subPath) => deps.resolveSubSelect(resolvePath(deps.path, subPath));
|
|
751
|
+
const destroy = () => {
|
|
752
|
+
if (isDestroyed) return;
|
|
753
|
+
isDestroyed = true;
|
|
754
|
+
unsubChange();
|
|
755
|
+
unsubFinalize();
|
|
756
|
+
unsubError();
|
|
757
|
+
listeners.clear();
|
|
758
|
+
finalListeners.clear();
|
|
759
|
+
errorListeners.clear();
|
|
760
|
+
pendingImmediateFires.length = 0;
|
|
761
|
+
if (!finalState.resolved) {
|
|
762
|
+
finalState.reject(new Error("[solid] stream destroyed"));
|
|
763
|
+
}
|
|
764
|
+
};
|
|
765
|
+
return { write: () => {
|
|
766
|
+
}, close: () => {
|
|
767
|
+
}, destroy, current, final, on, onFinal, onError, select };
|
|
768
|
+
};
|
|
769
|
+
var coversPath = (errPath, selPath) => {
|
|
770
|
+
if (selPath === "") return true;
|
|
771
|
+
if (errPath === selPath) return true;
|
|
772
|
+
return errPath.startsWith(selPath + ".");
|
|
773
|
+
};
|
|
774
|
+
var DEAD_REJECTION = Promise.reject(new Error("[solid] stream destroyed"));
|
|
775
|
+
DEAD_REJECTION.catch(() => {
|
|
776
|
+
});
|
|
777
|
+
var createDeadStream = () => ({
|
|
778
|
+
write: () => {
|
|
779
|
+
},
|
|
780
|
+
close: () => {
|
|
781
|
+
},
|
|
782
|
+
destroy: () => {
|
|
783
|
+
},
|
|
784
|
+
current: () => void 0,
|
|
785
|
+
final: () => DEAD_REJECTION,
|
|
786
|
+
on: () => () => {
|
|
787
|
+
},
|
|
788
|
+
onFinal: () => () => {
|
|
789
|
+
},
|
|
790
|
+
onError: () => () => {
|
|
791
|
+
},
|
|
792
|
+
select: () => createDeadStream()
|
|
793
|
+
});
|
|
794
|
+
var walkSchema = (root, segments) => {
|
|
795
|
+
let current = root;
|
|
796
|
+
for (const segment of segments) {
|
|
797
|
+
if (!current) return null;
|
|
798
|
+
current = stepInto(current, segment);
|
|
799
|
+
}
|
|
800
|
+
if (!current) return null;
|
|
801
|
+
return inspectSchema(current);
|
|
802
|
+
};
|
|
803
|
+
var stepInto = (schema, segment) => {
|
|
804
|
+
const s = unwrap(schema);
|
|
805
|
+
if (s instanceof z.ZodObject) {
|
|
806
|
+
const shape = s.shape;
|
|
807
|
+
return shape[segment] ?? null;
|
|
808
|
+
}
|
|
809
|
+
if (s instanceof z.ZodArray) {
|
|
810
|
+
return s.element;
|
|
811
|
+
}
|
|
812
|
+
if (s instanceof z.ZodTuple) {
|
|
813
|
+
const items = s._zod.def.items ?? [];
|
|
814
|
+
const idx = Number(segment);
|
|
815
|
+
if (Number.isInteger(idx) && idx >= 0 && idx < items.length) {
|
|
816
|
+
return items[idx] ?? null;
|
|
817
|
+
}
|
|
818
|
+
const rest = s._zod.def.rest ?? null;
|
|
819
|
+
return rest;
|
|
820
|
+
}
|
|
821
|
+
if (s instanceof z.ZodRecord) {
|
|
822
|
+
return s._zod.def.valueType ?? null;
|
|
823
|
+
}
|
|
824
|
+
if (s instanceof z.ZodUnion) {
|
|
825
|
+
const options = s._zod.def.options ?? [];
|
|
826
|
+
for (const opt of options) {
|
|
827
|
+
const next = stepInto(opt, segment);
|
|
828
|
+
if (next) return next;
|
|
829
|
+
}
|
|
830
|
+
return null;
|
|
831
|
+
}
|
|
832
|
+
if (isDiscriminatedUnion(s)) {
|
|
833
|
+
const options = getDiscriminatedUnionOptions(s);
|
|
834
|
+
for (const opt of options) {
|
|
835
|
+
const next = stepInto(opt, segment);
|
|
836
|
+
if (next) return next;
|
|
837
|
+
}
|
|
838
|
+
return null;
|
|
839
|
+
}
|
|
840
|
+
return null;
|
|
841
|
+
};
|
|
842
|
+
var inspectSchema = (schema) => {
|
|
843
|
+
const nullable = allowsNull(schema);
|
|
844
|
+
const s = unwrap(schema);
|
|
845
|
+
const withNull = (kinds) => {
|
|
846
|
+
if (nullable) kinds.add("null");
|
|
847
|
+
return kinds;
|
|
848
|
+
};
|
|
849
|
+
if (s instanceof z.ZodString) return { acceptedKinds: withNull(/* @__PURE__ */ new Set(["string"])), subSchema: schema };
|
|
850
|
+
if (s instanceof z.ZodNumber) return { acceptedKinds: withNull(/* @__PURE__ */ new Set(["number"])), subSchema: schema };
|
|
851
|
+
if (s instanceof z.ZodBoolean) return { acceptedKinds: withNull(/* @__PURE__ */ new Set(["boolean"])), subSchema: schema };
|
|
852
|
+
if (s instanceof z.ZodNull) return { acceptedKinds: /* @__PURE__ */ new Set(["null"]), subSchema: schema };
|
|
853
|
+
if (s instanceof z.ZodObject) return { acceptedKinds: withNull(/* @__PURE__ */ new Set(["object"])), subSchema: schema };
|
|
854
|
+
if (s instanceof z.ZodArray) return { acceptedKinds: withNull(/* @__PURE__ */ new Set(["array"])), subSchema: schema };
|
|
855
|
+
if (s instanceof z.ZodTuple) return { acceptedKinds: withNull(/* @__PURE__ */ new Set(["array"])), subSchema: schema };
|
|
856
|
+
if (s instanceof z.ZodRecord) return { acceptedKinds: withNull(/* @__PURE__ */ new Set(["object"])), subSchema: schema };
|
|
857
|
+
if (s instanceof z.ZodEnum) {
|
|
858
|
+
return { acceptedKinds: withNull(/* @__PURE__ */ new Set(["string"])), subSchema: schema };
|
|
859
|
+
}
|
|
860
|
+
if (s instanceof z.ZodLiteral) {
|
|
861
|
+
const values = [...s._zod.def.values ?? []];
|
|
862
|
+
const kinds = /* @__PURE__ */ new Set();
|
|
863
|
+
for (const v of values) kinds.add(literalToKind(v));
|
|
864
|
+
return { acceptedKinds: withNull(kinds), subSchema: schema };
|
|
865
|
+
}
|
|
866
|
+
if (s instanceof z.ZodUnion) {
|
|
867
|
+
const options = s._zod.def.options ?? [];
|
|
868
|
+
const kinds = /* @__PURE__ */ new Set();
|
|
869
|
+
for (const opt of options) {
|
|
870
|
+
const info = inspectSchema(opt);
|
|
871
|
+
if (info.acceptedKinds === "any") return { acceptedKinds: "any", subSchema: schema };
|
|
872
|
+
for (const k of info.acceptedKinds) kinds.add(k);
|
|
873
|
+
}
|
|
874
|
+
return { acceptedKinds: withNull(kinds), subSchema: schema };
|
|
875
|
+
}
|
|
876
|
+
if (isDiscriminatedUnion(s)) {
|
|
877
|
+
return { acceptedKinds: withNull(/* @__PURE__ */ new Set(["object"])), subSchema: schema };
|
|
878
|
+
}
|
|
879
|
+
return { acceptedKinds: "any", subSchema: schema };
|
|
880
|
+
};
|
|
881
|
+
var unwrap = (schema) => {
|
|
882
|
+
let s = schema;
|
|
883
|
+
for (let i = 0; i < 32; i++) {
|
|
884
|
+
if (s instanceof z.ZodOptional || s instanceof z.ZodNullable) {
|
|
885
|
+
s = s.unwrap();
|
|
886
|
+
continue;
|
|
887
|
+
}
|
|
888
|
+
if (s instanceof z.ZodDefault) {
|
|
889
|
+
s = s._zod.def.innerType;
|
|
890
|
+
continue;
|
|
891
|
+
}
|
|
892
|
+
return s;
|
|
893
|
+
}
|
|
894
|
+
return s;
|
|
895
|
+
};
|
|
896
|
+
var isDiscriminatedUnion = (schema) => {
|
|
897
|
+
const def = schema._zod?.def;
|
|
898
|
+
return def?.type === "discriminatedUnion" || def?.type === "discriminated_union";
|
|
899
|
+
};
|
|
900
|
+
var getDiscriminatedUnionOptions = (schema) => schema._zod.def.options ?? [];
|
|
901
|
+
var literalToKind = (v) => {
|
|
902
|
+
if (v === null) return "null";
|
|
903
|
+
switch (typeof v) {
|
|
904
|
+
case "string":
|
|
905
|
+
return "string";
|
|
906
|
+
case "number":
|
|
907
|
+
return "number";
|
|
908
|
+
case "boolean":
|
|
909
|
+
return "boolean";
|
|
910
|
+
case "object":
|
|
911
|
+
return Array.isArray(v) ? "array" : "object";
|
|
912
|
+
default:
|
|
913
|
+
return "string";
|
|
914
|
+
}
|
|
915
|
+
};
|
|
916
|
+
var allowsNull = (schema) => {
|
|
917
|
+
let s = schema;
|
|
918
|
+
for (let i = 0; i < 32; i++) {
|
|
919
|
+
if (s instanceof z.ZodNullable) return true;
|
|
920
|
+
if (s instanceof z.ZodOptional) {
|
|
921
|
+
s = s.unwrap();
|
|
922
|
+
continue;
|
|
923
|
+
}
|
|
924
|
+
if (s instanceof z.ZodDefault) {
|
|
925
|
+
s = s._zod.def.innerType;
|
|
926
|
+
continue;
|
|
927
|
+
}
|
|
928
|
+
return false;
|
|
929
|
+
}
|
|
930
|
+
return false;
|
|
931
|
+
};
|
|
932
|
+
|
|
933
|
+
// src/validator.ts
|
|
934
|
+
var createValidator = (deps) => {
|
|
935
|
+
let failed = false;
|
|
936
|
+
const finalizeChecked = /* @__PURE__ */ new Set();
|
|
937
|
+
const valueOpen = (path, kind) => {
|
|
938
|
+
if (deps.mode === "trust") return "accept";
|
|
939
|
+
if (failed) return "skip";
|
|
940
|
+
const segments = splitPath(path);
|
|
941
|
+
const info = walkSchema(deps.schema, segments);
|
|
942
|
+
if (!info) {
|
|
943
|
+
reportValueOpen(path, "<unknown path>", kind, deps);
|
|
944
|
+
if (deps.mode === "strict") {
|
|
945
|
+
failed = true;
|
|
946
|
+
deps.onFailed();
|
|
947
|
+
}
|
|
948
|
+
return "skip";
|
|
949
|
+
}
|
|
950
|
+
if (!kindAccepted(info, kind)) {
|
|
951
|
+
reportValueOpen(path, kindsLabel(info), kind, deps);
|
|
952
|
+
if (deps.mode === "strict") {
|
|
953
|
+
failed = true;
|
|
954
|
+
deps.onFailed();
|
|
955
|
+
}
|
|
956
|
+
return "skip";
|
|
957
|
+
}
|
|
958
|
+
return "accept";
|
|
959
|
+
};
|
|
960
|
+
const finalizeField = (path, getRoot) => {
|
|
961
|
+
if (failed) return;
|
|
962
|
+
if (finalizeChecked.has(path)) return;
|
|
963
|
+
finalizeChecked.add(path);
|
|
964
|
+
const segments = splitPath(path);
|
|
965
|
+
const info = walkSchema(deps.schema, segments);
|
|
966
|
+
if (!info || !info.subSchema) return;
|
|
967
|
+
const value = getByPath(getRoot(), segments);
|
|
968
|
+
if (value === void 0) return;
|
|
969
|
+
const result = info.subSchema.safeParse(value);
|
|
970
|
+
if (result.success) return;
|
|
971
|
+
const issue = result.error.issues[0];
|
|
972
|
+
deps.emitError({
|
|
973
|
+
path,
|
|
974
|
+
expected: kindsLabel(info),
|
|
975
|
+
received: describeValue(value),
|
|
976
|
+
phase: "finalize",
|
|
977
|
+
message: issue ? issue.message : "constraint violation"
|
|
978
|
+
});
|
|
979
|
+
if (deps.mode === "strict") {
|
|
980
|
+
failed = true;
|
|
981
|
+
deps.onFailed();
|
|
982
|
+
}
|
|
983
|
+
};
|
|
984
|
+
const finalizeSiblingsBefore = (containerPath, newKey, getRoot) => {
|
|
985
|
+
const keys = deps.getContainerKeys(containerPath);
|
|
986
|
+
if (!keys) return;
|
|
987
|
+
for (const key of keys) {
|
|
988
|
+
if (key === newKey) break;
|
|
989
|
+
const childPath = containerPath === "" ? key : `${containerPath}.${key}`;
|
|
990
|
+
finalizeField(childPath, getRoot);
|
|
991
|
+
if (failed) return;
|
|
992
|
+
}
|
|
993
|
+
};
|
|
994
|
+
const finalizeAllChildren = (containerPath, getRoot) => {
|
|
995
|
+
const keys = deps.getContainerKeys(containerPath);
|
|
996
|
+
if (!keys) return;
|
|
997
|
+
for (const key of keys) {
|
|
998
|
+
const childPath = containerPath === "" ? key : `${containerPath}.${key}`;
|
|
999
|
+
finalizeField(childPath, getRoot);
|
|
1000
|
+
if (failed) return;
|
|
1001
|
+
}
|
|
1002
|
+
finalizeField(containerPath, getRoot);
|
|
1003
|
+
};
|
|
1004
|
+
const processEvents = (events, getRoot) => {
|
|
1005
|
+
if (deps.mode === "trust") return;
|
|
1006
|
+
if (deps.constraints !== "finalize") return;
|
|
1007
|
+
for (const ev of events) {
|
|
1008
|
+
switch (ev.type) {
|
|
1009
|
+
case "enterKey":
|
|
1010
|
+
finalizeSiblingsBefore(ev.path, ev.key, getRoot);
|
|
1011
|
+
break;
|
|
1012
|
+
case "enterIndex":
|
|
1013
|
+
finalizeSiblingsBefore(ev.path, String(ev.index), getRoot);
|
|
1014
|
+
break;
|
|
1015
|
+
case "leaveObject":
|
|
1016
|
+
case "leaveArray":
|
|
1017
|
+
finalizeAllChildren(ev.path, getRoot);
|
|
1018
|
+
break;
|
|
1019
|
+
}
|
|
1020
|
+
if (failed) return;
|
|
1021
|
+
}
|
|
1022
|
+
};
|
|
1023
|
+
const isFailed = () => failed;
|
|
1024
|
+
return { valueOpen, processEvents, isFailed };
|
|
1025
|
+
};
|
|
1026
|
+
var kindAccepted = (info, kind) => {
|
|
1027
|
+
if (info.acceptedKinds === "any") return true;
|
|
1028
|
+
return info.acceptedKinds.has(kind);
|
|
1029
|
+
};
|
|
1030
|
+
var kindsLabel = (info) => {
|
|
1031
|
+
if (info.acceptedKinds === "any") return "any";
|
|
1032
|
+
return [...info.acceptedKinds].join(" | ");
|
|
1033
|
+
};
|
|
1034
|
+
var reportValueOpen = (path, expected, received, deps) => {
|
|
1035
|
+
deps.emitError({
|
|
1036
|
+
path,
|
|
1037
|
+
expected,
|
|
1038
|
+
received,
|
|
1039
|
+
phase: "value-open",
|
|
1040
|
+
message: `[solid] ${path || "<root>"}: expected ${expected}, got ${received}`
|
|
1041
|
+
});
|
|
1042
|
+
};
|
|
1043
|
+
var describeValue = (value) => {
|
|
1044
|
+
if (value === null) return "null";
|
|
1045
|
+
if (Array.isArray(value)) return "array";
|
|
1046
|
+
return typeof value;
|
|
1047
|
+
};
|
|
1048
|
+
|
|
1049
|
+
// src/create-stream.ts
|
|
1050
|
+
var createStream = (options) => {
|
|
1051
|
+
const base = resolveBase(options);
|
|
1052
|
+
const mode = options.mode ?? "recover";
|
|
1053
|
+
const constraints = options.constraints ?? "kind";
|
|
1054
|
+
let currentValue = structuredClone(base);
|
|
1055
|
+
let isClosed = false;
|
|
1056
|
+
let isDestroyed = false;
|
|
1057
|
+
let isFailed = false;
|
|
1058
|
+
let failureError = null;
|
|
1059
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
1060
|
+
const finalListeners = /* @__PURE__ */ new Set();
|
|
1061
|
+
const errorListeners = /* @__PURE__ */ new Set();
|
|
1062
|
+
const changeSubscribers = /* @__PURE__ */ new Set();
|
|
1063
|
+
const finalizeSubscribers = /* @__PURE__ */ new Set();
|
|
1064
|
+
const errorSubscribers = /* @__PURE__ */ new Set();
|
|
1065
|
+
let finalState = createPendingFinal();
|
|
1066
|
+
const selectedStreams = /* @__PURE__ */ new Map();
|
|
1067
|
+
let isNotifying = false;
|
|
1068
|
+
const pendingImmediateFires = [];
|
|
1069
|
+
const emitError = (err) => {
|
|
1070
|
+
for (const listener of errorListeners) listener(err);
|
|
1071
|
+
for (const sub of errorSubscribers) sub(err);
|
|
1072
|
+
};
|
|
1073
|
+
const enterFailedState = () => {
|
|
1074
|
+
if (isFailed) return;
|
|
1075
|
+
isFailed = true;
|
|
1076
|
+
isClosed = true;
|
|
1077
|
+
const error = new Error("[solid] stream failed validation");
|
|
1078
|
+
failureError = error;
|
|
1079
|
+
if (!finalState.resolved) {
|
|
1080
|
+
finalState.promise.catch(() => {
|
|
1081
|
+
});
|
|
1082
|
+
finalState.reject(error);
|
|
1083
|
+
}
|
|
1084
|
+
};
|
|
1085
|
+
const tracker = createFinalizationTracker();
|
|
1086
|
+
const validator = createValidator({
|
|
1087
|
+
schema: options.schema,
|
|
1088
|
+
mode,
|
|
1089
|
+
constraints,
|
|
1090
|
+
emitError,
|
|
1091
|
+
onFailed: enterFailedState,
|
|
1092
|
+
getContainerKeys: (path) => tracker.getContainerKeys(path)
|
|
1093
|
+
});
|
|
1094
|
+
const parser = createIncrementalParser(base, {
|
|
1095
|
+
valueOpenHook: validator.valueOpen
|
|
1096
|
+
});
|
|
1097
|
+
const notify = (value) => {
|
|
1098
|
+
isNotifying = true;
|
|
1099
|
+
for (const listener of listeners) {
|
|
1100
|
+
listener(value);
|
|
1101
|
+
}
|
|
1102
|
+
for (const sub of changeSubscribers) {
|
|
1103
|
+
sub();
|
|
1104
|
+
}
|
|
1105
|
+
isNotifying = false;
|
|
1106
|
+
while (pendingImmediateFires.length > 0) {
|
|
1107
|
+
const fire = pendingImmediateFires.shift();
|
|
1108
|
+
if (fire) fire();
|
|
1109
|
+
}
|
|
1110
|
+
};
|
|
1111
|
+
const notifyFinal = (value) => {
|
|
1112
|
+
if (finalState.resolved) return;
|
|
1113
|
+
const { resolve } = finalState;
|
|
1114
|
+
finalState = { resolved: true, value };
|
|
1115
|
+
resolve(value);
|
|
1116
|
+
for (const listener of finalListeners) {
|
|
1117
|
+
listener(value);
|
|
1118
|
+
}
|
|
1119
|
+
finalListeners.clear();
|
|
1120
|
+
for (const sub of finalizeSubscribers) {
|
|
1121
|
+
sub();
|
|
1122
|
+
}
|
|
1123
|
+
};
|
|
1124
|
+
const write = (chunk) => {
|
|
1125
|
+
if (isClosed || isDestroyed || isFailed) return;
|
|
1126
|
+
const events = parser.write(chunk);
|
|
1127
|
+
if (isFailed) return;
|
|
1128
|
+
tracker.process(events);
|
|
1129
|
+
const snap = parser.snapshot(currentValue);
|
|
1130
|
+
if (snap.changed) {
|
|
1131
|
+
currentValue = snap.value;
|
|
1132
|
+
notify(currentValue);
|
|
1133
|
+
}
|
|
1134
|
+
if (!snap.changed && events.length > 0) {
|
|
1135
|
+
for (const sub of changeSubscribers) {
|
|
1136
|
+
sub();
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
if (events.length > 0) {
|
|
1140
|
+
validator.processEvents(events, () => currentValue);
|
|
1141
|
+
if (isFailed) return;
|
|
1142
|
+
}
|
|
1143
|
+
if (tracker.isRootFinal()) {
|
|
1144
|
+
notifyFinal(currentValue);
|
|
1145
|
+
}
|
|
1146
|
+
};
|
|
1147
|
+
const close = () => {
|
|
1148
|
+
if (isDestroyed || isClosed) return;
|
|
1149
|
+
isClosed = true;
|
|
1150
|
+
if (!finalState.resolved) {
|
|
1151
|
+
notifyFinal(currentValue);
|
|
1152
|
+
}
|
|
1153
|
+
};
|
|
1154
|
+
const destroy = () => {
|
|
1155
|
+
if (isDestroyed) return;
|
|
1156
|
+
isDestroyed = true;
|
|
1157
|
+
listeners.clear();
|
|
1158
|
+
finalListeners.clear();
|
|
1159
|
+
errorListeners.clear();
|
|
1160
|
+
changeSubscribers.clear();
|
|
1161
|
+
finalizeSubscribers.clear();
|
|
1162
|
+
errorSubscribers.clear();
|
|
1163
|
+
pendingImmediateFires.length = 0;
|
|
1164
|
+
if (!finalState.resolved) {
|
|
1165
|
+
finalState.reject(new Error("[solid] stream destroyed"));
|
|
1166
|
+
}
|
|
1167
|
+
for (const [, selected] of selectedStreams) {
|
|
1168
|
+
selected.destroy();
|
|
1169
|
+
}
|
|
1170
|
+
selectedStreams.clear();
|
|
1171
|
+
};
|
|
1172
|
+
const current = () => currentValue;
|
|
1173
|
+
const final = () => {
|
|
1174
|
+
if (isFailed && failureError) return Promise.reject(failureError);
|
|
1175
|
+
if (finalState.resolved) return Promise.resolve(finalState.value);
|
|
1176
|
+
return finalState.promise;
|
|
1177
|
+
};
|
|
1178
|
+
const on = (listener) => {
|
|
1179
|
+
if (isDestroyed) return () => {
|
|
1180
|
+
};
|
|
1181
|
+
listeners.add(listener);
|
|
1182
|
+
if (isNotifying) {
|
|
1183
|
+
pendingImmediateFires.push(() => listener(currentValue));
|
|
1184
|
+
} else {
|
|
1185
|
+
listener(currentValue);
|
|
1186
|
+
}
|
|
1187
|
+
return () => {
|
|
1188
|
+
listeners.delete(listener);
|
|
1189
|
+
};
|
|
1190
|
+
};
|
|
1191
|
+
const onFinal = (listener) => {
|
|
1192
|
+
if (isDestroyed) return () => {
|
|
1193
|
+
};
|
|
1194
|
+
if (finalState.resolved) {
|
|
1195
|
+
listener(finalState.value);
|
|
1196
|
+
return () => {
|
|
1197
|
+
};
|
|
1198
|
+
}
|
|
1199
|
+
finalListeners.add(listener);
|
|
1200
|
+
return () => {
|
|
1201
|
+
finalListeners.delete(listener);
|
|
1202
|
+
};
|
|
1203
|
+
};
|
|
1204
|
+
const onError = (listener) => {
|
|
1205
|
+
if (isDestroyed) return () => {
|
|
1206
|
+
};
|
|
1207
|
+
errorListeners.add(listener);
|
|
1208
|
+
return () => {
|
|
1209
|
+
errorListeners.delete(listener);
|
|
1210
|
+
};
|
|
1211
|
+
};
|
|
1212
|
+
const select = (path) => {
|
|
1213
|
+
if (isDestroyed) return createDeadStream();
|
|
1214
|
+
const cached = selectedStreams.get(path);
|
|
1215
|
+
if (cached) return cached;
|
|
1216
|
+
const selected = createSelectedStream({
|
|
1217
|
+
path,
|
|
1218
|
+
getRootValue: () => currentValue,
|
|
1219
|
+
isTerminal: () => isClosed || isDestroyed,
|
|
1220
|
+
isPathFinal: () => tracker.isFinal(path),
|
|
1221
|
+
onRootChange: (listener) => {
|
|
1222
|
+
changeSubscribers.add(listener);
|
|
1223
|
+
return () => {
|
|
1224
|
+
changeSubscribers.delete(listener);
|
|
1225
|
+
};
|
|
1226
|
+
},
|
|
1227
|
+
onRootFinalize: (listener) => {
|
|
1228
|
+
finalizeSubscribers.add(listener);
|
|
1229
|
+
return () => {
|
|
1230
|
+
finalizeSubscribers.delete(listener);
|
|
1231
|
+
};
|
|
1232
|
+
},
|
|
1233
|
+
onRootError: (listener) => {
|
|
1234
|
+
errorSubscribers.add(listener);
|
|
1235
|
+
return () => {
|
|
1236
|
+
errorSubscribers.delete(listener);
|
|
1237
|
+
};
|
|
1238
|
+
},
|
|
1239
|
+
resolveSubSelect: (fullPath) => select(fullPath)
|
|
1240
|
+
});
|
|
1241
|
+
selectedStreams.set(path, selected);
|
|
1242
|
+
return selected;
|
|
1243
|
+
};
|
|
1244
|
+
return { write, close, destroy, current, final, on, onFinal, onError, select };
|
|
1245
|
+
};
|
|
1246
|
+
var resolveBase = (options) => {
|
|
1247
|
+
const { schema, initial } = options;
|
|
1248
|
+
if (initial !== void 0) {
|
|
1249
|
+
const result2 = schema.safeParse(initial);
|
|
1250
|
+
if (!result2.success) {
|
|
1251
|
+
throw new Error(`[solid] initial value is invalid: ${result2.error.message}`);
|
|
1252
|
+
}
|
|
1253
|
+
return result2.data;
|
|
1254
|
+
}
|
|
1255
|
+
const candidate = deriveDefaults(schema);
|
|
1256
|
+
const result = schema.safeParse(candidate);
|
|
1257
|
+
if (!result.success) {
|
|
1258
|
+
throw new Error(
|
|
1259
|
+
`[solid] cannot derive valid base from schema defaults. Provide an explicit initial value. Error: ${result.error.message}`
|
|
1260
|
+
);
|
|
1261
|
+
}
|
|
1262
|
+
return result.data;
|
|
1263
|
+
};
|
|
1264
|
+
|
|
1265
|
+
export { createStream };
|
|
1266
|
+
//# sourceMappingURL=index.js.map
|
|
1267
|
+
//# sourceMappingURL=index.js.map
|