@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.
Files changed (235) hide show
  1. package/README.md +111 -101
  2. package/app_meta.ts +56 -54
  3. package/config/config.ts +60 -57
  4. package/config/secrets.ts +45 -52
  5. package/config/value.ts +23 -26
  6. package/cron/mod.ts +28 -29
  7. package/dist/app_meta.d.ts +52 -50
  8. package/dist/app_meta.d.ts.map +1 -1
  9. package/dist/app_meta.js +2 -2
  10. package/dist/app_meta.js.map +1 -1
  11. package/dist/config/config.d.ts +58 -55
  12. package/dist/config/config.d.ts.map +1 -1
  13. package/dist/config/config.js +8 -9
  14. package/dist/config/config.js.map +1 -1
  15. package/dist/config/secrets.d.ts +45 -52
  16. package/dist/config/secrets.d.ts.map +1 -1
  17. package/dist/config/secrets.js +8 -9
  18. package/dist/config/secrets.js.map +1 -1
  19. package/dist/config/value.d.ts +20 -23
  20. package/dist/config/value.d.ts.map +1 -1
  21. package/dist/config/value.js +20 -23
  22. package/dist/config/value.js.map +1 -1
  23. package/dist/cron/mod.d.ts +26 -27
  24. package/dist/cron/mod.d.ts.map +1 -1
  25. package/dist/cron/mod.js +13 -14
  26. package/dist/cron/mod.js.map +1 -1
  27. package/dist/frontends/mod.d.ts +20 -19
  28. package/dist/frontends/mod.d.ts.map +1 -1
  29. package/dist/frontends/mod.js +20 -19
  30. package/dist/frontends/mod.js.map +1 -1
  31. package/dist/gateway/errors.d.ts +28 -30
  32. package/dist/gateway/errors.d.ts.map +1 -1
  33. package/dist/gateway/errors.js +29 -31
  34. package/dist/gateway/errors.js.map +1 -1
  35. package/dist/gateway/match.d.ts +54 -54
  36. package/dist/gateway/match.d.ts.map +1 -1
  37. package/dist/gateway/match.js +56 -56
  38. package/dist/gateway/match.js.map +1 -1
  39. package/dist/gateway/middleware.d.ts +62 -64
  40. package/dist/gateway/middleware.d.ts.map +1 -1
  41. package/dist/gateway/middleware.js +1 -1
  42. package/dist/gateway/middleware.js.map +1 -1
  43. package/dist/gateway/mod.d.ts +64 -67
  44. package/dist/gateway/mod.d.ts.map +1 -1
  45. package/dist/gateway/mod.js +72 -76
  46. package/dist/gateway/mod.js.map +1 -1
  47. package/dist/gateway/request.d.ts +138 -134
  48. package/dist/gateway/request.d.ts.map +1 -1
  49. package/dist/gateway/request.js +117 -108
  50. package/dist/gateway/request.js.map +1 -1
  51. package/dist/gateway/serve.d.ts +29 -34
  52. package/dist/gateway/serve.d.ts.map +1 -1
  53. package/dist/gateway/serve.js +51 -56
  54. package/dist/gateway/serve.js.map +1 -1
  55. package/dist/gateways/mod.d.ts +23 -21
  56. package/dist/gateways/mod.d.ts.map +1 -1
  57. package/dist/gateways/mod.js +25 -23
  58. package/dist/gateways/mod.js.map +1 -1
  59. package/dist/internal/runtime/napi/napi.cjs +1 -1
  60. package/dist/internal/runtime/napi/napi.d.cts +10 -3
  61. package/dist/internal/runtime/shutdown.d.ts +75 -57
  62. package/dist/internal/runtime/shutdown.d.ts.map +1 -1
  63. package/dist/internal/runtime/shutdown.js +88 -84
  64. package/dist/internal/runtime/shutdown.js.map +1 -1
  65. package/dist/log/logger.d.ts +43 -37
  66. package/dist/log/logger.d.ts.map +1 -1
  67. package/dist/log/logger.js +26 -25
  68. package/dist/log/logger.js.map +1 -1
  69. package/dist/metrics/mod.d.ts +133 -114
  70. package/dist/metrics/mod.d.ts.map +1 -1
  71. package/dist/metrics/mod.js +109 -101
  72. package/dist/metrics/mod.js.map +1 -1
  73. package/dist/otel.d.ts +6 -3
  74. package/dist/otel.d.ts.map +1 -1
  75. package/dist/otel.js +6 -3
  76. package/dist/otel.js.map +1 -1
  77. package/dist/pubsub/subscription.d.ts +88 -116
  78. package/dist/pubsub/subscription.d.ts.map +1 -1
  79. package/dist/pubsub/subscription.js +14 -17
  80. package/dist/pubsub/subscription.js.map +1 -1
  81. package/dist/pubsub/topic.d.ts +86 -113
  82. package/dist/pubsub/topic.d.ts.map +1 -1
  83. package/dist/pubsub/topic.js +68 -82
  84. package/dist/pubsub/topic.js.map +1 -1
  85. package/dist/reqctx/mod.d.ts +83 -86
  86. package/dist/reqctx/mod.d.ts.map +1 -1
  87. package/dist/reqctx/mod.js +39 -41
  88. package/dist/reqctx/mod.js.map +1 -1
  89. package/dist/services/index.d.ts +76 -75
  90. package/dist/services/index.d.ts.map +1 -1
  91. package/dist/services/index.js +73 -71
  92. package/dist/services/index.js.map +1 -1
  93. package/dist/shutdown/mod.d.ts +26 -25
  94. package/dist/shutdown/mod.d.ts.map +1 -1
  95. package/dist/shutdown/mod.js +26 -25
  96. package/dist/shutdown/mod.js.map +1 -1
  97. package/dist/storage/clickhouse/database.d.ts +113 -115
  98. package/dist/storage/clickhouse/database.d.ts.map +1 -1
  99. package/dist/storage/clickhouse/database.js +65 -79
  100. package/dist/storage/clickhouse/database.js.map +1 -1
  101. package/dist/storage/clickhouse/errors.d.ts +25 -26
  102. package/dist/storage/clickhouse/errors.d.ts.map +1 -1
  103. package/dist/storage/clickhouse/errors.js +15 -17
  104. package/dist/storage/clickhouse/errors.js.map +1 -1
  105. package/dist/storage/clickhouse/mod.d.ts +10 -10
  106. package/dist/storage/clickhouse/mod.js +10 -10
  107. package/dist/storage/objects/bucket.d.ts +196 -225
  108. package/dist/storage/objects/bucket.d.ts.map +1 -1
  109. package/dist/storage/objects/bucket.js +71 -84
  110. package/dist/storage/objects/bucket.js.map +1 -1
  111. package/dist/storage/objects/error.d.ts +4 -4
  112. package/dist/storage/objects/error.js +4 -4
  113. package/dist/storage/postgres/database.d.ts +124 -149
  114. package/dist/storage/postgres/database.d.ts.map +1 -1
  115. package/dist/storage/postgres/database.js +98 -135
  116. package/dist/storage/postgres/database.js.map +1 -1
  117. package/dist/storage/postgres/errors.d.ts +42 -48
  118. package/dist/storage/postgres/errors.d.ts.map +1 -1
  119. package/dist/storage/postgres/errors.js +44 -51
  120. package/dist/storage/postgres/errors.js.map +1 -1
  121. package/dist/storage/postgres/interval.d.ts +19 -19
  122. package/dist/storage/postgres/interval.d.ts.map +1 -1
  123. package/dist/storage/postgres/interval.js +21 -21
  124. package/dist/storage/postgres/interval.js.map +1 -1
  125. package/dist/storage/redis/bitfield.d.ts +48 -23
  126. package/dist/storage/redis/bitfield.d.ts.map +1 -1
  127. package/dist/storage/redis/bitfield.js +38 -19
  128. package/dist/storage/redis/bitfield.js.map +1 -1
  129. package/dist/storage/redis/command.d.ts +16 -12
  130. package/dist/storage/redis/command.d.ts.map +1 -1
  131. package/dist/storage/redis/command.js +16 -12
  132. package/dist/storage/redis/command.js.map +1 -1
  133. package/dist/storage/redis/commands.d.ts +99 -100
  134. package/dist/storage/redis/commands.d.ts.map +1 -1
  135. package/dist/storage/redis/commands.js +84 -85
  136. package/dist/storage/redis/commands.js.map +1 -1
  137. package/dist/storage/redis/database.d.ts +138 -122
  138. package/dist/storage/redis/database.d.ts.map +1 -1
  139. package/dist/storage/redis/database.js +120 -101
  140. package/dist/storage/redis/database.js.map +1 -1
  141. package/dist/storage/redis/mod.d.ts +52 -48
  142. package/dist/storage/redis/mod.d.ts.map +1 -1
  143. package/dist/storage/redis/mod.js +52 -48
  144. package/dist/storage/redis/mod.js.map +1 -1
  145. package/dist/storage/redis/options.d.ts +26 -24
  146. package/dist/storage/redis/options.d.ts.map +1 -1
  147. package/dist/storage/redis/options.js.map +1 -1
  148. package/dist/storage/redis/pubsub.d.ts +16 -16
  149. package/dist/storage/redis/pubsub.d.ts.map +1 -1
  150. package/dist/storage/redis/pubsub.js +14 -14
  151. package/dist/storage/redis/pubsub.js.map +1 -1
  152. package/dist/storage/redis/reply.d.ts +80 -72
  153. package/dist/storage/redis/reply.d.ts.map +1 -1
  154. package/dist/storage/redis/reply.js +53 -46
  155. package/dist/storage/redis/reply.js.map +1 -1
  156. package/dist/storage/redis/scan.d.ts +4 -4
  157. package/dist/storage/redis/scan.d.ts.map +1 -1
  158. package/dist/storage/redis/scan.js +6 -6
  159. package/dist/storage/redis/scan.js.map +1 -1
  160. package/dist/storage/redis/script.d.ts +28 -21
  161. package/dist/storage/redis/script.d.ts.map +1 -1
  162. package/dist/storage/redis/script.js +29 -22
  163. package/dist/storage/redis/script.js.map +1 -1
  164. package/dist/storage/redis/target.d.ts +17 -1
  165. package/dist/storage/redis/target.d.ts.map +1 -1
  166. package/dist/storage/redis/target.js +30 -1
  167. package/dist/storage/redis/target.js.map +1 -1
  168. package/dist/storage/redis/wire.d.ts +6 -6
  169. package/dist/storage/redis/wire.d.ts.map +1 -1
  170. package/dist/storage/redis/wire.js +6 -6
  171. package/dist/storage/redis/wire.js.map +1 -1
  172. package/dist/testing/mod.d.ts +83 -88
  173. package/dist/testing/mod.d.ts.map +1 -1
  174. package/dist/testing/mod.js +75 -81
  175. package/dist/testing/mod.js.map +1 -1
  176. package/dist/testing/playwright.d.ts +28 -32
  177. package/dist/testing/playwright.d.ts.map +1 -1
  178. package/dist/testing/playwright.js +22 -27
  179. package/dist/testing/playwright.js.map +1 -1
  180. package/dist/testing/puppeteer.d.ts +11 -12
  181. package/dist/testing/puppeteer.d.ts.map +1 -1
  182. package/dist/testing/puppeteer.js +17 -18
  183. package/dist/testing/puppeteer.js.map +1 -1
  184. package/dist/testing/system.d.ts +72 -90
  185. package/dist/testing/system.d.ts.map +1 -1
  186. package/dist/testing/system.js +90 -107
  187. package/dist/testing/system.js.map +1 -1
  188. package/dist/tsconfig.build.tsbuildinfo +1 -1
  189. package/frontends/mod.test.ts +6 -2
  190. package/frontends/mod.ts +20 -19
  191. package/gateway/errors.ts +30 -32
  192. package/gateway/match.ts +61 -57
  193. package/gateway/middleware.ts +62 -64
  194. package/gateway/mod.ts +73 -76
  195. package/gateway/request.ts +143 -140
  196. package/gateway/serve.ts +58 -63
  197. package/gateways/mod.ts +25 -23
  198. package/internal/runtime/napi/napi.cjs +1 -1
  199. package/internal/runtime/napi/napi.d.cts +10 -3
  200. package/internal/runtime/shutdown.ts +113 -101
  201. package/log/logger.test.ts +8 -0
  202. package/log/logger.ts +48 -45
  203. package/metrics/mod.ts +138 -122
  204. package/otel.ts +6 -3
  205. package/package.json +1 -1
  206. package/pubsub/subscription.ts +93 -123
  207. package/pubsub/topic.ts +96 -130
  208. package/reqctx/mod.ts +83 -86
  209. package/services/index.ts +78 -78
  210. package/shutdown/mod.ts +26 -25
  211. package/storage/clickhouse/database.ts +115 -129
  212. package/storage/clickhouse/errors.ts +27 -29
  213. package/storage/clickhouse/mod.ts +10 -10
  214. package/storage/objects/bucket.ts +209 -239
  215. package/storage/objects/error.ts +4 -4
  216. package/storage/postgres/database.ts +131 -164
  217. package/storage/postgres/errors.ts +45 -52
  218. package/storage/postgres/interval.ts +21 -21
  219. package/storage/redis/bitfield.ts +51 -26
  220. package/storage/redis/command.ts +16 -12
  221. package/storage/redis/commands.ts +99 -100
  222. package/storage/redis/database.ts +145 -123
  223. package/storage/redis/mod.ts +52 -48
  224. package/storage/redis/options.ts +26 -24
  225. package/storage/redis/pubsub.ts +14 -14
  226. package/storage/redis/redis.test.ts +17 -2
  227. package/storage/redis/reply.ts +85 -77
  228. package/storage/redis/scan.ts +6 -6
  229. package/storage/redis/script.ts +29 -22
  230. package/storage/redis/target.ts +34 -1
  231. package/storage/redis/wire.ts +8 -8
  232. package/testing/mod.ts +99 -105
  233. package/testing/playwright.ts +32 -37
  234. package/testing/puppeteer.ts +22 -23
  235. package/testing/system.ts +117 -133
