@dxos/effect 0.8.4-main.67995b8 → 0.8.4-main.70d3990

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 (55) hide show
  1. package/dist/lib/browser/index.mjs +132 -53
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/node-esm/index.mjs +132 -53
  5. package/dist/lib/node-esm/index.mjs.map +4 -4
  6. package/dist/lib/node-esm/meta.json +1 -1
  7. package/dist/types/src/ast.d.ts +3 -2
  8. package/dist/types/src/ast.d.ts.map +1 -1
  9. package/dist/types/src/context.d.ts +2 -1
  10. package/dist/types/src/context.d.ts.map +1 -1
  11. package/dist/types/src/errors.d.ts +31 -1
  12. package/dist/types/src/errors.d.ts.map +1 -1
  13. package/dist/types/src/index.d.ts +1 -1
  14. package/dist/types/src/index.d.ts.map +1 -1
  15. package/dist/types/src/interrupt.test.d.ts +2 -0
  16. package/dist/types/src/interrupt.test.d.ts.map +1 -0
  17. package/dist/types/src/{jsonPath.d.ts → json-path.d.ts} +10 -2
  18. package/dist/types/src/json-path.d.ts.map +1 -0
  19. package/dist/types/src/json-path.test.d.ts +2 -0
  20. package/dist/types/src/json-path.test.d.ts.map +1 -0
  21. package/dist/types/src/layers.test.d.ts +2 -0
  22. package/dist/types/src/layers.test.d.ts.map +1 -0
  23. package/dist/types/src/otel.d.ts +17 -0
  24. package/dist/types/src/otel.d.ts.map +1 -0
  25. package/dist/types/src/otel.test.d.ts +2 -0
  26. package/dist/types/src/otel.test.d.ts.map +1 -0
  27. package/dist/types/src/resource.d.ts +6 -2
  28. package/dist/types/src/resource.d.ts.map +1 -1
  29. package/dist/types/src/testing.d.ts +33 -1
  30. package/dist/types/src/testing.d.ts.map +1 -1
  31. package/dist/types/src/url.d.ts +3 -1
  32. package/dist/types/src/url.d.ts.map +1 -1
  33. package/dist/types/tsconfig.tsbuildinfo +1 -1
  34. package/package.json +17 -9
  35. package/src/ast.test.ts +5 -4
  36. package/src/ast.ts +52 -17
  37. package/src/context.ts +2 -1
  38. package/src/errors.test.ts +1 -1
  39. package/src/errors.ts +73 -20
  40. package/src/index.ts +1 -1
  41. package/src/interrupt.test.ts +33 -0
  42. package/src/{jsonPath.test.ts → json-path.test.ts} +40 -1
  43. package/src/{jsonPath.ts → json-path.ts} +29 -1
  44. package/src/layers.test.ts +110 -0
  45. package/src/otel.test.ts +126 -0
  46. package/src/otel.ts +45 -0
  47. package/src/resource.test.ts +5 -4
  48. package/src/resource.ts +8 -3
  49. package/src/sanity.test.ts +24 -11
  50. package/src/testing.ts +53 -1
  51. package/src/url.test.ts +1 -1
  52. package/src/url.ts +5 -2
  53. package/dist/types/src/jsonPath.d.ts.map +0 -1
  54. package/dist/types/src/jsonPath.test.d.ts +0 -2
  55. package/dist/types/src/jsonPath.test.d.ts.map +0 -1
@@ -1,13 +1,19 @@
1
1
  import "@dxos/node-std/globals";
2
2
 
3
3
  // src/ast.ts
4
- import { Option, pipe, SchemaAST, Schema } from "effect";
5
- import { isUndefinedKeyword } from "effect/SchemaAST";
4
+ import * as Function from "effect/Function";
5
+ import * as Option from "effect/Option";
6
+ import * as Schema from "effect/Schema";
7
+ import * as SchemaAST from "effect/SchemaAST";
6
8
  import { invariant } from "@dxos/invariant";
7
9
  import { isNonNullable } from "@dxos/util";
