@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/dist/gateway/mod.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
2
|
+
* Gateway middleware for authenticating, authorizing, rate-limiting, logging,
|
|
3
|
+
* or metering public requests before they reach a service and after responses
|
|
4
|
+
* return.
|
|
5
5
|
*
|
|
6
|
-
* A gateway with
|
|
7
|
-
* section in
|
|
8
|
-
* `encore.gateway.json`
|
|
9
|
-
* service
|
|
6
|
+
* A gateway with custom middleware is hosted by a service that includes a
|
|
7
|
+
* `gateway` section in its `encore.service.json`. The section accepts the same
|
|
8
|
+
* options as `encore.gateway.json` except `name`, since the gateway takes the
|
|
9
|
+
* name of its hosting service:
|
|
10
10
|
*
|
|
11
11
|
* ```json
|
|
12
12
|
* {
|
|
@@ -17,9 +17,9 @@
|
|
|
17
17
|
* }
|
|
18
18
|
* ```
|
|
19
19
|
*
|
|
20
|
-
* The service's
|
|
21
|
-
* else.
|
|
22
|
-
* and configs, and
|
|
20
|
+
* The service's entry point serves the gateway with its middleware and nothing
|
|
21
|
+
* else. Like any Encore service, it can declare its own secrets, caches,
|
|
22
|
+
* databases, Pub/Sub topics, and configs, and call other services.
|
|
23
23
|
*
|
|
24
24
|
* ```typescript
|
|
25
25
|
* import * as gateway from "@encore.dev/sdk/gateway";
|
|
@@ -43,64 +43,62 @@
|
|
|
43
43
|
*
|
|
44
44
|
* ## Hooks
|
|
45
45
|
*
|
|
46
|
-
* A {@link Middleware} is
|
|
46
|
+
* A {@link Middleware} is invoked only for the hooks it defines:
|
|
47
47
|
*
|
|
48
|
-
* - `onRequest
|
|
49
|
-
*
|
|
50
|
-
* - `onRequestBody` and `onResponseBody
|
|
51
|
-
*
|
|
52
|
-
* - `onResponse
|
|
53
|
-
*
|
|
54
|
-
*
|
|
48
|
+
* - `onRequest`: runs before the request is forwarded; can allow it through
|
|
49
|
+
* (optionally modified) or answer it directly.
|
|
50
|
+
* - `onRequestBody` and `onResponseBody`: observe each chunk of the request or
|
|
51
|
+
* response body as it streams.
|
|
52
|
+
* - `onResponse`: runs before the response is sent to the client; can edit
|
|
53
|
+
* response headers.
|
|
54
|
+
* - `onComplete`: runs asynchronously after the request finishes; ideal for
|
|
55
|
+
* logging, metrics, or billing.
|
|
55
56
|
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
* request's headers, query parameters and cookies; each reaches JavaScript
|
|
61
|
-
* only as the hook reads it.
|
|
57
|
+
* Encore evaluates each middleware's `match` condition before dispatching to
|
|
58
|
+
* JavaScript, so unmatched requests incur no JS overhead, and a middleware
|
|
59
|
+
* that only defines `onComplete` adds no latency to the request path. Headers,
|
|
60
|
+
* query parameters, and cookies are passed into JavaScript lazily when read.
|
|
62
61
|
*
|
|
63
|
-
* Middleware
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
62
|
+
* Middleware only sees public requests routed by the gateway. Service-to-service
|
|
63
|
+
* calls, Pub/Sub pushes, and unexposed paths are handled internally by Encore,
|
|
64
|
+
* as are CORS preflight requests (unless CORS is disabled via `"cors": false`,
|
|
65
|
+
* in which case preflights are routed like any other request).
|
|
67
66
|
*
|
|
68
67
|
* ## Answering a request
|
|
69
68
|
*
|
|
70
|
-
* `onRequest` returns nothing to
|
|
71
|
-
* {@link Rejection} to answer it
|
|
72
|
-
*
|
|
73
|
-
* any
|
|
74
|
-
*
|
|
75
|
-
*
|
|
69
|
+
* `onRequest` returns nothing (`void` / `undefined`) to allow the request
|
|
70
|
+
* through, or returns or throws a {@link Rejection} to answer it immediately.
|
|
71
|
+
* Use {@link reject} to respond with a standard Encore error, or {@link respond}
|
|
72
|
+
* for any custom HTTP response (such as a redirect). Any other thrown error or
|
|
73
|
+
* timeout fails the hook, and the middleware's `onError` policy decides what
|
|
74
|
+
* happens next: `"failClosed"` rejects the request, while `"failOpen"` lets it
|
|
75
|
+
* through.
|
|
76
76
|
*
|
|
77
77
|
* ## Request Context
|
|
78
78
|
*
|
|
79
|
-
* `onRequest`
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
* forwarded.
|
|
79
|
+
* `onRequest` can populate Request Context fields (`@encore.dev/sdk/reqctx`)
|
|
80
|
+
* with {@link Request.set}. Subsequent middleware, the target service, and
|
|
81
|
+
* downstream services called with that request context can read them via
|
|
82
|
+
* `field.get()`. Clients cannot forge Request Context fields. Every hook runs
|
|
83
|
+
* inside its request's context and can read fields with either `field.get()`
|
|
84
|
+
* or {@link Request.get} (in `onComplete`, reflecting the forwarded state).
|
|
86
85
|
*
|
|
87
86
|
* ## Concurrency
|
|
88
87
|
*
|
|
89
|
-
* Hooks run on the event loop.
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
* the hook awaits to stop too.
|
|
88
|
+
* Hooks run on the JavaScript event loop. For a single request, a middleware's
|
|
89
|
+
* hooks execute sequentially in lifecycle order; hooks for different requests
|
|
90
|
+
* run concurrently. Avoid blocking the event loop, as doing so stalls other
|
|
91
|
+
* requests. `onRequest` and `onResponse` enforce `timeoutMs` and abort
|
|
92
|
+
* {@link Request.signal} on expiry so awaited work can cancel cleanly.
|
|
95
93
|
*
|
|
96
|
-
*
|
|
97
|
-
* hooks it cannot queue
|
|
98
|
-
* requests
|
|
94
|
+
* If JavaScript falls too far behind incoming traffic, the gateway rejects
|
|
95
|
+
* hooks it cannot queue (handled according to `onError`) rather than buffering
|
|
96
|
+
* requests unboundedly.
|
|
99
97
|
*
|
|
100
|
-
* While a hook
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
* fields
|
|
98
|
+
* While a hook executes, the request's trace context and Request Context are
|
|
99
|
+
* active: newly created spans attach to the trace, `field.get()` reads the
|
|
100
|
+
* request's fields, and outgoing service calls propagate both (including any
|
|
101
|
+
* fields set so far in `onRequest`).
|
|
104
102
|
*
|
|
105
103
|
* @module
|
|
106
104
|
*/
|
|
@@ -112,19 +110,18 @@ export { Hook, Request, Response } from './request.js';
|
|
|
112
110
|
export type { AttributeValue, Completed, Hooks } from './request.js';
|
|
113
111
|
export type { Decision, ErrorPolicy, Middleware } from './middleware.js';
|
|
114
112
|
/**
|
|
115
|
-
*
|
|
116
|
-
* Encore shuts the process down. Call
|
|
117
|
-
* entry point.
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
* second SIGTERM
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
* regular expression; or the gateway is already served.
|
|
113
|
+
* Starts serving the gateway hosted by this service with the given `middleware`
|
|
114
|
+
* in order, running until Encore shuts the process down. Call this once at the
|
|
115
|
+
* end of the service's entry point.
|
|
116
|
+
*
|
|
117
|
+
* On the first `SIGTERM`, the gateway stops accepting new connections while
|
|
118
|
+
* draining in-flight hooks; a second `SIGTERM` accelerates shutdown. The
|
|
119
|
+
* returned promise resolves after the gateway stops and all queued hooks have
|
|
120
|
+
* finished, at which point Encore exits the process.
|
|
121
|
+
*
|
|
122
|
+
* Rejects with {@link GatewayError} if the process is not running as an Encore
|
|
123
|
+
* gateway, if any middleware definition or matcher is invalid (such as a
|
|
124
|
+
* duplicate name or invalid regex), or if `serve` has already been called.
|
|
128
125
|
*/
|
|
129
126
|
export declare function serve(...middleware: Middleware[]): Promise<void>;
|
|
130
127
|
//# sourceMappingURL=mod.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../gateway/mod.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../gateway/mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuGG;AAOH,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,cAAc,CAAC;AAS7C,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,YAAY,EAAC,IAAI,EAAE,OAAO,EAAC,MAAM,SAAS,CAAC;AAC3C,OAAO,EACL,OAAO,EACP,YAAY,EACZ,SAAS,EACT,UAAU,EACV,MAAM,EACN,OAAO,EACP,KAAK,OAAO,GACb,MAAM,UAAU,CAAC;AAClB,OAAO,EAAC,IAAI,EAAE,OAAO,EAAE,QAAQ,EAAC,MAAM,WAAW,CAAC;AAClD,YAAY,EAAC,cAAc,EAAE,SAAS,EAAE,KAAK,EAAC,MAAM,WAAW,CAAC;AAChE,YAAY,EAAC,QAAQ,EAAE,WAAW,EAAE,UAAU,EAAC,MAAM,cAAc,CAAC;AAiBpE;;;;;;;;;;;;;GAaG;AACH,wBAAsB,KAAK,CAAC,GAAG,UAAU,EAAE,UAAU,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CA4CtE"}
|
package/dist/gateway/mod.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
2
|
+
* Gateway middleware for authenticating, authorizing, rate-limiting, logging,
|
|
3
|
+
* or metering public requests before they reach a service and after responses
|
|
4
|
+
* return.
|
|
5
5
|
*
|
|
6
|
-
* A gateway with
|
|
7
|
-
* section in
|
|
8
|
-
* `encore.gateway.json`
|
|
9
|
-
* service
|
|
6
|
+
* A gateway with custom middleware is hosted by a service that includes a
|
|
7
|
+
* `gateway` section in its `encore.service.json`. The section accepts the same
|
|
8
|
+
* options as `encore.gateway.json` except `name`, since the gateway takes the
|
|
9
|
+
* name of its hosting service:
|
|
10
10
|
*
|
|
11
11
|
* ```json
|
|
12
12
|
* {
|
|
@@ -17,9 +17,9 @@
|
|
|
17
17
|
* }
|
|
18
18
|
* ```
|
|
19
19
|
*
|
|
20
|
-
* The service's
|
|
21
|
-
* else.
|
|
22
|
-
* and configs, and
|
|
20
|
+
* The service's entry point serves the gateway with its middleware and nothing
|
|
21
|
+
* else. Like any Encore service, it can declare its own secrets, caches,
|
|
22
|
+
* databases, Pub/Sub topics, and configs, and call other services.
|
|
23
23
|
*
|
|
24
24
|
* ```typescript
|
|
25
25
|
* import * as gateway from "@encore.dev/sdk/gateway";
|
|
@@ -43,64 +43,62 @@
|
|
|
43
43
|
*
|
|
44
44
|
* ## Hooks
|
|
45
45
|
*
|
|
46
|
-
* A {@link Middleware} is
|
|
46
|
+
* A {@link Middleware} is invoked only for the hooks it defines:
|
|
47
47
|
*
|
|
48
|
-
* - `onRequest
|
|
49
|
-
*
|
|
50
|
-
* - `onRequestBody` and `onResponseBody
|
|
51
|
-
*
|
|
52
|
-
* - `onResponse
|
|
53
|
-
*
|
|
54
|
-
*
|
|
48
|
+
* - `onRequest`: runs before the request is forwarded; can allow it through
|
|
49
|
+
* (optionally modified) or answer it directly.
|
|
50
|
+
* - `onRequestBody` and `onResponseBody`: observe each chunk of the request or
|
|
51
|
+
* response body as it streams.
|
|
52
|
+
* - `onResponse`: runs before the response is sent to the client; can edit
|
|
53
|
+
* response headers.
|
|
54
|
+
* - `onComplete`: runs asynchronously after the request finishes; ideal for
|
|
55
|
+
* logging, metrics, or billing.
|
|
55
56
|
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
* request's headers, query parameters and cookies; each reaches JavaScript
|
|
61
|
-
* only as the hook reads it.
|
|
57
|
+
* Encore evaluates each middleware's `match` condition before dispatching to
|
|
58
|
+
* JavaScript, so unmatched requests incur no JS overhead, and a middleware
|
|
59
|
+
* that only defines `onComplete` adds no latency to the request path. Headers,
|
|
60
|
+
* query parameters, and cookies are passed into JavaScript lazily when read.
|
|
62
61
|
*
|
|
63
|
-
* Middleware
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
62
|
+
* Middleware only sees public requests routed by the gateway. Service-to-service
|
|
63
|
+
* calls, Pub/Sub pushes, and unexposed paths are handled internally by Encore,
|
|
64
|
+
* as are CORS preflight requests (unless CORS is disabled via `"cors": false`,
|
|
65
|
+
* in which case preflights are routed like any other request).
|
|
67
66
|
*
|
|
68
67
|
* ## Answering a request
|
|
69
68
|
*
|
|
70
|
-
* `onRequest` returns nothing to
|
|
71
|
-
* {@link Rejection} to answer it
|
|
72
|
-
*
|
|
73
|
-
* any
|
|
74
|
-
*
|
|
75
|
-
*
|
|
69
|
+
* `onRequest` returns nothing (`void` / `undefined`) to allow the request
|
|
70
|
+
* through, or returns or throws a {@link Rejection} to answer it immediately.
|
|
71
|
+
* Use {@link reject} to respond with a standard Encore error, or {@link respond}
|
|
72
|
+
* for any custom HTTP response (such as a redirect). Any other thrown error or
|
|
73
|
+
* timeout fails the hook, and the middleware's `onError` policy decides what
|
|
74
|
+
* happens next: `"failClosed"` rejects the request, while `"failOpen"` lets it
|
|
75
|
+
* through.
|
|
76
76
|
*
|
|
77
77
|
* ## Request Context
|
|
78
78
|
*
|
|
79
|
-
* `onRequest`
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
* forwarded.
|
|
79
|
+
* `onRequest` can populate Request Context fields (`@encore.dev/sdk/reqctx`)
|
|
80
|
+
* with {@link Request.set}. Subsequent middleware, the target service, and
|
|
81
|
+
* downstream services called with that request context can read them via
|
|
82
|
+
* `field.get()`. Clients cannot forge Request Context fields. Every hook runs
|
|
83
|
+
* inside its request's context and can read fields with either `field.get()`
|
|
84
|
+
* or {@link Request.get} (in `onComplete`, reflecting the forwarded state).
|
|
86
85
|
*
|
|
87
86
|
* ## Concurrency
|
|
88
87
|
*
|
|
89
|
-
* Hooks run on the event loop.
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
* the hook awaits to stop too.
|
|
88
|
+
* Hooks run on the JavaScript event loop. For a single request, a middleware's
|
|
89
|
+
* hooks execute sequentially in lifecycle order; hooks for different requests
|
|
90
|
+
* run concurrently. Avoid blocking the event loop, as doing so stalls other
|
|
91
|
+
* requests. `onRequest` and `onResponse` enforce `timeoutMs` and abort
|
|
92
|
+
* {@link Request.signal} on expiry so awaited work can cancel cleanly.
|
|
95
93
|
*
|
|
96
|
-
*
|
|
97
|
-
* hooks it cannot queue
|
|
98
|
-
* requests
|
|
94
|
+
* If JavaScript falls too far behind incoming traffic, the gateway rejects
|
|
95
|
+
* hooks it cannot queue (handled according to `onError`) rather than buffering
|
|
96
|
+
* requests unboundedly.
|
|
99
97
|
*
|
|
100
|
-
* While a hook
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
* fields
|
|
98
|
+
* While a hook executes, the request's trace context and Request Context are
|
|
99
|
+
* active: newly created spans attach to the trace, `field.get()` reads the
|
|
100
|
+
* request's fields, and outgoing service calls propagate both (including any
|
|
101
|
+
* fields set so far in `onRequest`).
|
|
104
102
|
*
|
|
105
103
|
* @module
|
|
106
104
|
*/
|
|
@@ -114,37 +112,35 @@ export * as match from './match.js';
|
|
|
114
112
|
export { ErrCode, GatewayError, Rejection, httpStatus, reject, respond, } from './errors.js';
|
|
115
113
|
export { Hook, Request, Response } from './request.js';
|
|
116
114
|
let served = false;
|
|
117
|
-
/**
|
|
118
|
-
* program loads. */
|
|
115
|
+
/** Returns the native gateway bindings loaded by the gateway runtime variant. */
|
|
119
116
|
function native() {
|
|
120
117
|
const n = napi;
|
|
121
118
|
if (typeof n.gatewayServe !== 'function') {
|
|
122
|
-
throw new GatewayError('this process
|
|
123
|
-
'
|
|
124
|
-
'
|
|
119
|
+
throw new GatewayError('this process is not running the gateway variant of the Encore runtime: ' +
|
|
120
|
+
'add a "gateway" section to encore.service.json so Encore starts it as a gateway ' +
|
|
121
|
+
'(with ENCORE_GATEWAY_LISTEN set)');
|
|
125
122
|
}
|
|
126
123
|
return n;
|
|
127
124
|
}
|
|
128
125
|
/**
|
|
129
|
-
*
|
|
130
|
-
* Encore shuts the process down. Call
|
|
131
|
-
* entry point.
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
* second SIGTERM
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
* regular expression; or the gateway is already served.
|
|
126
|
+
* Starts serving the gateway hosted by this service with the given `middleware`
|
|
127
|
+
* in order, running until Encore shuts the process down. Call this once at the
|
|
128
|
+
* end of the service's entry point.
|
|
129
|
+
*
|
|
130
|
+
* On the first `SIGTERM`, the gateway stops accepting new connections while
|
|
131
|
+
* draining in-flight hooks; a second `SIGTERM` accelerates shutdown. The
|
|
132
|
+
* returned promise resolves after the gateway stops and all queued hooks have
|
|
133
|
+
* finished, at which point Encore exits the process.
|
|
134
|
+
*
|
|
135
|
+
* Rejects with {@link GatewayError} if the process is not running as an Encore
|
|
136
|
+
* gateway, if any middleware definition or matcher is invalid (such as a
|
|
137
|
+
* duplicate name or invalid regex), or if `serve` has already been called.
|
|
142
138
|
*/
|
|
143
139
|
export async function serve(...middleware) {
|
|
144
140
|
const checked = check(middleware);
|
|
145
141
|
const gw = native();
|
|
146
142
|
if (served)
|
|
147
|
-
throw new GatewayError('
|
|
143
|
+
throw new GatewayError('gateway.serve can only be called once per process');
|
|
148
144
|
served = true;
|
|
149
145
|
const mws = new Map();
|
|
150
146
|
let listen;
|
|
@@ -164,7 +160,7 @@ export async function serve(...middleware) {
|
|
|
164
160
|
dispatcher.deliver(events);
|
|
165
161
|
}
|
|
166
162
|
catch (err) {
|
|
167
|
-
log.error(err,
|
|
163
|
+
log.error(err, 'failed to dispatch gateway middleware events');
|
|
168
164
|
}
|
|
169
165
|
});
|
|
170
166
|
}
|
|
@@ -176,13 +172,13 @@ export async function serve(...middleware) {
|
|
|
176
172
|
catch (err) {
|
|
177
173
|
throw new GatewayError(err instanceof Error ? err.message : String(err));
|
|
178
174
|
}
|
|
179
|
-
log.debug(`serving
|
|
175
|
+
log.debug(`serving gateway on ${listen}`);
|
|
180
176
|
forceCleanupOnRepeat('SIGTERM');
|
|
181
177
|
await dispatcher.closed;
|
|
182
178
|
await dispatcher.drained();
|
|
183
179
|
const dropped = gw.gatewayDropped();
|
|
184
180
|
if (dropped > 0) {
|
|
185
|
-
log.warn(`dropped ${dropped} body chunks and
|
|
181
|
+
log.warn(`dropped ${dropped} body chunks and completion events because middleware could not keep up`);
|
|
186
182
|
}
|
|
187
183
|
}
|
|
188
184
|
//# sourceMappingURL=mod.js.map
|
package/dist/gateway/mod.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mod.js","sourceRoot":"","sources":["../../gateway/mod.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"mod.js","sourceRoot":"","sources":["../../gateway/mod.ts"],"names":[],"mappings":"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"}
|