@dxos/effect 0.8.3-staging.0fa589b → 0.8.4-main.84f28bd
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/dist/lib/browser/index.mjs +125 -11
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +125 -11
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/ast.d.ts +4 -0
- package/dist/types/src/ast.d.ts.map +1 -1
- package/dist/types/src/context.d.ts +4 -0
- package/dist/types/src/context.d.ts.map +1 -0
- package/dist/types/src/errors.d.ts +15 -0
- package/dist/types/src/errors.d.ts.map +1 -0
- package/dist/types/src/errors.test.d.ts +2 -0
- package/dist/types/src/errors.test.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +2 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +7 -6
- package/src/ast.test.ts +17 -0
- package/src/ast.ts +14 -0
- package/src/context.ts +15 -0
- package/src/errors.test.ts +22 -0
- package/src/errors.ts +129 -0
- package/src/index.ts +2 -0
- package/dist/lib/node/index.cjs +0 -487
- package/dist/lib/node/index.cjs.map +0 -7
- package/dist/lib/node/meta.json +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/effect",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.4-main.84f28bd",
|
|
4
4
|
"description": "Effect utils.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -24,13 +24,14 @@
|
|
|
24
24
|
],
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"jsonpath-plus": "10.2.0",
|
|
27
|
-
"@dxos/
|
|
28
|
-
"@dxos/invariant": "0.8.
|
|
29
|
-
"@dxos/node-std": "0.8.
|
|
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"
|
|
30
31
|
},
|
|
31
32
|
"devDependencies": {
|
|
32
|
-
"effect": "3.
|
|
33
|
-
"@dxos/log": "0.8.
|
|
33
|
+
"effect": "3.16.13",
|
|
34
|
+
"@dxos/log": "0.8.4-main.84f28bd"
|
|
34
35
|
},
|
|
35
36
|
"peerDependencies": {
|
|
36
37
|
"effect": "^3.13.3"
|
package/src/ast.test.ts
CHANGED
|
@@ -18,6 +18,7 @@ import {
|
|
|
18
18
|
isOption,
|
|
19
19
|
isSimpleType,
|
|
20
20
|
visit,
|
|
21
|
+
isArrayType,
|
|
21
22
|
} from './ast';
|
|
22
23
|
import { type JsonPath, type JsonProp } from './jsonPath';
|
|
23
24
|
|
|
@@ -178,4 +179,20 @@ describe('AST', () => {
|
|
|
178
179
|
);
|
|
179
180
|
}
|
|
180
181
|
});
|
|
182
|
+
|
|
183
|
+
test('Schema.pluck', ({ expect }) => {
|
|
184
|
+
const TestSchema = Schema.Struct({
|
|
185
|
+
name: Schema.String,
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
expect(TestSchema.pipe(Schema.pluck('name'), Schema.typeSchema).ast).toEqual(SchemaAST.stringKeyword);
|
|
189
|
+
expect(() => TestSchema.pipe(Schema.pluck('missing' as any), Schema.typeSchema)).to.throw();
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
test('isArray', ({ expect }) => {
|
|
193
|
+
expect(isArrayType(Schema.String.ast)).to.be.false;
|
|
194
|
+
expect(isArrayType(Schema.Array(Schema.String).ast)).to.be.true;
|
|
195
|
+
expect(isArrayType(findProperty(Schema.Struct({ a: Schema.Array(Schema.String) }), 'a' as JsonPath)!)).to.be.true;
|
|
196
|
+
expect(isArrayType(Schema.Union(Schema.String, Schema.Array(Schema.String)).ast)).to.be.false;
|
|
197
|
+
});
|
|
181
198
|
});
|
package/src/ast.ts
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { Option, pipe, SchemaAST, Schema } from 'effect';
|
|
6
|
+
import { isUndefinedKeyword } from 'effect/SchemaAST';
|
|
6
7
|
|
|
7
8
|
import { invariant } from '@dxos/invariant';
|
|
8
9
|
import { isNonNullable } from '@dxos/util';
|
|
@@ -454,3 +455,16 @@ export const mapAst = (
|
|
|
454
455
|
}
|
|
455
456
|
}
|
|
456
457
|
};
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* @returns true if AST is for Array(T) or optional(Array(T)).
|
|
461
|
+
*/
|
|
462
|
+
export const isArrayType = (node: SchemaAST.AST): boolean => {
|
|
463
|
+
return (
|
|
464
|
+
SchemaAST.isTupleType(node) ||
|
|
465
|
+
(SchemaAST.isUnion(node) &&
|
|
466
|
+
node.types.some(isArrayType) &&
|
|
467
|
+
node.types.some(isUndefinedKeyword) &&
|
|
468
|
+
node.types.length === 2)
|
|
469
|
+
);
|
|
470
|
+
};
|
package/src/context.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Effect, type Scope } from 'effect';
|
|
6
|
+
|
|
7
|
+
import { Context } from '@dxos/context';
|
|
8
|
+
|
|
9
|
+
// TODO(dmaretskyi): Error handling.
|
|
10
|
+
export const contextFromScope = (): Effect.Effect<Context, never, Scope.Scope> =>
|
|
11
|
+
Effect.gen(function* () {
|
|
12
|
+
const ctx = new Context();
|
|
13
|
+
yield* Effect.addFinalizer(() => Effect.promise(() => ctx.dispose()));
|
|
14
|
+
return ctx;
|
|
15
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Data } from 'effect';
|
|
6
|
+
import { test } from 'vitest';
|
|
7
|
+
|
|
8
|
+
class MyError extends Data.TaggedError('MyError')<{
|
|
9
|
+
message: string;
|
|
10
|
+
}> {
|
|
11
|
+
constructor() {
|
|
12
|
+
super({ message: 'My error message' });
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// Experimenting with error formatting:
|
|
17
|
+
// - If the error doesn't have the message set, vitest will print the error as a JS object.
|
|
18
|
+
// - If the error has non-empty message, vitest will pretty-print the error.
|
|
19
|
+
test.skip('Data error formatting', () => {
|
|
20
|
+
console.log(JSON.stringify(new MyError(), null, 2));
|
|
21
|
+
throw new MyError();
|
|
22
|
+
});
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Cause, Chunk, Effect, Exit, GlobalValue, Option } from 'effect';
|
|
6
|
+
import type { AnySpan, Span } from 'effect/Tracer';
|
|
7
|
+
|
|
8
|
+
const spanSymbol = Symbol.for('effect/SpanAnnotation');
|
|
9
|
+
const spanToTrace = GlobalValue.globalValue('effect/Tracer/spanToTrace', () => new WeakMap());
|
|
10
|
+
const locationRegex = /\((.*)\)/g;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Adds effect spans.
|
|
14
|
+
* Removes effect internal functions.
|
|
15
|
+
*/
|
|
16
|
+
const prettyErrorStack = (error: any, appendStacks: string[] = []): void => {
|
|
17
|
+
const span = error[spanSymbol];
|
|
18
|
+
|
|
19
|
+
const lines = typeof error.stack === 'string' ? error.stack.split('\n') : [];
|
|
20
|
+
const out = [];
|
|
21
|
+
|
|
22
|
+
let atStack = false;
|
|
23
|
+
for (let i = 0; i < lines.length; i++) {
|
|
24
|
+
if (!atStack && !lines[i].startsWith(' at ')) {
|
|
25
|
+
out.push(lines[i]);
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
atStack = true;
|
|
29
|
+
|
|
30
|
+
if (lines[i].includes(' at new BaseEffectError') || lines[i].includes(' at new YieldableError')) {
|
|
31
|
+
i++;
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
if (lines[i].includes('Generator.next')) {
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
if (lines[i].includes('effect_internal_function')) {
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
out.push(
|
|
41
|
+
lines[i]
|
|
42
|
+
.replace(/at .*effect_instruction_i.*\((.*)\)/, 'at $1')
|
|
43
|
+
.replace(/EffectPrimitive\.\w+/, '<anonymous>')
|
|
44
|
+
.replace(/at Arguments\./, 'at '),
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (span) {
|
|
49
|
+
let current: Span | AnySpan | undefined = span;
|
|
50
|
+
let i = 0;
|
|
51
|
+
while (current && current._tag === 'Span' && i < 10) {
|
|
52
|
+
const stackFn = spanToTrace.get(current);
|
|
53
|
+
if (typeof stackFn === 'function') {
|
|
54
|
+
const stack = stackFn();
|
|
55
|
+
if (typeof stack === 'string') {
|
|
56
|
+
const locationMatchAll = stack.matchAll(locationRegex);
|
|
57
|
+
let match = false;
|
|
58
|
+
for (const [, location] of locationMatchAll) {
|
|
59
|
+
match = true;
|
|
60
|
+
out.push(` at ${current.name} (${location})`);
|
|
61
|
+
}
|
|
62
|
+
if (!match) {
|
|
63
|
+
out.push(` at ${current.name} (${stack.replace(/^at /, '')})`);
|
|
64
|
+
}
|
|
65
|
+
} else {
|
|
66
|
+
out.push(` at ${current.name}`);
|
|
67
|
+
}
|
|
68
|
+
} else {
|
|
69
|
+
out.push(` at ${current.name}`);
|
|
70
|
+
}
|
|
71
|
+
current = Option.getOrUndefined(current.parent);
|
|
72
|
+
i++;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
out.push(...appendStacks);
|
|
77
|
+
|
|
78
|
+
Object.defineProperty(error, 'stack', {
|
|
79
|
+
value: out.join('\n'),
|
|
80
|
+
writable: true,
|
|
81
|
+
enumerable: false,
|
|
82
|
+
configurable: true,
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Runs the embedded effect asynchronously and throws any failures and defects as errors.
|
|
88
|
+
* Inserts effect spans as stack frames.
|
|
89
|
+
* The error will have stack frames of where the effect was run (if stack trace limit allows).
|
|
90
|
+
* Removes effect runtime internal stack frames.
|
|
91
|
+
*
|
|
92
|
+
* To be used in place of `Effect.runPromise`.
|
|
93
|
+
*
|
|
94
|
+
* @throws AggregateError if there are multiple errors.
|
|
95
|
+
*/
|
|
96
|
+
export const runAndForwardErrors = async <A, E>(
|
|
97
|
+
effect: Effect.Effect<A, E, never>,
|
|
98
|
+
options?: { signal?: AbortSignal },
|
|
99
|
+
): Promise<A> => {
|
|
100
|
+
const exit = await Effect.runPromiseExit(effect, options);
|
|
101
|
+
if (Exit.isSuccess(exit)) {
|
|
102
|
+
return exit.value;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (Cause.isEmpty(exit.cause)) {
|
|
106
|
+
throw new Error('Fiber failed without a cause');
|
|
107
|
+
} else if (Cause.isInterrupted(exit.cause)) {
|
|
108
|
+
throw new Error('Fiber was interrupted');
|
|
109
|
+
} else {
|
|
110
|
+
const errors = [...Chunk.toArray(Cause.failures(exit.cause)), ...Chunk.toArray(Cause.defects(exit.cause))];
|
|
111
|
+
|
|
112
|
+
const getStackFrames = (): string[] => {
|
|
113
|
+
const o: { stack: string } = {} as any;
|
|
114
|
+
Error.captureStackTrace(o, getStackFrames);
|
|
115
|
+
return o.stack.split('\n').slice(1);
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const stackFrames = getStackFrames();
|
|
119
|
+
for (const error of errors) {
|
|
120
|
+
prettyErrorStack(error, stackFrames);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (errors.length === 1) {
|
|
124
|
+
throw errors[0];
|
|
125
|
+
} else {
|
|
126
|
+
throw new AggregateError(errors);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
};
|