8
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
+ };
9
14
  var getSimpleType = (node) => {
10
- if (SchemaAST.isDeclaration(node) || SchemaAST.isObjectKeyword(node) || SchemaAST.isTypeLiteral(node) || isDiscriminatedUnion(node)) {
15
+ if (SchemaAST.isDeclaration(node) || SchemaAST.isObjectKeyword(node) || SchemaAST.isTypeLiteral(node) || // TODO(wittjosiah): Tuples are actually arrays.
16
+ isTupleType2(node) || isDiscriminatedUnion(node)) {
11
17
  return "object";
12
18
  }
13
19
  if (SchemaAST.isStringKeyword(node)) {
@@ -48,12 +54,12 @@ var isSimpleType = (node) => !!getSimpleType(node);
48
54
  }
49
55
  };
50
56
  })(SimpleType || (SimpleType = {}));
51
- var VisitResult = /* @__PURE__ */ function(VisitResult2) {
57
+ var VisitResult = /* @__PURE__ */ (function(VisitResult2) {
52
58
  VisitResult2[VisitResult2["CONTINUE"] = 0] = "CONTINUE";
53
59
  VisitResult2[VisitResult2["SKIP"] = 1] = "SKIP";
54
60
  VisitResult2[VisitResult2["EXIT"] = 2] = "EXIT";
55
61
  return VisitResult2;
56
- }({});
62
+ })({});
57
63
  var defaultTest = isSimpleType;
