@encore.dev/sdk 2.0.0-beta.7 → 2.0.0-beta.8

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 (356) hide show
  1. package/README.md +55 -0
  2. package/app_meta.ts +8 -0
  3. package/config/config.test.ts +10 -3
  4. package/config/config.ts +75 -21
  5. package/config/mod.ts +4 -3
  6. package/config/secrets.ts +72 -55
  7. package/cron/mod.ts +32 -0
  8. package/dist/app_meta.d.ts +7 -0
  9. package/dist/app_meta.d.ts.map +1 -0
  10. package/dist/app_meta.js +1 -0
  11. package/dist/app_meta.js.map +1 -1
  12. package/dist/config/config.d.ts +44 -18
  13. package/dist/config/config.d.ts.map +1 -0
  14. package/dist/config/config.js +43 -18
  15. package/dist/config/config.js.map +1 -1
  16. package/dist/config/mod.d.ts +5 -4
  17. package/dist/config/mod.d.ts.map +1 -0
  18. package/dist/config/mod.js +2 -2
  19. package/dist/config/mod.js.map +1 -1
  20. package/dist/config/secrets.d.ts +62 -39
  21. package/dist/config/secrets.d.ts.map +1 -0
  22. package/dist/config/secrets.js +21 -37
  23. package/dist/config/secrets.js.map +1 -1
  24. package/dist/config/value.d.ts +1 -0
  25. package/dist/config/value.d.ts.map +1 -0
  26. package/dist/cron/mod.d.ts +31 -0
  27. package/dist/cron/mod.d.ts.map +1 -0
  28. package/dist/cron/mod.js +23 -0
  29. package/dist/cron/mod.js.map +1 -1
  30. package/dist/gateway/errors.d.ts +101 -0
  31. package/dist/gateway/errors.d.ts.map +1 -0
  32. package/dist/gateway/errors.js +146 -0
  33. package/dist/gateway/errors.js.map +1 -0
  34. package/dist/gateway/match.d.ts +127 -0
  35. package/dist/gateway/match.d.ts.map +1 -0
  36. package/dist/gateway/match.js +206 -0
  37. package/dist/gateway/match.js.map +1 -0
  38. package/dist/gateway/middleware.d.ts +108 -0
  39. package/dist/gateway/middleware.d.ts.map +1 -0
  40. package/dist/gateway/middleware.js +9 -0
  41. package/dist/gateway/middleware.js.map +1 -0
  42. package/dist/gateway/mod.d.ts +129 -0
  43. package/dist/gateway/mod.d.ts.map +1 -0
  44. package/dist/gateway/mod.js +187 -0
  45. package/dist/gateway/mod.js.map +1 -0
  46. package/dist/gateway/request.d.ts +304 -0
  47. package/dist/gateway/request.d.ts.map +1 -0
  48. package/dist/gateway/request.js +403 -0
  49. package/dist/gateway/request.js.map +1 -0
  50. package/dist/gateway/serve.d.ts +114 -0
  51. package/dist/gateway/serve.d.ts.map +1 -0
  52. package/dist/gateway/serve.js +510 -0
  53. package/dist/gateway/serve.js.map +1 -0
  54. package/dist/internal/clients/call.d.ts +147 -0
  55. package/dist/internal/clients/call.d.ts.map +1 -0
  56. package/dist/internal/clients/call.js +274 -0
  57. package/dist/internal/clients/call.js.map +1 -0
  58. package/dist/internal/clients/grpc.d.ts +90 -0
  59. package/dist/internal/clients/grpc.d.ts.map +1 -0
  60. package/dist/internal/clients/grpc.js +355 -0
  61. package/dist/internal/clients/grpc.js.map +1 -0
  62. package/dist/internal/clients/mod.d.ts +81 -0
  63. package/dist/internal/clients/mod.d.ts.map +1 -0
  64. package/dist/internal/clients/mod.js +237 -0
  65. package/dist/internal/clients/mod.js.map +1 -0
  66. package/dist/internal/fetch.d.ts +54 -0
  67. package/dist/internal/fetch.d.ts.map +1 -0
  68. package/dist/internal/fetch.js +240 -0
  69. package/dist/internal/fetch.js.map +1 -0
  70. package/dist/internal/otel/backend.d.ts +2 -0
  71. package/dist/internal/otel/backend.d.ts.map +1 -0
  72. package/dist/internal/otel/context.d.ts +1 -0
  73. package/dist/internal/otel/context.d.ts.map +1 -0
  74. package/dist/internal/otel/context_manager.d.ts +1 -0
  75. package/dist/internal/otel/context_manager.d.ts.map +1 -0
  76. package/dist/internal/otel/encode.d.ts +2 -0
  77. package/dist/internal/otel/encode.d.ts.map +1 -0
  78. package/dist/internal/otel/index.d.ts +2 -2
  79. package/dist/internal/otel/index.d.ts.map +1 -0
  80. package/dist/internal/otel/index.js +1 -2
  81. package/dist/internal/otel/index.js.map +1 -1
  82. package/dist/internal/otel/meter.d.ts +1 -0
  83. package/dist/internal/otel/meter.d.ts.map +1 -0
  84. package/dist/internal/otel/propagator.d.ts +1 -0
  85. package/dist/internal/otel/propagator.d.ts.map +1 -0
  86. package/dist/internal/otel/provider.d.ts +1 -0
  87. package/dist/internal/otel/provider.d.ts.map +1 -0
  88. package/dist/internal/otel/span.d.ts +4 -0
  89. package/dist/internal/otel/span.d.ts.map +1 -0
  90. package/dist/internal/otel/span.js +5 -0
  91. package/dist/internal/otel/span.js.map +1 -1
  92. package/dist/internal/otel/tracer.d.ts +1 -0
  93. package/dist/internal/otel/tracer.d.ts.map +1 -0
  94. package/dist/internal/otel/tracer.js +11 -5
  95. package/dist/internal/otel/tracer.js.map +1 -1
  96. package/dist/internal/reqfields.d.ts +150 -0
  97. package/dist/internal/reqfields.d.ts.map +1 -0
  98. package/dist/internal/reqfields.js +312 -0
  99. package/dist/internal/reqfields.js.map +1 -0
  100. package/dist/internal/runtime/meter.d.ts +1 -0
  101. package/dist/internal/runtime/meter.d.ts.map +1 -0
  102. package/dist/internal/runtime/mod.d.ts +6 -0
  103. package/dist/internal/runtime/mod.d.ts.map +1 -0
  104. package/dist/internal/runtime/mod.js +32 -2
  105. package/dist/internal/runtime/mod.js.map +1 -1
  106. package/dist/internal/runtime/napi/napi.cjs +160 -16
  107. package/dist/internal/runtime/napi/napi.d.cts +474 -29
  108. package/dist/internal/runtime/shutdown.d.ts +5 -0
  109. package/dist/internal/runtime/shutdown.d.ts.map +1 -0
  110. package/dist/internal/runtime/shutdown.js +9 -1
  111. package/dist/internal/runtime/shutdown.js.map +1 -1
  112. package/dist/internal/testing/runner.d.ts +2 -0
  113. package/dist/internal/testing/runner.d.ts.map +1 -0
  114. package/dist/internal/testing/runner.js +10 -0
  115. package/dist/internal/testing/runner.js.map +1 -0
  116. package/dist/internal/testing/scope.d.ts +81 -0
  117. package/dist/internal/testing/scope.d.ts.map +1 -0
  118. package/dist/internal/testing/scope.js +237 -0
  119. package/dist/internal/testing/scope.js.map +1 -0
  120. package/dist/internal/utils/constraints.d.ts +1 -0
  121. package/dist/internal/utils/constraints.d.ts.map +1 -0
  122. package/dist/log/index.d.ts +1 -0
  123. package/dist/log/index.d.ts.map +1 -0
  124. package/dist/log/logger.d.ts +2 -0
  125. package/dist/log/logger.d.ts.map +1 -0
  126. package/dist/log/runtime.d.ts +2 -0
  127. package/dist/log/runtime.d.ts.map +1 -0
  128. package/dist/log/stderr.d.ts +1 -0
  129. package/dist/log/stderr.d.ts.map +1 -0
  130. package/dist/metrics/mod.d.ts +45 -118
  131. package/dist/metrics/mod.d.ts.map +1 -0
  132. package/dist/metrics/mod.js +44 -179
  133. package/dist/metrics/mod.js.map +1 -1
  134. package/dist/mod.d.ts +1 -2
  135. package/dist/mod.d.ts.map +1 -0
  136. package/dist/mod.js +0 -1
  137. package/dist/mod.js.map +1 -1
  138. package/dist/otel.d.ts +1 -0
  139. package/dist/otel.d.ts.map +1 -0
  140. package/dist/pubsub/encoding.d.ts +5 -0
  141. package/dist/pubsub/encoding.d.ts.map +1 -0
  142. package/dist/pubsub/encoding.js +10 -2
  143. package/dist/pubsub/encoding.js.map +1 -1
  144. package/dist/pubsub/mod.d.ts +4 -3
  145. package/dist/pubsub/mod.d.ts.map +1 -0
  146. package/dist/pubsub/mod.js +2 -2
  147. package/dist/pubsub/mod.js.map +1 -1
  148. package/dist/pubsub/refs.d.ts +8 -0
  149. package/dist/pubsub/refs.d.ts.map +1 -0
  150. package/dist/pubsub/refs.js.map +1 -1
  151. package/dist/pubsub/subscription.d.ts +104 -10
  152. package/dist/pubsub/subscription.d.ts.map +1 -0
  153. package/dist/pubsub/subscription.js +98 -16
  154. package/dist/pubsub/subscription.js.map +1 -1
  155. package/dist/pubsub/topic.d.ts +87 -23
  156. package/dist/pubsub/topic.d.ts.map +1 -0
  157. package/dist/pubsub/topic.js +143 -40
  158. package/dist/pubsub/topic.js.map +1 -1
  159. package/dist/reqctx/mod.d.ts +191 -0
  160. package/dist/reqctx/mod.d.ts.map +1 -0
  161. package/dist/reqctx/mod.js +160 -0
  162. package/dist/reqctx/mod.js.map +1 -0
  163. package/dist/services/index.d.ts +145 -4
  164. package/dist/services/index.d.ts.map +1 -0
  165. package/dist/services/index.js +155 -8
  166. package/dist/services/index.js.map +1 -1
  167. package/dist/shutdown/mod.d.ts +1 -0
  168. package/dist/shutdown/mod.d.ts.map +1 -0
  169. package/dist/storage/objects/bucket.d.ts +312 -7
  170. package/dist/storage/objects/bucket.d.ts.map +1 -0
  171. package/dist/storage/objects/bucket.js +305 -15
  172. package/dist/storage/objects/bucket.js.map +1 -1
  173. package/dist/storage/objects/error.d.ts +10 -0
  174. package/dist/storage/objects/error.d.ts.map +1 -0
  175. package/dist/storage/objects/error.js +25 -1
  176. package/dist/storage/objects/error.js.map +1 -1
  177. package/dist/storage/objects/mod.d.ts +4 -3
  178. package/dist/storage/objects/mod.d.ts.map +1 -0
  179. package/dist/storage/objects/mod.js +1 -1
  180. package/dist/storage/objects/mod.js.map +1 -1
  181. package/dist/storage/objects/refs.d.ts +20 -4
  182. package/dist/storage/objects/refs.d.ts.map +1 -0
  183. package/dist/storage/objects/refs.js +6 -0
  184. package/dist/storage/objects/refs.js.map +1 -1
  185. package/dist/storage/postgres/database.d.ts +81 -25
  186. package/dist/storage/postgres/database.d.ts.map +1 -0
  187. package/dist/storage/postgres/database.js +125 -37
  188. package/dist/storage/postgres/database.js.map +1 -1
  189. package/dist/storage/postgres/errors.d.ts +120 -0
  190. package/dist/storage/postgres/errors.d.ts.map +1 -0
  191. package/dist/storage/postgres/errors.js +162 -0
  192. package/dist/storage/postgres/errors.js.map +1 -0
  193. package/dist/storage/postgres/interval.d.ts +44 -0
  194. package/dist/storage/postgres/interval.d.ts.map +1 -0
  195. package/dist/storage/postgres/interval.js +153 -0
  196. package/dist/storage/postgres/interval.js.map +1 -0
  197. package/dist/storage/postgres/mod.d.ts +4 -1
  198. package/dist/storage/postgres/mod.d.ts.map +1 -0
  199. package/dist/storage/postgres/mod.js +2 -0
  200. package/dist/storage/postgres/mod.js.map +1 -1
  201. package/dist/storage/redis/bitfield.d.ts +67 -0
  202. package/dist/storage/redis/bitfield.d.ts.map +1 -0
  203. package/dist/storage/redis/bitfield.js +114 -0
  204. package/dist/storage/redis/bitfield.js.map +1 -0
  205. package/dist/storage/redis/command.d.ts +45 -0
  206. package/dist/storage/redis/command.d.ts.map +1 -0
  207. package/dist/storage/redis/command.js +78 -0
  208. package/dist/storage/redis/command.js.map +1 -0
  209. package/dist/storage/redis/commands.d.ts +223 -139
  210. package/dist/storage/redis/commands.d.ts.map +1 -0
  211. package/dist/storage/redis/commands.js +128 -6
  212. package/dist/storage/redis/commands.js.map +1 -1
  213. package/dist/storage/redis/database.d.ts +56 -22
  214. package/dist/storage/redis/database.d.ts.map +1 -0
  215. package/dist/storage/redis/database.js +62 -0
  216. package/dist/storage/redis/database.js.map +1 -1
  217. package/dist/storage/redis/mod.d.ts +31 -4
  218. package/dist/storage/redis/mod.d.ts.map +1 -0
  219. package/dist/storage/redis/mod.js +26 -1
  220. package/dist/storage/redis/mod.js.map +1 -1
  221. package/dist/storage/redis/native.d.ts +8 -0
  222. package/dist/storage/redis/native.d.ts.map +1 -0
  223. package/dist/storage/redis/native.js +10 -0
  224. package/dist/storage/redis/native.js.map +1 -1
  225. package/dist/storage/redis/options.d.ts +1 -0
  226. package/dist/storage/redis/options.d.ts.map +1 -0
  227. package/dist/storage/redis/pubsub.d.ts +2 -0
  228. package/dist/storage/redis/pubsub.d.ts.map +1 -0
  229. package/dist/storage/redis/reply.d.ts +34 -0
  230. package/dist/storage/redis/reply.d.ts.map +1 -0
  231. package/dist/storage/redis/reply.js +40 -1
  232. package/dist/storage/redis/reply.js.map +1 -1
  233. package/dist/storage/redis/scan.d.ts +1 -0
  234. package/dist/storage/redis/scan.d.ts.map +1 -0
  235. package/dist/storage/redis/script.d.ts +1 -0
  236. package/dist/storage/redis/script.d.ts.map +1 -0
  237. package/dist/storage/redis/target.d.ts +5 -3
  238. package/dist/storage/redis/target.d.ts.map +1 -0
  239. package/dist/storage/redis/target.js +39 -28
  240. package/dist/storage/redis/target.js.map +1 -1
  241. package/dist/storage/redis/wire.d.ts +7 -10
  242. package/dist/storage/redis/wire.d.ts.map +1 -0
  243. package/dist/storage/redis/wire.js +8 -32
  244. package/dist/storage/redis/wire.js.map +1 -1
  245. package/dist/testing/bun.d.ts +8 -0
  246. package/dist/testing/bun.d.ts.map +1 -0
  247. package/dist/testing/bun.js +13 -0
  248. package/dist/testing/bun.js.map +1 -0
  249. package/dist/testing/jest.d.ts +8 -0
  250. package/dist/testing/jest.d.ts.map +1 -0
  251. package/dist/testing/jest.js +11 -0
  252. package/dist/testing/jest.js.map +1 -0
  253. package/dist/testing/mod.d.ts +160 -0
  254. package/dist/testing/mod.d.ts.map +1 -0
  255. package/dist/testing/mod.js +227 -0
  256. package/dist/testing/mod.js.map +1 -0
  257. package/dist/testing/node.d.ts +9 -0
  258. package/dist/testing/node.d.ts.map +1 -0
  259. package/dist/testing/node.js +12 -0
  260. package/dist/testing/node.js.map +1 -0
  261. package/dist/testing/vitest.d.ts +8 -0
  262. package/dist/testing/vitest.d.ts.map +1 -0
  263. package/dist/testing/vitest.js +13 -0
  264. package/dist/testing/vitest.js.map +1 -0
  265. package/dist/tracing/mod.d.ts +19 -0
  266. package/dist/tracing/mod.d.ts.map +1 -0
  267. package/dist/tracing/mod.js +46 -0
  268. package/dist/tracing/mod.js.map +1 -0
  269. package/dist/tsconfig.build.tsbuildinfo +1 -1
  270. package/dist/types/mod.d.ts +1 -0
  271. package/dist/types/mod.d.ts.map +1 -0
  272. package/gateway/errors.ts +172 -0
  273. package/gateway/gateway.e2e.test.ts +297 -0
  274. package/gateway/gateway.test.ts +891 -0
  275. package/gateway/match.ts +239 -0
  276. package/gateway/middleware.ts +134 -0
  277. package/gateway/mod.ts +210 -0
  278. package/gateway/request.ts +578 -0
  279. package/gateway/serve.ts +738 -0
  280. package/internal/clients/call.ts +389 -0
  281. package/internal/clients/grpc.test.ts +368 -0
  282. package/internal/clients/grpc.ts +485 -0
  283. package/internal/clients/mod.test.ts +464 -0
  284. package/internal/clients/mod.ts +337 -0
  285. package/internal/fetch.ts +276 -0
  286. package/internal/otel/index.ts +1 -7
  287. package/internal/otel/span.ts +6 -0
  288. package/internal/otel/tracer.test.ts +43 -15
  289. package/internal/otel/tracer.ts +12 -5
  290. package/internal/reqfields.ts +390 -0
  291. package/internal/runtime/mod.ts +41 -2
  292. package/internal/runtime/napi/napi.cjs +160 -16
  293. package/internal/runtime/napi/napi.d.cts +474 -29
  294. package/internal/runtime/shutdown.ts +11 -1
  295. package/internal/testing/runner.ts +10 -0
  296. package/internal/testing/scope.ts +273 -0
  297. package/metrics/mod.test.ts +13 -16
  298. package/metrics/mod.ts +57 -226
  299. package/mod.ts +0 -8
  300. package/otel.test.ts +13 -6
  301. package/package.json +67 -15
  302. package/pubsub/encoding.ts +11 -1
  303. package/pubsub/mod.ts +10 -3
  304. package/pubsub/pubsub.test.ts +232 -14
  305. package/pubsub/refs.ts +16 -0
  306. package/pubsub/subscription.ts +194 -16
  307. package/pubsub/topic.test.ts +26 -0
  308. package/pubsub/topic.ts +220 -25
  309. package/reqctx/mod.ts +284 -0
  310. package/reqctx/reqctx.test.ts +193 -0
  311. package/scripts/postprocess-docs.test.ts +48 -0
  312. package/services/index.test.ts +341 -1
  313. package/services/index.ts +183 -8
  314. package/storage/objects/bucket.test.ts +573 -0
  315. package/storage/objects/bucket.ts +649 -20
  316. package/storage/objects/error.ts +28 -1
  317. package/storage/objects/mod.ts +15 -0
  318. package/storage/objects/refs.ts +51 -3
  319. package/storage/postgres/database.integration.test.ts +275 -0
  320. package/storage/postgres/database.test.ts +193 -6
  321. package/storage/postgres/database.ts +202 -55
  322. package/storage/postgres/errors.ts +186 -0
  323. package/storage/postgres/interval.test.ts +72 -0
  324. package/storage/postgres/interval.ts +170 -0
  325. package/storage/postgres/mod.ts +4 -0
  326. package/storage/redis/bitfield.ts +162 -0
  327. package/storage/redis/command.ts +84 -0
  328. package/storage/redis/commands.ts +271 -21
  329. package/storage/redis/database.ts +77 -23
  330. package/storage/redis/mod.ts +38 -2
  331. package/storage/redis/native.ts +11 -0
  332. package/storage/redis/redis.test.ts +170 -10
  333. package/storage/redis/reply.ts +76 -1
  334. package/storage/redis/runtime.test.ts +15 -0
  335. package/storage/redis/target.ts +47 -44
  336. package/storage/redis/wire.ts +8 -32
  337. package/testing/bun.ts +13 -0
  338. package/testing/jest.ts +11 -0
  339. package/testing/mod.ts +337 -0
  340. package/testing/node.ts +12 -0
  341. package/testing/vitest.ts +13 -0
  342. package/tracing/mod.ts +82 -0
  343. package/tracing/native.test.ts +170 -0
  344. package/tracing/tracing.test.ts +317 -0
  345. package/dist/internal/otel/request_context.d.ts +0 -11
  346. package/dist/internal/otel/request_context.js +0 -20
  347. package/dist/internal/otel/request_context.js.map +0 -1
  348. package/dist/internal/reqtrack/mod.d.ts +0 -6
  349. package/dist/internal/reqtrack/mod.js +0 -28
  350. package/dist/internal/reqtrack/mod.js.map +0 -1
  351. package/dist/req_meta.d.ts +0 -47
  352. package/dist/req_meta.js +0 -26
  353. package/dist/req_meta.js.map +0 -1
  354. package/internal/otel/request_context.ts +0 -32
  355. package/internal/reqtrack/mod.ts +0 -39
  356. package/req_meta.ts +0 -75
