@distilled.cloud/core 1.0.0-rc.2 → 1.0.0-rc.20
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/LICENSE +201 -0
- package/lib/_bundle/aws.js +2 -0
- package/lib/_bundle/aws.js.map +1 -0
- package/lib/_bundle/base.js +5 -0
- package/lib/_bundle/base.js.map +1 -0
- package/lib/_bundle/graphql.js +3 -0
- package/lib/_bundle/graphql.js.map +1 -0
- package/lib/_bundle/grpc.js +2 -0
- package/lib/_bundle/grpc.js.map +1 -0
- package/lib/_bundle/rest.js +2 -0
- package/lib/_bundle/rest.js.map +1 -0
- package/lib/_bundle/xml.js +4 -0
- package/lib/_bundle/xml.js.map +1 -0
- package/lib/annotations.d.ts +17 -0
- package/lib/annotations.d.ts.map +1 -0
- package/lib/annotations.js +2 -0
- package/lib/annotations.js.map +1 -0
- package/lib/api.d.ts +174 -18
- package/lib/api.d.ts.map +1 -1
- package/lib/api.js +1 -178
- package/lib/category.d.ts +4 -4
- package/lib/category.d.ts.map +1 -1
- package/lib/category.js +1 -280
- package/lib/chunks.d.ts +3 -0
- package/lib/chunks.d.ts.map +1 -0
- package/lib/chunks.js +1 -0
- package/lib/config.d.ts +68 -0
- package/lib/config.d.ts.map +1 -0
- package/lib/config.js +1 -0
- package/lib/crypto.d.ts +76 -0
- package/lib/crypto.d.ts.map +1 -0
- package/lib/crypto.js +1 -0
- package/lib/error-category.d.ts +1 -1
- package/lib/error-category.d.ts.map +1 -1
- package/lib/error-category.js +1 -46
- package/lib/errors.d.ts +64 -72
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +1 -186
- package/lib/graphql.d.ts +403 -0
- package/lib/graphql.d.ts.map +1 -0
- package/lib/graphql.js +1 -0
- package/lib/graphql.types.d.ts +12 -0
- package/lib/graphql.types.d.ts.map +1 -0
- package/lib/graphql.types.js +1 -0
- package/lib/grpc-transport-cloudflare-http2.d.ts +7 -0
- package/lib/grpc-transport-cloudflare-http2.d.ts.map +1 -0
- package/lib/grpc-transport-cloudflare-http2.js +6 -0
- package/lib/grpc-transport-cloudflare-http2.js.map +1 -0
- package/lib/grpc-transport-cloudflare-sockets.d.ts +7 -0
- package/lib/grpc-transport-cloudflare-sockets.d.ts.map +1 -0
- package/lib/grpc-transport-cloudflare-sockets.js +5 -0
- package/lib/grpc-transport-cloudflare-sockets.js.map +1 -0
- package/lib/grpc-transport-node-http2.d.ts +19 -0
- package/lib/grpc-transport-node-http2.d.ts.map +1 -0
- package/lib/grpc-transport-node-http2.js +2 -0
- package/lib/grpc-transport-node-http2.js.map +1 -0
- package/lib/grpc-transport.d.ts +76 -0
- package/lib/grpc-transport.d.ts.map +1 -0
- package/lib/grpc-transport.js +1 -0
- package/lib/hpack.d.ts +34 -0
- package/lib/hpack.d.ts.map +1 -0
- package/lib/hpack.js +2 -0
- package/lib/hpack.js.map +1 -0
- package/lib/index.d.ts +32 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +17 -0
- package/lib/pagination.d.ts +101 -24
- package/lib/pagination.d.ts.map +1 -1
- package/lib/pagination.js +1 -182
- package/lib/plain-values.d.ts +10 -0
- package/lib/plain-values.d.ts.map +1 -0
- package/lib/plain-values.js +2 -0
- package/lib/plain-values.js.map +1 -0
- package/lib/protobuf.d.ts +92 -0
- package/lib/protobuf.d.ts.map +1 -0
- package/lib/protobuf.js +1 -0
- package/lib/protocol-grpc.d.ts +86 -0
- package/lib/protocol-grpc.d.ts.map +1 -0
- package/lib/protocol-grpc.js +1 -0
- package/lib/protocol-http.d.ts +102 -23
- package/lib/protocol-http.d.ts.map +1 -1
- package/lib/protocol-http.js +1 -554
- package/lib/protocol-rest.d.ts +120 -50
- package/lib/protocol-rest.d.ts.map +1 -1
- package/lib/protocol-rest.js +1 -242
- package/lib/query.d.ts +29 -0
- package/lib/query.d.ts.map +1 -0
- package/lib/query.js +1 -0
- package/lib/response-validation-strict.d.ts +48 -0
- package/lib/response-validation-strict.d.ts.map +1 -0
- package/lib/response-validation-strict.js +1 -0
- package/lib/response-validation.d.ts +86 -0
- package/lib/response-validation.d.ts.map +1 -0
- package/lib/response-validation.js +1 -0
- package/lib/retry-after.d.ts +3 -3
- package/lib/retry-after.d.ts.map +1 -1
- package/lib/retry-after.js +1 -112
- package/lib/retry.d.ts +48 -29
- package/lib/retry.d.ts.map +1 -1
- package/lib/retry.js +1 -210
- package/lib/schema-lite-impl.d.ts +158 -0
- package/lib/schema-lite-impl.d.ts.map +1 -0
- package/lib/schema-lite-impl.js +1 -0
- package/lib/schema-lite-typed.d.ts +41 -0
- package/lib/schema-lite-typed.d.ts.map +1 -0
- package/lib/schema-lite-typed.js +1 -0
- package/lib/schema-lite.d.ts +40 -0
- package/lib/schema-lite.d.ts.map +1 -0
- package/lib/schema-lite.js +1 -0
- package/lib/schema.d.ts +21 -7
- package/lib/schema.d.ts.map +1 -1
- package/lib/schema.js +1 -48
- package/lib/sdk-error.d.ts +27 -0
- package/lib/sdk-error.d.ts.map +1 -0
- package/lib/sdk-error.js +1 -0
- package/lib/sensitive.d.ts +28 -0
- package/lib/sensitive.d.ts.map +1 -0
- package/lib/sensitive.js +2 -0
- package/lib/sensitive.js.map +1 -0
- package/lib/telemetry-debug.d.ts +6 -0
- package/lib/telemetry-debug.d.ts.map +1 -0
- package/lib/telemetry-debug.js +2 -0
- package/lib/telemetry-debug.js.map +1 -0
- package/lib/telemetry-hooks.d.ts +32 -0
- package/lib/telemetry-hooks.d.ts.map +1 -0
- package/lib/telemetry-hooks.js +2 -0
- package/lib/telemetry-hooks.js.map +1 -0
- package/lib/telemetry-host-rules.d.ts +23 -0
- package/lib/telemetry-host-rules.d.ts.map +1 -0
- package/lib/telemetry-host-rules.js +2 -0
- package/lib/telemetry-host-rules.js.map +1 -0
- package/lib/telemetry-lambda-context.d.ts +17 -0
- package/lib/telemetry-lambda-context.d.ts.map +1 -0
- package/lib/telemetry-lambda-context.js +2 -0
- package/lib/telemetry-lambda-context.js.map +1 -0
- package/lib/telemetry-message-context.d.ts +52 -0
- package/lib/telemetry-message-context.d.ts.map +1 -0
- package/lib/telemetry-message-context.js +2 -0
- package/lib/telemetry-message-context.js.map +1 -0
- package/lib/telemetry-metrics.d.ts +6 -0
- package/lib/telemetry-metrics.d.ts.map +1 -0
- package/lib/telemetry-metrics.js +2 -0
- package/lib/telemetry-metrics.js.map +1 -0
- package/lib/telemetry-name-template.d.ts +13 -0
- package/lib/telemetry-name-template.d.ts.map +1 -0
- package/lib/telemetry-name-template.js +2 -0
- package/lib/telemetry-name-template.js.map +1 -0
- package/lib/telemetry-stream.d.ts +11 -0
- package/lib/telemetry-stream.d.ts.map +1 -0
- package/lib/telemetry-stream.js +2 -0
- package/lib/telemetry-stream.js.map +1 -0
- package/lib/telemetry-xray.d.ts +28 -0
- package/lib/telemetry-xray.d.ts.map +1 -0
- package/lib/telemetry-xray.js +2 -0
- package/lib/telemetry-xray.js.map +1 -0
- package/lib/telemetry.d.ts +288 -0
- package/lib/telemetry.d.ts.map +1 -0
- package/lib/telemetry.js +3 -0
- package/lib/telemetry.js.map +1 -0
- package/lib/trait.d.ts +71 -20
- package/lib/trait.d.ts.map +1 -1
- package/lib/trait.js +1 -107
- package/lib/unknown-error.d.ts +18 -0
- package/lib/unknown-error.d.ts.map +1 -0
- package/lib/unknown-error.js +1 -0
- package/lib/xml.d.ts +44 -0
- package/lib/xml.d.ts.map +1 -0
- package/lib/xml.js +1 -0
- package/package.json +13 -13
- package/src/annotations.ts +53 -0
- package/src/api.ts +1370 -200
- package/src/category.ts +76 -106
- package/src/chunks.ts +30 -0
- package/src/config.ts +245 -0
- package/src/crypto.ts +202 -0
- package/src/error-category.ts +16 -29
- package/src/errors.ts +75 -75
- package/src/graphql.ts +1778 -0
- package/src/graphql.types.ts +105 -0
- package/src/grpc-transport-cloudflare-http2.ts +494 -0
- package/src/grpc-transport-cloudflare-sockets.ts +299 -0
- package/src/grpc-transport-node-http2.ts +234 -0
- package/src/grpc-transport.ts +118 -0
- package/src/hpack.ts +331 -0
- package/src/index.ts +31 -0
- package/src/pagination.ts +368 -94
- package/src/plain-values.ts +41 -0
- package/src/protobuf.ts +1332 -0
- package/src/protocol-grpc.ts +641 -0
- package/src/protocol-http.ts +622 -322
- package/src/protocol-rest.ts +440 -227
- package/src/query.ts +39 -0
- package/src/response-validation-strict.ts +327 -0
- package/src/response-validation.ts +161 -0
- package/src/retry-after.ts +7 -13
- package/src/retry.ts +96 -110
- package/src/schema-lite-impl.ts +550 -0
- package/src/schema-lite-typed.ts +44 -0
- package/src/schema-lite.ts +44 -0
- package/src/schema.ts +259 -15
- package/src/sdk-error.ts +33 -0
- package/src/sensitive.ts +154 -0
- package/src/telemetry-debug.ts +179 -0
- package/src/telemetry-hooks.ts +33 -0
- package/src/telemetry-host-rules.ts +39 -0
- package/src/telemetry-lambda-context.ts +46 -0
- package/src/telemetry-message-context.ts +170 -0
- package/src/telemetry-metrics.ts +69 -0
- package/src/telemetry-name-template.ts +26 -0
- package/src/telemetry-stream.ts +100 -0
- package/src/telemetry-xray.ts +66 -0
- package/src/telemetry.ts +832 -0
- package/src/trait.ts +106 -67
- package/src/unknown-error.ts +38 -0
- package/src/xml.ts +346 -0
- package/lib/api.js.map +0 -1
- package/lib/category.js.map +0 -1
- package/lib/codegen/cli.d.ts +0 -29
- package/lib/codegen/cli.d.ts.map +0 -1
- package/lib/codegen/cli.js +0 -165
- package/lib/codegen/cli.js.map +0 -1
- package/lib/codegen/emit.d.ts +0 -129
- package/lib/codegen/emit.d.ts.map +0 -1
- package/lib/codegen/emit.js +0 -105
- package/lib/codegen/emit.js.map +0 -1
- package/lib/codegen/format.d.ts +0 -23
- package/lib/codegen/format.d.ts.map +0 -1
- package/lib/codegen/format.js +0 -28
- package/lib/codegen/format.js.map +0 -1
- package/lib/codegen/generator.d.ts +0 -334
- package/lib/codegen/generator.d.ts.map +0 -1
- package/lib/codegen/generator.js +0 -691
- package/lib/codegen/generator.js.map +0 -1
- package/lib/codegen/graph.d.ts +0 -36
- package/lib/codegen/graph.d.ts.map +0 -1
- package/lib/codegen/graph.js +0 -136
- package/lib/codegen/graph.js.map +0 -1
- package/lib/codegen/members.d.ts +0 -25
- package/lib/codegen/members.d.ts.map +0 -1
- package/lib/codegen/members.js +0 -55
- package/lib/codegen/members.js.map +0 -1
- package/lib/codegen/naming.d.ts +0 -29
- package/lib/codegen/naming.d.ts.map +0 -1
- package/lib/codegen/naming.js +0 -74
- package/lib/codegen/naming.js.map +0 -1
- package/lib/codegen/openapi-cli.d.ts +0 -38
- package/lib/codegen/openapi-cli.d.ts.map +0 -1
- package/lib/codegen/openapi-cli.js +0 -107
- package/lib/codegen/openapi-cli.js.map +0 -1
- package/lib/codegen/openapi.d.ts +0 -115
- package/lib/codegen/openapi.d.ts.map +0 -1
- package/lib/codegen/openapi.js +0 -1220
- package/lib/codegen/openapi.js.map +0 -1
- package/lib/codegen/operations.d.ts +0 -24
- package/lib/codegen/operations.d.ts.map +0 -1
- package/lib/codegen/operations.js +0 -56
- package/lib/codegen/operations.js.map +0 -1
- package/lib/codegen/pagination.d.ts +0 -39
- package/lib/codegen/pagination.d.ts.map +0 -1
- package/lib/codegen/pagination.js +0 -33
- package/lib/codegen/pagination.js.map +0 -1
- package/lib/codegen/prelude.d.ts +0 -15
- package/lib/codegen/prelude.d.ts.map +0 -1
- package/lib/codegen/prelude.js +0 -60
- package/lib/codegen/prelude.js.map +0 -1
- package/lib/error-category.js.map +0 -1
- package/lib/errors.js.map +0 -1
- package/lib/json-patch.d.ts +0 -44
- package/lib/json-patch.d.ts.map +0 -1
- package/lib/json-patch.js +0 -174
- package/lib/json-patch.js.map +0 -1
- package/lib/pagination.js.map +0 -1
- package/lib/protocol-http.js.map +0 -1
- package/lib/protocol-rest.js.map +0 -1
- package/lib/retry-after.js.map +0 -1
- package/lib/retry.js.map +0 -1
- package/lib/schema.js.map +0 -1
- package/lib/trait.js.map +0 -1
- package/src/codegen/cli.ts +0 -268
- package/src/codegen/emit.ts +0 -207
- package/src/codegen/format.ts +0 -47
- package/src/codegen/generator.ts +0 -1153
- package/src/codegen/graph.ts +0 -151
- package/src/codegen/members.ts +0 -71
- package/src/codegen/naming.ts +0 -86
- package/src/codegen/openapi-cli.ts +0 -166
- package/src/codegen/openapi.ts +0 -1450
- package/src/codegen/operations.ts +0 -76
- package/src/codegen/pagination.ts +0 -71
- package/src/codegen/prelude.ts +0 -70
- package/src/json-patch.ts +0 -222
package/src/schema.ts
CHANGED
|
@@ -5,32 +5,39 @@
|
|
|
5
5
|
* provider package via `@distilled.cloud/core/schema`. Do not copy it into
|
|
6
6
|
* individual packages.
|
|
7
7
|
*
|
|
8
|
-
* Every real TYPE (`Schema`, `Codec`, `Top`,
|
|
8
|
+
* Every real TYPE (`Schema`, `Codec`, `Top`, ...) is re-exported untouched, so
|
|
9
9
|
* the `Schema.Schema<Foo>` annotations on generated consts (and the public
|
|
10
10
|
* `.d.ts`) stay precise. The schema *construction surface* (`Struct`,
|
|
11
|
-
* `optional`, `suspend`,
|
|
11
|
+
* `optional`, `suspend`, ...) and the leaf scalar schemas are retyped to `any`,
|
|
12
12
|
* so the compiler instantiates none of the heavy Schema generics while
|
|
13
|
-
* building a service file
|
|
13
|
+
* building a service file -- the explicit annotations carry the real types.
|
|
14
14
|
*
|
|
15
15
|
* Overriding a *value* export never affects the same-named *type* export (they
|
|
16
16
|
* live in separate namespaces and continue to flow through `export *`), so
|
|
17
17
|
* `Schema.Literal<"x">`-style type usages remain precise.
|
|
18
18
|
*
|
|
19
|
-
* Runtime behaviour is identical
|
|
20
|
-
* runtime value from `effect/Schema
|
|
19
|
+
* Runtime behaviour is identical. Every override aliases the exact same
|
|
20
|
+
* runtime value from `effect/Schema`, except `suspend`, `Array` and
|
|
21
|
+
* `Record`, which build the same schema lazily (see "Lazy schemas").
|
|
21
22
|
*/
|
|
22
|
-
export
|
|
23
|
+
// Types only: a value `export *` costs one binding per Effect Schema export
|
|
24
|
+
// (345 of them, ~70 KB) in every process that imports an SDK. The values
|
|
25
|
+
// generated and hand-written code use are re-exported explicitly below.
|
|
26
|
+
export type * from "effect/Schema";
|
|
27
|
+
export { declare, Duration, instanceOf, Redacted } from "effect/Schema";
|
|
23
28
|
|
|
29
|
+
import { pipeArguments } from "effect/Pipeable";
|
|
24
30
|
import * as S from "effect/Schema";
|
|
25
31
|
|
|
26
|
-
type AnyFn = (...args: any[]) => any;
|
|
32
|
+
export type AnyFn = (...args: any[]) => any;
|
|
27
33
|
|
|
28
|
-
// Construction surface
|
|
34
|
+
// Construction surface -- collapse to `any` so generics are never instantiated.
|
|
29
35
|
export const optional: AnyFn = S.optional as AnyFn;
|
|
30
36
|
export const Struct: AnyFn = S.Struct as AnyFn;
|
|
31
|
-
export const
|
|
32
|
-
export const
|
|
33
|
-
export const
|
|
37
|
+
export const StructWithRest: AnyFn = S.StructWithRest as AnyFn;
|
|
38
|
+
export const suspend: AnyFn = (f: () => S.Top) => lazySuspend(f);
|
|
39
|
+
export const Array: AnyFn = (item: S.Top) => lazyArray(item);
|
|
40
|
+
export const Record: AnyFn = (key: S.Top, value: S.Top) => lazyRecord(key, value);
|
|
34
41
|
export const Union: AnyFn = S.Union as AnyFn;
|
|
35
42
|
export const Literal: AnyFn = S.Literal as AnyFn;
|
|
36
43
|
export const Literals: AnyFn = S.Literals as AnyFn;
|
|
@@ -38,7 +45,7 @@ export const NullOr: AnyFn = S.NullOr as AnyFn;
|
|
|
38
45
|
export const Tuple: AnyFn = S.Tuple as AnyFn;
|
|
39
46
|
export const encodeKeys: AnyFn = S.encodeKeys as AnyFn;
|
|
40
47
|
|
|
41
|
-
// Leaf schemas
|
|
48
|
+
// Leaf schemas -- collapse to `any` so `.pipe(...)` on them is free.
|
|
42
49
|
export const String: any = S.String;
|
|
43
50
|
export const Number: any = S.Number;
|
|
44
51
|
export const Boolean: any = S.Boolean;
|
|
@@ -48,6 +55,243 @@ export const Null: any = S.Null;
|
|
|
48
55
|
export const Void: any = S.Void;
|
|
49
56
|
export const Any: any = S.Any;
|
|
50
57
|
|
|
51
|
-
// NOTE: `Schema` / `Codec` (types)
|
|
52
|
-
//
|
|
53
|
-
//
|
|
58
|
+
// NOTE: `Schema` / `Codec` (types) are intentionally NOT overridden -- they
|
|
59
|
+
// flow through `export *` with real types so cast targets stay precise.
|
|
60
|
+
// `TaggedError` is overridden with the same type (see "Lazy error classes").
|
|
61
|
+
|
|
62
|
+
//#region Lazy schemas
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Schemas whose real Effect schema is built on first use instead of at
|
|
66
|
+
* import.
|
|
67
|
+
*
|
|
68
|
+
* A service module declares every schema as a top-level const, and
|
|
69
|
+
* importing it constructs all of them: EC2 has 2,575 `S.suspend(...)` and
|
|
70
|
+
* 784 `S.Array(...)` consts, Elasticsearch 3,272 and 2,265. An Effect schema
|
|
71
|
+
* is a function object plus an AST node (a `suspend` also a wrapper
|
|
72
|
+
* closure), and `.annotate` copies both: about 550 bytes per suspend, more
|
|
73
|
+
* for an array whose item it forces. A lazy schema is one small object
|
|
74
|
+
* holding how to build the real one and any annotations added before then:
|
|
75
|
+
* about 180 bytes.
|
|
76
|
+
*
|
|
77
|
+
* It inherits Effect's own schema prototype, so `pipe`, `annotate`, `make`,
|
|
78
|
+
* `isSchema` and every combinator see a schema. On first use of `.ast` (or
|
|
79
|
+
* of an own member such as an array's `value`) it builds the real schema --
|
|
80
|
+
* `build().annotate(annotations)`, exactly what eager code would have made --
|
|
81
|
+
* and copies the real schema's own properties onto itself, so from then on
|
|
82
|
+
* it IS that schema: same AST (memoized, stable identity), same rebuild
|
|
83
|
+
* options for later `.annotate` / `.check` calls.
|
|
84
|
+
*/
|
|
85
|
+
const SchemaProto: object = /*@__PURE__*/ Object.getPrototypeOf(S.String);
|
|
86
|
+
const buildKey = /*@__PURE__*/ Symbol.for("@distilled.cloud/core/schema/build");
|
|
87
|
+
const annotationsKey = /*@__PURE__*/ Symbol.for("@distilled.cloud/core/schema/annotations");
|
|
88
|
+
|
|
89
|
+
type Annotations = Record<string, unknown>;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* How a lazy schema builds itself: a closure, or a shared builder plus data
|
|
93
|
+
* stored on the schema object (no closure per schema; see packed.ts).
|
|
94
|
+
*/
|
|
95
|
+
export interface LazyBuilder {
|
|
96
|
+
readonly build: (data: unknown) => S.Top;
|
|
97
|
+
}
|
|
98
|
+
const dataKey = /*@__PURE__*/ Symbol.for("@distilled.cloud/core/schema/data");
|
|
99
|
+
|
|
100
|
+
interface LazySchema {
|
|
101
|
+
[buildKey]?: (() => S.Top) | LazyBuilder;
|
|
102
|
+
[dataKey]?: unknown;
|
|
103
|
+
[annotationsKey]?: Annotations;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Build the real schema and become it. */
|
|
107
|
+
const force = (self: LazySchema): void => {
|
|
108
|
+
const build = self[buildKey];
|
|
109
|
+
if (!build) return;
|
|
110
|
+
const annotations = self[annotationsKey];
|
|
111
|
+
const built = typeof build === "function" ? build() : build.build(self[dataKey]);
|
|
112
|
+
const real = annotations ? built.annotate(annotations) : built;
|
|
113
|
+
delete self[buildKey];
|
|
114
|
+
delete self[dataKey];
|
|
115
|
+
delete self[annotationsKey];
|
|
116
|
+
Object.defineProperties(self, Object.getOwnPropertyDescriptors(real));
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
/** Members a built schema carries as own properties (array item, record key/value). */
|
|
120
|
+
const forwarded = (name: string): PropertyDescriptor => ({
|
|
121
|
+
configurable: true,
|
|
122
|
+
get(this: LazySchema & Record<string, unknown>) {
|
|
123
|
+
if (!this[buildKey]) return undefined;
|
|
124
|
+
force(this);
|
|
125
|
+
return this[name];
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
const LazyProto: object = /*@__PURE__*/ Object.create(SchemaProto, {
|
|
130
|
+
ast: /*@__PURE__*/ forwarded("ast"),
|
|
131
|
+
value: /*@__PURE__*/ forwarded("value"),
|
|
132
|
+
key: /*@__PURE__*/ forwarded("key"),
|
|
133
|
+
annotate: {
|
|
134
|
+
configurable: true,
|
|
135
|
+
writable: true,
|
|
136
|
+
value(this: LazySchema, annotations: Annotations) {
|
|
137
|
+
const build = this[buildKey];
|
|
138
|
+
// Once built, annotate like any schema (a rebuilt copy).
|
|
139
|
+
if (!build) return (SchemaProto as { annotate: Function }).annotate.call(this, annotations);
|
|
140
|
+
const previous = this[annotationsKey];
|
|
141
|
+
return lazy(build, previous ? { ...previous, ...annotations } : annotations, this[dataKey]);
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
// Effect detects schemas by this key on the prototype; if a future Effect
|
|
147
|
+
// moves it, build eagerly.
|
|
148
|
+
const supported = (SchemaProto as Record<string, unknown>)["~effect/Schema/Schema"] !== undefined;
|
|
149
|
+
|
|
150
|
+
const lazy = (
|
|
151
|
+
build: (() => S.Top) | LazyBuilder,
|
|
152
|
+
annotations?: Annotations,
|
|
153
|
+
data?: unknown,
|
|
154
|
+
): S.Top => {
|
|
155
|
+
if (!supported) {
|
|
156
|
+
const built = typeof build === "function" ? build() : build.build(data);
|
|
157
|
+
return annotations ? built.annotate(annotations) : built;
|
|
158
|
+
}
|
|
159
|
+
const self = Object.create(LazyProto);
|
|
160
|
+
self[buildKey] = build;
|
|
161
|
+
if (data !== undefined) self[dataKey] = data;
|
|
162
|
+
if (annotations) self[annotationsKey] = annotations;
|
|
163
|
+
return self;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
/** A schema built on first use by `builder.build(data)`. */
|
|
167
|
+
export const lazyFrom = (builder: LazyBuilder, data: unknown): S.Top =>
|
|
168
|
+
lazy(builder, undefined, data);
|
|
169
|
+
|
|
170
|
+
const lazySuspend = (f: () => S.Top): S.Top => lazy(() => S.suspend(f));
|
|
171
|
+
const lazyArray = (item: S.Top): S.Top => lazy(() => S.Array(item));
|
|
172
|
+
const lazyRecord = (key: S.Top, value: S.Top): S.Top => lazy(() => S.Record(key as any, value));
|
|
173
|
+
|
|
174
|
+
//#endregion
|
|
175
|
+
|
|
176
|
+
//#region Lazy error classes
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* `S.TaggedError` whose Effect class is built on first use instead of at
|
|
180
|
+
* import.
|
|
181
|
+
*
|
|
182
|
+
* Every error class Effect builds carries its own copies of about fifteen
|
|
183
|
+
* static methods plus a tagged struct schema: about 4.6 KB per class, and
|
|
184
|
+
* AWS EC2 alone declares 248 of them. This returns a small constructor that
|
|
185
|
+
* stands in for the class until something needs it -- constructing an
|
|
186
|
+
* instance, or a schema static such as `ast` / `fields` / `make`. Then it
|
|
187
|
+
* builds the real class with `S.TaggedError` and puts it underneath: the
|
|
188
|
+
* lazy class's prototype starts inheriting from the real prototype, and
|
|
189
|
+
* statics forward to the real class with `this` bound to the lazy class, so
|
|
190
|
+
* decoding produces instances of the lazy class (and of its subclasses).
|
|
191
|
+
*
|
|
192
|
+
* `pipe` (used to attach error categories), `identifier` and the schema
|
|
193
|
+
* marker answer without building. Instances behave as before: same
|
|
194
|
+
* `_tag`, fields, validation, `instanceof`, stack and `Effect.catchTag`.
|
|
195
|
+
*/
|
|
196
|
+
const lazyErrorKey = /*@__PURE__*/ Symbol.for("@distilled.cloud/core/schema/lazyError");
|
|
197
|
+
|
|
198
|
+
interface LazyErrorState {
|
|
199
|
+
readonly identifier: string | undefined;
|
|
200
|
+
readonly args: ReadonlyArray<unknown>;
|
|
201
|
+
readonly lazy: Function;
|
|
202
|
+
real?: Function;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const SchemaTypeId = "~effect/Schema/Schema";
|
|
206
|
+
|
|
207
|
+
// The base every Effect error class extends (`core.Error`, a yieldable
|
|
208
|
+
// Error). Found on first use, so bundles without error classes do not keep
|
|
209
|
+
// Effect's class machinery.
|
|
210
|
+
let errorBase: Function | undefined;
|
|
211
|
+
const ErrorBase = (): Function =>
|
|
212
|
+
(errorBase ??= Object.getPrototypeOf(S.TaggedError()("Probe", {})) as Function);
|
|
213
|
+
|
|
214
|
+
const realErrorClass = (cls: Function): Function => {
|
|
215
|
+
const state = (cls as unknown as Record<symbol, LazyErrorState>)[lazyErrorKey];
|
|
216
|
+
if (!state.real) {
|
|
217
|
+
const real = (S.TaggedError as Function)(state.identifier)(...state.args) as Function;
|
|
218
|
+
state.real = real;
|
|
219
|
+
Object.setPrototypeOf(state.lazy.prototype, real.prototype);
|
|
220
|
+
}
|
|
221
|
+
return state.real;
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
/** Effect's class statics that need the real class. */
|
|
225
|
+
const FORWARDED_STATICS = [
|
|
226
|
+
"ast",
|
|
227
|
+
"fields",
|
|
228
|
+
"make",
|
|
229
|
+
"makeOption",
|
|
230
|
+
"makeEffect",
|
|
231
|
+
"annotate",
|
|
232
|
+
"annotateKey",
|
|
233
|
+
"check",
|
|
234
|
+
"rebuild",
|
|
235
|
+
"extend",
|
|
236
|
+
"mapFields",
|
|
237
|
+
];
|
|
238
|
+
|
|
239
|
+
let lazyErrorStatics: object | undefined;
|
|
240
|
+
const LazyErrorStatics = (): object =>
|
|
241
|
+
(lazyErrorStatics ??= Object.create(ErrorBase(), {
|
|
242
|
+
[SchemaTypeId]: { value: SchemaTypeId, configurable: true },
|
|
243
|
+
identifier: {
|
|
244
|
+
configurable: true,
|
|
245
|
+
get(this: Record<symbol, LazyErrorState>) {
|
|
246
|
+
const state = this[lazyErrorKey];
|
|
247
|
+
return state.identifier ?? state.args[0];
|
|
248
|
+
},
|
|
249
|
+
},
|
|
250
|
+
pipe: {
|
|
251
|
+
configurable: true,
|
|
252
|
+
writable: true,
|
|
253
|
+
value(this: unknown) {
|
|
254
|
+
return pipeArguments(this, arguments);
|
|
255
|
+
},
|
|
256
|
+
},
|
|
257
|
+
...Object.fromEntries(
|
|
258
|
+
FORWARDED_STATICS.map((name) => [
|
|
259
|
+
name,
|
|
260
|
+
{
|
|
261
|
+
configurable: true,
|
|
262
|
+
get(this: Function) {
|
|
263
|
+
let owner: object | null = realErrorClass(this);
|
|
264
|
+
let descriptor: PropertyDescriptor | undefined;
|
|
265
|
+
while (owner && !(descriptor = Object.getOwnPropertyDescriptor(owner, name))) {
|
|
266
|
+
owner = Object.getPrototypeOf(owner);
|
|
267
|
+
}
|
|
268
|
+
if (!descriptor) return undefined;
|
|
269
|
+
if (descriptor.get) return descriptor.get.call(this);
|
|
270
|
+
return typeof descriptor.value === "function"
|
|
271
|
+
? descriptor.value.bind(this)
|
|
272
|
+
: descriptor.value;
|
|
273
|
+
},
|
|
274
|
+
},
|
|
275
|
+
]),
|
|
276
|
+
),
|
|
277
|
+
}));
|
|
278
|
+
|
|
279
|
+
const lazyTaggedError =
|
|
280
|
+
(identifier?: string) =>
|
|
281
|
+
(...args: ReadonlyArray<unknown>): Function => {
|
|
282
|
+
function LazyTaggedError(this: unknown, input?: unknown, options?: unknown) {
|
|
283
|
+
if (!new.target) throw new TypeError("Class constructor cannot be invoked without 'new'");
|
|
284
|
+
return Reflect.construct(realErrorClass(new.target), [input, options], new.target);
|
|
285
|
+
}
|
|
286
|
+
Object.setPrototypeOf(LazyTaggedError, LazyErrorStatics());
|
|
287
|
+
LazyTaggedError.prototype = Object.create(ErrorBase().prototype, {
|
|
288
|
+
constructor: { value: LazyTaggedError, writable: true, configurable: true },
|
|
289
|
+
});
|
|
290
|
+
const state: LazyErrorState = { identifier, args, lazy: LazyTaggedError };
|
|
291
|
+
Object.defineProperty(LazyTaggedError, lazyErrorKey, { value: state });
|
|
292
|
+
return LazyTaggedError;
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
export const TaggedError: typeof S.TaggedError = lazyTaggedError as typeof S.TaggedError;
|
|
296
|
+
|
|
297
|
+
//#endregion
|
package/src/sdk-error.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The error an operation fails with when the SDK itself cannot make the
|
|
3
|
+
* call: its generated config or a core module is broken (a paginated
|
|
4
|
+
* operation without the fields its strategy reads, a gRPC operation
|
|
5
|
+
* without a descriptor, ...), or one of its own modules fails to load. No
|
|
6
|
+
* input or response causes it; the generator refuses the configs that
|
|
7
|
+
* would. It is typed, so it fails the call instead of crashing the fiber,
|
|
8
|
+
* and it is never retried.
|
|
9
|
+
*
|
|
10
|
+
* Defined on its own, in core's `base` bundle group, so pagination and
|
|
11
|
+
* the protocols fail with it without importing the REST modules.
|
|
12
|
+
*/
|
|
13
|
+
import * as Effect from "effect/Effect";
|
|
14
|
+
import * as Schema from "./schema-lite-typed.ts";
|
|
15
|
+
|
|
16
|
+
export class SdkError
|
|
17
|
+
extends /*@__PURE__*/ Schema.TaggedError<SdkError>()("SdkError", {
|
|
18
|
+
message: Schema.String,
|
|
19
|
+
}) {}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Load one of the SDK's own modules on first use (`() => import("./x.ts")`).
|
|
23
|
+
* A rejected import -- a chunk missing from a bundle, a browser chunk that
|
|
24
|
+
* fails to download -- fails with {@link SdkError}.
|
|
25
|
+
*/
|
|
26
|
+
export const loadModule = <A>(load: () => PromiseLike<A>): Effect.Effect<A, SdkError> =>
|
|
27
|
+
Effect.tryPromise({
|
|
28
|
+
try: () => load(),
|
|
29
|
+
catch: (cause) =>
|
|
30
|
+
new SdkError({
|
|
31
|
+
message: `an SDK module failed to load: ${cause instanceof Error ? cause.message : String(cause)}`,
|
|
32
|
+
}),
|
|
33
|
+
});
|
package/src/sensitive.ts
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Secrets in values, logs and error messages: one rule for every SDK.
|
|
3
|
+
*
|
|
4
|
+
* A schema holds a secret when a value it reaches is marked sensitive
|
|
5
|
+
* ({@link sensitiveValueSymbol}: `T.SensitiveValue` in the REST SDKs, the
|
|
6
|
+
* `Sensitive*` schemas in AWS). An operation's input and its output are
|
|
7
|
+
* judged independently, each by its own schema:
|
|
8
|
+
*
|
|
9
|
+
* - its plain form (a serialized request, a response body, the value a
|
|
10
|
+
* parse failure quotes) is {@link REDACTED} whole: {@link redactPlain};
|
|
11
|
+
* - its parsed form shows only the sensitive values as `Redacted`:
|
|
12
|
+
* {@link redactParsed} for logs, {@link wrapSensitive} for decoded output.
|
|
13
|
+
*
|
|
14
|
+
* Imports only `effect/Redacted` and the AST helpers, so a protocol can use
|
|
15
|
+
* it without loading the REST protocol.
|
|
16
|
+
*/
|
|
17
|
+
import * as Redacted from "effect/Redacted";
|
|
18
|
+
import type * as AST from "effect/SchemaAST";
|
|
19
|
+
import { getAnn, getProps, resolveNode } from "./annotations.ts";
|
|
20
|
+
import { isOpaqueValue } from "./plain-values.ts";
|
|
21
|
+
|
|
22
|
+
export const sensitiveValueSymbol = /*@__PURE__*/ Symbol.for(
|
|
23
|
+
"@distilled.cloud/core/sensitive-value",
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
/** What the plain form of a value that can hold a secret is shown as. */
|
|
27
|
+
export const REDACTED = "[REDACTED]";
|
|
28
|
+
|
|
29
|
+
/** Whether `ast` is marked sensitive (through optional, Suspend and encoding wrappers). */
|
|
30
|
+
export const isSensitive = (ast: AST.AST): boolean =>
|
|
31
|
+
getAnn(ast, sensitiveValueSymbol) !== undefined;
|
|
32
|
+
|
|
33
|
+
const holds = /*@__PURE__*/ new WeakMap<AST.AST, boolean>();
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Whether a value of the type `ast` can hold a sensitive value, at any depth:
|
|
37
|
+
* the type itself, members, map values, list elements and union arms.
|
|
38
|
+
*/
|
|
39
|
+
export const holdsSensitive = (ast: AST.AST): boolean => {
|
|
40
|
+
const cached = holds.get(ast);
|
|
41
|
+
if (cached !== undefined) return cached;
|
|
42
|
+
const seen = new Set<AST.AST>();
|
|
43
|
+
const visit = (n: AST.AST): boolean => {
|
|
44
|
+
if (isSensitive(n)) return true;
|
|
45
|
+
const node = resolveNode(n);
|
|
46
|
+
if (seen.has(node)) return false;
|
|
47
|
+
seen.add(node);
|
|
48
|
+
switch (node._tag) {
|
|
49
|
+
case "Union":
|
|
50
|
+
return node.types.some(visit);
|
|
51
|
+
case "Arrays":
|
|
52
|
+
return [...(node.elements ?? []), ...(node.rest ?? [])].some(visit);
|
|
53
|
+
case "Objects":
|
|
54
|
+
return [...(node.propertySignatures ?? []), ...(node.indexSignatures ?? [])].some((m) =>
|
|
55
|
+
visit(m.type),
|
|
56
|
+
);
|
|
57
|
+
default:
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
const result = visit(ast);
|
|
62
|
+
holds.set(ast, result);
|
|
63
|
+
return result;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/** `plain`, the plain form of a value of the type `ast`: {@link REDACTED} when it can hold a secret. */
|
|
67
|
+
export const redactPlain = <A>(ast: AST.AST, plain: A): A | typeof REDACTED =>
|
|
68
|
+
holdsSensitive(ast) ? REDACTED : plain;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* `value`, a parsed value of the type `ast`, as a log shows it: every
|
|
72
|
+
* sensitive value `Redacted` whole, whatever its type, and the rest as is.
|
|
73
|
+
*/
|
|
74
|
+
export const redactParsed = (ast: AST.AST, value: unknown): unknown =>
|
|
75
|
+
holdsSensitive(ast) ? wrap(ast, value, true) : value;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Walk a decoded (TS-named) value alongside its schema AST, wrapping values
|
|
79
|
+
* marked sensitive (members, map values, list elements) in `Redacted`: a
|
|
80
|
+
* string, or any non-null value of an untyped (`S.Unknown`) schema. A typed
|
|
81
|
+
* struct is walked so its own sensitive members are wrapped and its type
|
|
82
|
+
* stays accurate. Keys the schema doesn't model pass through. A union
|
|
83
|
+
* applies every arm in turn; a map arm skips keys a sibling arm names.
|
|
84
|
+
*/
|
|
85
|
+
export const wrapSensitive = (
|
|
86
|
+
ast: AST.AST,
|
|
87
|
+
value: unknown,
|
|
88
|
+
siblingNames?: ReadonlySet<string>,
|
|
89
|
+
): unknown => wrap(ast, value, false, siblingNames);
|
|
90
|
+
|
|
91
|
+
/** Whether `wrap` wraps `v`, a value of the sensitive type `type`. */
|
|
92
|
+
const isSecretValue = (type: AST.AST, v: unknown, whole: boolean): boolean =>
|
|
93
|
+
whole
|
|
94
|
+
? v !== null && v !== undefined
|
|
95
|
+
: typeof v === "string" || (v !== null && resolveNode(type)._tag === "Unknown");
|
|
96
|
+
|
|
97
|
+
const wrap = (
|
|
98
|
+
ast: AST.AST,
|
|
99
|
+
value: unknown,
|
|
100
|
+
whole: boolean,
|
|
101
|
+
siblingNames?: ReadonlySet<string>,
|
|
102
|
+
): unknown => {
|
|
103
|
+
if (
|
|
104
|
+
value === null ||
|
|
105
|
+
typeof value !== "object" ||
|
|
106
|
+
isOpaqueValue(value) ||
|
|
107
|
+
Redacted.isRedacted(value)
|
|
108
|
+
) {
|
|
109
|
+
return value;
|
|
110
|
+
}
|
|
111
|
+
const node = resolveNode(ast);
|
|
112
|
+
if (node._tag === "Union") {
|
|
113
|
+
// Redact every possible sensitive member, including in partial responses.
|
|
114
|
+
const named = new Set(
|
|
115
|
+
node.types.flatMap((arm) => {
|
|
116
|
+
const armNode = resolveNode(arm);
|
|
117
|
+
return armNode._tag === "Objects" ? getProps(armNode).map((p) => String(p.name)) : [];
|
|
118
|
+
}),
|
|
119
|
+
);
|
|
120
|
+
return node.types.reduce<unknown>((redacted, arm) => wrap(arm, redacted, whole, named), value);
|
|
121
|
+
}
|
|
122
|
+
if (node._tag === "Arrays") {
|
|
123
|
+
if (!Array.isArray(value)) return value;
|
|
124
|
+
const elem = node.rest?.[0];
|
|
125
|
+
if (!elem) return value;
|
|
126
|
+
const sensitive = isSensitive(elem);
|
|
127
|
+
return value.map((v) =>
|
|
128
|
+
sensitive && isSecretValue(elem, v, whole) && !Redacted.isRedacted(v)
|
|
129
|
+
? Redacted.make(v)
|
|
130
|
+
: wrap(elem, v, whole),
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
if (node._tag === "Objects" && !Array.isArray(value)) {
|
|
134
|
+
const props = getProps(node);
|
|
135
|
+
const mapValue = node.indexSignatures?.[0]?.type;
|
|
136
|
+
if (props.length === 0 && mapValue === undefined) return value;
|
|
137
|
+
const byName = new Map(props.map((p) => [String(p.name), p]));
|
|
138
|
+
// fromEntries keeps a `__proto__` key an entry instead of the prototype.
|
|
139
|
+
return Object.fromEntries(
|
|
140
|
+
Object.entries(value as Record<string, unknown>).flatMap<[string, unknown]>(([k, v]) => {
|
|
141
|
+
if (v === undefined) return [];
|
|
142
|
+
const prop = byName.get(k);
|
|
143
|
+
const type = prop?.type ?? (siblingNames?.has(k) ? undefined : mapValue);
|
|
144
|
+
if (type === undefined) return [[k, v]];
|
|
145
|
+
// Union arms run over each other's output: keep a value one already wrapped.
|
|
146
|
+
if (isSensitive(type) && isSecretValue(type, v, whole)) {
|
|
147
|
+
return [[k, Redacted.isRedacted(v) ? v : Redacted.make(v)]];
|
|
148
|
+
}
|
|
149
|
+
return [[k, wrap(type, v, whole)]];
|
|
150
|
+
}),
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
return value;
|
|
154
|
+
};
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Debug telemetry: spans for distilled's internals, on top of the operation
|
|
3
|
+
* span every call records. Opt in by providing the layer; a program that
|
|
4
|
+
* never imports this module never bundles it.
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { TelemetryDebug } from "@distilled.cloud/core/telemetry-debug";
|
|
8
|
+
* program.pipe(Effect.provide(TelemetryDebug.layer));
|
|
9
|
+
* ```
|
|
10
|
+
*
|
|
11
|
+
* With it, the operation span is the parent of:
|
|
12
|
+
*
|
|
13
|
+
* - one `Attempt` span per try, with Effect's HTTP span under it;
|
|
14
|
+
* - INTERNAL spans at trace level `Debug` for first-call setup,
|
|
15
|
+
* serialize, sign, deserialize, backoff sleeps and the caller's read
|
|
16
|
+
* of a streamed output (`Tracer.MinimumTraceLevel` can drop them);
|
|
17
|
+
*
|
|
18
|
+
* and carries the caller's `code.*` attributes.
|
|
19
|
+
*
|
|
20
|
+
* Every SDK also writes the same `Effect.logDebug` lines for each call,
|
|
21
|
+
* with or without an exporter. They go to the program's logger, so they
|
|
22
|
+
* show when `References.MinimumLogLevel` admits `Debug`, and a logger that
|
|
23
|
+
* reads the current span (an OTel log exporter) links them to the trace:
|
|
24
|
+
*
|
|
25
|
+
* ```
|
|
26
|
+
* DEBUG distilled.operation=0ms: call S3.PutObject
|
|
27
|
+
* distilled.operation: S3.PutObject
|
|
28
|
+
* url.template: /{Bucket}/{Key+}
|
|
29
|
+
* DEBUG distilled.operation=23ms: -> PUT my-bucket.s3.us-east-1.amazonaws.com
|
|
30
|
+
* DEBUG distilled.operation=25ms: <- 200
|
|
31
|
+
* DEBUG distilled.operation=27ms: ok S3.PutObject
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* The `call` line carries the input and the `ok` line the output, each
|
|
35
|
+
* with only its sensitive values `Redacted` (`./sensitive.ts`; input and
|
|
36
|
+
* output judged independently). The lines hold no serialized body and no
|
|
37
|
+
* full URL (a URL can carry a presigned signature or a sensitive label).
|
|
38
|
+
*/
|
|
39
|
+
import * as Cause from "effect/Cause";
|
|
40
|
+
import * as Effect from "effect/Effect";
|
|
41
|
+
import * as Exit from "effect/Exit";
|
|
42
|
+
import * as Layer from "effect/Layer";
|
|
43
|
+
import * as Stream from "effect/Stream";
|
|
44
|
+
import * as Tracer from "effect/Tracer";
|
|
45
|
+
import { redactParsed } from "./sensitive.ts";
|
|
46
|
+
import { Debug, type DebugHooks, OperationSpan, tagOf } from "./telemetry.ts";
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The call's log lines. Every line inside (the HTTP ones included) carries
|
|
50
|
+
* the operation and its URL template, and the exit line its duration. The
|
|
51
|
+
* input and output show only their sensitive values `Redacted`; each is
|
|
52
|
+
* judged by its own schema.
|
|
53
|
+
*/
|
|
54
|
+
const call: DebugHooks["call"] = (info, input, schemas, effect) => {
|
|
55
|
+
const template = info.attributes["url.template"];
|
|
56
|
+
const start =
|
|
57
|
+
schemas === undefined
|
|
58
|
+
? Effect.logDebug(`call ${info.name}`)
|
|
59
|
+
: Effect.logDebug(`call ${info.name}`, redactParsed(schemas.input, input));
|
|
60
|
+
const logged = Effect.flatMap(start, () =>
|
|
61
|
+
Effect.onExit(effect, (exit) =>
|
|
62
|
+
Exit.isSuccess(exit)
|
|
63
|
+
? schemas === undefined
|
|
64
|
+
? Effect.logDebug(`ok ${info.name}`)
|
|
65
|
+
: Effect.logDebug(`ok ${info.name}`, redactParsed(schemas.output, exit.value))
|
|
66
|
+
: Cause.hasInterruptsOnly(exit.cause)
|
|
67
|
+
? Effect.logDebug(`interrupted ${info.name}`)
|
|
68
|
+
: Effect.logDebug(`failed ${info.name}: ${tagOf(Cause.squash(exit.cause))}`),
|
|
69
|
+
),
|
|
70
|
+
);
|
|
71
|
+
return Effect.annotateLogs(Effect.withLogSpan(logged, "distilled.operation"), {
|
|
72
|
+
"distilled.operation": info.name,
|
|
73
|
+
...(typeof template === "string" ? { "url.template": template } : {}),
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/** The host of `url`, without the path or query (which can carry secrets). */
|
|
78
|
+
const hostOf = (url: string): string => {
|
|
79
|
+
try {
|
|
80
|
+
return new URL(url).host;
|
|
81
|
+
} catch {
|
|
82
|
+
return "";
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/** One HTTP exchange: method and host out, status back. */
|
|
87
|
+
const http: DebugHooks["http"] = (request, sent) =>
|
|
88
|
+
Effect.flatMap(Effect.logDebug(`-> ${request.method} ${hostOf(request.url)}`), () =>
|
|
89
|
+
Effect.tap(sent, (response) => Effect.logDebug(`<- ${response.status}`)),
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
const phase: DebugHooks["phase"] = (name, effect, attributes) =>
|
|
93
|
+
Effect.withSpan(effect, name, { level: "Debug", attributes, captureStackTrace: false });
|
|
94
|
+
|
|
95
|
+
const attempt: DebugHooks["attempt"] = (base, n) =>
|
|
96
|
+
Effect.withFiber((fiber) => {
|
|
97
|
+
const call = fiber.getRef(OperationSpan);
|
|
98
|
+
if (call === undefined) return base;
|
|
99
|
+
return Effect.useSpan(
|
|
100
|
+
"Attempt",
|
|
101
|
+
{
|
|
102
|
+
parent: call.span,
|
|
103
|
+
attributes: { "distilled.attempt": n, "http.request.resend_count": n - 1 },
|
|
104
|
+
},
|
|
105
|
+
(span) => {
|
|
106
|
+
call.attempt = span;
|
|
107
|
+
const run = Effect.tapError(base, (error) =>
|
|
108
|
+
Effect.sync(() => span.attribute("error.type", tagOf(error))),
|
|
109
|
+
);
|
|
110
|
+
return Effect.provideService(run, Tracer.ParentSpan, span);
|
|
111
|
+
},
|
|
112
|
+
);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* `distilled.read_stream`: from the caller's first pull on a streamed
|
|
117
|
+
* output member (an S3 object body, a Bedrock event stream) to its end,
|
|
118
|
+
* with the member's name and how many items (and bytes) it carried. It
|
|
119
|
+
* fills the operation span's time after the response headers arrive.
|
|
120
|
+
*/
|
|
121
|
+
const readStream: DebugHooks["readStream"] = (stream, parent, member) =>
|
|
122
|
+
Stream.unwrap(
|
|
123
|
+
Effect.map(
|
|
124
|
+
Effect.makeSpan("distilled.read_stream", {
|
|
125
|
+
parent,
|
|
126
|
+
level: "Debug",
|
|
127
|
+
attributes: { "distilled.stream.member": member },
|
|
128
|
+
}),
|
|
129
|
+
(span) => {
|
|
130
|
+
let items = 0;
|
|
131
|
+
let bytes = 0;
|
|
132
|
+
const counted = Stream.map(stream, (item) => {
|
|
133
|
+
items++;
|
|
134
|
+
if (item instanceof Uint8Array) bytes += item.byteLength;
|
|
135
|
+
return item;
|
|
136
|
+
});
|
|
137
|
+
return Stream.onExit(counted, (exit) =>
|
|
138
|
+
Effect.clockWith((clock) =>
|
|
139
|
+
Effect.sync(() => {
|
|
140
|
+
span.attribute("distilled.stream.items", items);
|
|
141
|
+
if (bytes > 0) span.attribute("distilled.stream.bytes", bytes);
|
|
142
|
+
if (Exit.isFailure(exit) && !Cause.hasInterruptsOnly(exit.cause)) {
|
|
143
|
+
span.attribute("error.type", tagOf(Cause.squash(exit.cause)));
|
|
144
|
+
}
|
|
145
|
+
span.end(clock.currentTimeNanosUnsafe(), exit);
|
|
146
|
+
}),
|
|
147
|
+
),
|
|
148
|
+
);
|
|
149
|
+
},
|
|
150
|
+
),
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
const LOCATION = /\(?((?:file:\/\/)?[^\s()]+):(\d+):(\d+)\)?$/;
|
|
154
|
+
|
|
155
|
+
/** OTel `code.*` attributes for the caller's line. */
|
|
156
|
+
const callSite: DebugHooks["callSite"] = (site) => {
|
|
157
|
+
const line = site?.();
|
|
158
|
+
const match = line ? LOCATION.exec(line) : null;
|
|
159
|
+
if (!match) return undefined;
|
|
160
|
+
const fn = /^at (\S+) \(/.exec(line!)?.[1];
|
|
161
|
+
return {
|
|
162
|
+
"code.file.path": match[1],
|
|
163
|
+
"code.line.number": Number(match[2]),
|
|
164
|
+
"code.column.number": Number(match[3]),
|
|
165
|
+
...(fn ? { "code.function.name": fn } : {}),
|
|
166
|
+
};
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
export const TelemetryDebug = {
|
|
170
|
+
/** Turns on debug spans and debug log lines for every distilled call it is provided to. */
|
|
171
|
+
layer: /*@__PURE__*/ Layer.succeed(Debug, {
|
|
172
|
+
phase,
|
|
173
|
+
attempt,
|
|
174
|
+
readStream,
|
|
175
|
+
callSite,
|
|
176
|
+
call,
|
|
177
|
+
http,
|
|
178
|
+
}) as Layer.Layer<never>,
|
|
179
|
+
} as const;
|