@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.
Files changed (291) hide show
  1. package/LICENSE +201 -0
  2. package/lib/_bundle/aws.js +2 -0
  3. package/lib/_bundle/aws.js.map +1 -0
  4. package/lib/_bundle/base.js +5 -0
  5. package/lib/_bundle/base.js.map +1 -0
  6. package/lib/_bundle/graphql.js +3 -0
  7. package/lib/_bundle/graphql.js.map +1 -0
  8. package/lib/_bundle/grpc.js +2 -0
  9. package/lib/_bundle/grpc.js.map +1 -0
  10. package/lib/_bundle/rest.js +2 -0
  11. package/lib/_bundle/rest.js.map +1 -0
  12. package/lib/_bundle/xml.js +4 -0
  13. package/lib/_bundle/xml.js.map +1 -0
  14. package/lib/annotations.d.ts +17 -0
  15. package/lib/annotations.d.ts.map +1 -0
  16. package/lib/annotations.js +2 -0
  17. package/lib/annotations.js.map +1 -0
  18. package/lib/api.d.ts +174 -18
  19. package/lib/api.d.ts.map +1 -1
  20. package/lib/api.js +1 -178
  21. package/lib/category.d.ts +4 -4
  22. package/lib/category.d.ts.map +1 -1
  23. package/lib/category.js +1 -280
  24. package/lib/chunks.d.ts +3 -0
  25. package/lib/chunks.d.ts.map +1 -0
  26. package/lib/chunks.js +1 -0
  27. package/lib/config.d.ts +68 -0
  28. package/lib/config.d.ts.map +1 -0
  29. package/lib/config.js +1 -0
  30. package/lib/crypto.d.ts +76 -0
  31. package/lib/crypto.d.ts.map +1 -0
  32. package/lib/crypto.js +1 -0
  33. package/lib/error-category.d.ts +1 -1
  34. package/lib/error-category.d.ts.map +1 -1
  35. package/lib/error-category.js +1 -46
  36. package/lib/errors.d.ts +64 -72
  37. package/lib/errors.d.ts.map +1 -1
  38. package/lib/errors.js +1 -186
  39. package/lib/graphql.d.ts +403 -0
  40. package/lib/graphql.d.ts.map +1 -0
  41. package/lib/graphql.js +1 -0
  42. package/lib/graphql.types.d.ts +12 -0
  43. package/lib/graphql.types.d.ts.map +1 -0
  44. package/lib/graphql.types.js +1 -0
  45. package/lib/grpc-transport-cloudflare-http2.d.ts +7 -0
  46. package/lib/grpc-transport-cloudflare-http2.d.ts.map +1 -0
  47. package/lib/grpc-transport-cloudflare-http2.js +6 -0
  48. package/lib/grpc-transport-cloudflare-http2.js.map +1 -0
  49. package/lib/grpc-transport-cloudflare-sockets.d.ts +7 -0
  50. package/lib/grpc-transport-cloudflare-sockets.d.ts.map +1 -0
  51. package/lib/grpc-transport-cloudflare-sockets.js +5 -0
  52. package/lib/grpc-transport-cloudflare-sockets.js.map +1 -0
  53. package/lib/grpc-transport-node-http2.d.ts +19 -0
  54. package/lib/grpc-transport-node-http2.d.ts.map +1 -0
  55. package/lib/grpc-transport-node-http2.js +2 -0
  56. package/lib/grpc-transport-node-http2.js.map +1 -0
  57. package/lib/grpc-transport.d.ts +76 -0
  58. package/lib/grpc-transport.d.ts.map +1 -0
  59. package/lib/grpc-transport.js +1 -0
  60. package/lib/hpack.d.ts +34 -0
  61. package/lib/hpack.d.ts.map +1 -0
  62. package/lib/hpack.js +2 -0
  63. package/lib/hpack.js.map +1 -0
  64. package/lib/index.d.ts +32 -0
  65. package/lib/index.d.ts.map +1 -0
  66. package/lib/index.js +17 -0
  67. package/lib/pagination.d.ts +101 -24
  68. package/lib/pagination.d.ts.map +1 -1
  69. package/lib/pagination.js +1 -182
  70. package/lib/plain-values.d.ts +10 -0
  71. package/lib/plain-values.d.ts.map +1 -0
  72. package/lib/plain-values.js +2 -0
  73. package/lib/plain-values.js.map +1 -0
  74. package/lib/protobuf.d.ts +92 -0
  75. package/lib/protobuf.d.ts.map +1 -0
  76. package/lib/protobuf.js +1 -0
  77. package/lib/protocol-grpc.d.ts +86 -0
  78. package/lib/protocol-grpc.d.ts.map +1 -0
  79. package/lib/protocol-grpc.js +1 -0
  80. package/lib/protocol-http.d.ts +102 -23
  81. package/lib/protocol-http.d.ts.map +1 -1
  82. package/lib/protocol-http.js +1 -554
  83. package/lib/protocol-rest.d.ts +120 -50
  84. package/lib/protocol-rest.d.ts.map +1 -1
  85. package/lib/protocol-rest.js +1 -242
  86. package/lib/query.d.ts +29 -0
  87. package/lib/query.d.ts.map +1 -0
  88. package/lib/query.js +1 -0
  89. package/lib/response-validation-strict.d.ts +48 -0
  90. package/lib/response-validation-strict.d.ts.map +1 -0
  91. package/lib/response-validation-strict.js +1 -0
  92. package/lib/response-validation.d.ts +86 -0
  93. package/lib/response-validation.d.ts.map +1 -0
  94. package/lib/response-validation.js +1 -0
  95. package/lib/retry-after.d.ts +3 -3
  96. package/lib/retry-after.d.ts.map +1 -1
  97. package/lib/retry-after.js +1 -112
  98. package/lib/retry.d.ts +48 -29
  99. package/lib/retry.d.ts.map +1 -1
  100. package/lib/retry.js +1 -210
  101. package/lib/schema-lite-impl.d.ts +158 -0
  102. package/lib/schema-lite-impl.d.ts.map +1 -0
  103. package/lib/schema-lite-impl.js +1 -0
  104. package/lib/schema-lite-typed.d.ts +41 -0
  105. package/lib/schema-lite-typed.d.ts.map +1 -0
  106. package/lib/schema-lite-typed.js +1 -0
  107. package/lib/schema-lite.d.ts +40 -0
  108. package/lib/schema-lite.d.ts.map +1 -0
  109. package/lib/schema-lite.js +1 -0
  110. package/lib/schema.d.ts +21 -7
  111. package/lib/schema.d.ts.map +1 -1
  112. package/lib/schema.js +1 -48
  113. package/lib/sdk-error.d.ts +27 -0
  114. package/lib/sdk-error.d.ts.map +1 -0
  115. package/lib/sdk-error.js +1 -0
  116. package/lib/sensitive.d.ts +28 -0
  117. package/lib/sensitive.d.ts.map +1 -0
  118. package/lib/sensitive.js +2 -0
  119. package/lib/sensitive.js.map +1 -0
  120. package/lib/telemetry-debug.d.ts +6 -0
  121. package/lib/telemetry-debug.d.ts.map +1 -0
  122. package/lib/telemetry-debug.js +2 -0
  123. package/lib/telemetry-debug.js.map +1 -0
  124. package/lib/telemetry-hooks.d.ts +32 -0
  125. package/lib/telemetry-hooks.d.ts.map +1 -0
  126. package/lib/telemetry-hooks.js +2 -0
  127. package/lib/telemetry-hooks.js.map +1 -0
  128. package/lib/telemetry-host-rules.d.ts +23 -0
  129. package/lib/telemetry-host-rules.d.ts.map +1 -0
  130. package/lib/telemetry-host-rules.js +2 -0
  131. package/lib/telemetry-host-rules.js.map +1 -0
  132. package/lib/telemetry-lambda-context.d.ts +17 -0
  133. package/lib/telemetry-lambda-context.d.ts.map +1 -0
  134. package/lib/telemetry-lambda-context.js +2 -0
  135. package/lib/telemetry-lambda-context.js.map +1 -0
  136. package/lib/telemetry-message-context.d.ts +52 -0
  137. package/lib/telemetry-message-context.d.ts.map +1 -0
  138. package/lib/telemetry-message-context.js +2 -0
  139. package/lib/telemetry-message-context.js.map +1 -0
  140. package/lib/telemetry-metrics.d.ts +6 -0
  141. package/lib/telemetry-metrics.d.ts.map +1 -0
  142. package/lib/telemetry-metrics.js +2 -0
  143. package/lib/telemetry-metrics.js.map +1 -0
  144. package/lib/telemetry-name-template.d.ts +13 -0
  145. package/lib/telemetry-name-template.d.ts.map +1 -0
  146. package/lib/telemetry-name-template.js +2 -0
  147. package/lib/telemetry-name-template.js.map +1 -0
  148. package/lib/telemetry-stream.d.ts +11 -0
  149. package/lib/telemetry-stream.d.ts.map +1 -0
  150. package/lib/telemetry-stream.js +2 -0
  151. package/lib/telemetry-stream.js.map +1 -0
  152. package/lib/telemetry-xray.d.ts +28 -0
  153. package/lib/telemetry-xray.d.ts.map +1 -0
  154. package/lib/telemetry-xray.js +2 -0
  155. package/lib/telemetry-xray.js.map +1 -0
  156. package/lib/telemetry.d.ts +288 -0
  157. package/lib/telemetry.d.ts.map +1 -0
  158. package/lib/telemetry.js +3 -0
  159. package/lib/telemetry.js.map +1 -0
  160. package/lib/trait.d.ts +71 -20
  161. package/lib/trait.d.ts.map +1 -1
  162. package/lib/trait.js +1 -107
  163. package/lib/unknown-error.d.ts +18 -0
  164. package/lib/unknown-error.d.ts.map +1 -0
  165. package/lib/unknown-error.js +1 -0
  166. package/lib/xml.d.ts +44 -0
  167. package/lib/xml.d.ts.map +1 -0
  168. package/lib/xml.js +1 -0
  169. package/package.json +13 -13
  170. package/src/annotations.ts +53 -0
  171. package/src/api.ts +1370 -200
  172. package/src/category.ts +76 -106
  173. package/src/chunks.ts +30 -0
  174. package/src/config.ts +245 -0
  175. package/src/crypto.ts +202 -0
  176. package/src/error-category.ts +16 -29
  177. package/src/errors.ts +75 -75
  178. package/src/graphql.ts +1778 -0
  179. package/src/graphql.types.ts +105 -0
  180. package/src/grpc-transport-cloudflare-http2.ts +494 -0
  181. package/src/grpc-transport-cloudflare-sockets.ts +299 -0
  182. package/src/grpc-transport-node-http2.ts +234 -0
  183. package/src/grpc-transport.ts +118 -0
  184. package/src/hpack.ts +331 -0
  185. package/src/index.ts +31 -0
  186. package/src/pagination.ts +368 -94
  187. package/src/plain-values.ts +41 -0
  188. package/src/protobuf.ts +1332 -0
  189. package/src/protocol-grpc.ts +641 -0
  190. package/src/protocol-http.ts +622 -322
  191. package/src/protocol-rest.ts +440 -227
  192. package/src/query.ts +39 -0
  193. package/src/response-validation-strict.ts +327 -0
  194. package/src/response-validation.ts +161 -0
  195. package/src/retry-after.ts +7 -13
  196. package/src/retry.ts +96 -110
  197. package/src/schema-lite-impl.ts +550 -0
  198. package/src/schema-lite-typed.ts +44 -0
  199. package/src/schema-lite.ts +44 -0
  200. package/src/schema.ts +259 -15
  201. package/src/sdk-error.ts +33 -0
  202. package/src/sensitive.ts +154 -0
  203. package/src/telemetry-debug.ts +179 -0
  204. package/src/telemetry-hooks.ts +33 -0
  205. package/src/telemetry-host-rules.ts +39 -0
  206. package/src/telemetry-lambda-context.ts +46 -0
  207. package/src/telemetry-message-context.ts +170 -0
  208. package/src/telemetry-metrics.ts +69 -0
  209. package/src/telemetry-name-template.ts +26 -0
  210. package/src/telemetry-stream.ts +100 -0
  211. package/src/telemetry-xray.ts +66 -0
  212. package/src/telemetry.ts +832 -0
  213. package/src/trait.ts +106 -67
  214. package/src/unknown-error.ts +38 -0
  215. package/src/xml.ts +346 -0
  216. package/lib/api.js.map +0 -1
  217. package/lib/category.js.map +0 -1
  218. package/lib/codegen/cli.d.ts +0 -29
  219. package/lib/codegen/cli.d.ts.map +0 -1
  220. package/lib/codegen/cli.js +0 -165
  221. package/lib/codegen/cli.js.map +0 -1
  222. package/lib/codegen/emit.d.ts +0 -129
  223. package/lib/codegen/emit.d.ts.map +0 -1
  224. package/lib/codegen/emit.js +0 -105
  225. package/lib/codegen/emit.js.map +0 -1
  226. package/lib/codegen/format.d.ts +0 -23
  227. package/lib/codegen/format.d.ts.map +0 -1
  228. package/lib/codegen/format.js +0 -28
  229. package/lib/codegen/format.js.map +0 -1
  230. package/lib/codegen/generator.d.ts +0 -334
  231. package/lib/codegen/generator.d.ts.map +0 -1
  232. package/lib/codegen/generator.js +0 -691
  233. package/lib/codegen/generator.js.map +0 -1
  234. package/lib/codegen/graph.d.ts +0 -36
  235. package/lib/codegen/graph.d.ts.map +0 -1
  236. package/lib/codegen/graph.js +0 -136
  237. package/lib/codegen/graph.js.map +0 -1
  238. package/lib/codegen/members.d.ts +0 -25
  239. package/lib/codegen/members.d.ts.map +0 -1
  240. package/lib/codegen/members.js +0 -55
  241. package/lib/codegen/members.js.map +0 -1
  242. package/lib/codegen/naming.d.ts +0 -29
  243. package/lib/codegen/naming.d.ts.map +0 -1
  244. package/lib/codegen/naming.js +0 -74
  245. package/lib/codegen/naming.js.map +0 -1
  246. package/lib/codegen/openapi-cli.d.ts +0 -38
  247. package/lib/codegen/openapi-cli.d.ts.map +0 -1
  248. package/lib/codegen/openapi-cli.js +0 -107
  249. package/lib/codegen/openapi-cli.js.map +0 -1
  250. package/lib/codegen/openapi.d.ts +0 -115
  251. package/lib/codegen/openapi.d.ts.map +0 -1
  252. package/lib/codegen/openapi.js +0 -1220
  253. package/lib/codegen/openapi.js.map +0 -1
  254. package/lib/codegen/operations.d.ts +0 -24
  255. package/lib/codegen/operations.d.ts.map +0 -1
  256. package/lib/codegen/operations.js +0 -56
  257. package/lib/codegen/operations.js.map +0 -1
  258. package/lib/codegen/pagination.d.ts +0 -39
  259. package/lib/codegen/pagination.d.ts.map +0 -1
  260. package/lib/codegen/pagination.js +0 -33
  261. package/lib/codegen/pagination.js.map +0 -1
  262. package/lib/codegen/prelude.d.ts +0 -15
  263. package/lib/codegen/prelude.d.ts.map +0 -1
  264. package/lib/codegen/prelude.js +0 -60
  265. package/lib/codegen/prelude.js.map +0 -1
  266. package/lib/error-category.js.map +0 -1
  267. package/lib/errors.js.map +0 -1
  268. package/lib/json-patch.d.ts +0 -44
  269. package/lib/json-patch.d.ts.map +0 -1
  270. package/lib/json-patch.js +0 -174
  271. package/lib/json-patch.js.map +0 -1
  272. package/lib/pagination.js.map +0 -1
  273. package/lib/protocol-http.js.map +0 -1
  274. package/lib/protocol-rest.js.map +0 -1
  275. package/lib/retry-after.js.map +0 -1
  276. package/lib/retry.js.map +0 -1
  277. package/lib/schema.js.map +0 -1
  278. package/lib/trait.js.map +0 -1
  279. package/src/codegen/cli.ts +0 -268
  280. package/src/codegen/emit.ts +0 -207
  281. package/src/codegen/format.ts +0 -47
  282. package/src/codegen/generator.ts +0 -1153
  283. package/src/codegen/graph.ts +0 -151
  284. package/src/codegen/members.ts +0 -71
  285. package/src/codegen/naming.ts +0 -86
  286. package/src/codegen/openapi-cli.ts +0 -166
  287. package/src/codegen/openapi.ts +0 -1450
  288. package/src/codegen/operations.ts +0 -76
  289. package/src/codegen/pagination.ts +0 -71
  290. package/src/codegen/prelude.ts +0 -70
  291. 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