@@ -1,6 +1,7 @@
1
1
  /**
2
- * What kind of error a database reported, by its SQLSTATE: the cases an app
3
- * most often handles. `other` is any other; the SQLSTATE says what.
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
- /** None of the others; the SQLSTATE says what. */
17
+ /** Any other SQLSTATE; see `DatabaseError.sqlState` for details. */
17
18
  Other: 'other',
18
- /** A `NOT NULL` constraint would be violated (23502). */
19
+ /** A `NOT NULL` constraint was violated (SQLSTATE 23502). */
19
20
  NotNullViolation: 'not_null_violation',
20
- /** A foreign key constraint would be violated (23503). */
21
+ /** A foreign key constraint was violated (SQLSTATE 23503). */
21
22
  ForeignKeyViolation: 'foreign_key_violation',
22
- /** A unique constraint would be violated (23505). */
23
+ /** A unique constraint was violated (SQLSTATE 23505). */
23
24
  UniqueViolation: 'unique_violation',
24
- /** A check constraint would be violated (23514). */
25
+ /** A check constraint was violated (SQLSTATE 23514). */
25
26
  CheckViolation: 'check_violation',
26
- /** An exclusion constraint would be violated (23P01). */
27
+ /** An exclusion constraint was violated (SQLSTATE 23P01). */
27
28
  ExcludeViolation: 'exclude_violation',
