@distilled.cloud/core 1.0.0-rc.2 → 1.0.0-rc.21
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/README.md +33 -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 +3 -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 +220 -51
- package/lib/api.d.ts.map +1 -1
- package/lib/api.js +1 -178
- package/lib/category.d.ts +19 -16
- 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 +81 -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 +99 -74
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +1 -186
- package/lib/graphql.d.ts +420 -0
- package/lib/graphql.d.ts.map +1 -0
- package/lib/graphql.js +1 -0
- package/lib/graphql.types.d.ts +11 -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 +111 -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/max-message-bytes.d.ts +42 -0
- package/lib/max-message-bytes.d.ts.map +1 -0
- package/lib/max-message-bytes.js +2 -0
- package/lib/max-message-bytes.js.map +1 -0
- package/lib/ndjson.d.ts +3 -0
- package/lib/ndjson.d.ts.map +1 -0
- package/lib/ndjson.js +2 -0
- package/lib/ndjson.js.map +1 -0
- package/lib/pagination.d.ts +105 -25
- package/lib/pagination.d.ts.map +1 -1
- package/lib/pagination.js +1 -182
- package/lib/plain-values.d.ts +21 -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 +85 -0
- package/lib/protobuf.d.ts.map +1 -0
- package/lib/protobuf.js +1 -0
- package/lib/protocol-grpc.d.ts +123 -0
- package/lib/protocol-grpc.d.ts.map +1 -0
- package/lib/protocol-grpc.js +1 -0
- package/lib/protocol-http.d.ts +206 -25
- package/lib/protocol-http.d.ts.map +1 -1
- package/lib/protocol-http.js +1 -554
- package/lib/protocol-rest.d.ts +258 -64
- package/lib/protocol-rest.d.ts.map +1 -1
- package/lib/protocol-rest.js +1 -242
- package/lib/protocol.d.ts +113 -0
- package/lib/protocol.d.ts.map +1 -0
- package/lib/protocol.js +0 -0
- 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 +4 -4
- package/lib/retry-after.d.ts.map +1 -1
- package/lib/retry-after.js +1 -112
- package/lib/retry-state.d.ts +26 -0
- package/lib/retry-state.d.ts.map +1 -0
- package/lib/retry-state.js +2 -0
- package/lib/retry-state.js.map +1 -0
- package/lib/retry.d.ts +141 -42
- package/lib/retry.d.ts.map +1 -1
- package/lib/retry.js +1 -210
- package/lib/schema-lite-impl.d.ts +179 -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 +41 -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 +31 -0
- package/lib/sdk-error.d.ts.map +1 -0
- package/lib/sdk-error.js +1 -0
- package/lib/send.d.ts +37 -0
- package/lib/send.d.ts.map +1 -0
- package/lib/send.js +2 -0
- package/lib/send.js.map +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 +317 -0
- package/lib/telemetry.d.ts.map +1 -0
- package/lib/telemetry.js +3 -0
- package/lib/telemetry.js.map +1 -0
- package/lib/token-cache.d.ts +31 -0
- package/lib/token-cache.d.ts.map +1 -0
- package/lib/token-cache.js +2 -0
- package/lib/token-cache.js.map +1 -0
- package/lib/trait.d.ts +94 -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 +1597 -234
- package/src/category.ts +97 -134
- package/src/chunks.ts +30 -0
- package/src/config.ts +261 -0
- package/src/crypto.ts +202 -0
- package/src/error-category.ts +16 -29
- package/src/errors.ts +99 -77
- package/src/graphql.ts +1817 -0
- package/src/graphql.types.ts +120 -0
- package/src/grpc-transport-cloudflare-http2.ts +569 -0
- package/src/grpc-transport-cloudflare-sockets.ts +368 -0
- package/src/grpc-transport-node-http2.ts +307 -0
- package/src/grpc-transport.ts +287 -0
- package/src/hpack.ts +331 -0
- package/src/index.ts +31 -0
- package/src/max-message-bytes.ts +58 -0
- package/src/ndjson.ts +55 -0
- package/src/pagination.ts +379 -94
- package/src/plain-values.ts +70 -0
- package/src/protobuf.ts +1367 -0
- package/src/protocol-grpc.ts +811 -0
- package/src/protocol-http.ts +1045 -371
- package/src/protocol-rest.ts +908 -255
- package/src/protocol.ts +150 -0
- package/src/query.ts +39 -0
- package/src/response-validation-strict.ts +333 -0
- package/src/response-validation.ts +161 -0
- package/src/retry-after.ts +8 -14
- package/src/retry-state.ts +105 -0
- package/src/retry.ts +260 -145
- package/src/schema-lite-impl.ts +621 -0
- package/src/schema-lite-typed.ts +44 -0
- package/src/schema-lite.ts +45 -0
- package/src/schema.ts +259 -15
- package/src/sdk-error.ts +37 -0
- package/src/send.ts +156 -0
- package/src/sensitive.ts +188 -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 +800 -0
- package/src/token-cache.ts +117 -0
- package/src/trait.ts +139 -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/crypto.ts
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hashing and HMAC for request signing, as a swappable service.
|
|
3
|
+
*
|
|
4
|
+
* SDKs that sign requests (AWS SigV4, webhook verification, ...) read
|
|
5
|
+
* {@link Crypto} from the context. It is a reference with a default, so it
|
|
6
|
+
* never appears in an operation's requirements: by default it uses
|
|
7
|
+
* `node:crypto` when the runtime has it (Node, Bun, Deno -- synchronous, and
|
|
8
|
+
* several times faster than WebCrypto for small inputs) and WebCrypto
|
|
9
|
+
* (`crypto.subtle`) everywhere else.
|
|
10
|
+
*
|
|
11
|
+
* Provide a layer to choose or replace the implementation:
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { Crypto } from "@distilled.cloud/core";
|
|
15
|
+
* program.pipe(Effect.provide(Crypto.webCrypto)); // force WebCrypto
|
|
16
|
+
* program.pipe(Effect.provide(Crypto.layer(myHsmBackedCrypto)));
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* HMAC keys are imported once into an implementation-specific handle
|
|
20
|
+
* ({@link HmacKey}) so callers can cache them: preparing a raw key is most of
|
|
21
|
+
* an HMAC's cost in both `node:crypto` and WebCrypto.
|
|
22
|
+
*/
|
|
23
|
+
import * as Context from "effect/Context";
|
|
24
|
+
import * as Data from "effect/Data";
|
|
25
|
+
import * as Effect from "effect/Effect";
|
|
26
|
+
import * as Layer from "effect/Layer";
|
|
27
|
+
|
|
28
|
+
/** A hashing or HMAC operation failed in the crypto implementation. */
|
|
29
|
+
export class CryptoError
|
|
30
|
+
extends /*@__PURE__*/ Data.TaggedError("CryptoError")<{
|
|
31
|
+
readonly operation: "hmac" | "sha256" | "importKey";
|
|
32
|
+
readonly cause: unknown;
|
|
33
|
+
}> {}
|
|
34
|
+
|
|
35
|
+
/** An imported HMAC-SHA256 key. Opaque: only the implementation that made it can use it. */
|
|
36
|
+
export interface HmacKey {
|
|
37
|
+
readonly "~distilled/HmacKey": unique symbol;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export type Bytes = Uint8Array | string;
|
|
41
|
+
|
|
42
|
+
export interface CryptoImpl {
|
|
43
|
+
/** Name for diagnostics, e.g. "node:crypto" or "webcrypto". */
|
|
44
|
+
readonly name: string;
|
|
45
|
+
readonly sha256: (data: Bytes) => Effect.Effect<Uint8Array, CryptoError>;
|
|
46
|
+
readonly importHmacKey: (key: Bytes) => Effect.Effect<HmacKey, CryptoError>;
|
|
47
|
+
readonly hmacSha256: (key: HmacKey, data: Bytes) => Effect.Effect<Uint8Array, CryptoError>;
|
|
48
|
+
/**
|
|
49
|
+
* The same operations as plain functions, for implementations that are
|
|
50
|
+
* synchronous (node:crypto). They throw on failure. Signers use them to
|
|
51
|
+
* skip the per-operation effects when present.
|
|
52
|
+
*/
|
|
53
|
+
readonly sync?: {
|
|
54
|
+
readonly sha256: (data: Bytes) => Uint8Array;
|
|
55
|
+
readonly importHmacKey: (key: Bytes) => HmacKey;
|
|
56
|
+
readonly hmacSha256: (key: HmacKey, data: Bytes) => Uint8Array;
|
|
57
|
+
/** `hex(sha256(data))`, when the implementation can produce hex itself. */
|
|
58
|
+
readonly sha256Hex?: (data: Bytes) => string;
|
|
59
|
+
/** `hex(hmacSha256(key, data))`, when the implementation can produce hex itself. */
|
|
60
|
+
readonly hmacSha256Hex?: (key: HmacKey, data: Bytes) => string;
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
//#region node:crypto
|
|
65
|
+
|
|
66
|
+
interface Digest {
|
|
67
|
+
update(d: Bytes): { digest(): Uint8Array; digest(encoding: "hex"): string };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
interface NodeCryptoModule {
|
|
71
|
+
createSecretKey(key: Uint8Array): object;
|
|
72
|
+
createHmac(alg: string, key: object): Digest;
|
|
73
|
+
createHash(alg: string): Digest;
|
|
74
|
+
/** One-shot hashing (Node 20.12+/21.7+). */
|
|
75
|
+
hash?: (alg: string, data: Bytes, encoding: "hex") => string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** `node:crypto`, found without a static import so this module loads in any runtime. */
|
|
79
|
+
const nodeModule = (): NodeCryptoModule | undefined => {
|
|
80
|
+
try {
|
|
81
|
+
const p = (globalThis as { process?: { getBuiltinModule?: (id: string) => unknown } }).process;
|
|
82
|
+
return p?.getBuiltinModule?.("node:crypto") as NodeCryptoModule | undefined;
|
|
83
|
+
} catch {
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const encoder = /*@__PURE__*/ new TextEncoder();
|
|
89
|
+
const toBytes = (data: Bytes): Uint8Array =>
|
|
90
|
+
typeof data === "string" ? encoder.encode(data) : data;
|
|
91
|
+
|
|
92
|
+
/** `node:crypto` implementation, or undefined when the runtime has no `node:crypto`. */
|
|
93
|
+
export const makeNodeCrypto = (): CryptoImpl | undefined => {
|
|
94
|
+
const c = nodeModule();
|
|
95
|
+
if (!c) return undefined;
|
|
96
|
+
return {
|
|
97
|
+
name: "node:crypto",
|
|
98
|
+
sync: {
|
|
99
|
+
sha256: (data) => c.createHash("sha256").update(data).digest(),
|
|
100
|
+
importHmacKey: (key) => c.createSecretKey(toBytes(key)) as unknown as HmacKey,
|
|
101
|
+
hmacSha256: (key, data) =>
|
|
102
|
+
c
|
|
103
|
+
.createHmac("sha256", key as unknown as object)
|
|
104
|
+
.update(data)
|
|
105
|
+
.digest(),
|
|
106
|
+
sha256Hex: c.hash
|
|
107
|
+
? (data) => c.hash!("sha256", data, "hex")
|
|
108
|
+
: (data) => c.createHash("sha256").update(data).digest("hex"),
|
|
109
|
+
hmacSha256Hex: (key, data) =>
|
|
110
|
+
c
|
|
111
|
+
.createHmac("sha256", key as unknown as object)
|
|
112
|
+
.update(data)
|
|
113
|
+
.digest("hex"),
|
|
114
|
+
},
|
|
115
|
+
sha256: (data) =>
|
|
116
|
+
Effect.try({
|
|
117
|
+
try: () => c.createHash("sha256").update(data).digest(),
|
|
118
|
+
catch: (cause) => new CryptoError({ operation: "sha256", cause }),
|
|
119
|
+
}),
|
|
120
|
+
importHmacKey: (key) =>
|
|
121
|
+
Effect.try({
|
|
122
|
+
try: () => c.createSecretKey(toBytes(key)) as unknown as HmacKey,
|
|
123
|
+
catch: (cause) => new CryptoError({ operation: "importKey", cause }),
|
|
124
|
+
}),
|
|
125
|
+
hmacSha256: (key, data) =>
|
|
126
|
+
Effect.try({
|
|
127
|
+
try: () =>
|
|
128
|
+
c
|
|
129
|
+
.createHmac("sha256", key as unknown as object)
|
|
130
|
+
.update(data)
|
|
131
|
+
.digest(),
|
|
132
|
+
catch: (cause) => new CryptoError({ operation: "hmac", cause }),
|
|
133
|
+
}),
|
|
134
|
+
};
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
//#endregion
|
|
138
|
+
|
|
139
|
+
//#region WebCrypto
|
|
140
|
+
|
|
141
|
+
const buffer = (data: Bytes): Uint8Array<ArrayBuffer> => {
|
|
142
|
+
const b = toBytes(data);
|
|
143
|
+
return b.buffer instanceof ArrayBuffer ? (b as Uint8Array<ArrayBuffer>) : new Uint8Array(b);
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
/** WebCrypto (`crypto.subtle`) implementation. */
|
|
147
|
+
export const makeWebCrypto = (): CryptoImpl => ({
|
|
148
|
+
name: "webcrypto",
|
|
149
|
+
sha256: (data) =>
|
|
150
|
+
Effect.tryPromise({
|
|
151
|
+
try: async () => new Uint8Array(await crypto.subtle.digest("SHA-256", buffer(data))),
|
|
152
|
+
catch: (cause) => new CryptoError({ operation: "sha256", cause }),
|
|
153
|
+
}),
|
|
154
|
+
importHmacKey: (key) =>
|
|
155
|
+
Effect.tryPromise({
|
|
156
|
+
try: () =>
|
|
157
|
+
crypto.subtle.importKey("raw", buffer(key), { name: "HMAC", hash: "SHA-256" }, false, [
|
|
158
|
+
"sign",
|
|
159
|
+
]) as Promise<unknown> as Promise<HmacKey>,
|
|
160
|
+
catch: (cause) => new CryptoError({ operation: "importKey", cause }),
|
|
161
|
+
}),
|
|
162
|
+
hmacSha256: (key, data) =>
|
|
163
|
+
Effect.tryPromise({
|
|
164
|
+
try: async () =>
|
|
165
|
+
new Uint8Array(await crypto.subtle.sign("HMAC", key as unknown as CryptoKey, buffer(data))),
|
|
166
|
+
catch: (cause) => new CryptoError({ operation: "hmac", cause }),
|
|
167
|
+
}),
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
//#endregion
|
|
171
|
+
|
|
172
|
+
/** The crypto implementation signing code uses. Defaults to `node:crypto` when available, else WebCrypto. */
|
|
173
|
+
export const Crypto = /*@__PURE__*/ Context.Reference<CryptoImpl>("@distilled.cloud/core/Crypto", {
|
|
174
|
+
defaultValue: () => makeNodeCrypto() ?? makeWebCrypto(),
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
/** Use a custom implementation. */
|
|
178
|
+
export const layer = (impl: CryptoImpl): Layer.Layer<never> => Layer.succeed(Crypto, impl);
|
|
179
|
+
|
|
180
|
+
/** Force WebCrypto, even where `node:crypto` exists. */
|
|
181
|
+
export const webCrypto: Layer.Layer<never> = /*@__PURE__*/ Layer.sync(Crypto, makeWebCrypto);
|
|
182
|
+
|
|
183
|
+
/** Force `node:crypto`; fails at layer build when the runtime has none. */
|
|
184
|
+
export const nodeCrypto: Layer.Layer<never, CryptoError> = /*@__PURE__*/ Layer.effect(
|
|
185
|
+
Crypto,
|
|
186
|
+
/*@__PURE__*/ Effect.suspend(() => {
|
|
187
|
+
const impl = makeNodeCrypto();
|
|
188
|
+
return impl
|
|
189
|
+
? Effect.succeed(impl)
|
|
190
|
+
: Effect.fail(
|
|
191
|
+
new CryptoError({ operation: "importKey", cause: "node:crypto is not available" }),
|
|
192
|
+
);
|
|
193
|
+
}),
|
|
194
|
+
);
|
|
195
|
+
|
|
196
|
+
const HEX = /*@__PURE__*/ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
|
|
197
|
+
/** Lowercase hex of bytes. */
|
|
198
|
+
export const hex = (bytes: Uint8Array): string => {
|
|
199
|
+
let out = "";
|
|
200
|
+
for (let i = 0; i < bytes.length; i++) out += HEX[bytes[i]!];
|
|
201
|
+
return out;
|
|
202
|
+
};
|
package/src/error-category.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as Effect from "effect/Effect";
|
|
2
|
+
import * as Predicate from "effect/Predicate";
|
|
2
3
|
|
|
3
4
|
export const categoriesKey = "@distilled/meta/error-categories";
|
|
4
5
|
|
|
@@ -26,9 +27,7 @@ export const withCategory =
|
|
|
26
27
|
return Mixed as any;
|
|
27
28
|
};
|
|
28
29
|
|
|
29
|
-
export type AllKeys<E> = E extends { [categoriesKey]: infer Q }
|
|
30
|
-
? keyof Q
|
|
31
|
-
: never;
|
|
30
|
+
export type AllKeys<E> = E extends { [categoriesKey]: infer Q } ? keyof Q : never;
|
|
32
31
|
|
|
33
32
|
export type ExtractAll<E, Cats extends PropertyKey> = Cats extends any
|
|
34
33
|
? Extract<E, { [categoriesKey]: { [K in Cats]: any } }>
|
|
@@ -40,9 +39,7 @@ export type ExtractAll<E, Cats extends PropertyKey> = Cats extends any
|
|
|
40
39
|
*/
|
|
41
40
|
export const hasCategory =
|
|
42
41
|
<const Cat extends PropertyKey>(category: Cat) =>
|
|
43
|
-
(
|
|
44
|
-
value: unknown,
|
|
45
|
-
): value is { readonly [categoriesKey]: { readonly [K in Cat]: true } } => {
|
|
42
|
+
(value: unknown): value is { readonly [categoriesKey]: { readonly [K in Cat]: true } } => {
|
|
46
43
|
if (!Predicate.isObject(value)) return false;
|
|
47
44
|
if (!Predicate.hasProperty(categoriesKey)(value)) return false;
|
|
48
45
|
return category in (value as any)[categoriesKey];
|
|
@@ -59,26 +56,16 @@ export const hasAnyCategory =
|
|
|
59
56
|
return false;
|
|
60
57
|
};
|
|
61
58
|
|
|
62
|
-
export const catchCategory =
|
|
63
|
-
<E,
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
]
|
|
68
|
-
) =>
|
|
69
|
-
|
|
59
|
+
export const catchCategory = <E, const Categories extends Array<AllKeys<E>>, A2, E2, R2>(
|
|
60
|
+
...args: [...Categories, f: (err: ExtractAll<E, Categories[number]>) => Effect.Effect<A2, E2, R2>]
|
|
61
|
+
) => {
|
|
62
|
+
// Split once, without mutating `args`, so one catcher can be reused.
|
|
63
|
+
const f = args[args.length - 1] as (
|
|
64
|
+
err: ExtractAll<E, Categories[number]>,
|
|
65
|
+
) => Effect.Effect<A2, E2, R2>;
|
|
66
|
+
const matches = hasAnyCategory(...(args.slice(0, -1) as Array<PropertyKey>));
|
|
67
|
+
return <A, R>(
|
|
70
68
|
effect: Effect.Effect<A, E, R>,
|
|
71
|
-
): Effect.Effect<
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
R | R2
|
|
75
|
-
> => {
|
|
76
|
-
const f = args.pop()! as (
|
|
77
|
-
err: ExtractAll<E, Categories[number]>,
|
|
78
|
-
) => Effect.Effect<A2, E2, R2>;
|
|
79
|
-
const categories = args as Array<PropertyKey>;
|
|
80
|
-
const matches = hasAnyCategory(...categories);
|
|
81
|
-
return Effect.catchIf(effect, matches, (e) =>
|
|
82
|
-
f(e as ExtractAll<E, Categories[number]>),
|
|
83
|
-
) as any;
|
|
84
|
-
};
|
|
69
|
+
): Effect.Effect<A | A2, E2 | Exclude<E, ExtractAll<E, Categories[number]>>, R | R2> =>
|
|
70
|
+
Effect.catchIf(effect, matches, (e) => f(e as ExtractAll<E, Categories[number]>)) as any;
|
|
71
|
+
};
|
package/src/errors.ts
CHANGED
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
* that are used across all SDKs.
|
|
7
7
|
*/
|
|
8
8
|
import * as Duration from "effect/Duration";
|
|
9
|
-
import * as Schema from "effect/Schema";
|
|
10
9
|
import * as Category from "./category.ts";
|
|
10
|
+
import * as Schema from "./schema-lite-typed.ts";
|
|
11
11
|
|
|
12
12
|
// ============================================================================
|
|
13
13
|
// Shared Schema Helpers
|
|
@@ -18,13 +18,21 @@ import * as Category from "./category.ts";
|
|
|
18
18
|
*
|
|
19
19
|
* Used on retryable errors to carry a server-provided wait hint (e.g. parsed
|
|
20
20
|
* from `Retry-After` or the IETF `Ratelimit` header). Core does not encode/decode
|
|
21
|
-
* this field over the wire
|
|
21
|
+
* this field over the wire -- it is constructed at runtime by `matchError` from
|
|
22
22
|
* whatever the service actually returns (delay-seconds, HTTP-date, epoch, etc.)
|
|
23
23
|
* and consumed at runtime by the retry policy.
|
|
24
24
|
*/
|
|
25
|
-
export const DurationSchema = Schema.declare<Duration.Duration>(
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
export const DurationSchema = /*@__PURE__*/ Schema.declare<Duration.Duration>(Duration.isDuration);
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Fields every error built from an HTTP response declares: the parsed body
|
|
29
|
+
* (the raw text when it wasn't JSON) and the response headers, for whatever
|
|
30
|
+
* the SDK does not type -- a request ID, nested details.
|
|
31
|
+
*/
|
|
32
|
+
export const ResponseErrorFields = {
|
|
33
|
+
body: /*@__PURE__*/ Schema.optional(Schema.Unknown),
|
|
34
|
+
headers: /*@__PURE__*/ Schema.optional(/*@__PURE__*/ Schema.Record(Schema.String, Schema.String)),
|
|
35
|
+
};
|
|
28
36
|
|
|
29
37
|
// ============================================================================
|
|
30
38
|
// Common HTTP Status Error Classes
|
|
@@ -33,70 +41,79 @@ export const DurationSchema = Schema.declare<Duration.Duration>(
|
|
|
33
41
|
/**
|
|
34
42
|
* Unauthorized - Authentication failure (401).
|
|
35
43
|
*/
|
|
36
|
-
export class Unauthorized
|
|
37
|
-
"Unauthorized",
|
|
38
|
-
|
|
39
|
-
|
|
44
|
+
export class Unauthorized
|
|
45
|
+
extends /*@__PURE__*/ Schema.TaggedError<Unauthorized>()("Unauthorized", {
|
|
46
|
+
message: Schema.String,
|
|
47
|
+
...ResponseErrorFields,
|
|
48
|
+
}).pipe(Category.withAuthError) {}
|
|
40
49
|
|
|
41
50
|
/**
|
|
42
51
|
* Forbidden - Access denied (403).
|
|
43
52
|
*/
|
|
44
|
-
export class Forbidden
|
|
45
|
-
"Forbidden",
|
|
46
|
-
|
|
47
|
-
|
|
53
|
+
export class Forbidden
|
|
54
|
+
extends /*@__PURE__*/ Schema.TaggedError<Forbidden>()("Forbidden", {
|
|
55
|
+
message: Schema.String,
|
|
56
|
+
...ResponseErrorFields,
|
|
57
|
+
}).pipe(Category.withAuthError) {}
|
|
48
58
|
|
|
49
59
|
/**
|
|
50
60
|
* NotFound - Resource not found (404).
|
|
51
61
|
*/
|
|
52
|
-
export class NotFound
|
|
53
|
-
|
|
54
|
-
|
|
62
|
+
export class NotFound
|
|
63
|
+
extends /*@__PURE__*/ Schema.TaggedError<NotFound>()("NotFound", {
|
|
64
|
+
message: Schema.String,
|
|
65
|
+
...ResponseErrorFields,
|
|
66
|
+
}).pipe(Category.withNotFoundError) {}
|
|
55
67
|
|
|
56
68
|
/**
|
|
57
69
|
* BadRequest - Invalid request (400).
|
|
58
70
|
*/
|
|
59
|
-
export class BadRequest
|
|
60
|
-
"BadRequest",
|
|
61
|
-
|
|
62
|
-
|
|
71
|
+
export class BadRequest
|
|
72
|
+
extends /*@__PURE__*/ Schema.TaggedError<BadRequest>()("BadRequest", {
|
|
73
|
+
message: Schema.String,
|
|
74
|
+
...ResponseErrorFields,
|
|
75
|
+
}).pipe(Category.withBadRequestError) {}
|
|
63
76
|
|
|
64
77
|
/**
|
|
65
78
|
* Conflict - Resource conflict (409).
|
|
66
79
|
*/
|
|
67
|
-
export class Conflict
|
|
68
|
-
|
|
69
|
-
|
|
80
|
+
export class Conflict
|
|
81
|
+
extends /*@__PURE__*/ Schema.TaggedError<Conflict>()("Conflict", {
|
|
82
|
+
message: Schema.String,
|
|
83
|
+
...ResponseErrorFields,
|
|
84
|
+
}).pipe(Category.withConflictError) {}
|
|
70
85
|
|
|
71
86
|
/**
|
|
72
87
|
* UnprocessableEntity - Validation error (422).
|
|
73
88
|
*/
|
|
74
|
-
export class UnprocessableEntity
|
|
75
|
-
"UnprocessableEntity",
|
|
76
|
-
|
|
77
|
-
|
|
89
|
+
export class UnprocessableEntity
|
|
90
|
+
extends /*@__PURE__*/ Schema.TaggedError<UnprocessableEntity>()("UnprocessableEntity", {
|
|
91
|
+
message: Schema.String,
|
|
92
|
+
...ResponseErrorFields,
|
|
93
|
+
}).pipe(Category.withBadRequestError) {}
|
|
78
94
|
|
|
79
95
|
/**
|
|
80
96
|
* TooManyRequests - Rate limited (429).
|
|
81
97
|
*/
|
|
82
|
-
export class TooManyRequests
|
|
83
|
-
"TooManyRequests",
|
|
84
|
-
{
|
|
98
|
+
export class TooManyRequests
|
|
99
|
+
extends /*@__PURE__*/ Schema.TaggedError<TooManyRequests>()("TooManyRequests", {
|
|
85
100
|
message: Schema.String,
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
).pipe(
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
) {}
|
|
101
|
+
...ResponseErrorFields,
|
|
102
|
+
retryAfter: /*@__PURE__*/ Schema.optional(DurationSchema),
|
|
103
|
+
}).pipe(
|
|
104
|
+
Category.withThrottlingError,
|
|
105
|
+
/*@__PURE__*/ Category.withRetryable({ throttling: true }),
|
|
106
|
+
) {}
|
|
92
107
|
|
|
93
108
|
/**
|
|
94
109
|
* Locked - Resource locked (423).
|
|
95
110
|
*/
|
|
96
|
-
export class Locked
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
111
|
+
export class Locked
|
|
112
|
+
extends /*@__PURE__*/ Schema.TaggedError<Locked>()("Locked", {
|
|
113
|
+
message: Schema.String,
|
|
114
|
+
...ResponseErrorFields,
|
|
115
|
+
retryAfter: /*@__PURE__*/ Schema.optional(DurationSchema),
|
|
116
|
+
}).pipe(Category.withLockedError, /*@__PURE__*/ Category.withRetryable()) {}
|
|
100
117
|
|
|
101
118
|
/**
|
|
102
119
|
* InternalServerError - Server error.
|
|
@@ -106,55 +123,59 @@ export class Locked extends Schema.TaggedErrorClass<Locked>()("Locked", {
|
|
|
106
123
|
* 200 envelope carrying `{ code: 10002, message: "An unknown error has
|
|
107
124
|
* occurred" }` for transient backend failures on some endpoints).
|
|
108
125
|
*/
|
|
109
|
-
export class InternalServerError
|
|
110
|
-
"InternalServerError",
|
|
111
|
-
{
|
|
126
|
+
export class InternalServerError
|
|
127
|
+
extends /*@__PURE__*/ Schema.TaggedError<InternalServerError>()("InternalServerError", {
|
|
112
128
|
message: Schema.String,
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
).pipe(Category.withServerError, Category.withRetryable()) {}
|
|
129
|
+
...ResponseErrorFields,
|
|
130
|
+
code: /*@__PURE__*/ Schema.optional(Schema.Number),
|
|
131
|
+
retryAfter: /*@__PURE__*/ Schema.optional(DurationSchema),
|
|
132
|
+
}).pipe(Category.withServerError, /*@__PURE__*/ Category.withRetryable()) {}
|
|
117
133
|
|
|
118
134
|
/**
|
|
119
135
|
* BadGateway - Bad gateway (502).
|
|
120
136
|
*/
|
|
121
|
-
export class BadGateway
|
|
122
|
-
"BadGateway",
|
|
123
|
-
{
|
|
137
|
+
export class BadGateway
|
|
138
|
+
extends /*@__PURE__*/ Schema.TaggedError<BadGateway>()("BadGateway", {
|
|
124
139
|
message: Schema.String,
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
).pipe(Category.withServerError, Category.withRetryable()) {}
|
|
140
|
+
...ResponseErrorFields,
|
|
141
|
+
retryAfter: /*@__PURE__*/ Schema.optional(DurationSchema),
|
|
142
|
+
}).pipe(Category.withServerError, /*@__PURE__*/ Category.withRetryable()) {}
|
|
128
143
|
|
|
129
144
|
/**
|
|
130
145
|
* ServiceUnavailable - Service unavailable (503).
|
|
131
146
|
*/
|
|
132
|
-
export class ServiceUnavailable
|
|
133
|
-
"ServiceUnavailable",
|
|
134
|
-
{
|
|
147
|
+
export class ServiceUnavailable
|
|
148
|
+
extends /*@__PURE__*/ Schema.TaggedError<ServiceUnavailable>()("ServiceUnavailable", {
|
|
135
149
|
message: Schema.String,
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
).pipe(Category.withServerError, Category.withRetryable()) {}
|
|
150
|
+
...ResponseErrorFields,
|
|
151
|
+
retryAfter: /*@__PURE__*/ Schema.optional(DurationSchema),
|
|
152
|
+
}).pipe(Category.withServerError, /*@__PURE__*/ Category.withRetryable()) {}
|
|
139
153
|
|
|
140
154
|
/**
|
|
141
|
-
* GatewayTimeout -
|
|
155
|
+
* GatewayTimeout - HTTP request or gateway timeout (408, 504).
|
|
142
156
|
*/
|
|
143
|
-
export class GatewayTimeout
|
|
144
|
-
"GatewayTimeout",
|
|
145
|
-
{
|
|
157
|
+
export class GatewayTimeout
|
|
158
|
+
extends /*@__PURE__*/ Schema.TaggedError<GatewayTimeout>()("GatewayTimeout", {
|
|
146
159
|
message: Schema.String,
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
).pipe(Category.withServerError, Category.withRetryable()) {}
|
|
160
|
+
...ResponseErrorFields,
|
|
161
|
+
retryAfter: /*@__PURE__*/ Schema.optional(DurationSchema),
|
|
162
|
+
}).pipe(Category.withServerError, /*@__PURE__*/ Category.withRetryable()) {}
|
|
150
163
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
164
|
+
// Defined with the config readers (core's `base` bundle group, which every
|
|
165
|
+
// program loads) so they can fail with it without importing this module.
|
|
166
|
+
export { ConfigError } from "./config.ts";
|
|
167
|
+
|
|
168
|
+
// Defined with the label check (`protocol-http.ts`), so protocols can fail
|
|
169
|
+
// with it without importing every class in this module.
|
|
170
|
+
export { InvalidInput } from "./protocol-http.ts";
|
|
171
|
+
|
|
172
|
+
// Defined in the `base` bundle group (`sdk-error.ts`), with pagination and
|
|
173
|
+
// the operation runtime that fail with it.
|
|
174
|
+
export { SdkError } from "./sdk-error.ts";
|
|
175
|
+
|
|
176
|
+
// Defined in the `base` bundle group (`unknown-error.ts`), so AWS spreads
|
|
177
|
+
// them without importing the REST modules.
|
|
178
|
+
export { UnknownErrorFields, type UnknownErrorProps } from "./unknown-error.ts";
|
|
158
179
|
|
|
159
180
|
// ============================================================================
|
|
160
181
|
// Error Maps
|
|
@@ -168,6 +189,7 @@ export const HTTP_STATUS_MAP = {
|
|
|
168
189
|
401: Unauthorized,
|
|
169
190
|
403: Forbidden,
|
|
170
191
|
404: NotFound,
|
|
192
|
+
408: GatewayTimeout,
|
|
171
193
|
409: Conflict,
|
|
172
194
|
422: UnprocessableEntity,
|
|
173
195
|
423: Locked,
|
|
@@ -182,19 +204,19 @@ export const HTTP_STATUS_MAP = {
|
|
|
182
204
|
* HTTP status codes that are considered "default" errors (always present).
|
|
183
205
|
* These are excluded from per-operation error types since they're handled globally.
|
|
184
206
|
*/
|
|
185
|
-
export const DEFAULT_ERROR_STATUSES = new Set([401, 429, 500, 502, 503, 504]);
|
|
207
|
+
export const DEFAULT_ERROR_STATUSES = /*@__PURE__*/ new Set([401, 429, 500, 502, 503, 504]);
|
|
186
208
|
|
|
187
209
|
/**
|
|
188
210
|
* HTTP status codes whose corresponding error class in {@link HTTP_STATUS_MAP}
|
|
189
|
-
* declares a `retryAfter` field. The retry policy
|
|
190
|
-
*
|
|
211
|
+
* declares a `retryAfter` field. The retry policy waits at least
|
|
212
|
+
* `error.retryAfter` for these.
|
|
191
213
|
*
|
|
192
214
|
* `matchError` implementations should only pass `retryAfter` into the
|
|
193
215
|
* constructor when the status is in this set; passing it on non-retryable
|
|
194
216
|
* classes (BadRequest/Unauthorized/etc.) would silently retain it as a
|
|
195
217
|
* stale field on the instance and pollute serialized output.
|
|
196
218
|
*/
|
|
197
|
-
export const RETRYABLE_HTTP_STATUSES = new Set([423, 429, 500, 502, 503, 504]);
|
|
219
|
+
export const RETRYABLE_HTTP_STATUSES = /*@__PURE__*/ new Set([408, 423, 429, 500, 502, 503, 504]);
|
|
198
220
|
|
|
199
221
|
/**
|
|
200
222
|
* All common API error classes.
|