+ };
@@ -1,4 +1,5 @@
1
- import { Effect, Predicate } from "effect";
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, const Categories extends Array<AllKeys<E>>, A2, E2, R2>(
64
- ...args: [
65
- ...Categories,
66
- f: (err: ExtractAll<E, Categories[number]>) => Effect.Effect<A2, E2, R2>,
67
- ]
68
- ) =>
69
- <A, R>(
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
- A | A2,
73
- E2 | Exclude<E, ExtractAll<E, Categories[number]>>,
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,11 @@ 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 — it is constructed at runtime by `matchError` from
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
- Duration.isDuration,
27
- );
25
+ export const DurationSchema = /*@__PURE__*/ Schema.declare<Duration.Duration>(Duration.isDuration);
28
26
 
29
27
  // ============================================================================
30
28
  // Common HTTP Status Error Classes
@@ -33,70 +31,71 @@ export const DurationSchema = Schema.declare<Duration.Duration>(
33
31
  /**
34
32
  * Unauthorized - Authentication failure (401).
35
33
  */
36
- export class Unauthorized extends Schema.TaggedErrorClass<Unauthorized>()(
37
- "Unauthorized",
38
- { message: Schema.String },
39
- ).pipe(Category.withAuthError) {}
34
+ export class Unauthorized
35
+ extends /*@__PURE__*/ Schema.TaggedError<Unauthorized>()("Unauthorized", {
36
+ message: Schema.String,
37
+ }).pipe(Category.withAuthError) {}
40
38
 
41
39
  /**
42
40
  * Forbidden - Access denied (403).
43
41
  */
44
- export class Forbidden extends Schema.TaggedErrorClass<Forbidden>()(
45
- "Forbidden",
46
- { message: Schema.String },
47
- ).pipe(Category.withAuthError) {}
42
+ export class Forbidden
43
+ extends /*@__PURE__*/ Schema.TaggedError<Forbidden>()("Forbidden", {
44
+ message: Schema.String,
45
+ }).pipe(Category.withAuthError) {}
48
46
 
49
47
  /**
50
48
  * NotFound - Resource not found (404).
51
49
  */
52
- export class NotFound extends Schema.TaggedErrorClass<NotFound>()("NotFound", {
53
- message: Schema.String,
54
- }).pipe(Category.withNotFoundError) {}
50
+ export class NotFound
51
+ extends /*@__PURE__*/ Schema.TaggedError<NotFound>()("NotFound", {
52
+ message: Schema.String,
53
+ }).pipe(Category.withNotFoundError) {}
55
54
 
56
55
  /**
57
56
  * BadRequest - Invalid request (400).
58
57
  */
59
- export class BadRequest extends Schema.TaggedErrorClass<BadRequest>()(
60
- "BadRequest",
61
- { message: Schema.String },
62
- ).pipe(Category.withBadRequestError) {}
58
+ export class BadRequest
59
+ extends /*@__PURE__*/ Schema.TaggedError<BadRequest>()("BadRequest", {
60
+ message: Schema.String,
61
+ }).pipe(Category.withBadRequestError) {}
63
62
 
64
63
  /**
65
64
  * Conflict - Resource conflict (409).
66
65
  */
67
- export class Conflict extends Schema.TaggedErrorClass<Conflict>()("Conflict", {
68
- message: Schema.String,
69
- }).pipe(Category.withConflictError) {}
66
+ export class Conflict
67
+ extends /*@__PURE__*/ Schema.TaggedError<Conflict>()("Conflict", {
68
+ message: Schema.String,
69
+ }).pipe(Category.withConflictError) {}
70
70
 
71
71
  /**
72
72
  * UnprocessableEntity - Validation error (422).
73
73
  */
74
- export class UnprocessableEntity extends Schema.TaggedErrorClass<UnprocessableEntity>()(
75
- "UnprocessableEntity",
76
- { message: Schema.String },
77
- ).pipe(Category.withBadRequestError) {}
74
+ export class UnprocessableEntity
75
+ extends /*@__PURE__*/ Schema.TaggedError<UnprocessableEntity>()("UnprocessableEntity", {
76
+ message: Schema.String,
77
+ }).pipe(Category.withBadRequestError) {}
78
78
 
79
79
  /**
80
80
  * TooManyRequests - Rate limited (429).
81
81
  */
82
- export class TooManyRequests extends Schema.TaggedErrorClass<TooManyRequests>()(
83
- "TooManyRequests",
84
- {
82
+ export class TooManyRequests
83
+ extends /*@__PURE__*/ Schema.TaggedError<TooManyRequests>()("TooManyRequests", {
85
84
  message: Schema.String,
86
- retryAfter: Schema.optional(DurationSchema),
87
- },
88
- ).pipe(
89
- Category.withThrottlingError,
90
- Category.withRetryable({ throttling: true }),
91
- ) {}
85
+ retryAfter: /*@__PURE__*/ Schema.optional(DurationSchema),
86
+ }).pipe(
87
+ Category.withThrottlingError,
88
+ /*@__PURE__*/ Category.withRetryable({ throttling: true }),
89
+ ) {}
92
90
 
93
91
  /**
94
92
  * Locked - Resource locked (423).
95
93
  */
96
- export class Locked extends Schema.TaggedErrorClass<Locked>()("Locked", {
97
- message: Schema.String,
98
- retryAfter: Schema.optional(DurationSchema),
99
- }).pipe(Category.withLockedError, Category.withRetryable()) {}
94
+ export class Locked
95
+ extends /*@__PURE__*/ Schema.TaggedError<Locked>()("Locked", {
96
+ message: Schema.String,
97
+ retryAfter: /*@__PURE__*/ Schema.optional(DurationSchema),
98
+ }).pipe(Category.withLockedError, /*@__PURE__*/ Category.withRetryable()) {}
100
99
 
101
100
  /**
102
101
  * InternalServerError - Server error.
@@ -106,55 +105,55 @@ export class Locked extends Schema.TaggedErrorClass<Locked>()("Locked", {
106
105
  * 200 envelope carrying `{ code: 10002, message: "An unknown error has
107
106
  * occurred" }` for transient backend failures on some endpoints).
108
107
  */
109
- export class InternalServerError extends Schema.TaggedErrorClass<InternalServerError>()(
110
- "InternalServerError",
111
- {
108
+ export class InternalServerError
109
+ extends /*@__PURE__*/ Schema.TaggedError<InternalServerError>()("InternalServerError", {
112
110
  message: Schema.String,
113
- code: Schema.optional(Schema.Number),
114
- retryAfter: Schema.optional(DurationSchema),
115
- },
116
- ).pipe(Category.withServerError, Category.withRetryable()) {}
111
+ code: /*@__PURE__*/ Schema.optional(Schema.Number),
112
+ retryAfter: /*@__PURE__*/ Schema.optional(DurationSchema),
113
+ }).pipe(Category.withServerError, /*@__PURE__*/ Category.withRetryable()) {}
117
114
 
118
115
  /**
119
116
  * BadGateway - Bad gateway (502).
120
117
  */
121
- export class BadGateway extends Schema.TaggedErrorClass<BadGateway>()(
122
- "BadGateway",
123
- {
118
+ export class BadGateway
119
+ extends /*@__PURE__*/ Schema.TaggedError<BadGateway>()("BadGateway", {
124
120
  message: Schema.String,
125
- retryAfter: Schema.optional(DurationSchema),
126
- },
127
- ).pipe(Category.withServerError, Category.withRetryable()) {}
121
+ retryAfter: /*@__PURE__*/ Schema.optional(DurationSchema),
122
+ }).pipe(Category.withServerError, /*@__PURE__*/ Category.withRetryable()) {}
128
123
 
129
124
  /**
130
125
  * ServiceUnavailable - Service unavailable (503).
131
126
  */
132
- export class ServiceUnavailable extends Schema.TaggedErrorClass<ServiceUnavailable>()(
133
- "ServiceUnavailable",
134
- {
127
+ export class ServiceUnavailable
128
+ extends /*@__PURE__*/ Schema.TaggedError<ServiceUnavailable>()("ServiceUnavailable", {
135
129
  message: Schema.String,
136
- retryAfter: Schema.optional(DurationSchema),
137
- },
138
- ).pipe(Category.withServerError, Category.withRetryable()) {}
130
+ retryAfter: /*@__PURE__*/ Schema.optional(DurationSchema),
131
+ }).pipe(Category.withServerError, /*@__PURE__*/ Category.withRetryable()) {}
139
132
 
140
133
  /**
141
- * GatewayTimeout - Gateway timeout (504).
134
+ * GatewayTimeout - HTTP request or gateway timeout (408, 504).
142
135
  */
143
- export class GatewayTimeout extends Schema.TaggedErrorClass<GatewayTimeout>()(
144
- "GatewayTimeout",
145
- {
136
+ export class GatewayTimeout
137
+ extends /*@__PURE__*/ Schema.TaggedError<GatewayTimeout>()("GatewayTimeout", {
146
138
  message: Schema.String,
147
- retryAfter: Schema.optional(DurationSchema),
148
- },
149
- ).pipe(Category.withServerError, Category.withRetryable()) {}
139
+ retryAfter: /*@__PURE__*/ Schema.optional(DurationSchema),
140
+ }).pipe(Category.withServerError, /*@__PURE__*/ Category.withRetryable()) {}
150
141
 
151
- /**
152
- * Configuration error - missing or invalid configuration.
153
- */
154
- export class ConfigError extends Schema.TaggedErrorClass<ConfigError>()(
155
- "ConfigError",
156
- { message: Schema.String },
157
- ).pipe(Category.withConfigurationError) {}
142
+ // Defined with the config readers (core's `base` bundle group, which every
143
+ // program loads) so they can fail with it without importing this module.
144
+ export { ConfigError } from "./config.ts";
145
+
146
+ // Defined with the label check (`protocol-http.ts`), so protocols can fail
147
+ // with it without importing every class in this module.
148
+ export { InvalidInput } from "./protocol-http.ts";
149
+
150
+ // Defined in the `base` bundle group (`sdk-error.ts`), with pagination and
151
+ // the operation runtime that fail with it.
152
+ export { SdkError } from "./sdk-error.ts";
153
+
154
+ // Defined in the `base` bundle group (`unknown-error.ts`), so AWS spreads
155
+ // them without importing the REST modules.
156
+ export { UnknownErrorFields, type UnknownErrorProps } from "./unknown-error.ts";
158
157
 
159
158
  // ============================================================================
160
159
  // Error Maps
@@ -168,6 +167,7 @@ export const HTTP_STATUS_MAP = {
168
167
  401: Unauthorized,
169
168
  403: Forbidden,
170
169
  404: NotFound,
170
+ 408: GatewayTimeout,
171
171
  409: Conflict,
172
172
  422: UnprocessableEntity,
173
173
  423: Locked,
@@ -182,7 +182,7 @@ export const HTTP_STATUS_MAP = {
182
182
  * HTTP status codes that are considered "default" errors (always present).
183
183
  * These are excluded from per-operation error types since they're handled globally.
184
184
  */
185
- export const DEFAULT_ERROR_STATUSES = new Set([401, 429, 500, 502, 503, 504]);
185
+ export const DEFAULT_ERROR_STATUSES = /*@__PURE__*/ new Set([401, 429, 500, 502, 503, 504]);
186
186
 
187
187
  /**
188
188
  * HTTP status codes whose corresponding error class in {@link HTTP_STATUS_MAP}
@@ -194,7 +194,7 @@ export const DEFAULT_ERROR_STATUSES = new Set([401, 429, 500, 502, 503, 504]);
194
194
  * classes (BadRequest/Unauthorized/etc.) would silently retain it as a
195
195
  * stale field on the instance and pollute serialized output.
196
196
  */
197
- export const RETRYABLE_HTTP_STATUSES = new Set([423, 429, 500, 502, 503, 504]);
197
+ export const RETRYABLE_HTTP_STATUSES = /*@__PURE__*/ new Set([408, 423, 429, 500, 502, 503, 504]);
198
198
 
199
199
  /**
200
200
  * All common API error classes.