@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
|
@@ -45,9 +45,12 @@ export class BucketObject {
|
|
|
45
45
|
extendRetention(until: number, options?: ExtendRetentionOptions | undefined | null, source?: Buffer | undefined | null): Promise<ObjectAttrs | TypedObjectError>
|
|
46
46
|
signedUploadUrl(options?: UploadUrlOptions | undefined | null, source?: Buffer | undefined | null): Promise<SignedUploadUrl | TypedObjectError>
|
|
47
47
|
signedDownloadUrl(options?: DownloadUrlOptions | undefined | null, source?: Buffer | undefined | null): Promise<SignedDownloadUrl | TypedObjectError>
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
/**
|
|
49
|
+
* Begins a download: resolves once the provider has answered, with what
|
|
50
|
+
* it says of the object and its bytes still to come. A missing object
|
|
51
|
+
* fails here, before any bytes.
|
|
52
|
+
*/
|
|
53
|
+
download(options?: DownloadOptions | undefined | null, source?: Buffer | undefined | null): Promise<Download | TypedObjectError>
|
|
51
54
|
delete(options?: DeleteOptions | undefined | null, source?: Buffer | undefined | null): Promise<TypedObjectError | null>
|
|
52
55
|
publicUrl(): string
|
|
53
56
|
}
|
|
@@ -56,9 +59,14 @@ export interface BuildMeta {
|
|
|
56
59
|
revision: string
|
|
57
60
|
}
|
|
58
61
|
|
|
62
|
+
/**
|
|
63
|
+
* `length` bytes from `offset` (to the end of the object without a
|
|
64
|
+
* length), or the object's last `last` bytes.
|
|
65
|
+
*/
|
|
59
66
|
export interface ByteRange {
|
|
60
|
-
offset
|
|
67
|
+
offset?: number
|
|
61
68
|
length?: number
|
|
69
|
+
last?: number
|
|
62
70
|
}
|
|
63
71
|
|
|
64
72
|
/** Which of a database's users a call is made as. See [`clickhouse::Access`]. */
|
|
@@ -224,22 +232,40 @@ export interface DeployMeta {
|
|
|
224
232
|
worker?: string
|
|
225
233
|
}
|
|
226
234
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
235
|
+
/**
|
|
236
|
+
* A download under way: what the provider said of the object, and its
|
|
237
|
+
* bytes, read whole or one chunk at a time.
|
|
238
|
+
*/
|
|
239
|
+
export class Download {
|
|
240
|
+
get info(): DownloadInfo
|
|
241
|
+
/** The rest of the download, in one piece. It closes the download. */
|
|
242
|
+
bytes(): Promise<Buffer | TypedObjectError>
|
|
234
243
|
/**
|
|
235
|
-
* The next chunk, or null at the end. The
|
|
236
|
-
* on an error.
|
|
244
|
+
* The next chunk, or null at the end. The download closes at the end
|
|
245
|
+
* and on an error.
|
|
237
246
|
*/
|
|
238
247
|
next(): Promise<Buffer | null | TypedObjectError>
|
|
239
248
|
/** Stops the download, which ends its span. */
|
|
240
249
|
close(): Promise<void>
|
|
241
250
|
}
|
|
242
251
|
|
|
252
|
+
/** What a download's response says of the object being read. */
|
|
253
|
+
export interface DownloadInfo {
|
|
254
|
+
/** With `version`, the revision the bytes are of. */
|
|
255
|
+
etag: string
|
|
256
|
+
version?: string
|
|
257
|
+
/** The size of the whole object, also when a range of it is read. */
|
|
258
|
+
size: number
|
|
259
|
+
contentType?: string
|
|
260
|
+
/** Milliseconds since the Unix epoch. */
|
|
261
|
+
lastModified?: number
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export interface DownloadOptions {
|
|
265
|
+
version?: string
|
|
266
|
+
range?: ByteRange
|
|
267
|
+
}
|
|
268
|
+
|
|
243
269
|
export interface DownloadUrlOptions {
|
|
244
270
|
ttl?: number
|
|
245
271
|
}
|
|
@@ -407,6 +433,8 @@ export interface ListEntry {
|
|
|
407
433
|
name: string
|
|
408
434
|
size: number
|
|
409
435
|
etag: string
|
|
436
|
+
/** Milliseconds since the Unix epoch. None for a prefix. */
|
|
437
|
+
lastModified?: number
|
|
410
438
|
/** A common prefix of a delimited listing rather than an object. */
|
|
411
439
|
isPrefix: boolean
|
|
412
440
|
}
|
|
@@ -594,6 +622,13 @@ export class RedisConnection {
|
|
|
594
622
|
export class RedisDatabase {
|
|
595
623
|
/** The prefix every key of the database is stored under, if it has one. */
|
|
596
624
|
keyPrefix(): string | null
|
|
625
|
+
/**
|
|
626
|
+
* The database for a handle that only reads: from the primary if
|
|
627
|
+
* `primary`, and otherwise from a read replica where the database has a
|
|
628
|
+
* read-only pool on one. Its commands that write are refused before they
|
|
629
|
+
* are sent. See [`cache::Client::with_access`].
|
|
630
|
+
*/
|
|
631
|
+
reader(primary: boolean): RedisDatabase
|
|
597
632
|
/**
|
|
598
633
|
* The URL a client other than this one connects to the database with,
|
|
599
634
|
* past its key prefix.
|
|
@@ -756,12 +791,19 @@ export class Runtime {
|
|
|
756
791
|
* a name has one scope across the app.
|
|
757
792
|
*/
|
|
758
793
|
secret(encoreName: string): Secret | null
|
|
794
|
+
/**
|
|
795
|
+
* Whether a secret [`secret`](Self::secret) has no value of reads as `""`
|
|
796
|
+
* rather than failing: only where the app runs locally, as `encore run`
|
|
797
|
+
* and `encore test` do, where a secret lacks a value until it is set.
|
|
798
|
+
*/
|
|
799
|
+
unsetSecretsReadEmpty(): boolean
|
|
759
800
|
/** The telemetry pipeline's span lifecycle calls. */
|
|
760
801
|
telemetry(): Pipeline
|
|
761
802
|
/** The meter provider's instrument calls. */
|
|
762
803
|
meters(): Meters
|
|
763
804
|
/** Returns the version of the Encore runtime being used */
|
|
764
805
|
static version(): string
|
|
806
|
+
redact(spec: string, payload: Buffer): string
|
|
765
807
|
appMeta(): AppMeta
|
|
766
808
|
runtimeConfig(): RuntimeConfig
|
|
767
809
|
}
|
|
@@ -915,7 +957,7 @@ export type SQLTransactionOptions = SqlTransactionOptions
|
|
|
915
957
|
export class SystemTest {
|
|
916
958
|
/** The suite the process's tests are of. */
|
|
917
959
|
get suite(): string
|
|
918
|
-
/** The test session's id, as `encore run
|
|
960
|
+
/** The test session's id, as `encore run list` lists it. */
|
|
919
961
|
get session(): string
|
|
920
962
|
/** The session's service proxy: `/<service>/…` there reaches the service. */
|
|
921
963
|
get proxyUrl(): string
|
|
@@ -1177,7 +1219,7 @@ export class VersionIterator {
|
|
|
1177
1219
|
|
|
1178
1220
|
/**
|
|
1179
1221
|
* One log record in the Encore log format, newline included, for the
|
|
1180
|
-
* logger to print.
|
|
1222
|
+
* logger to print. An empty `message` is preserved. `level` is the
|
|
1181
1223
|
* OpenTelemetry severity number, 1–24; `time_unix_millis` absent is now;
|
|
1182
1224
|
* `span_ctx` is the span's 25-byte context: trace id, span id, flags;
|
|
1183
1225
|
* `exception_stacktrace` is in JavaScript's format; `code_*` are the
|
|
@@ -1188,7 +1230,7 @@ export class VersionIterator {
|
|
|
1188
1230
|
* field when valid, `raw.<key>` otherwise; one named like a key of the
|
|
1189
1231
|
* record's own fields is `raw.<key>`. Needs no runtime.
|
|
1190
1232
|
*/
|
|
1191
|
-
export declare function encodeLog(level: number, timeUnixMillis
|
|
1233
|
+
export declare function encodeLog(level: number, timeUnixMillis?: number | undefined | null, message?: string | undefined | null, logger?: string | undefined | null, spanCtx?: Buffer | undefined | null, exceptionType?: string | undefined | null, exceptionMessage?: string | undefined | null, exceptionStacktrace?: string | undefined | null, codeFunction?: string | undefined | null, codeFile?: string | undefined | null, codeLine?: number | undefined | null, codeColumn?: number | undefined | null, runtimeTarget?: string | undefined | null, attrsJson?: string | undefined | null): Buffer
|
|
1192
1234
|
|
|
1193
1235
|
/**
|
|
1194
1236
|
* Lets the request of the `request` event `eventId` through, with
|
|
@@ -1,70 +1,77 @@
|
|
|
1
1
|
import type {Runtime} from './napi/napi.cjs';
|
|
2
2
|
|
|
3
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* Current phase of Encore's graceful shutdown:
|
|
4
5
|
*
|
|
5
6
|
* - `not-started`: shutdown has not begun.
|
|
6
|
-
* - `started`: Pub/Sub intake has stopped; Encore
|
|
7
|
-
* handlers
|
|
8
|
-
* - `cleanup`: Encore is closing
|
|
9
|
-
* - `completed`: cleanup has finished
|
|
7
|
+
* - `started`: Pub/Sub intake has stopped; Encore is waiting for in-flight
|
|
8
|
+
* message handlers and application holds to finish.
|
|
9
|
+
* - `cleanup`: Encore is closing managed resources and flushing telemetry.
|
|
10
|
+
* - `completed`: cleanup has finished (whether it succeeded or timed out).
|
|
11
|
+
*/
|
|
10
12
|
export type ShutdownPhase = 'not-started' | 'started' | 'cleanup' | 'completed';
|
|
11
13
|
|
|
12
|
-
/**
|
|
13
|
-
*
|
|
14
|
-
*
|
|
14
|
+
/**
|
|
15
|
+
* Outcome of a completed shutdown: `'success'` if cleanup finished within the
|
|
16
|
+
* shutdown deadline, or `'timeout'` if the deadline expired. A failed cleanup
|
|
17
|
+
* rejects with {@link ShutdownError}.
|
|
18
|
+
*/
|
|
15
19
|
export type ShutdownResult = 'success' | 'timeout';
|
|
16
20
|
|
|
17
|
-
/** Encore's cleanup
|
|
21
|
+
/** Thrown when Encore's resource cleanup fails or a shutdown API is called invalidly. */
|
|
18
22
|
export class ShutdownError extends Error {
|
|
19
23
|
override name = 'ShutdownError';
|
|
20
24
|
}
|
|
21
25
|
|
|
22
|
-
/**
|
|
23
|
-
*
|
|
24
|
-
*
|
|
26
|
+
/**
|
|
27
|
+
* A hold that delays Encore resource cleanup until {@link release} is called
|
|
28
|
+
* or the shutdown deadline expires. Calling `release()` more than once is a
|
|
29
|
+
* no-op. Implements `Disposable` so `using` releases it automatically at the
|
|
30
|
+
* end of a scope.
|
|
31
|
+
*/
|
|
25
32
|
export interface ShutdownHold extends Disposable {
|
|
26
33
|
release(): void;
|
|
27
34
|
}
|
|
28
35
|
|
|
29
|
-
/**
|
|
36
|
+
/** Context passed to an {@link onShutdown} callback. */
|
|
30
37
|
export interface ShutdownContext {
|
|
31
|
-
/** Aborts when cleanup begins
|
|
32
|
-
*
|
|
38
|
+
/** Aborts when resource cleanup begins (either because the shutdown deadline
|
|
39
|
+
* expired or because cleanup was forced), even if the callback is still running. */
|
|
33
40
|
signal: AbortSignal;
|
|
34
|
-
/**
|
|
35
|
-
* `Date.now()`
|
|
41
|
+
/** Timestamp when the shutdown deadline expires, in milliseconds since the
|
|
42
|
+
* Unix epoch (matching `Date.now()`). */
|
|
36
43
|
deadline: number;
|
|
37
44
|
}
|
|
38
45
|
|
|
39
|
-
/**
|
|
46
|
+
/** Callback invoked when shutdown begins. Resource cleanup waits for any
|
|
47
|
+
* returned promise to settle (up to the shutdown deadline). */
|
|
40
48
|
export type ShutdownCallback = (ctx: ShutdownContext) => void | Promise<void>;
|
|
41
49
|
|
|
42
|
-
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
50
|
+
/** Signals that initiate graceful shutdown. On Windows under Node.js, includes
|
|
51
|
+
* `SIGBREAK` (sent by the daemon when a process does not handle stop requests);
|
|
52
|
+
* Bun terminates immediately on `SIGBREAK` regardless of listeners. */
|
|
45
53
|
const SIGNALS: readonly NodeJS.Signals[] =
|
|
46
54
|
process.platform === 'win32' && !process.versions.bun
|
|
47
55
|
? ['SIGTERM', 'SIGINT', 'SIGBREAK']
|
|
48
56
|
: ['SIGTERM', 'SIGINT'];
|
|
49
|
-
/**
|
|
50
|
-
*
|
|
51
|
-
* members already received. */
|
|
57
|
+
/** Minimum interval after the first signal before the same signal is treated
|
|
58
|
+
* as a deliberate repeat rather than a duplicate forwarded by a process group. */
|
|
52
59
|
const REPEAT_MS = 1000;
|
|
53
60
|
|
|
54
61
|
let runtime: Runtime | undefined;
|
|
55
|
-
/** Whether the runtime
|
|
56
|
-
*
|
|
62
|
+
/** Whether the native runtime owns the event loop ({@link runUntilShutdown}),
|
|
63
|
+
* handling signals natively and letting its caller exit. */
|
|
57
64
|
let hosted = false;
|
|
58
65
|
const callbacks = new Set<ShutdownCallback>();
|
|
59
|
-
/**
|
|
60
|
-
*
|
|
66
|
+
/** Callbacks registered internally by Encore (such as the API framework's
|
|
67
|
+
* server), which do not count as application-managed shutdown hooks. */
|
|
61
68
|
const ownCallbacks = new WeakSet<ShutdownCallback>();
|
|
62
69
|
let holds = 0;
|
|
63
70
|
let begun = false;
|
|
64
71
|
let markBegun!: () => void;
|
|
65
72
|
const begunAt = new Promise<void>(resolve => (markBegun = resolve));
|
|
66
73
|
let deadline = 0;
|
|
67
|
-
/** Whether
|
|
74
|
+
/** Whether the application has already been marked drained (new holds no longer delay cleanup). */
|
|
68
75
|
let drained = false;
|
|
69
76
|
let cleaningUp = false;
|
|
70
77
|
const startAbort = new AbortController();
|
|
@@ -75,29 +82,27 @@ let markCleanup!: () => void;
|
|
|
75
82
|
const cleanupAt = new Promise<void>(resolve => (markCleanup = resolve));
|
|
76
83
|
let completion: Promise<ShutdownResult> | undefined;
|
|
77
84
|
|
|
78
|
-
/**
|
|
79
|
-
* for
|
|
80
|
-
* waiting for its drain. */
|
|
85
|
+
/** Signals that force immediate cleanup when received a second time (a second
|
|
86
|
+
* Ctrl-C, or for gateway processes a second `SIGTERM` from the supervisor). */
|
|
81
87
|
const forcing = new Set<NodeJS.Signals>(['SIGINT']);
|
|
82
88
|
|
|
83
|
-
/**
|
|
89
|
+
/** Signal that initiated shutdown; when set, Encore exits the process once cleanup finishes. */
|
|
84
90
|
let signalled: NodeJS.Signals | undefined;
|
|
85
91
|
let signalledAt = 0;
|
|
86
|
-
/**
|
|
87
|
-
* its shutdown, until it is done. */
|
|
92
|
+
/** Automatic hold kept while an application handles the shutdown signal itself. */
|
|
88
93
|
let appHold: ShutdownHold | undefined;
|
|
89
|
-
/**
|
|
94
|
+
/** Signal re-raised by the application after removing its own listener, re-sent after cleanup. */
|
|
90
95
|
let reraised: NodeJS.Signals | undefined;
|
|
91
|
-
/** Whether Encore
|
|
96
|
+
/** Whether Encore is currently waiting on cleanup to exit the process. */
|
|
92
97
|
let exiting = false;
|
|
93
|
-
/** Whether the process
|
|
98
|
+
/** Whether the process exit path has already started. */
|
|
94
99
|
let finished = false;
|
|
95
100
|
|
|
96
|
-
/** Aborts
|
|
97
|
-
*
|
|
101
|
+
/** Aborts as soon as shutdown begins: Encore stops accepting new Pub/Sub
|
|
102
|
+
* messages, and your application should stop accepting new work as well. */
|
|
98
103
|
export const signal: AbortSignal = startAbort.signal;
|
|
99
104
|
|
|
100
|
-
/** Sets the runtime
|
|
105
|
+
/** Sets the native runtime instance used to coordinate shutdown. */
|
|
101
106
|
export function setShutdownRuntime(rt: Runtime): void {
|
|
102
107
|
runtime = rt;
|
|
103
108
|
}
|
|
@@ -107,13 +112,16 @@ function rt(): Runtime {
|
|
|
107
112
|
return runtime;
|
|
108
113
|
}
|
|
109
114
|
|
|
110
|
-
/**
|
|
111
|
-
*
|
|
112
|
-
*
|
|
115
|
+
/**
|
|
116
|
+
* Delays Encore's resource cleanup until the returned hold is released or the
|
|
117
|
+
* shutdown deadline expires. If called after the application has already
|
|
118
|
+
* drained or cleanup has begun, returns an already-released hold and logs a
|
|
119
|
+
* warning.
|
|
120
|
+
*/
|
|
113
121
|
export function hold(): ShutdownHold {
|
|
114
122
|
if (drained || cleaningUp) {
|
|
115
123
|
console.warn(
|
|
116
|
-
'encore:
|
|
124
|
+
'encore: shutdown hold acquired after the app drained; cleanup will not wait for it',
|
|
117
125
|
);
|
|
118
126
|
const release = () => {};
|
|
119
127
|
return {release, [Symbol.dispose]: release};
|
|
@@ -129,7 +137,7 @@ export function hold(): ShutdownHold {
|
|
|
129
137
|
return {release, [Symbol.dispose]: release};
|
|
130
138
|
}
|
|
131
139
|
|
|
132
|
-
/**
|
|
140
|
+
/** Delays resource cleanup until `work` settles. Any rejection is logged. */
|
|
133
141
|
export function waitUntil(work: PromiseLike<unknown>): void {
|
|
134
142
|
const held = hold();
|
|
135
143
|
Promise.resolve(work)
|
|
@@ -137,26 +145,31 @@ export function waitUntil(work: PromiseLike<unknown>): void {
|
|
|
137
145
|
.finally(() => held.release());
|
|
138
146
|
}
|
|
139
147
|
|
|
140
|
-
/**
|
|
141
|
-
*
|
|
142
|
-
*
|
|
148
|
+
/**
|
|
149
|
+
* Registers `callback` to run when shutdown begins (or immediately if shutdown
|
|
150
|
+
* has already begun), delaying resource cleanup until it settles. Disposing
|
|
151
|
+
* the returned handle unregisters the callback.
|
|
152
|
+
*/
|
|
143
153
|
export function onShutdown(callback: ShutdownCallback): Disposable {
|
|
144
154
|
callbacks.add(callback);
|
|
145
155
|
if (begun) run(callback);
|
|
146
156
|
return {[Symbol.dispose]: () => void callbacks.delete(callback)};
|
|
147
157
|
}
|
|
148
158
|
|
|
149
|
-
/** Registers
|
|
150
|
-
*
|
|
159
|
+
/** Registers an Encore-internal shutdown callback without disabling automatic
|
|
160
|
+
* detection of application-owned signal handlers. */
|
|
151
161
|
export function onShutdownInternal(callback: ShutdownCallback): Disposable {
|
|
152
162
|
ownCallbacks.add(callback);
|
|
153
163
|
return onShutdown(callback);
|
|
154
164
|
}
|
|
155
165
|
|
|
156
|
-
/**
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
*
|
|
166
|
+
/**
|
|
167
|
+
* Initiates graceful shutdown (idempotent): stops Pub/Sub intake, aborts
|
|
168
|
+
* {@link signal}, and invokes all registered {@link onShutdown} callbacks.
|
|
169
|
+
* Encore cleans up resources once every hold is released or the shutdown
|
|
170
|
+
* deadline expires. Outside tests, `SIGTERM` and `SIGINT` call this
|
|
171
|
+
* automatically.
|
|
172
|
+
*/
|
|
160
173
|
export function begin(): void {
|
|
161
174
|
if (begun) return;
|
|
162
175
|
const r = rt();
|
|
@@ -171,26 +184,27 @@ export function begin(): void {
|
|
|
171
184
|
settle();
|
|
172
185
|
}
|
|
173
186
|
|
|
174
|
-
/**
|
|
187
|
+
/** Returns the current shutdown phase. */
|
|
175
188
|
export function phase(): ShutdownPhase {
|
|
176
189
|
return (runtime?.shutdownPhase() ?? 'not-started') as ShutdownPhase;
|
|
177
190
|
}
|
|
178
191
|
|
|
179
|
-
/** Resolves once
|
|
180
|
-
*
|
|
192
|
+
/** Resolves once shutdown has begun and Pub/Sub intake has stopped. Awaiting
|
|
193
|
+
* this promise does not keep the event loop alive. */
|
|
181
194
|
export function started(): Promise<void> {
|
|
182
195
|
return startedAt;
|
|
183
196
|
}
|
|
184
197
|
|
|
185
|
-
/** Resolves once cleanup begins
|
|
186
|
-
*
|
|
198
|
+
/** Resolves once resource cleanup begins (because the app drained, the
|
|
199
|
+
* shutdown deadline expired, or cleanup was forced). Awaiting this promise
|
|
200
|
+
* does not keep the event loop alive. */
|
|
187
201
|
export function cleanup(): Promise<void> {
|
|
188
202
|
return cleanupAt;
|
|
189
203
|
}
|
|
190
204
|
|
|
191
|
-
/** Resolves with the shutdown
|
|
192
|
-
* deadline
|
|
193
|
-
*
|
|
205
|
+
/** Resolves with the shutdown result once cleanup completes (even after the
|
|
206
|
+
* deadline), or rejects with {@link ShutdownError} if cleanup failed. Once
|
|
207
|
+
* shutdown has begun, awaiting this keeps the event loop alive. */
|
|
194
208
|
export function completed(): Promise<ShutdownResult> {
|
|
195
209
|
return (completion ??= begunAt
|
|
196
210
|
.then(() => rt().shutdownCompleted())
|
|
@@ -202,40 +216,41 @@ export function completed(): Promise<ShutdownResult> {
|
|
|
202
216
|
}));
|
|
203
217
|
}
|
|
204
218
|
|
|
205
|
-
/**
|
|
206
|
-
*
|
|
219
|
+
/** Starts resource cleanup immediately without waiting for application holds
|
|
220
|
+
* or in-flight Pub/Sub handlers. Work still running may encounter closed
|
|
221
|
+
* resources. */
|
|
207
222
|
export function forceCleanup(): void {
|
|
208
223
|
if (!begun) throw new ShutdownError('shutdown has not begun');
|
|
209
224
|
rt().forceShutdownCleanup();
|
|
210
225
|
}
|
|
211
226
|
|
|
212
|
-
/**
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
*
|
|
216
|
-
* forces cleanup.
|
|
227
|
+
/**
|
|
228
|
+
* Installs signal listeners for `SIGTERM` and `SIGINT` (and daemon stop
|
|
229
|
+
* requests on Windows) to drive graceful shutdown and exit once cleanup
|
|
230
|
+
* completes. Exits with `process.exitCode` (0 unless set) if cleanup succeeds
|
|
231
|
+
* within the deadline, or 1 otherwise. A second `SIGINT` forces cleanup.
|
|
217
232
|
*
|
|
218
|
-
*
|
|
219
|
-
* until
|
|
220
|
-
*
|
|
233
|
+
* If the application registers its own signal listener without using Encore's
|
|
234
|
+
* shutdown hooks, Encore waits until the application re-raises the signal,
|
|
235
|
+
* exits, or drains its event loop.
|
|
236
|
+
*/
|
|
221
237
|
export function watchShutdownSignals(): void {
|
|
222
238
|
for (const s of SIGNALS) process.on(s, onSignal);
|
|
223
|
-
// Windows has no SIGTERM: the daemon
|
|
224
|
-
// as SIGTERM
|
|
239
|
+
// Windows has no SIGTERM: the daemon signals a named event instead, which we
|
|
240
|
+
// surface as SIGTERM so application listeners see it too.
|
|
225
241
|
rt().watchStopRequest(() => void process.emit('SIGTERM', 'SIGTERM'));
|
|
226
242
|
}
|
|
227
243
|
|
|
228
|
-
/** Runs `
|
|
229
|
-
* runtime handles SIGTERM
|
|
230
|
-
*
|
|
231
|
-
* holds. */
|
|
244
|
+
/** Runs `r` until shutdown completes and returns the exit code. The native
|
|
245
|
+
* runtime handles `SIGTERM`, `SIGINT`, and Windows console close events,
|
|
246
|
+
* triggers JS shutdown here, and waits for application holds before cleanup. */
|
|
232
247
|
export function runUntilShutdown(r: Runtime): Promise<number> {
|
|
233
248
|
runtime = r;
|
|
234
249
|
hosted = true;
|
|
235
250
|
return r.runForever(true, begin);
|
|
236
251
|
}
|
|
237
252
|
|
|
238
|
-
/**
|
|
253
|
+
/** Configures `sig` to force immediate cleanup when received a second time. */
|
|
239
254
|
export function forceCleanupOnRepeat(sig: NodeJS.Signals): void {
|
|
240
255
|
forcing.add(sig);
|
|
241
256
|
}
|
|
@@ -246,18 +261,18 @@ function onSignal(sig: NodeJS.Signals): void {
|
|
|
246
261
|
signalled = sig;
|
|
247
262
|
signalledAt = Date.now();
|
|
248
263
|
if (!appTellsShutdown() && process.listenerCount(sig) > 1) {
|
|
249
|
-
// The app's own listeners run after this one
|
|
264
|
+
// The app's own listeners run after this one and may take time to drain.
|
|
250
265
|
appHold = hold();
|
|
251
266
|
console.warn(
|
|
252
|
-
`encore:
|
|
253
|
-
'
|
|
267
|
+
`encore: detected custom ${sig} handler; Encore will clean up after it finishes. ` +
|
|
268
|
+
'Use onShutdown() from @encore.dev/sdk/shutdown to coordinate shutdown explicitly.',
|
|
254
269
|
);
|
|
255
270
|
}
|
|
256
271
|
process.on('beforeExit', onIdle);
|
|
257
272
|
begin();
|
|
258
273
|
}
|
|
259
274
|
|
|
260
|
-
/**
|
|
275
|
+
/** Returns whether the application has any active holds or user-registered callbacks. */
|
|
261
276
|
function appTellsShutdown(): boolean {
|
|
262
277
|
if (holds > 0) return true;
|
|
263
278
|
for (const callback of callbacks) {
|
|
@@ -269,8 +284,7 @@ function appTellsShutdown(): boolean {
|
|
|
269
284
|
function repeated(sig: NodeJS.Signals): void {
|
|
270
285
|
if (!signalled) return;
|
|
271
286
|
if (appHold && sig === signalled && process.listenerCount(sig) === 1) {
|
|
272
|
-
// The app
|
|
273
|
-
// the signal it handled counts: another may arrive while it still runs.
|
|
287
|
+
// The app removed its own listener and re-raised the original signal.
|
|
274
288
|
reraised = sig;
|
|
275
289
|
appHold.release();
|
|
276
290
|
return;
|
|
@@ -280,12 +294,11 @@ function repeated(sig: NodeJS.Signals): void {
|
|
|
280
294
|
}
|
|
281
295
|
}
|
|
282
296
|
|
|
283
|
-
/**
|
|
284
|
-
*
|
|
285
|
-
* open on the event loop may yet release them. */
|
|
297
|
+
/** Called when the event loop empties: releases any automatic app-signal hold
|
|
298
|
+
* while still honoring explicit holds until the deadline. */
|
|
286
299
|
function onIdle(): void {
|
|
287
|
-
//
|
|
288
|
-
//
|
|
300
|
+
// Defer one turn so a signal re-raised while closing the last handle is
|
|
301
|
+
// observed first.
|
|
289
302
|
setImmediate(() => {
|
|
290
303
|
appHold?.release();
|
|
291
304
|
if (!finished) exitOnCompletion();
|
|
@@ -299,11 +312,11 @@ function settle(): void {
|
|
|
299
312
|
function markDrained(): void {
|
|
300
313
|
drained = true;
|
|
301
314
|
rt().markApplicationDrained();
|
|
302
|
-
//
|
|
315
|
+
// Once the app is drained, keep the event loop alive while cleanup finishes.
|
|
303
316
|
if (signalled) exitOnCompletion();
|
|
304
317
|
}
|
|
305
318
|
|
|
306
|
-
/**
|
|
319
|
+
/** Waits for cleanup to complete (keeping the event loop alive) and exits. */
|
|
307
320
|
function exitOnCompletion(): void {
|
|
308
321
|
if (exiting) return;
|
|
309
322
|
exiting = true;
|
|
@@ -315,12 +328,11 @@ function exitOnCompletion(): void {
|
|
|
315
328
|
|
|
316
329
|
function onPhase(phase: string, result?: string): void {
|
|
317
330
|
reached(phase);
|
|
318
|
-
// Cleanup also completes
|
|
319
|
-
// or not; the exit code then says it was cut short.
|
|
331
|
+
// Cleanup also completes if the deadline expires before the app finishes.
|
|
320
332
|
if (phase === 'completed' && signalled) exit(result ?? 'failed');
|
|
321
333
|
}
|
|
322
334
|
|
|
323
|
-
/**
|
|
335
|
+
/** Resolves promises waiting on `phase` and any earlier phases. */
|
|
324
336
|
function reached(phase: string): void {
|
|
325
337
|
if (phase === 'cleanup' || phase === 'completed') {
|
|
326
338
|
cleaningUp = true;
|
|
@@ -330,12 +342,12 @@ function reached(phase: string): void {
|
|
|
330
342
|
markStarted();
|
|
331
343
|
}
|
|
332
344
|
|
|
333
|
-
/** Exits with 1 if cleanup failed or timed out
|
|
334
|
-
*
|
|
345
|
+
/** Exits with 1 if cleanup failed or timed out; otherwise re-raises any signal
|
|
346
|
+
* the application re-raised, or exits with `process.exitCode`. */
|
|
335
347
|
function exit(result: string): void {
|
|
336
348
|
if (finished) return;
|
|
337
349
|
finished = true;
|
|
338
|
-
// Completion may
|
|
350
|
+
// Completion may arrive before intermediate phase callbacks.
|
|
339
351
|
reached('completed');
|
|
340
352
|
if (result !== 'success') process.exit(1);
|
|
341
353
|
if (reraised) {
|
|
@@ -346,8 +358,8 @@ function exit(result: string): void {
|
|
|
346
358
|
process.exit();
|
|
347
359
|
}
|
|
348
360
|
|
|
349
|
-
/**
|
|
350
|
-
* cleanup
|
|
361
|
+
/** Handles synchronous process exit initiated by the application: finishes
|
|
362
|
+
* cleanup synchronously and overrides the exit code if cleanup fails. */
|
|
351
363
|
function finishOnExit(): void {
|
|
352
364
|
if (finished) return;
|
|
353
365
|
finished = true;
|
package/log/logger.test.ts
CHANGED
|
@@ -126,6 +126,14 @@ describe('Logger', () => {
|
|
|
126
126
|
expect(fatal).toMatchObject(exception);
|
|
127
127
|
});
|
|
128
128
|
|
|
129
|
+
test('an empty message is distinct from an omitted message', () => {
|
|
130
|
+
const {log, records} = setup();
|
|
131
|
+
const err = new Error('boom');
|
|
132
|
+
log.error(err, '');
|
|
133
|
+
log.error(err);
|
|
134
|
+
expect(records().map(r => r.message)).toEqual(['', undefined]);
|
|
135
|
+
});
|
|
136
|
+
|
|
129
137
|
test('a message is not an error; a non-error is wrapped', () => {
|
|
130
138
|
const {log, records} = setup();
|
|
131
139
|
log.warn('plain failure', {id: 1});
|