@dxos/effect 0.8.4-main.1da679c → 0.8.4-main.21d9917

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.
Files changed (71) hide show
  1. package/dist/lib/browser/chunk-CGS2ULMK.mjs +11 -0
  2. package/dist/lib/browser/chunk-CGS2ULMK.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +372 -231
  4. package/dist/lib/browser/index.mjs.map +4 -4
  5. package/dist/lib/browser/meta.json +1 -1
  6. package/dist/lib/browser/testing.mjs +38 -0
  7. package/dist/lib/browser/testing.mjs.map +7 -0
  8. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +11 -0
  9. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +7 -0
  10. package/dist/lib/node-esm/index.mjs +372 -231
  11. package/dist/lib/node-esm/index.mjs.map +4 -4
  12. package/dist/lib/node-esm/meta.json +1 -1
  13. package/dist/lib/node-esm/testing.mjs +38 -0
  14. package/dist/lib/node-esm/testing.mjs.map +7 -0
  15. package/dist/types/src/RuntimeProvider.d.ts +21 -0
  16. package/dist/types/src/RuntimeProvider.d.ts.map +1 -0
  17. package/dist/types/src/ast.d.ts +36 -22
  18. package/dist/types/src/ast.d.ts.map +1 -1
  19. package/dist/types/src/atom-kvs.d.ts +19 -0
  20. package/dist/types/src/atom-kvs.d.ts.map +1 -0
  21. package/dist/types/src/context.d.ts +2 -1
  22. package/dist/types/src/context.d.ts.map +1 -1
  23. package/dist/types/src/dynamic-runtime.d.ts +56 -0
  24. package/dist/types/src/dynamic-runtime.d.ts.map +1 -0
  25. package/dist/types/src/dynamic-runtime.test.d.ts +2 -0
  26. package/dist/types/src/dynamic-runtime.test.d.ts.map +1 -0
  27. package/dist/types/src/errors.d.ts +7 -1
  28. package/dist/types/src/errors.d.ts.map +1 -1
  29. package/dist/types/src/index.d.ts +5 -3
  30. package/dist/types/src/index.d.ts.map +1 -1
  31. package/dist/types/src/{jsonPath.d.ts → json-path.d.ts} +12 -4
  32. package/dist/types/src/json-path.d.ts.map +1 -0
  33. package/dist/types/src/json-path.test.d.ts +2 -0
  34. package/dist/types/src/json-path.test.d.ts.map +1 -0
  35. package/dist/types/src/otel.d.ts +17 -0
  36. package/dist/types/src/otel.d.ts.map +1 -0
  37. package/dist/types/src/otel.test.d.ts +2 -0
  38. package/dist/types/src/otel.test.d.ts.map +1 -0
  39. package/dist/types/src/resource.d.ts +6 -2
  40. package/dist/types/src/resource.d.ts.map +1 -1
  41. package/dist/types/src/testing.d.ts +24 -1
  42. package/dist/types/src/testing.d.ts.map +1 -1
  43. package/dist/types/src/url.d.ts +3 -1
  44. package/dist/types/src/url.d.ts.map +1 -1
  45. package/dist/types/tsconfig.tsbuildinfo +1 -1
  46. package/package.json +33 -9
  47. package/src/RuntimeProvider.ts +35 -0
  48. package/src/ast.test.ts +12 -9
  49. package/src/ast.ts +106 -89
  50. package/src/atom-kvs.ts +35 -0
  51. package/src/context.ts +2 -1
  52. package/src/dynamic-runtime.test.ts +465 -0
  53. package/src/dynamic-runtime.ts +195 -0
  54. package/src/errors.test.ts +1 -1
  55. package/src/errors.ts +25 -5
  56. package/src/index.ts +5 -3
  57. package/src/interrupt.test.ts +7 -6
  58. package/src/{jsonPath.test.ts → json-path.test.ts} +47 -8
  59. package/src/{jsonPath.ts → json-path.ts} +29 -4
  60. package/src/layers.test.ts +10 -4
  61. package/src/otel.test.ts +126 -0
  62. package/src/otel.ts +45 -0
  63. package/src/resource.test.ts +3 -3
  64. package/src/resource.ts +10 -5
  65. package/src/sanity.test.ts +30 -15
  66. package/src/testing.ts +29 -1
  67. package/src/url.test.ts +1 -1
  68. package/src/url.ts +5 -2
  69. package/dist/types/src/jsonPath.d.ts.map +0 -1
  70. package/dist/types/src/jsonPath.test.d.ts +0 -2
  71. package/dist/types/src/jsonPath.test.d.ts.map +0 -1
@@ -1,70 +1,59 @@
1
1
  import "@dxos/node-std/globals";
2
+ import {
3
+ __export
4
+ } from "./chunk-CGS2ULMK.mjs";
2
5
 
3
6
  // src/ast.ts
4
- import { Option, Schema, SchemaAST, pipe } from "effect";
5
- import { isUndefinedKeyword } from "effect/SchemaAST";
7
+ import * as Function from "effect/Function";
8
+ import * as Option from "effect/Option";
9
+ import * as Schema from "effect/Schema";
10
+ import * as SchemaAST from "effect/SchemaAST";
6
11
  import { invariant } from "@dxos/invariant";
