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

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