@dxos/effect 0.8.4-main.3a94e84 → 0.8.4-main.3c1ae3b
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 +271 -230
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing.mjs +37 -0
- package/dist/lib/browser/testing.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +271 -230
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing.mjs +37 -0
- package/dist/lib/node-esm/testing.mjs.map +7 -0
- package/dist/types/src/ast.d.ts +35 -22
- package/dist/types/src/ast.d.ts.map +1 -1
- package/dist/types/src/context.d.ts +2 -1
- package/dist/types/src/context.d.ts.map +1 -1
- package/dist/types/src/errors.d.ts +31 -1
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +2 -3
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/interrupt.test.d.ts +2 -0
- package/dist/types/src/interrupt.test.d.ts.map +1 -0
- package/dist/types/src/{jsonPath.d.ts → json-path.d.ts} +10 -2
- package/dist/types/src/json-path.d.ts.map +1 -0
- package/dist/types/src/json-path.test.d.ts +2 -0
- package/dist/types/src/json-path.test.d.ts.map +1 -0
- package/dist/types/src/layers.test.d.ts +2 -0
- package/dist/types/src/layers.test.d.ts.map +1 -0
- package/dist/types/src/otel.d.ts +17 -0
- package/dist/types/src/otel.d.ts.map +1 -0
- package/dist/types/src/otel.test.d.ts +2 -0
- package/dist/types/src/otel.test.d.ts.map +1 -0
- package/dist/types/src/resource.d.ts +6 -2
- package/dist/types/src/resource.d.ts.map +1 -1
- package/dist/types/src/testing.d.ts +33 -1
- package/dist/types/src/testing.d.ts.map +1 -1
- package/dist/types/src/url.d.ts +3 -1
- package/dist/types/src/url.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +27 -8
- package/src/ast.test.ts +14 -11
- package/src/ast.ts +118 -92
- package/src/context.ts +2 -1
- package/src/errors.test.ts +1 -1
- package/src/errors.ts +73 -20
- package/src/index.ts +2 -3
- package/src/interrupt.test.ts +33 -0
- package/src/{jsonPath.test.ts → json-path.test.ts} +40 -1
- package/src/{jsonPath.ts → json-path.ts} +29 -1
- package/src/layers.test.ts +110 -0
- package/src/otel.test.ts +126 -0
- package/src/otel.ts +45 -0
- package/src/resource.test.ts +5 -4
- package/src/resource.ts +8 -3
- package/src/sanity.test.ts +24 -11
- package/src/testing.ts +53 -1
- package/src/url.test.ts +1 -1
- package/src/url.ts +5 -2
- package/dist/types/src/jsonPath.d.ts.map +0 -1
- package/dist/types/src/jsonPath.test.d.ts +0 -2
- package/dist/types/src/jsonPath.test.d.ts.map +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/effect",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.3c1ae3b",
|
|
4
4
|
"description": "Effect utils.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -9,29 +9,48 @@
|
|
|
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"
|
|
16
|
+
},
|
|
17
|
+
"./testing": {
|
|
18
|
+
"source": "./src/testing.ts",
|
|
19
|
+
"types": "./dist/types/src/testing.d.ts",
|
|
20
|
+
"browser": "./dist/lib/browser/testing.mjs",
|
|
21
|
+
"node": "./dist/lib/node-esm/testing.mjs"
|
|
15
22
|
}
|
|
16
23
|
},
|
|
17
24
|
"types": "dist/types/src/index.d.ts",
|
|
18
25
|
"typesVersions": {
|
|
19
|
-
"*": {
|
|
26
|
+
"*": {
|
|
27
|
+
"testing": [
|
|
28
|
+
"./dist/types/src/testing.d.ts"
|
|
29
|
+
]
|
|
30
|
+
}
|
|
20
31
|
},
|
|
21
32
|
"files": [
|
|
22
33
|
"dist",
|
|
23
34
|
"src"
|
|
24
35
|
],
|
|
25
36
|
"dependencies": {
|
|
37
|
+
"@effect/opentelemetry": "^0.58.0",
|
|
38
|
+
"@opentelemetry/api": "^1.9.0",
|
|
26
39
|
"jsonpath-plus": "10.2.0",
|
|
27
|
-
"@dxos/context": "0.8.4-main.
|
|
28
|
-
"@dxos/invariant": "0.8.4-main.
|
|
29
|
-
"@dxos/node-std": "0.8.4-main.
|
|
30
|
-
"@dxos/util": "0.8.4-main.
|
|
40
|
+
"@dxos/context": "0.8.4-main.3c1ae3b",
|
|
41
|
+
"@dxos/invariant": "0.8.4-main.3c1ae3b",
|
|
42
|
+
"@dxos/node-std": "0.8.4-main.3c1ae3b",
|
|
43
|
+
"@dxos/util": "0.8.4-main.3c1ae3b"
|
|
31
44
|
},
|
|
32
45
|
"devDependencies": {
|
|
33
|
-
"
|
|
34
|
-
"@
|
|
46
|
+
"@opentelemetry/api-logs": "^0.203.0",
|
|
47
|
+
"@opentelemetry/resources": "^2.1.0",
|
|
48
|
+
"@opentelemetry/sdk-logs": "^0.203.0",
|
|
49
|
+
"@opentelemetry/sdk-node": "^0.203.0",
|
|
50
|
+
"@opentelemetry/sdk-trace-node": "^2.1.0",
|
|
51
|
+
"@opentelemetry/semantic-conventions": "^1.37.0",
|
|
52
|
+
"effect": "3.18.3",
|
|
53
|
+
"@dxos/log": "0.8.4-main.3c1ae3b"
|
|
35
54
|
},
|
|
36
55
|
"peerDependencies": {
|
|
37
56
|
"effect": "^3.13.3"
|
package/src/ast.test.ts
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import * as Schema from 'effect/Schema';
|
|
6
|
+
import * as SchemaAST from 'effect/SchemaAST';
|
|
6
7
|
import { describe, test } from 'vitest';
|
|
7
8
|
|
|
8
9
|
import { invariant } from '@dxos/invariant';
|
|
@@ -12,15 +13,14 @@ import {
|
|
|
12
13
|
findNode,
|
|
13
14
|
findProperty,
|
|
14
15
|
getAnnotation,
|
|
15
|
-
getDiscriminatingProps,
|
|
16
16
|
getDiscriminatedType,
|
|
17
|
-
|
|
17
|
+
getDiscriminatingProps,
|
|
18
|
+
isArrayType,
|
|
19
|
+
isDiscriminatedUnion,
|
|
18
20
|
isOption,
|
|
19
|
-
isSimpleType,
|
|
20
21
|
visit,
|
|
21
|
-
isArrayType,
|
|
22
22
|
} from './ast';
|
|
23
|
-
import { type JsonPath, type JsonProp } from './
|
|
23
|
+
import { type JsonPath, type JsonProp } from './json-path';
|
|
24
24
|
|
|
25
25
|
const ZipCode = Schema.String.pipe(
|
|
26
26
|
Schema.pattern(/^\d{5}$/, {
|
|
@@ -64,10 +64,9 @@ describe('AST', () => {
|
|
|
64
64
|
|
|
65
65
|
const prop = findProperty(TestSchema, 'name' as JsonProp);
|
|
66
66
|
invariant(prop);
|
|
67
|
-
const node = findNode(prop,
|
|
67
|
+
const node = findNode(prop, (node) => node._tag === 'StringKeyword');
|
|
68
68
|
invariant(node);
|
|
69
|
-
|
|
70
|
-
expect(type).to.eq('string');
|
|
69
|
+
expect(node._tag).to.eq('StringKeyword');
|
|
71
70
|
});
|
|
72
71
|
|
|
73
72
|
test('findProperty', ({ expect }) => {
|
|
@@ -136,7 +135,11 @@ describe('AST', () => {
|
|
|
136
135
|
});
|
|
137
136
|
|
|
138
137
|
const props: string[] = [];
|
|
139
|
-
visit(
|
|
138
|
+
visit(
|
|
139
|
+
TestSchema.ast,
|
|
140
|
+
(_, path) => props.push(path.join('.')),
|
|
141
|
+
(node, path, depth) => depth < 3,
|
|
142
|
+
);
|
|
140
143
|
});
|
|
141
144
|
|
|
142
145
|
test('discriminated unions', ({ expect }) => {
|
|
@@ -151,7 +154,7 @@ describe('AST', () => {
|
|
|
151
154
|
expect(isOption(TestUnionSchema.ast)).to.be.false;
|
|
152
155
|
expect(getDiscriminatingProps(TestUnionSchema.ast)).to.deep.eq(['kind']);
|
|
153
156
|
|
|
154
|
-
const node = findNode(TestUnionSchema.ast,
|
|
157
|
+
const node = findNode(TestUnionSchema.ast, isDiscriminatedUnion);
|
|
155
158
|
expect(node).to.eq(TestUnionSchema.ast);
|
|
156
159
|
}
|
|
157
160
|
|
package/src/ast.ts
CHANGED
|
@@ -2,13 +2,15 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
6
|
-
import
|
|
5
|
+
import * as Function from 'effect/Function';
|
|
6
|
+
import * as Option from 'effect/Option';
|
|
7
|
+
import * as Schema from 'effect/Schema';
|
|
8
|
+
import * as SchemaAST from 'effect/SchemaAST';
|
|
7
9
|
|
|
8
10
|
import { invariant } from '@dxos/invariant';
|
|
9
11
|
import { isNonNullable } from '@dxos/util';
|
|
10
12
|
|
|
11
|
-
import { type JsonPath, type JsonProp } from './
|
|
13
|
+
import { type JsonPath, type JsonProp } from './json-path';
|
|
12
14
|
|
|
13
15
|
//
|
|
14
16
|
// Refs
|
|
@@ -17,67 +19,53 @@ import { type JsonPath, type JsonProp } from './jsonPath';
|
|
|
17
19
|
// https://effect-ts.github.io/effect/schema/SchemaAST.ts.html
|
|
18
20
|
//
|
|
19
21
|
|
|
20
|
-
export type SimpleType = 'object' | 'string' | 'number' | 'boolean' | 'enum' | 'literal';
|
|
21
|
-
|
|
22
22
|
/**
|
|
23
|
-
*
|
|
23
|
+
* Unwraps and collects refinement filters.
|
|
24
24
|
*/
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
if (SchemaAST.isStringKeyword(node)) {
|
|
36
|
-
return 'string';
|
|
37
|
-
}
|
|
38
|
-
if (SchemaAST.isNumberKeyword(node)) {
|
|
39
|
-
return 'number';
|
|
40
|
-
}
|
|
41
|
-
if (SchemaAST.isBooleanKeyword(node)) {
|
|
42
|
-
return 'boolean';
|
|
25
|
+
const reduceRefinements = (
|
|
26
|
+
type: SchemaAST.AST,
|
|
27
|
+
refinements: SchemaAST.Refinement['filter'][] = [],
|
|
28
|
+
): { type: SchemaAST.AST; refinements: SchemaAST.Refinement['filter'][] } => {
|
|
29
|
+
if (SchemaAST.isRefinement(type)) {
|
|
30
|
+
const annotations = type.annotations;
|
|
31
|
+
const filter = type.filter;
|
|
32
|
+
const nextType = { ...type.from, annotations: { ...type.annotations, ...annotations } } as SchemaAST.AST;
|
|
33
|
+
return reduceRefinements(nextType, [...refinements, filter]);
|
|
43
34
|
}
|
|
44
35
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
36
|
+
return { type, refinements };
|
|
37
|
+
};
|
|
48
38
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
39
|
+
/**
|
|
40
|
+
* Get the base type of a property.
|
|
41
|
+
*
|
|
42
|
+
* Unwraps refinements and optional unions.
|
|
43
|
+
*/
|
|
44
|
+
export const getBaseType = (
|
|
45
|
+
prop: SchemaAST.PropertySignature,
|
|
46
|
+
): { type: SchemaAST.AST; refinements: SchemaAST.Refinement['filter'][] } => {
|
|
47
|
+
const encoded = SchemaAST.encodedBoundAST(prop.type);
|
|
48
|
+
// Extract property ast from optional union.
|
|
49
|
+
const unwrapped = prop.isOptional && encoded._tag === 'Union' ? encoded.types[0] : encoded;
|
|
50
|
+
return reduceRefinements(unwrapped);
|
|
52
51
|
};
|
|
53
52
|
|
|
54
|
-
export
|
|
53
|
+
export type SchemaProperty = Pick<SchemaAST.PropertySignature, 'name' | 'type' | 'isOptional' | 'isReadonly'> & {
|
|
54
|
+
refinements: SchemaAST.Refinement['filter'][];
|
|
55
|
+
};
|
|
55
56
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
69
|
-
case 'boolean': {
|
|
70
|
-
return false;
|
|
71
|
-
}
|
|
72
|
-
case 'object': {
|
|
73
|
-
return {};
|
|
74
|
-
}
|
|
75
|
-
default: {
|
|
76
|
-
throw new Error(`Unsupported type for default value: ${type}`);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
};
|
|
80
|
-
}
|
|
57
|
+
/**
|
|
58
|
+
* Get the property types of an AST.
|
|
59
|
+
*/
|
|
60
|
+
export const getProperties = (ast: SchemaAST.AST): SchemaProperty[] => {
|
|
61
|
+
const properties = SchemaAST.getPropertySignatures(ast);
|
|
62
|
+
return properties.map((prop) => ({
|
|
63
|
+
...getBaseType(prop),
|
|
64
|
+
name: prop.name,
|
|
65
|
+
isOptional: prop.isOptional,
|
|
66
|
+
isReadonly: prop.isReadonly,
|
|
67
|
+
}));
|
|
68
|
+
};
|
|
81
69
|
|
|
82
70
|
//
|
|
83
71
|
// Branded types
|
|
@@ -101,23 +89,14 @@ export type TestFn = (node: SchemaAST.AST, path: Path, depth: number) => VisitRe
|
|
|
101
89
|
|
|
102
90
|
export type VisitorFn = (node: SchemaAST.AST, path: Path, depth: number) => void;
|
|
103
91
|
|
|
104
|
-
const defaultTest: TestFn = isSimpleType;
|
|
105
|
-
|
|
106
92
|
/**
|
|
107
93
|
* Visit leaf nodes.
|
|
108
94
|
* Refs:
|
|
109
95
|
* - https://github.com/syntax-tree/unist-util-visit?tab=readme-ov-file#visitor
|
|
110
96
|
* - https://github.com/syntax-tree/unist-util-is?tab=readme-ov-file#test
|
|
111
97
|
*/
|
|
112
|
-
export const visit: {
|
|
113
|
-
(node
|
|
114
|
-
(node: SchemaAST.AST, test: TestFn, visitor: VisitorFn): void;
|
|
115
|
-
} = (node: SchemaAST.AST, testOrVisitor: TestFn | VisitorFn, visitor?: VisitorFn): void => {
|
|
116
|
-
if (!visitor) {
|
|
117
|
-
visitNode(node, defaultTest, testOrVisitor);
|
|
118
|
-
} else {
|
|
119
|
-
visitNode(node, testOrVisitor as TestFn, visitor);
|
|
120
|
-
}
|
|
98
|
+
export const visit = (node: SchemaAST.AST, testOrVisitor: TestFn | VisitorFn, visitor: VisitorFn): void => {
|
|
99
|
+
visitNode(node, testOrVisitor as TestFn, visitor);
|
|
121
100
|
};
|
|
122
101
|
|
|
123
102
|
const visitNode = (
|
|
@@ -127,15 +106,15 @@ const visitNode = (
|
|
|
127
106
|
path: Path = [],
|
|
128
107
|
depth = 0,
|
|
129
108
|
): VisitResult | undefined => {
|
|
130
|
-
const
|
|
109
|
+
const $result = test?.(node, path, depth);
|
|
131
110
|
const result: VisitResult =
|
|
132
|
-
|
|
111
|
+
$result === undefined
|
|
133
112
|
? VisitResult.CONTINUE
|
|
134
|
-
: typeof
|
|
135
|
-
?
|
|
113
|
+
: typeof $result === 'boolean'
|
|
114
|
+
? $result
|
|
136
115
|
? VisitResult.CONTINUE
|
|
137
116
|
: VisitResult.SKIP
|
|
138
|
-
:
|
|
117
|
+
: $result;
|
|
139
118
|
|
|
140
119
|
if (result === VisitResult.EXIT) {
|
|
141
120
|
return result;
|
|
@@ -204,6 +183,12 @@ export const findNode = (node: SchemaAST.AST, test: (node: SchemaAST.AST) => boo
|
|
|
204
183
|
return child;
|
|
205
184
|
}
|
|
206
185
|
}
|
|
186
|
+
for (const prop of getIndexSignatures(node)) {
|
|
187
|
+
const child = findNode(prop.type, test);
|
|
188
|
+
if (child) {
|
|
189
|
+
return child;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
207
192
|
}
|
|
208
193
|
|
|
209
194
|
// Tuple.
|
|
@@ -218,12 +203,14 @@ export const findNode = (node: SchemaAST.AST, test: (node: SchemaAST.AST) => boo
|
|
|
218
203
|
|
|
219
204
|
// Branching union (e.g., optional, discriminated unions).
|
|
220
205
|
else if (SchemaAST.isUnion(node)) {
|
|
221
|
-
if (
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
206
|
+
if (isLiteralUnion(node)) {
|
|
207
|
+
return undefined;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
for (const type of node.types) {
|
|
211
|
+
const child = findNode(type, test);
|
|
212
|
+
if (child) {
|
|
213
|
+
return child;
|
|
227
214
|
}
|
|
228
215
|
}
|
|
229
216
|
}
|
|
@@ -279,8 +266,8 @@ export const getAnnotation =
|
|
|
279
266
|
<T>(annotationId: symbol, noDefault = true) =>
|
|
280
267
|
(node: SchemaAST.AST): T | undefined => {
|
|
281
268
|
// Title fallback seems to be the identifier.
|
|
282
|
-
const id = pipe(SchemaAST.getIdentifierAnnotation(node), Option.getOrUndefined);
|
|
283
|
-
const value = pipe(SchemaAST.getAnnotation<T>(annotationId)(node), Option.getOrUndefined);
|
|
269
|
+
const id = Function.pipe(SchemaAST.getIdentifierAnnotation(node), Option.getOrUndefined);
|
|
270
|
+
const value = Function.pipe(SchemaAST.getAnnotation<T>(annotationId)(node), Option.getOrUndefined);
|
|
284
271
|
if (noDefault && (value === defaultAnnotations[node._tag]?.annotations[annotationId] || value === id)) {
|
|
285
272
|
return undefined;
|
|
286
273
|
}
|
|
@@ -326,10 +313,31 @@ export const isOption = (node: SchemaAST.AST): boolean => {
|
|
|
326
313
|
/**
|
|
327
314
|
* Determines if the node is a union of literal types.
|
|
328
315
|
*/
|
|
329
|
-
export const isLiteralUnion = (node: SchemaAST.AST):
|
|
316
|
+
export const isLiteralUnion = (node: SchemaAST.AST): node is SchemaAST.Union<SchemaAST.Literal> => {
|
|
330
317
|
return SchemaAST.isUnion(node) && node.types.every(SchemaAST.isLiteral);
|
|
331
318
|
};
|
|
332
319
|
|
|
320
|
+
/**
|
|
321
|
+
* Determines if the node is an array type.
|
|
322
|
+
*/
|
|
323
|
+
export const isArrayType = (node: SchemaAST.AST): node is SchemaAST.TupleType => {
|
|
324
|
+
return SchemaAST.isTupleType(node) && node.elements.length === 0 && node.rest.length === 1;
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Get the type of the array elements.
|
|
329
|
+
*/
|
|
330
|
+
export const getArrayElementType = (node: SchemaAST.AST): SchemaAST.AST | undefined => {
|
|
331
|
+
return isArrayType(node) ? node.rest.at(0)?.type : undefined;
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Determines if the node is a tuple type.
|
|
336
|
+
*/
|
|
337
|
+
export const isTupleType = (node: SchemaAST.AST): boolean => {
|
|
338
|
+
return SchemaAST.isTupleType(node) && node.elements.length > 0;
|
|
339
|
+
};
|
|
340
|
+
|
|
333
341
|
/**
|
|
334
342
|
* Determines if the node is a discriminated union.
|
|
335
343
|
*/
|
|
@@ -409,6 +417,20 @@ export const getDiscriminatedType = (
|
|
|
409
417
|
return schema.ast;
|
|
410
418
|
};
|
|
411
419
|
|
|
420
|
+
/**
|
|
421
|
+
* Determines if the node is a nested object type.
|
|
422
|
+
*/
|
|
423
|
+
export const isNestedType = (node: SchemaAST.AST): boolean => {
|
|
424
|
+
return (
|
|
425
|
+
SchemaAST.isDeclaration(node) ||
|
|
426
|
+
SchemaAST.isObjectKeyword(node) ||
|
|
427
|
+
SchemaAST.isTypeLiteral(node) ||
|
|
428
|
+
// TODO(wittjosiah): Tuples are actually arrays.
|
|
429
|
+
isTupleType(node) ||
|
|
430
|
+
isDiscriminatedUnion(node)
|
|
431
|
+
);
|
|
432
|
+
};
|
|
433
|
+
|
|
412
434
|
/**
|
|
413
435
|
* Maps AST nodes.
|
|
414
436
|
* The user is responsible for recursively calling {@link mapAst} on the SchemaAST.
|
|
@@ -432,6 +454,7 @@ export const mapAst = (
|
|
|
432
454
|
),
|
|
433
455
|
),
|
|
434
456
|
ast.indexSignatures,
|
|
457
|
+
ast.annotations,
|
|
435
458
|
);
|
|
436
459
|
}
|
|
437
460
|
case 'Union': {
|
|
@@ -456,15 +479,18 @@ export const mapAst = (
|
|
|
456
479
|
}
|
|
457
480
|
};
|
|
458
481
|
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
482
|
+
const getIndexSignatures = (ast: SchemaAST.AST): Array<SchemaAST.IndexSignature> => {
|
|
483
|
+
const annotation = SchemaAST.getSurrogateAnnotation(ast);
|
|
484
|
+
if (Option.isSome(annotation)) {
|
|
485
|
+
return getIndexSignatures(annotation.value);
|
|
486
|
+
}
|
|
487
|
+
switch (ast._tag) {
|
|
488
|
+
case 'TypeLiteral':
|
|
489
|
+
return ast.indexSignatures.slice();
|
|
490
|
+
case 'Suspend':
|
|
491
|
+
return getIndexSignatures(ast.f());
|
|
492
|
+
case 'Refinement':
|
|
493
|
+
return getIndexSignatures(ast.from);
|
|
494
|
+
}
|
|
495
|
+
return [];
|
|
470
496
|
};
|
package/src/context.ts
CHANGED
package/src/errors.test.ts
CHANGED
package/src/errors.ts
CHANGED
|
@@ -2,8 +2,13 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
6
|
-
import
|
|
5
|
+
import * as Cause from 'effect/Cause';
|
|
6
|
+
import * as Chunk from 'effect/Chunk';
|
|
7
|
+
import * as Effect from 'effect/Effect';
|
|
8
|
+
import * as Exit from 'effect/Exit';
|
|
9
|
+
import * as GlobalValue from 'effect/GlobalValue';
|
|
10
|
+
import * as Option from 'effect/Option';
|
|
11
|
+
import type * as Tracer from 'effect/Tracer';
|
|
7
12
|
|
|
8
13
|
const spanSymbol = Symbol.for('effect/SpanAnnotation');
|
|
9
14
|
const originalSymbol = Symbol.for('effect/OriginalAnnotation');
|
|
@@ -48,7 +53,7 @@ const prettyErrorStack = (error: any, appendStacks: string[] = []): any => {
|
|
|
48
53
|
}
|
|
49
54
|
|
|
50
55
|
if (span) {
|
|
51
|
-
let current: Span | AnySpan | undefined = span;
|
|
56
|
+
let current: Tracer.Span | Tracer.AnySpan | undefined = span;
|
|
52
57
|
let i = 0;
|
|
53
58
|
while (current && current._tag === 'Span' && i < 10) {
|
|
54
59
|
const stackFn = spanToTrace.get(current);
|
|
@@ -95,7 +100,7 @@ const prettyErrorStack = (error: any, appendStacks: string[] = []): any => {
|
|
|
95
100
|
};
|
|
96
101
|
|
|
97
102
|
/**
|
|
98
|
-
*
|
|
103
|
+
* Converts a cause to an error.
|
|
99
104
|
* Inserts effect spans as stack frames.
|
|
100
105
|
* The error will have stack frames of where the effect was run (if stack trace limit allows).
|
|
101
106
|
* Removes effect runtime internal stack frames.
|
|
@@ -104,21 +109,13 @@ const prettyErrorStack = (error: any, appendStacks: string[] = []): any => {
|
|
|
104
109
|
*
|
|
105
110
|
* @throws AggregateError if there are multiple errors.
|
|
106
111
|
*/
|
|
107
|
-
export const
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
if (Exit.isSuccess(exit)) {
|
|
113
|
-
return exit.value;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
if (Cause.isEmpty(exit.cause)) {
|
|
117
|
-
throw new Error('Fiber failed without a cause');
|
|
118
|
-
} else if (Cause.isInterrupted(exit.cause)) {
|
|
119
|
-
throw new Error('Fiber was interrupted');
|
|
112
|
+
export const causeToError = (cause: Cause.Cause<any>): Error => {
|
|
113
|
+
if (Cause.isEmpty(cause)) {
|
|
114
|
+
return new Error('Fiber failed without a cause');
|
|
115
|
+
} else if (Cause.isInterruptedOnly(cause)) {
|
|
116
|
+
return new Error('Fiber was interrupted');
|
|
120
117
|
} else {
|
|
121
|
-
const errors = [...Chunk.toArray(Cause.failures(
|
|
118
|
+
const errors = [...Chunk.toArray(Cause.failures(cause)), ...Chunk.toArray(Cause.defects(cause))];
|
|
122
119
|
|
|
123
120
|
const getStackFrames = (): string[] => {
|
|
124
121
|
const o: { stack: string } = {} as any;
|
|
@@ -130,9 +127,65 @@ export const runAndForwardErrors = async <A, E>(
|
|
|
130
127
|
const newErrors = errors.map((error) => prettyErrorStack(error, stackFrames));
|
|
131
128
|
|
|
132
129
|
if (newErrors.length === 1) {
|
|
133
|
-
|
|
130
|
+
return newErrors[0];
|
|
134
131
|
} else {
|
|
135
|
-
|
|
132
|
+
return new AggregateError(newErrors);
|
|
136
133
|
}
|
|
137
134
|
}
|
|
138
135
|
};
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Throws an error based on the cause.
|
|
139
|
+
* Inserts effect spans as stack frames.
|
|
140
|
+
* The error will have stack frames of where the effect was run (if stack trace limit allows).
|
|
141
|
+
* Removes effect runtime internal stack frames.
|
|
142
|
+
*
|
|
143
|
+
* To be used in place of `Effect.runPromise`.
|
|
144
|
+
*
|
|
145
|
+
* @throws AggregateError if there are multiple errors.
|
|
146
|
+
*/
|
|
147
|
+
export const throwCause = (cause: Cause.Cause<any>): never => {
|
|
148
|
+
throw causeToError(cause);
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
export const unwrapExit = <A>(exit: Exit.Exit<A, any>): A => {
|
|
152
|
+
if (Exit.isSuccess(exit)) {
|
|
153
|
+
return exit.value;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return throwCause(exit.cause);
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Runs the embedded effect asynchronously and throws any failures and defects as errors.
|
|
161
|
+
* Inserts effect spans as stack frames.
|
|
162
|
+
* The error will have stack frames of where the effect was run (if stack trace limit allows).
|
|
163
|
+
* Removes effect runtime internal stack frames.
|
|
164
|
+
*
|
|
165
|
+
* To be used in place of `Effect.runPromise`.
|
|
166
|
+
*
|
|
167
|
+
* @throws AggregateError if there are multiple errors.
|
|
168
|
+
*/
|
|
169
|
+
export const runAndForwardErrors = async <A, E>(
|
|
170
|
+
effect: Effect.Effect<A, E, never>,
|
|
171
|
+
options?: { signal?: AbortSignal },
|
|
172
|
+
): Promise<A> => {
|
|
173
|
+
const exit = await Effect.runPromiseExit(effect, options);
|
|
174
|
+
return unwrapExit(exit);
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Like `Effect.promise` but also caputes spans for defects.
|
|
179
|
+
* Workaround for: https://github.com/Effect-TS/effect/issues/5436
|
|
180
|
+
*/
|
|
181
|
+
export const promiseWithCauseCapture: <A>(evaluate: (signal: AbortSignal) => PromiseLike<A>) => Effect.Effect<A> = (
|
|
182
|
+
evaluate,
|
|
183
|
+
) =>
|
|
184
|
+
Effect.promise(async (signal) => {
|
|
185
|
+
try {
|
|
186
|
+
const result = await evaluate(signal);
|
|
187
|
+
return Effect.succeed(result);
|
|
188
|
+
} catch (err) {
|
|
189
|
+
return Effect.die(err);
|
|
190
|
+
}
|
|
191
|
+
}).pipe(Effect.flatten);
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { it } from '@effect/vitest';
|
|
6
|
+
import * as Cause from 'effect/Cause';
|
|
7
|
+
import * as Effect from 'effect/Effect';
|
|
8
|
+
import * as Fiber from 'effect/Fiber';
|
|
9
|
+
|
|
10
|
+
const doWork = Effect.fn('doWork')(function* () {
|
|
11
|
+
yield* Effect.sleep('1 minute');
|
|
12
|
+
return 'work done';
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it.effect.skip(
|
|
16
|
+
'call a function to generate a research report',
|
|
17
|
+
Effect.fnUntraced(
|
|
18
|
+
function* (_) {
|
|
19
|
+
const resultFiber = yield* doWork().pipe(Effect.fork);
|
|
20
|
+
setTimeout(() => {
|
|
21
|
+
void Effect.runPromise(Fiber.interrupt(resultFiber));
|
|
22
|
+
}, 2_000);
|
|
23
|
+
|
|
24
|
+
const result = yield* resultFiber;
|
|
25
|
+
console.log({ result });
|
|
26
|
+
},
|
|
27
|
+
Effect.catchAllCause((cause) => {
|
|
28
|
+
// console.log(inspect(cause, { depth: null, colors: true }));
|
|
29
|
+
console.log(Cause.pretty(cause));
|
|
30
|
+
return Effect.failCause(cause);
|
|
31
|
+
}),
|
|
32
|
+
),
|
|
33
|
+
);
|