@dxos/effect 0.8.4-main.2e9d522 → 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.
Files changed (59) hide show
  1. package/dist/lib/browser/index.mjs +271 -230
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/browser/testing.mjs +37 -0
  5. package/dist/lib/browser/testing.mjs.map +7 -0
  6. package/dist/lib/node-esm/index.mjs +271 -230
  7. package/dist/lib/node-esm/index.mjs.map +4 -4
  8. package/dist/lib/node-esm/meta.json +1 -1
  9. package/dist/lib/node-esm/testing.mjs +37 -0
  10. package/dist/lib/node-esm/testing.mjs.map +7 -0
  11. package/dist/types/src/ast.d.ts +35 -22
  12. package/dist/types/src/ast.d.ts.map +1 -1
  13. package/dist/types/src/context.d.ts +2 -1
  14. package/dist/types/src/context.d.ts.map +1 -1
  15. package/dist/types/src/errors.d.ts +31 -1
  16. package/dist/types/src/errors.d.ts.map +1 -1
  17. package/dist/types/src/index.d.ts +2 -3
  18. package/dist/types/src/index.d.ts.map +1 -1
  19. package/dist/types/src/interrupt.test.d.ts +2 -0
  20. package/dist/types/src/interrupt.test.d.ts.map +1 -0
  21. package/dist/types/src/{jsonPath.d.ts → json-path.d.ts} +10 -2
  22. package/dist/types/src/json-path.d.ts.map +1 -0
  23. package/dist/types/src/json-path.test.d.ts +2 -0
  24. package/dist/types/src/json-path.test.d.ts.map +1 -0
  25. package/dist/types/src/layers.test.d.ts +2 -0
  26. package/dist/types/src/layers.test.d.ts.map +1 -0
  27. package/dist/types/src/otel.d.ts +17 -0
  28. package/dist/types/src/otel.d.ts.map +1 -0
  29. package/dist/types/src/otel.test.d.ts +2 -0
  30. package/dist/types/src/otel.test.d.ts.map +1 -0
  31. package/dist/types/src/resource.d.ts +6 -2
  32. package/dist/types/src/resource.d.ts.map +1 -1
  33. package/dist/types/src/testing.d.ts +33 -1
  34. package/dist/types/src/testing.d.ts.map +1 -1
  35. package/dist/types/src/url.d.ts +3 -1
  36. package/dist/types/src/url.d.ts.map +1 -1
  37. package/dist/types/tsconfig.tsbuildinfo +1 -1
  38. package/package.json +27 -8
  39. package/src/ast.test.ts +14 -11
  40. package/src/ast.ts +118 -92
  41. package/src/context.ts +2 -1
  42. package/src/errors.test.ts +1 -1
  43. package/src/errors.ts +73 -20
  44. package/src/index.ts +2 -3
  45. package/src/interrupt.test.ts +33 -0
  46. package/src/{jsonPath.test.ts → json-path.test.ts} +40 -1
  47. package/src/{jsonPath.ts → json-path.ts} +29 -1
  48. package/src/layers.test.ts +110 -0
  49. package/src/otel.test.ts +126 -0
  50. package/src/otel.ts +45 -0
  51. package/src/resource.test.ts +5 -4
  52. package/src/resource.ts +8 -3
  53. package/src/sanity.test.ts +24 -11
  54. package/src/testing.ts +53 -1
  55. package/src/url.test.ts +1 -1
  56. package/src/url.ts +5 -2
  57. package/dist/types/src/jsonPath.d.ts.map +0 -1
  58. package/dist/types/src/jsonPath.test.d.ts +0 -2
  59. package/dist/types/src/jsonPath.test.d.ts.map +0 -1
@@ -1,70 +1,60 @@
1
1
  import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
2
2
 
3
3
  // src/ast.ts
4
- import { Option, pipe, SchemaAST, Schema } from "effect";
5
- import { isUndefinedKeyword } from "effect/SchemaAST";
4
+ import * as Function from "effect/Function";
5
+ import * as Option from "effect/Option";
6
+ import * as Schema from "effect/Schema";
7
+ import * as SchemaAST from "effect/SchemaAST";
6
8
  import { invariant } from "@dxos/invariant";
7
9
  import { isNonNullable } from "@dxos/util";
8
10
  var __dxlog_file = "/__w/dxos/dxos/packages/common/effect/src/ast.ts";
