@encore.dev/sdk 2.0.0-beta.14 → 2.0.0-beta.16

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 (269) hide show
  1. package/README.md +144 -90
  2. package/app_meta.ts +56 -54
  3. package/config/config.test.ts +47 -13
  4. package/config/config.ts +60 -57
  5. package/config/secrets.ts +47 -50
  6. package/config/value.ts +23 -26
  7. package/cron/mod.test.ts +12 -0
  8. package/cron/mod.ts +37 -22
  9. package/dist/app_meta.d.ts +52 -50
  10. package/dist/app_meta.d.ts.map +1 -1
  11. package/dist/app_meta.js +2 -2
  12. package/dist/app_meta.js.map +1 -1
  13. package/dist/config/config.d.ts +58 -55
  14. package/dist/config/config.d.ts.map +1 -1
  15. package/dist/config/config.js +8 -9
  16. package/dist/config/config.js.map +1 -1
  17. package/dist/config/secrets.d.ts +45 -50
  18. package/dist/config/secrets.d.ts.map +1 -1
  19. package/dist/config/secrets.js +11 -9
  20. package/dist/config/secrets.js.map +1 -1
  21. package/dist/config/value.d.ts +20 -23
  22. package/dist/config/value.d.ts.map +1 -1
  23. package/dist/config/value.js +20 -23
  24. package/dist/config/value.js.map +1 -1
  25. package/dist/cron/mod.d.ts +34 -20
  26. package/dist/cron/mod.d.ts.map +1 -1
  27. package/dist/cron/mod.js +13 -13
  28. package/dist/cron/mod.js.map +1 -1
  29. package/dist/frontends/mod.d.ts +20 -19
  30. package/dist/frontends/mod.d.ts.map +1 -1
  31. package/dist/frontends/mod.js +20 -19
  32. package/dist/frontends/mod.js.map +1 -1
  33. package/dist/gateway/errors.d.ts +28 -30
  34. package/dist/gateway/errors.d.ts.map +1 -1
  35. package/dist/gateway/errors.js +29 -31
  36. package/dist/gateway/errors.js.map +1 -1
  37. package/dist/gateway/match.d.ts +54 -54
  38. package/dist/gateway/match.d.ts.map +1 -1
  39. package/dist/gateway/match.js +56 -56
  40. package/dist/gateway/match.js.map +1 -1
  41. package/dist/gateway/middleware.d.ts +62 -64
  42. package/dist/gateway/middleware.d.ts.map +1 -1
  43. package/dist/gateway/middleware.js +1 -1
  44. package/dist/gateway/middleware.js.map +1 -1
  45. package/dist/gateway/mod.d.ts +64 -67
  46. package/dist/gateway/mod.d.ts.map +1 -1
  47. package/dist/gateway/mod.js +72 -76
  48. package/dist/gateway/mod.js.map +1 -1
  49. package/dist/gateway/request.d.ts +138 -134
  50. package/dist/gateway/request.d.ts.map +1 -1
  51. package/dist/gateway/request.js +117 -108
  52. package/dist/gateway/request.js.map +1 -1
  53. package/dist/gateway/serve.d.ts +29 -34
  54. package/dist/gateway/serve.d.ts.map +1 -1
  55. package/dist/gateway/serve.js +51 -56
  56. package/dist/gateway/serve.js.map +1 -1
  57. package/dist/gateways/mod.d.ts +23 -21
  58. package/dist/gateways/mod.d.ts.map +1 -1
  59. package/dist/gateways/mod.js +25 -23
  60. package/dist/gateways/mod.js.map +1 -1
  61. package/dist/internal/otel/http.d.ts +4 -2
  62. package/dist/internal/otel/http.d.ts.map +1 -1
  63. package/dist/internal/otel/http.js +32 -5
  64. package/dist/internal/otel/http.js.map +1 -1
  65. package/dist/internal/runtime/napi/napi.cjs +6 -5
  66. package/dist/internal/runtime/napi/napi.d.cts +58 -16
  67. package/dist/internal/runtime/shutdown.d.ts +75 -57
  68. package/dist/internal/runtime/shutdown.d.ts.map +1 -1
  69. package/dist/internal/runtime/shutdown.js +88 -84
  70. package/dist/internal/runtime/shutdown.js.map +1 -1
  71. package/dist/log/logger.d.ts +43 -37
  72. package/dist/log/logger.d.ts.map +1 -1
  73. package/dist/log/logger.js +26 -25
  74. package/dist/log/logger.js.map +1 -1
  75. package/dist/metrics/mod.d.ts +133 -114
  76. package/dist/metrics/mod.d.ts.map +1 -1
  77. package/dist/metrics/mod.js +109 -101
  78. package/dist/metrics/mod.js.map +1 -1
  79. package/dist/otel.d.ts +6 -3
  80. package/dist/otel.d.ts.map +1 -1
  81. package/dist/otel.js +6 -3
  82. package/dist/otel.js.map +1 -1
  83. package/dist/pubsub/subscription.d.ts +88 -116
  84. package/dist/pubsub/subscription.d.ts.map +1 -1
  85. package/dist/pubsub/subscription.js +14 -17
  86. package/dist/pubsub/subscription.js.map +1 -1
  87. package/dist/pubsub/topic.d.ts +86 -113
  88. package/dist/pubsub/topic.d.ts.map +1 -1
  89. package/dist/pubsub/topic.js +68 -82
  90. package/dist/pubsub/topic.js.map +1 -1
  91. package/dist/redaction/mod.d.ts +62 -0
  92. package/dist/redaction/mod.d.ts.map +1 -0
  93. package/dist/redaction/mod.js +102 -0
  94. package/dist/redaction/mod.js.map +1 -0
  95. package/dist/reqctx/mod.d.ts +83 -86
  96. package/dist/reqctx/mod.d.ts.map +1 -1
  97. package/dist/reqctx/mod.js +39 -41
  98. package/dist/reqctx/mod.js.map +1 -1
  99. package/dist/services/index.d.ts +76 -75
  100. package/dist/services/index.d.ts.map +1 -1
  101. package/dist/services/index.js +73 -71
  102. package/dist/services/index.js.map +1 -1
  103. package/dist/shutdown/mod.d.ts +26 -25
  104. package/dist/shutdown/mod.d.ts.map +1 -1
  105. package/dist/shutdown/mod.js +26 -25
  106. package/dist/shutdown/mod.js.map +1 -1
  107. package/dist/storage/clickhouse/database.d.ts +113 -115
  108. package/dist/storage/clickhouse/database.d.ts.map +1 -1
  109. package/dist/storage/clickhouse/database.js +65 -79
  110. package/dist/storage/clickhouse/database.js.map +1 -1
  111. package/dist/storage/clickhouse/errors.d.ts +25 -26
  112. package/dist/storage/clickhouse/errors.d.ts.map +1 -1
  113. package/dist/storage/clickhouse/errors.js +15 -17
  114. package/dist/storage/clickhouse/errors.js.map +1 -1
  115. package/dist/storage/clickhouse/mod.d.ts +10 -10
  116. package/dist/storage/clickhouse/mod.js +10 -10
  117. package/dist/storage/objects/bucket.d.ts +230 -203
  118. package/dist/storage/objects/bucket.d.ts.map +1 -1
  119. package/dist/storage/objects/bucket.js +146 -106
  120. package/dist/storage/objects/bucket.js.map +1 -1
  121. package/dist/storage/objects/error.d.ts +4 -4
  122. package/dist/storage/objects/error.js +4 -4
  123. package/dist/storage/objects/mod.d.ts +1 -1
  124. package/dist/storage/objects/mod.d.ts.map +1 -1
  125. package/dist/storage/objects/mod.js.map +1 -1
  126. package/dist/storage/objects/refs.d.ts +2 -5
  127. package/dist/storage/objects/refs.d.ts.map +1 -1
  128. package/dist/storage/objects/refs.js.map +1 -1
  129. package/dist/storage/postgres/database.d.ts +124 -149
  130. package/dist/storage/postgres/database.d.ts.map +1 -1
  131. package/dist/storage/postgres/database.js +98 -135
  132. package/dist/storage/postgres/database.js.map +1 -1
  133. package/dist/storage/postgres/errors.d.ts +42 -48
  134. package/dist/storage/postgres/errors.d.ts.map +1 -1
  135. package/dist/storage/postgres/errors.js +44 -51
  136. package/dist/storage/postgres/errors.js.map +1 -1
  137. package/dist/storage/postgres/interval.d.ts +19 -19
  138. package/dist/storage/postgres/interval.d.ts.map +1 -1
  139. package/dist/storage/postgres/interval.js +21 -21
  140. package/dist/storage/postgres/interval.js.map +1 -1
  141. package/dist/storage/redis/bitfield.d.ts +48 -23
  142. package/dist/storage/redis/bitfield.d.ts.map +1 -1
  143. package/dist/storage/redis/bitfield.js +38 -19
  144. package/dist/storage/redis/bitfield.js.map +1 -1
  145. package/dist/storage/redis/command.d.ts +16 -12
  146. package/dist/storage/redis/command.d.ts.map +1 -1
  147. package/dist/storage/redis/command.js +16 -12
  148. package/dist/storage/redis/command.js.map +1 -1
  149. package/dist/storage/redis/commands.d.ts +99 -100
  150. package/dist/storage/redis/commands.d.ts.map +1 -1
  151. package/dist/storage/redis/commands.js +84 -85
  152. package/dist/storage/redis/commands.js.map +1 -1
  153. package/dist/storage/redis/database.d.ts +138 -122
  154. package/dist/storage/redis/database.d.ts.map +1 -1
  155. package/dist/storage/redis/database.js +120 -101
  156. package/dist/storage/redis/database.js.map +1 -1
  157. package/dist/storage/redis/mod.d.ts +52 -48
  158. package/dist/storage/redis/mod.d.ts.map +1 -1
  159. package/dist/storage/redis/mod.js +52 -48
  160. package/dist/storage/redis/mod.js.map +1 -1
  161. package/dist/storage/redis/options.d.ts +26 -24
  162. package/dist/storage/redis/options.d.ts.map +1 -1
  163. package/dist/storage/redis/options.js.map +1 -1
  164. package/dist/storage/redis/pubsub.d.ts +16 -16
  165. package/dist/storage/redis/pubsub.d.ts.map +1 -1
  166. package/dist/storage/redis/pubsub.js +14 -14
  167. package/dist/storage/redis/pubsub.js.map +1 -1
  168. package/dist/storage/redis/reply.d.ts +80 -72
  169. package/dist/storage/redis/reply.d.ts.map +1 -1
  170. package/dist/storage/redis/reply.js +53 -46
  171. package/dist/storage/redis/reply.js.map +1 -1
  172. package/dist/storage/redis/scan.d.ts +4 -4
  173. package/dist/storage/redis/scan.d.ts.map +1 -1
  174. package/dist/storage/redis/scan.js +6 -6
  175. package/dist/storage/redis/scan.js.map +1 -1
  176. package/dist/storage/redis/script.d.ts +28 -21
  177. package/dist/storage/redis/script.d.ts.map +1 -1
  178. package/dist/storage/redis/script.js +29 -22
  179. package/dist/storage/redis/script.js.map +1 -1
  180. package/dist/storage/redis/target.d.ts +17 -1
  181. package/dist/storage/redis/target.d.ts.map +1 -1
  182. package/dist/storage/redis/target.js +30 -1
  183. package/dist/storage/redis/target.js.map +1 -1
  184. package/dist/storage/redis/wire.d.ts +6 -6
  185. package/dist/storage/redis/wire.d.ts.map +1 -1
  186. package/dist/storage/redis/wire.js +6 -6
  187. package/dist/storage/redis/wire.js.map +1 -1
  188. package/dist/testing/mod.d.ts +83 -88
  189. package/dist/testing/mod.d.ts.map +1 -1
  190. package/dist/testing/mod.js +75 -81
  191. package/dist/testing/mod.js.map +1 -1
  192. package/dist/testing/playwright.d.ts +28 -32
  193. package/dist/testing/playwright.d.ts.map +1 -1
  194. package/dist/testing/playwright.js +22 -27
  195. package/dist/testing/playwright.js.map +1 -1
  196. package/dist/testing/puppeteer.d.ts +11 -12
  197. package/dist/testing/puppeteer.d.ts.map +1 -1
  198. package/dist/testing/puppeteer.js +17 -18
  199. package/dist/testing/puppeteer.js.map +1 -1
  200. package/dist/testing/system.d.ts +72 -90
  201. package/dist/testing/system.d.ts.map +1 -1
  202. package/dist/testing/system.js +90 -107
  203. package/dist/testing/system.js.map +1 -1
  204. package/dist/tracing/http.d.ts +2 -1
  205. package/dist/tracing/http.d.ts.map +1 -1
  206. package/dist/tracing/http.js +2 -1
  207. package/dist/tracing/http.js.map +1 -1
  208. package/dist/tracing/node.d.ts.map +1 -1
  209. package/dist/tracing/node.js +1 -1
  210. package/dist/tracing/node.js.map +1 -1
  211. package/dist/tsconfig.build.tsbuildinfo +1 -1
  212. package/frontends/mod.test.ts +6 -2
  213. package/frontends/mod.ts +20 -19
  214. package/gateway/errors.ts +30 -32
  215. package/gateway/match.ts +61 -57
  216. package/gateway/middleware.ts +62 -64
  217. package/gateway/mod.ts +73 -76
  218. package/gateway/request.ts +143 -140
  219. package/gateway/serve.ts +58 -63
  220. package/gateways/mod.ts +25 -23
  221. package/internal/otel/http.test.ts +50 -0
  222. package/internal/otel/http.ts +40 -5
  223. package/internal/runtime/napi/napi.cjs +6 -5
  224. package/internal/runtime/napi/napi.d.cts +58 -16
  225. package/internal/runtime/shutdown.ts +113 -101
  226. package/log/logger.test.ts +8 -0
  227. package/log/logger.ts +48 -45
  228. package/metrics/mod.ts +138 -122
  229. package/otel.ts +6 -3
  230. package/package.json +6 -1
  231. package/pubsub/subscription.ts +93 -123
  232. package/pubsub/topic.ts +96 -130
  233. package/redaction/bridge.test.ts +99 -0
  234. package/redaction/mod.test.ts +188 -0
  235. package/redaction/mod.ts +226 -0
  236. package/reqctx/mod.ts +83 -86
  237. package/services/index.ts +78 -78
  238. package/shutdown/mod.ts +26 -25
  239. package/storage/clickhouse/database.ts +115 -129
  240. package/storage/clickhouse/errors.ts +27 -29
  241. package/storage/clickhouse/mod.ts +10 -10
  242. package/storage/objects/bucket.test.ts +168 -16
  243. package/storage/objects/bucket.ts +336 -247
  244. package/storage/objects/error.ts +4 -4
  245. package/storage/objects/mod.ts +1 -0
  246. package/storage/objects/refs.ts +2 -5
  247. package/storage/postgres/database.ts +131 -164
  248. package/storage/postgres/errors.ts +45 -52
  249. package/storage/postgres/interval.ts +21 -21
  250. package/storage/redis/bitfield.ts +51 -26
  251. package/storage/redis/command.ts +16 -12
  252. package/storage/redis/commands.ts +99 -100
  253. package/storage/redis/database.ts +145 -123
  254. package/storage/redis/mod.ts +52 -48
  255. package/storage/redis/options.ts +26 -24
  256. package/storage/redis/pubsub.ts +14 -14
  257. package/storage/redis/redis.test.ts +17 -2
  258. package/storage/redis/reply.ts +85 -77
  259. package/storage/redis/scan.ts +6 -6
  260. package/storage/redis/script.ts +29 -22
  261. package/storage/redis/target.ts +34 -1
  262. package/storage/redis/wire.ts +8 -8
  263. package/testing/mod.ts +99 -105
  264. package/testing/playwright.ts +32 -37
  265. package/testing/puppeteer.ts +22 -23
  266. package/testing/system.ts +117 -133
  267. package/tracing/http.ts +2 -1
  268. package/tracing/native.test.ts +158 -8
  269. package/tracing/node.ts +1 -0
