@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.
- package/README.md +144 -90
- package/app_meta.ts +56 -54
- package/config/config.test.ts +47 -13
- package/config/config.ts +60 -57
- package/config/secrets.ts +47 -50
- package/config/value.ts +23 -26
- package/cron/mod.test.ts +12 -0
- package/cron/mod.ts +37 -22
- package/dist/app_meta.d.ts +52 -50
- package/dist/app_meta.d.ts.map +1 -1
- package/dist/app_meta.js +2 -2
- package/dist/app_meta.js.map +1 -1
- package/dist/config/config.d.ts +58 -55
- package/dist/config/config.d.ts.map +1 -1
- package/dist/config/config.js +8 -9
- package/dist/config/config.js.map +1 -1
- package/dist/config/secrets.d.ts +45 -50
- package/dist/config/secrets.d.ts.map +1 -1
- package/dist/config/secrets.js +11 -9
- package/dist/config/secrets.js.map +1 -1
- package/dist/config/value.d.ts +20 -23
- package/dist/config/value.d.ts.map +1 -1
- package/dist/config/value.js +20 -23
- package/dist/config/value.js.map +1 -1
- package/dist/cron/mod.d.ts +34 -20
- package/dist/cron/mod.d.ts.map +1 -1
- package/dist/cron/mod.js +13 -13
- package/dist/cron/mod.js.map +1 -1
- package/dist/frontends/mod.d.ts +20 -19
- package/dist/frontends/mod.d.ts.map +1 -1
- package/dist/frontends/mod.js +20 -19
- package/dist/frontends/mod.js.map +1 -1
- package/dist/gateway/errors.d.ts +28 -30
- package/dist/gateway/errors.d.ts.map +1 -1
- package/dist/gateway/errors.js +29 -31
- package/dist/gateway/errors.js.map +1 -1
- package/dist/gateway/match.d.ts +54 -54
- package/dist/gateway/match.d.ts.map +1 -1
- package/dist/gateway/match.js +56 -56
- package/dist/gateway/match.js.map +1 -1
- package/dist/gateway/middleware.d.ts +62 -64
- package/dist/gateway/middleware.d.ts.map +1 -1
- package/dist/gateway/middleware.js +1 -1
- package/dist/gateway/middleware.js.map +1 -1
- package/dist/gateway/mod.d.ts +64 -67
- package/dist/gateway/mod.d.ts.map +1 -1
- package/dist/gateway/mod.js +72 -76
- package/dist/gateway/mod.js.map +1 -1
- package/dist/gateway/request.d.ts +138 -134
- package/dist/gateway/request.d.ts.map +1 -1
- package/dist/gateway/request.js +117 -108
- package/dist/gateway/request.js.map +1 -1
- package/dist/gateway/serve.d.ts +29 -34
- package/dist/gateway/serve.d.ts.map +1 -1
- package/dist/gateway/serve.js +51 -56
- package/dist/gateway/serve.js.map +1 -1
- package/dist/gateways/mod.d.ts +23 -21
- package/dist/gateways/mod.d.ts.map +1 -1
- package/dist/gateways/mod.js +25 -23
- package/dist/gateways/mod.js.map +1 -1
- package/dist/internal/otel/http.d.ts +4 -2
- package/dist/internal/otel/http.d.ts.map +1 -1
- package/dist/internal/otel/http.js +32 -5
- package/dist/internal/otel/http.js.map +1 -1
- package/dist/internal/runtime/napi/napi.cjs +6 -5
- package/dist/internal/runtime/napi/napi.d.cts +58 -16
- package/dist/internal/runtime/shutdown.d.ts +75 -57
- package/dist/internal/runtime/shutdown.d.ts.map +1 -1
- package/dist/internal/runtime/shutdown.js +88 -84
- package/dist/internal/runtime/shutdown.js.map +1 -1
- package/dist/log/logger.d.ts +43 -37
- package/dist/log/logger.d.ts.map +1 -1
- package/dist/log/logger.js +26 -25
- package/dist/log/logger.js.map +1 -1
- package/dist/metrics/mod.d.ts +133 -114
- package/dist/metrics/mod.d.ts.map +1 -1
- package/dist/metrics/mod.js +109 -101
- package/dist/metrics/mod.js.map +1 -1
- package/dist/otel.d.ts +6 -3
- package/dist/otel.d.ts.map +1 -1
- package/dist/otel.js +6 -3
- package/dist/otel.js.map +1 -1
- package/dist/pubsub/subscription.d.ts +88 -116
- package/dist/pubsub/subscription.d.ts.map +1 -1
- package/dist/pubsub/subscription.js +14 -17
- package/dist/pubsub/subscription.js.map +1 -1
- package/dist/pubsub/topic.d.ts +86 -113
- package/dist/pubsub/topic.d.ts.map +1 -1
- package/dist/pubsub/topic.js +68 -82
- package/dist/pubsub/topic.js.map +1 -1
- package/dist/redaction/mod.d.ts +62 -0
- package/dist/redaction/mod.d.ts.map +1 -0
- package/dist/redaction/mod.js +102 -0
- package/dist/redaction/mod.js.map +1 -0
- package/dist/reqctx/mod.d.ts +83 -86
- package/dist/reqctx/mod.d.ts.map +1 -1
- package/dist/reqctx/mod.js +39 -41
- package/dist/reqctx/mod.js.map +1 -1
- package/dist/services/index.d.ts +76 -75
- package/dist/services/index.d.ts.map +1 -1
- package/dist/services/index.js +73 -71
- package/dist/services/index.js.map +1 -1
- package/dist/shutdown/mod.d.ts +26 -25
- package/dist/shutdown/mod.d.ts.map +1 -1
- package/dist/shutdown/mod.js +26 -25
- package/dist/shutdown/mod.js.map +1 -1
- package/dist/storage/clickhouse/database.d.ts +113 -115
- package/dist/storage/clickhouse/database.d.ts.map +1 -1
- package/dist/storage/clickhouse/database.js +65 -79
- package/dist/storage/clickhouse/database.js.map +1 -1
- package/dist/storage/clickhouse/errors.d.ts +25 -26
- package/dist/storage/clickhouse/errors.d.ts.map +1 -1
- package/dist/storage/clickhouse/errors.js +15 -17
- package/dist/storage/clickhouse/errors.js.map +1 -1
- package/dist/storage/clickhouse/mod.d.ts +10 -10
- package/dist/storage/clickhouse/mod.js +10 -10
- package/dist/storage/objects/bucket.d.ts +230 -203
- package/dist/storage/objects/bucket.d.ts.map +1 -1
- package/dist/storage/objects/bucket.js +146 -106
- package/dist/storage/objects/bucket.js.map +1 -1
- package/dist/storage/objects/error.d.ts +4 -4
- package/dist/storage/objects/error.js +4 -4
- package/dist/storage/objects/mod.d.ts +1 -1
- package/dist/storage/objects/mod.d.ts.map +1 -1
- package/dist/storage/objects/mod.js.map +1 -1
- package/dist/storage/objects/refs.d.ts +2 -5
- package/dist/storage/objects/refs.d.ts.map +1 -1
- package/dist/storage/objects/refs.js.map +1 -1
- package/dist/storage/postgres/database.d.ts +124 -149
- package/dist/storage/postgres/database.d.ts.map +1 -1
- package/dist/storage/postgres/database.js +98 -135
- package/dist/storage/postgres/database.js.map +1 -1
- package/dist/storage/postgres/errors.d.ts +42 -48
- package/dist/storage/postgres/errors.d.ts.map +1 -1
- package/dist/storage/postgres/errors.js +44 -51
- package/dist/storage/postgres/errors.js.map +1 -1
- package/dist/storage/postgres/interval.d.ts +19 -19
- package/dist/storage/postgres/interval.d.ts.map +1 -1
- package/dist/storage/postgres/interval.js +21 -21
- package/dist/storage/postgres/interval.js.map +1 -1
- package/dist/storage/redis/bitfield.d.ts +48 -23
- package/dist/storage/redis/bitfield.d.ts.map +1 -1
- package/dist/storage/redis/bitfield.js +38 -19
- package/dist/storage/redis/bitfield.js.map +1 -1
- package/dist/storage/redis/command.d.ts +16 -12
- package/dist/storage/redis/command.d.ts.map +1 -1
- package/dist/storage/redis/command.js +16 -12
- package/dist/storage/redis/command.js.map +1 -1
- package/dist/storage/redis/commands.d.ts +99 -100
- package/dist/storage/redis/commands.d.ts.map +1 -1
- package/dist/storage/redis/commands.js +84 -85
- package/dist/storage/redis/commands.js.map +1 -1
- package/dist/storage/redis/database.d.ts +138 -122
- package/dist/storage/redis/database.d.ts.map +1 -1
- package/dist/storage/redis/database.js +120 -101
- package/dist/storage/redis/database.js.map +1 -1
- package/dist/storage/redis/mod.d.ts +52 -48
- package/dist/storage/redis/mod.d.ts.map +1 -1
- package/dist/storage/redis/mod.js +52 -48
- package/dist/storage/redis/mod.js.map +1 -1
- package/dist/storage/redis/options.d.ts +26 -24
- package/dist/storage/redis/options.d.ts.map +1 -1
- package/dist/storage/redis/options.js.map +1 -1
- package/dist/storage/redis/pubsub.d.ts +16 -16
- package/dist/storage/redis/pubsub.d.ts.map +1 -1
- package/dist/storage/redis/pubsub.js +14 -14
- package/dist/storage/redis/pubsub.js.map +1 -1
- package/dist/storage/redis/reply.d.ts +80 -72
- package/dist/storage/redis/reply.d.ts.map +1 -1
- package/dist/storage/redis/reply.js +53 -46
- package/dist/storage/redis/reply.js.map +1 -1
- package/dist/storage/redis/scan.d.ts +4 -4
- package/dist/storage/redis/scan.d.ts.map +1 -1
- package/dist/storage/redis/scan.js +6 -6
- package/dist/storage/redis/scan.js.map +1 -1
- package/dist/storage/redis/script.d.ts +28 -21
- package/dist/storage/redis/script.d.ts.map +1 -1
- package/dist/storage/redis/script.js +29 -22
- package/dist/storage/redis/script.js.map +1 -1
- package/dist/storage/redis/target.d.ts +17 -1
- package/dist/storage/redis/target.d.ts.map +1 -1
- package/dist/storage/redis/target.js +30 -1
- package/dist/storage/redis/target.js.map +1 -1
- package/dist/storage/redis/wire.d.ts +6 -6
- package/dist/storage/redis/wire.d.ts.map +1 -1
- package/dist/storage/redis/wire.js +6 -6
- package/dist/storage/redis/wire.js.map +1 -1
- package/dist/testing/mod.d.ts +83 -88
- package/dist/testing/mod.d.ts.map +1 -1
- package/dist/testing/mod.js +75 -81
- package/dist/testing/mod.js.map +1 -1
- package/dist/testing/playwright.d.ts +28 -32
- package/dist/testing/playwright.d.ts.map +1 -1
- package/dist/testing/playwright.js +22 -27
- package/dist/testing/playwright.js.map +1 -1
- package/dist/testing/puppeteer.d.ts +11 -12
- package/dist/testing/puppeteer.d.ts.map +1 -1
- package/dist/testing/puppeteer.js +17 -18
- package/dist/testing/puppeteer.js.map +1 -1
- package/dist/testing/system.d.ts +72 -90
- package/dist/testing/system.d.ts.map +1 -1
- package/dist/testing/system.js +90 -107
- package/dist/testing/system.js.map +1 -1
- package/dist/tracing/http.d.ts +2 -1
- package/dist/tracing/http.d.ts.map +1 -1
- package/dist/tracing/http.js +2 -1
- package/dist/tracing/http.js.map +1 -1
- package/dist/tracing/node.d.ts.map +1 -1
- package/dist/tracing/node.js +1 -1
- package/dist/tracing/node.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/frontends/mod.test.ts +6 -2
- package/frontends/mod.ts +20 -19
- package/gateway/errors.ts +30 -32
- package/gateway/match.ts +61 -57
- package/gateway/middleware.ts +62 -64
- package/gateway/mod.ts +73 -76
- package/gateway/request.ts +143 -140
- package/gateway/serve.ts +58 -63
- package/gateways/mod.ts +25 -23
- package/internal/otel/http.test.ts +50 -0
- package/internal/otel/http.ts +40 -5
- package/internal/runtime/napi/napi.cjs +6 -5
- package/internal/runtime/napi/napi.d.cts +58 -16
- package/internal/runtime/shutdown.ts +113 -101
- package/log/logger.test.ts +8 -0
- package/log/logger.ts +48 -45
- package/metrics/mod.ts +138 -122
- package/otel.ts +6 -3
- package/package.json +6 -1
- package/pubsub/subscription.ts +93 -123
- package/pubsub/topic.ts +96 -130
- package/redaction/bridge.test.ts +99 -0
- package/redaction/mod.test.ts +188 -0
- package/redaction/mod.ts +226 -0
- package/reqctx/mod.ts +83 -86
- package/services/index.ts +78 -78
- package/shutdown/mod.ts +26 -25
- package/storage/clickhouse/database.ts +115 -129
- package/storage/clickhouse/errors.ts +27 -29
- package/storage/clickhouse/mod.ts +10 -10
- package/storage/objects/bucket.test.ts +168 -16
- package/storage/objects/bucket.ts +336 -247
- package/storage/objects/error.ts +4 -4
- package/storage/objects/mod.ts +1 -0
- package/storage/objects/refs.ts +2 -5
- package/storage/postgres/database.ts +131 -164
- package/storage/postgres/errors.ts +45 -52
- package/storage/postgres/interval.ts +21 -21
- package/storage/redis/bitfield.ts +51 -26
- package/storage/redis/command.ts +16 -12
- package/storage/redis/commands.ts +99 -100
- package/storage/redis/database.ts +145 -123
- package/storage/redis/mod.ts +52 -48
- package/storage/redis/options.ts +26 -24
- package/storage/redis/pubsub.ts +14 -14
- package/storage/redis/redis.test.ts +17 -2
- package/storage/redis/reply.ts +85 -77
- package/storage/redis/scan.ts +6 -6
- package/storage/redis/script.ts +29 -22
- package/storage/redis/target.ts +34 -1
- package/storage/redis/wire.ts +8 -8
- package/testing/mod.ts +99 -105
- package/testing/playwright.ts +32 -37
- package/testing/puppeteer.ts +22 -23
- package/testing/system.ts +117 -133
- package/tracing/http.ts +2 -1
- package/tracing/native.test.ts +158 -8
- package/tracing/node.ts +1 -0
package/gateway/request.ts
CHANGED
|
@@ -6,8 +6,8 @@ import * as reqfields from '../internal/reqfields';
|
|
|
6
6
|
import {Field} from '../reqctx/mod';
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
10
|
-
*
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
31
|
+
/** Native response header view. @internal */
|
|
32
32
|
export interface NativeHeaders {
|
|
33
33
|
values(name: string): string[];
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
/**
|
|
37
|
-
*
|
|
38
|
-
* combined with
|
|
39
|
-
*
|
|
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
|
|
53
|
+
/** A {@link Hook} flag or bitwise-OR combination of flags. */
|
|
53
54
|
export type Hooks = number;
|
|
54
55
|
|
|
55
|
-
/**
|
|
56
|
+
/** Allowed value type for request span attributes. */
|
|
56
57
|
export type AttributeValue = string | boolean | number;
|
|
57
58
|
|
|
58
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
*
|
|
85
|
-
* headers,
|
|
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
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
141
|
-
*
|
|
142
|
-
*
|
|
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
|
|
143
|
+
/** Never aborts; used as the signal for untimed hooks. */
|
|
146
144
|
const NEVER = new AbortController().signal;
|
|
147
145
|
|
|
148
146
|
/**
|
|
149
|
-
*
|
|
150
|
-
*
|
|
151
|
-
*
|
|
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
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
*
|
|
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
|
-
/**
|
|
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
|
|
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
|
|
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
|
|
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
|
-
/**
|
|
190
|
+
/** Uppercase HTTP method (e.g. `"GET"`). */
|
|
195
191
|
get method(): string {
|
|
196
192
|
return this.view?.method ?? '';
|
|
197
193
|
}
|
|
198
194
|
|
|
199
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
210
|
+
/** Matched route template (e.g. `/users/{id}`). */
|
|
216
211
|
get route(): string {
|
|
217
212
|
return this.view?.route ?? '';
|
|
218
213
|
}
|
|
219
214
|
|
|
220
|
-
/**
|
|
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
|
|
228
|
-
* `onResponse`
|
|
229
|
-
*
|
|
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
|
-
/**
|
|
237
|
-
*
|
|
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
|
-
/**
|
|
244
|
-
*
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
258
|
-
*
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
270
|
-
*
|
|
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
|
-
/**
|
|
276
|
-
*
|
|
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
|
|
312
|
-
*
|
|
313
|
-
* Encore
|
|
314
|
-
*
|
|
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
|
-
/**
|
|
321
|
-
*
|
|
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
|
-
/**
|
|
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
|
-
*
|
|
333
|
-
* tenant
|
|
334
|
-
*
|
|
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)}
|
|
354
|
-
'
|
|
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
|
-
/**
|
|
361
|
-
*
|
|
362
|
-
* `req.skip(Hook.Response | Hook.Complete)
|
|
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
|
|
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
|
-
*
|
|
375
|
-
*
|
|
376
|
-
*
|
|
377
|
-
*
|
|
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
|
|
387
|
-
*
|
|
388
|
-
*
|
|
389
|
-
* `field.get()
|
|
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`.
|
|
407
|
-
*
|
|
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
|
|
410
|
-
* {@link Field}.
|
|
411
|
-
* @throws RangeError
|
|
412
|
-
* or 1 KiB for
|
|
413
|
-
*
|
|
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
|
-
/**
|
|
428
|
-
*
|
|
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
|
-
*
|
|
438
|
-
* user `onRequest`
|
|
439
|
-
*
|
|
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
|
-
*
|
|
442
|
-
*
|
|
443
|
-
*
|
|
444
|
-
*
|
|
445
|
-
*
|
|
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
|
|
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
|
-
*
|
|
479
|
-
*
|
|
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
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
535
|
-
*
|
|
536
|
-
*
|
|
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
|
-
/**
|
|
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
|
|
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
|
-
/**
|
|
557
|
+
/** Summary of a completed request passed to `onComplete`. */
|
|
553
558
|
export interface Completed {
|
|
554
|
-
/** The request
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
561
|
-
*
|
|
562
|
-
*
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
571
|
+
/** Timestamp when the gateway received the request. */
|
|
568
572
|
readonly received: Date;
|
|
569
|
-
/**
|
|
573
|
+
/** Total time spent by the gateway handling the request, in milliseconds. */
|
|
570
574
|
readonly durationMs: number;
|
|
571
|
-
/**
|
|
572
|
-
* forwarded. */
|
|
575
|
+
/** Time spent waiting for the upstream service, in milliseconds (if forwarded). */
|
|
573
576
|
readonly upstreamDurationMs: number | undefined;
|
|
574
|
-
/**
|
|
577
|
+
/** Size of the request body in bytes. */
|
|
575
578
|
readonly requestBodySize: number;
|
|
576
|
-
/**
|
|
579
|
+
/** Size of the response body in bytes. */
|
|
577
580
|
readonly responseBodySize: number;
|
|
578
581
|
}
|