@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,146 @@
1
+ /**
2
+ * The code of an Encore error, which a {@link reject}ion answers with: what
3
+ * went wrong, as Encore's clients read it, and the HTTP status the answer
4
+ * has. They are the codes a service's errors answer with.
5
+ */
6
+ export var ErrCode;
7
+ (function (ErrCode) {
8
+ /** 499. */
9
+ ErrCode["Canceled"] = "canceled";
10
+ /** 500. */
11
+ ErrCode["Unknown"] = "unknown";
12
+ /** 400. */
13
+ ErrCode["InvalidArgument"] = "invalid_argument";
14
+ /** 504. */
15
+ ErrCode["DeadlineExceeded"] = "deadline_exceeded";
16
+ /** 404. */
17
+ ErrCode["NotFound"] = "not_found";
18
+ /** 409. */
19
+ ErrCode["AlreadyExists"] = "already_exists";
20
+ /** 403. */
21
+ ErrCode["PermissionDenied"] = "permission_denied";
22
+ /** 429. */
23
+ ErrCode["ResourceExhausted"] = "resource_exhausted";
24
+ /** 400. */
25
+ ErrCode["FailedPrecondition"] = "failed_precondition";
26
+ /** 409. */
27
+ ErrCode["Aborted"] = "aborted";
28
+ /** 400. */
29
+ ErrCode["OutOfRange"] = "out_of_range";
30
+ /** 501. */
31
+ ErrCode["Unimplemented"] = "unimplemented";
32
+ /** 500. */
33
+ ErrCode["Internal"] = "internal";
34
+ /** 503. */
35
+ ErrCode["Unavailable"] = "unavailable";
36
+ /** 500. */
37
+ ErrCode["DataLoss"] = "data_loss";
38
+ /** 401. */
39
+ ErrCode["Unauthenticated"] = "unauthenticated";
40
+ })(ErrCode || (ErrCode = {}));
41
+ const STATUS = {
42
+ [ErrCode.Canceled]: 499,
43
+ [ErrCode.Unknown]: 500,
44
+ [ErrCode.InvalidArgument]: 400,
45
+ [ErrCode.DeadlineExceeded]: 504,
46
+ [ErrCode.NotFound]: 404,
47
+ [ErrCode.AlreadyExists]: 409,
48
+ [ErrCode.PermissionDenied]: 403,
49
+ [ErrCode.ResourceExhausted]: 429,
50
+ [ErrCode.FailedPrecondition]: 400,
51
+ [ErrCode.Aborted]: 409,
52
+ [ErrCode.OutOfRange]: 400,
53
+ [ErrCode.Unimplemented]: 501,
54
+ [ErrCode.Internal]: 500,
55
+ [ErrCode.Unavailable]: 503,
56
+ [ErrCode.DataLoss]: 500,
57
+ [ErrCode.Unauthenticated]: 401,
58
+ };
59
+ /** The HTTP status an answer with `code` has: 401 for
60
+ * {@link ErrCode.Unauthenticated}, 404 for {@link ErrCode.NotFound}, and so
61
+ * on. */
62
+ export function httpStatus(code) {
63
+ return STATUS[code];
64
+ }
65
+ /**
66
+ * A Middleware's answer to a request it does not let through: `onRequest`
67
+ * returns or throws one, and the gateway writes it to the client, with the
68
+ * gateway's CORS headers, rather than forwarding the request.
69
+ *
70
+ * {@link reject} answers as a service answers with an Encore error, which
71
+ * Encore's clients read as one; {@link respond} answers with any response,
72
+ * such as a redirect.
73
+ */
74
+ export class Rejection extends Error {
75
+ name = 'Rejection';
76
+ /** The HTTP status the request is answered with. */
77
+ status;
78
+ /** The Encore error code, for a {@link reject}; undefined for a
79
+ * {@link respond}. */
80
+ code;
81
+ /** What went wrong, for a {@link reject}; empty for a {@link respond}. */
82
+ errorMessage;
83
+ /** The response's headers, as name and value pairs. */
84
+ headers;
85
+ /** The response's body, for a {@link respond}. */
86
+ body;
87
+ /** @internal Made by {@link reject} and {@link respond}. */
88
+ constructor(status, code, message, headers, body) {
89
+ super(`rejected with ${status}` +
90
+ (code !== undefined ? ` ${code}: ${message}` : ''));
91
+ this.status = status;
92
+ this.code = code;
93
+ this.errorMessage = message;
94
+ this.headers = headers;
95
+ this.body = body;
96
+ }
97
+ /** Adds a header to the response, as `Retry-After` to a 429, and returns
98
+ * the rejection. */
99
+ withHeader(name, value) {
100
+ this.headers.push([name, value]);
101
+ return this;
102
+ }
103
+ }
104
+ /**
105
+ * Answers a request with an Encore error with `code` and `message`, and the
106
+ * HTTP status of the code: 401 for {@link ErrCode.Unauthenticated}, 403 for
107
+ * {@link ErrCode.PermissionDenied}, 429 for
108
+ * {@link ErrCode.ResourceExhausted}, and so on.
109
+ *
110
+ * ```typescript
111
+ * return gateway.reject(ErrCode.Unauthenticated, "missing token");
112
+ * ```
113
+ */
114
+ export function reject(code, message) {
115
+ if (!(code in REVERSE)) {
116
+ throw new TypeError(`${String(code)} is not an ErrCode`);
117
+ }
118
+ return new Rejection(STATUS[code], code, message, [], undefined);
119
+ }
120
+ const REVERSE = Object.fromEntries(Object.values(ErrCode).map(c => [c, true]));
121
+ /**
122
+ * Answers a request with `status`, `headers` and `body` as they are, as a
123
+ * redirect to sign in:
124
+ *
125
+ * ```typescript
126
+ * return gateway.respond(302, { location: "/login" });
127
+ * ```
128
+ */
129
+ export function respond(status, headers = [], body = new Uint8Array()) {
130
+ if (!Number.isInteger(status) || status < 100 || status > 599) {
131
+ throw new RangeError(`status ${status} is not an HTTP status`);
132
+ }
133
+ const pairs = Array.isArray(headers)
134
+ ? headers.map(([n, v]) => [n, v])
135
+ : Object.entries(headers);
136
+ const bytes = typeof body === 'string' ? new TextEncoder().encode(body) : body;
137
+ return new Rejection(status, undefined, '', pairs, bytes);
138
+ }
139
+ /**
140
+ * The gateway cannot be served: a Middleware is invalid, the process is not
141
+ * a gateway program Encore runs, or the gateway is already served.
142
+ */
143
+ export class GatewayError extends Error {
144
+ name = 'GatewayError';
145
+ }
146
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../gateway/errors.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,CAAN,IAAY,OAiCX;AAjCD,WAAY,OAAO;IACjB,WAAW;IACX,gCAAqB,CAAA;IACrB,WAAW;IACX,8BAAmB,CAAA;IACnB,WAAW;IACX,+CAAoC,CAAA;IACpC,WAAW;IACX,iDAAsC,CAAA;IACtC,WAAW;IACX,iCAAsB,CAAA;IACtB,WAAW;IACX,2CAAgC,CAAA;IAChC,WAAW;IACX,iDAAsC,CAAA;IACtC,WAAW;IACX,mDAAwC,CAAA;IACxC,WAAW;IACX,qDAA0C,CAAA;IAC1C,WAAW;IACX,8BAAmB,CAAA;IACnB,WAAW;IACX,sCAA2B,CAAA;IAC3B,WAAW;IACX,0CAA+B,CAAA;IAC/B,WAAW;IACX,gCAAqB,CAAA;IACrB,WAAW;IACX,sCAA2B,CAAA;IAC3B,WAAW;IACX,iCAAsB,CAAA;IACtB,WAAW;IACX,8CAAmC,CAAA;AACrC,CAAC,EAjCW,OAAO,KAAP,OAAO,QAiClB;AAED,MAAM,MAAM,GAA4B;IACtC,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,GAAG;IACvB,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,GAAG;IACtB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AC,UAAU,MAAM,wBAAwB,CAAC,CAAC;IACjE,CAAC;IACD,MAAM,KAAK,GAA4B,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC;QAC3D,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;QACjC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,OAAiC,CAAC,CAAC;IACtD,MAAM,KAAK,GACT,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IACnE,OAAO,IAAI,SAAS,CAAC,MAAM,EAAE,SAAS,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;AAC5D,CAAC;AAED;;;GAGG;AACH,MAAM,OAAO,YAAa,SAAQ,KAAK;IAC5B,IAAI,GAAG,cAAc,CAAC;CAChC"}
@@ -0,0 +1,127 @@
1
+ /**
2
+ * Which requests a Middleware is called for.
3
+ *
4
+ * A {@link Matcher} is checked by Encore before a request reaches
5
+ * JavaScript: a request no Middleware matches costs the program nothing.
6
+ * Matching sees the request as the client sent it, so the header edits of an
7
+ * earlier Middleware do not change which later ones are called, and a
8
+ * Middleware need not read what it matches on.
9
+ *
10
+ * Matchers combine with `.and()`, `.or()` and `.not()`, or {@link allOf},
11
+ * {@link anyOf} and {@link not}:
12
+ *
13
+ * ```typescript
14
+ * import { match } from "@encore.dev/sdk/gateway";
15
+ *
16
+ * const admin = match.pathPrefix("/admin").and(match.method("OPTIONS").not());
17
+ * const bearer = match.header("authorization", match.prefix("Bearer "));
18
+ * const either = match.service("billing", "orders").or(match.route("GET", "/users/{id}"));
19
+ * ```
20
+ *
21
+ * @module
22
+ */
23
+ /** A matcher or condition as the gateway core's JSON spells it. */
24
+ type Expr = Record<string, unknown>;
25
+ /**
26
+ * A condition on a request: on its service, route, method, path, headers,
27
+ * query parameters or cookies. Made by this module's functions.
28
+ */
29
+ export declare class Matcher {
30
+ /** @internal */
31
+ readonly expr: Expr;
32
+ /** @internal */
33
+ constructor(expr: Expr);
34
+ /** Matches the requests both match. */
35
+ and(other: Matcher): Matcher;
36
+ /** Matches the requests either matches. */
37
+ or(other: Matcher): Matcher;
38
+ /** Matches the requests this does not. */
39
+ not(): Matcher;
40
+ /** The matcher as the gateway core's JSON spells it. */
41
+ toJSON(): Expr;
42
+ }
43
+ /**
44
+ * What a header, query parameter or cookie must be, for {@link header},
45
+ * {@link query} and {@link cookie}. One sent more than once satisfies a
46
+ * condition, other than {@link absent}, when any of its values does. Made by
47
+ * this module's functions.
48
+ */
49
+ export declare class Cond {
50
+ /** @internal */
51
+ readonly expr: Expr;
52
+ /** @internal */
53
+ constructor(expr: Expr);
54
+ /** The condition as the gateway core's JSON spells it. */
55
+ toJSON(): Expr;
56
+ }
57
+ /** Matches the requests every one of `ms` matches: every request when there
58
+ * are none. */
59
+ export declare function allOf(...ms: Matcher[]): Matcher;
60
+ /** Matches the requests any of `ms` matches: none when there are none. */
61
+ export declare function anyOf(...ms: Matcher[]): Matcher;
62
+ /** Matches the requests `m` does not, as `m.not()`. */
63
+ export declare function not(m: Matcher): Matcher;
64
+ /** Matches the requests the gateway routes to any of `services`. */
65
+ export declare function service(...services: string[]): Matcher;
66
+ /**
67
+ * Matches the requests routed by the gateway's route with `template`, such
68
+ * as `/users/{id}`, answering `method`, or any method when it is undefined.
69
+ * The names of the template's parameters do not matter, as they do not to
70
+ * the router: `/users/{userId}` names the same route.
71
+ */
72
+ export declare function route(method: string | undefined, template: string): Matcher;
73
+ /** Matches the requests with any of `methods`, such as `"POST"`. */
74
+ export declare function method(...methods: string[]): Matcher;
75
+ /**
76
+ * Matches the requests for exactly `path`, as the client sent it, not
77
+ * percent-decoded. A trailing slash does not matter, as it does not to the
78
+ * router.
79
+ */
80
+ export declare function path(path: string): Matcher;
81
+ /**
82
+ * Matches the requests whose path starts with `prefix`, by whole segments:
83
+ * `/admin` matches `/admin` and `/admin/users`, not `/administrators`.
84
+ */
85
+ export declare function pathPrefix(prefix: string): Matcher;
86
+ /**
87
+ * Matches the requests whose path the glob matches: a `*` segment matches
88
+ * any one segment, and a `**` segment any number of them, none included.
89
+ * `/users/*` matches `/users/1`, and `/users/**` matches `/users` and
90
+ * `/users/1/posts/2`.
91
+ */
92
+ export declare function pathGlob(glob: string): Matcher;
93
+ /**
94
+ * Matches the requests whose header `name` satisfies `cond`, or is sent,
95
+ * without one. Header names are case-insensitive; values are compared as
96
+ * sent.
97
+ */
98
+ export declare function header(name: string, cond?: Cond): Matcher;
99
+ /**
100
+ * Matches the requests whose query parameter `name` satisfies `cond`, or is
101
+ * sent, without one. Names are case-sensitive; values are percent-decoded
102
+ * before they are compared.
103
+ */
104
+ export declare function query(name: string, cond?: Cond): Matcher;
105
+ /**
106
+ * Matches the requests whose cookie `name` satisfies `cond`, or is sent,
107
+ * without one. Names are case-sensitive; values are compared as sent.
108
+ */
109
+ export declare function cookie(name: string, cond?: Cond): Matcher;
110
+ /** Holds when the field is sent. */
111
+ export declare function present(): Cond;
112
+ /** Holds when the field is not sent. */
113
+ export declare function absent(): Cond;
114
+ /** Holds when the field's value is `value`. */
115
+ export declare function equals(value: string): Cond;
116
+ /** Holds when the field's value starts with `prefix`, as `"Bearer "`. */
117
+ export declare function prefix(prefix: string): Cond;
118
+ /** Holds when the field's value is one of `values`. */
119
+ export declare function oneOf(...values: string[]): Cond;
120
+ /**
121
+ * Holds when the regular expression matches the field's whole value. In RE2
122
+ * syntax, not JavaScript's: Encore checks it before a request reaches
123
+ * JavaScript. An invalid one makes `serve` fail.
124
+ */
125
+ export declare function regex(pattern: string): Cond;
126
+ export {};
127
+ //# sourceMappingURL=match.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"match.d.ts","sourceRoot":"","sources":["../../gateway/match.ts"],"names":[],"mappings":"AAAA;;;;;;;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GAAG,OAAO,CAE1C;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAElD;AAED;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAE9C;AAUD;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,IAAI,GAAG,OAAO,CAEzD;AAED;;;;GAIG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,IAAI,GAAG,OAAO,CAExD;AAED;;;GAGG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,IAAI,GAAG,OAAO,CAEzD;AAED,oCAAoC;AACpC,wBAAgB,OAAO,IAAI,IAAI,CAE9B;AAED,wCAAwC;AACxC,wBAAgB,MAAM,IAAI,IAAI,CAE7B;AAED,+CAA+C;AAC/C,wBAAgB,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAE1C;AAED,yEAAyE;AACzE,wBAAgB,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAE3C;AAED,uDAAuD;AACvD,wBAAgB,KAAK,CAAC,GAAG,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,CAE/C;AAED;;;;GAIG;AACH,wBAAgB,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAE3C"}
@@ -0,0 +1,206 @@
1
+ /**
2
+ * Which requests a Middleware is called for.
3
+ *
4
+ * A {@link Matcher} is checked by Encore before a request reaches
5
+ * JavaScript: a request no Middleware matches costs the program nothing.
6
+ * Matching sees the request as the client sent it, so the header edits of an
7
+ * earlier Middleware do not change which later ones are called, and a
8
+ * Middleware need not read what it matches on.
9
+ *
10
+ * Matchers combine with `.and()`, `.or()` and `.not()`, or {@link allOf},
11
+ * {@link anyOf} and {@link not}:
12
+ *
13
+ * ```typescript
14
+ * import { match } from "@encore.dev/sdk/gateway";
15
+ *
16
+ * const admin = match.pathPrefix("/admin").and(match.method("OPTIONS").not());
17
+ * const bearer = match.header("authorization", match.prefix("Bearer "));
18
+ * const either = match.service("billing", "orders").or(match.route("GET", "/users/{id}"));
19
+ * ```
20
+ *
21
+ * @module
22
+ */
23
+ /**
24
+ * A condition on a request: on its service, route, method, path, headers,
25
+ * query parameters or cookies. Made by this module's functions.
26
+ */
27
+ export class Matcher {
28
+ /** @internal */
29
+ expr;
30
+ /** @internal */
31
+ constructor(expr) {
32
+ this.expr = expr;
33
+ }
34
+ /** Matches the requests both match. */
35
+ and(other) {
36
+ return allOf(this, other);
37
+ }
38
+ /** Matches the requests either matches. */
39
+ or(other) {
40
+ return anyOf(this, other);
41
+ }
42
+ /** Matches the requests this does not. */
43
+ not() {
44
+ return not(this);
45
+ }
46
+ /** The matcher as the gateway core's JSON spells it. */
47
+ toJSON() {
48
+ return this.expr;
49
+ }
50
+ }
51
+ /**
52
+ * What a header, query parameter or cookie must be, for {@link header},
53
+ * {@link query} and {@link cookie}. One sent more than once satisfies a
54
+ * condition, other than {@link absent}, when any of its values does. Made by
55
+ * this module's functions.
56
+ */
57
+ export class Cond {
58
+ /** @internal */
59
+ expr;
60
+ /** @internal */
61
+ constructor(expr) {
62
+ this.expr = expr;
63
+ }
64
+ /** The condition as the gateway core's JSON spells it. */
65
+ toJSON() {
66
+ return this.expr;
67
+ }
68
+ }
69
+ function matchers(what, ms) {
70
+ return ms.map(m => {
71
+ if (!(m instanceof Matcher)) {
72
+ throw new TypeError(`match.${what} takes Matchers, not ${describe(m)}`);
73
+ }
74
+ return m.expr;
75
+ });
76
+ }
77
+ function strings(what, values) {
78
+ for (const v of values) {
79
+ if (typeof v !== 'string') {
80
+ throw new TypeError(`match.${what} takes strings, not ${describe(v)}`);
81
+ }
82
+ }
83
+ return [...values];
84
+ }
85
+ function describe(v) {
86
+ return v === null
87
+ ? 'null'
88
+ : typeof v === 'object'
89
+ ? v.constructor.name
90
+ : typeof v;
91
+ }
92
+ /** Matches the requests every one of `ms` matches: every request when there
93
+ * are none. */
94
+ export function allOf(...ms) {
95
+ return new Matcher({ all: matchers('allOf', ms) });
96
+ }
97
+ /** Matches the requests any of `ms` matches: none when there are none. */
98
+ export function anyOf(...ms) {
99
+ return new Matcher({ anyOf: matchers('anyOf', ms) });
100
+ }
101
+ /** Matches the requests `m` does not, as `m.not()`. */
102
+ export function not(m) {
103
+ return new Matcher({ not: matchers('not', [m])[0] });
104
+ }
105
+ /** Matches the requests the gateway routes to any of `services`. */
106
+ export function service(...services) {
107
+ return new Matcher({ service: strings('service', services) });
108
+ }
109
+ /**
110
+ * Matches the requests routed by the gateway's route with `template`, such
111
+ * as `/users/{id}`, answering `method`, or any method when it is undefined.
112
+ * The names of the template's parameters do not matter, as they do not to
113
+ * the router: `/users/{userId}` names the same route.
114
+ */
115
+ export function route(method, template) {
116
+ return new Matcher({
117
+ route: method === undefined ? { template } : { method, template },
118
+ });
119
+ }
120
+ /** Matches the requests with any of `methods`, such as `"POST"`. */
121
+ export function method(...methods) {
122
+ return new Matcher({ method: strings('method', methods) });
123
+ }
124
+ /**
125
+ * Matches the requests for exactly `path`, as the client sent it, not
126
+ * percent-decoded. A trailing slash does not matter, as it does not to the
127
+ * router.
128
+ */
129
+ export function path(path) {
130
+ return new Matcher({ path: { exact: path } });
131
+ }
132
+ /**
133
+ * Matches the requests whose path starts with `prefix`, by whole segments:
134
+ * `/admin` matches `/admin` and `/admin/users`, not `/administrators`.
135
+ */
136
+ export function pathPrefix(prefix) {
137
+ return new Matcher({ path: { prefix } });
138
+ }
139
+ /**
140
+ * Matches the requests whose path the glob matches: a `*` segment matches
141
+ * any one segment, and a `**` segment any number of them, none included.
142
+ * `/users/*` matches `/users/1`, and `/users/**` matches `/users` and
143
+ * `/users/1/posts/2`.
144
+ */
145
+ export function pathGlob(glob) {
146
+ return new Matcher({ path: { glob } });
147
+ }
148
+ function field(kind, name, cond) {
149
+ const c = cond ?? present();
150
+ if (!(c instanceof Cond)) {
151
+ throw new TypeError(`match.${kind} takes a Cond, not ${describe(c)}`);
152
+ }
153
+ return new Matcher({ [kind]: { name, cond: c.expr } });
154
+ }
155
+ /**
156
+ * Matches the requests whose header `name` satisfies `cond`, or is sent,
157
+ * without one. Header names are case-insensitive; values are compared as
158
+ * sent.
159
+ */
160
+ export function header(name, cond) {
161
+ return field('header', name, cond);
162
+ }
163
+ /**
164
+ * Matches the requests whose query parameter `name` satisfies `cond`, or is
165
+ * sent, without one. Names are case-sensitive; values are percent-decoded
166
+ * before they are compared.
167
+ */
168
+ export function query(name, cond) {
169
+ return field('query', name, cond);
170
+ }
171
+ /**
172
+ * Matches the requests whose cookie `name` satisfies `cond`, or is sent,
173
+ * without one. Names are case-sensitive; values are compared as sent.
174
+ */
175
+ export function cookie(name, cond) {
176
+ return field('cookie', name, cond);
177
+ }
178
+ /** Holds when the field is sent. */
179
+ export function present() {
180
+ return new Cond({ present: true });
181
+ }
182
+ /** Holds when the field is not sent. */
183
+ export function absent() {
184
+ return new Cond({ absent: true });
185
+ }
186
+ /** Holds when the field's value is `value`. */
187
+ export function equals(value) {
188
+ return new Cond({ equals: value });
189
+ }
190
+ /** Holds when the field's value starts with `prefix`, as `"Bearer "`. */
191
+ export function prefix(prefix) {
192
+ return new Cond({ prefix });
193
+ }
194
+ /** Holds when the field's value is one of `values`. */
195
+ export function oneOf(...values) {
196
+ return new Cond({ oneOf: strings('oneOf', values) });
197
+ }
198
+ /**
199
+ * Holds when the regular expression matches the field's whole value. In RE2
200
+ * syntax, not JavaScript's: Encore checks it before a request reaches
201
+ * JavaScript. An invalid one makes `serve` fail.
202
+ */
203
+ export function regex(pattern) {
204
+ return new Cond({ regex: pattern });
205
+ }
206
+ //# sourceMappingURL=match.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"match.js","sourceRoot":"","sources":["../../gateway/match.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG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3
+ import type { Completed, Request, Response } from './request.js';
4
+ /**
5
+ * What a Middleware's `onRequest` failing does to the request: throwing an
6
+ * error that is not a {@link Rejection}, or timing out.
7
+ *
8
+ * - `"failClosed"`, the default, refuses the request: 503 Service
9
+ * Unavailable when the hook timed out, or the gateway could not hand it to
10
+ * JavaScript, which a client may retry, and 500 Internal Server Error
11
+ * otherwise. What a Middleware that authenticates needs, so that its
12
+ * failure lets no one in.
13
+ * - `"failOpen"` lets the request through, as if the hook had, untouched.
14
+ * For a Middleware the request does not depend on, such as a rate limiter
15
+ * whose store is down.
16
+ */
17
+ export type ErrorPolicy = 'failClosed' | 'failOpen';
18
+ /** What `onRequest` returns: nothing to let the request through, or a
19
+ * {@link Rejection} to answer it. */
20
+ export type Decision = void | undefined | Rejection;
21
+ /**
22
+ * Code the gateway runs on the public requests it serves: before they reach
23
+ * a service, as they return, and once they are done.
24
+ *
25
+ * A Middleware is an object, or an instance of a class, with the hooks it is
26
+ * called at; the others cost nothing. Each is called only for the requests
27
+ * `match` matches. Middleware are called in the order `serve` is given them:
28
+ * each `onRequest` sees the headers the ones before it left, and the
29
+ * `onResponse` hooks run in reverse, only for the Middleware that let the
30
+ * request through.
31
+ *
32
+ * ```typescript
33
+ * class RateLimit implements gateway.Middleware {
34
+ * constructor(readonly name: string, readonly match: gateway.Matcher) {}
35
+ * async onRequest(req: gateway.Request) { ... }
36
+ * }
37
+ * ```
38
+ *
39
+ * `serve` checks every Middleware before serving: that it has a hook, that
40
+ * every other property whose name starts with `on` and a capital is
41
+ * `onError` (no hook is left uncalled for a typo), that its settings are
42
+ * valid, and that the names are unique.
43
+ */
44
+ export interface Middleware {
45
+ /**
46
+ * Names the Middleware in logs and traces, and as what answered a request
47
+ * it refused ({@link Completed.rejectedBy}). Kebab-case, and unique in the
48
+ * gateway. When unset, its class's name in kebab-case: `RateLimit` is
49
+ * `rate-limit`; a plain object needs one.
50
+ */
51
+ readonly name?: string;
52
+ /** The requests it is called for; every request when unset. */
53
+ readonly match?: Matcher;
54
+ /**
55
+ * How long `onRequest` and `onResponse` may take, in milliseconds,
56
+ * counted from when the gateway handed the hook to JavaScript; 5000 when
57
+ * unset. The hook has timed out then: its {@link Request.signal} aborts,
58
+ * the gateway goes on without it, and `onError` decides, a Middleware
59
+ * failing closed answering 503, as the gateway answers its own timeout. A
60
+ * hook still waiting to run then is not run. For the later hooks of a hook
61
+ * that failed or timed out, its header edits and Request Context fields
62
+ * are dropped, and `state` is
63
+ * the object it was before the hook: see {@link Request.state}.
64
+ */
65
+ readonly timeoutMs?: number;
66
+ /** What `onRequest` failing does to the request; `"failClosed"` when
67
+ * unset. */
68
+ readonly onError?: ErrorPolicy;
69
+ /**
70
+ * Called before the request is forwarded. It may edit the request's
71
+ * headers ({@link Request.setHeader}), set its Request Context fields
72
+ * ({@link Request.set}) and attributes of its span
73
+ * ({@link Request.setAttributes}), and narrow its later hooks
74
+ * ({@link Request.skip}).
75
+ *
76
+ * Returning nothing lets the request through. Returning or throwing a
77
+ * {@link Rejection} ({@link reject}, {@link respond}) answers it instead:
78
+ * no later Middleware is called, and the request is not forwarded. Any
79
+ * other error fails the hook, and `onError` decides.
80
+ */
81
+ onRequest?(req: Request): Decision | Promise<Decision>;
82
+ /**
83
+ * Shown each chunk of the request's body as it is forwarded, `end` set
84
+ * with the last. It observes: the body is forwarded whatever it does.
85
+ * Under heavy load, chunks it cannot keep up with are dropped, and logged.
86
+ */
87
+ onRequestBody?(req: Request, chunk: Uint8Array, end: boolean): void | Promise<void>;
88
+ /** Called with the response's status and headers before they are
89
+ * written. It may edit the response's headers
90
+ * ({@link Response.setHeader}). */
91
+ onResponse?(req: Request, resp: Response): void | Promise<void>;
92
+ /** Shown each chunk of the response's body, as `onRequestBody` is the
93
+ * request's. */
94
+ onResponseBody?(req: Request, chunk: Uint8Array, end: boolean): void | Promise<void>;
95
+ /**
96
+ * Called once the request is done, off its path: the client has its
97
+ * response, or never will. Called for every request the Middleware
98
+ * matches, also one another Middleware answered, so it is where to log,
99
+ * meter or bill requests. Under heavy load, completions it cannot keep up
100
+ * with are dropped, and logged.
101
+ */
102
+ onComplete?(done: Completed): void | Promise<void>;
103
+ }
104
+ /** The hooks a Middleware may have, as they are named. @internal */
105
+ export declare const HOOKS: readonly ["onRequest", "onRequestBody", "onResponse", "onResponseBody", "onComplete"];
106
+ /** A hook's name. @internal */
107
+ export type HookName = (typeof HOOKS)[number];
108
+ //# sourceMappingURL=middleware.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../../gateway/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,SAAS,EAAC,MAAM,UAAU,CAAC;AACxC,OAAO,KAAK,EAAC,OAAO,EAAC,MAAM,SAAS,CAAC;AACrC,OAAO,KAAK,EAAC,SAAS,EAAE,OAAO,EAAE,QAAQ,EAAC,MAAM,WAAW,CAAC;AAE5D;;;;;;;;;;;;GAYG;AACH,MAAM,MAAM,WAAW,GAAG,YAAY,GAAG,UAAU,CAAC;AAEpD;qCACqC;AACrC,MAAM,MAAM,QAAQ,GAAG,IAAI,GAAG,SAAS,GAAG,SAAS,CAAC;AAEpD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,WAAW,UAAU;IACzB;;;;;OAKG;IACH,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IAEvB,+DAA+D;IAC/D,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC;IAEzB;;;;;;;;;;OAUG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAE5B;gBACY;IACZ,QAAQ,CAAC,OAAO,CAAC,EAAE,WAAW,CAAC;IAE/B;;;;;;;;;;;OAWG;IACH,SAAS,CAAC,CAAC,GAAG,EAAE,OAAO,GAAG,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;IAEvD;;;;OAIG;IACH,aAAa,CAAC,CACZ,GAAG,EAAE,OAAO,EACZ,KAAK,EAAE,UAAU,EACjB,GAAG,EAAE,OAAO,GACX,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAExB;;uCAEmC;IACnC,UAAU,CAAC,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAEhE;oBACgB;IAChB,cAAc,CAAC,CACb,GAAG,EAAE,OAAO,EACZ,KAAK,EAAE,UAAU,EACjB,GAAG,EAAE,OAAO,GACX,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAExB;;;;;;OAMG;IACH,UAAU,CAAC,CAAC,IAAI,EAAE,SAAS,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACpD;AAED,oEAAoE;AACpE,eAAO,MAAM,KAAK,uFAMR,CAAC;AAEX,+BAA+B;AAC/B,MAAM,MAAM,QAAQ,GAAG,CAAC,OAAO,KAAK,CAAC,CAAC,MAAM,CAAC,CAAC"}
@@ -0,0 +1,9 @@
1
+ /** The hooks a Middleware may have, as they are named. @internal */
2
+ export const HOOKS = [
3
+ 'onRequest',
4
+ 'onRequestBody',
5
+ 'onResponse',
6
+ 'onResponseBody',
7
+ 'onComplete',
8
+ ];
9
+ //# sourceMappingURL=middleware.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"middleware.js","sourceRoot":"","sources":["../../gateway/middleware.ts"],"names":[],"mappings":"AA2HA,oEAAoE;AACpE,MAAM,CAAC,MAAM,KAAK,GAAG;IACnB,WAAW;IACX,eAAe;IACf,YAAY;IACZ,gBAAgB;IAChB,YAAY;CACJ,CAAC"}