@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
@@ -6,8 +6,8 @@ import * as reqfields from '../internal/reqfields';
6
6
  import {Field} from '../reqctx/mod';
7
7
 
8
8
  /**
9
- * A request as the native runtime keeps it: its headers, query parameters
10
- * and cookies are handed to JavaScript only as a hook reads them.
9
+ * Native request view whose headers, query parameters, and cookies are loaded
10
+ * into JavaScript lazily on read.
11
11
  * @internal
12
12
  */
13
13
  export interface NativeView {
@@ -20,7 +20,7 @@ export interface NativeView {
20
20
  readonly clientIp?: string | null;
21
21
  readonly traceparent?: string | null;
22
22
  readonly tracestate?: string | null;
23
- /** The Request Context fields earlier Middleware set, encoded. */
23
+ /** Encoded Request Context fields set by earlier middleware. */
24
24
  readonly context?: readonly NativeField[] | null;
25
25
  pathParam(name: string): string | null | undefined;
26
26
  headerValues(name: string): string[];
@@ -28,15 +28,16 @@ export interface NativeView {
28
28
  cookie(name: string): string | null | undefined;
29
29
  }
30
30
 
31
- /** A response's headers as the native runtime keeps them. @internal */
31
+ /** Native response header view. @internal */
32
32
  export interface NativeHeaders {
33
33
  values(name: string): string[];
34
34
  }
35
35
 
36
36
  /**
37
- * A set of a Middleware's later hooks, which {@link Request.skip} narrows,
38
- * combined with `|`: `Hook.Response | Hook.Complete`. `onRequest` is not
39
- * among them: it is the hook that skips.
37
+ * Bitmask flags identifying later middleware hooks that can be skipped with
38
+ * {@link Request.skip}, combined with `|` (e.g.
39
+ * `Hook.Response | Hook.Complete`). `onRequest` is omitted because it is the
40
+ * hook that calls `skip`.
40
41
  */
41
42
  export const Hook = {
42
43
  /** `onRequestBody`. */
@@ -49,27 +50,26 @@ export const Hook = {
49
50
  Complete: 1 << 4,
50
51
  } as const;
51
52
 
52
- /** A value of {@link Hook}, or several combined with `|`. */
53
+ /** A {@link Hook} flag or bitwise-OR combination of flags. */
53
54
  export type Hooks = number;
54
55
 
55
- /** The value of an attribute of a request's span. */
56
+ /** Allowed value type for request span attributes. */
56
57
  export type AttributeValue = string | boolean | number;
57
58
 
58
- /** An edit to headers: op 0 sets, 1 adds, 2 removes. @internal */
59
+ /** Header mutation sent to the native gateway (op 0 = set, 1 = add, 2 = remove). @internal */
59
60
  export interface HeaderEdit {
60
61
  op: number;
61
62
  name: string;
62
63
  value: string;
63
64
  }
64
65
 
65
- /** A Request Context field, encoded, as the native runtime shows and takes
66
- * it: a null value clears it. @internal */
66
+ /** Encoded Request Context field passed across the native boundary (`null` clears it). @internal */
67
67
  export interface NativeField {
68
68
  name: string;
69
69
  value?: string | null;
70
70
  }
71
71
 
72
- /** An attribute as the native runtime takes it. @internal */
72
+ /** Span attribute passed across the native boundary. @internal */
73
73
  export interface NativeAttribute {
74
74
  key: string;
75
75
  value: AttributeValue;
@@ -81,9 +81,8 @@ const ADD = 1;
81
81
  const REMOVE = 2;
82
82
 
83
83
  /**
84
- * Whether a Middleware may not edit the header `name`, lowercase: Encore's
85
- * headers, the trace context's, those framing the message, and CORS's. The
86
- * gateway refuses the same edits.
84
+ * Reports whether a lowercase header name is reserved by Encore (internal
85
+ * `x-encore-*` headers, trace context, HTTP framing, and CORS headers).
87
86
  * @internal
88
87
  */
89
88
  export function isProtected(name: string): boolean {
@@ -107,8 +106,7 @@ const PROTECTED = new Set([
107
106
  'upgrade',
108
107
  ]);
109
108
 
110
- /** Header values edited by a Middleware, by lowercase name, as they read
111
- * now. */
109
+ /** Header values edited by a middleware, keyed by lowercase name. */
112
110
  type Overrides = Map<string, string[]>;
113
111
 
114
112
  function overridden(
@@ -125,38 +123,38 @@ function overridden(
125
123
  return out;
126
124
  }
127
125
 
128
- /** One Middleware's state for one request, shared by the Requests its hooks
129
- * are shown. @internal */
126
+ /** Per-middleware, per-request state shared across hooks. @internal */
130
127
  export interface Lane {
131
128
  state: unknown;
132
129
  }
133
130
 
134
- /** One run of a timed hook, `onRequest` or `onResponse`: expired once it
135
- * has timed out and the gateway went on without it. @internal */
131
+ /** Tracks whether an active timed hook (`onRequest` or `onResponse`) has expired. @internal */
136
132
  export interface HookRun {
137
133
  expired: boolean;
138
134
  }
139
135
 
140
- /** The timed hook the code running now is part of, following its `await`s,
141
- * timers and callbacks: so that what a timed-out hook goes on doing cannot
142
- * change the state its later hooks are shown. @internal */
136
+ /**
137
+ * Tracks the currently running timed hook across async continuations so a
138
+ * timed-out hook cannot mutate `Request.state` after the gateway has moved on.
139
+ * @internal
140
+ */
143
141
  export const hookRuns = new AsyncLocalStorage<HookRun>();
144
142
 
145
- /** Never aborts: the signal of a hook without a timeout. */
143
+ /** Never aborts; used as the signal for untimed hooks. */
146
144
  const NEVER = new AbortController().signal;
147
145
 
148
146
  /**
149
- * A public request the gateway serves, as a Middleware is shown it. Made by
150
- * Encore: the same object is passed to each of one Middleware's hooks for
151
- * one request.
147
+ * Represents an incoming public request passed to a middleware's hooks. The
148
+ * same instance is reused across all hooks of a single middleware for a given
149
+ * request.
152
150
  *
153
- * It reads the request's headers, query parameters and cookies as the hook
154
- * asks for them, as the Middleware before this one left them, and with this
155
- * one's edits. Encore's own headers, `x-encore-*`, are never shown. Edits
156
- * are only allowed in `onRequest`; elsewhere they throw.
151
+ * Headers, query parameters, and cookies are read lazily and reflect edits
152
+ * made by earlier middleware as well as this middleware. Internal `x-encore-*`
153
+ * headers are never exposed. Mutating methods are only permitted inside
154
+ * `onRequest` and throw if called elsewhere.
157
155
  */
158
156
  export class Request {
159
- /** Identifies the request among those in flight; unique while it is. */
157
+ /** Identifier unique among currently in-flight requests. */
160
158
  readonly id: number;
161
159
 
162
160
  /** @internal */ readonly middleware: string;
@@ -165,17 +163,15 @@ export class Request {
165
163
  /** @internal */ overrides: Overrides | undefined;
166
164
  /** @internal */ deciding = false;
167
165
  /** @internal */ edits: HeaderEdit[] = [];
168
- /** @internal The request's Request Context fields, encoded, with this
169
- * Middleware's changes: changed in place, as the context its hooks run in
170
- * holds this record. */
166
+ /** @internal Encoded Request Context fields, mutated in place so active hook contexts see updates immediately. */
171
167
  readonly fields: Record<string, string> = {};
172
- /** @internal The changes `set` and `unset` made, for the gateway. */
168
+ /** @internal Field mutations queued for the native gateway. */
173
169
  fieldEdits: NativeField[] = [];
174
170
  /** @internal */ attributes: NativeAttribute[] = [];
175
171
  /** @internal */ skipped = 0;
176
172
  /** @internal */ hookSignal: AbortSignal = NEVER;
177
173
 
178
- /** @internal Made by Encore. */
174
+ /** @internal Created by Encore. */
179
175
  constructor(
180
176
  middleware: string,
181
177
  id: number,
@@ -191,62 +187,62 @@ export class Request {
191
187
  }
192
188
  }
193
189
 
194
- /** The HTTP method, upper case. */
190
+ /** Uppercase HTTP method (e.g. `"GET"`). */
195
191
  get method(): string {
196
192
  return this.view?.method ?? '';
197
193
  }
198
194
 
199
- /** The host the client addressed, from its Host header, if any. */
195
+ /** Value of the client's `Host` header, if present. */
200
196
  get host(): string | undefined {
201
197
  return this.view?.host ?? undefined;
202
198
  }
203
199
 
204
- /** The path, as the client sent it: not percent-decoded, without the
205
- * query. */
200
+ /** Raw request path as sent by the client (not percent-decoded, without query string). */
206
201
  get path(): string {
207
202
  return this.view?.path ?? '';
208
203
  }
209
204
 
210
- /** The service the gateway routes the request to. */
205
+ /** Target service name that the gateway routes this request to. */
211
206
  get service(): string {
212
207
  return this.view?.service ?? '';
213
208
  }
214
209
 
215
- /** The route template it matched, such as `/users/{id}`. */
210
+ /** Matched route template (e.g. `/users/{id}`). */
216
211
  get route(): string {
217
212
  return this.view?.route ?? '';
218
213
  }
219
214
 
220
- /** The address of the client the gateway took the request from, if
221
- * known. */
215
+ /** Client IP address observed by the gateway, if known. */
222
216
  get clientIp(): string | undefined {
223
217
  return this.view?.clientIp ?? undefined;
224
218
  }
225
219
 
226
220
  /**
227
- * Aborts when the hook running now runs out of time: `onRequest` and
228
- * `onResponse` have their Middleware's timeout, and the gateway goes on
229
- * without them then. Pass it to what the hook awaits, as `fetch`, so the
230
- * work stops too. It never aborts in the other hooks.
221
+ * Aborts when the current hook exceeds its timeout (`onRequest` and
222
+ * `onResponse` only). Pass this signal to async operations such as `fetch`
223
+ * so they cancel cleanly if the gateway moves on. Never aborts in other hooks.
231
224
  */
232
225
  get signal(): AbortSignal {
233
226
  return this.hookSignal;
234
227
  }
235
228
 
236
- /** The value of the path parameter `name` of the matched route, as it was
237
- * sent, not percent-decoded; undefined when the route has none of that
238
- * name. */
229
+ /**
230
+ * Returns the raw (non-percent-decoded) value of path parameter `name`, or
231
+ * `undefined` if the matched route has no parameter with that name.
232
+ */
239
233
  pathParam(name: string): string | undefined {
240
234
  return this.view?.pathParam(name) ?? undefined;
241
235
  }
242
236
 
243
- /** The first value of the request header `name`, as earlier Middleware
244
- * left it; undefined when it is not sent. Any case. */
237
+ /**
238
+ * Returns the first value of request header `name` (case-insensitive), or
239
+ * `undefined` if absent. Reflects edits by earlier middleware and this hook.
240
+ */
245
241
  header(name: string): string | undefined {
246
242
  return this.headerValues(name)[0];
247
243
  }
248
244
 
249
- /** Every value of the request header `name`, in order. Any case. */
245
+ /** Returns all values of request header `name` (case-insensitive) in order. */
250
246
  headerValues(name: string): string[] {
251
247
  const lower = name.toLowerCase();
252
248
  const edited = this.overrides?.get(lower);
@@ -254,27 +250,31 @@ export class Request {
254
250
  return this.view?.headerValues(lower) ?? [];
255
251
  }
256
252
 
257
- /** The first value of the query parameter `name`, percent-decoded;
258
- * undefined when it is not sent. */
253
+ /**
254
+ * Returns the first percent-decoded value of query parameter `name`, or
255
+ * `undefined` if absent.
256
+ */
259
257
  query(name: string): string | undefined {
260
258
  return this.queryValues(name)[0];
261
259
  }
262
260
 
263
- /** Every value of the query parameter `name`, percent-decoded, in
264
- * order. */
261
+ /** Returns all percent-decoded values of query parameter `name` in order. */
265
262
  queryValues(name: string): string[] {
266
263
  return this.view?.queryValues(name) ?? [];
267
264
  }
268
265
 
269
- /** The value of the cookie `name`, from the `Cookie` headers as the
270
- * Middleware before this one left them; undefined when it is not sent. */
266
+ /**
267
+ * Returns the value of cookie `name` from the current `Cookie` headers, or
268
+ * `undefined` if absent.
269
+ */
271
270
  cookie(name: string): string | undefined {
272
271
  return this.view?.cookie(name) ?? undefined;
273
272
  }
274
273
 
275
- /** The context of the request's span at the gateway, which the spans its
276
- * hooks start are children of; undefined when the request is not
277
- * traced. */
274
+ /**
275
+ * Span context for this request at the gateway (parent of spans started by
276
+ * hooks), or `undefined` if the request is not traced.
277
+ */
278
278
  get spanContext(): SpanContext | undefined {
279
279
  const tp = this.view?.traceparent;
280
280
  if (!tp) return undefined;
@@ -308,30 +308,35 @@ export class Request {
308
308
  }
309
309
 
310
310
  /**
311
- * Sets the request header `name` to `value`, replacing any values, for the
312
- * Middleware after this one and the service. Only in `onRequest`. Not for
313
- * Encore's headers (`x-encore-*`, the trace context), those framing the
314
- * message (`content-length` and the like) and CORS's: those throw.
311
+ * Sets request header `name` to `value`, replacing any existing values, for
312
+ * downstream middleware and the target service. Only valid in `onRequest`.
313
+ * Throws if `name` is reserved by Encore (`x-encore-*`, trace context,
314
+ * HTTP framing, or CORS headers).
315
315
  */
316
316
  setHeader(name: string, value: string): void {
317
317
  this.edit('setHeader', SET, name, String(value));
318
318
  }
319
319
 
320
- /** Adds `value` to the request header `name`, keeping its values. As
321
- * {@link Request.setHeader}. */
320
+ /**
321
+ * Appends `value` to request header `name` without removing existing values.
322
+ * Only valid in `onRequest`; see {@link Request.setHeader}.
323
+ */
322
324
  addHeader(name: string, value: string): void {
323
325
  this.edit('addHeader', ADD, name, String(value));
324
326
  }
325
327
 
326
- /** Removes the request header `name`. As {@link Request.setHeader}. */
328
+ /**
329
+ * Removes request header `name`. Only valid in `onRequest`; see
330
+ * {@link Request.setHeader}.
331
+ */
327
332
  delHeader(name: string): void {
328
333
  this.edit('delHeader', REMOVE, name, '');
329
334
  }
330
335
 
331
336
  /**
332
- * Sets attributes of the request's span at the gateway, as a user or
333
- * tenant id to find its trace by. Never secrets. Only in `onRequest`. An
334
- * integer number is recorded as an integer, another as a float.
337
+ * Attaches attributes to the gateway's request span (for example, a user or
338
+ * tenant ID for trace search — never secrets). Only valid in `onRequest`.
339
+ * Safe integers are recorded as integers; other numbers are recorded as floats.
335
340
  */
336
341
  setAttributes(attributes: Record<string, AttributeValue>): void {
337
342
  this.mustDecide('setAttributes');
@@ -350,32 +355,32 @@ export class Request {
350
355
  break;
351
356
  default:
352
357
  throw new TypeError(
353
- `span attribute ${JSON.stringify(key)} is a ${typeof value}; ` +
354
- 'a span attribute is a string, boolean or number',
358
+ `span attribute ${JSON.stringify(key)} has type ${typeof value}; ` +
359
+ 'expected a string, boolean, or number',
355
360
  );
356
361
  }
357
362
  }
358
363
  }
359
364
 
360
- /** Skips this Middleware's later `hooks` for this request, as an
361
- * `onResponse` a request without a session has no use for:
362
- * `req.skip(Hook.Response | Hook.Complete)`. Only in `onRequest`. */
365
+ /**
366
+ * Skips the specified later `hooks` of this middleware for this request
367
+ * (e.g. `req.skip(Hook.Response | Hook.Complete)`). Only valid in `onRequest`.
368
+ */
363
369
  skip(hooks: Hooks): void {
364
370
  this.mustDecide('skip');
365
371
  if (!Number.isInteger(hooks) || hooks < 0 || hooks > 0xff) {
366
372
  throw new TypeError(
367
- 'skip takes Hook flags, as Hook.Response | Hook.Complete',
373
+ 'skip expects Hook flags (e.g. Hook.Response | Hook.Complete)',
368
374
  );
369
375
  }
370
376
  this.skipped |= hooks & ~1;
371
377
  }
372
378
 
373
379
  /**
374
- * The Request Context field's value for the request; undefined when it is
375
- * not set: as the Middleware before this one set it, or this one did. In
376
- * a hook, `field.get()` reads the same, as hooks run in their request's
377
- * context; in `onComplete`, as the request was forwarded. A value that is
378
- * not JSON reads as not set, as it does to `field.get()`.
380
+ * Returns the current value of Request Context `field` for this request, or
381
+ * `undefined` if unset. Hooks can equivalently call `field.get()`. In
382
+ * `onComplete`, reflects the value as forwarded. Invalid JSON values are
383
+ * treated as unset.
379
384
  */
380
385
  get<T>(field: Field<T>): T | undefined {
381
386
  checkField('get', field);
@@ -383,11 +388,10 @@ export class Request {
383
388
  }
384
389
 
385
390
  /**
386
- * Sets the Request Context field to `value` for the request. The
387
- * Middleware after this one, the service the request reaches, and every
388
- * service it calls in turn with the request's context read it, with
389
- * `field.get()`; so do the calls this hook makes from then on. It is how a
390
- * Middleware that authenticates says who the request is for:
391
+ * Sets Request Context `field` to `value` for this request. Subsequent
392
+ * middleware, the target service, downstream services called with the
393
+ * request's context, and calls made by this hook afterward will read it via
394
+ * `field.get()`.
391
395
  *
392
396
  * ```ts
393
397
  * export const currentUser = new Field<User>('user');
@@ -403,14 +407,14 @@ export class Request {
403
407
  * }
404
408
  * ```
405
409
  *
406
- * Only in `onRequest`. The request takes the fields once the hook
407
- * returns: none when it fails or answers the request.
410
+ * Only valid in `onRequest`. Changes are applied only if `onRequest` lets
411
+ * the request through; they are discarded if the hook fails or rejects.
408
412
  *
409
- * @throws TypeError - The value has no JSON form, or `field` is not a
410
- * {@link Field}.
411
- * @throws RangeError - The value encodes to more than a field may: 4 KiB,
412
- * or 1 KiB for a field declared with `pubsub`; or it makes the request's
413
- * fields encode to more than 8 KiB.
413
+ * @throws TypeError If `value` cannot be serialized to JSON or `field` is
414
+ * not a {@link Field}.
415
+ * @throws RangeError If the encoded value exceeds the per-field limit (4 KiB,
416
+ * or 1 KiB for fields with `pubsub` enabled) or pushes total request fields
417
+ * past 8 KiB.
414
418
  */
415
419
  set<T>(field: Field<T>, value: T): void {
416
420
  this.mustDecide('set');
@@ -424,8 +428,10 @@ export class Request {
424
428
  this.fieldEdits.push({name: field.name, value: encoded});
425
429
  }
426
430
 
427
- /** Clears the Request Context field for the request, as one the
428
- * Middleware before this one set. As {@link Request.set} sets it. */
431
+ /**
432
+ * Clears Request Context `field` for this request (including values set by
433
+ * earlier middleware). Only valid in `onRequest`.
434
+ */
429
435
  unset(field: Field<unknown>): void {
430
436
  this.mustDecide('unset');
431
437
  checkField('unset', field);
@@ -434,16 +440,15 @@ export class Request {
434
440
  }
435
441
 
436
442
  /**
437
- * What this Middleware kept for the request, for its later hooks: the
438
- * user `onRequest` found, for `onComplete` to bill. Undefined until set.
439
- * Encore drops it when the request is done.
443
+ * Per-request state private to this middleware, shared across its hooks
444
+ * (for example, saving an authenticated user in `onRequest` for `onComplete`
445
+ * to record). Starts as `undefined` and is dropped when the request finishes.
440
446
  *
441
- * When `onRequest` or `onResponse` fails or times out, `state` goes back
442
- * to the object it was before the hook; what the hook changed inside that
443
- * object stays, since Encore does not copy it. A hook that may fail after
444
- * changing it should set a new object, as `req.state = {...req.state,
445
- * billed: true}`, rather than change the one it has. A hook that goes on
446
- * after it timed out may no longer set it: that throws.
447
+ * If `onRequest` or `onResponse` fails or times out, `state` reverts to its
448
+ * reference before the hook ran. Because Encore does not deep-copy `state`,
449
+ * assign a new object (e.g. `req.state = {...req.state, billed: true}`)
450
+ * rather than mutating in place if the hook might fail later. Setting `state`
451
+ * after a hook has timed out throws an error.
447
452
  */
448
453
  get state(): unknown {
449
454
  return this.lane.state;
@@ -452,8 +457,7 @@ export class Request {
452
457
  set state(value: unknown) {
453
458
  if (hookRuns.getStore()?.expired) {
454
459
  throw new Error(
455
- `middleware ${JSON.stringify(this.middleware)}: Request.state is not set by a hook ` +
456
- 'that timed out: the gateway went on without it',
460
+ `middleware ${JSON.stringify(this.middleware)}: cannot set Request.state after the hook timed out`,
457
461
  );
458
462
  }
459
463
  this.lane.state = value;
@@ -475,12 +479,11 @@ function checkField(method: string, field: unknown): void {
475
479
  }
476
480
 
477
481
  /**
478
- * A response to a request the gateway served, in `onResponse`. Made by
479
- * Encore. It reads the response's headers as the hook asks for them, as the
480
- * Middleware after this one left them, and with this one's edits.
482
+ * Represents the HTTP response passed to `onResponse`. Headers are read lazily
483
+ * and reflect edits made by downstream middleware as well as this middleware.
481
484
  */
482
485
  export class Response {
483
- /** The HTTP status. */
486
+ /** The HTTP status code. */
484
487
  readonly status: number;
485
488
 
486
489
  /** @internal */ readonly middleware: string;
@@ -489,7 +492,7 @@ export class Response {
489
492
  /** @internal */ editing = false;
490
493
  /** @internal */ edits: HeaderEdit[] = [];
491
494
 
492
- /** @internal Made by Encore. */
495
+ /** @internal Created by Encore. */
493
496
  constructor(
494
497
  middleware: string,
495
498
  status: number,
@@ -500,12 +503,12 @@ export class Response {
500
503
  this.headers = headers;
501
504
  }
502
505
 
503
- /** The first value of the response header `name`, if any. Any case. */
506
+ /** Returns the first value of response header `name` (case-insensitive), or `undefined` if absent. */
504
507
  header(name: string): string | undefined {
505
508
  return this.headerValues(name)[0];
506
509
  }
507
510
 
508
- /** Every value of the response header `name`, in order. Any case. */
511
+ /** Returns all values of response header `name` (case-insensitive) in order. */
509
512
  headerValues(name: string): string[] {
510
513
  const lower = name.toLowerCase();
511
514
  const edited = this.overrides?.get(lower);
@@ -531,48 +534,48 @@ export class Response {
531
534
  this.overrides = overridden(this.overrides, op, lower, value, current);
532
535
  }
533
536
 
534
- /** Sets the response header `name` to `value`, replacing any values,
535
- * before it is written to the client. Not for the headers
536
- * {@link Request.setHeader} may not set. */
537
+ /**
538
+ * Sets response header `name` to `value`, replacing any existing values,
539
+ * before the response is sent to the client. Rejects the same reserved
540
+ * headers as {@link Request.setHeader}.
541
+ */
537
542
  setHeader(name: string, value: string): void {
538
543
  this.edit('setHeader', SET, name, String(value));
539
544
  }
540
545
 
541
- /** Adds `value` to the response header `name`. */
546
+ /** Appends `value` to response header `name`. */
542
547
  addHeader(name: string, value: string): void {
543
548
  this.edit('addHeader', ADD, name, String(value));
544
549
  }
545
550
 
546
- /** Removes the response header `name`. */
551
+ /** Removes response header `name`. */
547
552
  delHeader(name: string): void {
548
553
  this.edit('delHeader', REMOVE, name, '');
549
554
  }
550
555
  }
551
556
 
552
- /** A request the gateway is done with, for `onComplete`. */
557
+ /** Summary of a completed request passed to `onComplete`. */
553
558
  export interface Completed {
554
- /** The request, with the headers as they were forwarded, and the state
555
- * its earlier hooks kept. */
559
+ /** The request with its forwarded headers and the `state` stored by earlier hooks. */
556
560
  readonly request: Request;
557
- /** The status the client was answered with; undefined when it was never
558
- * answered, as when it disconnected first. */
561
+ /** HTTP status code sent to the client, or `undefined` if no response was sent (e.g. client disconnected). */
559
562
  readonly status: number | undefined;
560
- /** The name of the Middleware that answered the request, or `"gateway"`
561
- * when the gateway did, as for a route it does not expose; undefined when
562
- * the service answered. */
563
+ /**
564
+ * Name of the middleware that rejected the request, `"gateway"` if the
565
+ * gateway rejected it (e.g. unexposed route), or `undefined` if the service
566
+ * handled it.
567
+ */
563
568
  readonly rejectedBy: string | undefined;
564
- /** Why the request failed, if it did: a hook that failed, or a service
565
- * that could not be reached. */
569
+ /** Error message if the request failed due to a hook error or unreachable service. */
566
570
  readonly error: string | undefined;
567
- /** When the gateway received the request. */
571
+ /** Timestamp when the gateway received the request. */
568
572
  readonly received: Date;
569
- /** How long the gateway took to answer it, in milliseconds. */
573
+ /** Total time spent by the gateway handling the request, in milliseconds. */
570
574
  readonly durationMs: number;
571
- /** How long the service took, in milliseconds, if the request was
572
- * forwarded. */
575
+ /** Time spent waiting for the upstream service, in milliseconds (if forwarded). */
573
576
  readonly upstreamDurationMs: number | undefined;
574
- /** The bytes of the request's body. */
577
+ /** Size of the request body in bytes. */
575
578
  readonly requestBodySize: number;
576
- /** The bytes of the response's body. */
579
+ /** Size of the response body in bytes. */
577
580
  readonly responseBodySize: number;
578
581
  }