@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/config/config.ts
CHANGED
|
@@ -6,18 +6,18 @@ import {build} from './value';
|
|
|
6
6
|
export {Duration} from './value';
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
* A
|
|
10
|
-
*
|
|
9
|
+
* A typed configuration value defined in config files alongside your code and
|
|
10
|
+
* hot-reloaded while the app runs. Call the handle to read the current value.
|
|
11
11
|
*
|
|
12
|
-
* Declare
|
|
13
|
-
* argument. Encore
|
|
14
|
-
*
|
|
12
|
+
* Declare a config with {@link config}, passing its TypeScript type as the
|
|
13
|
+
* type argument. Encore derives a schema from that type and validates config
|
|
14
|
+
* files against it before delivering values to your code.
|
|
15
15
|
*
|
|
16
16
|
* ```ts
|
|
17
17
|
* import { config, type Min, type Max, type Duration } from "@encore.dev/sdk/config";
|
|
18
18
|
*
|
|
19
19
|
* interface Checkout {
|
|
20
|
-
* //
|
|
20
|
+
* // Maximum number of items in an order.
|
|
21
21
|
* maxItems: number & Int & Min<1> & Max<500>;
|
|
22
22
|
* currencies?: string[];
|
|
23
23
|
* paymentTimeout: Duration;
|
|
@@ -28,72 +28,76 @@ export {Duration} from './value';
|
|
|
28
28
|
* if (items.length > checkout().maxItems) { ... }
|
|
29
29
|
* ```
|
|
30
30
|
*
|
|
31
|
-
* #
|
|
31
|
+
* # Config files
|
|
32
32
|
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
* `encore.app.json` for a shared or global config.
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
* `checkout.
|
|
39
|
-
*
|
|
33
|
+
* Values live in a file named after the config — `checkout.config.toml` or
|
|
34
|
+
* `.json` — next to the declaring service's `encore.service.json` (or next to
|
|
35
|
+
* `encore.app.json` for a shared or global config). Environment-specific
|
|
36
|
+
* overlay files override the base file:
|
|
37
|
+
* - `checkout.production.config.toml` for all environments of a given type
|
|
38
|
+
* - `checkout.@prod-eu.config.toml` for a single named environment
|
|
39
|
+
* - `checkout.user.config.toml` (gitignored) for local overrides during
|
|
40
|
+
* `encore run` (ignored in tests)
|
|
40
41
|
*
|
|
41
|
-
* Calling the config returns a snapshot
|
|
42
|
-
* changes
|
|
43
|
-
*
|
|
42
|
+
* Calling the config returns a snapshot that remains the same object until the
|
|
43
|
+
* value changes. Because values can update at runtime without a restart, call
|
|
44
|
+
* the config where you use it rather than caching the result, or subscribe
|
|
45
|
+
* with {@link Config.onChange}.
|
|
44
46
|
*/
|
|
45
47
|
export interface Config<T> {
|
|
46
|
-
/**
|
|
48
|
+
/** Returns the current config value. */
|
|
47
49
|
(): T;
|
|
48
50
|
|
|
49
51
|
/** The name the config was declared with. */
|
|
50
52
|
readonly name: string;
|
|
51
53
|
|
|
52
54
|
/**
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
* coalesced
|
|
55
|
+
* Registers a `listener` invoked whenever the config value changes while the
|
|
56
|
+
* app is running. Returns an unsubscribe function (the subscription stays
|
|
57
|
+
* active even if you discard the returned function). Rapid updates are
|
|
58
|
+
* coalesced so the listener receives the latest value once. Does not keep
|
|
59
|
+
* the process alive.
|
|
57
60
|
*/
|
|
58
61
|
onChange(listener: (value: T) => void): () => void;
|
|
59
62
|
}
|
|
60
63
|
|
|
61
|
-
/**
|
|
64
|
+
/** Options for declaring a config with {@link config}. */
|
|
62
65
|
export interface ConfigOptions {
|
|
63
66
|
/**
|
|
64
|
-
*
|
|
65
|
-
*
|
|
67
|
+
* Controls which services own and can read the config. Must be a string
|
|
68
|
+
* literal so Encore can resolve it statically:
|
|
66
69
|
*
|
|
67
|
-
* - `"service"
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
* - `"shared"`:
|
|
71
|
-
* recorded as
|
|
72
|
-
* - `"global"`:
|
|
73
|
-
* without
|
|
70
|
+
* - `"service"` (default): scoped to the declaring service, which has its
|
|
71
|
+
* own value. Two services may declare configs with the same name. Must be
|
|
72
|
+
* declared inside a service.
|
|
73
|
+
* - `"shared"`: app-wide with a single value; each service that reads it is
|
|
74
|
+
* recorded as a consumer. May be declared outside a service.
|
|
75
|
+
* - `"global"`: app-wide with a single value available to every service
|
|
76
|
+
* without tracking per-service usage. May be declared outside a service.
|
|
74
77
|
*
|
|
75
|
-
*
|
|
76
|
-
* declared shared or global
|
|
78
|
+
* Each config name has a single scope across the app, just like secrets: a
|
|
79
|
+
* name declared as `shared` or `global` cannot also be declared as a
|
|
80
|
+
* service-scoped config.
|
|
77
81
|
*/
|
|
78
82
|
scope?: 'service' | 'shared' | 'global';
|
|
79
83
|
|
|
80
84
|
/**
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
+
* Static string-literal tags that classify the config for policy checks
|
|
86
|
+
* (e.g. `{data: "pci"}`). Keys and values may contain lowercase letters,
|
|
87
|
+
* digits, `_`, and `-`, up to 63 characters. Keys must start with a letter
|
|
88
|
+
* and cannot start with the reserved prefix `encore`. At most 32 tags.
|
|
85
89
|
*/
|
|
86
90
|
tags?: Record<string, string>;
|
|
87
91
|
}
|
|
88
92
|
|
|
89
93
|
/**
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
94
|
+
* Declares a {@link Config}, scoped by default to the declaring service (see
|
|
95
|
+
* {@link ConfigOptions.scope}). Must be a module-level `export const` with a
|
|
96
|
+
* string-literal name.
|
|
93
97
|
*
|
|
94
98
|
* @example
|
|
95
99
|
* ```ts
|
|
96
|
-
* // A config several services
|
|
100
|
+
* // A config shared by several services.
|
|
97
101
|
* export const flags = config<Flags>("flags", {scope: "shared"});
|
|
98
102
|
* ```
|
|
99
103
|
*/
|
|
@@ -113,20 +117,19 @@ export function config<T>(
|
|
|
113
117
|
const names = new WeakMap<Config<any>, string>();
|
|
114
118
|
|
|
115
119
|
/**
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
* returns it from then on. For `@encore.dev/sdk/testing`.
|
|
120
|
+
* Sets `cfg` to `value` for the given test `scope` after validating it against
|
|
121
|
+
* the config's schema. For `@encore.dev/sdk/testing`.
|
|
119
122
|
*
|
|
120
123
|
* @internal
|
|
121
124
|
*/
|
|
122
125
|
export function setTestValue<T>(scope: Scope, cfg: Config<T>, value: T): void {
|
|
123
126
|
const name = names.get(cfg);
|
|
124
127
|
if (name === undefined) {
|
|
125
|
-
throw new TypeError('setConfig
|
|
128
|
+
throw new TypeError('setConfig requires a config declared with `config()`');
|
|
126
129
|
}
|
|
127
130
|
const json = JSON.stringify(value);
|
|
128
131
|
if (json === undefined) {
|
|
129
|
-
throw new TypeError(`config ${name}:
|
|
132
|
+
throw new TypeError(`config ${name}: value cannot be serialized to JSON`);
|
|
130
133
|
}
|
|
131
134
|
const prepared = JSON.parse(scope.native.setConfig(name, json));
|
|
132
135
|
const impl = runtime.RT.config(name);
|
|
@@ -208,29 +211,29 @@ function makeConfig<T>(name: string, impl: Impl | null): Config<T> {
|
|
|
208
211
|
}
|
|
209
212
|
|
|
210
213
|
/**
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
*
|
|
214
|
+
* Requires a `number` config field to be at least `N`, written as an
|
|
215
|
+
* intersection (`number & Min<1>`). Validated by Encore against config files;
|
|
216
|
+
* at runtime the field is a plain `number`.
|
|
214
217
|
*/
|
|
215
218
|
export type Min<N extends number> = {readonly '~encore.min'?: N};
|
|
216
219
|
|
|
217
|
-
/**
|
|
220
|
+
/** Requires a `number` config field to be at most `N` (`number & Max<500>`). See {@link Min}. */
|
|
218
221
|
export type Max<N extends number> = {readonly '~encore.max'?: N};
|
|
219
222
|
|
|
220
|
-
/**
|
|
223
|
+
/** Requires a `number` config field to be an integer (`number & Int`). See {@link Min}. */
|
|
221
224
|
export type Int = {readonly '~encore.int'?: true};
|
|
222
225
|
|
|
223
|
-
/**
|
|
226
|
+
/** Requires a `string` config field to be at least `N` characters long (`string & MinLen<3>`). */
|
|
224
227
|
export type MinLen<N extends number> = {readonly '~encore.min_len'?: N};
|
|
225
228
|
|
|
226
|
-
/**
|
|
229
|
+
/** Requires a `string` config field to be at most `N` characters long (`string & MaxLen<64>`). */
|
|
227
230
|
export type MaxLen<N extends number> = {readonly '~encore.max_len'?: N};
|
|
228
231
|
|
|
229
|
-
/**
|
|
232
|
+
/** Requires a `string` config field to match the regular expression `S` (`string & Pattern<"^[A-Z]+$">`). */
|
|
230
233
|
export type Pattern<S extends string> = {readonly '~encore.pattern'?: S};
|
|
231
234
|
|
|
232
|
-
/**
|
|
235
|
+
/** Requires an array config field to have at least `N` items (`string[] & MinItems<1>`). */
|
|
233
236
|
export type MinItems<N extends number> = {readonly '~encore.min_items'?: N};
|
|
234
237
|
|
|
235
|
-
/**
|
|
238
|
+
/** Requires an array config field to have at most `N` items (`string[] & MaxItems<10>`). */
|
|
236
239
|
export type MaxItems<N extends number> = {readonly '~encore.max_items'?: N};
|
package/config/secrets.ts
CHANGED
|
@@ -3,14 +3,12 @@ import {lookupScope, testMode} from '../internal/testing/scope';
|
|
|
3
3
|
import {StringLiteral} from '../internal/utils/constraints';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* so that you can write functions which expect a specific one.
|
|
6
|
+
* Represents a single secret loaded into the application. It is strongly typed
|
|
7
|
+
* to its secret name so functions can require a specific secret.
|
|
9
8
|
*
|
|
10
|
-
*
|
|
11
|
-
* it's name.
|
|
9
|
+
* Use {@link AnySecret} to accept any secret regardless of its name.
|
|
12
10
|
*
|
|
13
|
-
* `T` is the
|
|
11
|
+
* `T` is the secret's value type (only `string` is supported).
|
|
14
12
|
*
|
|
15
13
|
* @example
|
|
16
14
|
*
|
|
@@ -22,13 +20,12 @@ export interface Secret<Name extends string, T extends string = string> {
|
|
|
22
20
|
/**
|
|
23
21
|
* Returns the value of the secret.
|
|
24
22
|
*
|
|
25
|
-
* The value is
|
|
26
|
-
*
|
|
23
|
+
* The value is loaded once per process and does not change while it is
|
|
24
|
+
* running; updated values take effect when the service restarts.
|
|
27
25
|
*
|
|
28
|
-
* Locally (`encore run`, `encore test`),
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
* Anywhere else it throws an `Error` naming the secret.
|
|
26
|
+
* Locally (`encore run`, `encore test`), an unset secret returns `""` so
|
|
27
|
+
* checks like `if (!apiKey())` can skip unconfigured integrations. In all
|
|
28
|
+
* other environments, reading an unset secret throws an `Error` naming it.
|
|
32
29
|
*/
|
|
33
30
|
(): T;
|
|
34
31
|
|
|
@@ -38,69 +35,66 @@ export interface Secret<Name extends string, T extends string = string> {
|
|
|
38
35
|
readonly name: Name;
|
|
39
36
|
|
|
40
37
|
/**
|
|
41
|
-
*
|
|
42
|
-
*
|
|
38
|
+
* Type brand used by Encore's parser to detect secret fields inside a config
|
|
39
|
+
* type. Never set at runtime.
|
|
43
40
|
*
|
|
44
|
-
*
|
|
45
|
-
* secret
|
|
46
|
-
*
|
|
47
|
-
*
|
|
41
|
+
* When used in a config interface, `apiKey: Secret<"StripeKey">` references
|
|
42
|
+
* the secret with that name in the config's scope (the declaring service for
|
|
43
|
+
* a service config, or the app for a shared or global config). The secret
|
|
44
|
+
* itself must still be declared with {@link secret}.
|
|
48
45
|
*/
|
|
49
46
|
readonly '~encore.secret'?: true;
|
|
50
47
|
}
|
|
51
48
|
|
|
52
49
|
/**
|
|
53
|
-
*
|
|
54
|
-
* without knowing its name.
|
|
50
|
+
* Represents any {@link Secret} without requiring a specific name.
|
|
55
51
|
*/
|
|
56
52
|
export type AnySecret = Secret<string>;
|
|
57
53
|
|
|
58
54
|
/**
|
|
59
|
-
*
|
|
60
|
-
*
|
|
55
|
+
* Options for declaring a secret with {@link secret}. Values must be literals
|
|
56
|
+
* so Encore's parser can inspect them statically.
|
|
61
57
|
*/
|
|
62
58
|
export interface SecretOptions {
|
|
63
59
|
/**
|
|
64
|
-
*
|
|
65
|
-
*
|
|
60
|
+
* Controls which services own and can read the secret. Must be a string
|
|
61
|
+
* literal so Encore can resolve it statically:
|
|
66
62
|
*
|
|
67
|
-
* - `"service"
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
* later is easier than narrowing it.
|
|
78
|
-
* - `"global"`: the whole app, with one value available to every service
|
|
79
|
-
* without being granted. It may be declared outside a service.
|
|
63
|
+
* - `"service"` (default): scoped to the declaring service. Two services may
|
|
64
|
+
* declare secrets with the same name and hold independent values, avoiding
|
|
65
|
+
* accidental collisions or silent reuse. Must be declared inside a
|
|
66
|
+
* service; Encore reports an error otherwise.
|
|
67
|
+
* - `"shared"`: app-wide and readable from any service, with each reading
|
|
68
|
+
* service recorded as a consumer. May be declared outside a service. Use
|
|
69
|
+
* this only when multiple services genuinely share a value — widening a
|
|
70
|
+
* secret's scope later is easier than narrowing it.
|
|
71
|
+
* - `"global"`: app-wide with a single value available to every service
|
|
72
|
+
* without requiring per-service grants. May be declared outside a service.
|
|
80
73
|
*/
|
|
81
74
|
scope?: 'service' | 'shared' | 'global';
|
|
82
75
|
|
|
83
76
|
/**
|
|
84
|
-
*
|
|
85
|
-
*
|
|
77
|
+
* Human-readable description of the secret (what it is, who issues it, and
|
|
78
|
+
* how to rotate it), displayed wherever secret values are managed. A doc
|
|
79
|
+
* comment on the declaration takes precedence if both are present.
|
|
86
80
|
*/
|
|
87
81
|
description?: string;
|
|
88
82
|
|
|
89
83
|
/**
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
84
|
+
* Static string-literal tags that classify the secret for policy checks
|
|
85
|
+
* (e.g. `{data: "pci"}`). Keys and values may contain lowercase letters,
|
|
86
|
+
* digits, `_`, and `-`, up to 63 characters. Keys must start with a letter
|
|
87
|
+
* and cannot start with the reserved prefix `encore`. At most 32 tags.
|
|
94
88
|
*/
|
|
95
89
|
tags?: Record<string, string>;
|
|
96
90
|
}
|
|
97
91
|
|
|
98
92
|
/**
|
|
99
|
-
*
|
|
100
|
-
*
|
|
93
|
+
* Declares a {@link Secret}, scoped by default to the declaring service. See
|
|
94
|
+
* {@link SecretOptions.scope} for app-wide secrets.
|
|
101
95
|
*
|
|
102
|
-
* A doc comment on the declaration
|
|
103
|
-
* wherever
|
|
96
|
+
* A doc comment on the declaration (or the `description` option) is shown
|
|
97
|
+
* wherever secret values are managed.
|
|
104
98
|
*
|
|
105
99
|
* @example
|
|
106
100
|
* ```ts
|
|
@@ -111,14 +105,14 @@ export interface SecretOptions {
|
|
|
111
105
|
*
|
|
112
106
|
* @example
|
|
113
107
|
* ```ts
|
|
114
|
-
* // A secret several services
|
|
108
|
+
* // A secret shared by several services.
|
|
115
109
|
* import {secret} from "@encore.dev/sdk/config";
|
|
116
110
|
* const signingKey = secret("SigningKey", {scope: "shared"});
|
|
117
111
|
* ```
|
|
118
112
|
*
|
|
119
113
|
* @example
|
|
120
114
|
* ```ts
|
|
121
|
-
* // Documenting
|
|
115
|
+
* // Documenting a secret.
|
|
122
116
|
* const apiKey = secret("ApiKey", {
|
|
123
117
|
* description: "Issued by Acme Pay. Rotate it in their portal under Settings → API.",
|
|
124
118
|
* });
|
|
@@ -135,9 +129,8 @@ export function secret<Name extends string>(
|
|
|
135
129
|
}
|
|
136
130
|
|
|
137
131
|
/**
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
* is read by name whatever the config's scope. Internal to the config module.
|
|
132
|
+
* Returns the secret handle referenced by a config's secret field placeholder.
|
|
133
|
+
* Internal to the config module.
|
|
141
134
|
*
|
|
142
135
|
* @internal
|
|
143
136
|
*/
|
package/config/value.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* can be tested without one.
|
|
2
|
+
* Converts raw config JSON from the runtime into typed values using the
|
|
3
|
+
* config's JSON Schema: duration strings become {@link Duration} instances and
|
|
4
|
+
* secret placeholders become secret handles. Kept separate from the runtime so
|
|
5
|
+
* it can be unit-tested standalone.
|
|
7
6
|
*/
|
|
8
7
|
|
|
9
|
-
/**
|
|
8
|
+
/** Matches a non-negative duration string (one or more number+unit segments). */
|
|
10
9
|
const DURATION = /^([0-9]+(\.[0-9]+)?(ns|us|µs|ms|s|m|h))+$/;
|
|
11
10
|
const UNIT_MS: Record<string, number> = {
|
|
12
11
|
ns: 1e-6,
|
|
@@ -19,9 +18,10 @@ const UNIT_MS: Record<string, number> = {
|
|
|
19
18
|
};
|
|
20
19
|
|
|
21
20
|
/**
|
|
22
|
-
* A length of time, written in config files as
|
|
23
|
-
*
|
|
24
|
-
* `
|
|
21
|
+
* A non-negative length of time, written in config files as one or more
|
|
22
|
+
* number-and-unit pairs such as `"30s"`, `"250ms"`, or `"1h30m"`. Supported
|
|
23
|
+
* units are `ns`, `us`, `ms`, `s`, `m`, and `h` (matching Go's
|
|
24
|
+
* `time.ParseDuration`, without a leading sign).
|
|
25
25
|
*
|
|
26
26
|
* @example
|
|
27
27
|
* interface Checkout { paymentTimeout: Duration }
|
|
@@ -29,8 +29,8 @@ const UNIT_MS: Record<string, number> = {
|
|
|
29
29
|
*/
|
|
30
30
|
export class Duration {
|
|
31
31
|
/**
|
|
32
|
-
*
|
|
33
|
-
*
|
|
32
|
+
* Type brand used by Encore's parser to identify duration fields in a config
|
|
33
|
+
* type. Never set at runtime.
|
|
34
34
|
*/
|
|
35
35
|
declare readonly '~encore.duration'?: true;
|
|
36
36
|
|
|
@@ -40,11 +40,11 @@ export class Duration {
|
|
|
40
40
|
this.ms = ms;
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
/**
|
|
43
|
+
/** Parses a duration string and throws if the format is invalid. */
|
|
44
44
|
static parse(text: string): Duration {
|
|
45
45
|
if (!DURATION.test(text)) {
|
|
46
46
|
throw new Error(
|
|
47
|
-
`${JSON.stringify(text)} is not a duration
|
|
47
|
+
`${JSON.stringify(text)} is not a valid duration (expected e.g. "30s", "250ms", or "1h30m")`,
|
|
48
48
|
);
|
|
49
49
|
}
|
|
50
50
|
let ms = 0;
|
|
@@ -57,25 +57,25 @@ export class Duration {
|
|
|
57
57
|
return new Duration(Math.round(ms * 1e6) / 1e6);
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
/**
|
|
60
|
+
/** Creates a duration of `ms` milliseconds. */
|
|
61
61
|
static ofMilliseconds(ms: number): Duration {
|
|
62
62
|
if (!(ms >= 0)) {
|
|
63
|
-
throw new Error('
|
|
63
|
+
throw new Error('duration must not be negative');
|
|
64
64
|
}
|
|
65
65
|
return new Duration(ms);
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
/**
|
|
68
|
+
/** Returns the duration in milliseconds (suitable for `setTimeout`). */
|
|
69
69
|
get milliseconds(): number {
|
|
70
70
|
return this.ms;
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
/**
|
|
73
|
+
/** Returns the duration in seconds. */
|
|
74
74
|
get seconds(): number {
|
|
75
75
|
return this.ms / 1000;
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
/**
|
|
78
|
+
/** Formats the duration in config-file syntax (e.g. `"1h30m"`, `"250ms"`, `"0s"`). */
|
|
79
79
|
toString(): string {
|
|
80
80
|
let ms = this.ms;
|
|
81
81
|
if (ms === 0) return '0s';
|
|
@@ -104,22 +104,19 @@ export class Duration {
|
|
|
104
104
|
}
|
|
105
105
|
|
|
106
106
|
/**
|
|
107
|
-
*
|
|
108
|
-
* `{"$encore_secret": "<name>"}`. The name alone says which secret it is: a
|
|
109
|
-
* secret's name has one scope across the app.
|
|
107
|
+
* Property key used in runtime secret placeholders (`{"$encore_secret": "<name>"}`).
|
|
110
108
|
*/
|
|
111
109
|
export const PLACEHOLDER_KEY = '$encore_secret';
|
|
112
110
|
|
|
113
|
-
/**
|
|
111
|
+
/** Resolves a secret placeholder's name into a secret handle. */
|
|
114
112
|
export type SecretOf = (name: string) => unknown;
|
|
115
113
|
|
|
116
114
|
type Schema = Record<string, any>;
|
|
117
115
|
|
|
118
116
|
/**
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
* `secretOf` builds.
|
|
117
|
+
* Converts a raw JSON config `value` according to `schema`, turning duration
|
|
118
|
+
* strings into {@link Duration} instances and secret placeholders into handles
|
|
119
|
+
* via `secretOf`.
|
|
123
120
|
*/
|
|
124
121
|
export function build(
|
|
125
122
|
schema: Schema,
|
package/cron/mod.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import {DurationString} from '../types/mod';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* before:
|
|
4
|
+
* Header containing a cron run's idempotency key. The value stays the same
|
|
5
|
+
* across retries of the same execution, so a job that must not run its side
|
|
6
|
+
* effects twice can deduplicate using this key:
|
|
8
7
|
*
|
|
9
8
|
* ```ts
|
|
10
9
|
* app.post('/internal/cleanup', (req, res) => {
|
|
@@ -13,66 +12,66 @@ import {DurationString} from '../types/mod';
|
|
|
13
12
|
* });
|
|
14
13
|
* ```
|
|
15
14
|
*
|
|
16
|
-
* Only Encore
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* schedule while
|
|
15
|
+
* Only Encore sets this header; gateways strip it from external client
|
|
16
|
+
* requests. Run `encore cron trigger <name>` to execute a job immediately
|
|
17
|
+
* during local development. Jobs declared with `runIn: {local: true}` also run
|
|
18
|
+
* on schedule while a local session is active.
|
|
20
19
|
*/
|
|
21
20
|
export const IDEMPOTENCY_KEY_HEADER = 'x-encore-cron-execution';
|
|
22
21
|
|
|
23
22
|
/**
|
|
24
|
-
*
|
|
25
|
-
*
|
|
23
|
+
* Header indicating what triggered a cron run: `"scheduled"` when the job ran
|
|
24
|
+
* on its schedule, or `"manual"` when triggered manually.
|
|
26
25
|
*/
|
|
27
26
|
export const TRIGGER_HEADER = 'x-encore-cron-trigger';
|
|
28
27
|
|
|
29
28
|
export class CronJob {
|
|
30
29
|
readonly name: string;
|
|
31
30
|
/**
|
|
32
|
-
*
|
|
33
|
-
*
|
|
31
|
+
* The cron job configuration as declared in source code. Read-only because
|
|
32
|
+
* Encore reads the schedule and target path statically from the declaration.
|
|
34
33
|
*/
|
|
35
34
|
readonly cfg: Readonly<CronJobConfig>;
|
|
36
35
|
constructor(name: string, cfg: CronJobConfig) {
|
|
37
36
|
this.name = name;
|
|
38
|
-
// Frozen as well as typed `readonly`, so
|
|
39
|
-
//
|
|
37
|
+
// Frozen at runtime as well as typed `readonly`, so mutations throw even
|
|
38
|
+
// when running without TypeScript type checking.
|
|
40
39
|
this.cfg = Object.freeze({...cfg});
|
|
41
40
|
Object.freeze(this);
|
|
42
41
|
}
|
|
43
42
|
}
|
|
44
43
|
|
|
45
44
|
/**
|
|
46
|
-
*
|
|
45
|
+
* Configuration for a cron job: the endpoint path to call and its schedule.
|
|
47
46
|
*
|
|
48
|
-
* When the job is due, Encore sends a `POST` request
|
|
49
|
-
* that declares the job.
|
|
50
|
-
*
|
|
47
|
+
* When the job is due, Encore sends a `POST` request to `path` on the service
|
|
48
|
+
* that declares the job. All configuration fields must be string/boolean
|
|
49
|
+
* literals because Encore parses them statically from source code.
|
|
51
50
|
*/
|
|
52
51
|
export type CronJobConfig = {
|
|
53
|
-
/** The path the
|
|
52
|
+
/** The endpoint path on the declaring service to `POST` to, starting with `/`. */
|
|
54
53
|
path: string;
|
|
55
54
|
title?: string;
|
|
56
55
|
/**
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
56
|
+
* Classification tags for policies, written as string literals (for example,
|
|
57
|
+
* `{data: "pci"}`). Keys and values may contain lowercase letters, digits,
|
|
58
|
+
* `_`, and `-`, up to 63 characters. Keys must start with a letter and cannot
|
|
59
|
+
* start with `encore` (which is reserved). At most 32 tags are allowed.
|
|
61
60
|
*/
|
|
62
61
|
tags?: Record<string, string>;
|
|
63
|
-
/**
|
|
62
|
+
/** Controls which environment types run this job on schedule. */
|
|
64
63
|
runIn?: CronJobRunIn;
|
|
65
64
|
} & ({every: DurationString} | {schedule: string});
|
|
66
65
|
|
|
67
66
|
/**
|
|
68
|
-
*
|
|
69
|
-
* deployed
|
|
67
|
+
* Controls which environment types run a cron job on schedule. Cron jobs always
|
|
68
|
+
* run in deployed environments; locally they only run when `local` is `true`.
|
|
70
69
|
*/
|
|
71
70
|
export type CronJobRunIn = {
|
|
72
71
|
/**
|
|
73
|
-
* Whether the job
|
|
74
|
-
*
|
|
75
|
-
*
|
|
72
|
+
* Whether the job also runs on schedule during local development (for example
|
|
73
|
+
* while `encore run` is active). Defaults to `false`, and developers can
|
|
74
|
+
* override it per local checkout.
|
|
76
75
|
*/
|
|
77
76
|
local?: boolean;
|
|
78
77
|
};
|