7
12
  import { isNonNullable } from "@dxos/util";
8
13
  var __dxlog_file = "/__w/dxos/dxos/packages/common/effect/src/ast.ts";
9
- var isTupleType = (node) => {
10
- return SchemaAST.isTupleType(node) && node.elements.length > 0;
11
- };
12
- var getSimpleType = (node) => {
13
- if (SchemaAST.isDeclaration(node) || SchemaAST.isObjectKeyword(node) || SchemaAST.isTypeLiteral(node) || // TODO(wittjosiah): Tuples are actually arrays.
14
- isTupleType(node) || isDiscriminatedUnion(node)) {
15
- return "object";
16
- }
17
- if (SchemaAST.isStringKeyword(node)) {
18
- return "string";
19
- }
20
- if (SchemaAST.isNumberKeyword(node)) {
21
- return "number";
22
- }
23
- if (SchemaAST.isBooleanKeyword(node)) {
24
- return "boolean";
25
- }
26
- if (SchemaAST.isEnums(node)) {
27
- return "enum";
28
- }
29
- if (SchemaAST.isLiteral(node)) {
30
- return "literal";
31
- }
32
- };
33
- var isSimpleType = (node) => !!getSimpleType(node);
34
- (function(SimpleType2) {
35
- SimpleType2.getDefaultValue = (type) => {
36
- switch (type) {
37
- case "string": {
38
- return "";
14
+ var reduceRefinements = (type, refinements = []) => {
15
+ if (SchemaAST.isRefinement(type)) {
16
+ const annotations = type.annotations;
17
+ const filter = type.filter;
18
+ const nextType = {
19
+ ...type.from,
20
+ annotations: {
21
+ ...type.annotations,
22
+ ...annotations
39
23
  }
40
- case "number": {
41
- return 0;
42
- }
43
- case "boolean": {
44
- return false;
45
- }
46
- case "object": {
47
- return {};
48
- }
49
- default: {
50
- throw new Error(`Unsupported type for default value: ${type}`);
51
- }
52
- }
24
+ };
25
+ return reduceRefinements(nextType, [
26
+ ...refinements,
27
+ filter
28
+ ]);
29
+ }
30
+ return {
31
+ type,
32
+ refinements
53
33
  };
54
- })(SimpleType || (SimpleType = {}));
34
+ };
35
+ var getBaseType = (prop) => {
36
+ const encoded = SchemaAST.encodedBoundAST(prop.type);
37
+ const unwrapped = prop.isOptional && encoded._tag === "Union" ? encoded.types[0] : encoded;
38
+ return reduceRefinements(unwrapped);
39
+ };
40
+ var getProperties = (ast) => {
41
+ const properties = SchemaAST.getPropertySignatures(ast);
42
+ return properties.map((prop) => ({
43
+ ...getBaseType(prop),
44
+ name: prop.name,
45
+ isOptional: prop.isOptional,
46
+ isReadonly: prop.isReadonly
47
+ }));
48
+ };
55
49
  var VisitResult = /* @__PURE__ */ (function(VisitResult2) {
56
50
  VisitResult2[VisitResult2["CONTINUE"] = 0] = "CONTINUE";
57
51
  VisitResult2[VisitResult2["SKIP"] = 1] = "SKIP";
58
52
  VisitResult2[VisitResult2["EXIT"] = 2] = "EXIT";
59
53
  return VisitResult2;
60
54
  })({});
61
- var defaultTest = isSimpleType;
62
55
  var visit = (node, testOrVisitor, visitor) => {
63
- if (!visitor) {
64
- visitNode(node, defaultTest, testOrVisitor);
65
- } else {
66
- visitNode(node, testOrVisitor, visitor);
67
- }
56
+ visitNode(node, testOrVisitor, visitor);
68
57
  };
69
58
  var visitNode = (node, test, visitor, path = [], depth = 0) => {
70
59
  const $result = test?.(node, path, depth);
@@ -121,6 +110,12 @@ var findNode = (node, test) => {
121
110
  return child;
122
111
  }
123
112
  }
113
+ for (const prop of getIndexSignatures(node)) {
114
+ const child = findNode(prop.type, test);
115
+ if (child) {
116
+ return child;
117
+ }
118
+ }
124
119
  } else if (SchemaAST.isTupleType(node)) {
125
120
  for (const [_, element] of node.elements.entries()) {
126
121
  const child = findNode(element.type, test);
@@ -148,7 +143,7 @@ var findProperty = (schema, path) => {
148
143
  const typeNode = findNode(node, SchemaAST.isTypeLiteral);
149
144
  invariant(typeNode, void 0, {
150
145
  F: __dxlog_file,
151
- L: 257,
146
+ L: 235,
152
147
  S: void 0,
153
148
  A: [
154
149
  "typeNode",
@@ -173,16 +168,16 @@ var defaultAnnotations = {
173
168
  ["NumberKeyword"]: SchemaAST.numberKeyword,
174
169
  ["BooleanKeyword"]: SchemaAST.booleanKeyword
175
170
  };
176
- var getAnnotation = (annotationId, noDefault = true) => (node) => {
177
- const id = pipe(SchemaAST.getIdentifierAnnotation(node), Option.getOrUndefined);
178
- const value = pipe(SchemaAST.getAnnotation(annotationId)(node), Option.getOrUndefined);
171
+ var getAnnotation2 = (annotationId, noDefault = true) => (node) => {
172
+ const id = Function.pipe(SchemaAST.getIdentifierAnnotation(node), Option.getOrUndefined);
173
+ const value = Function.pipe(SchemaAST.getAnnotation(annotationId)(node), Option.getOrUndefined);
179
174
  if (noDefault && (value === defaultAnnotations[node._tag]?.annotations[annotationId] || value === id)) {
180
175
  return void 0;
181
176
  }
182
177
  return value;
183
178
  };
184
179
  var findAnnotation = (node, annotationId, noDefault = true) => {
185
- const getAnnotationById = getAnnotation(annotationId, noDefault);
180
+ const getAnnotationById = getAnnotation2(annotationId, noDefault);
186
181
  const getBaseAnnotation = (node2) => {
187
182
  const value = getAnnotationById(node2);
188
183
  if (value !== void 0) {
@@ -202,13 +197,22 @@ var isOption = (node) => {
202
197
  var isLiteralUnion = (node) => {
203
198
  return SchemaAST.isUnion(node) && node.types.every(SchemaAST.isLiteral);
204
199
  };
200
+ var isArrayType = (node) => {
201
+ return SchemaAST.isTupleType(node) && node.elements.length === 0 && node.rest.length === 1;
202
+ };
203
+ var getArrayElementType = (node) => {
204
+ return isArrayType(node) ? node.rest.at(0)?.type : void 0;
205
+ };
206
+ var isTupleType2 = (node) => {
207
+ return SchemaAST.isTupleType(node) && node.elements.length > 0;
208
+ };
205
209
  var isDiscriminatedUnion = (node) => {
206
210
  return SchemaAST.isUnion(node) && !!getDiscriminatingProps(node)?.length;
207
211
  };
208
212
  var getDiscriminatingProps = (node) => {
209
213
  invariant(SchemaAST.isUnion(node), void 0, {
210
214
  F: __dxlog_file,
211
- L: 354,
215
+ L: 353,
212
216
  S: void 0,
213
217
  A: [
214
218
  "SchemaAST.isUnion(node)",
@@ -226,7 +230,7 @@ var getDiscriminatingProps = (node) => {
226
230
  var getDiscriminatedType = (node, value = {}) => {
227
231
  invariant(SchemaAST.isUnion(node), void 0, {
228
232
  F: __dxlog_file,
229
- L: 378,
233
+ L: 377,
230
234
  S: void 0,
231
235
  A: [
232
236
  "SchemaAST.isUnion(node)",
@@ -235,7 +239,7 @@ var getDiscriminatedType = (node, value = {}) => {
235
239
  });
236
240
  invariant(value, void 0, {
237
241
  F: __dxlog_file,
238
- L: 379,
242
+ L: 378,
239
243
  S: void 0,
240
244
  A: [
241
245
  "value",
@@ -250,7 +254,7 @@ var getDiscriminatedType = (node, value = {}) => {
250
254
  const match = SchemaAST.getPropertySignatures(type).filter((prop) => props?.includes(prop.name.toString())).every((prop) => {
251
255
  invariant(SchemaAST.isLiteral(prop.type), void 0, {
252
256
  F: __dxlog_file,
253
- L: 390,
257
+ L: 389,
254
258
  S: void 0,
255
259
  A: [
256
260
  "SchemaAST.isLiteral(prop.type)",
@@ -268,7 +272,7 @@ var getDiscriminatedType = (node, value = {}) => {
268
272
  const literal = SchemaAST.getPropertySignatures(type).find((p) => p.name.toString() === prop);
269
273
  invariant(SchemaAST.isLiteral(literal.type), void 0, {
270
274
  F: __dxlog_file,
271
- L: 408,
275
+ L: 407,
272
276
  S: void 0,
273
277
  A: [
274
278
  "SchemaAST.isLiteral(literal.type)",
@@ -285,10 +289,14 @@ var getDiscriminatedType = (node, value = {}) => {
285
289
  const schema = Schema.Struct(fields);
286
290
  return schema.ast;
287
291
  };
292
+ var isNestedType = (node) => {
293
+ return SchemaAST.isDeclaration(node) || SchemaAST.isObjectKeyword(node) || SchemaAST.isTypeLiteral(node) || // TODO(wittjosiah): Tuples are actually arrays.
294
+ isTupleType2(node) || isDiscriminatedUnion(node);
295
+ };
288
296
  var mapAst = (ast, f) => {
289
297
  switch (ast._tag) {
290
298
  case "TypeLiteral": {
291
- return new SchemaAST.TypeLiteral(ast.propertySignatures.map((prop) => new SchemaAST.PropertySignature(prop.name, f(prop.type, prop.name), prop.isOptional, prop.isReadonly, prop.annotations)), ast.indexSignatures);
299
+ return new SchemaAST.TypeLiteral(ast.propertySignatures.map((prop) => new SchemaAST.PropertySignature(prop.name, f(prop.type, prop.name), prop.isOptional, prop.isReadonly, prop.annotations)), ast.indexSignatures, ast.annotations);
292
300
  }
293
301
  case "Union": {
294
302
  return SchemaAST.Union.make(ast.types.map(f), ast.annotations);
@@ -305,161 +313,76 @@ var mapAst = (ast, f) => {
305
313
  }
306
314
  }
307
315
  };
308
- var isArrayType = (node) => {
309
- return SchemaAST.isTupleType(node) || SchemaAST.isUnion(node) && node.types.some(isArrayType) && node.types.some(isUndefinedKeyword) && node.types.length === 2;
310
- };
311
- var SimpleType;
312
-
313
- // src/jsonPath.ts
314
- import { Option as Option2, Schema as Schema2 } from "effect";
315
- import { JSONPath } from "jsonpath-plus";
316
- import { invariant as invariant2 } from "@dxos/invariant";
317
- var __dxlog_file2 = "/__w/dxos/dxos/packages/common/effect/src/jsonPath.ts";
318
- var PATH_REGEX = /^($|[a-zA-Z_$][\w$]*(?:\.[a-zA-Z_$][\w$]*|\[\d+\](?:\.)?)*$)/;
319
- var PROP_REGEX = /^\w+$/;
320
- var JsonPath = Schema2.String.pipe(Schema2.pattern(PATH_REGEX)).annotations({
321
- title: "JSON path",
322
- description: "JSON path to a property"
323
- });
324
- var JsonProp = Schema2.NonEmptyString.pipe(Schema2.pattern(PROP_REGEX, {
325
- message: () => "Property name must contain only letters, numbers, and underscores"
326
- }));
327
- var isJsonPath = (value) => {
328
- return Option2.isSome(Schema2.validateOption(JsonPath)(value));
329
- };
330
- var createJsonPath = (path) => {
331
- const candidatePath = path.map((p, i) => {
332
- if (typeof p === "number") {
333
- return `[${p}]`;
334
- } else {
335
- return i === 0 ? p : `.${p}`;
336
- }
337
- }).join("");
338
- invariant2(isJsonPath(candidatePath), `Invalid JsonPath: ${candidatePath}`, {
339
- F: __dxlog_file2,
340
- L: 63,
341
- S: void 0,
342
- A: [
343
- "isJsonPath(candidatePath)",
344
- "`Invalid JsonPath: ${candidatePath}`"
345
- ]
346
- });
347
- return candidatePath;
348
- };
349
- var fromEffectValidationPath = (effectPath) => {
350
- const jsonPath = effectPath.replace(/\.\[(\d+)\]/g, "[$1]");
351
- invariant2(isJsonPath(jsonPath), `Invalid JsonPath: ${jsonPath}`, {
352
- F: __dxlog_file2,
353
- L: 74,
354
- S: void 0,
355
- A: [
356
- "isJsonPath(jsonPath)",
357
- "`Invalid JsonPath: ${jsonPath}`"
358
- ]
359
- });
360
- return jsonPath;
361
- };
362
- var splitJsonPath = (path) => {
363
- if (!isJsonPath(path)) {
364
- return [];
316
+ var getIndexSignatures = (ast) => {
317
+ const annotation = SchemaAST.getSurrogateAnnotation(ast);
318
+ if (Option.isSome(annotation)) {
319
+ return getIndexSignatures(annotation.value);
365
320
  }
366
- return path.match(/[a-zA-Z_$][\w$]*|\[\d+\]/g)?.map((part) => part.startsWith("[") ? part.replace(/[[\]]/g, "") : part) ?? [];
367
- };
368
- var getField = (object, path) => {
369
- return JSONPath({
370
- path,
371
- json: object
372
- })[0];
321
+ switch (ast._tag) {
322
+ case "TypeLiteral":
323
+ return ast.indexSignatures.slice();
324
+ case "Suspend":
325
+ return getIndexSignatures(ast.f());
326
+ case "Refinement":
327
+ return getIndexSignatures(ast.from);
328
+ }
329
+ return [];
373
330
  };
374
331
 
375
- // src/url.ts
376
- import { Option as Option3, SchemaAST as SchemaAST2, pipe as pipe2 } from "effect";
377
- import { decamelize } from "@dxos/util";
378
- function _define_property(obj, key, value) {
379
- if (key in obj) {
380
- Object.defineProperty(obj, key, {
381
- value,
382
- enumerable: true,
383
- configurable: true,
384
- writable: true
385
- });
386
- } else {
387
- obj[key] = value;
388
- }
389
- return obj;
390
- }
391
- var ParamKeyAnnotationId = Symbol.for("@dxos/schema/annotation/ParamKey");
392
- var getParamKeyAnnotation = SchemaAST2.getAnnotation(ParamKeyAnnotationId);
393
- var ParamKeyAnnotation = (value) => (self) => self.annotations({
394
- [ParamKeyAnnotationId]: value
395
- });
396
- var UrlParser = class {
397
- /**
398
- * Parse URL params.
399
- */
400
- parse(_url) {
401
- const url = new URL(_url);
402
- return Object.entries(this._schema.fields).reduce((params, [key, type]) => {
403
- let value = url.searchParams.get(decamelize(key));
404
- if (value == null) {
405
- value = url.searchParams.get(key);
406
- }
407
- if (value != null) {
408
- if (SchemaAST2.isNumberKeyword(type.ast)) {
409
- params[key] = parseInt(value);
410
- } else if (SchemaAST2.isBooleanKeyword(type.ast)) {
411
- params[key] = value === "true" || value === "1";
412
- } else {
413
- params[key] = value;
414
- }
415
- }
416
- return params;
417
- }, {});
418
- }
419
- /**
420
- * Return URL with encoded params.
421
- */
422
- create(_url, params) {
423
- const url = new URL(_url);
424
- Object.entries(params).forEach(([key, value]) => {
425
- if (value !== void 0) {
426
- const field = this._schema.fields[key];
427
- if (field) {
428
- const { key: serializedKey } = pipe2(getParamKeyAnnotation(field.ast), Option3.getOrElse(() => ({
429
- key: decamelize(key)
430
- })));
431
- url.searchParams.set(serializedKey, String(value));
432
- }
433
- }
434
- });
435
- return url;
436
- }
437
- constructor(_schema) {
438
- _define_property(this, "_schema", void 0);
439
- this._schema = _schema;
440
- }
332
+ // src/atom-kvs.ts
333
+ import * as BrowserKeyValueStore from "@effect/platform-browser/BrowserKeyValueStore";
334
+ import { Atom } from "@effect-atom/atom-react";
335
+ var defaultRuntime = Atom.runtime(BrowserKeyValueStore.layerLocalStorage);
336
+ var createKvsStore = (options) => {
337
+ const runtime2 = options.runtime ?? defaultRuntime;
338
+ return Atom.kvs({
339
+ runtime: runtime2,
340
+ key: options.key,
341
+ schema: options.schema,
342
+ defaultValue: options.defaultValue
343
+ }).pipe(Atom.keepAlive);
441
344
  };
442
345
 
443
346
  // src/context.ts
444
- import { Effect } from "effect";
347
+ import * as Effect from "effect/Effect";
445
348
  import { Context } from "@dxos/context";
446
- var __dxlog_file3 = "/__w/dxos/dxos/packages/common/effect/src/context.ts";
349
+ var __dxlog_file2 = "/__w/dxos/dxos/packages/common/effect/src/context.ts";
447
350
  var contextFromScope = () => Effect.gen(function* () {
448
351
  const ctx = new Context(void 0, {
449
- F: __dxlog_file3,
450
- L: 12
352
+ F: __dxlog_file2,
353
+ L: 13
451
354
  });
452
355
  yield* Effect.addFinalizer(() => Effect.promise(() => ctx.dispose()));
453
356
  return ctx;
454
357
  });
455
358
 
359
+ // src/dynamic-runtime.ts
360
+ var dynamic_runtime_exports = {};
361
+ __export(dynamic_runtime_exports, {
362
+ make: () => make
363
+ });
364
+ import * as Context2 from "effect/Context";
365
+ import * as Effect3 from "effect/Effect";
366
+ import * as Exit2 from "effect/Exit";
367
+ import * as Option3 from "effect/Option";
368
+ import * as Runtime2 from "effect/Runtime";
369
+
456
370
  // src/errors.ts
457
- import { Cause, Chunk, Effect as Effect2, Exit, GlobalValue, Option as Option4 } from "effect";
371
+ import * as Cause from "effect/Cause";
372
+ import * as Chunk from "effect/Chunk";
373
+ import * as Effect2 from "effect/Effect";
374
+ import * as Exit from "effect/Exit";
375
+ import * as GlobalValue from "effect/GlobalValue";
376
+ import * as Option2 from "effect/Option";
377
+ import * as Runtime from "effect/Runtime";
458
378
  var spanSymbol = Symbol.for("effect/SpanAnnotation");
459
379
  var originalSymbol = Symbol.for("effect/OriginalAnnotation");
460
380
  var spanToTrace = GlobalValue.globalValue("effect/Tracer/spanToTrace", () => /* @__PURE__ */ new WeakMap());
461
381
  var locationRegex = /\((.*)\)/g;
462
382
  var prettyErrorStack = (error, appendStacks = []) => {
383
+ if (typeof error !== "object" || error === null) {
384
+ return error;
385
+ }
463
386
  const span = error[spanSymbol];
464
387
  const lines = typeof error.stack === "string" ? error.stack.split("\n") : [];
465
388
  const out = [];
@@ -505,7 +428,7 @@ var prettyErrorStack = (error, appendStacks = []) => {
505
428
  } else {
506
429
  out.push(` at ${current.name}`);
507
430
  }
508
- current = Option4.getOrUndefined(current.parent);
431
+ current = Option2.getOrUndefined(current.parent);
509
432
  i++;
510
433
  }
511
434
  }
@@ -535,7 +458,7 @@ var causeToError = (cause) => {
535
458
  ...Chunk.toArray(Cause.defects(cause))
536
459
  ];
537
460
  const getStackFrames = () => {
538
- const o = {};
461
+ const o = new Error();
539
462
  Error.captureStackTrace(o, getStackFrames);
540
463
  return o.stack.split("\n").slice(1);
541
464
  };
@@ -561,6 +484,10 @@ var runAndForwardErrors = async (effect, options) => {
561
484
  const exit = await Effect2.runPromiseExit(effect, options);
562
485
  return unwrapExit(exit);
563
486
  };
487
+ var runInRuntime = async (runtime2, effect, options) => {
488
+ const exit = await Runtime.runPromiseExit(runtime2, effect, options);
489
+ return unwrapExit(exit);
490
+ };
564
491
  var promiseWithCauseCapture = (evaluate) => Effect2.promise(async (signal) => {
565
492
  try {
566
493
  const result = await evaluate(signal);
@@ -570,77 +497,291 @@ var promiseWithCauseCapture = (evaluate) => Effect2.promise(async (signal) => {
570
497
  }
571
498
  }).pipe(Effect2.flatten);
572
499
 
573
- // src/testing.ts
574
- import { Effect as Effect3 } from "effect";
575
- (function(TestHelpers2) {
576
- TestHelpers2.runIf = (condition) => (effect, ctx) => Effect3.gen(function* () {
577
- if (!condition) {
578
- ctx.skip();
579
- } else {
580
- return yield* effect;
500
+ // src/dynamic-runtime.ts
501
+ var validateTags = (context, tags) => Effect3.gen(function* () {
502
+ const missingTags = [];
503
+ for (const tag of tags) {
504
+ const option = Context2.getOption(context, tag);
505
+ if (Option3.isNone(option)) {
506
+ missingTags.push(tag.key);
581
507
  }
582
- });
583
- TestHelpers2.skipIf = (condition) => (effect, ctx) => Effect3.gen(function* () {
584
- if (condition) {
585
- ctx.skip();
586
- } else {
587
- return yield* effect;
508
+ }
509
+ if (missingTags.length > 0) {
510
+ return yield* Effect3.die(new Error(`Missing required tags in runtime: ${missingTags.join(", ")}`));
511
+ }
512
+ });
513
+ function make(managedRuntime, tags) {
514
+ const managedRuntimeAny = managedRuntime;
515
+ let cachedRuntime;
516
+ let validatedRuntimePromise;
517
+ const getValidatedRuntimeAsync = async () => {
518
+ if (!validatedRuntimePromise) {
519
+ validatedRuntimePromise = managedRuntimeAny.runPromise(Effect3.gen(function* () {
520
+ const rt = yield* managedRuntimeAny.runtimeEffect;
521
+ yield* validateTags(rt.context, tags);
522
+ return rt;
523
+ }));
588
524
  }
589
- });
590
- TestHelpers2.taggedTest = (tag) => (effect, ctx) => Effect3.gen(function* () {
591
- if (!process.env.DX_TEST_TAGS?.includes(tag)) {
592
- ctx.skip();
525
+ return validatedRuntimePromise;
526
+ };
527
+ const getValidatedRuntime = () => {
528
+ const validationExit = managedRuntimeAny.runSyncExit(Effect3.gen(function* () {
529
+ const rt = yield* managedRuntimeAny.runtimeEffect;
530
+ yield* validateTags(rt.context, tags);
531
+ return rt;
532
+ }));
533
+ return unwrapExit(validationExit);
534
+ };
535
+ return {
536
+ managedRuntime: managedRuntimeAny,
537
+ runPromise: async (effect) => {
538
+ const runtime2 = await getValidatedRuntimeAsync();
539
+ return Runtime2.runPromise(runtime2)(effect);
540
+ },
541
+ runSync: (effect) => {
542
+ const runtime2 = getValidatedRuntime();
543
+ return Runtime2.runSync(runtime2)(effect);
544
+ },
545
+ runSyncExit: (effect) => {
546
+ const validationExit = managedRuntimeAny.runSyncExit(Effect3.gen(function* () {
547
+ const rt = yield* managedRuntimeAny.runtimeEffect;
548
+ yield* validateTags(rt.context, tags);
549
+ return rt;
550
+ }));
551
+ if (Exit2.isSuccess(validationExit)) {
552
+ const runtime2 = validationExit.value;
553
+ return Runtime2.runSyncExit(runtime2)(effect);
554
+ }
555
+ return validationExit;
556
+ },
557
+ runPromiseExit: async (effect) => {
558
+ try {
559
+ const runtime2 = await getValidatedRuntimeAsync();
560
+ return Runtime2.runPromiseExit(runtime2)(effect);
561
+ } catch (error) {
562
+ return Exit2.die(error);
563
+ }
564
+ },
565
+ runFork: (effect) => {
566
+ const runtime2 = getValidatedRuntime();
567
+ return Runtime2.runFork(runtime2)(effect);
568
+ },
569
+ runtimeEffect: Effect3.gen(function* () {
570
+ if (cachedRuntime) {
571
+ return cachedRuntime;
572
+ }
573
+ const rt = yield* managedRuntimeAny.runtimeEffect;
574
+ yield* validateTags(rt.context, tags);
575
+ const runtime2 = rt;
576
+ cachedRuntime = runtime2;
577
+ return runtime2;
578
+ }).pipe(Effect3.catchAll(() => (
579
+ // This should never happen since validateTags uses Effect.die
580
+ Effect3.die(new Error("Unexpected error in runtimeEffect validation"))
581
+ ))),
582
+ dispose: async () => {
583
+ await managedRuntimeAny.dispose();
584
+ }
585
+ };
586
+ }
587
+
588
+ // src/json-path.ts
589
+ import * as Option4 from "effect/Option";
590
+ import * as Schema2 from "effect/Schema";
591
+ import { JSONPath } from "jsonpath-plus";
592
+ import { invariant as invariant2 } from "@dxos/invariant";
593
+ import { getDeep, setDeep } from "@dxos/util";
594
+ var __dxlog_file3 = "/__w/dxos/dxos/packages/common/effect/src/json-path.ts";
595
+ var PATH_REGEX = /^($|[a-zA-Z_$][\w$]*(?:\.[a-zA-Z_$][\w$]*|\[\d+\](?:\.)?)*$)/;
596
+ var PROP_REGEX = /^\w+$/;
597
+ var JsonPath = Schema2.String.pipe(Schema2.pattern(PATH_REGEX)).annotations({
598
+ title: "JSON path",
599
+ description: "JSON path to a property"
600
+ });
601
+ var JsonProp = Schema2.NonEmptyString.pipe(Schema2.pattern(PROP_REGEX, {
602
+ message: () => "Property name must contain only letters, numbers, and underscores"
603
+ }));
604
+ var isJsonPath = (value) => {
605
+ return Option4.isSome(Schema2.validateOption(JsonPath)(value));
606
+ };
607
+ var createJsonPath = (path) => {
608
+ const candidatePath = path.map((p, i) => {
609
+ if (typeof p === "number") {
610
+ return `[${p}]`;
593
611
  } else {
594
- return yield* effect;
612
+ return i === 0 ? p : `.${p}`;
595
613
  }
614
+ }).join("");
615
+ invariant2(isJsonPath(candidatePath), `Invalid JsonPath: ${candidatePath}`, {
616
+ F: __dxlog_file3,
617
+ L: 69,
618
+ S: void 0,
619
+ A: [
620
+ "isJsonPath(candidatePath)",
621
+ "`Invalid JsonPath: ${candidatePath}`"
622
+ ]
596
623
  });
597
- })(TestHelpers || (TestHelpers = {}));
598
- var TestHelpers;
624
+ return candidatePath;
625
+ };
626
+ var fromEffectValidationPath = (effectPath) => {
627
+ const jsonPath = effectPath.replace(/\.\[(\d+)\]/g, "[$1]");
628
+ invariant2(isJsonPath(jsonPath), `Invalid JsonPath: ${jsonPath}`, {
629
+ F: __dxlog_file3,
630
+ L: 80,
631
+ S: void 0,
632
+ A: [
633
+ "isJsonPath(jsonPath)",
634
+ "`Invalid JsonPath: ${jsonPath}`"
635
+ ]
636
+ });
637
+ return jsonPath;
638
+ };
639
+ var splitJsonPath = (path) => {
640
+ if (!isJsonPath(path)) {
641
+ return [];
642
+ }
643
+ return path.match(/[a-zA-Z_$][\w$]*|\[\d+\]/g)?.map((part) => part.replace(/[[\]]/g, "")).map((part) => {
644
+ const parsed = Number.parseInt(part, 10);
645
+ return Number.isNaN(parsed) ? part : parsed;
646
+ }) ?? [];
647
+ };
648
+ var getField = (object, path) => {
649
+ return JSONPath({
650
+ path,
651
+ json: object
652
+ })[0];
653
+ };
654
+ var getValue = (obj, path) => {
655
+ return getDeep(obj, splitJsonPath(path));
656
+ };
657
+ var setValue = (obj, path, value) => {
658
+ return setDeep(obj, splitJsonPath(path), value);
659
+ };
599
660
 
600
661
  // src/resource.ts
601
- import { Effect as Effect4 } from "effect";
602
- var accuireReleaseResource = (getResource) => Effect4.acquireRelease(Effect4.gen(function* () {
662
+ import * as Effect4 from "effect/Effect";
663
+ var acquireReleaseResource = (getResource) => Effect4.acquireRelease(Effect4.gen(function* () {
603
664
  const resource = getResource();
604
665
  yield* Effect4.promise(async () => {
605
- resource.open?.();
666
+ await resource.open?.();
606
667
  return void 0;
607
668
  });
608
669
  return resource;
609
670
  }), (resource) => Effect4.promise(async () => {
610
- resource.close?.();
671
+ await resource.close?.();
611
672
  return void 0;
612
673
  }));
674
+
675
+ // src/url.ts
676
+ import * as Function2 from "effect/Function";
677
+ import * as Option5 from "effect/Option";
678
+ import * as SchemaAST2 from "effect/SchemaAST";
679
+ import { decamelize } from "@dxos/util";
680
+ var ParamKeyAnnotationId = Symbol.for("@dxos/schema/annotation/ParamKey");
681
+ var getParamKeyAnnotation = SchemaAST2.getAnnotation(ParamKeyAnnotationId);
682
+ var ParamKeyAnnotation = (value) => (self) => self.annotations({
683
+ [ParamKeyAnnotationId]: value
684
+ });
685
+ var UrlParser = class {
686
+ _schema;
687
+ constructor(_schema) {
688
+ this._schema = _schema;
689
+ }
690
+ /**
691
+ * Parse URL params.
692
+ */
693
+ parse(_url) {
694
+ const url = new URL(_url);
695
+ return Object.entries(this._schema.fields).reduce((params, [key, type]) => {
696
+ let value = url.searchParams.get(decamelize(key));
697
+ if (value == null) {
698
+ value = url.searchParams.get(key);
699
+ }
700
+ if (value != null) {
701
+ if (SchemaAST2.isNumberKeyword(type.ast)) {
702
+ params[key] = parseInt(value);
703
+ } else if (SchemaAST2.isBooleanKeyword(type.ast)) {
704
+ params[key] = value === "true" || value === "1";
705
+ } else {
706
+ params[key] = value;
707
+ }
708
+ }
709
+ return params;
710
+ }, {});
711
+ }
712
+ /**
713
+ * Return URL with encoded params.
714
+ */
715
+ create(_url, params) {
716
+ const url = new URL(_url);
717
+ Object.entries(params).forEach(([key, value]) => {
718
+ if (value !== void 0) {
719
+ const field = this._schema.fields[key];
720
+ if (field) {
721
+ const { key: serializedKey } = Function2.pipe(getParamKeyAnnotation(field.ast), Option5.getOrElse(() => ({
722
+ key: decamelize(key)
723
+ })));
724
+ url.searchParams.set(serializedKey, String(value));
725
+ }
726
+ }
727
+ });
728
+ return url;
729
+ }
730
+ };
731
+
732
+ // src/RuntimeProvider.ts
733
+ var RuntimeProvider_exports = {};
734
+ __export(RuntimeProvider_exports, {
735
+ currentRuntime: () => currentRuntime,
736
+ provide: () => provide2,
737
+ runPromise: () => runPromise2
738
+ });
739
+ import * as Effect5 from "effect/Effect";
740
+ import * as Runtime3 from "effect/Runtime";
741
+ var currentRuntime = () => Effect5.runtime().pipe(Effect5.map(Effect5.succeed));
742
+ var runPromise2 = (provider) => async (effect) => {
743
+ const runtime2 = await runAndForwardErrors(provider);
744
+ return unwrapExit(await effect.pipe(Runtime3.runPromiseExit(runtime2)));
745
+ };
746
+ var provide2 = (runtimeProvider) => (effect) => Effect5.flatMap(runtimeProvider, (runtime2) => Effect5.provide(effect, runtime2));
613
747
  export {
748
+ dynamic_runtime_exports as DynamicRuntime,
614
749
  JsonPath,
615
750
  JsonProp,
616
751
  ParamKeyAnnotation,
617
- SimpleType,
618
- TestHelpers,
752
+ RuntimeProvider_exports as RuntimeProvider,
619
753
  UrlParser,
620
754
  VisitResult,
621
- accuireReleaseResource,
755
+ acquireReleaseResource,
622
756
  causeToError,
623
757
  contextFromScope,
624
758
  createJsonPath,
759
+ createKvsStore,
625
760
  findAnnotation,
626
761
  findNode,
627
762
  findProperty,
628
763
  fromEffectValidationPath,
629
- getAnnotation,
764
+ getAnnotation2 as getAnnotation,
765
+ getArrayElementType,
766
+ getBaseType,
630
767
  getDiscriminatedType,
631
768
  getDiscriminatingProps,
632
769
  getField,
633
770
  getParamKeyAnnotation,
634
- getSimpleType,
771
+ getProperties,
772
+ getValue,
635
773
  isArrayType,
636
774
  isDiscriminatedUnion,
637
775
  isJsonPath,
638
776
  isLiteralUnion,
777
+ isNestedType,
639
778
  isOption,
640
- isSimpleType,
779
+ isTupleType2 as isTupleType,
641
780
  mapAst,
642
781
  promiseWithCauseCapture,
643
782
  runAndForwardErrors,
783
+ runInRuntime,
784
+ setValue,
644
785
  splitJsonPath,
645
786
  throwCause,
646
787
  unwrapExit,