@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/hpack.ts ADDED
@@ -0,0 +1,331 @@
1
+ /**
2
+ * HPACK (RFC 7541), the header compression of HTTP/2, for a client that
3
+ * speaks HTTP/2 without a platform implementation
4
+ * (`grpc-transport-cloudflare-http2`).
5
+ *
6
+ * {@link makeDecoder} reads everything a server may send: the static and
7
+ * dynamic tables, every literal form, and Huffman-coded strings.
8
+ * {@link encode} writes the simplest valid form, each field as a literal
9
+ * with no indexing and no Huffman coding, so a connection keeps no encoder
10
+ * state.
11
+ */
12
+
13
+ /** A header block that breaks RFC 7541. HTTP/2 treats it as a connection error. */
14
+ export class HpackError extends Error {}
15
+
16
+ /** A header field: a lowercase name and its value. */
17
+ export type HeaderField = readonly [name: string, value: string];
18
+
19
+ /** RFC 7541 Appendix A. Index 1 is the first entry. */
20
+ const STATIC_TABLE: ReadonlyArray<HeaderField> = [
21
+ [":authority", ""],
22
+ [":method", "GET"],
23
+ [":method", "POST"],
24
+ [":path", "/"],
25
+ [":path", "/index.html"],
26
+ [":scheme", "http"],
27
+ [":scheme", "https"],
28
+ [":status", "200"],
29
+ [":status", "204"],
30
+ [":status", "206"],
31
+ [":status", "304"],
32
+ [":status", "400"],
33
+ [":status", "404"],
34
+ [":status", "500"],
35
+ ["accept-charset", ""],
36
+ ["accept-encoding", "gzip, deflate"],
37
+ ["accept-language", ""],
38
+ ["accept-ranges", ""],
39
+ ["accept", ""],
40
+ ["access-control-allow-origin", ""],
41
+ ["age", ""],
42
+ ["allow", ""],
43
+ ["authorization", ""],
44
+ ["cache-control", ""],
45
+ ["content-disposition", ""],
46
+ ["content-encoding", ""],
47
+ ["content-language", ""],
48
+ ["content-length", ""],
49
+ ["content-location", ""],
50
+ ["content-range", ""],
51
+ ["content-type", ""],
52
+ ["cookie", ""],
53
+ ["date", ""],
54
+ ["etag", ""],
55
+ ["expect", ""],
56
+ ["expires", ""],
57
+ ["from", ""],
58
+ ["host", ""],
59
+ ["if-match", ""],
60
+ ["if-modified-since", ""],
61
+ ["if-none-match", ""],
62
+ ["if-range", ""],
63
+ ["if-unmodified-since", ""],
64
+ ["last-modified", ""],
65
+ ["link", ""],
66
+ ["location", ""],
67
+ ["max-forwards", ""],
68
+ ["proxy-authenticate", ""],
69
+ ["proxy-authorization", ""],
70
+ ["range", ""],
71
+ ["referer", ""],
72
+ ["refresh", ""],
73
+ ["retry-after", ""],
74
+ ["server", ""],
75
+ ["set-cookie", ""],
76
+ ["strict-transport-security", ""],
77
+ ["transfer-encoding", ""],
78
+ ["user-agent", ""],
79
+ ["vary", ""],
80
+ ["via", ""],
81
+ ["www-authenticate", ""],
82
+ ];
83
+
84
+ /**
85
+ * RFC 7541 Appendix B: the bit length of each symbol's code (the 256 octets,
86
+ * then EOS). The code is canonical, so the codes themselves follow from the
87
+ * lengths: symbols ordered by length, then by value, count up from zero.
88
+ */
89
+ const HUFFMAN_BITS: ReadonlyArray<number> = [
90
+ 13, 23, 28, 28, 28, 28, 28, 28, 28, 24, 30, 28, 28, 30, 28, 28, 28, 28, 28, 28, 28, 28, 30, 28,
91
+ 28, 28, 28, 28, 28, 28, 28, 28, 6, 10, 10, 12, 13, 6, 8, 11, 10, 10, 8, 11, 8, 6, 6, 6, 5, 5, 5,
92
+ 6, 6, 6, 6, 6, 6, 6, 7, 8, 15, 6, 12, 10, 13, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
93
+ 7, 7, 7, 7, 7, 7, 8, 7, 8, 13, 19, 13, 14, 6, 15, 5, 6, 5, 6, 5, 6, 6, 6, 5, 7, 7, 6, 6, 6, 5, 6,
94
+ 7, 6, 5, 5, 6, 7, 7, 7, 7, 7, 15, 11, 14, 13, 28, 20, 22, 20, 20, 22, 22, 22, 23, 22, 23, 23, 23,
95
+ 23, 23, 24, 23, 24, 24, 22, 23, 24, 23, 23, 23, 23, 21, 22, 23, 22, 23, 23, 24, 22, 21, 20, 22,
96
+ 22, 23, 23, 21, 23, 22, 22, 24, 21, 22, 23, 23, 21, 21, 22, 21, 23, 22, 23, 23, 20, 22, 22, 22,
97
+ 23, 22, 22, 23, 26, 26, 20, 19, 22, 23, 22, 25, 26, 26, 26, 27, 27, 26, 24, 25, 19, 21, 26, 27,
98
+ 27, 26, 27, 24, 21, 21, 26, 26, 28, 27, 27, 27, 20, 24, 20, 21, 22, 21, 21, 23, 22, 22, 25, 25,
99
+ 24, 24, 26, 23, 26, 27, 26, 26, 27, 27, 27, 27, 27, 28, 27, 27, 27, 27, 27, 26, 30,
100
+ ];
101
+
102
+ const EOS = 256;
103
+
104
+ /**
105
+ * The code as a binary tree in one array. Node `n` keeps its two branches at
106
+ * `2n` and `2n + 1`: a positive entry is the next node, a negative one is a
107
+ * symbol (`-(symbol + 1)`).
108
+ */
109
+ const buildHuffmanTree = (): number[] => {
110
+ // The sort is stable, so codes of one length stay in symbol order.
111
+ const symbols = HUFFMAN_BITS.map((bits, symbol) => ({ bits, symbol })).sort(
112
+ (a, b) => a.bits - b.bits,
113
+ );
114
+ const tree = [0, 0];
115
+ let code = 0;
116
+ let previous = 0;
117
+ for (const { bits, symbol } of symbols) {
118
+ code <<= bits - previous;
119
+ previous = bits;
120
+ let node = 0;
121
+ for (let bit = bits - 1; bit >= 0; bit--) {
122
+ const slot = node * 2 + ((code >>> bit) & 1);
123
+ if (bit === 0) tree[slot] = -(symbol + 1);
124
+ else {
125
+ if (tree[slot] === 0) {
126
+ tree[slot] = tree.length / 2;
127
+ tree.push(0, 0);
128
+ }
129
+ node = tree[slot]!;
130
+ }
131
+ }
132
+ code++;
133
+ }
134
+ return tree;
135
+ };
136
+
137
+ let huffmanTree: number[] | undefined;
138
+
139
+ /** Decode a Huffman-coded string (RFC 7541 section 5.2). */
140
+ export const huffmanDecode = (bytes: Uint8Array): Uint8Array => {
141
+ const tree = (huffmanTree ??= buildHuffmanTree());
142
+ const out: number[] = [];
143
+ let node = 0;
144
+ // The bits read since the last symbol: at the end they are the padding,
145
+ // which must be fewer than 8 bits, all ones.
146
+ let pending = 0;
147
+ let ones = true;
148
+ for (const byte of bytes) {
149
+ for (let bit = 7; bit >= 0; bit--) {
150
+ const value = (byte >>> bit) & 1;
151
+ const next = tree[node * 2 + value]!;
152
+ if (next < 0) {
153
+ const symbol = -next - 1;
154
+ if (symbol === EOS) throw new HpackError("Huffman string contains EOS");
155
+ out.push(symbol);
156
+ node = 0;
157
+ pending = 0;
158
+ ones = true;
159
+ } else {
160
+ node = next;
161
+ pending++;
162
+ if (value === 0) ones = false;
163
+ }
164
+ }
165
+ }
166
+ if (pending > 7 || !ones) throw new HpackError("invalid Huffman padding");
167
+ return Uint8Array.from(out);
168
+ };
169
+
170
+ // Header octets are opaque; one character per octet keeps them all, as
171
+ // `node:http2` does.
172
+ const textOf = (bytes: Uint8Array): string => {
173
+ let out = "";
174
+ for (let i = 0; i < bytes.length; i += 8192) {
175
+ out += String.fromCharCode(...bytes.subarray(i, i + 8192));
176
+ }
177
+ return out;
178
+ };
179
+
180
+ interface Entry {
181
+ readonly name: string;
182
+ readonly value: string;
183
+ /** RFC 7541 section 4.1: the octets of the name and value, plus 32. */
184
+ readonly size: number;
185
+ }
186
+
187
+ /** Reads the header blocks of one connection, in the order they arrive. */
188
+ export interface HpackDecoder {
189
+ /** The fields of one complete header block. */
190
+ decode(block: Uint8Array): HeaderField[];
191
+ }
192
+
193
+ /**
194
+ * A decoder for one connection. `maxTableSize` is the dynamic table size
195
+ * this end allows (`SETTINGS_HEADER_TABLE_SIZE`, 4096 unless set).
196
+ */
197
+ export const makeDecoder = (maxTableSize = 4096): HpackDecoder => {
198
+ // Newest first, so dynamic index 62 is `dynamic[0]`.
199
+ const dynamic: Entry[] = [];
200
+ let size = 0;
201
+ let limit = maxTableSize;
202
+
203
+ const evict = () => {
204
+ while (size > limit) size -= dynamic.pop()!.size;
205
+ };
206
+
207
+ const lookup = (index: number): { readonly name: string; readonly value: string } => {
208
+ if (index >= 1 && index <= STATIC_TABLE.length) {
209
+ const [name, value] = STATIC_TABLE[index - 1]!;
210
+ return { name, value };
211
+ }
212
+ const entry = dynamic[index - STATIC_TABLE.length - 1];
213
+ if (entry === undefined) throw new HpackError(`no header at index ${index}`);
214
+ return entry;
215
+ };
216
+
217
+ return {
218
+ decode(block) {
219
+ const fields: HeaderField[] = [];
220
+ let at = 0;
221
+
222
+ // Every caller has checked that the octet the integer starts in is there.
223
+ const integer = (prefix: number): number => {
224
+ const max = 2 ** prefix - 1;
225
+ let value = block[at++]! & max;
226
+ if (value < max) return value;
227
+ for (let shift = 0; ; shift += 7) {
228
+ if (at >= block.length) throw new HpackError("header block ends inside an integer");
229
+ if (shift > 28) throw new HpackError("integer is too large");
230
+ const byte = block[at++]!;
231
+ value += (byte & 0x7f) * 2 ** shift;
232
+ if ((byte & 0x80) === 0) return value;
233
+ }
234
+ };
235
+
236
+ const string = (): { readonly text: string; readonly octets: number } => {
237
+ const first = block[at];
238
+ if (first === undefined) throw new HpackError("header block ends inside a string");
239
+ const huffman = (first & 0x80) !== 0;
240
+ const length = integer(7);
241
+ if (at + length > block.length) throw new HpackError("header block ends inside a string");
242
+ const raw = block.subarray(at, at + length);
243
+ at += length;
244
+ const bytes = huffman ? huffmanDecode(raw) : raw;
245
+ return { text: textOf(bytes), octets: bytes.length };
246
+ };
247
+
248
+ const literal = (prefix: number, index: boolean) => {
249
+ const nameIndex = integer(prefix);
250
+ let name: string;
251
+ let nameOctets: number;
252
+ if (nameIndex === 0) {
253
+ const read = string();
254
+ name = read.text;
255
+ nameOctets = read.octets;
256
+ } else {
257
+ name = lookup(nameIndex).name;
258
+ nameOctets = name.length;
259
+ }
260
+ const value = string();
261
+ fields.push([name, value.text]);
262
+ if (index) {
263
+ const entry = { name, value: value.text, size: nameOctets + value.octets + 32 };
264
+ dynamic.unshift(entry);
265
+ size += entry.size;
266
+ evict();
267
+ }
268
+ };
269
+
270
+ while (at < block.length) {
271
+ const first = block[at]!;
272
+ if ((first & 0x80) !== 0) {
273
+ const { name, value } = lookup(integer(7));
274
+ fields.push([name, value]);
275
+ } else if ((first & 0x40) !== 0) {
276
+ literal(6, true);
277
+ } else if ((first & 0x20) !== 0) {
278
+ // RFC 7541 section 4.2: only at the start of a block.
279
+ if (fields.length > 0) {
280
+ throw new HpackError("dynamic table size update after a header field");
281
+ }
282
+ const next = integer(5);
283
+ if (next > maxTableSize) throw new HpackError("dynamic table size exceeds the limit");
284
+ limit = next;
285
+ evict();
286
+ } else {
287
+ // Without indexing (0000) and never indexed (0001) read the same.
288
+ literal(4, false);
289
+ }
290
+ }
291
+ return fields;
292
+ },
293
+ };
294
+ };
295
+
296
+ const encoder = /*@__PURE__*/ new TextEncoder();
297
+
298
+ const pushInteger = (out: number[], value: number, prefix: number, flags: number) => {
299
+ const max = 2 ** prefix - 1;
300
+ if (value < max) {
301
+ out.push(flags | value);
302
+ return;
303
+ }
304
+ out.push(flags | max);
305
+ let rest = value - max;
306
+ while (rest >= 128) {
307
+ out.push((rest % 128) | 0x80);
308
+ rest = Math.floor(rest / 128);
309
+ }
310
+ out.push(rest);
311
+ };
312
+
313
+ const pushString = (out: number[], text: string) => {
314
+ const bytes = encoder.encode(text);
315
+ pushInteger(out, bytes.length, 7, 0);
316
+ for (const byte of bytes) out.push(byte);
317
+ };
318
+
319
+ /**
320
+ * A header block for `fields`, each a literal without indexing (RFC 7541
321
+ * section 6.2.2). Names must be lowercase, pseudo-headers first.
322
+ */
323
+ export const encode = (fields: ReadonlyArray<HeaderField>): Uint8Array => {
324
+ const out: number[] = [];
325
+ for (const [name, value] of fields) {
326
+ out.push(0);
327
+ pushString(out, name);
328
+ pushString(out, value);
329
+ }
330
+ return Uint8Array.from(out);
331
+ };
package/src/index.ts ADDED
@@ -0,0 +1,31 @@
1
+ /**
2
+ * `@distilled.cloud/core` -- the runtime shared by every distilled SDK.
3
+ *
4
+ * ```ts
5
+ * import { ResponseValidation, Retry } from "@distilled.cloud/core";
6
+ *
7
+ * program.pipe(Effect.provide(ResponseValidation.strict));
8
+ * ```
9
+ *
10
+ * Each namespace is also importable on its own subpath
11
+ * (`@distilled.cloud/core/response-validation`, ...); generated SDKs use the
12
+ * subpaths. The code generator and test helpers live in the unpublished
13
+ * `@distilled.cloud/dev` package.
14
+ */
15
+ export * as API from "./api.ts";
16
+ export * as Category from "./category.ts";
17
+ export * as Errors from "./errors.ts";
18
+ export * as GraphQL from "./graphql.ts";
19
+ export * as GrpcTransport from "./grpc-transport.ts";
20
+ export * as Pagination from "./pagination.ts";
21
+ export * as ProtocolGrpc from "./protocol-grpc.ts";
22
+ export * as ProtocolHttp from "./protocol-http.ts";
23
+ export * as ProtocolRest from "./protocol-rest.ts";
24
+ export { Query } from "./query.ts";
25
+ export * as Crypto from "./crypto.ts";
26
+ export * as ResponseValidation from "./response-validation.ts";
27
+ export * as Retry from "./retry.ts";
28
+ export * as RetryAfter from "./retry-after.ts";
29
+ export * as Sensitive from "./sensitive.ts";
30
+ export * as Telemetry from "./telemetry.ts";
31
+ export * as Trait from "./trait.ts";