@@ -1,14 +1,14 @@
1
1
  /**
2
- * Which requests a Middleware is called for.
2
+ * Matchers that select which requests trigger a middleware.
3
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.
4
+ * Encore evaluates every {@link Matcher} natively before a request reaches
5
+ * JavaScript, so unmatched requests incur no JS overhead. Matchers inspect the
6
+ * original client request — header edits by earlier middleware do not affect
7
+ * which later middleware run — and a middleware does not need to read a field
8
+ * in order to match on it.
9
9
  *
10
- * Matchers combine with `.and()`, `.or()` and `.not()`, or {@link allOf},
11
- * {@link anyOf} and {@link not}:
10
+ * Combine matchers with `.and()`, `.or()`, and `.not()`, or with {@link allOf},
11
+ * {@link anyOf}, and {@link not}:
12
12
  *
13
13
  * ```typescript
14
14
  * import { match } from "@encore.dev/sdk/gateway";
@@ -21,8 +21,9 @@
21
21
  * @module
22
22
  */
23
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.
24
+ * A predicate over incoming requests (by service, route, method, path,
25
+ * headers, query parameters, or cookies). Construct matchers using the
26
+ * functions in this module.
26
27
  */
27
28
  export class Matcher {
28
29
  /** @internal */
@@ -31,28 +32,28 @@ export class Matcher {
31
32
  constructor(expr) {
32
33
  this.expr = expr;
33
34
  }
34
- /** Matches the requests both match. */
35
+ /** Matches requests that satisfy both this matcher and `other`. */
35
36
  and(other) {
36
37
  return allOf(this, other);
37
38
  }
38
- /** Matches the requests either matches. */
39
+ /** Matches requests that satisfy either this matcher or `other`. */
39
40
  or(other) {
40
41
  return anyOf(this, other);
41
42
  }
42
- /** Matches the requests this does not. */
43
+ /** Matches requests that do not satisfy this matcher. */
43
44
  not() {
44
45
  return not(this);
45
46
  }
46
- /** The matcher as the gateway core's JSON spells it. */
47
+ /** Serializes the matcher to the gateway core's JSON format. */
47
48
  toJSON() {
48
49
  return this.expr;
49
50
  }
50
51
  }
51
52
  /**
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.
53
+ * A value condition for {@link header}, {@link query}, or {@link cookie}.
54
+ * When a field appears multiple times, any condition other than {@link absent}
55
+ * matches if at least one value satisfies it. Construct conditions using the
56
+ * functions in this module.
56
57
  */
57
58
  export class Cond {
58
59
  /** @internal */
@@ -61,7 +62,7 @@ export class Cond {
61
62
  constructor(expr) {
62
63
  this.expr = expr;
63
64
  }
64
- /** The condition as the gateway core's JSON spells it. */
65
+ /** Serializes the condition to the gateway core's JSON format. */
65
66
  toJSON() {
66
67
  return this.expr;
67
68
  }
@@ -69,7 +70,7 @@ export class Cond {
69
70
  function matchers(what, ms) {
70
71
  return ms.map(m => {
71
72
  if (!(m instanceof Matcher)) {
72
- throw new TypeError(`match.${what} takes Matchers, not ${describe(m)}`);
73
+ throw new TypeError(`match.${what} expects Matcher arguments, got ${describe(m)}`);
73
74
  }
74
75
  return m.expr;
75
76
  });
@@ -77,7 +78,7 @@ function matchers(what, ms) {
77
78
  function strings(what, values) {
78
79
  for (const v of values) {
79
80
  if (typeof v !== 'string') {
80
- throw new TypeError(`match.${what} takes strings, not ${describe(v)}`);
81
+ throw new TypeError(`match.${what} expects string arguments, got ${describe(v)}`);
81
82
  }
82
83
  }
83
84
  return [...values];
@@ -89,58 +90,56 @@ function describe(v) {
89
90
  ? v.constructor.name
90
91
  : typeof v;
91
92
  }
92
- /** Matches the requests every one of `ms` matches: every request when there
93
- * are none. */
93
+ /** Matches requests that satisfy every matcher in `ms` (matches all requests when empty). */
94
94
  export function allOf(...ms) {
95
95
  return new Matcher({ all: matchers('allOf', ms) });
96
96
  }
97
- /** Matches the requests any of `ms` matches: none when there are none. */
97
+ /** Matches requests that satisfy at least one matcher in `ms` (matches no requests when empty). */
98
98
  export function anyOf(...ms) {
99
99
  return new Matcher({ anyOf: matchers('anyOf', ms) });
100
100
  }
101
- /** Matches the requests `m` does not, as `m.not()`. */
101
+ /** Matches requests that do not satisfy `m` (equivalent to `m.not()`). */
102
102
  export function not(m) {
103
103
  return new Matcher({ not: matchers('not', [m])[0] });
104
104
  }
105
- /** Matches the requests the gateway routes to any of `services`. */
105
+ /** Matches requests routed to any of the given `services`. */
106
106
  export function service(...services) {
107
107
  return new Matcher({ service: strings('service', services) });
108
108
  }
109
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.
110
+ * Matches requests routed to the route matching `template` (e.g.
111
+ * `/users/{id}`) and `method` (or any HTTP method if `method` is `undefined`).
112
+ * Path parameter names are ignored, matching router behavior (`/users/{userId}`
113
+ * matches the same route).
114
114
  */
115
115
  export function route(method, template) {
116
116
  return new Matcher({
117
117
  route: method === undefined ? { template } : { method, template },
118
118
  });
119
119
  }
120
- /** Matches the requests with any of `methods`, such as `"POST"`. */
120
+ /** Matches requests using any of the given HTTP `methods` (e.g. `"POST"`). */
121
121
  export function method(...methods) {
122
122
  return new Matcher({ method: strings('method', methods) });
123
123
  }
124
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.
125
+ * Matches requests whose raw (non-percent-decoded) path equals `path`.
126
+ * Trailing slashes are ignored, matching router behavior.
128
127
  */
129
128
  export function path(path) {
130
129
  return new Matcher({ path: { exact: path } });
131
130
  }
132
131
  /**
133
- * Matches the requests whose path starts with `prefix`, by whole segments:
134
- * `/admin` matches `/admin` and `/admin/users`, not `/administrators`.
132
+ * Matches requests whose path starts with `prefix` on whole-segment boundaries
133
+ * (for example, `/admin` matches `/admin` and `/admin/users`, but not
134
+ * `/administrators`).
135
135
  */
136
136
  export function pathPrefix(prefix) {
137
137
  return new Matcher({ path: { prefix } });
138
138
  }
139
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`.
140
+ * Matches requests whose path matches `glob`, where `*` matches a single path
141
+ * segment and `**` matches zero or more segments (e.g. `/users/*` matches
142
+ * `/users/1`, and `/users/**` matches `/users` and `/users/1/posts/2`).
144
143
  */
145
144
  export function pathGlob(glob) {
146
145
  return new Matcher({ path: { glob } });
@@ -148,57 +147,58 @@ export function pathGlob(glob) {
148
147
  function field(kind, name, cond) {
149
148
  const c = cond ?? present();
150
149
  if (!(c instanceof Cond)) {
151
- throw new TypeError(`match.${kind} takes a Cond, not ${describe(c)}`);
150
+ throw new TypeError(`match.${kind} expects a Cond, got ${describe(c)}`);
152
151
  }
153
152
  return new Matcher({ [kind]: { name, cond: c.expr } });
154
153
  }
155
154
  /**
156
- * Matches the requests whose header `name` satisfies `cond`, or is sent,
157
- * without one. Header names are case-insensitive; values are compared as
155
+ * Matches requests whose header `name` satisfies `cond`, or is present when
156
+ * `cond` is omitted. Header names are case-insensitive; values are compared as
158
157
  * sent.
159
158
  */
160
159
  export function header(name, cond) {
161
160
  return field('header', name, cond);
162
161
  }
163
162
  /**
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.
163
+ * Matches requests whose query parameter `name` satisfies `cond`, or is
164
+ * present when `cond` is omitted. Parameter names are case-sensitive; values
165
+ * are percent-decoded before comparison.
167
166
  */
168
167
  export function query(name, cond) {
169
168
  return field('query', name, cond);
170
169
  }
171
170
  /**
172
- * Matches the requests whose cookie `name` satisfies `cond`, or is sent,
173
- * without one. Names are case-sensitive; values are compared as sent.
171
+ * Matches requests whose cookie `name` satisfies `cond`, or is present when
172
+ * `cond` is omitted. Cookie names are case-sensitive; values are compared as
173
+ * sent.
174
174
  */
175
175
  export function cookie(name, cond) {
176
176
  return field('cookie', name, cond);
177
177
  }
178
- /** Holds when the field is sent. */
178
+ /** Matches when the field is present. */
179
179
  export function present() {
180
180
  return new Cond({ present: true });
181
181
  }
182
- /** Holds when the field is not sent. */
182
+ /** Matches when the field is absent. */
183
183
  export function absent() {
184
184
  return new Cond({ absent: true });
185
185
  }
186
- /** Holds when the field's value is `value`. */
186
+ /** Matches when the field equals `value`. */
187
187
  export function equals(value) {
188
188
  return new Cond({ equals: value });
189
189
  }
190
- /** Holds when the field's value starts with `prefix`, as `"Bearer "`. */
190
+ /** Matches when the field starts with `prefix` (e.g. `"Bearer "`). */
191
191
  export function prefix(prefix) {
192
192
  return new Cond({ prefix });
193
193
  }
194
- /** Holds when the field's value is one of `values`. */
194
+ /** Matches when the field equals one of `values`. */
195
195
  export function oneOf(...values) {
196
196
  return new Cond({ oneOf: strings('oneOf', values) });
197
197
  }
198
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.
199
+ * Matches when `pattern` matches the field's entire value. Uses RE2 syntax
200
+ * (not JavaScript RegExp syntax) because Encore evaluates it natively. An
201
+ * invalid pattern causes `serve` to reject.
202
202
  */
203
203
  export function regex(pattern) {
204
204
  return new Cond({ regex: pattern });
@@ -1 +1 @@
1
- {"version":3,"file":"match.js","sourceRoot":"","sources":["../../gateway/match.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAKH;;;GAGG;AACH,MAAM,OAAO,OAAO;IAClB,gBAAgB;IACP,IAAI,CAAO;IAEpB,gBAAgB;IAChB,YAAY,IAAU;QACpB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;IAED,uCAAuC;IACvC,GAAG,CAAC,KAAc;QAChB,OAAO,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IAC5B,CAAC;IAED,2CAA2C;IAC3C,EAAE,CAAC,KAAc;QACf,OAAO,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IAC5B,CAAC;IAED,0CAA0C;IAC1C,GAAG;QACD,OAAO,GAAG,CAAC,IAAI,CAAC,CAAC;IACnB,CAAC;IAED,wDAAwD;IACxD,MAAM;QACJ,OAAO,IAAI,CAAC,IAAI,CAAC;IACnB,CAAC;CACF;AAED;;;;;GAKG;AACH,MAAM,OAAO,IAAI;IACf,gBAAgB;IACP,IAAI,CAAO;IAEpB,gBAAgB;IAChB,YAAY,IAAU;QACpB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;IAED,0DAA0D;IAC1D,MAAM;QACJ,OAAO,IAAI,CAAC,IAAI,CAAC;IACnB,CAAC;CACF;AAED,SAAS,QAAQ,CAAC,IAAY,EAAE,EAAsB;IACpD,OAAO,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE;QAChB,IAAI,CAAC,CAAC,CAAC,YAAY,OAAO,CAAC,EAAE,CAAC;YAC5B,MAAM,IAA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AAK,CAAC,GAAG,MAAgB;IACvC,OAAO,IAAI,IAAI,CAAC,EAAC,KAAK,EAAE,OAAO,CAAC,OAAO,EAAE,MAAM,CAAC,EAAC,CAAC,CAAC;AACrD,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,KAAK,CAAC,OAAe;IACnC,OAAO,IAAI,IAAI,CAAC,EAAC,KAAK,EAAE,OAAO,EAAC,CAAC,CAAC;AACpC,CAAC"}
@@ -2,32 +2,33 @@ import type { Rejection } from './errors.js';
2
2
  import type { Matcher } from './match.js';
3
3
  import type { Completed, Request, Response } from './request.js';
4
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.
5
+ * Determines how the gateway handles an `onRequest` failure (an uncaught error
6
+ * that is not a {@link Rejection}, or a timeout).
7
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.
8
+ * - `"failClosed"` (default): rejects the request — returning 503 Service
9
+ * Unavailable if the hook timed out or could not be dispatched to JavaScript
10
+ * (allowing clients to retry), and 500 Internal Server Error otherwise. Use
11
+ * this for authentication or authorization middleware so failures never grant
12
+ * access.
13
+ * - `"failOpen"`: lets the request through unmodified, as if the hook had
14
+ * succeeded. Use this for non-critical middleware such as a rate limiter
15
+ * backed by an unavailable store.
16
16
  */
17
17
  export type ErrorPolicy = 'failClosed' | 'failOpen';
18
- /** What `onRequest` returns: nothing to let the request through, or a
19
- * {@link Rejection} to answer it. */
18
+ /**
19
+ * Return value of `onRequest`: return nothing to allow the request through, or
20
+ * a {@link Rejection} to answer it directly.
21
+ */
20
22
  export type Decision = void | undefined | Rejection;
21
23
  /**
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
+ * Middleware executed by the gateway on public requests before they reach a
25
+ * service, as responses return, and after requests finish.
24
26
  *
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.
27
+ * Provide a plain object or class instance implementing only the hooks you
28
+ * need; unimplemented hooks incur no overhead. A middleware runs only on
29
+ * requests matching `match`. Middleware execute in the order passed to `serve`:
30
+ * each `onRequest` sees headers written by earlier middleware, and `onResponse`
31
+ * hooks run in reverse order only for middleware that let the request through.
31
32
  *
32
33
  * ```typescript
33
34
  * class RateLimit implements gateway.Middleware {
@@ -36,73 +37,70 @@ export type Decision = void | undefined | Rejection;
36
37
  * }
37
38
  * ```
38
39
  *
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.
40
+ * Before starting, `serve` validates every middleware: it must define at least
41
+ * one hook, have unique kebab-case names, use valid settings, and not define
42
+ * unrecognized `on[A-Z]*` properties (catching misspelled hook names).
43
43
  */
44
44
  export interface Middleware {
45
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.
46
+ * Unique kebab-case identifier for this middleware, used in logs, traces,
47
+ * and {@link Completed.rejectedBy}. Defaults to the class name in kebab-case
48
+ * (e.g. `RateLimit` becomes `rate-limit`); required for plain objects.
50
49
  */
51
50
  readonly name?: string;
52
- /** The requests it is called for; every request when unset. */
51
+ /** Requests that trigger this middleware. Matches all requests when unset. */
53
52
  readonly match?: Matcher;
54
53
  /**
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}.
54
+ * Maximum time in milliseconds allowed for `onRequest` and `onResponse`,
55
+ * measured from when the gateway enqueues the hook. Defaults to 5000 ms.
56
+ *
57
+ * On expiry, {@link Request.signal} aborts, the gateway moves on without
58
+ * waiting, and `onError` decides the outcome (`"failClosed"` responds with
59
+ * 503). Hooks that expire before starting are skipped. When a hook fails or
60
+ * times out, its header edits and Request Context changes are discarded, and
61
+ * `state` reverts to its previous reference (see {@link Request.state}).
64
62
  */
65
63
  readonly timeoutMs?: number;
66
- /** What `onRequest` failing does to the request; `"failClosed"` when
67
- * unset. */
64
+ /** How `onRequest` failures affect the request. Defaults to `"failClosed"`. */
68
65
  readonly onError?: ErrorPolicy;
69
66
  /**
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}).
67
+ * Runs before the request is forwarded. May modify headers
68
+ * ({@link Request.setHeader}), set Request Context fields
69
+ * ({@link Request.set}) or span attributes ({@link Request.setAttributes}),
70
+ * and skip later hooks ({@link Request.skip}).
75
71
  *
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.
72
+ * Return nothing to let the request through, or return/throw a
73
+ * {@link Rejection} ({@link reject} or {@link respond}) to answer the client
74
+ * immediately without calling later middleware or forwarding the request. Any
75
+ * other error fails the hook and is handled according to `onError`.
80
76
  */
81
77
  onRequest?(req: Request): Decision | Promise<Decision>;
82
78
  /**
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.
79
+ * Observes each chunk of the request body as it is forwarded (`end` is `true`
80
+ * on the final chunk). Forwarding does not wait on this hook; under heavy
81
+ * load, excess chunks may be dropped and logged.
86
82
  */
87
83
  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}). */
84
+ /**
85
+ * Runs with the response status and headers before they are sent to the
86
+ * client. May modify response headers ({@link Response.setHeader}).
87
+ */
91
88
  onResponse?(req: Request, resp: Response): void | Promise<void>;
92
- /** Shown each chunk of the response's body, as `onRequestBody` is the
93
- * request's. */
89
+ /**
90
+ * Observes each chunk of the response body, behaving like `onRequestBody`.
91
+ */
94
92
  onResponseBody?(req: Request, chunk: Uint8Array, end: boolean): void | Promise<void>;
95
93
  /**
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.
94
+ * Runs asynchronously after the request completes (once the client has
95
+ * received the response or disconnected). Invoked for every matched request,
96
+ * even those rejected by another middleware, making it ideal for logging,
97
+ * metrics, or billing. Under heavy load, excess events may be dropped and
98
+ * logged.
101
99
  */
102
100
  onComplete?(done: Completed): void | Promise<void>;
103
101
  }
104
- /** The hooks a Middleware may have, as they are named. @internal */
102
+ /** Supported middleware hook names. @internal */
105
103
  export declare const HOOKS: readonly ["onRequest", "onRequestBody", "onResponse", "onResponseBody", "onComplete"];
106
- /** A hook's name. @internal */
104
+ /** Union of supported middleware hook names. @internal */
107
105
  export type HookName = (typeof HOOKS)[number];
108
106
  //# sourceMappingURL=middleware.d.ts.map
@@ -1 +1 @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Z,GAAG,EAAE,OAAO,EACZ,KAAK,EAAE,UAAU,EACjB,GAAG,EAAE,OAAO,GACX,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAExB;;;OAGG;IACH,UAAU,CAAC,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAEhE;;OAEG;IACH,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,iDAAiD;AACjD,eAAO,MAAM,KAAK,uFAMR,CAAC;AAEX,0DAA0D;AAC1D,MAAM,MAAM,QAAQ,GAAG,CAAC,OAAO,KAAK,CAAC,CAAC,MAAM,CAAC,CAAC"}
@@ -1,4 +1,4 @@
1
- /** The hooks a Middleware may have, as they are named. @internal */
1
+ /** Supported middleware hook names. @internal */
2
2
  export const HOOKS = [
3
3
  'onRequest',
4
4
  'onRequestBody',
@@ -1 +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"}
1
+ {"version":3,"file":"middleware.js","sourceRoot":"","sources":["../../gateway/middleware.ts"],"names":[],"mappings":"AAyHA,iDAAiD;AACjD,MAAM,CAAC,MAAM,KAAK,GAAG;IACnB,WAAW;IACX,eAAe;IACf,YAAY;IACZ,gBAAgB;IAChB,YAAY;CACJ,CAAC"}