@@ -0,0 +1,129 @@
1
+ /**
2
+ * Code in an application's gateway: Middleware that authenticates,
3
+ * authorizes, limits, logs or meters the public requests the gateway serves,
4
+ * before they reach a service and after they return.
5
+ *
6
+ * A gateway with code is a service that hosts it, declared by a `gateway`
7
+ * section in the service's `encore.service.json`. The section takes what
8
+ * `encore.gateway.json` takes but the name: the gateway is named after its
9
+ * service.
10
+ *
11
+ * ```json
12
+ * {
13
+ * "name": "edge",
14
+ * "lang": "typescript",
15
+ * "run": {"cmd": ["node", "main.ts"]},
16
+ * "gateway": {"default": true}
17
+ * }
18
+ * ```
19
+ *
20
+ * The service's program serves the gateway with its Middleware, and nothing
21
+ * else. As any service, it has its own secrets, caches, databases, topics
22
+ * and configs, and may call other services.
23
+ *
24
+ * ```typescript
25
+ * import * as gateway from "@encore.dev/sdk/gateway";
26
+ * import { ErrCode, match } from "@encore.dev/sdk/gateway";
27
+ *
28
+ * class Auth implements gateway.Middleware {
29
+ * match = match.pathPrefix("/api");
30
+ *
31
+ * async onRequest(req: gateway.Request) {
32
+ * const user = await verify(req.header("authorization"), req.signal);
33
+ * if (user === undefined) {
34
+ * return gateway.reject(ErrCode.Unauthenticated, "invalid token");
35
+ * }
36
+ * req.setHeader("x-user-id", user.id);
37
+ * req.state = user;
38
+ * }
39
+ * }
40
+ *
41
+ * await gateway.serve(new Auth());
42
+ * ```
43
+ *
44
+ * ## Hooks
45
+ *
46
+ * A {@link Middleware} is called at the hooks it has, and no others:
47
+ *
48
+ * - `onRequest`, before the request is forwarded: it lets the request
49
+ * through, edited, or answers it itself;
50
+ * - `onRequestBody` and `onResponseBody`, with each chunk of the bodies, to
51
+ * observe;
52
+ * - `onResponse`, before the response is written, to edit its headers;
53
+ * - `onComplete`, once the request is done, off its path: where to log,
54
+ * meter or bill it.
55
+ *
56
+ * A Middleware is called only for the requests its `match` matches. Encore
57
+ * checks it before a request reaches JavaScript, so a request no Middleware
58
+ * is interested in costs the program nothing, and one whose only Middleware
59
+ * takes `onComplete` costs nothing on its way. A hook may read any of the
60
+ * request's headers, query parameters and cookies; each reaches JavaScript
61
+ * only as the hook reads it.
62
+ *
63
+ * Middleware see only public requests the gateway routes: calls between
64
+ * services, Pub/Sub pushes, CORS preflights and requests for paths the
65
+ * gateway does not expose are Encore's alone.
66
+ *
67
+ * ## Answering a request
68
+ *
69
+ * `onRequest` returns nothing to let the request through, or a
70
+ * {@link Rejection} to answer it, returned or thrown: {@link reject}
71
+ * answers with an Encore error, as a service does, and {@link respond} with
72
+ * any response. Any other error, or a timeout, fails the hook, and the
73
+ * Middleware's `onError` says what happens to the request: `"failClosed"`
74
+ * refuses it, `"failOpen"` lets it through.
75
+ *
76
+ * ## Request Context
77
+ *
78
+ * `onRequest` sets the request's Request Context fields
79
+ * (`@encore.dev/sdk/reqctx`) with {@link Request.set}: the Middleware after
80
+ * it, the service the request reaches and the services that one calls with
81
+ * the request's context read them, as `currentUser.get()`. No client can set
82
+ * one. Every hook runs in its request's context, so it reads them with
83
+ * `field.get()` too, or {@link Request.get}: `onComplete` as the request was
84
+ * forwarded.
85
+ *
86
+ * ## Concurrency
87
+ *
88
+ * Hooks run on the event loop. One Middleware's hooks for one request are
89
+ * called one at a time, in the order of the request's life; hooks for
90
+ * different requests run concurrently. A hook that blocks the event loop
91
+ * delays every request. `onRequest` and `onResponse` have a timeout
92
+ * (`timeoutMs`); {@link Request.signal} aborts when it passes, for the work
93
+ * the hook awaits to stop too.
94
+ *
95
+ * Should JavaScript fall far behind the requests, the gateway refuses the
96
+ * hooks it cannot queue, as failures `onError` handles, rather than holding
97
+ * requests without limit.
98
+ *
99
+ * While a hook runs, the request's trace and Request Context are the
100
+ * current ones: spans it starts are part of the trace, `field.get()` reads
101
+ * the request's fields, and calls it makes to services carry both, with the
102
+ * fields `onRequest` has set so far.
103
+ *
104
+ * @module
105
+ */
106
+ import type { Middleware } from './middleware.js';
107
+ export * as match from './match.js';
108
+ export type { Cond, Matcher } from './match.js';
109
+ export { ErrCode, GatewayError, Rejection, httpStatus, reject, respond, type Headers, } from './errors.js';
110
+ export { Hook, Request, Response } from './request.js';
111
+ export type { AttributeValue, Completed, Hooks } from './request.js';
112
+ export type { Decision, ErrorPolicy, Middleware } from './middleware.js';
113
+ /**
114
+ * Serves the gateway this service hosts, with `middleware` in order, until
115
+ * Encore shuts the process down. Call it once, at the end of the program's
116
+ * entry point.
117
+ *
118
+ * While the gateway drains on the first SIGTERM, the hooks keep running; a
119
+ * second SIGTERM hurries the shutdown. The promise resolves once the gateway
120
+ * has stopped and every hook it queued has been called; Encore exits the
121
+ * process once its shutdown completes.
122
+ *
123
+ * It rejects with a {@link GatewayError} when the process is not a gateway
124
+ * program Encore runs, as under `encore run`; a Middleware is invalid, such
125
+ * as a name repeated, or a `match` the gateway refuses, such as an invalid
126
+ * regular expression; or the gateway is already served.
127
+ */
128
+ export declare function serve(...middleware: Middleware[]): Promise<void>;
129
+ //# sourceMappingURL=mod.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../gateway/mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwGG;AAOH,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,cAAc,CAAC;AAS7C,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,YAAY,EAAC,IAAI,EAAE,OAAO,EAAC,MAAM,SAAS,CAAC;AAC3C,OAAO,EACL,OAAO,EACP,YAAY,EACZ,SAAS,EACT,UAAU,EACV,MAAM,EACN,OAAO,EACP,KAAK,OAAO,GACb,MAAM,UAAU,CAAC;AAClB,OAAO,EAAC,IAAI,EAAE,OAAO,EAAE,QAAQ,EAAC,MAAM,WAAW,CAAC;AAClD,YAAY,EAAC,cAAc,EAAE,SAAS,EAAE,KAAK,EAAC,MAAM,WAAW,CAAC;AAChE,YAAY,EAAC,QAAQ,EAAE,WAAW,EAAE,UAAU,EAAC,MAAM,cAAc,CAAC;AAkBpE;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,KAAK,CAAC,GAAG,UAAU,EAAE,UAAU,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CA2CtE"}
@@ -0,0 +1,187 @@
1
+ /**
2
+ * Code in an application's gateway: Middleware that authenticates,
3
+ * authorizes, limits, logs or meters the public requests the gateway serves,
4
+ * before they reach a service and after they return.
5
+ *
6
+ * A gateway with code is a service that hosts it, declared by a `gateway`
7
+ * section in the service's `encore.service.json`. The section takes what
8
+ * `encore.gateway.json` takes but the name: the gateway is named after its
9
+ * service.
10
+ *
11
+ * ```json
12
+ * {
13
+ * "name": "edge",
14
+ * "lang": "typescript",
15
+ * "run": {"cmd": ["node", "main.ts"]},
16
+ * "gateway": {"default": true}
17
+ * }
18
+ * ```
19
+ *
20
+ * The service's program serves the gateway with its Middleware, and nothing
21
+ * else. As any service, it has its own secrets, caches, databases, topics
22
+ * and configs, and may call other services.
23
+ *
24
+ * ```typescript
25
+ * import * as gateway from "@encore.dev/sdk/gateway";
26
+ * import { ErrCode, match } from "@encore.dev/sdk/gateway";
27
+ *
28
+ * class Auth implements gateway.Middleware {
29
+ * match = match.pathPrefix("/api");
30
+ *
31
+ * async onRequest(req: gateway.Request) {
32
+ * const user = await verify(req.header("authorization"), req.signal);
33
+ * if (user === undefined) {
34
+ * return gateway.reject(ErrCode.Unauthenticated, "invalid token");
35
+ * }
36
+ * req.setHeader("x-user-id", user.id);
37
+ * req.state = user;
38
+ * }
39
+ * }
40
+ *
41
+ * await gateway.serve(new Auth());
42
+ * ```
43
+ *
44
+ * ## Hooks
45
+ *
46
+ * A {@link Middleware} is called at the hooks it has, and no others:
47
+ *
48
+ * - `onRequest`, before the request is forwarded: it lets the request
49
+ * through, edited, or answers it itself;
50
+ * - `onRequestBody` and `onResponseBody`, with each chunk of the bodies, to
51
+ * observe;
52
+ * - `onResponse`, before the response is written, to edit its headers;
53
+ * - `onComplete`, once the request is done, off its path: where to log,
54
+ * meter or bill it.
55
+ *
56
+ * A Middleware is called only for the requests its `match` matches. Encore
57
+ * checks it before a request reaches JavaScript, so a request no Middleware
58
+ * is interested in costs the program nothing, and one whose only Middleware
59
+ * takes `onComplete` costs nothing on its way. A hook may read any of the
60
+ * request's headers, query parameters and cookies; each reaches JavaScript
61
+ * only as the hook reads it.
62
+ *
63
+ * Middleware see only public requests the gateway routes: calls between
64
+ * services, Pub/Sub pushes, CORS preflights and requests for paths the
65
+ * gateway does not expose are Encore's alone.
66
+ *
67
+ * ## Answering a request
68
+ *
69
+ * `onRequest` returns nothing to let the request through, or a
70
+ * {@link Rejection} to answer it, returned or thrown: {@link reject}
71
+ * answers with an Encore error, as a service does, and {@link respond} with
72
+ * any response. Any other error, or a timeout, fails the hook, and the
73
+ * Middleware's `onError` says what happens to the request: `"failClosed"`
74
+ * refuses it, `"failOpen"` lets it through.
75
+ *
76
+ * ## Request Context
77
+ *
78
+ * `onRequest` sets the request's Request Context fields
79
+ * (`@encore.dev/sdk/reqctx`) with {@link Request.set}: the Middleware after
80
+ * it, the service the request reaches and the services that one calls with
81
+ * the request's context read them, as `currentUser.get()`. No client can set
82
+ * one. Every hook runs in its request's context, so it reads them with
83
+ * `field.get()` too, or {@link Request.get}: `onComplete` as the request was
84
+ * forwarded.
85
+ *
86
+ * ## Concurrency
87
+ *
88
+ * Hooks run on the event loop. One Middleware's hooks for one request are
89
+ * called one at a time, in the order of the request's life; hooks for
90
+ * different requests run concurrently. A hook that blocks the event loop
91
+ * delays every request. `onRequest` and `onResponse` have a timeout
92
+ * (`timeoutMs`); {@link Request.signal} aborts when it passes, for the work
93
+ * the hook awaits to stop too.
94
+ *
95
+ * Should JavaScript fall far behind the requests, the gateway refuses the
96
+ * hooks it cannot queue, as failures `onError` handles, rather than holding
97
+ * requests without limit.
98
+ *
99
+ * While a hook runs, the request's trace and Request Context are the
100
+ * current ones: spans it starts are part of the trace, `field.get()` reads
101
+ * the request's fields, and calls it makes to services carry both, with the
102
+ * fields `onRequest` has set so far.
103
+ *
104
+ * @module
105
+ */
106
+ import { RT } from '../internal/runtime/mod.js';
107
+ import * as napi from '../internal/runtime/napi/napi.cjs';
108
+ import { forceCleanupOnRepeat, hold } from '../internal/runtime/shutdown.js';
109
+ import log from '../log/index.js';
110
+ import { GatewayError } from './errors.js';
111
+ import { check, Dispatcher, spec, } from './serve.js';
112
+ export * as match from './match.js';
113
+ export { ErrCode, GatewayError, Rejection, httpStatus, reject, respond, } from './errors.js';
114
+ export { Hook, Request, Response } from './request.js';
115
+ let served = false;
116
+ /** The runtime's gateway functions, of its gateway variant, which a gateway
117
+ * program loads. */
118
+ function native() {
119
+ const n = napi;
120
+ if (typeof n.gatewayServe !== 'function') {
121
+ throw new GatewayError('this process does not run the gateway variant of the Encore runtime: Encore runs a ' +
122
+ 'service with a "gateway" section in its encore.service.json on it, telling it where ' +
123
+ 'to serve the gateway in ENCORE_GATEWAY_LISTEN');
124
+ }
125
+ return n;
126
+ }
127
+ /**
128
+ * Serves the gateway this service hosts, with `middleware` in order, until
129
+ * Encore shuts the process down. Call it once, at the end of the program's
130
+ * entry point.
131
+ *
132
+ * While the gateway drains on the first SIGTERM, the hooks keep running; a
133
+ * second SIGTERM hurries the shutdown. The promise resolves once the gateway
134
+ * has stopped and every hook it queued has been called; Encore exits the
135
+ * process once its shutdown completes.
136
+ *
137
+ * It rejects with a {@link GatewayError} when the process is not a gateway
138
+ * program Encore runs, as under `encore run`; a Middleware is invalid, such
139
+ * as a name repeated, or a `match` the gateway refuses, such as an invalid
140
+ * regular expression; or the gateway is already served.
141
+ */
142
+ export async function serve(...middleware) {
143
+ const checked = check(middleware);
144
+ const gw = native();
145
+ if (served)
146
+ throw new GatewayError('the gateway is served once per process');
147
+ served = true;
148
+ const mws = new Map();
149
+ let listen;
150
+ const dispatcher = new Dispatcher(gw, mws);
151
+ try {
152
+ for (const reg of checked)
153
+ mws.set(gw.gatewayRegister(spec(reg)), reg);
154
+ // Encore cleans up the infrastructure the hooks use only once every hook
155
+ // the gateway queued has been called.
156
+ const held = hold();
157
+ void dispatcher.closed
158
+ .then(() => dispatcher.drained())
159
+ .finally(() => held.release());
160
+ try {
161
+ listen = gw.gatewayServe(RT, events => {
162
+ try {
163
+ dispatcher.deliver(events);
164
+ }
165
+ catch (err) {
166
+ log.error(err, "cannot hand on the gateway's middleware events");
167
+ }
168
+ });
169
+ }
170
+ catch (err) {
171
+ held.release();
172
+ throw err;
173
+ }
174
+ }
175
+ catch (err) {
176
+ throw new GatewayError(err instanceof Error ? err.message : String(err));
177
+ }
178
+ log.debug(`serving the gateway on ${listen}`);
179
+ forceCleanupOnRepeat('SIGTERM');
180
+ await dispatcher.closed;
181
+ await dispatcher.drained();
182
+ const dropped = gw.gatewayDropped();
183
+ if (dropped > 0) {
184
+ log.warn(`dropped ${dropped} body chunks and completions the middleware did not keep up with`);
185
+ }
186
+ }
187
+ //# sourceMappingURL=mod.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.js","sourceRoot":"","sources":["../../gateway/mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwGG;AAEH,OAAO,EAAC,EAAE,EAAC,MAAM,yBAAyB,CAAC;AAC3C,OAAO,KAAK,IAAI,MAAM,mCAAmC,CAAC;AAC1D,OAAO,EAAC,oBAAoB,EAAE,IAAI,EAAC,MAAM,8BAA8B,CAAC;AACxE,OAAO,GAAG,MAAM,QAAQ,CAAC;AACzB,OAAO,EAAC,YAAY,EAAC,MAAM,UAAU,CAAC;AAEtC,OAAO,EACL,KAAK,EACL,UAAU,EACV,IAAI,GAGL,MAAM,SAAS,CAAC;AAEjB,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AAEjC,OAAO,EACL,OAAO,EACP,YAAY,EACZ,SAAS,EACT,UAAU,EACV,MAAM,EACN,OAAO,GAER,MAAM,UAAU,CAAC;AAClB,OAAO,EAAC,IAAI,EAAE,OAAO,EAAE,QAAQ,EAAC,MAAM,WAAW,CAAC;AAIlD,IAAI,MAAM,GAAG,KAAK,CAAC;AAEnB;oBACoB;AACpB,SAAS,MAAM;IACb,MAAM,CAAC,GAAG,IAAyC,CAAC;IACpD,IAAI,OAAO,CAAC,CAAC,YAAY,KAAK,UAAU,EAAE,CAAC;QACzC,MAAM,IAAI,YAAY,CACpB,qFAAqF;YACnF,sFAAsF;YACtF,+CAA+C,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,IAAI,CAAC,OAAO,EAAE,CAAC;YACf,MAAM,GAAG,CAAC;QACZ,CAAC;IACH,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,MAAM,IAAI,YAAY,CAAC,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;IAC3E,CAAC;IACD,GAAG,CAAC,KAAK,CAAC,0BAA0B,MAAM,EAAE,CAAC,CAAC;IAC9C,oBAAoB,CAAC,SAAS,CAAC,CAAC;IAEhC,MAAM,UAAU,CAAC,MAAM,CAAC;IACxB,MAAM,UAAU,CAAC,OAAO,EAAE,CAAC;IAC3B,MAAM,OAAO,GAAG,EAAE,CAAC,cAAc,EAAE,CAAC;IACpC,IAAI,OAAO,GAAG,CAAC,EAAE,CAAC;QAChB,GAAG,CAAC,IAAI,CACN,WAAW,OAAO,kEAAkE,CACrF,CAAC;IACJ,CAAC;AACH,CAAC"}
@@ -0,0 +1,304 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import { AsyncLocalStorage } from 'node:async_hooks';
4
+ import type { SpanContext } from '@opentelemetry/api';
5
+ import { Field } from '../reqctx/mod.js';
6
+ /**
7
+ * A request as the native runtime keeps it: its headers, query parameters
8
+ * and cookies are handed to JavaScript only as a hook reads them.
9
+ * @internal
10
+ */
11
+ export interface NativeView {
12
+ readonly gateway: string;
13
+ readonly service: string;
14
+ readonly route: string;
15
+ readonly method: string;
16
+ readonly path: string;
17
+ readonly host?: string | null;
18
+ readonly clientIp?: string | null;
19
+ readonly traceparent?: string | null;
20
+ readonly tracestate?: string | null;
21
+ /** The Request Context fields earlier Middleware set, encoded. */
22
+ readonly context?: readonly NativeField[] | null;
23
+ pathParam(name: string): string | null | undefined;
24
+ headerValues(name: string): string[];
25
+ queryValues(name: string): string[];
26
+ cookie(name: string): string | null | undefined;
27
+ }
28
+ /** A response's headers as the native runtime keeps them. @internal */
29
+ export interface NativeHeaders {
30
+ values(name: string): string[];
31
+ }
32
+ /**
33
+ * A set of a Middleware's later hooks, which {@link Request.skip} narrows,
34
+ * combined with `|`: `Hook.Response | Hook.Complete`. `onRequest` is not
35
+ * among them: it is the hook that skips.
36
+ */
37
+ export declare const Hook: {
38
+ /** `onRequestBody`. */
39
+ readonly RequestBody: number;
40
+ /** `onResponse`. */
41
+ readonly Response: number;
42
+ /** `onResponseBody`. */
43
+ readonly ResponseBody: number;
44
+ /** `onComplete`. */
45
+ readonly Complete: number;
46
+ };
47
+ /** A value of {@link Hook}, or several combined with `|`. */
48
+ export type Hooks = number;
49
+ /** The value of an attribute of a request's span. */
50
+ export type AttributeValue = string | boolean | number;
51
+ /** An edit to headers: op 0 sets, 1 adds, 2 removes. @internal */
52
+ export interface HeaderEdit {
53
+ op: number;
54
+ name: string;
55
+ value: string;
56
+ }
57
+ /** A Request Context field, encoded, as the native runtime shows and takes
58
+ * it: a null value clears it. @internal */
59
+ export interface NativeField {
60
+ name: string;
61
+ value?: string | null;
62
+ }
63
+ /** An attribute as the native runtime takes it. @internal */
64
+ export interface NativeAttribute {
65
+ key: string;
66
+ value: AttributeValue;
67
+ integer: boolean;
68
+ }
69
+ /**
70
+ * Whether a Middleware may not edit the header `name`, lowercase: Encore's
71
+ * headers, the trace context's, those framing the message, and CORS's. The
72
+ * gateway refuses the same edits.
73
+ * @internal
74
+ */
75
+ export declare function isProtected(name: string): boolean;
76
+ /** Header values edited by a Middleware, by lowercase name, as they read
77
+ * now. */
78
+ type Overrides = Map<string, string[]>;
79
+ /** One Middleware's state for one request, shared by the Requests its hooks
80
+ * are shown. @internal */
81
+ export interface Lane {
82
+ state: unknown;
83
+ }
84
+ /** One run of a timed hook, `onRequest` or `onResponse`: expired once it
85
+ * has timed out and the gateway went on without it. @internal */
86
+ export interface HookRun {
87
+ expired: boolean;
88
+ }
89
+ /** The timed hook the code running now is part of, following its `await`s,
90
+ * timers and callbacks: so that what a timed-out hook goes on doing cannot
91
+ * change the state its later hooks are shown. @internal */
92
+ export declare const hookRuns: AsyncLocalStorage<HookRun>;
93
+ /**
94
+ * A public request the gateway serves, as a Middleware is shown it. Made by
95
+ * Encore: the same object is passed to each of one Middleware's hooks for
96
+ * one request.
97
+ *
98
+ * It reads the request's headers, query parameters and cookies as the hook
99
+ * asks for them, as the Middleware before this one left them, and with this
100
+ * one's edits. Encore's own headers, `x-encore-*`, are never shown. Edits
101
+ * are only allowed in `onRequest`; elsewhere they throw.
102
+ */
103
+ export declare class Request {
104
+ /** Identifies the request among those in flight; unique while it is. */
105
+ readonly id: number;
106
+ /** @internal */ readonly middleware: string;
107
+ /** @internal */ readonly view: NativeView | undefined;
108
+ /** @internal */ readonly lane: Lane;
109
+ /** @internal */ overrides: Overrides | undefined;
110
+ /** @internal */ deciding: boolean;
111
+ /** @internal */ edits: HeaderEdit[];
112
+ /** @internal The request's Request Context fields, encoded, with this
113
+ * Middleware's changes: changed in place, as the context its hooks run in
114
+ * holds this record. */
115
+ readonly fields: Record<string, string>;
116
+ /** @internal The changes `set` and `unset` made, for the gateway. */
117
+ fieldEdits: NativeField[];
118
+ /** @internal */ attributes: NativeAttribute[];
119
+ /** @internal */ skipped: number;
120
+ /** @internal */ hookSignal: AbortSignal;
121
+ /** @internal Made by Encore. */
122
+ constructor(middleware: string, id: number, view: NativeView | undefined, lane: Lane);
123
+ /** The HTTP method, upper case. */
124
+ get method(): string;
125
+ /** The host the client addressed, from its Host header, if any. */
126
+ get host(): string | undefined;
127
+ /** The path, as the client sent it: not percent-decoded, without the
128
+ * query. */
129
+ get path(): string;
130
+ /** The service the gateway routes the request to. */
131
+ get service(): string;
132
+ /** The route template it matched, such as `/users/{id}`. */
133
+ get route(): string;
134
+ /** The address of the client the gateway took the request from, if
135
+ * known. */
136
+ get clientIp(): string | undefined;
137
+ /**
138
+ * Aborts when the hook running now runs out of time: `onRequest` and
139
+ * `onResponse` have their Middleware's timeout, and the gateway goes on
140
+ * without them then. Pass it to what the hook awaits, as `fetch`, so the
141
+ * work stops too. It never aborts in the other hooks.
142
+ */
143
+ get signal(): AbortSignal;
144
+ /** The value of the path parameter `name` of the matched route, as it was
145
+ * sent, not percent-decoded; undefined when the route has none of that
146
+ * name. */
147
+ pathParam(name: string): string | undefined;
148
+ /** The first value of the request header `name`, as earlier Middleware
149
+ * left it; undefined when it is not sent. Any case. */
150
+ header(name: string): string | undefined;
151
+ /** Every value of the request header `name`, in order. Any case. */
152
+ headerValues(name: string): string[];
153
+ /** The first value of the query parameter `name`, percent-decoded;
154
+ * undefined when it is not sent. */
155
+ query(name: string): string | undefined;
156
+ /** Every value of the query parameter `name`, percent-decoded, in
157
+ * order. */
158
+ queryValues(name: string): string[];
159
+ /** The value of the cookie `name`, from the `Cookie` headers as the
160
+ * Middleware before this one left them; undefined when it is not sent. */
161
+ cookie(name: string): string | undefined;
162
+ /** The context of the request's span at the gateway, which the spans its
163
+ * hooks start are children of; undefined when the request is not
164
+ * traced. */
165
+ get spanContext(): SpanContext | undefined;
166
+ private mustDecide;
167
+ private edit;
168
+ /**
169
+ * Sets the request header `name` to `value`, replacing any values, for the
170
+ * Middleware after this one and the service. Only in `onRequest`. Not for
171
+ * Encore's headers (`x-encore-*`, the trace context), those framing the
172
+ * message (`content-length` and the like) and CORS's: those throw.
173
+ */
174
+ setHeader(name: string, value: string): void;
175
+ /** Adds `value` to the request header `name`, keeping its values. As
176
+ * {@link Request.setHeader}. */
177
+ addHeader(name: string, value: string): void;
178
+ /** Removes the request header `name`. As {@link Request.setHeader}. */
179
+ delHeader(name: string): void;
180
+ /**
181
+ * Sets attributes of the request's span at the gateway, as a user or
182
+ * tenant id to find its trace by. Never secrets. Only in `onRequest`. An
183
+ * integer number is recorded as an integer, another as a float.
184
+ */
185
+ setAttributes(attributes: Record<string, AttributeValue>): void;
186
+ /** Skips this Middleware's later `hooks` for this request, as an
187
+ * `onResponse` a request without a session has no use for:
188
+ * `req.skip(Hook.Response | Hook.Complete)`. Only in `onRequest`. */
189
+ skip(hooks: Hooks): void;
190
+ /**
191
+ * The Request Context field's value for the request; undefined when it is
192
+ * not set: as the Middleware before this one set it, or this one did. In
193
+ * a hook, `field.get()` reads the same, as hooks run in their request's
194
+ * context; in `onComplete`, as the request was forwarded. A value that is
195
+ * not JSON reads as not set, as it does to `field.get()`.
196
+ */
197
+ get<T>(field: Field<T>): T | undefined;
198
+ /**
199
+ * Sets the Request Context field to `value` for the request. The
200
+ * Middleware after this one, the service the request reaches, and every
201
+ * service it calls in turn with the request's context read it, with
202
+ * `field.get()`; so do the calls this hook makes from then on. It is how a
203
+ * Middleware that authenticates says who the request is for:
204
+ *
205
+ * ```ts
206
+ * export const currentUser = new Field<User>('user');
207
+ *
208
+ * class Auth implements gateway.Middleware {
209
+ * async onRequest(req: gateway.Request) {
210
+ * const user = await verify(req.header('authorization'), req.signal);
211
+ * if (user === undefined) {
212
+ * return gateway.reject(ErrCode.Unauthenticated, 'invalid token');
213
+ * }
214
+ * req.set(currentUser, user);
215
+ * }
216
+ * }
217
+ * ```
218
+ *
219
+ * Only in `onRequest`. The request takes the fields once the hook
220
+ * returns: none when it fails or answers the request.
221
+ *
222
+ * @throws TypeError - The value has no JSON form, or `field` is not a
223
+ * {@link Field}.
224
+ * @throws RangeError - The value encodes to more than a field may: 4 KiB,
225
+ * or 1 KiB for a field declared with `pubsub`; or it makes the request's
226
+ * fields encode to more than 8 KiB.
227
+ */
228
+ set<T>(field: Field<T>, value: T): void;
229
+ /** Clears the Request Context field for the request, as one the
230
+ * Middleware before this one set. As {@link Request.set} sets it. */
231
+ unset(field: Field<unknown>): void;
232
+ /**
233
+ * What this Middleware kept for the request, for its later hooks: the
234
+ * user `onRequest` found, for `onComplete` to bill. Undefined until set.
235
+ * Encore drops it when the request is done.
236
+ *
237
+ * When `onRequest` or `onResponse` fails or times out, `state` goes back
238
+ * to the object it was before the hook; what the hook changed inside that
239
+ * object stays, since Encore does not copy it. A hook that may fail after
240
+ * changing it should set a new object, as `req.state = {...req.state,
241
+ * billed: true}`, rather than change the one it has. A hook that goes on
242
+ * after it timed out may no longer set it: that throws.
243
+ */
244
+ get state(): unknown;
245
+ set state(value: unknown);
246
+ }
247
+ /**
248
+ * A response to a request the gateway served, in `onResponse`. Made by
249
+ * Encore. It reads the response's headers as the hook asks for them, as the
250
+ * Middleware after this one left them, and with this one's edits.
251
+ */
252
+ export declare class Response {
253
+ /** The HTTP status. */
254
+ readonly status: number;
255
+ /** @internal */ readonly middleware: string;
256
+ /** @internal */ readonly headers: NativeHeaders | undefined;
257
+ /** @internal */ overrides: Overrides | undefined;
258
+ /** @internal */ editing: boolean;
259
+ /** @internal */ edits: HeaderEdit[];
260
+ /** @internal Made by Encore. */
261
+ constructor(middleware: string, status: number, headers: NativeHeaders | undefined);
262
+ /** The first value of the response header `name`, if any. Any case. */
263
+ header(name: string): string | undefined;
264
+ /** Every value of the response header `name`, in order. Any case. */
265
+ headerValues(name: string): string[];
266
+ private edit;
267
+ /** Sets the response header `name` to `value`, replacing any values,
268
+ * before it is written to the client. Not for the headers
269
+ * {@link Request.setHeader} may not set. */
270
+ setHeader(name: string, value: string): void;
271
+ /** Adds `value` to the response header `name`. */
272
+ addHeader(name: string, value: string): void;
273
+ /** Removes the response header `name`. */
274
+ delHeader(name: string): void;
275
+ }
276
+ /** A request the gateway is done with, for `onComplete`. */
277
+ export interface Completed {
278
+ /** The request, with the headers as they were forwarded, and the state
279
+ * its earlier hooks kept. */
280
+ readonly request: Request;
281
+ /** The status the client was answered with; undefined when it was never
282
+ * answered, as when it disconnected first. */
283
+ readonly status: number | undefined;
284
+ /** The name of the Middleware that answered the request, or `"gateway"`
285
+ * when the gateway did, as for a route it does not expose; undefined when
286
+ * the service answered. */
287
+ readonly rejectedBy: string | undefined;
288
+ /** Why the request failed, if it did: a hook that failed, or a service
289
+ * that could not be reached. */
290
+ readonly error: string | undefined;
291
+ /** When the gateway received the request. */
292
+ readonly received: Date;
293
+ /** How long the gateway took to answer it, in milliseconds. */
294
+ readonly durationMs: number;
295
+ /** How long the service took, in milliseconds, if the request was
296
+ * forwarded. */
297
+ readonly upstreamDurationMs: number | undefined;
298
+ /** The bytes of the request's body. */
299
+ readonly requestBodySize: number;
300
+ /** The bytes of the response's body. */
301
+ readonly responseBodySize: number;
302
+ }
303
+ export {};
304
+ //# sourceMappingURL=request.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../gateway/request.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAC,iBAAiB,EAAC,MAAM,kBAAkB,CAAC;AACnD,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,oBAAoB,CAAC;AAIpD,OAAO,EAAC,KAAK,EAAC,MAAM,eAAe,CAAC;AAEpC;;;;GAIG;AACH,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAClC,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACrC,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACpC,kEAAkE;IAClE,QAAQ,CAAC,OAAO,CAAC,EAAE,SAAS,WAAW,EAAE,GAAG,IAAI,CAAC;IACjD,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC;IACnD,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACrC,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,E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