@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.
- package/dist/lib/browser/index.mjs +132 -53
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +132 -53
- 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 +3 -2
- 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 +1 -1
- 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 +17 -9
- package/src/ast.test.ts +5 -4
- package/src/ast.ts +52 -17
- package/src/context.ts +2 -1
- package/src/errors.test.ts +1 -1
- package/src/errors.ts +73 -20
- package/src/index.ts +1 -1
- 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.70d3990",
|
|
4
4
|
"description": "Effect utils.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -9,10 +9,10 @@
|
|
|
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
|
-
"node": "./dist/lib/node-esm/index.mjs"
|
|
15
|
-
"source": "./src/index.ts"
|
|
15
|
+
"node": "./dist/lib/node-esm/index.mjs"
|
|
16
16
|
}
|
|
17
17
|
},
|
|
18
18
|
"types": "dist/types/src/index.d.ts",
|
|
@@ -24,15 +24,23 @@
|
|
|
24
24
|
"src"
|
|
25
25
|
],
|
|
26
26
|
"dependencies": {
|
|
27
|
+
"@effect/opentelemetry": "^0.58.0",
|
|
28
|
+
"@opentelemetry/api": "^1.9.0",
|
|
27
29
|
"jsonpath-plus": "10.2.0",
|
|
28
|
-
"@dxos/context": "0.8.4-main.
|
|
29
|
-
"@dxos/node-std": "0.8.4-main.
|
|
30
|
-
"@dxos/
|
|
31
|
-
"@dxos/
|
|
30
|
+
"@dxos/context": "0.8.4-main.70d3990",
|
|
31
|
+
"@dxos/node-std": "0.8.4-main.70d3990",
|
|
32
|
+
"@dxos/util": "0.8.4-main.70d3990",
|
|
33
|
+
"@dxos/invariant": "0.8.4-main.70d3990"
|
|
32
34
|
},
|
|
33
35
|
"devDependencies": {
|
|
34
|
-
"
|
|
35
|
-
"@
|
|
36
|
+
"@opentelemetry/api-logs": "^0.203.0",
|
|
37
|
+
"@opentelemetry/resources": "^2.1.0",
|
|
38
|
+
"@opentelemetry/sdk-logs": "^0.203.0",
|
|
39
|
+
"@opentelemetry/sdk-node": "^0.203.0",
|
|
40
|
+
"@opentelemetry/sdk-trace-node": "^2.1.0",
|
|
41
|
+
"@opentelemetry/semantic-conventions": "^1.37.0",
|
|
42
|
+
"effect": "3.18.3",
|
|
43
|
+
"@dxos/log": "0.8.4-main.70d3990"
|
|
36
44
|
},
|
|
37
45
|
"peerDependencies": {
|
|
38
46
|
"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,15 @@ import {
|
|
|
12
13
|
findNode,
|
|
13
14
|
findProperty,
|
|
14
15
|
getAnnotation,
|
|
15
|
-
getDiscriminatingProps,
|
|
16
16
|
getDiscriminatedType,
|
|
17
|
+
getDiscriminatingProps,
|
|
17
18
|
getSimpleType,
|
|
19
|
+
isArrayType,
|
|
18
20
|
isOption,
|
|
19
21
|
isSimpleType,
|
|
20
22
|
visit,
|
|
21
|
-
isArrayType,
|
|
22
23
|
} from './ast';
|
|
23
|
-
import { type JsonPath, type JsonProp } from './
|
|
24
|
+
import { type JsonPath, type JsonProp } from './json-path';
|
|
24
25
|
|
|
25
26
|
const ZipCode = Schema.String.pipe(
|
|
26
27
|
Schema.pattern(/^\d{5}$/, {
|
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,8 +19,14 @@ import { type JsonPath, type JsonProp } from './jsonPath';
|
|
|
17
19
|
// https://effect-ts.github.io/effect/schema/SchemaAST.ts.html
|
|
18
20
|
//
|
|
19
21
|
|
|
22
|
+
// TODO(wittjosiah): What is a "simple type"?
|
|
20
23
|
export type SimpleType = 'object' | 'string' | 'number' | 'boolean' | 'enum' | 'literal';
|
|
21
24
|
|
|
25
|
+
const isTupleType = (node: SchemaAST.AST): boolean => {
|
|
26
|
+
// NOTE: Arrays are represented as tuples with no elements and a rest part.
|
|
27
|
+
return SchemaAST.isTupleType(node) && node.elements.length > 0;
|
|
28
|
+
};
|
|
29
|
+
|
|
22
30
|
/**
|
|
23
31
|
* Get the base type; e.g., traverse through refinements.
|
|
24
32
|
*/
|
|
@@ -27,6 +35,8 @@ export const getSimpleType = (node: SchemaAST.AST): SimpleType | undefined => {
|
|
|
27
35
|
SchemaAST.isDeclaration(node) ||
|
|
28
36
|
SchemaAST.isObjectKeyword(node) ||
|
|
29
37
|
SchemaAST.isTypeLiteral(node) ||
|
|
38
|
+
// TODO(wittjosiah): Tuples are actually arrays.
|
|
39
|
+
isTupleType(node) ||
|
|
30
40
|
isDiscriminatedUnion(node)
|
|
31
41
|
) {
|
|
32
42
|
return 'object';
|
|
@@ -127,15 +137,15 @@ const visitNode = (
|
|
|
127
137
|
path: Path = [],
|
|
128
138
|
depth = 0,
|
|
129
139
|
): VisitResult | undefined => {
|
|
130
|
-
const
|
|
140
|
+
const $result = test?.(node, path, depth);
|
|
131
141
|
const result: VisitResult =
|
|
132
|
-
|
|
142
|
+
$result === undefined
|
|
133
143
|
? VisitResult.CONTINUE
|
|
134
|
-
: typeof
|
|
135
|
-
?
|
|
144
|
+
: typeof $result === 'boolean'
|
|
145
|
+
? $result
|
|
136
146
|
? VisitResult.CONTINUE
|
|
137
147
|
: VisitResult.SKIP
|
|
138
|
-
:
|
|
148
|
+
: $result;
|
|
139
149
|
|
|
140
150
|
if (result === VisitResult.EXIT) {
|
|
141
151
|
return result;
|
|
@@ -204,6 +214,12 @@ export const findNode = (node: SchemaAST.AST, test: (node: SchemaAST.AST) => boo
|
|
|
204
214
|
return child;
|
|
205
215
|
}
|
|
206
216
|
}
|
|
217
|
+
for (const prop of getIndexSignatures(node)) {
|
|
218
|
+
const child = findNode(prop.type, test);
|
|
219
|
+
if (child) {
|
|
220
|
+
return child;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
207
223
|
}
|
|
208
224
|
|
|
209
225
|
// Tuple.
|
|
@@ -218,12 +234,14 @@ export const findNode = (node: SchemaAST.AST, test: (node: SchemaAST.AST) => boo
|
|
|
218
234
|
|
|
219
235
|
// Branching union (e.g., optional, discriminated unions).
|
|
220
236
|
else if (SchemaAST.isUnion(node)) {
|
|
221
|
-
if (
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
237
|
+
if (isLiteralUnion(node)) {
|
|
238
|
+
return undefined;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
for (const type of node.types) {
|
|
242
|
+
const child = findNode(type, test);
|
|
243
|
+
if (child) {
|
|
244
|
+
return child;
|
|
227
245
|
}
|
|
228
246
|
}
|
|
229
247
|
}
|
|
@@ -279,8 +297,8 @@ export const getAnnotation =
|
|
|
279
297
|
<T>(annotationId: symbol, noDefault = true) =>
|
|
280
298
|
(node: SchemaAST.AST): T | undefined => {
|
|
281
299
|
// 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);
|
|
300
|
+
const id = Function.pipe(SchemaAST.getIdentifierAnnotation(node), Option.getOrUndefined);
|
|
301
|
+
const value = Function.pipe(SchemaAST.getAnnotation<T>(annotationId)(node), Option.getOrUndefined);
|
|
284
302
|
if (noDefault && (value === defaultAnnotations[node._tag]?.annotations[annotationId] || value === id)) {
|
|
285
303
|
return undefined;
|
|
286
304
|
}
|
|
@@ -432,6 +450,7 @@ export const mapAst = (
|
|
|
432
450
|
),
|
|
433
451
|
),
|
|
434
452
|
ast.indexSignatures,
|
|
453
|
+
ast.annotations,
|
|
435
454
|
);
|
|
436
455
|
}
|
|
437
456
|
case 'Union': {
|
|
@@ -464,7 +483,23 @@ export const isArrayType = (node: SchemaAST.AST): boolean => {
|
|
|
464
483
|
SchemaAST.isTupleType(node) ||
|
|
465
484
|
(SchemaAST.isUnion(node) &&
|
|
466
485
|
node.types.some(isArrayType) &&
|
|
467
|
-
node.types.some(isUndefinedKeyword) &&
|
|
486
|
+
node.types.some(SchemaAST.isUndefinedKeyword) &&
|
|
468
487
|
node.types.length === 2)
|
|
469
488
|
);
|
|
470
489
|
};
|
|
490
|
+
|
|
491
|
+
const getIndexSignatures = (ast: SchemaAST.AST): Array<SchemaAST.IndexSignature> => {
|
|
492
|
+
const annotation = SchemaAST.getSurrogateAnnotation(ast);
|
|
493
|
+
if (Option.isSome(annotation)) {
|
|
494
|
+
return getIndexSignatures(annotation.value);
|
|
495
|
+
}
|
|
496
|
+
switch (ast._tag) {
|
|
497
|
+
case 'TypeLiteral':
|
|
498
|
+
return ast.indexSignatures.slice();
|
|
499
|
+
case 'Suspend':
|
|
500
|
+
return getIndexSignatures(ast.f());
|
|
501
|
+
case 'Refinement':
|
|
502
|
+
return getIndexSignatures(ast.from);
|
|
503
|
+
}
|
|
504
|
+
return [];
|
|
505
|
+
};
|
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
|
+
);
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import { describe, expect, test } from 'vitest';
|
|
6
6
|
|
|
7
|
-
import { createJsonPath, getField, isJsonPath,
|
|
7
|
+
import { type JsonPath, createJsonPath, getField, getValue, isJsonPath, setValue, splitJsonPath } from './json-path';
|
|
8
8
|
|
|
9
9
|
describe('createJsonPath', () => {
|
|
10
10
|
test('supported path subset', () => {
|
|
@@ -99,3 +99,42 @@ describe('createJsonPath', () => {
|
|
|
99
99
|
expect(getField({ a: 'foo' }, 'a' as JsonPath)).toBe('foo');
|
|
100
100
|
});
|
|
101
101
|
});
|
|
102
|
+
|
|
103
|
+
describe('Types', () => {
|
|
104
|
+
test('checks sanity', async ({ expect }) => {
|
|
105
|
+
const obj = {};
|
|
106
|
+
expect(obj).to.exist;
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
describe('get/set deep', () => {
|
|
111
|
+
test('get/set operations', ({ expect }) => {
|
|
112
|
+
const obj = {
|
|
113
|
+
name: 'test',
|
|
114
|
+
items: ['a', 'b', 'c'],
|
|
115
|
+
nested: {
|
|
116
|
+
prop: 'value',
|
|
117
|
+
arr: [1, 2, 3],
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
// Basic property access.
|
|
122
|
+
expect(getValue(obj, 'name' as JsonPath)).toBe('test');
|
|
123
|
+
|
|
124
|
+
// Array index access.
|
|
125
|
+
expect(getValue(obj, 'items[1]' as JsonPath)).toBe('b');
|
|
126
|
+
|
|
127
|
+
// Nested property access.
|
|
128
|
+
expect(getValue(obj, 'nested.prop' as JsonPath)).toBe('value');
|
|
129
|
+
|
|
130
|
+
// Nested array access.
|
|
131
|
+
expect(getValue(obj, 'nested.arr[2]' as JsonPath)).toBe(3);
|
|
132
|
+
|
|
133
|
+
// Setting values.
|
|
134
|
+
const updated1 = setValue(obj, 'items[1]' as JsonPath, 'x');
|
|
135
|
+
expect(updated1.items[1]).toBe('x');
|
|
136
|
+
|
|
137
|
+
const updated2 = setValue(obj, 'nested.arr[0]' as JsonPath, 99);
|
|
138
|
+
expect(updated2.nested.arr[0]).toBe(99);
|
|
139
|
+
});
|
|
140
|
+
});
|
|
@@ -2,20 +2,26 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import * as Option from 'effect/Option';
|
|
6
|
+
import * as Schema from 'effect/Schema';
|
|
6
7
|
import { JSONPath } from 'jsonpath-plus';
|
|
7
8
|
|
|
8
9
|
import { invariant } from '@dxos/invariant';
|
|
10
|
+
import { getDeep, setDeep } from '@dxos/util';
|
|
9
11
|
|
|
10
12
|
export type JsonProp = string & { __JsonPath: true; __JsonProp: true };
|
|
11
13
|
export type JsonPath = string & { __JsonPath: true };
|
|
12
14
|
|
|
15
|
+
// TODO(burdon): Start with "$."?
|
|
16
|
+
|
|
13
17
|
const PATH_REGEX = /^($|[a-zA-Z_$][\w$]*(?:\.[a-zA-Z_$][\w$]*|\[\d+\](?:\.)?)*$)/;
|
|
18
|
+
|
|
14
19
|
const PROP_REGEX = /^\w+$/;
|
|
15
20
|
|
|
16
21
|
/**
|
|
17
22
|
* https://www.ietf.org/archive/id/draft-goessner-dispatch-jsonpath-00.html
|
|
18
23
|
*/
|
|
24
|
+
// TODO(burdon): Keys could be arbitrary strings.
|
|
19
25
|
export const JsonPath = Schema.String.pipe(Schema.pattern(PATH_REGEX)).annotations({
|
|
20
26
|
title: 'JSON path',
|
|
21
27
|
description: 'JSON path to a property',
|
|
@@ -94,7 +100,29 @@ export const splitJsonPath = (path: JsonPath): string[] => {
|
|
|
94
100
|
/**
|
|
95
101
|
* Applies a JsonPath to an object.
|
|
96
102
|
*/
|
|
103
|
+
// TODO(burdon): Reconcile with getValue.
|
|
97
104
|
export const getField = (object: any, path: JsonPath): any => {
|
|
98
105
|
// By default, JSONPath returns an array of results.
|
|
99
106
|
return JSONPath({ path, json: object })[0];
|
|
100
107
|
};
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Get value from object using JsonPath.
|
|
111
|
+
*/
|
|
112
|
+
export const getValue = <T extends object>(obj: T, path: JsonPath): any => {
|
|
113
|
+
return getDeep(
|
|
114
|
+
obj,
|
|
115
|
+
splitJsonPath(path).map((p) => p.replace(/[[\]]/g, '')),
|
|
116
|
+
);
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Set value on object using JsonPath.
|
|
121
|
+
*/
|
|
122
|
+
export const setValue = <T extends object>(obj: T, path: JsonPath, value: any): T => {
|
|
123
|
+
return setDeep(
|
|
124
|
+
obj,
|
|
125
|
+
splitJsonPath(path).map((p) => p.replace(/[[\]]/g, '')),
|
|
126
|
+
value,
|
|
127
|
+
);
|
|
128
|
+
};
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { it } from '@effect/vitest';
|
|
6
|
+
import * as Context from 'effect/Context';
|
|
7
|
+
import * as Duration from 'effect/Duration';
|
|
8
|
+
import * as Effect from 'effect/Effect';
|
|
9
|
+
import * as Layer from 'effect/Layer';
|
|
10
|
+
import * as ManagedRuntime from 'effect/ManagedRuntime';
|
|
11
|
+
import { test } from 'vitest';
|
|
12
|
+
|
|
13
|
+
class ClientConfig extends Context.Tag('ClientConfig')<ClientConfig, { endpoint: string }>() {}
|
|
14
|
+
|
|
15
|
+
class Client extends Context.Tag('Client')<Client, { call: () => Effect.Effect<void> }>() {
|
|
16
|
+
static layer = Layer.effect(
|
|
17
|
+
Client,
|
|
18
|
+
Effect.gen(function* () {
|
|
19
|
+
const config = yield* ClientConfig;
|
|
20
|
+
return {
|
|
21
|
+
call: () => {
|
|
22
|
+
console.log('called', config.endpoint);
|
|
23
|
+
return Effect.void;
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
}),
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const ServerLive = Layer.scoped(
|
|
31
|
+
ClientConfig,
|
|
32
|
+
Effect.gen(function* () {
|
|
33
|
+
console.log('start server');
|
|
34
|
+
|
|
35
|
+
yield* Effect.sleep(Duration.millis(100));
|
|
36
|
+
|
|
37
|
+
yield* Effect.addFinalizer(
|
|
38
|
+
Effect.fn(function* () {
|
|
39
|
+
yield* Effect.sleep(Duration.millis(100));
|
|
40
|
+
console.log('stop server');
|
|
41
|
+
}),
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
endpoint: 'http://localhost:8080',
|
|
46
|
+
};
|
|
47
|
+
}),
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
it.effect.skip(
|
|
51
|
+
'test',
|
|
52
|
+
Effect.fn(
|
|
53
|
+
function* (_) {
|
|
54
|
+
const client = yield* Client;
|
|
55
|
+
yield* client.call();
|
|
56
|
+
},
|
|
57
|
+
Effect.provide(Layer.provide(Client.layer, ServerLive)),
|
|
58
|
+
),
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
class ServerPlugin {
|
|
62
|
+
#runtime = ManagedRuntime.make(ServerLive);
|
|
63
|
+
|
|
64
|
+
readonly clientConfigLayer = Layer.effectContext(
|
|
65
|
+
this.#runtime.runtimeEffect.pipe(Effect.map((rt) => rt.context.pipe(Context.pick(ClientConfig)))),
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
async dispose() {
|
|
69
|
+
await this.#runtime.dispose();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
class ClientPlugin {
|
|
74
|
+
constructor(private readonly _serverPlugin: ServerPlugin) {}
|
|
75
|
+
|
|
76
|
+
async run() {
|
|
77
|
+
const layer = Layer.provide(Client.layer, this._serverPlugin.clientConfigLayer);
|
|
78
|
+
|
|
79
|
+
await Effect.runPromise(
|
|
80
|
+
Effect.gen(function* () {
|
|
81
|
+
const client = yield* Client;
|
|
82
|
+
yield* client.call();
|
|
83
|
+
}).pipe(Effect.provide(layer)),
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
test.skip('plugins', async () => {
|
|
89
|
+
const serverPlugin = new ServerPlugin();
|
|
90
|
+
console.log('ServerPlugin created');
|
|
91
|
+
|
|
92
|
+
await Effect.runPromise(Effect.sleep(Duration.millis(500)));
|
|
93
|
+
console.log('wake up');
|
|
94
|
+
|
|
95
|
+
{
|
|
96
|
+
const clientPlugin1 = new ClientPlugin(serverPlugin);
|
|
97
|
+
console.log('ClientPlugin1 created');
|
|
98
|
+
await clientPlugin1.run();
|
|
99
|
+
console.log('client1 run');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
{
|
|
103
|
+
const clientPlugin2 = new ClientPlugin(serverPlugin);
|
|
104
|
+
console.log('ClientPlugin2 created');
|
|
105
|
+
await clientPlugin2.run();
|
|
106
|
+
console.log('client2 run');
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
await serverPlugin.dispose();
|
|
110
|
+
});
|