28
- /** A statement ran in a transaction an earlier statement failed (25P02). */
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 refused a connection: it has as many as it allows (53300). */
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 read transaction could not be serialized
36
- * with a concurrent one (40001); retrying it may succeed.
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 one through a read handle. */
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 taken without waiting (55P03), as with
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 (57014): by a timeout, or on request. */
47
+ /** The statement was canceled by timeout or explicit request (SQLSTATE 57014). */
47
48
  QueryCanceled: 'query_canceled',
48
- /** The database could not be reached, or the connection to it was lost (class 08). */
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
- /** One of the {@link ErrorCode} values: `'unique_violation'`. */
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
- * An error a database reported, or a failure to reach it.
57
+ * Error reported by PostgreSQL or caused by a connection failure.
57
58
  *
58
- * A failure to reach the database, which the server can't report, has
59
- * `sqlState` `"08001"`, `code` `connection_failure`, the client's message
60
- * and none of the optional fields. The caller's own mistakes, such as a
61
- * parameter of the wrong type, and using a busy or closed handle, are plain
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
- * The optional fields are sent only for some errors (see
65
- * https://www.postgresql.org/docs/current/protocol-error-fields.html); one
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
- * Its `message` reads `ERROR: duplicate key value violates unique constraint
69
- * "users_pkey" (Code unique_violation: SQLSTATE 23505)`. A subscription
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
- /** What kind of error it is, by its SQLSTATE. */
72
+ /** Normalized error category derived from `sqlState`. */
76
73
  readonly code: ErrorCode;
77
- /** The SQLSTATE: `"23505"`. */
74
+ /** Five-character PostgreSQL SQLSTATE code (e.g. `"23505"`). */
78
75
  readonly sqlState: string;
79
- /** `ERROR`, `FATAL` or `PANIC`. */
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
- /** A suggestion what to do about it. */
80
+ /** Optional PostgreSQL hint suggesting how to resolve the error. */
87
81
  readonly hint?: string;
88
- /** The schema of the object the error is about. */
82
+ /** Schema name associated with the error, if provided. */
89
83
  readonly schemaName?: string;
90
- /** The table the error is about. */
84
+ /** Table name associated with the error, if provided. */
91
85
  readonly tableName?: string;
92
- /** The column the error is about. */
86
+ /** Column name associated with the error, if provided. */
93
87
  readonly columnName?: string;
94
- /** The data type the error is about. */
88
+ /** Data type name associated with the error, if provided. */
95
89
  readonly dataTypeName?: string;
96
- /** The constraint the error is about; an index counts as one. */
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` for a query that returns more than one row. */
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
- * Begins the message of an error the native module rejects with for a
147
- * `DatabaseError`, which the rest of the message holds as JSON. The same as
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, throwing the error it rejects with as a
154
- * `DatabaseError` if it is one.
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
- /** `err`, a native database call's, as a `DatabaseError` if it is one. */
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
- * A Postgres `interval`, exactly as Postgres holds it: months, days and
8
- * microseconds apart, since a month has no fixed number of days, nor a day
9
- * (across a change of daylight saving time) of microseconds.
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
- * An `interval` column comes back as a string in ISO 8601's form, as
12
- * Postgres writes one (`'P1Y2M3DT4H5M6.5S'`, and `'P1M-2D'` where the parts'
13
- * signs differ), which {@link Interval.parse} reads. A parameter of type
14
- * `interval` takes an `Interval`, a `Temporal.Duration`, or a string in any
15
- * form Postgres parses (`'1 day 02:00:00'`, `'P1DT2H'`, `'90 minutes'`).
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, within a Postgres `int4`.
25
- * @param days - Whole days, within a Postgres `int4`.
26
- * @param microseconds - Whole microseconds, a safe integer.
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's form (`P1Y2M3DT4H5M6.5S`, `P2W`), as
40
- * an `interval` column comes back, or in the form Postgres writes one by
41
- * default (`1 year 2 mons 3 days 04:05:06.5`, `-1 days +02:00:00`).
42
- * Postgres's infinite intervals (`infinity`, `-infinity`) have no exact
43
- * form, and throw. Each part but the seconds is a whole
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} if `s` is in neither form.
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
- /** The interval in ISO 8601's form, as Postgres writes it: `P1Y2M3DT4H5M6.5S`, `PT0S`. */
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
- `an Interval's ${name} must be an integer of at most ${max} in magnitude, not ${value}`,
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
- /** `s`, a number of seconds with up to six decimals, in microseconds. */
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
- /** A bitfield integer's type: signed or unsigned, and its width: `"u8"`, `"i5"`. */
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
- * Where a bitfield integer starts: at a bit, or at a multiple of its type's
10
- * width (`"#2"` of a `u8` is bit 16).
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
- * What a `set` or `incrBy` that leaves its type's range does, from the
16
- * `overflow` that says so on: wrap around (the default), stop at the type's
17
- * smallest or largest value, or fail, leaving the value as it was and replying
18
- * `null`.
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` and `incrBy`. */
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 replied no array');
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: the bitfield integer ${item} does not fit a number`,
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
- * A string's bitfield integers, read only (`BITFIELD_RO`), made by
66
- * `bitmap.bitfieldRo`: add the reads, then {@link BitfieldRo.invoke}.
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
- * Runs the operations, replying one value per `get`, `set` and `incrBy`, in
83
- * order: `null` for one an `overflow('fail')` stopped. Throws a
84
- * `RangeError` for a value a `number` can't hold exactly.
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
- * A string's bitfield integers, made by `bitmap.bitfield`: add the
93
- * operations, which run in order as one command, then {@link Bitfield.invoke}.
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
- /** Sets the integer of type `type` at `offset`, replying what it was. */
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
- /** Adds `increment` to the integer at `offset`, replying what it is then. */
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
- /** What the `set`s and `incrBy`s after this do when they leave their range. */
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 a transaction. */
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, as one command. */
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 a transaction. */
156
+ /** A {@link Bitfield} builder being queued on a pipeline or transaction. */
140
157
  export class QueuedBitfield extends QueuedBitfieldRo {
141
- /** Sets the integer of type `type` at `offset`, replying what it was. */
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
- /** Adds `increment` to the integer at `offset`, replying what it is then. */
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
- /** What the `set`s and `incrBy`s after this do when they leave their range. */
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;
@@ -16,9 +16,10 @@ export function rawCommand(name: string, readOnly: boolean): Cmd {
16
16
  }
17
17
 
18
18
  /**
19
- * A command the command groups lack, made by `command()`: give its arguments
20
- * in order with {@link CommandInvocation.key} and {@link CommandInvocation.arg},
21
- * then {@link CommandInvocation.invoke}.
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
- /** The next argument, which is a key: it gets the database's prefix. */
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
- /** The next argument, which is not a key: it is sent as it is. */
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, replying what the server did, but for the keys the
44
- * pops and stream reads reply with, which lose the database's prefix. A
45
- * command the runtime refuses fails with a `TypeError` saying why.
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
- /** A command the command groups lack being queued on a pipeline or a transaction. */
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
- /** The next argument, which is a key: it gets the database's prefix. */
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
- /** The next argument, which is not a key: it is sent as it is. */
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
- /** Queues the command. */
84
+ /** Appends the command to the pipeline or transaction queue. */
81
85
  queue(): void {
82
86
  this.queued.push(this.c);
83
87
  }