9
- var getSimpleType = (node) => {
10
- if (SchemaAST.isDeclaration(node) || SchemaAST.isObjectKeyword(node) || SchemaAST.isTypeLiteral(node) || isDiscriminatedUnion(node)) {
11
- return "object";
12
- }
13
- if (SchemaAST.isStringKeyword(node)) {
14
- return "string";
15
- }
16
- if (SchemaAST.isNumberKeyword(node)) {
17
- return "number";
18
- }
19
- if (SchemaAST.isBooleanKeyword(node)) {
20
- return "boolean";
21
- }
22
- if (SchemaAST.isEnums(node)) {
23
- return "enum";
24
- }
25
- if (SchemaAST.isLiteral(node)) {
26
- return "literal";
27
- }
28
- };
29
- var isSimpleType = (node) => !!getSimpleType(node);
30
- (function(SimpleType2) {
31
- SimpleType2.getDefaultValue = (type) => {
32
- switch (type) {
33
- case "string": {
34
- return "";
35
- }
36
- case "number": {
37
- return 0;
38
- }
39
- case "boolean": {
40
- return false;
11
+ var reduceRefinements = (type, refinements = []) => {
12
+ if (SchemaAST.isRefinement(type)) {
13
+ const annotations = type.annotations;
14
+ const filter = type.filter;
15
+ const nextType = {
16
+ ...type.from,
17
+ annotations: {
18
+ ...type.annotations,
19
+ ...annotations
41
20
  }
42
- case "object": {
43
- return {};
44
- }
45
- default: {
46
- throw new Error(`Unsupported type for default value: ${type}`);
47
- }
48
- }
21
+ };
22
+ return reduceRefinements(nextType, [
23
+ ...refinements,
24
+ filter
25
+ ]);
26
+ }
27
+ return {
28
+ type,
29
+ refinements
49
30
  };
50
- })(SimpleType || (SimpleType = {}));
51
- var VisitResult = /* @__PURE__ */ function(VisitResult2) {
31
+ };
32
+ var getBaseType = (prop) => {
33
+ const encoded = SchemaAST.encodedBoundAST(prop.type);
34
+ const unwrapped = prop.isOptional && encoded._tag === "Union" ? encoded.types[0] : encoded;
35
+ return reduceRefinements(unwrapped);
36
+ };
37
+ var getProperties = (ast) => {
38
+ const properties = SchemaAST.getPropertySignatures(ast);
39
+ return properties.map((prop) => ({
40
+ ...getBaseType(prop),
41
+ name: prop.name,
42
+ isOptional: prop.isOptional,
43
+ isReadonly: prop.isReadonly
44
+ }));
45
+ };
46
+ var VisitResult = /* @__PURE__ */ (function(VisitResult2) {
52
47
  VisitResult2[VisitResult2["CONTINUE"] = 0] = "CONTINUE";
53
48
  VisitResult2[VisitResult2["SKIP"] = 1] = "SKIP";
54
49
  VisitResult2[VisitResult2["EXIT"] = 2] = "EXIT";
55
50
  return VisitResult2;
56
- }({});
57
- var defaultTest = isSimpleType;
51
+ })({});
58
52
  var visit = (node, testOrVisitor, visitor) => {
59
- if (!visitor) {
60
- visitNode(node, defaultTest, testOrVisitor);
61
- } else {
62
- visitNode(node, testOrVisitor, visitor);
63
- }
53
+ visitNode(node, testOrVisitor, visitor);
64
54
  };
65
55
  var visitNode = (node, test, visitor, path = [], depth = 0) => {
66
- const _result = test?.(node, path, depth);
67
- const result = _result === void 0 ? 0 : typeof _result === "boolean" ? _result ? 0 : 1 : _result;
56
+ const $result = test?.(node, path, depth);
57
+ const result = $result === void 0 ? 0 : typeof $result === "boolean" ? $result ? 0 : 1 : $result;
68
58
  if (result === 2) {
69
59
  return result;
70
60
  }
@@ -117,6 +107,12 @@ var findNode = (node, test) => {
117
107
  return child;
118
108
  }
119
109
  }
110
+ for (const prop of getIndexSignatures(node)) {
111
+ const child = findNode(prop.type, test);
112
+ if (child) {
113
+ return child;
114
+ }
115
+ }
120
116
  } else if (SchemaAST.isTupleType(node)) {
121
117
  for (const [_, element] of node.elements.entries()) {
122
118
  const child = findNode(element.type, test);
@@ -125,12 +121,13 @@ var findNode = (node, test) => {
125
121
  }
126
122
  }
127
123
  } else if (SchemaAST.isUnion(node)) {
128
- if (isOption(node)) {
129
- for (const type of node.types) {
130
- const child = findNode(type, test);
131
- if (child) {
132
- return child;
133
- }
124
+ if (isLiteralUnion(node)) {
125
+ return void 0;
126
+ }
127
+ for (const type of node.types) {
128
+ const child = findNode(type, test);
129
+ if (child) {
130
+ return child;
134
131
  }
135
132
  }
136
133
  } else if (SchemaAST.isRefinement(node)) {
@@ -143,7 +140,7 @@ var findProperty = (schema, path) => {
143
140
  const typeNode = findNode(node, SchemaAST.isTypeLiteral);
144
141
  invariant(typeNode, void 0, {
145
142
  F: __dxlog_file,
146
- L: 247,
143
+ L: 234,
147
144
  S: void 0,
148
145
  A: [
149
146
  "typeNode",
@@ -168,16 +165,16 @@ var defaultAnnotations = {
168
165
  ["NumberKeyword"]: SchemaAST.numberKeyword,
169
166
  ["BooleanKeyword"]: SchemaAST.booleanKeyword
170
167
  };
171
- var getAnnotation = (annotationId, noDefault = true) => (node) => {
172
- const id = pipe(SchemaAST.getIdentifierAnnotation(node), Option.getOrUndefined);
173
- const value = pipe(SchemaAST.getAnnotation(annotationId)(node), Option.getOrUndefined);
168
+ var getAnnotation2 = (annotationId, noDefault = true) => (node) => {
169
+ const id = Function.pipe(SchemaAST.getIdentifierAnnotation(node), Option.getOrUndefined);
170
+ const value = Function.pipe(SchemaAST.getAnnotation(annotationId)(node), Option.getOrUndefined);
174
171
  if (noDefault && (value === defaultAnnotations[node._tag]?.annotations[annotationId] || value === id)) {
175
172
  return void 0;
176
173
  }
177
174
  return value;
178
175
  };
179
176
  var findAnnotation = (node, annotationId, noDefault = true) => {
180
- const getAnnotationById = getAnnotation(annotationId, noDefault);
177
+ const getAnnotationById = getAnnotation2(annotationId, noDefault);
181
178
  const getBaseAnnotation = (node2) => {
182
179
  const value = getAnnotationById(node2);
183
180
  if (value !== void 0) {
@@ -197,13 +194,22 @@ var isOption = (node) => {
197
194
  var isLiteralUnion = (node) => {
198
195
  return SchemaAST.isUnion(node) && node.types.every(SchemaAST.isLiteral);
199
196
  };
197
+ var isArrayType = (node) => {
198
+ return SchemaAST.isTupleType(node) && node.elements.length === 0 && node.rest.length === 1;
199
+ };
200
+ var getArrayElementType = (node) => {
201
+ return isArrayType(node) ? node.rest.at(0)?.type : void 0;
202
+ };
203
+ var isTupleType2 = (node) => {
204
+ return SchemaAST.isTupleType(node) && node.elements.length > 0;
205
+ };
200
206
  var isDiscriminatedUnion = (node) => {
201
207
  return SchemaAST.isUnion(node) && !!getDiscriminatingProps(node)?.length;
202
208
  };
203
209
  var getDiscriminatingProps = (node) => {
204
210
  invariant(SchemaAST.isUnion(node), void 0, {
205
211
  F: __dxlog_file,
206
- L: 344,
212
+ L: 352,
207
213
  S: void 0,
208
214
  A: [
209
215
  "SchemaAST.isUnion(node)",
@@ -221,7 +227,7 @@ var getDiscriminatingProps = (node) => {
221
227
  var getDiscriminatedType = (node, value = {}) => {
222
228
  invariant(SchemaAST.isUnion(node), void 0, {
223
229
  F: __dxlog_file,
224
- L: 368,
230
+ L: 376,
225
231
  S: void 0,
226
232
  A: [
227
233
  "SchemaAST.isUnion(node)",
@@ -230,7 +236,7 @@ var getDiscriminatedType = (node, value = {}) => {
230
236
  });
231
237
  invariant(value, void 0, {
232
238
  F: __dxlog_file,
233
- L: 369,
239
+ L: 377,
234
240
  S: void 0,
235
241
  A: [
236
242
  "value",
@@ -245,7 +251,7 @@ var getDiscriminatedType = (node, value = {}) => {
245
251
  const match = SchemaAST.getPropertySignatures(type).filter((prop) => props?.includes(prop.name.toString())).every((prop) => {
246
252
  invariant(SchemaAST.isLiteral(prop.type), void 0, {
247
253
  F: __dxlog_file,
248
- L: 380,
254
+ L: 388,
249
255
  S: void 0,
250
256
  A: [
251
257
  "SchemaAST.isLiteral(prop.type)",
@@ -263,7 +269,7 @@ var getDiscriminatedType = (node, value = {}) => {
263
269
  const literal = SchemaAST.getPropertySignatures(type).find((p) => p.name.toString() === prop);
264
270
  invariant(SchemaAST.isLiteral(literal.type), void 0, {
265
271
  F: __dxlog_file,
266
- L: 398,
272
+ L: 406,
267
273
  S: void 0,
268
274
  A: [
269
275
  "SchemaAST.isLiteral(literal.type)",
@@ -280,10 +286,14 @@ var getDiscriminatedType = (node, value = {}) => {
280
286
  const schema = Schema.Struct(fields);
281
287
  return schema.ast;
282
288
  };
289
+ var isNestedType = (node) => {
290
+ return SchemaAST.isDeclaration(node) || SchemaAST.isObjectKeyword(node) || SchemaAST.isTypeLiteral(node) || // TODO(wittjosiah): Tuples are actually arrays.
291
+ isTupleType2(node) || isDiscriminatedUnion(node);
292
+ };
283
293
  var mapAst = (ast, f) => {
284
294
  switch (ast._tag) {
285
295
  case "TypeLiteral": {
286
- return new SchemaAST.TypeLiteral(ast.propertySignatures.map((prop) => new SchemaAST.PropertySignature(prop.name, f(prop.type, prop.name), prop.isOptional, prop.isReadonly, prop.annotations)), ast.indexSignatures);
296
+ return new SchemaAST.TypeLiteral(ast.propertySignatures.map((prop) => new SchemaAST.PropertySignature(prop.name, f(prop.type, prop.name), prop.isOptional, prop.isReadonly, prop.annotations)), ast.indexSignatures, ast.annotations);
287
297
  }
288
298
  case "Union": {
289
299
  return SchemaAST.Union.make(ast.types.map(f), ast.annotations);
@@ -300,142 +310,42 @@ var mapAst = (ast, f) => {
300
310
  }
301
311
  }
302
312
  };
303
- var isArrayType = (node) => {
304
- return SchemaAST.isTupleType(node) || SchemaAST.isUnion(node) && node.types.some(isArrayType) && node.types.some(isUndefinedKeyword) && node.types.length === 2;
305
- };
306
- var SimpleType;
307
-
308
- // src/jsonPath.ts
309
- import { Schema as Schema2, Option as Option2 } from "effect";
310
- import { JSONPath } from "jsonpath-plus";
311
- import { invariant as invariant2 } from "@dxos/invariant";
312
- var __dxlog_file2 = "/__w/dxos/dxos/packages/common/effect/src/jsonPath.ts";
313
- var PATH_REGEX = /^($|[a-zA-Z_$][\w$]*(?:\.[a-zA-Z_$][\w$]*|\[\d+\](?:\.)?)*$)/;
314
- var PROP_REGEX = /^\w+$/;
315
- var JsonPath = Schema2.String.pipe(Schema2.pattern(PATH_REGEX)).annotations({
316
- title: "JSON path",
317
- description: "JSON path to a property"
318
- });
319
- var JsonProp = Schema2.NonEmptyString.pipe(Schema2.pattern(PROP_REGEX, {
320
- message: () => "Property name must contain only letters, numbers, and underscores"
321
- }));
322
- var isJsonPath = (value) => {
323
- return Option2.isSome(Schema2.validateOption(JsonPath)(value));
324
- };
325
- var createJsonPath = (path) => {
326
- const candidatePath = path.map((p, i) => {
327
- if (typeof p === "number") {
328
- return `[${p}]`;
329
- } else {
330
- return i === 0 ? p : `.${p}`;
331
- }
332
- }).join("");
333
- invariant2(isJsonPath(candidatePath), `Invalid JsonPath: ${candidatePath}`, {
334
- F: __dxlog_file2,
335
- L: 63,
336
- S: void 0,
337
- A: [
338
- "isJsonPath(candidatePath)",
339
- "`Invalid JsonPath: ${candidatePath}`"
340
- ]
341
- });
342
- return candidatePath;
343
- };
344
- var fromEffectValidationPath = (effectPath) => {
345
- const jsonPath = effectPath.replace(/\.\[(\d+)\]/g, "[$1]");
346
- invariant2(isJsonPath(jsonPath), `Invalid JsonPath: ${jsonPath}`, {
347
- F: __dxlog_file2,
348
- L: 74,
349
- S: void 0,
350
- A: [
351
- "isJsonPath(jsonPath)",
352
- "`Invalid JsonPath: ${jsonPath}`"
353
- ]
354
- });
355
- return jsonPath;
356
- };
357
- var splitJsonPath = (path) => {
358
- if (!isJsonPath(path)) {
359
- return [];
360
- }
361
- return path.match(/[a-zA-Z_$][\w$]*|\[\d+\]/g)?.map((part) => part.startsWith("[") ? part.replace(/[[\]]/g, "") : part) ?? [];
362
- };
363
- var getField = (object, path) => {
364
- return JSONPath({
365
- path,
366
- json: object
367
- })[0];
368
- };
369
-
370
- // src/url.ts
371
- import { SchemaAST as SchemaAST2, Option as Option3, pipe as pipe2 } from "effect";
372
- import { decamelize } from "@dxos/util";
373
- var ParamKeyAnnotationId = Symbol.for("@dxos/schema/annotation/ParamKey");
374
- var getParamKeyAnnotation = SchemaAST2.getAnnotation(ParamKeyAnnotationId);
375
- var ParamKeyAnnotation = (value) => (self) => self.annotations({
376
- [ParamKeyAnnotationId]: value
377
- });
378
- var UrlParser = class {
379
- constructor(_schema) {
380
- this._schema = _schema;
313
+ var getIndexSignatures = (ast) => {
314
+ const annotation = SchemaAST.getSurrogateAnnotation(ast);
315
+ if (Option.isSome(annotation)) {
316
+ return getIndexSignatures(annotation.value);
381
317
  }
382
- /**
383
- * Parse URL params.
384
- */
385
- parse(_url) {
386
- const url = new URL(_url);
387
- return Object.entries(this._schema.fields).reduce((params, [key, type]) => {
388
- let value = url.searchParams.get(decamelize(key));
389
- if (value == null) {
390
- value = url.searchParams.get(key);
391
- }
392
- if (value != null) {
393
- if (SchemaAST2.isNumberKeyword(type.ast)) {
394
- params[key] = parseInt(value);
395
- } else if (SchemaAST2.isBooleanKeyword(type.ast)) {
396
- params[key] = value === "true" || value === "1";
397
- } else {
398
- params[key] = value;
399
- }
400
- }
401
- return params;
402
- }, {});
403
- }
404
- /**
405
- * Return URL with encoded params.
406
- */
407
- create(_url, params) {
408
- const url = new URL(_url);
409
- Object.entries(params).forEach(([key, value]) => {
410
- if (value !== void 0) {
411
- const field = this._schema.fields[key];
412
- if (field) {
413
- const { key: serializedKey } = pipe2(getParamKeyAnnotation(field.ast), Option3.getOrElse(() => ({
414
- key: decamelize(key)
415
- })));
416
- url.searchParams.set(serializedKey, String(value));
417
- }
418
- }
419
- });
420
- return url;
318
+ switch (ast._tag) {
319
+ case "TypeLiteral":
320
+ return ast.indexSignatures.slice();
321
+ case "Suspend":
322
+ return getIndexSignatures(ast.f());
323
+ case "Refinement":
324
+ return getIndexSignatures(ast.from);
421
325
  }
326
+ return [];
422
327
  };
423
328
 
424
329
  // src/context.ts
425
- import { Effect } from "effect";
330
+ import * as Effect from "effect/Effect";
426
331
  import { Context } from "@dxos/context";
427
- var __dxlog_file3 = "/__w/dxos/dxos/packages/common/effect/src/context.ts";
332
+ var __dxlog_file2 = "/__w/dxos/dxos/packages/common/effect/src/context.ts";
428
333
  var contextFromScope = () => Effect.gen(function* () {
429
334
  const ctx = new Context(void 0, {
430
- F: __dxlog_file3,
431
- L: 12
335
+ F: __dxlog_file2,
336
+ L: 13
432
337
  });
433
338
  yield* Effect.addFinalizer(() => Effect.promise(() => ctx.dispose()));
434
339
  return ctx;
435
340
  });
436
341
 
437
342
  // src/errors.ts
438
- import { Cause, Chunk, Effect as Effect2, Exit, GlobalValue, Option as Option4 } from "effect";
343
+ import * as Cause from "effect/Cause";
344
+ import * as Chunk from "effect/Chunk";
345
+ import * as Effect2 from "effect/Effect";
346
+ import * as Exit from "effect/Exit";
347
+ import * as GlobalValue from "effect/GlobalValue";
348
+ import * as Option2 from "effect/Option";
439
349
  var spanSymbol = Symbol.for("effect/SpanAnnotation");
440
350
  var originalSymbol = Symbol.for("effect/OriginalAnnotation");
441
351
  var spanToTrace = GlobalValue.globalValue("effect/Tracer/spanToTrace", () => /* @__PURE__ */ new WeakMap());
@@ -486,7 +396,7 @@ var prettyErrorStack = (error, appendStacks = []) => {
486
396
  } else {
487
397
  out.push(` at ${current.name}`);
488
398
  }
489
- current = Option4.getOrUndefined(current.parent);
399
+ current = Option2.getOrUndefined(current.parent);
490
400
  i++;
491
401
  }
492
402
  }
@@ -505,19 +415,15 @@ var prettyErrorStack = (error, appendStacks = []) => {
505
415
  });
506
416
  return error;
507
417
  };
508
- var runAndForwardErrors = async (effect, options) => {
509
- const exit = await Effect2.runPromiseExit(effect, options);
510
- if (Exit.isSuccess(exit)) {
511
- return exit.value;
512
- }
513
- if (Cause.isEmpty(exit.cause)) {
514
- throw new Error("Fiber failed without a cause");
515
- } else if (Cause.isInterrupted(exit.cause)) {
516
- throw new Error("Fiber was interrupted");
418
+ var causeToError = (cause) => {
419
+ if (Cause.isEmpty(cause)) {
420
+ return new Error("Fiber failed without a cause");
421
+ } else if (Cause.isInterruptedOnly(cause)) {
422
+ return new Error("Fiber was interrupted");
517
423
  } else {
518
424
  const errors = [
519
- ...Chunk.toArray(Cause.failures(exit.cause)),
520
- ...Chunk.toArray(Cause.defects(exit.cause))
425
+ ...Chunk.toArray(Cause.failures(cause)),
426
+ ...Chunk.toArray(Cause.defects(cause))
521
427
  ];
522
428
  const getStackFrames = () => {
523
429
  const o = {};
@@ -527,76 +433,211 @@ var runAndForwardErrors = async (effect, options) => {
527
433
  const stackFrames = getStackFrames();
528
434
  const newErrors = errors.map((error) => prettyErrorStack(error, stackFrames));
529
435
  if (newErrors.length === 1) {
530
- throw newErrors[0];
436
+ return newErrors[0];
531
437
  } else {
532
- throw new AggregateError(newErrors);
438
+ return new AggregateError(newErrors);
533
439
  }
534
440
  }
535
441
  };
442
+ var throwCause = (cause) => {
443
+ throw causeToError(cause);
444
+ };
445
+ var unwrapExit = (exit) => {
446
+ if (Exit.isSuccess(exit)) {
447
+ return exit.value;
448
+ }
449
+ return throwCause(exit.cause);
450
+ };
451
+ var runAndForwardErrors = async (effect, options) => {
452
+ const exit = await Effect2.runPromiseExit(effect, options);
453
+ return unwrapExit(exit);
454
+ };
455
+ var promiseWithCauseCapture = (evaluate) => Effect2.promise(async (signal) => {
456
+ try {
457
+ const result = await evaluate(signal);
458
+ return Effect2.succeed(result);
459
+ } catch (err) {
460
+ return Effect2.die(err);
461
+ }
462
+ }).pipe(Effect2.flatten);
536
463
 
537
- // src/testing.ts
538
- import { Effect as Effect3 } from "effect";
539
- (function(TestHelpers2) {
540
- TestHelpers2.runIf = (condition) => (effect, ctx) => Effect3.gen(function* () {
541
- if (!condition) {
542
- ctx.skip();
464
+ // src/json-path.ts
465
+ import * as Option3 from "effect/Option";
466
+ import * as Schema2 from "effect/Schema";
467
+ import { JSONPath } from "jsonpath-plus";
468
+ import { invariant as invariant2 } from "@dxos/invariant";
469
+ import { getDeep, setDeep } from "@dxos/util";
470
+ var __dxlog_file3 = "/__w/dxos/dxos/packages/common/effect/src/json-path.ts";
471
+ var PATH_REGEX = /^($|[a-zA-Z_$][\w$]*(?:\.[a-zA-Z_$][\w$]*|\[\d+\](?:\.)?)*$)/;
472
+ var PROP_REGEX = /^\w+$/;
473
+ var JsonPath = Schema2.String.pipe(Schema2.pattern(PATH_REGEX)).annotations({
474
+ title: "JSON path",
475
+ description: "JSON path to a property"
476
+ });
477
+ var JsonProp = Schema2.NonEmptyString.pipe(Schema2.pattern(PROP_REGEX, {
478
+ message: () => "Property name must contain only letters, numbers, and underscores"
479
+ }));
480
+ var isJsonPath = (value) => {
481
+ return Option3.isSome(Schema2.validateOption(JsonPath)(value));
482
+ };
483
+ var createJsonPath = (path) => {
484
+ const candidatePath = path.map((p, i) => {
485
+ if (typeof p === "number") {
486
+ return `[${p}]`;
543
487
  } else {
544
- return yield* effect;
488
+ return i === 0 ? p : `.${p}`;
545
489
  }
490
+ }).join("");
491
+ invariant2(isJsonPath(candidatePath), `Invalid JsonPath: ${candidatePath}`, {
492
+ F: __dxlog_file3,
493
+ L: 69,
494
+ S: void 0,
495
+ A: [
496
+ "isJsonPath(candidatePath)",
497
+ "`Invalid JsonPath: ${candidatePath}`"
498
+ ]
546
499
  });
547
- TestHelpers2.skipIf = (condition) => (effect, ctx) => Effect3.gen(function* () {
548
- if (condition) {
549
- ctx.skip();
550
- } else {
551
- return yield* effect;
552
- }
500
+ return candidatePath;
501
+ };
502
+ var fromEffectValidationPath = (effectPath) => {
503
+ const jsonPath = effectPath.replace(/\.\[(\d+)\]/g, "[$1]");
504
+ invariant2(isJsonPath(jsonPath), `Invalid JsonPath: ${jsonPath}`, {
505
+ F: __dxlog_file3,
506
+ L: 80,
507
+ S: void 0,
508
+ A: [
509
+ "isJsonPath(jsonPath)",
510
+ "`Invalid JsonPath: ${jsonPath}`"
511
+ ]
553
512
  });
554
- })(TestHelpers || (TestHelpers = {}));
555
- var TestHelpers;
513
+ return jsonPath;
514
+ };
515
+ var splitJsonPath = (path) => {
516
+ if (!isJsonPath(path)) {
517
+ return [];
518
+ }
519
+ return path.match(/[a-zA-Z_$][\w$]*|\[\d+\]/g)?.map((part) => part.startsWith("[") ? part.replace(/[[\]]/g, "") : part) ?? [];
520
+ };
521
+ var getField = (object, path) => {
522
+ return JSONPath({
523
+ path,
524
+ json: object
525
+ })[0];
526
+ };
527
+ var getValue = (obj, path) => {
528
+ return getDeep(obj, splitJsonPath(path).map((p) => p.replace(/[[\]]/g, "")));
529
+ };
530
+ var setValue = (obj, path, value) => {
531
+ return setDeep(obj, splitJsonPath(path).map((p) => p.replace(/[[\]]/g, "")), value);
532
+ };
556
533
 
557
534
  // src/resource.ts
558
- import { Effect as Effect4 } from "effect";
559
- var accuireReleaseResource = (getResource) => Effect4.acquireRelease(Effect4.gen(function* () {
535
+ import * as Effect3 from "effect/Effect";
536
+ var acquireReleaseResource = (getResource) => Effect3.acquireRelease(Effect3.gen(function* () {
560
537
  const resource = getResource();
561
- yield* Effect4.promise(async () => {
538
+ yield* Effect3.promise(async () => {
562
539
  resource.open?.();
563
540
  return void 0;
564
541
  });
565
542
  return resource;
566
- }), (resource) => Effect4.promise(async () => {
543
+ }), (resource) => Effect3.promise(async () => {
567
544
  resource.close?.();
568
545
  return void 0;
569
546
  }));
547
+
548
+ // src/url.ts
549
+ import * as Function2 from "effect/Function";
550
+ import * as Option4 from "effect/Option";
551
+ import * as SchemaAST2 from "effect/SchemaAST";
552
+ import { decamelize } from "@dxos/util";
553
+ var ParamKeyAnnotationId = Symbol.for("@dxos/schema/annotation/ParamKey");
554
+ var getParamKeyAnnotation = SchemaAST2.getAnnotation(ParamKeyAnnotationId);
555
+ var ParamKeyAnnotation = (value) => (self) => self.annotations({
556
+ [ParamKeyAnnotationId]: value
557
+ });
558
+ var UrlParser = class {
559
+ _schema;
560
+ constructor(_schema) {
561
+ this._schema = _schema;
562
+ }
563
+ /**
564
+ * Parse URL params.
565
+ */
566
+ parse(_url) {
567
+ const url = new URL(_url);
568
+ return Object.entries(this._schema.fields).reduce((params, [key, type]) => {
569
+ let value = url.searchParams.get(decamelize(key));
570
+ if (value == null) {
571
+ value = url.searchParams.get(key);
572
+ }
573
+ if (value != null) {
574
+ if (SchemaAST2.isNumberKeyword(type.ast)) {
575
+ params[key] = parseInt(value);
576
+ } else if (SchemaAST2.isBooleanKeyword(type.ast)) {
577
+ params[key] = value === "true" || value === "1";
578
+ } else {
579
+ params[key] = value;
580
+ }
581
+ }
582
+ return params;
583
+ }, {});
584
+ }
585
+ /**
586
+ * Return URL with encoded params.
587
+ */
588
+ create(_url, params) {
589
+ const url = new URL(_url);
590
+ Object.entries(params).forEach(([key, value]) => {
591
+ if (value !== void 0) {
592
+ const field = this._schema.fields[key];
593
+ if (field) {
594
+ const { key: serializedKey } = Function2.pipe(getParamKeyAnnotation(field.ast), Option4.getOrElse(() => ({
595
+ key: decamelize(key)
596
+ })));
597
+ url.searchParams.set(serializedKey, String(value));
598
+ }
599
+ }
600
+ });
601
+ return url;
602
+ }
603
+ };
570
604
  export {
571
605
  JsonPath,
572
606
  JsonProp,
573
607
  ParamKeyAnnotation,
574
- SimpleType,
575
- TestHelpers,
576
608
  UrlParser,
577
609
  VisitResult,
578
- accuireReleaseResource,
610
+ acquireReleaseResource,
611
+ causeToError,
579
612
  contextFromScope,
580
613
  createJsonPath,
581
614
  findAnnotation,
582
615
  findNode,
583
616
  findProperty,
584
617
  fromEffectValidationPath,
585
- getAnnotation,
618
+ getAnnotation2 as getAnnotation,
619
+ getArrayElementType,
620
+ getBaseType,
586
621
  getDiscriminatedType,
587
622
  getDiscriminatingProps,
588
623
  getField,
589
624
  getParamKeyAnnotation,
590
- getSimpleType,
625
+ getProperties,
626
+ getValue,
591
627
  isArrayType,
592
628
  isDiscriminatedUnion,
593
629
  isJsonPath,
594
630
  isLiteralUnion,
631
+ isNestedType,
595
632
  isOption,
596
- isSimpleType,
633
+ isTupleType2 as isTupleType,
597
634
  mapAst,
635
+ promiseWithCauseCapture,
598
636
  runAndForwardErrors,
637
+ setValue,
599
638
  splitJsonPath,
639
+ throwCause,
640
+ unwrapExit,
600
641
  visit
601
642
  };
602
643
  //# sourceMappingURL=index.mjs.map