@dxos/effect 0.8.4-main.f9ba587 → 0.8.4-main.fd6878d

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.f9ba587",
3
+ "version": "0.8.4-main.fd6878d",
4
4
  "description": "Effect utils.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -9,6 +9,7 @@
9
9
  "type": "module",
10
10
  "exports": {
11
11
  ".": {
12
+ "source": "./src/index.ts",
12
13
  "types": "./dist/types/src/index.d.ts",
13
14
  "browser": "./dist/lib/browser/index.mjs",
14
15
  "node": "./dist/lib/node-esm/index.mjs"
@@ -24,14 +25,14 @@
24
25
  ],
25
26
  "dependencies": {
26
27
  "jsonpath-plus": "10.2.0",
27
- "@dxos/context": "0.8.4-main.f9ba587",
28
- "@dxos/invariant": "0.8.4-main.f9ba587",
29
- "@dxos/node-std": "0.8.4-main.f9ba587",
30
- "@dxos/util": "0.8.4-main.f9ba587"
28
+ "@dxos/context": "0.8.4-main.fd6878d",
29
+ "@dxos/invariant": "0.8.4-main.fd6878d",
30
+ "@dxos/node-std": "0.8.4-main.fd6878d",
31
+ "@dxos/util": "0.8.4-main.fd6878d"
31
32
  },
32
33
  "devDependencies": {
33
- "effect": "3.16.13",
34
- "@dxos/log": "0.8.4-main.f9ba587"
34
+ "effect": "3.17.7",
35
+ "@dxos/log": "0.8.4-main.fd6878d"
35
36
  },
36
37
  "peerDependencies": {
37
38
  "effect": "^3.13.3"
package/src/ast.test.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { SchemaAST, Schema } from 'effect';
5
+ import { Schema, SchemaAST } from 'effect';
6
6
  import { describe, test } from 'vitest';
7
7
 
8
8
  import { invariant } from '@dxos/invariant';
@@ -12,13 +12,13 @@ import {
12
12
  findNode,
13
13
  findProperty,
14
14
  getAnnotation,
15
- getDiscriminatingProps,
16
15
  getDiscriminatedType,
16
+ getDiscriminatingProps,
17
17
  getSimpleType,
18
+ isArrayType,
18
19
  isOption,
19
20
  isSimpleType,
20
21
  visit,
21
- isArrayType,
22
22
  } from './ast';
23
23
  import { type JsonPath, type JsonProp } from './jsonPath';
24
24
 
package/src/ast.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { Option, pipe, SchemaAST, Schema } from 'effect';
5
+ import { Option, Schema, SchemaAST, pipe } from 'effect';
6
6
  import { isUndefinedKeyword } from 'effect/SchemaAST';
7
7
 
8
8
  import { invariant } from '@dxos/invariant';
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';
@@ -4,7 +4,7 @@
4
4
 
5
5
  import { describe, expect, test } from 'vitest';
6
6
 
7
- import { createJsonPath, getField, isJsonPath, type JsonPath, splitJsonPath } from './jsonPath';
7
+ import { type JsonPath, createJsonPath, getField, isJsonPath, splitJsonPath } from './jsonPath';
8
8
 
9
9
  describe('createJsonPath', () => {
10
10
  test('supported path subset', () => {
package/src/jsonPath.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Schema, Option } from 'effect';
5
+ import { Option, Schema } from 'effect';
6
6
  import { JSONPath } from 'jsonpath-plus';
7
7
 
8
8
  import { invariant } from '@dxos/invariant';
@@ -0,0 +1,106 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { it } from '@effect/vitest';
6
+ import { Context, Duration, Effect, Layer, ManagedRuntime } from 'effect';
7
+ import { test } from 'vitest';
8
+
9
+ class ClientConfig extends Context.Tag('ClientConfig')<ClientConfig, { endpoint: string }>() {}
10
+
11
+ class Client extends Context.Tag('Client')<Client, { call: () => Effect.Effect<void> }>() {
12
+ static layer = Layer.effect(
13
+ Client,
14
+ Effect.gen(function* () {
15
+ const config = yield* ClientConfig;
16
+ return {
17
+ call: () => {
18
+ console.log('called', config.endpoint);
19
+ return Effect.void;
20
+ },
21
+ };
22
+ }),
23
+ );
24
+ }
25
+
26
+ const ServerLive = Layer.scoped(
27
+ ClientConfig,
28
+ Effect.gen(function* () {
29
+ console.log('start server');
30
+
31
+ yield* Effect.sleep(Duration.millis(100));
32
+
33
+ yield* Effect.addFinalizer(
34
+ Effect.fn(function* () {
35
+ yield* Effect.sleep(Duration.millis(100));
36
+ console.log('stop server');
37
+ }),
38
+ );
39
+
40
+ return {
41
+ endpoint: 'http://localhost:8080',
42
+ };
43
+ }),
44
+ );
45
+
46
+ it.effect.skip(
47
+ 'test',
48
+ Effect.fn(
49
+ function* ({ expect: _ }) {
50
+ const client = yield* Client;
51
+ yield* client.call();
52
+ },
53
+ Effect.provide(Layer.provide(Client.layer, ServerLive)),
54
+ ),
55
+ );
56
+
57
+ class ServerPlugin {
58
+ #runtime = ManagedRuntime.make(ServerLive);
59
+
60
+ readonly clientConfigLayer = Layer.effectContext(
61
+ this.#runtime.runtimeEffect.pipe(Effect.map((rt) => rt.context.pipe(Context.pick(ClientConfig)))),
62
+ );
63
+
64
+ async dispose() {
65
+ await this.#runtime.dispose();
66
+ }
67
+ }
68
+
69
+ class ClientPlugin {
70
+ constructor(private readonly _serverPlugin: ServerPlugin) {}
71
+
72
+ async run() {
73
+ const layer = Layer.provide(Client.layer, this._serverPlugin.clientConfigLayer);
74
+
75
+ await Effect.runPromise(
76
+ Effect.gen(function* () {
77
+ const client = yield* Client;
78
+ yield* client.call();
79
+ }).pipe(Effect.provide(layer)),
80
+ );
81
+ }
82
+ }
83
+
84
+ test.skip('plugins', async () => {
85
+ const serverPlugin = new ServerPlugin();
86
+ console.log('ServerPlugin created');
87
+
88
+ await Effect.runPromise(Effect.sleep(Duration.millis(500)));
89
+ console.log('wake up');
90
+
91
+ {
92
+ const clientPlugin1 = new ClientPlugin(serverPlugin);
93
+ console.log('ClientPlugin1 created');
94
+ await clientPlugin1.run();
95
+ console.log('client1 run');
96
+ }
97
+
98
+ {
99
+ const clientPlugin2 = new ClientPlugin(serverPlugin);
100
+ console.log('ClientPlugin2 created');
101
+ await clientPlugin2.run();
102
+ console.log('client2 run');
103
+ }
104
+
105
+ await serverPlugin.dispose();
106
+ });
@@ -0,0 +1,32 @@
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
+ const makeResource = accuireReleaseResource(() => ({
15
+ open: () => {
16
+ events.push('open');
17
+ },
18
+ close: () => {
19
+ events.push('close');
20
+ },
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
+
29
+ events.push('3');
30
+ expect(events).to.deep.equal(['1', 'open', '2', 'close', '3']);
31
+ }),
32
+ );
@@ -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,79 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { Effect } from 'effect';
6
+ import type { TestContext } from 'vitest';
7
+
8
+ // TODO(dmaretskyi): Add all different test tags here.
9
+ export type TestTag = 'flaky' | 'llm';
10
+
11
+ export namespace TestHelpers {
12
+ /**
13
+ * Skip the test if the condition is false.
14
+ *
15
+ * Exmaple:
16
+ * ```ts
17
+ * it.effect(
18
+ * 'should process an agentic loop using Claude',
19
+ * Effect.fn(function* ({ expect }) {
20
+ * // ...
21
+ * }),
22
+ * TestHelpers.runIf(process.env.ANTHROPIC_API_KEY),
23
+ * );
24
+ * ```
25
+ */
26
+ export const runIf =
27
+ (condition: unknown) =>
28
+ <A, E, R>(effect: Effect.Effect<A, E, R>, ctx: TestContext): Effect.Effect<A, E, R> =>
29
+ Effect.gen(function* () {
30
+ if (!condition) {
31
+ ctx.skip();
32
+ } else {
33
+ return yield* effect;
34
+ }
35
+ });
36
+
37
+ /**
38
+ * Skip the test if the condition is true.
39
+ *
40
+ * Exmaple:
41
+ * ```ts
42
+ * it.effect(
43
+ * 'should process an agentic loop using Claude',
44
+ * Effect.fn(function* ({ expect }) {
45
+ * // ...
46
+ * }),
47
+ * TestHelpers.skipIf(!process.env.ANTHROPIC_API_KEY),
48
+ * );
49
+ * ```
50
+ */
51
+ export const skipIf =
52
+ (condition: unknown) =>
53
+ <A, E, R>(effect: Effect.Effect<A, E, R>, ctx: TestContext): Effect.Effect<A, E, R> =>
54
+ Effect.gen(function* () {
55
+ if (condition) {
56
+ ctx.skip();
57
+ } else {
58
+ return yield* effect;
59
+ }
60
+ });
61
+
62
+ /**
63
+ * Skips this test if the tag is not in the list of tags to run.
64
+ * Tags are specified in the `DX_TEST_TAGS` environment variable.
65
+ *
66
+ * @param tag
67
+ * @returns
68
+ */
69
+ export const taggedTest =
70
+ (tag: TestTag) =>
71
+ <A, E, R>(effect: Effect.Effect<A, E, R>, ctx: TestContext): Effect.Effect<A, E, R> =>
72
+ Effect.gen(function* () {
73
+ if (!process.env.DX_TEST_TAGS?.includes(tag)) {
74
+ ctx.skip();
75
+ } else {
76
+ return yield* effect;
77
+ }
78
+ });
79
+ }
package/src/url.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { SchemaAST, type Schema, Option, pipe } from 'effect';
5
+ import { Option, type Schema, SchemaAST, pipe } from 'effect';
6
6
 
7
7
  import { decamelize } from '@dxos/util';
8
8