@dxos/effect 0.11.0 → 0.12.0-next-2be9f24c465d98f4bd186aa03ece788902e6c17a
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/index.mjs +398 -300
- package/dist/lib/index.mjs.map +1 -1
- package/dist/lib/testing.mjs +7 -1
- package/dist/lib/testing.mjs.map +1 -1
- package/dist/types/src/EffectEx.d.ts +2 -1
- package/dist/types/src/EffectEx.d.ts.map +1 -1
- package/dist/types/src/RuntimeProvider.d.ts +8 -4
- package/dist/types/src/RuntimeProvider.d.ts.map +1 -1
- package/dist/types/src/SchemaEx.d.ts +1 -1
- package/dist/types/src/SchemaEx.d.ts.map +1 -1
- package/dist/types/src/atom-kvs.d.ts +2 -2
- package/dist/types/src/atom-kvs.d.ts.map +1 -1
- package/dist/types/src/dynamic-runtime.d.ts +6 -7
- package/dist/types/src/dynamic-runtime.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +2 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/internal/GlobalValue.d.ts +7 -0
- package/dist/types/src/internal/GlobalValue.d.ts.map +1 -0
- package/dist/types/src/internal/ast.d.ts +50 -70
- package/dist/types/src/internal/ast.d.ts.map +1 -1
- package/dist/types/src/internal/async-task-tagging.d.ts.map +1 -1
- package/dist/types/src/internal/errors.d.ts +10 -6
- package/dist/types/src/internal/errors.d.ts.map +1 -1
- package/dist/types/src/internal/json-path.d.ts +2 -2
- package/dist/types/src/internal/json-path.d.ts.map +1 -1
- package/dist/types/src/internal/schema-ast.d.ts +72 -0
- package/dist/types/src/internal/schema-ast.d.ts.map +1 -0
- package/dist/types/src/internal/stream.d.ts +28 -0
- package/dist/types/src/internal/stream.d.ts.map +1 -0
- package/dist/types/src/internal/url.d.ts +3 -4
- package/dist/types/src/internal/url.d.ts.map +1 -1
- package/dist/types/src/layer-memoization.test.d.ts +2 -0
- package/dist/types/src/layer-memoization.test.d.ts.map +1 -0
- package/dist/types/src/otel.d.ts.map +1 -1
- package/dist/types/src/testing.d.ts +17 -1
- package/dist/types/src/testing.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +11 -14
- package/src/EffectEx.ts +10 -1
- package/src/RuntimeProvider.ts +14 -9
- package/src/SchemaEx.ts +2 -1
- package/src/ast.test.ts +95 -37
- package/src/atom-kvs.ts +2 -2
- package/src/dynamic-runtime.test.ts +11 -10
- package/src/dynamic-runtime.ts +34 -35
- package/src/index.ts +2 -0
- package/src/internal/GlobalValue.ts +31 -0
- package/src/internal/ast.ts +212 -313
- package/src/internal/async-task-tagging.ts +27 -16
- package/src/internal/errors.ts +28 -13
- package/src/internal/json-path.ts +14 -8
- package/src/internal/schema-ast.ts +213 -0
- package/src/internal/stream.ts +50 -0
- package/src/internal/url.ts +11 -15
- package/src/interrupt.test.ts +3 -3
- package/src/layer-memoization.test.ts +41 -0
- package/src/layers.test.ts +4 -4
- package/src/otel.ts +2 -2
- package/src/sanity.test.ts +6 -18
- package/src/testing.ts +26 -4
package/dist/lib/index.mjs
CHANGED
|
@@ -1,27 +1,26 @@
|
|
|
1
|
-
import { Atom } from "@effect-atom/atom";
|
|
2
1
|
import * as BrowserKeyValueStore from "@effect/platform-browser/BrowserKeyValueStore";
|
|
2
|
+
import * as Atom from "effect/unstable/reactivity/Atom";
|
|
3
3
|
import * as Context$1 from "effect/Context";
|
|
4
4
|
import * as Effect from "effect/Effect";
|
|
5
5
|
import * as Exit from "effect/Exit";
|
|
6
6
|
import * as Option from "effect/Option";
|
|
7
|
-
import * as Runtime from "effect/Runtime";
|
|
8
7
|
import * as Cause from "effect/Cause";
|
|
9
|
-
import * as Chunk from "effect/Chunk";
|
|
10
|
-
import * as GlobalValue from "effect/GlobalValue";
|
|
11
|
-
import * as Function from "effect/Function";
|
|
12
8
|
import { pipe } from "effect/Function";
|
|
13
9
|
import * as Layer from "effect/Layer";
|
|
14
10
|
import * as Predicate from "effect/Predicate";
|
|
15
11
|
import * as Tracer$1 from "effect/Tracer";
|
|
16
12
|
import { Context } from "@dxos/context";
|
|
13
|
+
import * as Queue from "effect/Queue";
|
|
14
|
+
import * as Stream from "effect/Stream";
|
|
15
|
+
import * as Tracer from "@effect/opentelemetry/OtelTracer";
|
|
17
16
|
import * as Resource from "@effect/opentelemetry/Resource";
|
|
18
|
-
import * as Tracer from "@effect/opentelemetry/Tracer";
|
|
19
17
|
import { trace } from "@opentelemetry/api";
|
|
20
18
|
import * as Schema from "effect/Schema";
|
|
21
19
|
import * as SchemaAST from "effect/SchemaAST";
|
|
22
|
-
import {
|
|
23
|
-
import { decamelize, getDeep, isNonNullable, setDeep } from "@dxos/util";
|
|
20
|
+
import { Arrays, Declaration, IndexSignature, Literal, Objects, PropertySignature, Suspend, Union, any as anyKeyword, boolean as booleanKeyword, isAny as isAnyKeyword, isArrays as isArrays$1, isBoolean as isBooleanKeyword, isDeclaration, isEnum as isEnums, isLiteral, isNever as isNeverKeyword, isNumber as isNumberKeyword, isObjectKeyword, isObjects, isOptional, isString as isStringKeyword, isSuspend, isSymbol as isSymbolKeyword, isUndefined as isUndefinedKeyword, isUnion, isUnknown as isUnknownKeyword, never as neverKeyword, number as numberKeyword, objectKeyword, resolve, string as stringKeyword, toEncoded, toEncoded as encodedBoundAST, toType, unknown as unknownKeyword } from "effect/SchemaAST";
|
|
24
21
|
import { JSONPath } from "jsonpath-plus";
|
|
22
|
+
import { invariant } from "@dxos/invariant";
|
|
23
|
+
import { decamelize, getDeep, setDeep } from "@dxos/util";
|
|
25
24
|
//#region \0rolldown/runtime.js
|
|
26
25
|
var __defProp = Object.defineProperty;
|
|
27
26
|
var __exportAll = (all, no_symbols) => {
|
|
@@ -62,9 +61,27 @@ var createKvsStore = (options) => {
|
|
|
62
61
|
}).pipe(Atom.keepAlive);
|
|
63
62
|
};
|
|
64
63
|
//#endregion
|
|
64
|
+
//#region src/internal/GlobalValue.ts
|
|
65
|
+
var GlobalValue_exports = /* @__PURE__ */ __exportAll({ globalValue: () => globalValue });
|
|
66
|
+
var REGISTRY = Symbol.for("@dxos/effect/globalValue");
|
|
67
|
+
var registry = () => {
|
|
68
|
+
const global = globalThis;
|
|
69
|
+
return global[REGISTRY] ??= /* @__PURE__ */ new Map();
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Returns the value stored for `id`, computing and storing it on first use.
|
|
73
|
+
*
|
|
74
|
+
* @param id Stable identity for the value; equal ids share one instance process-wide.
|
|
75
|
+
*/
|
|
76
|
+
var globalValue = (id, compute) => {
|
|
77
|
+
const map = registry();
|
|
78
|
+
if (!map.has(id)) map.set(id, compute());
|
|
79
|
+
return map.get(id);
|
|
80
|
+
};
|
|
81
|
+
//#endregion
|
|
65
82
|
//#region src/internal/errors.ts
|
|
66
83
|
var spanSymbol = Symbol.for("effect/SpanAnnotation");
|
|
67
|
-
var spanToTrace =
|
|
84
|
+
var spanToTrace = globalValue("effect/Tracer/spanToTrace", () => /* @__PURE__ */ new WeakMap());
|
|
68
85
|
var locationRegex = /\((.*)\)/g;
|
|
69
86
|
/**
|
|
70
87
|
* Adds effect spans.
|
|
@@ -121,7 +138,6 @@ var prettyErrorStack = (error, appendStacks = []) => {
|
|
|
121
138
|
}
|
|
122
139
|
}
|
|
123
140
|
out.push(...appendStacks);
|
|
124
|
-
error = Cause.originalError(error);
|
|
125
141
|
if (error.cause) error.cause = prettyErrorStack(error.cause);
|
|
126
142
|
Object.defineProperty(error, "stack", {
|
|
127
143
|
value: out.join("\n"),
|
|
@@ -142,10 +158,10 @@ var prettyErrorStack = (error, appendStacks = []) => {
|
|
|
142
158
|
* @throws AggregateError if there are multiple errors.
|
|
143
159
|
*/
|
|
144
160
|
var causeToError = (cause) => {
|
|
145
|
-
if (
|
|
146
|
-
else if (Cause.
|
|
161
|
+
if (cause.reasons.length === 0) return /* @__PURE__ */ new Error("Fiber failed without a cause");
|
|
162
|
+
else if (Cause.hasInterruptsOnly(cause)) return /* @__PURE__ */ new Error("Fiber was interrupted");
|
|
147
163
|
else {
|
|
148
|
-
const errors = [...
|
|
164
|
+
const errors = [...cause.reasons.filter(Cause.isFailReason).map((reason) => reason.error), ...cause.reasons.filter(Cause.isDieReason).map((reason) => reason.defect)];
|
|
149
165
|
const getStackFrames = () => {
|
|
150
166
|
const err = /* @__PURE__ */ new Error();
|
|
151
167
|
Error.captureStackTrace(err, causeToError);
|
|
@@ -190,18 +206,28 @@ var runAndForwardErrors = async (effect, options) => {
|
|
|
190
206
|
/** Alias for {@link runAndForwardErrors} — preferred name when accessed via `EffectEx.runPromise`. */
|
|
191
207
|
var runPromise$1 = runAndForwardErrors;
|
|
192
208
|
/**
|
|
209
|
+
* Runs a fire-and-forget effect whose fiber may be interrupted by teardown (component unmount,
|
|
210
|
+
* plugin-manager shutdown): interruption-only exits are absorbed, while failures and defects
|
|
211
|
+
* still surface as unhandled rejections (via {@link unwrapExit}) so real bugs are not silenced.
|
|
212
|
+
*/
|
|
213
|
+
var runDetached = (effect) => {
|
|
214
|
+
Effect.runPromiseExit(effect).then((exit) => {
|
|
215
|
+
if (Exit.isFailure(exit) && !Cause.hasInterruptsOnly(exit.cause)) unwrapExit(exit);
|
|
216
|
+
});
|
|
217
|
+
};
|
|
218
|
+
/**
|
|
193
219
|
* Runs the embedded effect asynchronously and throws any failures and defects as errors.
|
|
194
220
|
*/
|
|
195
221
|
var runInRuntime = (...args) => {
|
|
196
222
|
if (args.length === 1) {
|
|
197
223
|
const [runtime] = args;
|
|
198
224
|
return async (effect, options) => {
|
|
199
|
-
return unwrapExit(await
|
|
225
|
+
return unwrapExit(await runtime.runPromiseExit(effect, options));
|
|
200
226
|
};
|
|
201
227
|
} else {
|
|
202
228
|
const [runtime, effect, options] = args;
|
|
203
229
|
return (async () => {
|
|
204
|
-
return unwrapExit(await
|
|
230
|
+
return unwrapExit(await runtime.runPromiseExit(effect, options));
|
|
205
231
|
})();
|
|
206
232
|
}
|
|
207
233
|
};
|
|
@@ -228,16 +254,16 @@ function make(managedRuntime, tags) {
|
|
|
228
254
|
let validatedRuntimePromise;
|
|
229
255
|
const getValidatedRuntimeAsync = async () => {
|
|
230
256
|
if (!validatedRuntimePromise) validatedRuntimePromise = managedRuntimeAny.runPromise(Effect.gen(function* () {
|
|
231
|
-
const rt = yield* managedRuntimeAny.
|
|
232
|
-
yield* validateTags(rt
|
|
257
|
+
const rt = yield* managedRuntimeAny.contextEffect;
|
|
258
|
+
yield* validateTags(rt, tags);
|
|
233
259
|
return rt;
|
|
234
260
|
}));
|
|
235
261
|
return validatedRuntimePromise;
|
|
236
262
|
};
|
|
237
263
|
const getValidatedRuntime = () => {
|
|
238
264
|
return unwrapExit(managedRuntimeAny.runSyncExit(Effect.gen(function* () {
|
|
239
|
-
const rt = yield* managedRuntimeAny.
|
|
240
|
-
yield* validateTags(rt
|
|
265
|
+
const rt = yield* managedRuntimeAny.contextEffect;
|
|
266
|
+
yield* validateTags(rt, tags);
|
|
241
267
|
return rt;
|
|
242
268
|
})));
|
|
243
269
|
};
|
|
@@ -245,44 +271,44 @@ function make(managedRuntime, tags) {
|
|
|
245
271
|
managedRuntime: managedRuntimeAny,
|
|
246
272
|
runPromise: async (effect) => {
|
|
247
273
|
const runtime = await getValidatedRuntimeAsync();
|
|
248
|
-
return
|
|
274
|
+
return runAndForwardErrors(Effect.provideContext(effect, runtime));
|
|
249
275
|
},
|
|
250
276
|
runSync: (effect) => {
|
|
251
277
|
const runtime = getValidatedRuntime();
|
|
252
|
-
return
|
|
278
|
+
return Effect.runSync(Effect.provideContext(effect, runtime));
|
|
253
279
|
},
|
|
254
280
|
runSyncExit: (effect) => {
|
|
255
281
|
const validationExit = managedRuntimeAny.runSyncExit(Effect.gen(function* () {
|
|
256
|
-
const rt = yield* managedRuntimeAny.
|
|
257
|
-
yield* validateTags(rt
|
|
282
|
+
const rt = yield* managedRuntimeAny.contextEffect;
|
|
283
|
+
yield* validateTags(rt, tags);
|
|
258
284
|
return rt;
|
|
259
285
|
}));
|
|
260
286
|
if (Exit.isSuccess(validationExit)) {
|
|
261
287
|
const runtime = validationExit.value;
|
|
262
|
-
return
|
|
288
|
+
return Effect.runSyncExit(Effect.provideContext(effect, runtime));
|
|
263
289
|
}
|
|
264
290
|
return validationExit;
|
|
265
291
|
},
|
|
266
292
|
runPromiseExit: async (effect) => {
|
|
267
293
|
try {
|
|
268
294
|
const runtime = await getValidatedRuntimeAsync();
|
|
269
|
-
return
|
|
295
|
+
return Effect.runPromiseExit(Effect.provideContext(effect, runtime));
|
|
270
296
|
} catch (error) {
|
|
271
297
|
return Exit.die(error);
|
|
272
298
|
}
|
|
273
299
|
},
|
|
274
300
|
runFork: (effect) => {
|
|
275
301
|
const runtime = getValidatedRuntime();
|
|
276
|
-
return
|
|
302
|
+
return Effect.runFork(Effect.provideContext(effect, runtime));
|
|
277
303
|
},
|
|
278
|
-
|
|
304
|
+
contextEffect: Effect.gen(function* () {
|
|
279
305
|
if (cachedRuntime) return cachedRuntime;
|
|
280
|
-
const rt = yield* managedRuntimeAny.
|
|
281
|
-
yield* validateTags(rt
|
|
306
|
+
const rt = yield* managedRuntimeAny.contextEffect;
|
|
307
|
+
yield* validateTags(rt, tags);
|
|
282
308
|
const runtime = rt;
|
|
283
309
|
cachedRuntime = runtime;
|
|
284
310
|
return runtime;
|
|
285
|
-
}).pipe(Effect.
|
|
311
|
+
}).pipe(Effect.catch(() => Effect.die(/* @__PURE__ */ new Error("Unexpected error in contextEffect validation")))),
|
|
286
312
|
dispose: async () => {
|
|
287
313
|
await managedRuntimeAny.dispose();
|
|
288
314
|
}
|
|
@@ -296,7 +322,7 @@ var runInTask = Symbol("runInTask");
|
|
|
296
322
|
*/
|
|
297
323
|
var asyncTaskTaggingLayer = () => {
|
|
298
324
|
if (Predicate.hasProperty(console, "createTask") === false) return Layer.empty;
|
|
299
|
-
|
|
325
|
+
const makeTracer = Effect.gen(function* () {
|
|
300
326
|
const oldTracer = yield* Effect.tracer;
|
|
301
327
|
return Tracer$1.make({
|
|
302
328
|
span: (name, ...args) => {
|
|
@@ -305,25 +331,27 @@ var asyncTaskTaggingLayer = () => {
|
|
|
305
331
|
span[runInTask] = (f) => trace.run(f);
|
|
306
332
|
return span;
|
|
307
333
|
},
|
|
308
|
-
context: (f, fiber) => {
|
|
309
|
-
const
|
|
310
|
-
|
|
334
|
+
...oldTracer.context ? { context: (f, fiber) => {
|
|
335
|
+
const delegate = () => oldTracer.context(f, fiber);
|
|
336
|
+
const maybeParentSpan = Context$1.getOption(Tracer$1.ParentSpan)(fiber.context);
|
|
337
|
+
if (maybeParentSpan._tag === "None") return delegate();
|
|
311
338
|
const parentSpan = maybeParentSpan.value;
|
|
312
|
-
if (parentSpan._tag === "ExternalSpan") return
|
|
339
|
+
if (parentSpan._tag === "ExternalSpan") return delegate();
|
|
313
340
|
const span = parentSpan;
|
|
314
|
-
if (runInTask in span && typeof span[runInTask] === "function") return span[runInTask](
|
|
315
|
-
return
|
|
316
|
-
}
|
|
341
|
+
if (runInTask in span && typeof span[runInTask] === "function") return span[runInTask](delegate);
|
|
342
|
+
return delegate();
|
|
343
|
+
} } : {}
|
|
317
344
|
});
|
|
318
|
-
})
|
|
345
|
+
});
|
|
346
|
+
return pipe(makeTracer, Effect.map((tracer) => Layer.succeed(Tracer$1.Tracer, tracer)), Layer.unwrap);
|
|
319
347
|
};
|
|
320
348
|
//#endregion
|
|
321
349
|
//#region src/internal/context.ts
|
|
322
|
-
var __dxlog_file$
|
|
350
|
+
var __dxlog_file$1 = "/__w/dxos/dxos/packages/common/effect/src/internal/context.ts";
|
|
323
351
|
var contextFromScope = () => Effect.gen(function* () {
|
|
324
352
|
const ctx = new Context(void 0, {
|
|
325
353
|
"~LogMeta": "~LogMeta",
|
|
326
|
-
F: __dxlog_file$
|
|
354
|
+
F: __dxlog_file$1,
|
|
327
355
|
L: 13
|
|
328
356
|
});
|
|
329
357
|
yield* Effect.addFinalizer(() => Effect.promise(() => ctx.dispose()));
|
|
@@ -344,6 +372,25 @@ var acquireReleaseResource = (getResource) => Effect.acquireRelease(Effect.gen(f
|
|
|
344
372
|
await resource.close?.();
|
|
345
373
|
}));
|
|
346
374
|
//#endregion
|
|
375
|
+
//#region src/internal/stream.ts
|
|
376
|
+
/**
|
|
377
|
+
* Builds a stream from a push-based source that is registered once and cancelled on termination.
|
|
378
|
+
*
|
|
379
|
+
* Effect 4 replaced the emitter-shaped `Stream.async` with the queue-shaped `Stream.callback`; the
|
|
380
|
+
* repo's callback sources (observables, protobuf service streams, event emitters) are all push-once
|
|
381
|
+
* registrations, which this expresses without threading a `Queue` through each one.
|
|
382
|
+
*
|
|
383
|
+
* @param register Subscribes the source; the returned effect is run when the stream terminates.
|
|
384
|
+
*/
|
|
385
|
+
var streamFromEmitter = (register, options) => Stream.callback((queue) => Effect.suspend(() => {
|
|
386
|
+
const cleanup = register({
|
|
387
|
+
single: (value) => void Queue.offerUnsafe(queue, value),
|
|
388
|
+
fail: (error) => void Queue.failCauseUnsafe(queue, Cause.fail(error)),
|
|
389
|
+
end: () => void Queue.endUnsafe(queue)
|
|
390
|
+
});
|
|
391
|
+
return cleanup ? Effect.addFinalizer(() => Effect.asVoid(cleanup)) : Effect.void;
|
|
392
|
+
}), options);
|
|
393
|
+
//#endregion
|
|
347
394
|
//#region src/EffectEx.ts
|
|
348
395
|
var EffectEx_exports = /* @__PURE__ */ __exportAll({
|
|
349
396
|
acquireReleaseResource: () => acquireReleaseResource,
|
|
@@ -351,14 +398,16 @@ var EffectEx_exports = /* @__PURE__ */ __exportAll({
|
|
|
351
398
|
causeToError: () => causeToError,
|
|
352
399
|
contextFromScope: () => contextFromScope,
|
|
353
400
|
runAndForwardErrors: () => runAndForwardErrors,
|
|
401
|
+
runDetached: () => runDetached,
|
|
354
402
|
runInRuntime: () => runInRuntime,
|
|
355
403
|
runPromise: () => runPromise$1,
|
|
404
|
+
streamFromEmitter: () => streamFromEmitter,
|
|
356
405
|
throwCause: () => throwCause,
|
|
357
406
|
unwrapExit: () => unwrapExit
|
|
358
407
|
});
|
|
359
408
|
//#endregion
|
|
360
409
|
//#region src/otel.ts
|
|
361
|
-
var layerOtel = (evaluate) => Layer.
|
|
410
|
+
var layerOtel = (evaluate) => Layer.unwrap(Effect.map(Effect.isEffect(evaluate) ? evaluate : Effect.sync(evaluate), (config) => {
|
|
362
411
|
const ResourceLive = Resource.layerFromEnv(config.resource && Resource.configToAttributes(config.resource));
|
|
363
412
|
const provider = trace.getTracerProvider();
|
|
364
413
|
const TracerLive = Layer.provide(Tracer.layer, Layer.succeed(Tracer.OtelTracerProvider, provider));
|
|
@@ -397,92 +446,224 @@ var RuntimeProvider_exports = /* @__PURE__ */ __exportAll({
|
|
|
397
446
|
* Bridges a runtime provider into a {@link Layer} exposing its services, so a stack that resolves
|
|
398
447
|
* dependencies via `RuntimeProvider.currentRuntime` can be provided from an existing runtime.
|
|
399
448
|
*/
|
|
400
|
-
var toLayer = (provider) => Layer.effectContext(
|
|
449
|
+
var toLayer = (provider) => Layer.effectContext(provider);
|
|
401
450
|
/**
|
|
402
451
|
* @returns Runtime provider from the current context.
|
|
403
452
|
*/
|
|
404
|
-
var currentRuntime = () => Effect.
|
|
453
|
+
var currentRuntime = () => Effect.context().pipe(Effect.map(Effect.succeed));
|
|
405
454
|
/**
|
|
406
|
-
* Run effect, within
|
|
455
|
+
* Run effect, within runtime, clean errors and fix stack-traces.
|
|
407
456
|
*/
|
|
408
457
|
var runPromise = (provider) => async (effect) => {
|
|
409
|
-
const
|
|
410
|
-
return unwrapExit(await
|
|
458
|
+
const context = await runAndForwardErrors(provider);
|
|
459
|
+
return unwrapExit(await Effect.runPromiseExit(Effect.provideContext(effect, context)));
|
|
411
460
|
};
|
|
412
461
|
/**
|
|
413
462
|
* Provide services from runtime provider to effect.
|
|
414
463
|
*/
|
|
415
|
-
var provide = (runtimeProvider) => (effect) => Effect.flatMap(runtimeProvider, (
|
|
464
|
+
var provide = (runtimeProvider) => (effect) => Effect.flatMap(runtimeProvider, (context) => Effect.provideContext(effect, context));
|
|
416
465
|
//#endregion
|
|
417
|
-
//#region src/internal/ast.ts
|
|
418
|
-
var
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
466
|
+
//#region src/internal/schema-ast.ts
|
|
467
|
+
var schema_ast_exports = /* @__PURE__ */ __exportAll({
|
|
468
|
+
Arrays: () => Arrays,
|
|
469
|
+
Declaration: () => Declaration,
|
|
470
|
+
DefaultAnnotationId: () => DefaultAnnotationId,
|
|
471
|
+
DescriptionAnnotationId: () => DescriptionAnnotationId,
|
|
472
|
+
ExamplesAnnotationId: () => ExamplesAnnotationId,
|
|
473
|
+
IdentifierAnnotationId: () => IdentifierAnnotationId,
|
|
474
|
+
IndexSignature: () => IndexSignature,
|
|
475
|
+
JSONSchemaAnnotationId: () => JSONSchemaAnnotationId,
|
|
476
|
+
Literal: () => Literal,
|
|
477
|
+
Objects: () => Objects,
|
|
478
|
+
PropertySignature: () => PropertySignature,
|
|
479
|
+
Suspend: () => Suspend,
|
|
480
|
+
TitleAnnotationId: () => TitleAnnotationId,
|
|
481
|
+
Union: () => Union,
|
|
482
|
+
annotate: () => annotate,
|
|
483
|
+
anyKeyword: () => anyKeyword,
|
|
484
|
+
assignFields: () => assignFields,
|
|
485
|
+
booleanKeyword: () => booleanKeyword,
|
|
486
|
+
encodedBoundAST: () => encodedBoundAST,
|
|
487
|
+
getAnnotation: () => getAnnotation$1,
|
|
488
|
+
getChecks: () => getChecks$1,
|
|
489
|
+
getDefaultAnnotation: () => getDefaultAnnotation,
|
|
490
|
+
getDescriptionAnnotation: () => getDescriptionAnnotation,
|
|
491
|
+
getIdentifierAnnotation: () => getIdentifierAnnotation,
|
|
492
|
+
getIndexSignatures: () => getIndexSignatures,
|
|
493
|
+
getJSONSchemaAnnotation: () => getJSONSchemaAnnotation,
|
|
494
|
+
getPropertySignatures: () => getPropertySignatures$1,
|
|
495
|
+
getTitleAnnotation: () => getTitleAnnotation,
|
|
496
|
+
isAnyKeyword: () => isAnyKeyword,
|
|
497
|
+
isArrays: () => isArrays$1,
|
|
498
|
+
isBooleanKeyword: () => isBooleanKeyword,
|
|
499
|
+
isDeclaration: () => isDeclaration,
|
|
500
|
+
isEnums: () => isEnums,
|
|
501
|
+
isLiteral: () => isLiteral,
|
|
502
|
+
isMutable: () => isMutable$1,
|
|
503
|
+
isNeverKeyword: () => isNeverKeyword,
|
|
504
|
+
isNumberKeyword: () => isNumberKeyword,
|
|
505
|
+
isObjectKeyword: () => isObjectKeyword,
|
|
506
|
+
isObjects: () => isObjects,
|
|
507
|
+
isOptional: () => isOptional,
|
|
508
|
+
isRefinement: () => isRefinement,
|
|
509
|
+
isStringKeyword: () => isStringKeyword,
|
|
510
|
+
isSuspend: () => isSuspend,
|
|
511
|
+
isSymbolKeyword: () => isSymbolKeyword,
|
|
512
|
+
isTransformation: () => isTransformation,
|
|
513
|
+
isUndefinedKeyword: () => isUndefinedKeyword,
|
|
514
|
+
isUnion: () => isUnion,
|
|
515
|
+
isUnknownKeyword: () => isUnknownKeyword,
|
|
516
|
+
neverKeyword: () => neverKeyword,
|
|
517
|
+
numberKeyword: () => numberKeyword,
|
|
518
|
+
objectKeyword: () => objectKeyword,
|
|
519
|
+
omit: () => omit,
|
|
520
|
+
pick: () => pick,
|
|
521
|
+
resolve: () => resolve,
|
|
522
|
+
resolveAnnotations: () => resolveAnnotations$1,
|
|
523
|
+
stringKeyword: () => stringKeyword,
|
|
524
|
+
toEncoded: () => toEncoded,
|
|
525
|
+
toType: () => toType,
|
|
526
|
+
unknownKeyword: () => unknownKeyword
|
|
527
|
+
});
|
|
528
|
+
/** Annotation ids are plain string keys; named constants keep the ~40 call sites typo-safe. */
|
|
529
|
+
var TitleAnnotationId = "title";
|
|
530
|
+
var DescriptionAnnotationId = "description";
|
|
531
|
+
var ExamplesAnnotationId = "examples";
|
|
532
|
+
var IdentifierAnnotationId = "identifier";
|
|
533
|
+
var DefaultAnnotationId = "default";
|
|
534
|
+
var JSONSchemaAnnotationId = "toJsonSchema";
|
|
535
|
+
/**
|
|
536
|
+
* Annotating a node that carries checks attaches to the last check rather than the node, and the
|
|
537
|
+
* resolver reads back only that check, so every read must go through here or annotations on
|
|
538
|
+
* refined types silently vanish.
|
|
539
|
+
*
|
|
540
|
+
* The node's own annotations are layered underneath for the mirror-image case — annotated first,
|
|
541
|
+
* `Schema.check`ed after — which `SchemaAST.resolve` drops entirely.
|
|
542
|
+
*/
|
|
543
|
+
var resolveAnnotations$1 = (ast) => {
|
|
544
|
+
const resolved = SchemaAST.resolve(ast);
|
|
545
|
+
if (resolved === void 0 || resolved === ast.annotations || ast.annotations === void 0) return resolved ?? ast.annotations;
|
|
433
546
|
return {
|
|
434
|
-
|
|
435
|
-
|
|
547
|
+
...ast.annotations,
|
|
548
|
+
...resolved
|
|
436
549
|
};
|
|
437
550
|
};
|
|
551
|
+
/** `SchemaAST.annotate(ast, ...)` is internal in v4; the `Schema` wrapper is public. */
|
|
552
|
+
var annotate = (ast, annotations) => Schema.make(ast).annotate(annotations).ast;
|
|
438
553
|
/**
|
|
439
|
-
*
|
|
554
|
+
* Drops the named properties from an object node.
|
|
440
555
|
*
|
|
441
|
-
*
|
|
556
|
+
* v4 removed the AST-level `omit`; `Objects` is rebuilt directly because the `Schema`-level
|
|
557
|
+
* equivalent needs the key literals at the type level, which a runtime name list cannot supply.
|
|
442
558
|
*/
|
|
443
|
-
var
|
|
444
|
-
const
|
|
445
|
-
|
|
559
|
+
var omit = (ast, names) => {
|
|
560
|
+
const node = unwrapSuspend(ast);
|
|
561
|
+
if (node._tag !== "Objects") return ast;
|
|
562
|
+
const dropped = new Set(names);
|
|
563
|
+
return new SchemaAST.Objects(node.propertySignatures.filter((property) => !dropped.has(property.name)), node.indexSignatures, node.annotations, node.checks, node.encoding, node.context, node.encodingChecks);
|
|
446
564
|
};
|
|
447
565
|
/**
|
|
448
|
-
*
|
|
566
|
+
* Keeps only the named properties of an object node.
|
|
567
|
+
*
|
|
568
|
+
* The mirror of {@link omit}, and needed for the same reason: `mapFields(Struct.pick(...))` wants
|
|
569
|
+
* the key literals at the type level, which a runtime name list cannot supply.
|
|
449
570
|
*/
|
|
450
|
-
var
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
571
|
+
var pick = (ast, names) => {
|
|
572
|
+
const node = unwrapSuspend(ast);
|
|
573
|
+
if (node._tag !== "Objects") return ast;
|
|
574
|
+
const kept = new Set(names);
|
|
575
|
+
return new SchemaAST.Objects(node.propertySignatures.filter((property) => kept.has(property.name)), node.indexSignatures, node.annotations, node.checks, node.encoding, node.context, node.encodingChecks);
|
|
576
|
+
};
|
|
577
|
+
/**
|
|
578
|
+
* Merges another object node's properties in, the later declaration winning on a name clash.
|
|
579
|
+
*
|
|
580
|
+
* v4 removed `Schema.extend`; its replacement (`fieldsAssign`) needs both sides' fields at the type
|
|
581
|
+
* level, which a schema known only as a `Codec` cannot supply. Non-object nodes are returned as-is
|
|
582
|
+
* rather than throwing, matching {@link omit}.
|
|
583
|
+
*/
|
|
584
|
+
var assignFields = (ast, other) => {
|
|
585
|
+
const node = unwrapSuspend(ast);
|
|
586
|
+
const source = unwrapSuspend(other);
|
|
587
|
+
if (node._tag !== "Objects" || source._tag !== "Objects") return ast;
|
|
588
|
+
const added = new Set(source.propertySignatures.map((property) => property.name));
|
|
589
|
+
return new SchemaAST.Objects([...node.propertySignatures.filter((property) => !added.has(property.name)), ...source.propertySignatures], [...node.indexSignatures, ...source.indexSignatures], node.annotations, node.checks, node.encoding, node.context, node.encodingChecks);
|
|
590
|
+
};
|
|
591
|
+
/** `SchemaAST.isMutable` is internal in v4. */
|
|
592
|
+
var isMutable$1 = (ast) => ast.context?.isMutable ?? false;
|
|
593
|
+
/** v4 dropped the top-level accessor; `Objects` carries the signatures directly. */
|
|
594
|
+
var getPropertySignatures$1 = (ast) => {
|
|
595
|
+
const node = unwrapSuspend(ast);
|
|
596
|
+
return node._tag === "Objects" ? node.propertySignatures : [];
|
|
597
|
+
};
|
|
598
|
+
var getIndexSignatures = (ast) => {
|
|
599
|
+
const node = unwrapSuspend(ast);
|
|
600
|
+
return node._tag === "Objects" ? node.indexSignatures : [];
|
|
601
|
+
};
|
|
602
|
+
var unwrapSuspend = (ast) => SchemaAST.isSuspend(ast) ? unwrapSuspend(ast.thunk()) : ast;
|
|
603
|
+
/** Refinements are `Check`s attached to the node in v4 rather than a wrapper node. */
|
|
604
|
+
var getChecks$1 = (ast) => ast.checks ?? [];
|
|
605
|
+
/** There is no `Refinement` node in v4; a node either carries checks or it does not. */
|
|
606
|
+
var isRefinement = (ast) => (ast.checks?.length ?? 0) > 0;
|
|
607
|
+
/** Transformations are an encoding chain in v4 rather than a node kind. */
|
|
608
|
+
var isTransformation = (ast) => ast.encoding !== void 0;
|
|
609
|
+
var annotationGetter = (key) => (ast) => resolveAnnotations$1(ast)?.[key];
|
|
610
|
+
var getTitleAnnotation = annotationGetter(TitleAnnotationId);
|
|
611
|
+
var getDescriptionAnnotation = annotationGetter(DescriptionAnnotationId);
|
|
612
|
+
var getIdentifierAnnotation = annotationGetter(IdentifierAnnotationId);
|
|
613
|
+
var getDefaultAnnotation = annotationGetter(DefaultAnnotationId);
|
|
614
|
+
var getJSONSchemaAnnotation = annotationGetter(JSONSchemaAnnotationId);
|
|
615
|
+
/**
|
|
616
|
+
* Reads one annotation by key.
|
|
617
|
+
*
|
|
618
|
+
* v3 returned an `Option`; v4 annotations are a plain record, so this returns the value or
|
|
619
|
+
* `undefined`. Dual like the Effect APIs it replaces, since call sites use both orders.
|
|
620
|
+
*/
|
|
621
|
+
var getAnnotation$1 = ((...args) => {
|
|
622
|
+
if (args.length === 2) {
|
|
623
|
+
const [ast, key] = args;
|
|
624
|
+
return resolveAnnotations$1(ast)?.[key];
|
|
625
|
+
}
|
|
626
|
+
const [key] = args;
|
|
627
|
+
return (ast) => resolveAnnotations$1(ast)?.[key];
|
|
628
|
+
});
|
|
629
|
+
//#endregion
|
|
630
|
+
//#region src/internal/ast.ts
|
|
631
|
+
var annotateAst = annotate;
|
|
632
|
+
var isMutable = isMutable$1;
|
|
633
|
+
var resolveAnnotations = resolveAnnotations$1;
|
|
634
|
+
var getPropertySignatures = getPropertySignatures$1;
|
|
635
|
+
var getChecks = getChecks$1;
|
|
636
|
+
/**
|
|
637
|
+
* Get the base type of a property: strips the encoding chain and the optional union.
|
|
638
|
+
*/
|
|
639
|
+
var getBaseType = (type) => {
|
|
640
|
+
const unwrapped = unwrapOptional(SchemaAST.toEncoded(type));
|
|
641
|
+
return {
|
|
642
|
+
type: unwrapped,
|
|
643
|
+
checks: getChecks(unwrapped)
|
|
644
|
+
};
|
|
467
645
|
};
|
|
646
|
+
/**
|
|
647
|
+
* Get the property types of an AST.
|
|
648
|
+
*/
|
|
649
|
+
var getProperties = (ast) => getPropertySignatures(ast).map((prop) => {
|
|
650
|
+
const { type, checks } = getBaseType(prop.type);
|
|
651
|
+
const keyAnnotations = prop.type.context?.annotations;
|
|
652
|
+
const mergedType = keyAnnotations && Object.keys(keyAnnotations).length > 0 ? annotateAst(type, keyAnnotations) : type;
|
|
653
|
+
return {
|
|
654
|
+
name: prop.name,
|
|
655
|
+
type: mergedType,
|
|
656
|
+
checks,
|
|
657
|
+
isOptional: SchemaAST.isOptional(prop.type),
|
|
658
|
+
isReadonly: !isMutable(prop.type)
|
|
659
|
+
};
|
|
660
|
+
});
|
|
468
661
|
var VisitResult = /* @__PURE__ */ function(VisitResult) {
|
|
469
662
|
VisitResult[VisitResult["CONTINUE"] = 0] = "CONTINUE";
|
|
470
|
-
/**
|
|
471
|
-
* Skip visiting children.
|
|
472
|
-
*/
|
|
473
663
|
VisitResult[VisitResult["SKIP"] = 1] = "SKIP";
|
|
474
|
-
/**
|
|
475
|
-
* Stop traversing immediately.
|
|
476
|
-
*/
|
|
477
664
|
VisitResult[VisitResult["EXIT"] = 2] = "EXIT";
|
|
478
665
|
return VisitResult;
|
|
479
666
|
}({});
|
|
480
|
-
/**
|
|
481
|
-
* Visit leaf nodes.
|
|
482
|
-
* Refs:
|
|
483
|
-
* - https://github.com/syntax-tree/unist-util-visit?tab=readme-ov-file#visitor
|
|
484
|
-
* - https://github.com/syntax-tree/unist-util-is?tab=readme-ov-file#test
|
|
485
|
-
*/
|
|
486
667
|
var visit = (node, testOrVisitor, visitor) => {
|
|
487
668
|
visitNode(node, testOrVisitor, visitor);
|
|
488
669
|
};
|
|
@@ -491,250 +672,164 @@ var visitNode = (node, test, visitor, path = [], depth = 0) => {
|
|
|
491
672
|
const result = $result === void 0 ? 0 : typeof $result === "boolean" ? $result ? 0 : 1 : $result;
|
|
492
673
|
if (result === 2) return result;
|
|
493
674
|
if (result !== 1) visitor(node, path, depth);
|
|
494
|
-
if (
|
|
495
|
-
const
|
|
496
|
-
|
|
497
|
-
if (result === 2) return result;
|
|
498
|
-
}
|
|
499
|
-
else if (SchemaAST.isTupleType(node)) for (const [i, element] of node.elements.entries()) {
|
|
500
|
-
const currentPath = [...path, i];
|
|
501
|
-
const result = visitNode(element.type, test, visitor, currentPath, depth);
|
|
502
|
-
if (result === 2) return result;
|
|
675
|
+
if (node._tag === "Objects") for (const prop of node.propertySignatures) {
|
|
676
|
+
const child = visitNode(prop.type, test, visitor, [...path, prop.name.toString()], depth + 1);
|
|
677
|
+
if (child === 2) return child;
|
|
503
678
|
}
|
|
504
|
-
else if (
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
const
|
|
510
|
-
|
|
679
|
+
else if (node._tag === "Arrays") {
|
|
680
|
+
for (const [index, element] of node.elements.entries()) {
|
|
681
|
+
const child = visitNode(element, test, visitor, [...path, index], depth);
|
|
682
|
+
if (child === 2) return child;
|
|
683
|
+
}
|
|
684
|
+
for (const rest of node.rest) {
|
|
685
|
+
const child = visitNode(rest, test, visitor, path, depth);
|
|
686
|
+
if (child === 2) return child;
|
|
687
|
+
}
|
|
688
|
+
} else if (node._tag === "Union") for (const type of node.types) {
|
|
689
|
+
const child = visitNode(type, test, visitor, path, depth);
|
|
690
|
+
if (child === 2) return child;
|
|
511
691
|
}
|
|
512
692
|
};
|
|
513
693
|
/**
|
|
514
|
-
* Recursively descend into AST to find first node that passes the test.
|
|
694
|
+
* Recursively descend into AST to find the first node that passes the test.
|
|
515
695
|
*/
|
|
516
696
|
var findNode = (node, test) => {
|
|
517
697
|
if (test(node)) return node;
|
|
518
|
-
|
|
519
|
-
for (const prop of
|
|
698
|
+
if (node._tag === "Objects") {
|
|
699
|
+
for (const prop of node.propertySignatures) {
|
|
520
700
|
const child = findNode(prop.type, test);
|
|
521
701
|
if (child) return child;
|
|
522
702
|
}
|
|
523
|
-
for (const
|
|
524
|
-
const child = findNode(
|
|
703
|
+
for (const index of node.indexSignatures) {
|
|
704
|
+
const child = findNode(index.type, test);
|
|
525
705
|
if (child) return child;
|
|
526
706
|
}
|
|
527
|
-
} else if (
|
|
528
|
-
const
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
707
|
+
} else if (node._tag === "Arrays") {
|
|
708
|
+
for (const element of node.elements) {
|
|
709
|
+
const child = findNode(element, test);
|
|
710
|
+
if (child) return child;
|
|
711
|
+
}
|
|
712
|
+
for (const rest of node.rest) {
|
|
713
|
+
const child = findNode(rest, test);
|
|
714
|
+
if (child) return child;
|
|
715
|
+
}
|
|
716
|
+
} else if (node._tag === "Union") {
|
|
532
717
|
if (isLiteralUnion(node)) return;
|
|
533
718
|
for (const type of node.types) {
|
|
534
719
|
const child = findNode(type, test);
|
|
535
720
|
if (child) return child;
|
|
536
721
|
}
|
|
537
|
-
} else if (
|
|
722
|
+
} else if (node._tag === "Suspend") return findNode(node.thunk(), test);
|
|
538
723
|
};
|
|
539
724
|
/**
|
|
540
725
|
* Get the AST node for the given property (dot-path).
|
|
541
726
|
*/
|
|
542
727
|
var findProperty = (schema, path) => {
|
|
543
|
-
const getProp = (node,
|
|
544
|
-
const [name, ...rest] =
|
|
545
|
-
const typeNode = findNode(node,
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
F: __dxlog_file$1,
|
|
549
|
-
L: 247,
|
|
550
|
-
S: void 0,
|
|
551
|
-
A: ["typeNode", ""]
|
|
552
|
-
});
|
|
553
|
-
for (const prop of SchemaAST.getPropertySignatures(typeNode)) if (prop.name === name) if (rest.length) return getProp(prop.type, rest);
|
|
554
|
-
else return prop.type;
|
|
728
|
+
const getProp = (node, segments) => {
|
|
729
|
+
const [name, ...rest] = segments;
|
|
730
|
+
const typeNode = findNode(node, (candidate) => candidate._tag === "Objects");
|
|
731
|
+
if (!typeNode) return;
|
|
732
|
+
for (const prop of getPropertySignatures(typeNode)) if (prop.name === name) return rest.length ? getProp(prop.type, rest) : prop.type;
|
|
555
733
|
};
|
|
556
734
|
return getProp(schema.ast, path.split("."));
|
|
557
735
|
};
|
|
558
|
-
var defaultAnnotations = {
|
|
559
|
-
ObjectKeyword: SchemaAST.objectKeyword,
|
|
560
|
-
StringKeyword: SchemaAST.stringKeyword,
|
|
561
|
-
NumberKeyword: SchemaAST.numberKeyword,
|
|
562
|
-
BooleanKeyword: SchemaAST.booleanKeyword
|
|
563
|
-
};
|
|
564
736
|
/**
|
|
565
|
-
*
|
|
566
|
-
*
|
|
567
|
-
* @param noDefault If true, then return undefined for effect library defined values.
|
|
737
|
+
* v4 annotations are a plain string-keyed record, so the v3 dance of stripping
|
|
738
|
+
* library defaults collapses to a lookup plus an identifier guard.
|
|
568
739
|
*/
|
|
569
|
-
var getAnnotation = (
|
|
570
|
-
const
|
|
571
|
-
const value =
|
|
572
|
-
if (noDefault &&
|
|
740
|
+
var getAnnotation = (key, noDefault = true) => (node) => {
|
|
741
|
+
const annotations = resolveAnnotations(node);
|
|
742
|
+
const value = annotations?.[key];
|
|
743
|
+
if (noDefault && value !== void 0 && value === annotations?.identifier) return;
|
|
573
744
|
return value;
|
|
574
745
|
};
|
|
575
746
|
/**
|
|
576
|
-
* Recursively descend into AST to find first matching
|
|
577
|
-
* Optionally skips default annotations for basic types (e.g., 'a string').
|
|
578
|
-
*/
|
|
579
|
-
var findAnnotation = (node, annotationId, noDefault = true) => {
|
|
580
|
-
const getAnnotationById = getAnnotation(annotationId, noDefault);
|
|
581
|
-
const getBaseAnnotation = (node) => {
|
|
582
|
-
const value = getAnnotationById(node);
|
|
583
|
-
if (value !== void 0) return value;
|
|
584
|
-
if (SchemaAST.isUnion(node)) {
|
|
585
|
-
if (isOption(node)) return getAnnotationById(node.types[0]);
|
|
586
|
-
}
|
|
587
|
-
};
|
|
588
|
-
return getBaseAnnotation(node);
|
|
589
|
-
};
|
|
590
|
-
/**
|
|
591
|
-
* Effect Schema.optional creates a union type with undefined as the second type.
|
|
592
|
-
*/
|
|
593
|
-
var isOption = (node) => {
|
|
594
|
-
return SchemaAST.isUnion(node) && node.types.length === 2 && SchemaAST.isUndefinedKeyword(node.types[1]);
|
|
595
|
-
};
|
|
596
|
-
/**
|
|
597
|
-
* Determines if the node is a union of literal types.
|
|
747
|
+
* Recursively descend into AST to find the first matching annotation.
|
|
598
748
|
*/
|
|
599
|
-
var
|
|
600
|
-
|
|
749
|
+
var findAnnotation = (node, key, noDefault = true) => {
|
|
750
|
+
const get = getAnnotation(key, noDefault);
|
|
751
|
+
const value = get(node);
|
|
752
|
+
if (value !== void 0) return value;
|
|
753
|
+
if (isOption(node)) return get(node.types[0]);
|
|
601
754
|
};
|
|
755
|
+
/** `Schema.optional` still produces a `T | undefined` union in v4. */
|
|
756
|
+
var isOption = (node) => SchemaAST.isUnion(node) && node.types.length === 2 && SchemaAST.isUndefined(node.types[1]);
|
|
757
|
+
var isLiteralUnion = (node) => SchemaAST.isUnion(node) && node.types.length > 0 && node.types.every(SchemaAST.isLiteral);
|
|
602
758
|
/**
|
|
603
|
-
*
|
|
604
|
-
*
|
|
605
|
-
*
|
|
759
|
+
* The literal values of a union of literals, or `[]` for any other schema.
|
|
760
|
+
*
|
|
761
|
+
* Narrowed to the schema's own type so `Schema.Union([Schema.Literal('a'), …])` yields `('a' | …)[]`
|
|
762
|
+
* rather than all of `LiteralValue` — callers key React lists by these. The AST erases the literal
|
|
763
|
+
* type, so re-attaching it here is the one place the two can be reconnected.
|
|
606
764
|
*/
|
|
607
765
|
var getLiteralValues = (schema) => {
|
|
608
|
-
|
|
609
|
-
return
|
|
610
|
-
};
|
|
611
|
-
/**
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
var
|
|
615
|
-
|
|
616
|
-
};
|
|
617
|
-
/**
|
|
618
|
-
* Get the type of the array elements.
|
|
619
|
-
*/
|
|
620
|
-
var getArrayElementType = (node) => {
|
|
621
|
-
return isArrayType(node) ? node.rest.at(0)?.type : void 0;
|
|
622
|
-
};
|
|
623
|
-
/**
|
|
624
|
-
* Determines if the node is a tuple type.
|
|
625
|
-
*/
|
|
626
|
-
var isTupleType = (node) => {
|
|
627
|
-
return SchemaAST.isTupleType(node) && node.elements.length > 0;
|
|
628
|
-
};
|
|
629
|
-
/**
|
|
630
|
-
* Determines if the node is a discriminated union.
|
|
631
|
-
*/
|
|
632
|
-
var isDiscriminatedUnion = (node) => {
|
|
633
|
-
return SchemaAST.isUnion(node) && !!getDiscriminatingProps(node)?.length;
|
|
634
|
-
};
|
|
635
|
-
/**
|
|
636
|
-
* Get the discriminating properties for the given union type.
|
|
637
|
-
*/
|
|
766
|
+
const ast = schema.ast;
|
|
767
|
+
return isLiteralUnion(ast) ? ast.types.map((node) => node.literal) : [];
|
|
768
|
+
};
|
|
769
|
+
/** An unbounded array: no fixed elements, exactly one rest type. */
|
|
770
|
+
var isArrayType = (node) => SchemaAST.isArrays(node) && node.elements.length === 0 && node.rest.length === 1;
|
|
771
|
+
var getArrayElementType = (node) => isArrayType(node) ? node.rest[0] : void 0;
|
|
772
|
+
var isArrays = (node) => SchemaAST.isArrays(node) && node.elements.length > 0;
|
|
773
|
+
var isDiscriminatedUnion = (node) => SchemaAST.isUnion(node) && !!getDiscriminatingProps(node)?.length;
|
|
638
774
|
var getDiscriminatingProps = (node) => {
|
|
639
|
-
|
|
640
|
-
"~LogMeta": "~LogMeta",
|
|
641
|
-
F: __dxlog_file$1,
|
|
642
|
-
L: 379,
|
|
643
|
-
S: void 0,
|
|
644
|
-
A: ["SchemaAST.isUnion(node)", ""]
|
|
645
|
-
});
|
|
646
|
-
if (isOption(node)) return;
|
|
775
|
+
if (!SchemaAST.isUnion(node) || isOption(node)) return;
|
|
647
776
|
return node.types.reduce((shared, type) => {
|
|
648
|
-
const props =
|
|
777
|
+
const props = getPropertySignatures(type).filter((prop) => SchemaAST.isLiteral(prop.type)).map((prop) => prop.name.toString());
|
|
649
778
|
return shared.length === 0 ? props : shared.filter((prop) => props.includes(prop));
|
|
650
779
|
}, []);
|
|
651
780
|
};
|
|
652
|
-
/**
|
|
653
|
-
* Get the discriminated type for the given value.
|
|
654
|
-
*/
|
|
655
781
|
var getDiscriminatedType = (node, value = {}) => {
|
|
656
|
-
invariant(SchemaAST.isUnion(node), void 0, {
|
|
657
|
-
"~LogMeta": "~LogMeta",
|
|
658
|
-
F: __dxlog_file$1,
|
|
659
|
-
L: 403,
|
|
660
|
-
S: void 0,
|
|
661
|
-
A: ["SchemaAST.isUnion(node)", ""]
|
|
662
|
-
});
|
|
663
|
-
invariant(value, void 0, {
|
|
664
|
-
"~LogMeta": "~LogMeta",
|
|
665
|
-
F: __dxlog_file$1,
|
|
666
|
-
L: 404,
|
|
667
|
-
S: void 0,
|
|
668
|
-
A: ["value", ""]
|
|
669
|
-
});
|
|
670
782
|
const props = getDiscriminatingProps(node);
|
|
671
|
-
if (!props?.length) return;
|
|
672
|
-
for (const type of node.types) if (
|
|
673
|
-
invariant(SchemaAST.isLiteral(prop.type), void 0, {
|
|
674
|
-
"~LogMeta": "~LogMeta",
|
|
675
|
-
F: __dxlog_file$1,
|
|
676
|
-
L: 415,
|
|
677
|
-
S: void 0,
|
|
678
|
-
A: ["SchemaAST.isLiteral(prop.type)", ""]
|
|
679
|
-
});
|
|
680
|
-
return prop.type.literal === value[prop.name.toString()];
|
|
681
|
-
})) return type;
|
|
783
|
+
if (!props?.length || !SchemaAST.isUnion(node)) return;
|
|
784
|
+
for (const type of node.types) if (getPropertySignatures(type).filter((prop) => props.includes(prop.name.toString())).every((prop) => SchemaAST.isLiteral(prop.type) && prop.type.literal === value[prop.name.toString()])) return type;
|
|
682
785
|
const fields = Object.fromEntries(props.map((prop) => {
|
|
683
786
|
const literals = node.types.map((type) => {
|
|
684
|
-
const
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
S: void 0,
|
|
690
|
-
A: ["SchemaAST.isLiteral(literal.type)", ""]
|
|
691
|
-
});
|
|
692
|
-
return literal.type.literal;
|
|
693
|
-
}).filter(isNonNullable);
|
|
694
|
-
return literals.length ? [prop, Schema.Literal(...literals)] : void 0;
|
|
695
|
-
}).filter(isNonNullable));
|
|
787
|
+
const found = getPropertySignatures(type).find((candidate) => candidate.name.toString() === prop);
|
|
788
|
+
return found && SchemaAST.isLiteral(found.type) ? found.type.literal : void 0;
|
|
789
|
+
}).filter((literal) => literal !== void 0);
|
|
790
|
+
return literals.length ? [prop, Schema.Literals(literals)] : void 0;
|
|
791
|
+
}).filter((entry) => entry !== void 0));
|
|
696
792
|
return Schema.Struct(fields).ast;
|
|
697
793
|
};
|
|
698
794
|
/**
|
|
699
|
-
* If a property
|
|
700
|
-
*
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
if (!property.isOptional || !SchemaAST.isUnion(property.type) || !isOption(property.type)) return property;
|
|
704
|
-
return property.type.types[0];
|
|
705
|
-
};
|
|
706
|
-
/**
|
|
707
|
-
* Determines if the node is a nested object type.
|
|
795
|
+
* If a property type is optional (T | undefined), return the inner non-undefined node.
|
|
796
|
+
*
|
|
797
|
+
* Applied until the type is no longer optional: v4's `Schema.optional` is not idempotent (it nests
|
|
798
|
+
* as `(T | undefined) | undefined`), which v3's `Schema.partial` over an already-optional field was.
|
|
708
799
|
*/
|
|
709
|
-
var
|
|
710
|
-
|
|
800
|
+
var unwrapOptional = (type) => {
|
|
801
|
+
let node = type;
|
|
802
|
+
while (isOption(node)) node = node.types[0];
|
|
803
|
+
return node;
|
|
711
804
|
};
|
|
805
|
+
var isNestedType = (node) => SchemaAST.isDeclaration(node) || SchemaAST.isObjectKeyword(node) || node._tag === "Objects" || isArrays(node) || isDiscriminatedUnion(node);
|
|
712
806
|
/**
|
|
713
|
-
* Maps AST nodes.
|
|
714
|
-
* The user is responsible for recursively calling {@link mapAst} on the SchemaAST.
|
|
807
|
+
* Maps AST nodes. The caller is responsible for recursing.
|
|
715
808
|
* NOTE: Will evaluate suspended ASTs.
|
|
716
809
|
*/
|
|
717
810
|
var mapAst = (ast, f) => {
|
|
718
811
|
switch (ast._tag) {
|
|
719
|
-
case "
|
|
720
|
-
case "Union": return SchemaAST.Union
|
|
721
|
-
case "
|
|
812
|
+
case "Objects": return new SchemaAST.Objects(ast.propertySignatures.map((prop) => new SchemaAST.PropertySignature(prop.name, retainContext(prop.type, f(prop.type, prop.name)))), ast.indexSignatures, ast.annotations, ast.checks, ast.encoding, ast.context);
|
|
813
|
+
case "Union": return new SchemaAST.Union(ast.types.map((type) => f(type, void 0)), ast.mode, ast.annotations, ast.checks, ast.encoding, ast.context);
|
|
814
|
+
case "Arrays": return new SchemaAST.Arrays(ast.isMutable, ast.elements.map((element, index) => f(element, index)), ast.rest.map((rest) => f(rest, void 0)), ast.annotations, ast.checks, ast.encoding, ast.context);
|
|
722
815
|
case "Suspend": {
|
|
723
|
-
const
|
|
724
|
-
return new SchemaAST.Suspend(() =>
|
|
816
|
+
const next = f(ast.thunk(), void 0);
|
|
817
|
+
return new SchemaAST.Suspend(() => next, ast.annotations, void 0, ast.encoding, ast.context);
|
|
725
818
|
}
|
|
726
819
|
default: return ast;
|
|
727
820
|
}
|
|
728
821
|
};
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
822
|
+
/**
|
|
823
|
+
* Optionality and mutability ride on `context` in v4, so a mapper that rebuilds a
|
|
824
|
+
* property type must carry the original context across or the key silently becomes required.
|
|
825
|
+
* `SchemaAST.replaceContext` is internal, so this rebuilds through the public key combinators.
|
|
826
|
+
*/
|
|
827
|
+
var retainContext = (original, mapped) => {
|
|
828
|
+
if (!original.context || mapped.context) return mapped;
|
|
829
|
+
let schema = Schema.make(mapped);
|
|
830
|
+
if (original.context.isOptional) schema = Schema.optionalKey(schema);
|
|
831
|
+
if (original.context.isMutable) schema = Schema.mutableKey(schema);
|
|
832
|
+
return schema.ast;
|
|
738
833
|
};
|
|
739
834
|
//#endregion
|
|
740
835
|
//#region src/internal/json-path.ts
|
|
@@ -744,13 +839,13 @@ var PROP_REGEX = /^\w+$/;
|
|
|
744
839
|
/**
|
|
745
840
|
* https://www.ietf.org/archive/id/draft-goessner-dispatch-jsonpath-00.html
|
|
746
841
|
*/
|
|
747
|
-
var JsonPath = Schema.String.pipe(Schema.
|
|
842
|
+
var JsonPath = Schema.String.pipe(Schema.check(Schema.isPattern(PATH_REGEX)), Schema.refine((value) => PATH_REGEX.test(value))).annotate({
|
|
748
843
|
title: "JSON path",
|
|
749
844
|
description: "JSON path to a property"
|
|
750
845
|
});
|
|
751
|
-
var JsonProp = Schema.NonEmptyString.pipe(Schema.
|
|
846
|
+
var JsonProp = Schema.NonEmptyString.pipe(Schema.check(Schema.isPattern(PROP_REGEX, { message: "Property name must contain only letters, numbers, and underscores" })), Schema.refine((value) => PROP_REGEX.test(value)));
|
|
752
847
|
var isJsonPath = (value) => {
|
|
753
|
-
return Option.isSome(Schema.
|
|
848
|
+
return Option.isSome(Schema.decodeUnknownOption(JsonPath)(value));
|
|
754
849
|
};
|
|
755
850
|
/**
|
|
756
851
|
* Creates a JsonPath from an array of path segments.
|
|
@@ -779,7 +874,7 @@ var createJsonPath = (path) => {
|
|
|
779
874
|
invariant(isJsonPath(candidatePath), `Invalid JsonPath: ${candidatePath}`, {
|
|
780
875
|
"~LogMeta": "~LogMeta",
|
|
781
876
|
F: __dxlog_file,
|
|
782
|
-
L:
|
|
877
|
+
L: 75,
|
|
783
878
|
S: void 0,
|
|
784
879
|
A: ["isJsonPath(candidatePath)", "`Invalid JsonPath: ${candidatePath}`"]
|
|
785
880
|
});
|
|
@@ -794,7 +889,7 @@ var fromEffectValidationPath = (effectPath) => {
|
|
|
794
889
|
invariant(isJsonPath(jsonPath), `Invalid JsonPath: ${jsonPath}`, {
|
|
795
890
|
"~LogMeta": "~LogMeta",
|
|
796
891
|
F: __dxlog_file,
|
|
797
|
-
L:
|
|
892
|
+
L: 86,
|
|
798
893
|
S: void 0,
|
|
799
894
|
A: ["isJsonPath(jsonPath)", "`Invalid JsonPath: ${jsonPath}`"]
|
|
800
895
|
});
|
|
@@ -834,9 +929,9 @@ var setValue = (obj, path, value) => {
|
|
|
834
929
|
};
|
|
835
930
|
//#endregion
|
|
836
931
|
//#region src/internal/url.ts
|
|
837
|
-
var ParamKeyAnnotationId =
|
|
838
|
-
var getParamKeyAnnotation =
|
|
839
|
-
var ParamKeyAnnotation = (value) => (self) => self.
|
|
932
|
+
var ParamKeyAnnotationId = "@dxos/schema/annotation/ParamKey";
|
|
933
|
+
var getParamKeyAnnotation = (ast) => getAnnotation$1(ParamKeyAnnotationId)(ast);
|
|
934
|
+
var ParamKeyAnnotation = (value) => (self) => self.annotate({ [ParamKeyAnnotationId]: value });
|
|
840
935
|
/**
|
|
841
936
|
* HTTP params parser.
|
|
842
937
|
* Supports custom key serialization.
|
|
@@ -854,9 +949,11 @@ var UrlParser = class {
|
|
|
854
949
|
return Object.entries(this._schema.fields).reduce((params, [key, type]) => {
|
|
855
950
|
let value = url.searchParams.get(decamelize(key));
|
|
856
951
|
if (value == null) value = url.searchParams.get(key);
|
|
857
|
-
if (value != null)
|
|
858
|
-
|
|
859
|
-
|
|
952
|
+
if (value != null) {
|
|
953
|
+
if (isNumberKeyword(type.ast)) params[key] = parseInt(value);
|
|
954
|
+
else if (isBooleanKeyword(type.ast)) params[key] = value === "true" || value === "1";
|
|
955
|
+
else params[key] = value;
|
|
956
|
+
}
|
|
860
957
|
return params;
|
|
861
958
|
}, {});
|
|
862
959
|
}
|
|
@@ -869,7 +966,7 @@ var UrlParser = class {
|
|
|
869
966
|
if (value !== void 0) {
|
|
870
967
|
const field = this._schema.fields[key];
|
|
871
968
|
if (field) {
|
|
872
|
-
const
|
|
969
|
+
const serializedKey = getParamKeyAnnotation(field.ast)?.key ?? decamelize(key);
|
|
873
970
|
url.searchParams.set(serializedKey, String(value));
|
|
874
971
|
}
|
|
875
972
|
}
|
|
@@ -901,19 +998,20 @@ var SchemaEx_exports = /* @__PURE__ */ __exportAll({
|
|
|
901
998
|
getProperties: () => getProperties,
|
|
902
999
|
getValue: () => getValue,
|
|
903
1000
|
isArrayType: () => isArrayType,
|
|
1001
|
+
isArrays: () => isArrays,
|
|
904
1002
|
isDiscriminatedUnion: () => isDiscriminatedUnion,
|
|
905
1003
|
isJsonPath: () => isJsonPath,
|
|
906
1004
|
isLiteralUnion: () => isLiteralUnion,
|
|
907
1005
|
isNestedType: () => isNestedType,
|
|
908
1006
|
isOption: () => isOption,
|
|
909
|
-
isTupleType: () => isTupleType,
|
|
910
1007
|
mapAst: () => mapAst,
|
|
1008
|
+
retainContext: () => retainContext,
|
|
911
1009
|
setValue: () => setValue,
|
|
912
1010
|
splitJsonPath: () => splitJsonPath,
|
|
913
1011
|
unwrapOptional: () => unwrapOptional,
|
|
914
1012
|
visit: () => visit
|
|
915
1013
|
});
|
|
916
1014
|
//#endregion
|
|
917
|
-
export { dynamic_runtime_exports as DynamicRuntime, EffectEx_exports as EffectEx, Performance_exports as Performance, RuntimeProvider_exports as RuntimeProvider, SchemaEx_exports as SchemaEx, createKvsStore, layerOtel };
|
|
1015
|
+
export { dynamic_runtime_exports as DynamicRuntime, EffectEx_exports as EffectEx, GlobalValue_exports as GlobalValue, Performance_exports as Performance, RuntimeProvider_exports as RuntimeProvider, schema_ast_exports as SchemaAST, SchemaEx_exports as SchemaEx, createKvsStore, layerOtel };
|
|
918
1016
|
|
|
919
1017
|
//# sourceMappingURL=index.mjs.map
|