58
64
  var visit = (node, testOrVisitor, visitor) => {
59
65
  if (!visitor) {
@@ -63,8 +69,8 @@ var visit = (node, testOrVisitor, visitor) => {
63
69
  }
64
70
  };
65
71
  var visitNode = (node, test, visitor, path = [], depth = 0) => {
66
- const _result = test?.(node, path, depth);
67
- const result = _result === void 0 ? 0 : typeof _result === "boolean" ? _result ? 0 : 1 : _result;
72
+ const $result = test?.(node, path, depth);
73
+ const result = $result === void 0 ? 0 : typeof $result === "boolean" ? $result ? 0 : 1 : $result;
68
74
  if (result === 2) {
69
75
  return result;
70
76
  }
@@ -117,6 +123,12 @@ var findNode = (node, test) => {
117
123
  return child;
118
124
  }
119
125
  }
126
+ for (const prop of getIndexSignatures(node)) {
127
+ const child = findNode(prop.type, test);
128
+ if (child) {
129
+ return child;
130
+ }
131
+ }
120
132
  } else if (SchemaAST.isTupleType(node)) {
121
133
  for (const [_, element] of node.elements.entries()) {
122
134
  const child = findNode(element.type, test);
@@ -125,12 +137,13 @@ var findNode = (node, test) => {
125
137
  }
126
138
  }
127
139
  } else if (SchemaAST.isUnion(node)) {
128
- if (isOption(node)) {
129
- for (const type of node.types) {
130
- const child = findNode(type, test);
131
- if (child) {
132
- return child;
133
- }
140
+ if (isLiteralUnion(node)) {
141
+ return void 0;
142
+ }
143
+ for (const type of node.types) {
144
+ const child = findNode(type, test);
145
+ if (child) {
146
+ return child;
134
147
  }
135
148
  }
136
149
  } else if (SchemaAST.isRefinement(node)) {
@@ -143,7 +156,7 @@ var findProperty = (schema, path) => {
143
156
  const typeNode = findNode(node, SchemaAST.isTypeLiteral);
144
157
  invariant(typeNode, void 0, {
145
158
  F: __dxlog_file,
146
- L: 247,
159
+ L: 265,
147
160
  S: void 0,
148
161
  A: [
149
162
  "typeNode",
@@ -168,16 +181,16 @@ var defaultAnnotations = {
168
181
  ["NumberKeyword"]: SchemaAST.numberKeyword,
169
182
  ["BooleanKeyword"]: SchemaAST.booleanKeyword
170
183
  };
171
- var getAnnotation = (annotationId, noDefault = true) => (node) => {
172
- const id = pipe(SchemaAST.getIdentifierAnnotation(node), Option.getOrUndefined);
173
- const value = pipe(SchemaAST.getAnnotation(annotationId)(node), Option.getOrUndefined);
184
+ var getAnnotation2 = (annotationId, noDefault = true) => (node) => {
185
+ const id = Function.pipe(SchemaAST.getIdentifierAnnotation(node), Option.getOrUndefined);
186
+ const value = Function.pipe(SchemaAST.getAnnotation(annotationId)(node), Option.getOrUndefined);
174
187
  if (noDefault && (value === defaultAnnotations[node._tag]?.annotations[annotationId] || value === id)) {
175
188
  return void 0;
176
189
  }
177
190
  return value;
178
191
  };
179
192
  var findAnnotation = (node, annotationId, noDefault = true) => {
180
- const getAnnotationById = getAnnotation(annotationId, noDefault);
193
+ const getAnnotationById = getAnnotation2(annotationId, noDefault);
181
194
  const getBaseAnnotation = (node2) => {
182
195
  const value = getAnnotationById(node2);
183
196
  if (value !== void 0) {
@@ -203,7 +216,7 @@ var isDiscriminatedUnion = (node) => {
203
216
  var getDiscriminatingProps = (node) => {
204
217
  invariant(SchemaAST.isUnion(node), void 0, {
205
218
  F: __dxlog_file,
206
- L: 344,
219
+ L: 362,
207
220
  S: void 0,
208
221
  A: [
209
222
  "SchemaAST.isUnion(node)",
@@ -221,7 +234,7 @@ var getDiscriminatingProps = (node) => {
221
234
  var getDiscriminatedType = (node, value = {}) => {
222
235
  invariant(SchemaAST.isUnion(node), void 0, {
223
236
  F: __dxlog_file,
224
- L: 368,
237
+ L: 386,
225
238
  S: void 0,
226
239
  A: [
227
240
  "SchemaAST.isUnion(node)",
@@ -230,7 +243,7 @@ var getDiscriminatedType = (node, value = {}) => {
230
243
  });
231
244
  invariant(value, void 0, {
232
245
  F: __dxlog_file,
233
- L: 369,
246
+ L: 387,
234
247
  S: void 0,
235
248
  A: [
236
249
  "value",
@@ -245,7 +258,7 @@ var getDiscriminatedType = (node, value = {}) => {
245
258
  const match = SchemaAST.getPropertySignatures(type).filter((prop) => props?.includes(prop.name.toString())).every((prop) => {
246
259
  invariant(SchemaAST.isLiteral(prop.type), void 0, {
247
260
  F: __dxlog_file,
248
- L: 380,
261
+ L: 398,
249
262
  S: void 0,
250
263
  A: [
251
264
  "SchemaAST.isLiteral(prop.type)",
@@ -263,7 +276,7 @@ var getDiscriminatedType = (node, value = {}) => {
263
276
  const literal = SchemaAST.getPropertySignatures(type).find((p) => p.name.toString() === prop);
264
277
  invariant(SchemaAST.isLiteral(literal.type), void 0, {
265
278
  F: __dxlog_file,
266
- L: 398,
279
+ L: 416,
267
280
  S: void 0,
268
281
  A: [
269
282
  "SchemaAST.isLiteral(literal.type)",
@@ -283,7 +296,7 @@ var getDiscriminatedType = (node, value = {}) => {
283
296
  var mapAst = (ast, f) => {
284
297
  switch (ast._tag) {
285
298
  case "TypeLiteral": {
286
- 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);
287
300
  }
288
301
  case "Union": {
289
302
  return SchemaAST.Union.make(ast.types.map(f), ast.annotations);
@@ -301,15 +314,32 @@ var mapAst = (ast, f) => {
301
314
  }
302
315
  };
303
316
  var isArrayType = (node) => {
304
- return SchemaAST.isTupleType(node) || SchemaAST.isUnion(node) && node.types.some(isArrayType) && node.types.some(isUndefinedKeyword) && node.types.length === 2;
317
+ return SchemaAST.isTupleType(node) || SchemaAST.isUnion(node) && node.types.some(isArrayType) && node.types.some(SchemaAST.isUndefinedKeyword) && node.types.length === 2;
318
+ };
319
+ var getIndexSignatures = (ast) => {
320
+ const annotation = SchemaAST.getSurrogateAnnotation(ast);
321
+ if (Option.isSome(annotation)) {
322
+ return getIndexSignatures(annotation.value);
323
+ }
324
+ switch (ast._tag) {
325
+ case "TypeLiteral":
326
+ return ast.indexSignatures.slice();
327
+ case "Suspend":
328
+ return getIndexSignatures(ast.f());
329
+ case "Refinement":
330
+ return getIndexSignatures(ast.from);
331
+ }
332
+ return [];
305
333
  };
306
334
  var SimpleType;
307
335
 
308
- // src/jsonPath.ts
309
- import { Schema as Schema2, Option as Option2 } from "effect";
336
+ // src/json-path.ts
337
+ import * as Option2 from "effect/Option";
338
+ import * as Schema2 from "effect/Schema";
310
339
  import { JSONPath } from "jsonpath-plus";
311
340
  import { invariant as invariant2 } from "@dxos/invariant";
312
- var __dxlog_file2 = "/__w/dxos/dxos/packages/common/effect/src/jsonPath.ts";
341
+ import { getDeep, setDeep } from "@dxos/util";
342
+ var __dxlog_file2 = "/__w/dxos/dxos/packages/common/effect/src/json-path.ts";
313
343
  var PATH_REGEX = /^($|[a-zA-Z_$][\w$]*(?:\.[a-zA-Z_$][\w$]*|\[\d+\](?:\.)?)*$)/;
314
344
  var PROP_REGEX = /^\w+$/;
315
345
  var JsonPath = Schema2.String.pipe(Schema2.pattern(PATH_REGEX)).annotations({
@@ -332,7 +362,7 @@ var createJsonPath = (path) => {
332
362
  }).join("");
333
363
  invariant2(isJsonPath(candidatePath), `Invalid JsonPath: ${candidatePath}`, {
334
364
  F: __dxlog_file2,
335
- L: 63,
365
+ L: 69,
336
366
  S: void 0,
337
367
  A: [
338
368
  "isJsonPath(candidatePath)",
@@ -345,7 +375,7 @@ var fromEffectValidationPath = (effectPath) => {
345
375
  const jsonPath = effectPath.replace(/\.\[(\d+)\]/g, "[$1]");
346
376
  invariant2(isJsonPath(jsonPath), `Invalid JsonPath: ${jsonPath}`, {
347
377
  F: __dxlog_file2,
348
- L: 74,
378
+ L: 80,
349
379
  S: void 0,
350
380
  A: [
351
381
  "isJsonPath(jsonPath)",
@@ -366,9 +396,17 @@ var getField = (object, path) => {
366
396
  json: object
367
397
  })[0];
368
398
  };
399
+ var getValue = (obj, path) => {
400
+ return getDeep(obj, splitJsonPath(path).map((p) => p.replace(/[[\]]/g, "")));
401
+ };
402
+ var setValue = (obj, path, value) => {
403
+ return setDeep(obj, splitJsonPath(path).map((p) => p.replace(/[[\]]/g, "")), value);
404
+ };
369
405
 
370
406
  // src/url.ts
371
- import { SchemaAST as SchemaAST2, Option as Option3, pipe as pipe2 } from "effect";
407
+ import * as Function2 from "effect/Function";
408
+ import * as Option3 from "effect/Option";
409
+ import * as SchemaAST2 from "effect/SchemaAST";
372
410
  import { decamelize } from "@dxos/util";
373
411
  var ParamKeyAnnotationId = Symbol.for("@dxos/schema/annotation/ParamKey");
374
412
  var getParamKeyAnnotation = SchemaAST2.getAnnotation(ParamKeyAnnotationId);
@@ -376,6 +414,7 @@ var ParamKeyAnnotation = (value) => (self) => self.annotations({
376
414
  [ParamKeyAnnotationId]: value
377
415
  });
378
416
  var UrlParser = class {
417
+ _schema;
379
418
  constructor(_schema) {
380
419
  this._schema = _schema;
381
420
  }
@@ -410,7 +449,7 @@ var UrlParser = class {
410
449
  if (value !== void 0) {
411
450
  const field = this._schema.fields[key];
412
451
  if (field) {
413
- const { key: serializedKey } = pipe2(getParamKeyAnnotation(field.ast), Option3.getOrElse(() => ({
452
+ const { key: serializedKey } = Function2.pipe(getParamKeyAnnotation(field.ast), Option3.getOrElse(() => ({
414
453
  key: decamelize(key)
415
454
  })));
416
455
  url.searchParams.set(serializedKey, String(value));
@@ -422,20 +461,25 @@ var UrlParser = class {
422
461
  };
423
462
 
424
463
  // src/context.ts
425
- import { Effect } from "effect";
464
+ import * as Effect from "effect/Effect";
426
465
  import { Context } from "@dxos/context";
427
466
  var __dxlog_file3 = "/__w/dxos/dxos/packages/common/effect/src/context.ts";
428
467
  var contextFromScope = () => Effect.gen(function* () {
429
468
  const ctx = new Context(void 0, {
430
469
  F: __dxlog_file3,
431
- L: 12
470
+ L: 13
432
471
  });
433
472
  yield* Effect.addFinalizer(() => Effect.promise(() => ctx.dispose()));
434
473
  return ctx;
435
474
  });
436
475
 
437
476
  // src/errors.ts
438
- import { Cause, Chunk, Effect as Effect2, Exit, GlobalValue, Option as Option4 } from "effect";
477
+ import * as Cause from "effect/Cause";
478
+ import * as Chunk from "effect/Chunk";
479
+ import * as Effect2 from "effect/Effect";
480
+ import * as Exit from "effect/Exit";
481
+ import * as GlobalValue from "effect/GlobalValue";
482
+ import * as Option4 from "effect/Option";
439
483
  var spanSymbol = Symbol.for("effect/SpanAnnotation");
440
484
  var originalSymbol = Symbol.for("effect/OriginalAnnotation");
441
485
  var spanToTrace = GlobalValue.globalValue("effect/Tracer/spanToTrace", () => /* @__PURE__ */ new WeakMap());
@@ -505,19 +549,15 @@ var prettyErrorStack = (error, appendStacks = []) => {
505
549
  });
506
550
  return error;
507
551
  };
508
- var runAndForwardErrors = async (effect, options) => {
509
- const exit = await Effect2.runPromiseExit(effect, options);
510
- if (Exit.isSuccess(exit)) {
511
- return exit.value;
512
- }
513
- if (Cause.isEmpty(exit.cause)) {
514
- throw new Error("Fiber failed without a cause");
515
- } else if (Cause.isInterrupted(exit.cause)) {
516
- throw new Error("Fiber was interrupted");
552
+ var causeToError = (cause) => {
553
+ if (Cause.isEmpty(cause)) {
554
+ return new Error("Fiber failed without a cause");
555
+ } else if (Cause.isInterruptedOnly(cause)) {
556
+ return new Error("Fiber was interrupted");
517
557
  } else {
518
558
  const errors = [
519
- ...Chunk.toArray(Cause.failures(exit.cause)),
520
- ...Chunk.toArray(Cause.defects(exit.cause))
559
+ ...Chunk.toArray(Cause.failures(cause)),
560
+ ...Chunk.toArray(Cause.defects(cause))
521
561
  ];
522
562
  const getStackFrames = () => {
523
563
  const o = {};
@@ -527,15 +567,37 @@ var runAndForwardErrors = async (effect, options) => {
527
567
  const stackFrames = getStackFrames();
528
568
  const newErrors = errors.map((error) => prettyErrorStack(error, stackFrames));
529
569
  if (newErrors.length === 1) {
530
- throw newErrors[0];
570
+ return newErrors[0];
531
571
  } else {
532
- throw new AggregateError(newErrors);
572
+ return new AggregateError(newErrors);
533
573
  }
534
574
  }
535
575
  };
576
+ var throwCause = (cause) => {
577
+ throw causeToError(cause);
578
+ };
579
+ var unwrapExit = (exit) => {
580
+ if (Exit.isSuccess(exit)) {
581
+ return exit.value;
582
+ }
583
+ return throwCause(exit.cause);
584
+ };
585
+ var runAndForwardErrors = async (effect, options) => {
586
+ const exit = await Effect2.runPromiseExit(effect, options);
587
+ return unwrapExit(exit);
588
+ };
589
+ var promiseWithCauseCapture = (evaluate) => Effect2.promise(async (signal) => {
590
+ try {
591
+ const result = await evaluate(signal);
592
+ return Effect2.succeed(result);
593
+ } catch (err) {
594
+ return Effect2.die(err);
595
+ }
596
+ }).pipe(Effect2.flatten);
536
597
 
537
598
  // src/testing.ts
538
- import { Effect as Effect3 } from "effect";
599
+ import * as Context2 from "effect/Context";
600
+ import * as Effect3 from "effect/Effect";
539
601
  (function(TestHelpers2) {
540
602
  TestHelpers2.runIf = (condition) => (effect, ctx) => Effect3.gen(function* () {
541
603
  if (!condition) {
@@ -551,12 +613,22 @@ import { Effect as Effect3 } from "effect";
551
613
  return yield* effect;
552
614
  }
553
615
  });
616
+ TestHelpers2.taggedTest = (tag) => (effect, ctx) => Effect3.gen(function* () {
617
+ if (!process.env.DX_TEST_TAGS?.includes(tag)) {
618
+ ctx.skip();
619
+ } else {
620
+ return yield* effect;
621
+ }
622
+ });
623
+ TestHelpers2.provideTestContext = (effect, ctx) => Effect3.provideService(effect, TestContextService, ctx);
554
624
  })(TestHelpers || (TestHelpers = {}));
625
+ var TestContextService = class extends Context2.Tag("@dxos/effect/TestContextService")() {
626
+ };
555
627
  var TestHelpers;
556
628
 
557
629
  // src/resource.ts
558
- import { Effect as Effect4 } from "effect";
559
- var accuireReleaseResource = (getResource) => Effect4.acquireRelease(Effect4.gen(function* () {
630
+ import * as Effect4 from "effect/Effect";
631
+ var acquireReleaseResource = (getResource) => Effect4.acquireRelease(Effect4.gen(function* () {
560
632
  const resource = getResource();
561
633
  yield* Effect4.promise(async () => {
562
634
  resource.open?.();
@@ -572,22 +644,25 @@ export {
572
644
  JsonProp,
573
645
  ParamKeyAnnotation,
574
646
  SimpleType,
647
+ TestContextService,
575
648
  TestHelpers,
576
649
  UrlParser,
577
650
  VisitResult,
578
- accuireReleaseResource,
651
+ acquireReleaseResource,
652
+ causeToError,
579
653
  contextFromScope,
580
654
  createJsonPath,
581
655
  findAnnotation,
582
656
  findNode,
583
657
  findProperty,
584
658
  fromEffectValidationPath,
585
- getAnnotation,
659
+ getAnnotation2 as getAnnotation,
586
660
  getDiscriminatedType,
587
661
  getDiscriminatingProps,
588
662
  getField,
589
663
  getParamKeyAnnotation,
590
664
  getSimpleType,
665
+ getValue,
591
666
  isArrayType,
592
667
  isDiscriminatedUnion,
593
668
  isJsonPath,
@@ -595,8 +670,12 @@ export {
595
670
  isOption,
596
671
  isSimpleType,
597
672
  mapAst,
673
+ promiseWithCauseCapture,
598
674
  runAndForwardErrors,
675
+ setValue,
599
676
  splitJsonPath,
677
+ throwCause,
678
+ unwrapExit,
600
679
  visit
601
680
  };
602
681
  //# sourceMappingURL=index.mjs.map