@encore.dev/sdk 2.0.0-beta.15 → 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 +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
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* Normalized PostgreSQL error categories mapped from SQLSTATE codes for common
|
|
3
|
+
* application error handling. Unmapped SQLSTATEs use `other` (inspect
|
|
4
|
+
* {@link DatabaseError.sqlState} for the exact code).
|
|
4
5
|
*
|
|
5
6
|
* @example
|
|
6
7
|
* try {
|
|
@@ -13,87 +14,80 @@
|
|
|
13
14
|
* }
|
|
14
15
|
*/
|
|
15
16
|
export const ErrorCode = {
|
|
16
|
-
/**
|
|
17
|
+
/** Any other SQLSTATE; see `DatabaseError.sqlState` for details. */
|
|
17
18
|
Other: 'other',
|
|
18
|
-
/** A `NOT NULL` constraint
|
|
19
|
+
/** A `NOT NULL` constraint was violated (SQLSTATE 23502). */
|
|
19
20
|
NotNullViolation: 'not_null_violation',
|
|
20
|
-
/** A foreign key constraint
|
|
21
|
+
/** A foreign key constraint was violated (SQLSTATE 23503). */
|
|
21
22
|
ForeignKeyViolation: 'foreign_key_violation',
|
|
22
|
-
/** A unique constraint
|
|
23
|
+
/** A unique constraint was violated (SQLSTATE 23505). */
|
|
23
24
|
UniqueViolation: 'unique_violation',
|
|
24
|
-
/** A check constraint
|
|
25
|
+
/** A check constraint was violated (SQLSTATE 23514). */
|
|
25
26
|
CheckViolation: 'check_violation',
|
|
26
|
-
/** An exclusion constraint
|
|
27
|
+
/** An exclusion constraint was violated (SQLSTATE 23P01). */
|
|
27
28
|
ExcludeViolation: 'exclude_violation',
|
|
28
|
-
/** A statement ran in a transaction an earlier
|
|
29
|
+
/** A statement ran in a transaction aborted by an earlier error (SQLSTATE 25P02). */
|
|
29
30
|
TransactionFailed: 'transaction_failed',
|
|
30
|
-
/** A deadlock was detected (40P01). */
|
|
31
|
+
/** A deadlock was detected (SQLSTATE 40P01). */
|
|
31
32
|
DeadlockDetected: 'deadlock_detected',
|
|
32
|
-
/** The server
|
|
33
|
+
/** The server rejected the connection due to its connection limit (SQLSTATE 53300). */
|
|
33
34
|
TooManyConnections: 'too_many_connections',
|
|
34
35
|
/**
|
|
35
|
-
* A serializable or repeatable
|
|
36
|
-
*
|
|
36
|
+
* A serializable or repeatable-read transaction conflicted with a concurrent
|
|
37
|
+
* transaction (SQLSTATE 40001); retrying the transaction may succeed.
|
|
37
38
|
*/
|
|
38
39
|
SerializationFailure: 'serialization_failure',
|
|
39
|
-
/** A write in a read-only transaction (25006), such as
|
|
40
|
+
/** A write was attempted in a read-only transaction (SQLSTATE 25006), such as via a read handle. */
|
|
40
41
|
ReadOnlyTransaction: 'read_only_transaction',
|
|
41
42
|
/**
|
|
42
|
-
* A lock could not be
|
|
43
|
-
* `FOR UPDATE NOWAIT` or `lock_timeout`.
|
|
43
|
+
* A lock could not be acquired without waiting (SQLSTATE 55P03), such as
|
|
44
|
+
* with `FOR UPDATE NOWAIT` or `lock_timeout`.
|
|
44
45
|
*/
|
|
45
46
|
LockNotAvailable: 'lock_not_available',
|
|
46
|
-
/** The statement was canceled
|
|
47
|
+
/** The statement was canceled by timeout or explicit request (SQLSTATE 57014). */
|
|
47
48
|
QueryCanceled: 'query_canceled',
|
|
48
|
-
/** The database could not be reached
|
|
49
|
+
/** The database could not be reached or the connection was lost (SQLSTATE class 08). */
|
|
49
50
|
ConnectionFailure: 'connection_failure',
|
|
50
51
|
} as const;
|
|
51
52
|
|
|
52
|
-
/**
|
|
53
|
+
/** Union of {@link ErrorCode} string values (e.g. `'unique_violation'`). */
|
|
53
54
|
export type ErrorCode = (typeof ErrorCode)[keyof typeof ErrorCode];
|
|
54
55
|
|
|
55
56
|
/**
|
|
56
|
-
*
|
|
57
|
+
* Error reported by PostgreSQL or caused by a connection failure.
|
|
57
58
|
*
|
|
58
|
-
*
|
|
59
|
-
* `
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
* errors instead.
|
|
59
|
+
* Connection failures (which do not originate from the server) have `sqlState`
|
|
60
|
+
* `"08001"`, `code` `"connection_failure"`, the client error message, and no
|
|
61
|
+
* optional fields. Caller mistakes (such as invalid parameter types or using a
|
|
62
|
+
* closed handle) throw standard `Error`/`TypeError` instances instead.
|
|
63
63
|
*
|
|
64
|
-
*
|
|
65
|
-
* https://www.postgresql.org/docs/current/protocol-error-fields.html)
|
|
66
|
-
* being there says nothing about the others.
|
|
64
|
+
* Optional diagnostic fields are populated only when sent by PostgreSQL (see
|
|
65
|
+
* https://www.postgresql.org/docs/current/protocol-error-fields.html).
|
|
67
66
|
*
|
|
68
|
-
*
|
|
69
|
-
* "users_pkey" (Code unique_violation: SQLSTATE 23505)`.
|
|
70
|
-
* handler that throws one fails with the API error code of its SQLSTATE:
|
|
71
|
-
* `already_exists` for a unique violation.
|
|
67
|
+
* `message` is formatted like `ERROR: duplicate key value violates unique
|
|
68
|
+
* constraint "users_pkey" (Code unique_violation: SQLSTATE 23505)`.
|
|
72
69
|
*/
|
|
73
70
|
export class DatabaseError extends Error {
|
|
74
71
|
declare readonly name: 'DatabaseError';
|
|
75
|
-
/**
|
|
72
|
+
/** Normalized error category derived from `sqlState`. */
|
|
76
73
|
readonly code: ErrorCode;
|
|
77
|
-
/**
|
|
74
|
+
/** Five-character PostgreSQL SQLSTATE code (e.g. `"23505"`). */
|
|
78
75
|
readonly sqlState: string;
|
|
79
|
-
/** `ERROR`, `FATAL
|
|
76
|
+
/** PostgreSQL severity (`ERROR`, `FATAL`, or `PANIC`). */
|
|
80
77
|
readonly severity: string;
|
|
81
|
-
/**
|
|
82
|
-
* A secondary message with more detail, such as the key a unique
|
|
83
|
-
* violation conflicted on.
|
|
84
|
-
*/
|
|
78
|
+
/** Optional secondary detail message (e.g. the conflicting key in a unique violation). */
|
|
85
79
|
readonly detail?: string;
|
|
86
|
-
/**
|
|
80
|
+
/** Optional PostgreSQL hint suggesting how to resolve the error. */
|
|
87
81
|
readonly hint?: string;
|
|
88
|
-
/**
|
|
82
|
+
/** Schema name associated with the error, if provided. */
|
|
89
83
|
readonly schemaName?: string;
|
|
90
|
-
/**
|
|
84
|
+
/** Table name associated with the error, if provided. */
|
|
91
85
|
readonly tableName?: string;
|
|
92
|
-
/**
|
|
86
|
+
/** Column name associated with the error, if provided. */
|
|
93
87
|
readonly columnName?: string;
|
|
94
|
-
/**
|
|
88
|
+
/** Data type name associated with the error, if provided. */
|
|
95
89
|
readonly dataTypeName?: string;
|
|
96
|
-
/**
|
|
90
|
+
/** Constraint or index name associated with the error, if provided. */
|
|
97
91
|
readonly constraintName?: string;
|
|
98
92
|
|
|
99
93
|
constructor(fields: {
|
|
@@ -128,7 +122,7 @@ export class DatabaseError extends Error {
|
|
|
128
122
|
}
|
|
129
123
|
}
|
|
130
124
|
|
|
131
|
-
/** Thrown by `queryRow` and `rawQueryRow`
|
|
125
|
+
/** Thrown by `queryRow` and `rawQueryRow` when a query returns more than one row. */
|
|
132
126
|
export class TooManyRowsError extends Error {
|
|
133
127
|
declare readonly name: 'TooManyRowsError';
|
|
134
128
|
|
|
@@ -143,15 +137,14 @@ export class TooManyRowsError extends Error {
|
|
|
143
137
|
}
|
|
144
138
|
|
|
145
139
|
/**
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
* `DATABASE_ERROR_PREFIX` in `sdk/ts/src/postgres.rs`.
|
|
140
|
+
* Prefix for JSON-encoded `DatabaseError` messages from the native module.
|
|
141
|
+
* Must match `DATABASE_ERROR_PREFIX` in `sdk/ts/src/postgres.rs`.
|
|
149
142
|
*/
|
|
150
143
|
const DATABASE_ERROR_PREFIX = 'encore:postgres.DatabaseError:';
|
|
151
144
|
|
|
152
145
|
/**
|
|
153
|
-
* Awaits a native database call
|
|
154
|
-
*
|
|
146
|
+
* Awaits a native database call and converts encoded native errors into
|
|
147
|
+
* {@link DatabaseError}.
|
|
155
148
|
*/
|
|
156
149
|
export async function native<T>(call: Promise<T>): Promise<T> {
|
|
157
150
|
try {
|
|
@@ -161,7 +154,7 @@ export async function native<T>(call: Promise<T>): Promise<T> {
|
|
|
161
154
|
}
|
|
162
155
|
}
|
|
163
156
|
|
|
164
|
-
/**
|
|
157
|
+
/** Converts an encoded native error into a {@link DatabaseError}, passing other errors through. */
|
|
165
158
|
function fromNative(err: unknown): unknown {
|
|
166
159
|
if (
|
|
167
160
|
!(err instanceof Error) ||
|
|
@@ -4,15 +4,16 @@ const MICROS_PER_HOUR = 60 * MICROS_PER_MINUTE;
|
|
|
4
4
|
const INT32_MAX = 2 ** 31 - 1;
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* (
|
|
7
|
+
* Represents a PostgreSQL `interval` with its three independent components —
|
|
8
|
+
* `months`, `days`, and `microseconds` — matching PostgreSQL's internal model
|
|
9
|
+
* (months do not have a fixed number of days, and days do not have a fixed
|
|
10
|
+
* number of microseconds across daylight-saving transitions).
|
|
10
11
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* `
|
|
15
|
-
*
|
|
12
|
+
* `interval` columns are returned as ISO 8601 strings (e.g.
|
|
13
|
+
* `'P1Y2M3DT4H5M6.5S'` or `'P1M-2D'`), which can be parsed with
|
|
14
|
+
* {@link Interval.parse}. Query parameters for `interval` columns accept an
|
|
15
|
+
* `Interval`, a `Temporal.Duration`, or any string PostgreSQL parses (e.g.
|
|
16
|
+
* `'1 day 02:00:00'`, `'P1DT2H'`, `'90 minutes'`).
|
|
16
17
|
*
|
|
17
18
|
* @example
|
|
18
19
|
* const row = await db.queryRow`SELECT ${new Interval(1, 0, 0)} + now() AS due`;
|
|
@@ -21,9 +22,9 @@ const INT32_MAX = 2 ** 31 - 1;
|
|
|
21
22
|
*/
|
|
22
23
|
export class Interval {
|
|
23
24
|
/**
|
|
24
|
-
* @param months - Whole months
|
|
25
|
-
* @param days - Whole days
|
|
26
|
-
* @param microseconds - Whole microseconds
|
|
25
|
+
* @param months - Whole months (signed 32-bit integer).
|
|
26
|
+
* @param days - Whole days (signed 32-bit integer).
|
|
27
|
+
* @param microseconds - Whole microseconds (safe integer).
|
|
27
28
|
*/
|
|
28
29
|
constructor(
|
|
29
30
|
readonly months: number,
|
|
@@ -36,15 +37,14 @@ export class Interval {
|
|
|
36
37
|
}
|
|
37
38
|
|
|
38
39
|
/**
|
|
39
|
-
* Parses an interval in ISO 8601
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
* number. To have Postgres parse other forms, pass the string as a query
|
|
40
|
+
* Parses an interval string in ISO 8601 format (`P1Y2M3DT4H5M6.5S`, `P2W`)
|
|
41
|
+
* or PostgreSQL's default format (`1 year 2 mons 3 days 04:05:06.5`,
|
|
42
|
+
* `-1 days +02:00:00`). All components except seconds must be whole numbers;
|
|
43
|
+
* infinite intervals (`infinity`, `-infinity`) throw. To parse other
|
|
44
|
+
* PostgreSQL interval syntaxes, pass the string directly as a query
|
|
45
45
|
* parameter instead.
|
|
46
46
|
*
|
|
47
|
-
* @throws {TypeError}
|
|
47
|
+
* @throws {TypeError} If `s` is not in a supported format.
|
|
48
48
|
*/
|
|
49
49
|
static parse(s: string): Interval {
|
|
50
50
|
const parsed = parseISO(s) ?? parsePostgres(s);
|
|
@@ -56,7 +56,7 @@ export class Interval {
|
|
|
56
56
|
return new Interval(parsed.months, parsed.days, parsed.microseconds);
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
/**
|
|
59
|
+
/** Formats the interval as an ISO 8601 duration string (e.g. `P1Y2M3DT4H5M6.5S`, `PT0S`). */
|
|
60
60
|
toString(): string {
|
|
61
61
|
const years = Math.trunc(this.months / 12);
|
|
62
62
|
const months = this.months % 12;
|
|
@@ -90,7 +90,7 @@ export class Interval {
|
|
|
90
90
|
function check(name: string, value: number, max: number) {
|
|
91
91
|
if (!Number.isInteger(value) || Math.abs(value) > max) {
|
|
92
92
|
throw new RangeError(
|
|
93
|
-
`
|
|
93
|
+
`Interval ${name} must be an integer with magnitude at most ${max}, got ${value}`,
|
|
94
94
|
);
|
|
95
95
|
}
|
|
96
96
|
}
|
|
@@ -160,7 +160,7 @@ function parsePostgres(s: string): Parts | null {
|
|
|
160
160
|
return any ? parts : null;
|
|
161
161
|
}
|
|
162
162
|
|
|
163
|
-
/**
|
|
163
|
+
/** Converts a decimal seconds string (up to 6 decimal places) into microseconds. */
|
|
164
164
|
function seconds(s: string): number {
|
|
165
165
|
const negative = s.startsWith('-');
|
|
166
166
|
const [whole, fraction = ''] = s.replace(/^[+-]/, '').split('.');
|
|
@@ -2,20 +2,26 @@ import {numberOf} from './reply';
|
|
|
2
2
|
import type {Target} from './target';
|
|
3
3
|
import {Cmd, ServerError, type Value} from './wire';
|
|
4
4
|
|
|
5
|
-
/**
|
|
5
|
+
/**
|
|
6
|
+
* A bitfield integer type consisting of a signedness prefix (`"i"` or `"u"`)
|
|
7
|
+
* and a bit width, such as `"u8"` or `"i5"`.
|
|
8
|
+
*/
|
|
6
9
|
export type BitfieldType = `${'i' | 'u'}${number}`;
|
|
7
10
|
|
|
8
11
|
/**
|
|
9
|
-
*
|
|
10
|
-
* width (`"#2"`
|
|
12
|
+
* The starting position of a bitfield integer: either a bit index, or a
|
|
13
|
+
* multiple of the integer type's width (for example, `"#2"` for a `u8` is
|
|
14
|
+
* bit 16).
|
|
11
15
|
*/
|
|
12
16
|
export type BitOffset = number | `#${number}`;
|
|
13
17
|
|
|
14
18
|
/**
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* `
|
|
19
|
+
* Controls how subsequent `set` and `incrBy` operations handle values that
|
|
20
|
+
* overflow their integer type's range:
|
|
21
|
+
*
|
|
22
|
+
* - `"wrap"`: wrap around (default)
|
|
23
|
+
* - `"sat"`: saturate at the type's minimum or maximum value
|
|
24
|
+
* - `"fail"`: leave the value unchanged and return `null`
|
|
19
25
|
*/
|
|
20
26
|
export type Overflow = 'wrap' | 'sat' | 'fail';
|
|
21
27
|
|
|
@@ -45,16 +51,16 @@ function overflow(c: Cmd, mode: Overflow) {
|
|
|
45
51
|
c.arg('OVERFLOW').arg(mode.toUpperCase());
|
|
46
52
|
}
|
|
47
53
|
|
|
48
|
-
/** A bitfield's replies, one per `get`, `set
|
|
54
|
+
/** A bitfield's replies, one per `get`, `set`, and `incrBy`. */
|
|
49
55
|
function replies(v: Value): (number | null)[] {
|
|
50
56
|
if (v instanceof ServerError) throw v;
|
|
51
57
|
if (!Array.isArray(v))
|
|
52
|
-
throw new TypeError('redis: BITFIELD
|
|
58
|
+
throw new TypeError('redis: BITFIELD did not return an array');
|
|
53
59
|
return v.map(item => {
|
|
54
60
|
if (item === null) return null;
|
|
55
61
|
if (typeof item === 'bigint' && !Number.isSafeInteger(Number(item))) {
|
|
56
62
|
throw new RangeError(
|
|
57
|
-
`redis:
|
|
63
|
+
`redis: bitfield integer ${item} does not fit in a safe JavaScript number`,
|
|
58
64
|
);
|
|
59
65
|
}
|
|
60
66
|
return numberOf(item);
|
|
@@ -62,8 +68,9 @@ function replies(v: Value): (number | null)[] {
|
|
|
62
68
|
}
|
|
63
69
|
|
|
64
70
|
/**
|
|
65
|
-
*
|
|
66
|
-
* `bitmap.bitfieldRo
|
|
71
|
+
* Builder for read-only bitfield operations (`BITFIELD_RO`), created by
|
|
72
|
+
* `bitmap.bitfieldRo`. Add reads with {@link BitfieldRo.get} and run them with
|
|
73
|
+
* {@link BitfieldRo.invoke}.
|
|
67
74
|
*/
|
|
68
75
|
export class BitfieldRo {
|
|
69
76
|
/** @internal */
|
|
@@ -79,9 +86,10 @@ export class BitfieldRo {
|
|
|
79
86
|
}
|
|
80
87
|
|
|
81
88
|
/**
|
|
82
|
-
*
|
|
83
|
-
* order
|
|
84
|
-
* `
|
|
89
|
+
* Executes the operations and returns one result per `get`, `set`, and
|
|
90
|
+
* `incrBy` in order (`null` for any operation aborted by
|
|
91
|
+
* `overflow('fail')`). Throws a `RangeError` if a returned value cannot be
|
|
92
|
+
* represented accurately as a JavaScript `number`.
|
|
85
93
|
*/
|
|
86
94
|
async invoke(): Promise<(number | null)[]> {
|
|
87
95
|
return replies(await this.t.run(this.c));
|
|
@@ -89,17 +97,24 @@ export class BitfieldRo {
|
|
|
89
97
|
}
|
|
90
98
|
|
|
91
99
|
/**
|
|
92
|
-
*
|
|
93
|
-
* operations
|
|
100
|
+
* Builder for bitfield operations (`BITFIELD`), created by `bitmap.bitfield`.
|
|
101
|
+
* Add operations in order and execute them as a single command with
|
|
102
|
+
* {@link Bitfield.invoke}.
|
|
94
103
|
*/
|
|
95
104
|
export class Bitfield extends BitfieldRo {
|
|
96
|
-
/**
|
|
105
|
+
/**
|
|
106
|
+
* Sets the integer of type `type` at `offset` to `value` and returns its
|
|
107
|
+
* previous value.
|
|
108
|
+
*/
|
|
97
109
|
set(type: BitfieldType, offset: BitOffset, value: number | bigint): this {
|
|
98
110
|
set(this.c, type, offset, value);
|
|
99
111
|
return this;
|
|
100
112
|
}
|
|
101
113
|
|
|
102
|
-
/**
|
|
114
|
+
/**
|
|
115
|
+
* Adds `increment` to the integer of type `type` at `offset` and returns the
|
|
116
|
+
* new value.
|
|
117
|
+
*/
|
|
103
118
|
incrBy(
|
|
104
119
|
type: BitfieldType,
|
|
105
120
|
offset: BitOffset,
|
|
@@ -109,14 +124,16 @@ export class Bitfield extends BitfieldRo {
|
|
|
109
124
|
return this;
|
|
110
125
|
}
|
|
111
126
|
|
|
112
|
-
/**
|
|
127
|
+
/**
|
|
128
|
+
* Sets the overflow behavior for subsequent `set` and `incrBy` operations.
|
|
129
|
+
*/
|
|
113
130
|
overflow(mode: Overflow): this {
|
|
114
131
|
overflow(this.c, mode);
|
|
115
132
|
return this;
|
|
116
133
|
}
|
|
117
134
|
}
|
|
118
135
|
|
|
119
|
-
/** A {@link BitfieldRo} being queued on a pipeline or
|
|
136
|
+
/** A {@link BitfieldRo} builder being queued on a pipeline or transaction. */
|
|
120
137
|
export class QueuedBitfieldRo {
|
|
121
138
|
/** @internal */
|
|
122
139
|
constructor(
|
|
@@ -130,21 +147,27 @@ export class QueuedBitfieldRo {
|
|
|
130
147
|
return this;
|
|
131
148
|
}
|
|
132
149
|
|
|
133
|
-
/** Queues the operations
|
|
150
|
+
/** Queues the accumulated operations as a single command. */
|
|
134
151
|
queue(): void {
|
|
135
152
|
this.queued.push(this.c);
|
|
136
153
|
}
|
|
137
154
|
}
|
|
138
155
|
|
|
139
|
-
/** A {@link Bitfield} being queued on a pipeline or
|
|
156
|
+
/** A {@link Bitfield} builder being queued on a pipeline or transaction. */
|
|
140
157
|
export class QueuedBitfield extends QueuedBitfieldRo {
|
|
141
|
-
/**
|
|
158
|
+
/**
|
|
159
|
+
* Sets the integer of type `type` at `offset` to `value` and returns its
|
|
160
|
+
* previous value.
|
|
161
|
+
*/
|
|
142
162
|
set(type: BitfieldType, offset: BitOffset, value: number | bigint): this {
|
|
143
163
|
set(this.c, type, offset, value);
|
|
144
164
|
return this;
|
|
145
165
|
}
|
|
146
166
|
|
|
147
|
-
/**
|
|
167
|
+
/**
|
|
168
|
+
* Adds `increment` to the integer of type `type` at `offset` and returns the
|
|
169
|
+
* new value.
|
|
170
|
+
*/
|
|
148
171
|
incrBy(
|
|
149
172
|
type: BitfieldType,
|
|
150
173
|
offset: BitOffset,
|
|
@@ -154,7 +177,9 @@ export class QueuedBitfield extends QueuedBitfieldRo {
|
|
|
154
177
|
return this;
|
|
155
178
|
}
|
|
156
179
|
|
|
157
|
-
/**
|
|
180
|
+
/**
|
|
181
|
+
* Sets the overflow behavior for subsequent `set` and `incrBy` operations.
|
|
182
|
+
*/
|
|
158
183
|
overflow(mode: Overflow): this {
|
|
159
184
|
overflow(this.c, mode);
|
|
160
185
|
return this;
|
package/storage/redis/command.ts
CHANGED
|
@@ -16,9 +16,10 @@ export function rawCommand(name: string, readOnly: boolean): Cmd {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
19
|
+
* Builder for an arbitrary Redis command not covered by the typed command
|
|
20
|
+
* groups, created by `command()`. Pass arguments in order with
|
|
21
|
+
* {@link CommandInvocation.key} and {@link CommandInvocation.arg}, then execute
|
|
22
|
+
* with {@link CommandInvocation.invoke}.
|
|
22
23
|
*/
|
|
23
24
|
export class CommandInvocation {
|
|
24
25
|
/** @internal */
|
|
@@ -27,22 +28,23 @@ export class CommandInvocation {
|
|
|
27
28
|
private readonly c: Cmd,
|
|
28
29
|
) {}
|
|
29
30
|
|
|
30
|
-
/**
|
|
31
|
+
/** Appends a key argument, automatically applying the database's key prefix. */
|
|
31
32
|
key(key: string): this {
|
|
32
33
|
this.c.key(key);
|
|
33
34
|
return this;
|
|
34
35
|
}
|
|
35
36
|
|
|
36
|
-
/**
|
|
37
|
+
/** Appends a non-key argument as-is. */
|
|
37
38
|
arg(arg: RedisArg): this {
|
|
38
39
|
this.c.arg(arg);
|
|
39
40
|
return this;
|
|
40
41
|
}
|
|
41
42
|
|
|
42
43
|
/**
|
|
43
|
-
* Sends the command
|
|
44
|
-
*
|
|
45
|
-
*
|
|
44
|
+
* Sends the command and returns the server's reply. Note that keys returned
|
|
45
|
+
* by pop and stream-read commands have the database's key prefix stripped.
|
|
46
|
+
* Throws a `TypeError` with an explanation if the runtime rejects the
|
|
47
|
+
* command.
|
|
46
48
|
*/
|
|
47
49
|
invoke(): ReplyPromise {
|
|
48
50
|
return new ReplyPromise(
|
|
@@ -57,7 +59,9 @@ export class CommandInvocation {
|
|
|
57
59
|
}
|
|
58
60
|
}
|
|
59
61
|
|
|
60
|
-
/**
|
|
62
|
+
/**
|
|
63
|
+
* Builder for queueing an arbitrary Redis command on a pipeline or transaction.
|
|
64
|
+
*/
|
|
61
65
|
export class QueuedCommandInvocation {
|
|
62
66
|
/** @internal */
|
|
63
67
|
constructor(
|
|
@@ -65,19 +69,19 @@ export class QueuedCommandInvocation {
|
|
|
65
69
|
private readonly c: Cmd,
|
|
66
70
|
) {}
|
|
67
71
|
|
|
68
|
-
/**
|
|
72
|
+
/** Appends a key argument, automatically applying the database's key prefix. */
|
|
69
73
|
key(key: string): this {
|
|
70
74
|
this.c.key(key);
|
|
71
75
|
return this;
|
|
72
76
|
}
|
|
73
77
|
|
|
74
|
-
/**
|
|
78
|
+
/** Appends a non-key argument as-is. */
|
|
75
79
|
arg(arg: RedisArg): this {
|
|
76
80
|
this.c.arg(arg);
|
|
77
81
|
return this;
|
|
78
82
|
}
|
|
79
83
|
|
|
80
|
-
/**
|
|
84
|
+
/** Appends the command to the pipeline or transaction queue. */
|
|
81
85
|
queue(): void {
|
|
82
86
|
this.queued.push(this.c);
|
|
83
87
|
}
|