@dxos/effect 0.8.4-main.ead640a → 0.8.4-main.ef1bc66f44

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