@encore.dev/sdk 2.0.0-beta.15 → 2.0.0-beta.17
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 +111 -101
- package/app_meta.ts +56 -54
- package/config/config.ts +60 -57
- package/config/secrets.ts +45 -52
- package/config/value.ts +23 -26
- package/cron/mod.ts +28 -29
- 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 -52
- package/dist/config/secrets.d.ts.map +1 -1
- package/dist/config/secrets.js +8 -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 +26 -27
- package/dist/cron/mod.d.ts.map +1 -1
- package/dist/cron/mod.js +13 -14
- 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/runtime/napi/napi.cjs +1 -1
- package/dist/internal/runtime/napi/napi.d.cts +10 -3
- 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/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 +196 -225
- package/dist/storage/objects/bucket.d.ts.map +1 -1
- package/dist/storage/objects/bucket.js +71 -84
- 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/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/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/runtime/napi/napi.cjs +1 -1
- package/internal/runtime/napi/napi.d.cts +10 -3
- 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 +1 -1
- package/pubsub/subscription.ts +93 -123
- package/pubsub/topic.ts +96 -130
- 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.ts +209 -239
- package/storage/objects/error.ts +4 -4
- 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/reqctx/mod.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Request Context: typed fields that
|
|
3
|
-
* it reaches, such as the user
|
|
2
|
+
* Request Context: typed fields that propagate with a request across every
|
|
3
|
+
* service it reaches, such as the authenticated user.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
* and
|
|
7
|
-
* name must be a string literal:
|
|
5
|
+
* Declare each field once in the app as a module-level `const` with a
|
|
6
|
+
* string-literal name and a value type so Encore's parser can inspect it:
|
|
8
7
|
*
|
|
9
8
|
* ```ts
|
|
10
9
|
* import {Field} from '@encore.dev/sdk/reqctx';
|
|
@@ -17,55 +16,53 @@
|
|
|
17
16
|
* export const currentUser = new Field<User>('user', {pubsub: true});
|
|
18
17
|
* ```
|
|
19
18
|
*
|
|
20
|
-
* Gateway
|
|
21
|
-
* `req.set(currentUser, user)` (`@encore.dev/sdk/gateway`), and
|
|
22
|
-
*
|
|
19
|
+
* Gateway middleware populates fields on incoming requests using
|
|
20
|
+
* `req.set(currentUser, user)` (`@encore.dev/sdk/gateway`), and services read
|
|
21
|
+
* them while handling requests:
|
|
23
22
|
*
|
|
24
23
|
* ```ts
|
|
25
24
|
* const user = currentUser.get();
|
|
26
25
|
* if (user === undefined) throw new Error('not signed in');
|
|
27
26
|
* ```
|
|
28
27
|
*
|
|
29
|
-
*
|
|
28
|
+
* Services can also set or clear fields for a scoped block and any downstream
|
|
30
29
|
* calls it makes:
|
|
31
30
|
*
|
|
32
31
|
* ```ts
|
|
33
32
|
* await currentUser.with({id: 'system', email: ''}, () => fetchInvoices());
|
|
34
33
|
* ```
|
|
35
34
|
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
35
|
+
* Other services (including those written in another language) can bind to a
|
|
36
|
+
* field by name with `Field.named<User>('user')`.
|
|
38
37
|
*
|
|
39
|
-
* ##
|
|
38
|
+
* ## Propagation
|
|
40
39
|
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
* without a global context manager
|
|
40
|
+
* Fields are stored in the OpenTelemetry context alongside trace state, so they
|
|
41
|
+
* automatically follow async execution across `await`, timers, and callbacks
|
|
42
|
+
* (even without a global OpenTelemetry context manager).
|
|
44
43
|
*
|
|
45
|
-
* ##
|
|
44
|
+
* ## Security
|
|
46
45
|
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
* services.
|
|
46
|
+
* External clients cannot forge Request Context fields: Encore strips these
|
|
47
|
+
* headers at application ingress and signs them between services, so services
|
|
48
|
+
* can trust values they read as much as they trust other services in the app.
|
|
51
49
|
*
|
|
52
50
|
* ## Receiving and sending fields
|
|
53
51
|
*
|
|
54
|
-
* {@link extract}
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
* services
|
|
59
|
-
*
|
|
60
|
-
*
|
|
52
|
+
* {@link extract} reads Request Context headers from an incoming request into a
|
|
53
|
+
* context, and {@link Propagator} (composed into the global propagator) handles
|
|
54
|
+
* this automatically for instrumented servers. {@link inject} writes context
|
|
55
|
+
* fields onto outgoing request headers — use it only for calls to other Encore
|
|
56
|
+
* services so internal context is never leaked to third parties. Pub/Sub
|
|
57
|
+
* messages automatically carry any set fields declared with `pubsub: true` into
|
|
58
|
+
* subscriber handlers.
|
|
61
59
|
*
|
|
62
|
-
* ##
|
|
60
|
+
* ## Size limits
|
|
63
61
|
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
* that set it, rather than the request failing some hops later.
|
|
62
|
+
* Each field is serialized as base64url-encoded JSON in its own header and must
|
|
63
|
+
* stay small: at most 4 KiB per field (1 KiB for fields with `pubsub: true`)
|
|
64
|
+
* and 8 KiB across all fields on a request. Exceeding these limits throws
|
|
65
|
+
* immediately when setting the value rather than failing downstream.
|
|
69
66
|
*
|
|
70
67
|
* @module
|
|
71
68
|
*/
|
|
@@ -80,49 +77,51 @@ export type {
|
|
|
80
77
|
HeadersLike,
|
|
81
78
|
} from '../internal/reqfields';
|
|
82
79
|
|
|
83
|
-
/**
|
|
84
|
-
*
|
|
80
|
+
/**
|
|
81
|
+
* Propagation and metadata options for a {@link Field}. Boolean and tag values
|
|
82
|
+
* must be literals so Encore's parser can inspect them statically.
|
|
83
|
+
*/
|
|
85
84
|
export interface FieldConfig {
|
|
86
85
|
/**
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
86
|
+
* Propagate this field on Pub/Sub messages published while it is set, making
|
|
87
|
+
* it available in subscriber handlers. Limits the encoded value to 1 KiB
|
|
88
|
+
* (matching GCP Pub/Sub attribute limits).
|
|
90
89
|
*/
|
|
91
90
|
pubsub?: boolean;
|
|
92
91
|
/**
|
|
93
|
-
*
|
|
94
|
-
* `encore.ctx.<name
|
|
95
|
-
*
|
|
92
|
+
* Attach this field's JSON value to spans started while it is set, under the
|
|
93
|
+
* attribute `encore.ctx.<name>`. Useful for searchable identifiers such as
|
|
94
|
+
* tenant IDs — never enable this for secrets.
|
|
96
95
|
*/
|
|
97
96
|
trace?: boolean;
|
|
98
97
|
/**
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
98
|
+
* Static string-literal tags that classify the field for policy checks
|
|
99
|
+
* (e.g. `{data: "pci"}`). Keys and values may contain lowercase letters,
|
|
100
|
+
* digits, `_`, and `-`, up to 63 characters. Keys must start with a letter
|
|
101
|
+
* and cannot start with the reserved prefix `encore`. At most 32 tags.
|
|
103
102
|
*/
|
|
104
103
|
tags?: Record<string, string>;
|
|
105
104
|
}
|
|
106
105
|
|
|
107
106
|
/**
|
|
108
|
-
* A Request Context field holding a JSON value of type `T`.
|
|
107
|
+
* A typed Request Context field holding a JSON-serializable value of type `T`.
|
|
109
108
|
*
|
|
110
|
-
* Declare
|
|
111
|
-
*
|
|
112
|
-
*
|
|
113
|
-
*
|
|
109
|
+
* Declare fields at module level (`new Field<User>("user")`). Encore extracts a
|
|
110
|
+
* schema from `T` and validates cross-service `Field.named` bindings against
|
|
111
|
+
* it. At runtime, values are decoded directly from JSON without custom
|
|
112
|
+
* deserializers.
|
|
114
113
|
*/
|
|
115
114
|
export class Field<T> {
|
|
116
|
-
/** The name
|
|
115
|
+
/** The name this field was declared with. */
|
|
117
116
|
readonly name: string;
|
|
118
117
|
|
|
119
118
|
/**
|
|
120
|
-
*
|
|
119
|
+
* Declares a Request Context field.
|
|
121
120
|
*
|
|
122
|
-
* @param name -
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
* @param cfg -
|
|
121
|
+
* @param name - Unique kebab-case field name (lowercase letters, digits, and
|
|
122
|
+
* hyphens, starting with a letter and ending with a letter or digit). Must be
|
|
123
|
+
* a string literal for Encore's parser.
|
|
124
|
+
* @param cfg - Propagation and classification options.
|
|
126
125
|
*/
|
|
127
126
|
constructor(name: string, cfg?: FieldConfig) {
|
|
128
127
|
this.name = name;
|
|
@@ -133,11 +132,9 @@ export class Field<T> {
|
|
|
133
132
|
}
|
|
134
133
|
|
|
135
134
|
/**
|
|
136
|
-
*
|
|
137
|
-
* language.
|
|
138
|
-
* declaration's
|
|
139
|
-
* declaration says: carried on Pub/Sub messages, and put on spans, as
|
|
140
|
-
* declared.
|
|
135
|
+
* Binds to a field declared elsewhere in the app (including in another
|
|
136
|
+
* language). Encore validates `T` against the declaration, and the bound
|
|
137
|
+
* field inherits the declaration's `pubsub` and `trace` settings.
|
|
141
138
|
*/
|
|
142
139
|
static named<T>(name: string): Field<T> {
|
|
143
140
|
const field = Object.create(Field.prototype) as Field<T>;
|
|
@@ -149,11 +146,11 @@ export class Field<T> {
|
|
|
149
146
|
}
|
|
150
147
|
|
|
151
148
|
/**
|
|
152
|
-
*
|
|
153
|
-
*
|
|
149
|
+
* Returns this field's value from `ctx` (or the active context if omitted),
|
|
150
|
+
* or `undefined` if unset.
|
|
154
151
|
*
|
|
155
|
-
* Each call
|
|
156
|
-
*
|
|
152
|
+
* Each call returns a freshly decoded value that the caller may safely
|
|
153
|
+
* mutate. Invalid JSON values are treated as unset and logged once.
|
|
157
154
|
*/
|
|
158
155
|
get(ctx?: Context): T | undefined {
|
|
159
156
|
const value = reqfields.current(ctx ?? activeContext())[this.name];
|
|
@@ -167,29 +164,28 @@ export class Field<T> {
|
|
|
167
164
|
}
|
|
168
165
|
|
|
169
166
|
/**
|
|
170
|
-
*
|
|
171
|
-
*
|
|
167
|
+
* Runs `fn` with this field set to `value` in the active context and returns
|
|
168
|
+
* its result (or promise for async functions).
|
|
172
169
|
*
|
|
173
|
-
* @throws TypeError
|
|
174
|
-
* `undefined
|
|
175
|
-
* @throws RangeError
|
|
176
|
-
* or 1 KiB
|
|
177
|
-
*
|
|
170
|
+
* @throws TypeError If `value` cannot be serialized to JSON (e.g. contains a
|
|
171
|
+
* `bigint`, circular reference, or is `undefined`).
|
|
172
|
+
* @throws RangeError If the encoded value exceeds the per-field limit (4 KiB,
|
|
173
|
+
* or 1 KiB when `pubsub` is enabled) or pushes total request fields past
|
|
174
|
+
* 8 KiB.
|
|
178
175
|
*/
|
|
179
176
|
with<R>(value: T, fn: () => R): R {
|
|
180
177
|
return withContext(this.context(value), fn);
|
|
181
178
|
}
|
|
182
179
|
|
|
183
|
-
/**
|
|
184
|
-
* makes. */
|
|
180
|
+
/** Runs `fn` with this field cleared from the active context. */
|
|
185
181
|
without<R>(fn: () => R): R {
|
|
186
182
|
return withContext(this.contextWithout(), fn);
|
|
187
183
|
}
|
|
188
184
|
|
|
189
185
|
/**
|
|
190
|
-
*
|
|
191
|
-
* `value
|
|
192
|
-
* {@link Field.with}
|
|
186
|
+
* Returns a copy of `ctx` (or the active context) with this field set to
|
|
187
|
+
* `value`, for code passing OpenTelemetry contexts explicitly. Throws the
|
|
188
|
+
* same errors as {@link Field.with}.
|
|
193
189
|
*/
|
|
194
190
|
context(value: T, ctx?: Context): Context {
|
|
195
191
|
let encoded: string;
|
|
@@ -211,21 +207,22 @@ export class Field<T> {
|
|
|
211
207
|
}
|
|
212
208
|
}
|
|
213
209
|
|
|
214
|
-
/**
|
|
210
|
+
/** Returns a copy of `ctx` (or the active context) with this field removed. */
|
|
215
211
|
contextWithout(ctx?: Context): Context {
|
|
216
212
|
return reqfields.without(this.name, ctx ?? activeContext());
|
|
217
213
|
}
|
|
218
214
|
|
|
219
|
-
/** `Field("name")`. */
|
|
215
|
+
/** Returns `Field("name")`. */
|
|
220
216
|
toString(): string {
|
|
221
217
|
return `Field(${JSON.stringify(this.name)})`;
|
|
222
218
|
}
|
|
223
219
|
}
|
|
224
220
|
|
|
225
221
|
/**
|
|
226
|
-
*
|
|
227
|
-
*
|
|
228
|
-
*
|
|
222
|
+
* Extracts Request Context fields from incoming request `headers` into a copy
|
|
223
|
+
* of `ctx` (or the active context), replacing any previously held fields.
|
|
224
|
+
* Reads the first value of each field header case-insensitively. Returns the
|
|
225
|
+
* base context unchanged if no fields are present.
|
|
229
226
|
*/
|
|
230
227
|
export function extract(
|
|
231
228
|
headers: reqfields.HeaderSource,
|
|
@@ -239,11 +236,11 @@ export function extract(
|
|
|
239
236
|
}
|
|
240
237
|
|
|
241
238
|
/**
|
|
242
|
-
*
|
|
243
|
-
*
|
|
239
|
+
* Writes the Request Context fields from `ctx` (or the active context) onto
|
|
240
|
+
* outgoing request `headers`, replacing any existing field headers.
|
|
244
241
|
*
|
|
245
|
-
*
|
|
246
|
-
*
|
|
242
|
+
* Only call this for requests to other Encore services in the same app so
|
|
243
|
+
* internal context is not sent to third parties.
|
|
247
244
|
*/
|
|
248
245
|
export function inject(headers: reqfields.HeaderSink, ctx?: Context): void {
|
|
249
246
|
reqfields.toHeaders(headers, ctx ?? activeContext());
|
package/services/index.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Calling
|
|
2
|
+
* Calling other services within your Encore application.
|
|
3
3
|
*
|
|
4
|
-
* {@link client}
|
|
5
|
-
* service
|
|
4
|
+
* {@link client} creates a client for a target service whose `fetch` method
|
|
5
|
+
* accepts service-relative paths:
|
|
6
6
|
*
|
|
7
7
|
* ```ts
|
|
8
8
|
* import {client} from '@encore.dev/sdk/services';
|
|
@@ -16,17 +16,19 @@
|
|
|
16
16
|
* }
|
|
17
17
|
* ```
|
|
18
18
|
*
|
|
19
|
-
*
|
|
20
|
-
* trace and Request Context
|
|
21
|
-
* to
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
19
|
+
* Calls made with {@link client} or this module's {@link fetch} automatically
|
|
20
|
+
* propagate the active trace context and Request Context
|
|
21
|
+
* (`@encore.dev/sdk/reqctx`) to other services in the app. Requests to external
|
|
22
|
+
* URLs never receive Request Context headers so internal context is not leaked
|
|
23
|
+
* to third parties. Pass {@link fetch} to any HTTP library or generated client
|
|
24
|
+
* that accepts a custom fetch implementation.
|
|
25
25
|
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
26
|
+
* ## Generated clients
|
|
27
|
+
*
|
|
28
|
+
* When a service lists another service in `encore.service.json` under
|
|
29
|
+
* `clients`, Encore generates typed clients under `encore_gen/clients` (one
|
|
30
|
+
* module per service and API). Each endpoint is exported as a plain function,
|
|
31
|
+
* so no manual client setup is needed:
|
|
30
32
|
*
|
|
31
33
|
* ```ts
|
|
32
34
|
* import {catalog} from "./encore_gen/clients/index.js";
|
|
@@ -36,14 +38,14 @@
|
|
|
36
38
|
* const stuff = await catalog.grpc.stuffService.doStuff({});
|
|
37
39
|
* ```
|
|
38
40
|
*
|
|
39
|
-
* Importing the index
|
|
40
|
-
*
|
|
41
|
-
*
|
|
41
|
+
* Importing the top-level index is lightweight — gRPC clients lazily load
|
|
42
|
+
* connect-es on their first call. You can also deep-import a single API:
|
|
43
|
+
* `import * as openapi from "./encore_gen/clients/catalog/openapi.js"`.
|
|
42
44
|
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
45
|
+
* gRPC clients use protobuf-es messages generated in `encore_gen/protos` (one
|
|
46
|
+
* module per `.proto` file, shared across all clients of the service). Calls
|
|
47
|
+
* accept either a message instance or a plain init object (like protobuf-es's
|
|
48
|
+
* `create`) and return a message:
|
|
47
49
|
*
|
|
48
50
|
* ```ts
|
|
49
51
|
* import {create} from "@bufbuild/protobuf";
|
|
@@ -54,13 +56,14 @@
|
|
|
54
56
|
* const stuff = await catalog.grpc.stuffService.doStuff({id: "x", note});
|
|
55
57
|
* ```
|
|
56
58
|
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
59
|
+
* ## Error handling
|
|
60
|
+
*
|
|
61
|
+
* Failed generated calls throw a {@link CallError} regardless of protocol. Its
|
|
62
|
+
* `code` reflects the callee's response and is not automatically forwarded to
|
|
63
|
+
* your own callers — an uncaught `CallError` in an `@encore.dev/framework`
|
|
64
|
+
* handler becomes an internal error just like any non-`APIError` exception.
|
|
65
|
+
* Convert it explicitly when appropriate, or use {@link unlessNotFound} to turn
|
|
66
|
+
* `not_found` errors into `undefined`:
|
|
64
67
|
*
|
|
65
68
|
* ```ts
|
|
66
69
|
* import {APIError} from "@encore.dev/framework";
|
|
@@ -78,10 +81,12 @@
|
|
|
78
81
|
* const product = await unlessNotFound(catalog.openapi.getProduct("xyz"));
|
|
79
82
|
* ```
|
|
80
83
|
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
84
|
+
* ## Options and transport
|
|
85
|
+
*
|
|
86
|
+
* Every generated call accepts {@link CallOptions} (per-call `timeout` in
|
|
87
|
+
* milliseconds, extra `headers`, and an `AbortSignal`), which override client
|
|
88
|
+
* defaults. Construct a module's `Client` with {@link ClientOptions} to share
|
|
89
|
+
* settings across calls:
|
|
85
90
|
*
|
|
86
91
|
* ```ts
|
|
87
92
|
* const fresh = await catalog.openapi.getProduct("xyz", {timeout: 2_000});
|
|
@@ -89,26 +94,25 @@
|
|
|
89
94
|
* const mocked = new catalog.openapi.Client({baseUrl: mockServer.url});
|
|
90
95
|
* ```
|
|
91
96
|
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
*
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
* first called (`system` of `@encore.dev/sdk/testing`).
|
|
97
|
+
* HTTP generated clients use this module's {@link fetch}, propagating traces
|
|
98
|
+
* and Request Context automatically. gRPC clients speak gRPC over HTTP/2
|
|
99
|
+
* (h2c for `http://`, TLS for `https://`), sharing one connection per base URL
|
|
100
|
+
* and propagating traces and Request Context the same way; gRPC status details
|
|
101
|
+
* are exposed on {@link CallError.grpcDetails}. Client instances hold only
|
|
102
|
+
* configuration, so creating one is cheap and requires no cleanup.
|
|
103
|
+
*
|
|
104
|
+
* ## Static dependencies and testing
|
|
105
|
+
*
|
|
106
|
+
* Calling {@link baseUrl}, {@link serviceUrl}, or {@link client} registers a
|
|
107
|
+
* static dependency from the calling service to the target service. Therefore,
|
|
108
|
+
* the service name must be a string literal, the function must be called
|
|
109
|
+
* directly (not aliased or passed around), and the call must occur inside a
|
|
110
|
+
* service.
|
|
111
|
+
*
|
|
112
|
+
* Under `encore test` (unit tests), no other services are running; service
|
|
113
|
+
* calls receive `501 Not Implemented` identifying the target service and path
|
|
114
|
+
* so you can mock them. In system tests (`system` from
|
|
115
|
+
* `@encore.dev/sdk/testing`), calls reach real services, which start on demand.
|
|
112
116
|
*
|
|
113
117
|
* @module
|
|
114
118
|
*/
|
|
@@ -130,11 +134,11 @@ export {
|
|
|
130
134
|
} from '../internal/clients/call';
|
|
131
135
|
|
|
132
136
|
/**
|
|
133
|
-
*
|
|
134
|
-
*
|
|
137
|
+
* Returns the base URL for `service` without a trailing slash, so paths can be
|
|
138
|
+
* appended directly (e.g. `` `${baseUrl("orders")}/orders/1` ``).
|
|
135
139
|
*
|
|
136
|
-
* Throws if
|
|
137
|
-
*
|
|
140
|
+
* Throws if `service` is unknown to this process (either no such service exists
|
|
141
|
+
* or service discovery was not configured).
|
|
138
142
|
*
|
|
139
143
|
* @example
|
|
140
144
|
* import {baseUrl} from "@encore.dev/sdk/services";
|
|
@@ -145,15 +149,15 @@ export function baseUrl(service: string): string {
|
|
|
145
149
|
}
|
|
146
150
|
|
|
147
151
|
/**
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
* service is unknown to this process.
|
|
152
|
+
* Returns the base URL of `service` as a `URL` ending with `/`, suitable for
|
|
153
|
+
* resolving relative paths (`new URL("orders/1", serviceUrl("orders"))`).
|
|
154
|
+
* Throws if `service` is unknown to this process.
|
|
151
155
|
*
|
|
152
|
-
*
|
|
153
|
-
* {@link baseUrl}
|
|
156
|
+
* Registers a static dependency on `service` under the same rules as
|
|
157
|
+
* {@link baseUrl}.
|
|
154
158
|
*
|
|
155
|
-
* @deprecated Use {@link baseUrl}, which returns
|
|
156
|
-
*
|
|
159
|
+
* @deprecated Use {@link baseUrl}, which returns a string without a trailing
|
|
160
|
+
* slash consistently across Encore SDKs.
|
|
157
161
|
*/
|
|
158
162
|
export function serviceUrl(service: string): URL {
|
|
159
163
|
const u = new URL(lookup(service));
|
|
@@ -161,8 +165,7 @@ export function serviceUrl(service: string): URL {
|
|
|
161
165
|
return u;
|
|
162
166
|
}
|
|
163
167
|
|
|
164
|
-
/**
|
|
165
|
-
* it records as one of the app's services'. */
|
|
168
|
+
/** Looks up `service`'s base URL from runtime config and marks its origin as internal. */
|
|
166
169
|
function lookup(service: string): string {
|
|
167
170
|
const base = runtimeConfig().services[service];
|
|
168
171
|
if (base === undefined) {
|
|
@@ -172,28 +175,25 @@ function lookup(service: string): string {
|
|
|
172
175
|
return base;
|
|
173
176
|
}
|
|
174
177
|
|
|
175
|
-
/**
|
|
178
|
+
/** HTTP client scoped to a single Encore service. */
|
|
176
179
|
export interface ServiceClient {
|
|
177
|
-
/**
|
|
178
|
-
* returns it. */
|
|
180
|
+
/** Base URL of the target service without a trailing slash (same as {@link baseUrl}). */
|
|
179
181
|
readonly baseUrl: string;
|
|
180
182
|
/**
|
|
181
|
-
* Calls the service
|
|
182
|
-
*
|
|
183
|
-
* `'
|
|
184
|
-
* used as it is.
|
|
183
|
+
* Calls the service using this module's {@link fetch}. Relative `path` values
|
|
184
|
+
* are joined to `baseUrl` with a single `/` (so `'/invoices'` and
|
|
185
|
+
* `'invoices'` are equivalent); absolute URLs are used as-is.
|
|
185
186
|
*/
|
|
186
187
|
fetch(path: string, init?: RequestInit): Promise<Response>;
|
|
187
188
|
}
|
|
188
189
|
|
|
189
190
|
/**
|
|
190
|
-
*
|
|
191
|
-
* service
|
|
192
|
-
* context.
|
|
191
|
+
* Returns a {@link ServiceClient} for `service` whose `fetch` accepts
|
|
192
|
+
* service-relative paths and propagates the active trace and Request Context.
|
|
193
193
|
*
|
|
194
|
-
* Like {@link baseUrl},
|
|
195
|
-
*
|
|
196
|
-
* Throws if
|
|
194
|
+
* Like {@link baseUrl}, registers a static dependency on `service`, so the name
|
|
195
|
+
* must be a string literal and the function must be called directly inside a
|
|
196
|
+
* service. Throws if `service` is unknown to this process.
|
|
197
197
|
*
|
|
198
198
|
* @example
|
|
199
199
|
* import {client} from "@encore.dev/sdk/services";
|
|
@@ -208,7 +208,7 @@ export function client(service: string): ServiceClient {
|
|
|
208
208
|
};
|
|
209
209
|
}
|
|
210
210
|
|
|
211
|
-
/** `path`
|
|
211
|
+
/** Joins `path` onto `base` with a single `/`, leaving absolute URLs unchanged. */
|
|
212
212
|
function join(base: string, path: string): string {
|
|
213
213
|
if (/^[a-z][a-z0-9+.-]*:/i.test(path)) return path;
|
|
214
214
|
return `${base}/${path.replace(/^\/+/, '')}`;
|
package/shutdown/mod.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Graceful shutdown on SIGTERM or SIGINT
|
|
2
|
+
* Graceful shutdown on `SIGTERM` or `SIGINT`.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* and telemetry, and exits
|
|
7
|
-
*
|
|
8
|
-
*
|
|
4
|
+
* When a shutdown signal arrives, Encore stops accepting new Pub/Sub messages,
|
|
5
|
+
* waits for in-flight message handlers and application work to finish, closes
|
|
6
|
+
* managed resources, flushes metrics and telemetry, and exits. If cleanup
|
|
7
|
+
* finishes within the shutdown deadline, the process exits with
|
|
8
|
+
* `process.exitCode` (0 unless set by the application); otherwise it exits
|
|
9
|
+
* with 1. A second `SIGINT` (Ctrl-C) skips waiting for application work and
|
|
10
|
+
* forces immediate cleanup.
|
|
9
11
|
*
|
|
10
|
-
*
|
|
11
|
-
* {@link onShutdown} callback:
|
|
12
|
+
* Use {@link onShutdown} to drain servers or close frameworks cleanly:
|
|
12
13
|
*
|
|
13
14
|
* ```typescript
|
|
14
15
|
* import { onShutdown } from "@encore.dev/sdk/shutdown";
|
|
@@ -17,17 +18,17 @@
|
|
|
17
18
|
* onShutdown(() => new Promise((resolve) => server.close(() => resolve())));
|
|
18
19
|
* ```
|
|
19
20
|
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* `enableShutdownHooks()
|
|
21
|
+
* If your framework has its own shutdown hooks, invoke them inside
|
|
22
|
+
* `onShutdown` instead of registering separate signal handlers. For example,
|
|
23
|
+
* with NestJS (instead of `enableShutdownHooks()`):
|
|
23
24
|
*
|
|
24
25
|
* ```typescript
|
|
25
26
|
* onShutdown(() => app.close());
|
|
26
27
|
* ```
|
|
27
28
|
*
|
|
28
|
-
*
|
|
29
|
-
* {@link
|
|
30
|
-
*
|
|
29
|
+
* For in-flight background work that isn't a shutdown callback, delay cleanup
|
|
30
|
+
* with {@link hold} or {@link waitUntil}. With `using`, a hold is released
|
|
31
|
+
* automatically when the block exits:
|
|
31
32
|
*
|
|
32
33
|
* ```typescript
|
|
33
34
|
* import * as shutdown from "@encore.dev/sdk/shutdown";
|
|
@@ -38,19 +39,19 @@
|
|
|
38
39
|
* }
|
|
39
40
|
* ```
|
|
40
41
|
*
|
|
41
|
-
* Encore
|
|
42
|
-
*
|
|
43
|
-
*
|
|
42
|
+
* Encore begins resource cleanup once all holds are released or the shutdown
|
|
43
|
+
* deadline expires, whichever comes first. {@link signal} aborts when shutdown
|
|
44
|
+
* starts; each callback's `ctx.signal` aborts when cleanup starts.
|
|
44
45
|
*
|
|
45
|
-
*
|
|
46
|
-
* `enableShutdownHooks()`
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
46
|
+
* If an application registers its own signal listener without using Encore's
|
|
47
|
+
* shutdown APIs (such as NestJS's `enableShutdownHooks()`), Encore stops
|
|
48
|
+
* taking new work immediately and waits for the application to re-raise the
|
|
49
|
+
* signal, exit, or drain its event loop before cleaning up. The process then
|
|
50
|
+
* exits with the application's own exit code, or with 1 if the shutdown
|
|
51
|
+
* deadline expires first.
|
|
51
52
|
*
|
|
52
|
-
*
|
|
53
|
-
* stay open until the
|
|
53
|
+
* Database connections owned by third-party pools (such as `pg` or Drizzle)
|
|
54
|
+
* stay open until the application closes them or the process exits.
|
|
54
55
|
*
|
|
55
56
|
* @module
|
|
56
57
|
*/
|