@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/storage/redis/mod.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Redis databases.
|
|
3
|
-
* is
|
|
2
|
+
* Redis databases. Declare a database at module scope; its connection pool
|
|
3
|
+
* is created on first use and reused thereafter.
|
|
4
4
|
*
|
|
5
5
|
* ```ts
|
|
6
6
|
* import { Database } from "@encore.dev/sdk/storage/redis";
|
|
@@ -13,22 +13,22 @@
|
|
|
13
13
|
* }
|
|
14
14
|
* ```
|
|
15
15
|
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
16
|
+
* Each database is a standalone resource with no clusters or keyspaces to
|
|
17
|
+
* declare; keys are arbitrary strings passed by your code.
|
|
18
18
|
*
|
|
19
19
|
* ## Commands
|
|
20
20
|
*
|
|
21
|
-
* Commands are grouped by
|
|
22
|
-
*
|
|
23
|
-
* `
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* `
|
|
27
|
-
* `zrangeWithscores`.
|
|
21
|
+
* Commands are grouped by data type as properties on the database: `string`,
|
|
22
|
+
* `hash`, `list`, `set`, `sortedSet`, `stream`, `geo`, `bitmap`,
|
|
23
|
+
* `hyperloglog`, `pubsub`, and `generic` (for commands that work on any key).
|
|
24
|
+
* Within each group, methods are named after the underlying Redis command —
|
|
25
|
+
* `get`, `hset`, `zadd`, `xadd` — with descriptive names for variants such as
|
|
26
|
+
* `setEx` and `zrangeWithscores`.
|
|
28
27
|
*
|
|
29
|
-
*
|
|
30
|
-
* `sismember` a boolean.
|
|
31
|
-
* {@link ReplyPromise},
|
|
28
|
+
* Commands with a fixed reply type return it directly (`strlen` returns a
|
|
29
|
+
* number; `sismember` returns a boolean). Commands whose reply format depends
|
|
30
|
+
* on the caller return a {@link ReplyPromise}, which you can decode into the
|
|
31
|
+
* desired type:
|
|
32
32
|
*
|
|
33
33
|
* ```ts
|
|
34
34
|
* const name = await SESSIONS.string.get("name:1").text();
|
|
@@ -36,45 +36,47 @@
|
|
|
36
36
|
* const fields = await SESSIONS.hash.hgetall("user:1").textMap();
|
|
37
37
|
* ```
|
|
38
38
|
*
|
|
39
|
-
* A missing key
|
|
39
|
+
* A missing key decodes as `null`.
|
|
40
40
|
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
41
|
+
* Always call a command in the same expression that accesses its group. This
|
|
42
|
+
* makes the command visible at the call site so Encore's parser can statically
|
|
43
|
+
* grant each service only the access it uses. Storing or passing a command
|
|
44
|
+
* group obscures how it will be used and results in a compile error — to pass
|
|
45
|
+
* database access around, create a handle instead.
|
|
46
46
|
*
|
|
47
|
-
*
|
|
48
|
-
* `
|
|
49
|
-
*
|
|
50
|
-
*
|
|
47
|
+
* Blocking commands — such as `list.blpop`, `sortedSet.bzpopmin`, and
|
|
48
|
+
* `stream.xreadBlock` — run on a dedicated connection so they do not block
|
|
49
|
+
* other calls. A timeout resolves to `null`. Blocking commands cannot be
|
|
50
|
+
* queued in pipelines or transactions, and cannot run on an acquired
|
|
51
|
+
* connection.
|
|
51
52
|
*
|
|
52
53
|
* ## Any command
|
|
53
54
|
*
|
|
54
|
-
* {@link DatabaseRef.command} sends a command
|
|
55
|
-
*
|
|
55
|
+
* {@link DatabaseRef.command} sends a command that is not covered by the typed
|
|
56
|
+
* command groups, by specifying its name and marking any key arguments:
|
|
56
57
|
*
|
|
57
58
|
* ```ts
|
|
58
59
|
* const freq = await SESSIONS.command("OBJECT").arg("FREQ").key("hot").invoke().number();
|
|
59
60
|
* ```
|
|
60
61
|
*
|
|
61
|
-
* The name must be a string literal
|
|
62
|
-
*
|
|
63
|
-
* command table:
|
|
64
|
-
* that
|
|
65
|
-
*
|
|
66
|
-
* `FLUSHALL`, …) fail with a `TypeError`. A handle
|
|
67
|
-
*
|
|
62
|
+
* The command name must be a string literal so Encore can statically grant the
|
|
63
|
+
* required access. Before sending the command, the runtime validates it against
|
|
64
|
+
* Redis's command table: every key argument must be passed with `key()`, and
|
|
65
|
+
* commands that reach outside the database, alter a shared connection, or
|
|
66
|
+
* already have dedicated APIs (`MULTI`, `SUBSCRIBE`, `EVAL`, `SCAN`,
|
|
67
|
+
* `FLUSHALL`, …) fail with a `TypeError`. A read-only handle only allows
|
|
68
|
+
* commands that Redis marks as read-only.
|
|
68
69
|
*
|
|
69
70
|
* ## Handles
|
|
70
71
|
*
|
|
71
|
-
* A
|
|
72
|
-
*
|
|
73
|
-
* {@link Database.
|
|
74
|
-
*
|
|
72
|
+
* A {@link Database} instance cannot be passed around as a value. To pass
|
|
73
|
+
* database access to other code, create a handle with the permissions that
|
|
74
|
+
* code needs — {@link Database.write}, {@link Database.read}, or
|
|
75
|
+
* {@link Database.readPrimary}. Creating a handle grants the calling service
|
|
76
|
+
* full access at that permission level:
|
|
75
77
|
*
|
|
76
78
|
* ```ts
|
|
77
|
-
* // Library code takes a handle
|
|
79
|
+
* // Library code takes a handle and can only perform the operations it allows.
|
|
78
80
|
* function nameOf(db: ReadOnlyDatabaseRef, user: string) {
|
|
79
81
|
* return db.string.get(`name:${user}`).text();
|
|
80
82
|
* }
|
|
@@ -86,16 +88,18 @@
|
|
|
86
88
|
*
|
|
87
89
|
* ## Keys
|
|
88
90
|
*
|
|
89
|
-
* Every key is stored under a
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
* -
|
|
95
|
-
*
|
|
96
|
-
* database's isolation, so
|
|
97
|
-
*
|
|
98
|
-
*
|
|
91
|
+
* Every key is automatically stored under a database-specific prefix to isolate
|
|
92
|
+
* databases sharing a server. The SDK applies the prefix to every key passed to
|
|
93
|
+
* a command and strips it from every key returned, so application code never
|
|
94
|
+
* sees it. Keep two caveats in mind:
|
|
95
|
+
*
|
|
96
|
+
* - A {@link Script}'s `KEYS` are prefixed automatically, but nothing else in
|
|
97
|
+
* the script is. Any key referenced directly in script source or `ARGV` is
|
|
98
|
+
* accessed unprefixed, outside the database's isolation, so always pass keys
|
|
99
|
+
* through `KEYS`.
|
|
100
|
+
* - {@link DatabaseRef.connectionString} connects without key prefixing, so
|
|
101
|
+
* external clients will not see keys written by this SDK under the same
|
|
102
|
+
* names.
|
|
99
103
|
*
|
|
100
104
|
* @module
|
|
101
105
|
*/
|
package/storage/redis/options.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import type {RedisArg} from './wire';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
* (`px`), at a Unix
|
|
6
|
-
*
|
|
4
|
+
* Expiration setting for `setOptions`: expire after a relative duration in
|
|
5
|
+
* seconds (`ex`) or milliseconds (`px`), at a Unix timestamp in seconds
|
|
6
|
+
* (`exAt`) or milliseconds (`pxAt`), or preserve the key's existing expiration
|
|
7
|
+
* (`keepTtl`).
|
|
7
8
|
*/
|
|
8
9
|
export type SetExpiry =
|
|
9
10
|
| {ex: number}
|
|
@@ -12,25 +13,25 @@ export type SetExpiry =
|
|
|
12
13
|
| {pxAt: number}
|
|
13
14
|
| {keepTtl: true};
|
|
14
15
|
|
|
15
|
-
/**
|
|
16
|
+
/** Options for setting a key with `setOptions`. */
|
|
16
17
|
export interface SetOptions {
|
|
17
18
|
/**
|
|
18
|
-
*
|
|
19
|
-
*
|
|
19
|
+
* Condition for setting the key: only if it does not exist (`"nx"`), or only
|
|
20
|
+
* if it already exists (`"xx"`).
|
|
20
21
|
*/
|
|
21
22
|
condition?: 'nx' | 'xx';
|
|
22
|
-
/**
|
|
23
|
+
/** Return the key's previous value, or `null` if it did not exist. */
|
|
23
24
|
get?: boolean;
|
|
24
25
|
/**
|
|
25
|
-
* When the key
|
|
26
|
-
*
|
|
26
|
+
* When the key should expire. If omitted, the key does not expire and any
|
|
27
|
+
* existing expiration is cleared.
|
|
27
28
|
*/
|
|
28
29
|
expiry?: SetExpiry;
|
|
29
30
|
}
|
|
30
31
|
|
|
31
32
|
/**
|
|
32
|
-
*
|
|
33
|
-
*
|
|
33
|
+
* Expiration update for `getEx`: set a new expiration as in {@link SetExpiry},
|
|
34
|
+
* or remove the existing expiration (`persist`).
|
|
34
35
|
*/
|
|
35
36
|
export type Expiry =
|
|
36
37
|
| {ex: number}
|
|
@@ -39,40 +40,41 @@ export type Expiry =
|
|
|
39
40
|
| {pxAt: number}
|
|
40
41
|
| {persist: true};
|
|
41
42
|
|
|
42
|
-
/**
|
|
43
|
+
/** The end of a list: its head (`"left"`) or its tail (`"right"`). */
|
|
43
44
|
export type Direction = 'left' | 'right';
|
|
44
45
|
|
|
45
|
-
/** A unit of distance: meters, kilometers, miles or feet. */
|
|
46
|
+
/** A unit of distance: meters, kilometers, miles, or feet. */
|
|
46
47
|
export type Unit = 'm' | 'km' | 'mi' | 'ft';
|
|
47
48
|
|
|
48
|
-
/**
|
|
49
|
+
/** Options controlling which fields and how many results a geo radius search returns. */
|
|
49
50
|
export interface RadiusOptions {
|
|
50
|
-
/**
|
|
51
|
+
/** Include each member's longitude and latitude coordinates. */
|
|
51
52
|
withCoord?: boolean;
|
|
52
|
-
/**
|
|
53
|
+
/** Include each member's distance from the search center. */
|
|
53
54
|
withDist?: boolean;
|
|
54
|
-
/**
|
|
55
|
+
/** Include each member's geohash integer. */
|
|
55
56
|
withHash?: boolean;
|
|
56
|
-
/**
|
|
57
|
+
/** Maximum number of matching members to return. */
|
|
57
58
|
count?: number;
|
|
58
|
-
/**
|
|
59
|
+
/** Sort order: nearest first (`"asc"`) or farthest first (`"desc"`). Unsorted if omitted. */
|
|
59
60
|
order?: 'asc' | 'desc';
|
|
60
61
|
}
|
|
61
62
|
|
|
62
63
|
/**
|
|
63
|
-
*
|
|
64
|
-
*
|
|
64
|
+
* Maximum number of entries to keep when trimming a stream: exact (`approx`
|
|
65
|
+
* omitted or `false`), or approximate (`approx: true`), which keeps at least
|
|
66
|
+
* `maxlen` entries and is cheaper to compute.
|
|
65
67
|
*/
|
|
66
68
|
export interface StreamMaxlen {
|
|
67
69
|
maxlen: number;
|
|
68
70
|
approx?: boolean;
|
|
69
71
|
}
|
|
70
72
|
|
|
71
|
-
/**
|
|
73
|
+
/** Options controlling what a scan matches and how much work it does per round trip. */
|
|
72
74
|
export interface ScanOptions {
|
|
73
|
-
/** Only
|
|
75
|
+
/** Only return elements matching a glob-style pattern (for example, `user:*`). */
|
|
74
76
|
pattern?: string;
|
|
75
|
-
/**
|
|
77
|
+
/** Hint for how many elements to inspect per round trip. */
|
|
76
78
|
count?: number;
|
|
77
79
|
}
|
|
78
80
|
|
package/storage/redis/pubsub.ts
CHANGED
|
@@ -9,32 +9,32 @@ import {encodeList} from './wire';
|
|
|
9
9
|
export class Message {
|
|
10
10
|
/** @internal */
|
|
11
11
|
constructor(
|
|
12
|
-
/** The channel
|
|
12
|
+
/** The channel the message was published to. */
|
|
13
13
|
readonly channel: string,
|
|
14
|
-
/**
|
|
14
|
+
/** The raw message payload as published. */
|
|
15
15
|
readonly payload: Buffer,
|
|
16
16
|
) {}
|
|
17
17
|
|
|
18
|
-
/**
|
|
18
|
+
/** Returns the payload decoded as UTF-8 text. */
|
|
19
19
|
text(): string {
|
|
20
20
|
return this.payload.toString('utf8');
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
/**
|
|
23
|
+
/** Parses the UTF-8 payload as JSON. */
|
|
24
24
|
json<T = unknown>(): T {
|
|
25
25
|
return JSON.parse(this.text()) as T;
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
/**
|
|
30
|
-
* An
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* `@encore.dev/sdk/pubsub`.
|
|
30
|
+
* An active Redis pub/sub subscription running on a dedicated connection.
|
|
31
|
+
* Redis pub/sub delivers messages only to subscribers connected at the time of
|
|
32
|
+
* publishing. For durable messaging where messages are retained until handled,
|
|
33
|
+
* use `@encore.dev/sdk/pubsub`.
|
|
34
34
|
*
|
|
35
|
-
* Iterate over
|
|
36
|
-
*
|
|
37
|
-
* closes it
|
|
35
|
+
* Iterate over the subscription with `for await` to receive messages. Call
|
|
36
|
+
* {@link Subscription.close} when done to unsubscribe and close the underlying
|
|
37
|
+
* connection; breaking out of a `for await` loop closes it automatically.
|
|
38
38
|
*
|
|
39
39
|
* ```ts
|
|
40
40
|
* const sub = await EVENTS.pubsub.subscribe("orders");
|
|
@@ -47,7 +47,7 @@ export class Subscription implements AsyncIterable<Message>, AsyncDisposable {
|
|
|
47
47
|
/** @internal */
|
|
48
48
|
constructor(private readonly impl: runtime.RedisSubscription) {}
|
|
49
49
|
|
|
50
|
-
/**
|
|
50
|
+
/** Returns the next message, or `null` once the subscription has closed. */
|
|
51
51
|
async next(): Promise<Message | null> {
|
|
52
52
|
const message = await this.impl.next();
|
|
53
53
|
if (message === null) return null;
|
|
@@ -66,7 +66,7 @@ export class Subscription implements AsyncIterable<Message>, AsyncDisposable {
|
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
/** Closes the subscription. Safe to call
|
|
69
|
+
/** Closes the subscription. Safe to call multiple times. */
|
|
70
70
|
async close(): Promise<void> {
|
|
71
71
|
await this.impl.close();
|
|
72
72
|
}
|
|
@@ -76,7 +76,7 @@ export class Subscription implements AsyncIterable<Message>, AsyncDisposable {
|
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
-
/** Subscribes to `channels` and to
|
|
79
|
+
/** Subscribes to `channels` and to channels matching `patterns`. */
|
|
80
80
|
export async function subscribe(
|
|
81
81
|
t: Target,
|
|
82
82
|
channels: string[],
|
|
@@ -174,8 +174,16 @@ function executeEach(batch: Buffer, atomic: boolean): Promise<Buffer> {
|
|
|
174
174
|
return Promise.resolve(encode(cmds.map(answer)));
|
|
175
175
|
}
|
|
176
176
|
|
|
177
|
+
/** Whether each reader the runtime was asked for reads from the primary. */
|
|
178
|
+
const readers: boolean[] = [];
|
|
179
|
+
|
|
177
180
|
const native = {
|
|
178
181
|
keyPrefix: () => server.prefix,
|
|
182
|
+
// The runtime's reader refuses writes itself; here it is the same handle.
|
|
183
|
+
reader: (primary: boolean) => {
|
|
184
|
+
readers.push(primary);
|
|
185
|
+
return native;
|
|
186
|
+
},
|
|
179
187
|
connectionUrl: () => 'redis://127.0.0.1:6379/0',
|
|
180
188
|
execute,
|
|
181
189
|
executeEach,
|
|
@@ -527,8 +535,15 @@ describe('handles', () => {
|
|
|
527
535
|
expect((reader.pubsub as Record<string, unknown>).publish).toBeUndefined();
|
|
528
536
|
expect(typeof reader.pubsub.subscribe).toBe('function');
|
|
529
537
|
expect(typeof reader.generic.scan).toBe('function');
|
|
530
|
-
|
|
531
|
-
|
|
538
|
+
|
|
539
|
+
// Each reads through the runtime's reader for it, made once.
|
|
540
|
+
script('GET', 'v');
|
|
541
|
+
expect(await reader.string.get('k').text()).toBe('v');
|
|
542
|
+
expect(await DB.readPrimary().string.get('k').text()).toBe('v');
|
|
543
|
+
expect(await DB.read().string.get('k').text()).toBe('v');
|
|
544
|
+
expect(readers.filter(primary => !primary)).toEqual([false]);
|
|
545
|
+
expect(readers.filter(primary => primary)).toEqual([true]);
|
|
546
|
+
server.sent = [];
|
|
532
547
|
|
|
533
548
|
const writer = DB.write();
|
|
534
549
|
await writer.string.set('k', 'v');
|