@dxos/effect 0.8.4-main.84f28bd → 0.8.4-main.b97322e

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/effect",
3
- "version": "0.8.4-main.84f28bd",
3
+ "version": "0.8.4-main.b97322e",
4
4
  "description": "Effect utils.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -24,14 +24,14 @@
24
24
  ],
25
25
  "dependencies": {
26
26
  "jsonpath-plus": "10.2.0",
27
- "@dxos/context": "0.8.4-main.84f28bd",
28
- "@dxos/invariant": "0.8.4-main.84f28bd",
29
- "@dxos/node-std": "0.8.4-main.84f28bd",
30
- "@dxos/util": "0.8.4-main.84f28bd"
27
+ "@dxos/context": "0.8.4-main.b97322e",
28
+ "@dxos/node-std": "0.8.4-main.b97322e",
29
+ "@dxos/util": "0.8.4-main.b97322e",
30
+ "@dxos/invariant": "0.8.4-main.b97322e"
31
31
  },
32
32
  "devDependencies": {
33
- "effect": "3.16.13",
34
- "@dxos/log": "0.8.4-main.84f28bd"
33
+ "effect": "3.17.0",
34
+ "@dxos/log": "0.8.4-main.b97322e"
35
35
  },
36
36
  "peerDependencies": {
37
37
  "effect": "^3.13.3"
package/src/errors.ts CHANGED
@@ -6,14 +6,16 @@ import { Cause, Chunk, Effect, Exit, GlobalValue, Option } from 'effect';
6
6
  import type { AnySpan, Span } from 'effect/Tracer';
7
7
 
8
8
  const spanSymbol = Symbol.for('effect/SpanAnnotation');
9
+ const originalSymbol = Symbol.for('effect/OriginalAnnotation');
9
10
  const spanToTrace = GlobalValue.globalValue('effect/Tracer/spanToTrace', () => new WeakMap());
10
11
  const locationRegex = /\((.*)\)/g;
11
12
 
12
13
  /**
13
14
  * Adds effect spans.
14
15
  * Removes effect internal functions.
16
+ * Unwraps error proxy.
15
17
  */
16
- const prettyErrorStack = (error: any, appendStacks: string[] = []): void => {
18
+ const prettyErrorStack = (error: any, appendStacks: string[] = []): any => {
17
19
  const span = error[spanSymbol];
18
20
 
19
21
  const lines = typeof error.stack === 'string' ? error.stack.split('\n') : [];
@@ -75,12 +77,21 @@ const prettyErrorStack = (error: any, appendStacks: string[] = []): void => {
75
77
 
76
78
  out.push(...appendStacks);
77
79
 
80
+ if (error[originalSymbol]) {
81
+ error = error[originalSymbol];
82
+ }
83
+ if (error.cause) {
84
+ error.cause = prettyErrorStack(error.cause);
85
+ }
86
+
78
87
  Object.defineProperty(error, 'stack', {
79
88
  value: out.join('\n'),
80
89
  writable: true,
81
90
  enumerable: false,
82
91
  configurable: true,
83
92
  });
93
+
94
+ return error;
84
95
  };
85
96
 
86
97
  /**
@@ -116,14 +127,12 @@ export const runAndForwardErrors = async <A, E>(
116
127
  };
117
128
 
118
129
  const stackFrames = getStackFrames();
119
- for (const error of errors) {
120
- prettyErrorStack(error, stackFrames);
121
- }
130
+ const newErrors = errors.map((error) => prettyErrorStack(error, stackFrames));
122
131
 
123
- if (errors.length === 1) {
124
- throw errors[0];
132
+ if (newErrors.length === 1) {
133
+ throw newErrors[0];
125
134
  } else {
126
- throw new AggregateError(errors);
135
+ throw new AggregateError(newErrors);
127
136
  }
128
137
  }
129
138
  };
package/src/index.ts CHANGED
@@ -7,3 +7,5 @@ export * from './jsonPath';
7
7
  export * from './url';
8
8
  export * from './context';
9
9
  export * from './errors';
10
+ export * from './testing';
11
+ export * from './resource';
@@ -0,0 +1,31 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { it } from '@effect/vitest';
6
+ import { Effect } from 'effect';
7
+
8
+ import { accuireReleaseResource } from './resource';
9
+
10
+ it.effect(
11
+ 'acquire-release',
12
+ Effect.fn(function* ({ expect }) {
13
+ const events: string[] = [];
14
+
15
+ const makeResource = accuireReleaseResource(() => ({
16
+ open: () => {
17
+ events.push('open');
18
+ },
19
+ close: () => {
20
+ events.push('close');
21
+ },
22
+ }));
23
+ yield* Effect.gen(function* () {
24
+ events.push('1');
25
+ const _resource = yield* makeResource;
26
+ events.push('2');
27
+ }).pipe(Effect.scoped);
28
+ events.push('3');
29
+ expect(events).to.deep.equal(['1', 'open', '2', 'close', '3']);
30
+ }),
31
+ );
@@ -0,0 +1,25 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { Effect } from 'effect';
6
+
7
+ import type { Lifecycle } from '@dxos/context';
8
+
9
+ // TODO(dmaretskyi): Extract to effect-utils.
10
+ export const accuireReleaseResource = <T extends Lifecycle>(getResource: () => T) =>
11
+ Effect.acquireRelease(
12
+ Effect.gen(function* () {
13
+ const resource = getResource();
14
+ yield* Effect.promise(async () => {
15
+ resource.open?.();
16
+ return undefined;
17
+ });
18
+ return resource;
19
+ }),
20
+ (resource) =>
21
+ Effect.promise(async () => {
22
+ resource.close?.();
23
+ return undefined;
24
+ }),
25
+ );
package/src/testing.ts ADDED
@@ -0,0 +1,58 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { Effect } from 'effect';
6
+ import type { TestContext } from 'vitest';
7
+
8
+ export namespace TestHelpers {
9
+ /**
10
+ * Skip the test if the condition is false.
11
+ *
12
+ * Exmaple:
13
+ * ```ts
14
+ * it.effect(
15
+ * 'should process an agentic loop using Claude',
16
+ * Effect.fn(function* ({ expect }) {
17
+ * // ...
18
+ * }),
19
+ * TestHelpers.runIf(process.env.ANTHROPIC_API_KEY),
20
+ * );
21
+ * ```
22
+ */
23
+ export const runIf =
24
+ (condition: unknown) =>
25
+ <A, E, R>(effect: Effect.Effect<A, E, R>, ctx: TestContext): Effect.Effect<A, E, R> =>
26
+ Effect.gen(function* () {
27
+ if (!condition) {
28
+ ctx.skip();
29
+ } else {
30
+ return yield* effect;
31
+ }
32
+ });
33
+
34
+ /**
35
+ * Skip the test if the condition is true.
36
+ *
37
+ * Exmaple:
38
+ * ```ts
39
+ * it.effect(
40
+ * 'should process an agentic loop using Claude',
41
+ * Effect.fn(function* ({ expect }) {
42
+ * // ...
43
+ * }),
44
+ * TestHelpers.skipIf(!process.env.ANTHROPIC_API_KEY),
45
+ * );
46
+ * ```
47
+ */
48
+ export const skipIf =
49
+ (condition: unknown) =>
50
+ <A, E, R>(effect: Effect.Effect<A, E, R>, ctx: TestContext): Effect.Effect<A, E, R> =>
51
+ Effect.gen(function* () {
52
+ if (condition) {
53
+ ctx.skip();
54
+ } else {
55
+ return yield* effect;
56
+ }
57
+ });
58
+ }