@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.
Files changed (321) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +33 -0
  3. package/lib/_bundle/aws.js +2 -0
  4. package/lib/_bundle/aws.js.map +1 -0
  5. package/lib/_bundle/base.js +5 -0
  6. package/lib/_bundle/base.js.map +1 -0
  7. package/lib/_bundle/graphql.js +3 -0
  8. package/lib/_bundle/graphql.js.map +1 -0
  9. package/lib/_bundle/grpc.js +3 -0
  10. package/lib/_bundle/grpc.js.map +1 -0
  11. package/lib/_bundle/rest.js +2 -0
  12. package/lib/_bundle/rest.js.map +1 -0
  13. package/lib/_bundle/xml.js +4 -0
  14. package/lib/_bundle/xml.js.map +1 -0
  15. package/lib/annotations.d.ts +17 -0
  16. package/lib/annotations.d.ts.map +1 -0
  17. package/lib/annotations.js +2 -0
  18. package/lib/annotations.js.map +1 -0
  19. package/lib/api.d.ts +220 -51
  20. package/lib/api.d.ts.map +1 -1
  21. package/lib/api.js +1 -178
  22. package/lib/category.d.ts +19 -16
  23. package/lib/category.d.ts.map +1 -1
  24. package/lib/category.js +1 -280
  25. package/lib/chunks.d.ts +3 -0
  26. package/lib/chunks.d.ts.map +1 -0
  27. package/lib/chunks.js +1 -0
  28. package/lib/config.d.ts +81 -0
  29. package/lib/config.d.ts.map +1 -0
  30. package/lib/config.js +1 -0
  31. package/lib/crypto.d.ts +76 -0
  32. package/lib/crypto.d.ts.map +1 -0
  33. package/lib/crypto.js +1 -0
  34. package/lib/error-category.d.ts +1 -1
  35. package/lib/error-category.d.ts.map +1 -1
  36. package/lib/error-category.js +1 -46
  37. package/lib/errors.d.ts +99 -74
  38. package/lib/errors.d.ts.map +1 -1
  39. package/lib/errors.js +1 -186
  40. package/lib/graphql.d.ts +420 -0
  41. package/lib/graphql.d.ts.map +1 -0
  42. package/lib/graphql.js +1 -0
  43. package/lib/graphql.types.d.ts +11 -0
  44. package/lib/graphql.types.d.ts.map +1 -0
  45. package/lib/graphql.types.js +1 -0
  46. package/lib/grpc-transport-cloudflare-http2.d.ts +7 -0
  47. package/lib/grpc-transport-cloudflare-http2.d.ts.map +1 -0
  48. package/lib/grpc-transport-cloudflare-http2.js +6 -0
  49. package/lib/grpc-transport-cloudflare-http2.js.map +1 -0
  50. package/lib/grpc-transport-cloudflare-sockets.d.ts +7 -0
  51. package/lib/grpc-transport-cloudflare-sockets.d.ts.map +1 -0
  52. package/lib/grpc-transport-cloudflare-sockets.js +5 -0
  53. package/lib/grpc-transport-cloudflare-sockets.js.map +1 -0
  54. package/lib/grpc-transport-node-http2.d.ts +19 -0
  55. package/lib/grpc-transport-node-http2.d.ts.map +1 -0
  56. package/lib/grpc-transport-node-http2.js +2 -0
  57. package/lib/grpc-transport-node-http2.js.map +1 -0
  58. package/lib/grpc-transport.d.ts +111 -0
  59. package/lib/grpc-transport.d.ts.map +1 -0
  60. package/lib/grpc-transport.js +1 -0
  61. package/lib/hpack.d.ts +34 -0
  62. package/lib/hpack.d.ts.map +1 -0
  63. package/lib/hpack.js +2 -0
  64. package/lib/hpack.js.map +1 -0
  65. package/lib/index.d.ts +32 -0
  66. package/lib/index.d.ts.map +1 -0
  67. package/lib/index.js +17 -0
  68. package/lib/max-message-bytes.d.ts +42 -0
  69. package/lib/max-message-bytes.d.ts.map +1 -0
  70. package/lib/max-message-bytes.js +2 -0
  71. package/lib/max-message-bytes.js.map +1 -0
  72. package/lib/ndjson.d.ts +3 -0
  73. package/lib/ndjson.d.ts.map +1 -0
  74. package/lib/ndjson.js +2 -0
  75. package/lib/ndjson.js.map +1 -0
  76. package/lib/pagination.d.ts +105 -25
  77. package/lib/pagination.d.ts.map +1 -1
  78. package/lib/pagination.js +1 -182
  79. package/lib/plain-values.d.ts +21 -0
  80. package/lib/plain-values.d.ts.map +1 -0
  81. package/lib/plain-values.js +2 -0
  82. package/lib/plain-values.js.map +1 -0
  83. package/lib/protobuf.d.ts +85 -0
  84. package/lib/protobuf.d.ts.map +1 -0
  85. package/lib/protobuf.js +1 -0
  86. package/lib/protocol-grpc.d.ts +123 -0
  87. package/lib/protocol-grpc.d.ts.map +1 -0
  88. package/lib/protocol-grpc.js +1 -0
  89. package/lib/protocol-http.d.ts +206 -25
  90. package/lib/protocol-http.d.ts.map +1 -1
  91. package/lib/protocol-http.js +1 -554
  92. package/lib/protocol-rest.d.ts +258 -64
  93. package/lib/protocol-rest.d.ts.map +1 -1
  94. package/lib/protocol-rest.js +1 -242
  95. package/lib/protocol.d.ts +113 -0
  96. package/lib/protocol.d.ts.map +1 -0
  97. package/lib/protocol.js +0 -0
  98. package/lib/query.d.ts +29 -0
  99. package/lib/query.d.ts.map +1 -0
  100. package/lib/query.js +1 -0
  101. package/lib/response-validation-strict.d.ts +48 -0
  102. package/lib/response-validation-strict.d.ts.map +1 -0
  103. package/lib/response-validation-strict.js +1 -0
  104. package/lib/response-validation.d.ts +86 -0
  105. package/lib/response-validation.d.ts.map +1 -0
  106. package/lib/response-validation.js +1 -0
  107. package/lib/retry-after.d.ts +4 -4
  108. package/lib/retry-after.d.ts.map +1 -1
  109. package/lib/retry-after.js +1 -112
  110. package/lib/retry-state.d.ts +26 -0
  111. package/lib/retry-state.d.ts.map +1 -0
  112. package/lib/retry-state.js +2 -0
  113. package/lib/retry-state.js.map +1 -0
  114. package/lib/retry.d.ts +141 -42
  115. package/lib/retry.d.ts.map +1 -1
  116. package/lib/retry.js +1 -210
  117. package/lib/schema-lite-impl.d.ts +179 -0
  118. package/lib/schema-lite-impl.d.ts.map +1 -0
  119. package/lib/schema-lite-impl.js +1 -0
  120. package/lib/schema-lite-typed.d.ts +41 -0
  121. package/lib/schema-lite-typed.d.ts.map +1 -0
  122. package/lib/schema-lite-typed.js +1 -0
  123. package/lib/schema-lite.d.ts +41 -0
  124. package/lib/schema-lite.d.ts.map +1 -0
  125. package/lib/schema-lite.js +1 -0
  126. package/lib/schema.d.ts +21 -7
  127. package/lib/schema.d.ts.map +1 -1
  128. package/lib/schema.js +1 -48
  129. package/lib/sdk-error.d.ts +31 -0
  130. package/lib/sdk-error.d.ts.map +1 -0
  131. package/lib/sdk-error.js +1 -0
  132. package/lib/send.d.ts +37 -0
  133. package/lib/send.d.ts.map +1 -0
  134. package/lib/send.js +2 -0
  135. package/lib/send.js.map +1 -0
  136. package/lib/sensitive.d.ts +28 -0
  137. package/lib/sensitive.d.ts.map +1 -0
  138. package/lib/sensitive.js +2 -0
  139. package/lib/sensitive.js.map +1 -0
  140. package/lib/telemetry-debug.d.ts +6 -0
  141. package/lib/telemetry-debug.d.ts.map +1 -0
  142. package/lib/telemetry-debug.js +2 -0
  143. package/lib/telemetry-debug.js.map +1 -0
  144. package/lib/telemetry-hooks.d.ts +32 -0
  145. package/lib/telemetry-hooks.d.ts.map +1 -0
  146. package/lib/telemetry-hooks.js +2 -0
  147. package/lib/telemetry-hooks.js.map +1 -0
  148. package/lib/telemetry-host-rules.d.ts +23 -0
  149. package/lib/telemetry-host-rules.d.ts.map +1 -0
  150. package/lib/telemetry-host-rules.js +2 -0
  151. package/lib/telemetry-host-rules.js.map +1 -0
  152. package/lib/telemetry-lambda-context.d.ts +17 -0
  153. package/lib/telemetry-lambda-context.d.ts.map +1 -0
  154. package/lib/telemetry-lambda-context.js +2 -0
  155. package/lib/telemetry-lambda-context.js.map +1 -0
  156. package/lib/telemetry-message-context.d.ts +52 -0
  157. package/lib/telemetry-message-context.d.ts.map +1 -0
  158. package/lib/telemetry-message-context.js +2 -0
  159. package/lib/telemetry-message-context.js.map +1 -0
  160. package/lib/telemetry-metrics.d.ts +6 -0
  161. package/lib/telemetry-metrics.d.ts.map +1 -0
  162. package/lib/telemetry-metrics.js +2 -0
  163. package/lib/telemetry-metrics.js.map +1 -0
  164. package/lib/telemetry-name-template.d.ts +13 -0
  165. package/lib/telemetry-name-template.d.ts.map +1 -0
  166. package/lib/telemetry-name-template.js +2 -0
  167. package/lib/telemetry-name-template.js.map +1 -0
  168. package/lib/telemetry-stream.d.ts +11 -0
  169. package/lib/telemetry-stream.d.ts.map +1 -0
  170. package/lib/telemetry-stream.js +2 -0
  171. package/lib/telemetry-stream.js.map +1 -0
  172. package/lib/telemetry-xray.d.ts +28 -0
  173. package/lib/telemetry-xray.d.ts.map +1 -0
  174. package/lib/telemetry-xray.js +2 -0
  175. package/lib/telemetry-xray.js.map +1 -0
  176. package/lib/telemetry.d.ts +317 -0
  177. package/lib/telemetry.d.ts.map +1 -0
  178. package/lib/telemetry.js +3 -0
  179. package/lib/telemetry.js.map +1 -0
  180. package/lib/token-cache.d.ts +31 -0
  181. package/lib/token-cache.d.ts.map +1 -0
  182. package/lib/token-cache.js +2 -0
  183. package/lib/token-cache.js.map +1 -0
  184. package/lib/trait.d.ts +94 -20
  185. package/lib/trait.d.ts.map +1 -1
  186. package/lib/trait.js +1 -107
  187. package/lib/unknown-error.d.ts +18 -0
  188. package/lib/unknown-error.d.ts.map +1 -0
  189. package/lib/unknown-error.js +1 -0
  190. package/lib/xml.d.ts +44 -0
  191. package/lib/xml.d.ts.map +1 -0
  192. package/lib/xml.js +1 -0
  193. package/package.json +13 -13
  194. package/src/annotations.ts +53 -0
  195. package/src/api.ts +1597 -234
  196. package/src/category.ts +97 -134
  197. package/src/chunks.ts +30 -0
  198. package/src/config.ts +261 -0
  199. package/src/crypto.ts +202 -0
  200. package/src/error-category.ts +16 -29
  201. package/src/errors.ts +99 -77
  202. package/src/graphql.ts +1817 -0
  203. package/src/graphql.types.ts +120 -0
  204. package/src/grpc-transport-cloudflare-http2.ts +569 -0
  205. package/src/grpc-transport-cloudflare-sockets.ts +368 -0
  206. package/src/grpc-transport-node-http2.ts +307 -0
  207. package/src/grpc-transport.ts +287 -0
  208. package/src/hpack.ts +331 -0
  209. package/src/index.ts +31 -0
  210. package/src/max-message-bytes.ts +58 -0
  211. package/src/ndjson.ts +55 -0
  212. package/src/pagination.ts +379 -94
  213. package/src/plain-values.ts +70 -0
  214. package/src/protobuf.ts +1367 -0
  215. package/src/protocol-grpc.ts +811 -0
  216. package/src/protocol-http.ts +1045 -371
  217. package/src/protocol-rest.ts +908 -255
  218. package/src/protocol.ts +150 -0
  219. package/src/query.ts +39 -0
  220. package/src/response-validation-strict.ts +333 -0
  221. package/src/response-validation.ts +161 -0
  222. package/src/retry-after.ts +8 -14
  223. package/src/retry-state.ts +105 -0
  224. package/src/retry.ts +260 -145
  225. package/src/schema-lite-impl.ts +621 -0
  226. package/src/schema-lite-typed.ts +44 -0
  227. package/src/schema-lite.ts +45 -0
  228. package/src/schema.ts +259 -15
  229. package/src/sdk-error.ts +37 -0
  230. package/src/send.ts +156 -0
  231. package/src/sensitive.ts +188 -0
  232. package/src/telemetry-debug.ts +179 -0
  233. package/src/telemetry-hooks.ts +33 -0
  234. package/src/telemetry-host-rules.ts +39 -0
  235. package/src/telemetry-lambda-context.ts +46 -0
  236. package/src/telemetry-message-context.ts +170 -0
  237. package/src/telemetry-metrics.ts +69 -0
  238. package/src/telemetry-name-template.ts +26 -0
  239. package/src/telemetry-stream.ts +100 -0
  240. package/src/telemetry-xray.ts +66 -0
  241. package/src/telemetry.ts +800 -0
  242. package/src/token-cache.ts +117 -0
  243. package/src/trait.ts +139 -67
  244. package/src/unknown-error.ts +38 -0
  245. package/src/xml.ts +346 -0
  246. package/lib/api.js.map +0 -1
  247. package/lib/category.js.map +0 -1
  248. package/lib/codegen/cli.d.ts +0 -29
  249. package/lib/codegen/cli.d.ts.map +0 -1
  250. package/lib/codegen/cli.js +0 -165
  251. package/lib/codegen/cli.js.map +0 -1
  252. package/lib/codegen/emit.d.ts +0 -129
  253. package/lib/codegen/emit.d.ts.map +0 -1
  254. package/lib/codegen/emit.js +0 -105
  255. package/lib/codegen/emit.js.map +0 -1
  256. package/lib/codegen/format.d.ts +0 -23
  257. package/lib/codegen/format.d.ts.map +0 -1
  258. package/lib/codegen/format.js +0 -28
  259. package/lib/codegen/format.js.map +0 -1
  260. package/lib/codegen/generator.d.ts +0 -334
  261. package/lib/codegen/generator.d.ts.map +0 -1
  262. package/lib/codegen/generator.js +0 -691
  263. package/lib/codegen/generator.js.map +0 -1
  264. package/lib/codegen/graph.d.ts +0 -36
  265. package/lib/codegen/graph.d.ts.map +0 -1
  266. package/lib/codegen/graph.js +0 -136
  267. package/lib/codegen/graph.js.map +0 -1
  268. package/lib/codegen/members.d.ts +0 -25
  269. package/lib/codegen/members.d.ts.map +0 -1
  270. package/lib/codegen/members.js +0 -55
  271. package/lib/codegen/members.js.map +0 -1
  272. package/lib/codegen/naming.d.ts +0 -29
  273. package/lib/codegen/naming.d.ts.map +0 -1
  274. package/lib/codegen/naming.js +0 -74
  275. package/lib/codegen/naming.js.map +0 -1
  276. package/lib/codegen/openapi-cli.d.ts +0 -38
  277. package/lib/codegen/openapi-cli.d.ts.map +0 -1
  278. package/lib/codegen/openapi-cli.js +0 -107
  279. package/lib/codegen/openapi-cli.js.map +0 -1
  280. package/lib/codegen/openapi.d.ts +0 -115
  281. package/lib/codegen/openapi.d.ts.map +0 -1
  282. package/lib/codegen/openapi.js +0 -1220
  283. package/lib/codegen/openapi.js.map +0 -1
  284. package/lib/codegen/operations.d.ts +0 -24
  285. package/lib/codegen/operations.d.ts.map +0 -1
  286. package/lib/codegen/operations.js +0 -56
  287. package/lib/codegen/operations.js.map +0 -1
  288. package/lib/codegen/pagination.d.ts +0 -39
  289. package/lib/codegen/pagination.d.ts.map +0 -1
  290. package/lib/codegen/pagination.js +0 -33
  291. package/lib/codegen/pagination.js.map +0 -1
  292. package/lib/codegen/prelude.d.ts +0 -15
  293. package/lib/codegen/prelude.d.ts.map +0 -1
  294. package/lib/codegen/prelude.js +0 -60
  295. package/lib/codegen/prelude.js.map +0 -1
  296. package/lib/error-category.js.map +0 -1
  297. package/lib/errors.js.map +0 -1
  298. package/lib/json-patch.d.ts +0 -44
  299. package/lib/json-patch.d.ts.map +0 -1
  300. package/lib/json-patch.js +0 -174
  301. package/lib/json-patch.js.map +0 -1
  302. package/lib/pagination.js.map +0 -1
  303. package/lib/protocol-http.js.map +0 -1
  304. package/lib/protocol-rest.js.map +0 -1
  305. package/lib/retry-after.js.map +0 -1
  306. package/lib/retry.js.map +0 -1
  307. package/lib/schema.js.map +0 -1
  308. package/lib/trait.js.map +0 -1
  309. package/src/codegen/cli.ts +0 -268
  310. package/src/codegen/emit.ts +0 -207
  311. package/src/codegen/format.ts +0 -47
  312. package/src/codegen/generator.ts +0 -1153
  313. package/src/codegen/graph.ts +0 -151
  314. package/src/codegen/members.ts +0 -71
  315. package/src/codegen/naming.ts +0 -86
  316. package/src/codegen/openapi-cli.ts +0 -166
  317. package/src/codegen/openapi.ts +0 -1450
  318. package/src/codegen/operations.ts +0 -76
  319. package/src/codegen/pagination.ts +0 -71
  320. package/src/codegen/prelude.ts +0 -70
  321. 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,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 — 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);
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 extends Schema.TaggedErrorClass<Unauthorized>()(
37
- "Unauthorized",
38
- { message: Schema.String },
39
- ).pipe(Category.withAuthError) {}
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 extends Schema.TaggedErrorClass<Forbidden>()(
45
- "Forbidden",
46
- { message: Schema.String },
47
- ).pipe(Category.withAuthError) {}
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 extends Schema.TaggedErrorClass<NotFound>()("NotFound", {
53
- message: Schema.String,
54
- }).pipe(Category.withNotFoundError) {}
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 extends Schema.TaggedErrorClass<BadRequest>()(
60
- "BadRequest",
61
- { message: Schema.String },
62
- ).pipe(Category.withBadRequestError) {}
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 extends Schema.TaggedErrorClass<Conflict>()("Conflict", {
68
- message: Schema.String,
69
- }).pipe(Category.withConflictError) {}
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 extends Schema.TaggedErrorClass<UnprocessableEntity>()(
75
- "UnprocessableEntity",
76
- { message: Schema.String },
77
- ).pipe(Category.withBadRequestError) {}
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 extends Schema.TaggedErrorClass<TooManyRequests>()(
83
- "TooManyRequests",
84
- {
98
+ export class TooManyRequests
99
+ extends /*@__PURE__*/ Schema.TaggedError<TooManyRequests>()("TooManyRequests", {
85
100
  message: Schema.String,
86
- retryAfter: Schema.optional(DurationSchema),
87
- },
88
- ).pipe(
89
- Category.withThrottlingError,
90
- Category.withRetryable({ throttling: true }),
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 extends Schema.TaggedErrorClass<Locked>()("Locked", {
97
- message: Schema.String,
98
- retryAfter: Schema.optional(DurationSchema),
99
- }).pipe(Category.withLockedError, Category.withRetryable()) {}
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 extends Schema.TaggedErrorClass<InternalServerError>()(
110
- "InternalServerError",
111
- {
126
+ export class InternalServerError
127
+ extends /*@__PURE__*/ Schema.TaggedError<InternalServerError>()("InternalServerError", {
112
128
  message: Schema.String,
113
- code: Schema.optional(Schema.Number),
114
- retryAfter: Schema.optional(DurationSchema),
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 extends Schema.TaggedErrorClass<BadGateway>()(
122
- "BadGateway",
123
- {
137
+ export class BadGateway
138
+ extends /*@__PURE__*/ Schema.TaggedError<BadGateway>()("BadGateway", {
124
139
  message: Schema.String,
125
- retryAfter: Schema.optional(DurationSchema),
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 extends Schema.TaggedErrorClass<ServiceUnavailable>()(
133
- "ServiceUnavailable",
134
- {
147
+ export class ServiceUnavailable
148
+ extends /*@__PURE__*/ Schema.TaggedError<ServiceUnavailable>()("ServiceUnavailable", {
135
149
  message: Schema.String,
136
- retryAfter: Schema.optional(DurationSchema),
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 - Gateway timeout (504).
155
+ * GatewayTimeout - HTTP request or gateway timeout (408, 504).
142
156
  */
143
- export class GatewayTimeout extends Schema.TaggedErrorClass<GatewayTimeout>()(
144
- "GatewayTimeout",
145
- {
157
+ export class GatewayTimeout
158
+ extends /*@__PURE__*/ Schema.TaggedError<GatewayTimeout>()("GatewayTimeout", {
146
159
  message: Schema.String,
147
- retryAfter: Schema.optional(DurationSchema),
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
- * 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) {}
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 honors `error.retryAfter`
190
- * with precedence over the default backoff for these.
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.