@encore.dev/sdk 0.0.0 → 2.0.0-beta.4

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 (250) hide show
  1. package/LICENSE +362 -0
  2. package/README.md +122 -2
  3. package/app_meta.test.ts +134 -0
  4. package/app_meta.ts +186 -0
  5. package/config/config.test.ts +381 -0
  6. package/config/config.ts +178 -0
  7. package/config/mod.ts +14 -0
  8. package/config/secrets.ts +147 -0
  9. package/config/value.ts +243 -0
  10. package/cron/mod.ts +23 -0
  11. package/dist/app_meta.d.ts +93 -0
  12. package/dist/app_meta.js +65 -0
  13. package/dist/app_meta.js.map +1 -0
  14. package/dist/config/config.d.ts +105 -0
  15. package/dist/config/config.js +87 -0
  16. package/dist/config/config.js.map +1 -0
  17. package/dist/config/mod.d.ts +4 -0
  18. package/dist/config/mod.js +3 -0
  19. package/dist/config/mod.js.map +1 -0
  20. package/dist/config/secrets.d.ts +92 -0
  21. package/dist/config/secrets.js +80 -0
  22. package/dist/config/secrets.js.map +1 -0
  23. package/dist/config/value.d.ts +51 -0
  24. package/dist/config/value.js +178 -0
  25. package/dist/config/value.js.map +1 -0
  26. package/dist/cron/mod.d.ts +22 -0
  27. package/dist/cron/mod.js +9 -0
  28. package/dist/cron/mod.js.map +1 -0
  29. package/dist/internal/otel/backend.d.ts +50 -0
  30. package/dist/internal/otel/backend.js +2 -0
  31. package/dist/internal/otel/backend.js.map +1 -0
  32. package/dist/internal/otel/context.d.ts +10 -0
  33. package/dist/internal/otel/context.js +34 -0
  34. package/dist/internal/otel/context.js.map +1 -0
  35. package/dist/internal/otel/context_manager.d.ts +22 -0
  36. package/dist/internal/otel/context_manager.js +120 -0
  37. package/dist/internal/otel/context_manager.js.map +1 -0
  38. package/dist/internal/otel/encode.d.ts +112 -0
  39. package/dist/internal/otel/encode.js +290 -0
  40. package/dist/internal/otel/encode.js.map +1 -0
  41. package/dist/internal/otel/index.d.ts +9 -0
  42. package/dist/internal/otel/index.js +9 -0
  43. package/dist/internal/otel/index.js.map +1 -0
  44. package/dist/internal/otel/meter.d.ts +73 -0
  45. package/dist/internal/otel/meter.js +265 -0
  46. package/dist/internal/otel/meter.js.map +1 -0
  47. package/dist/internal/otel/propagator.d.ts +14 -0
  48. package/dist/internal/otel/propagator.js +61 -0
  49. package/dist/internal/otel/propagator.js.map +1 -0
  50. package/dist/internal/otel/provider.d.ts +9 -0
  51. package/dist/internal/otel/provider.js +19 -0
  52. package/dist/internal/otel/provider.js.map +1 -0
  53. package/dist/internal/otel/request_context.d.ts +11 -0
  54. package/dist/internal/otel/request_context.js +20 -0
  55. package/dist/internal/otel/request_context.js.map +1 -0
  56. package/dist/internal/otel/span.d.ts +61 -0
  57. package/dist/internal/otel/span.js +206 -0
  58. package/dist/internal/otel/span.js.map +1 -0
  59. package/dist/internal/otel/tracer.d.ts +15 -0
  60. package/dist/internal/otel/tracer.js +65 -0
  61. package/dist/internal/otel/tracer.js.map +1 -0
  62. package/dist/internal/reqtrack/mod.d.ts +6 -0
  63. package/dist/internal/reqtrack/mod.js +28 -0
  64. package/dist/internal/reqtrack/mod.js.map +1 -0
  65. package/dist/internal/runtime/meter.d.ts +11 -0
  66. package/dist/internal/runtime/meter.js +22 -0
  67. package/dist/internal/runtime/meter.js.map +1 -0
  68. package/dist/internal/runtime/mod.d.ts +12 -0
  69. package/dist/internal/runtime/mod.js +28 -0
  70. package/dist/internal/runtime/mod.js.map +1 -0
  71. package/dist/internal/runtime/napi/napi.cjs +122 -0
  72. package/dist/internal/runtime/napi/napi.d.cts +601 -0
  73. package/dist/internal/runtime/shutdown.d.ts +13 -0
  74. package/dist/internal/runtime/shutdown.js +56 -0
  75. package/dist/internal/runtime/shutdown.js.map +1 -0
  76. package/dist/internal/utils/constraints.d.ts +9 -0
  77. package/dist/internal/utils/constraints.js +2 -0
  78. package/dist/internal/utils/constraints.js.map +1 -0
  79. package/dist/log/index.d.ts +39 -0
  80. package/dist/log/index.js +44 -0
  81. package/dist/log/index.js.map +1 -0
  82. package/dist/log/logger.d.ts +123 -0
  83. package/dist/log/logger.js +203 -0
  84. package/dist/log/logger.js.map +1 -0
  85. package/dist/log/runtime.d.ts +19 -0
  86. package/dist/log/runtime.js +22 -0
  87. package/dist/log/runtime.js.map +1 -0
  88. package/dist/log/stderr.d.ts +9 -0
  89. package/dist/log/stderr.js +25 -0
  90. package/dist/log/stderr.js.map +1 -0
  91. package/dist/metrics/mod.d.ts +315 -0
  92. package/dist/metrics/mod.js +488 -0
  93. package/dist/metrics/mod.js.map +1 -0
  94. package/dist/mod.d.ts +6 -0
  95. package/dist/mod.js +4 -0
  96. package/dist/mod.js.map +1 -0
  97. package/dist/otel.d.ts +9 -0
  98. package/dist/otel.js +13 -0
  99. package/dist/otel.js.map +1 -0
  100. package/dist/pubsub/encoding.d.ts +3 -0
  101. package/dist/pubsub/encoding.js +10 -0
  102. package/dist/pubsub/encoding.js.map +1 -0
  103. package/dist/pubsub/mod.d.ts +6 -0
  104. package/dist/pubsub/mod.js +3 -0
  105. package/dist/pubsub/mod.js.map +1 -0
  106. package/dist/pubsub/refs.d.ts +12 -0
  107. package/dist/pubsub/refs.js +11 -0
  108. package/dist/pubsub/refs.js.map +1 -0
  109. package/dist/pubsub/subscription.d.ts +128 -0
  110. package/dist/pubsub/subscription.js +39 -0
  111. package/dist/pubsub/subscription.js.map +1 -0
  112. package/dist/pubsub/topic.d.ts +148 -0
  113. package/dist/pubsub/topic.js +164 -0
  114. package/dist/pubsub/topic.js.map +1 -0
  115. package/dist/req_meta.d.ts +47 -0
  116. package/dist/req_meta.js +26 -0
  117. package/dist/req_meta.js.map +1 -0
  118. package/dist/services/index.d.ts +7 -0
  119. package/dist/services/index.js +17 -0
  120. package/dist/services/index.js.map +1 -0
  121. package/dist/shutdown/mod.d.ts +23 -0
  122. package/dist/shutdown/mod.js +24 -0
  123. package/dist/shutdown/mod.js.map +1 -0
  124. package/dist/storage/objects/bucket.d.ts +216 -0
  125. package/dist/storage/objects/bucket.js +123 -0
  126. package/dist/storage/objects/bucket.js.map +1 -0
  127. package/dist/storage/objects/error.d.ts +14 -0
  128. package/dist/storage/objects/error.js +81 -0
  129. package/dist/storage/objects/error.js.map +1 -0
  130. package/dist/storage/objects/mod.d.ts +4 -0
  131. package/dist/storage/objects/mod.js +3 -0
  132. package/dist/storage/objects/mod.js.map +1 -0
  133. package/dist/storage/objects/refs.d.ts +31 -0
  134. package/dist/storage/objects/refs.js +20 -0
  135. package/dist/storage/objects/refs.js.map +1 -0
  136. package/dist/storage/postgres/database.d.ts +295 -0
  137. package/dist/storage/postgres/database.js +378 -0
  138. package/dist/storage/postgres/database.js.map +1 -0
  139. package/dist/storage/postgres/mod.d.ts +2 -0
  140. package/dist/storage/postgres/mod.js +2 -0
  141. package/dist/storage/postgres/mod.js.map +1 -0
  142. package/dist/storage/redis/commands.d.ts +573 -0
  143. package/dist/storage/redis/commands.js +637 -0
  144. package/dist/storage/redis/commands.js.map +1 -0
  145. package/dist/storage/redis/database.d.ts +302 -0
  146. package/dist/storage/redis/database.js +336 -0
  147. package/dist/storage/redis/database.js.map +1 -0
  148. package/dist/storage/redis/mod.d.ts +88 -0
  149. package/dist/storage/redis/mod.js +84 -0
  150. package/dist/storage/redis/mod.js.map +1 -0
  151. package/dist/storage/redis/native.d.ts +8 -0
  152. package/dist/storage/redis/native.js +11 -0
  153. package/dist/storage/redis/native.js.map +1 -0
  154. package/dist/storage/redis/options.d.ts +89 -0
  155. package/dist/storage/redis/options.js +53 -0
  156. package/dist/storage/redis/options.js.map +1 -0
  157. package/dist/storage/redis/pubsub.d.ts +53 -0
  158. package/dist/storage/redis/pubsub.js +88 -0
  159. package/dist/storage/redis/pubsub.js.map +1 -0
  160. package/dist/storage/redis/reply.d.ts +256 -0
  161. package/dist/storage/redis/reply.js +572 -0
  162. package/dist/storage/redis/reply.js.map +1 -0
  163. package/dist/storage/redis/scan.d.ts +11 -0
  164. package/dist/storage/redis/scan.js +76 -0
  165. package/dist/storage/redis/scan.js.map +1 -0
  166. package/dist/storage/redis/script.d.ts +91 -0
  167. package/dist/storage/redis/script.js +143 -0
  168. package/dist/storage/redis/script.js.map +1 -0
  169. package/dist/storage/redis/target.d.ts +47 -0
  170. package/dist/storage/redis/target.js +82 -0
  171. package/dist/storage/redis/target.js.map +1 -0
  172. package/dist/storage/redis/wire.d.ts +105 -0
  173. package/dist/storage/redis/wire.js +333 -0
  174. package/dist/storage/redis/wire.js.map +1 -0
  175. package/dist/tsconfig.build.tsbuildinfo +1 -0
  176. package/dist/types/mod.d.ts +42 -0
  177. package/dist/types/mod.js +65 -0
  178. package/dist/types/mod.js.map +1 -0
  179. package/internal/otel/backend.ts +60 -0
  180. package/internal/otel/context.test.ts +70 -0
  181. package/internal/otel/context.ts +48 -0
  182. package/internal/otel/context_manager.test.ts +154 -0
  183. package/internal/otel/context_manager.ts +141 -0
  184. package/internal/otel/encode.test.ts +266 -0
  185. package/internal/otel/encode.ts +408 -0
  186. package/internal/otel/index.ts +20 -0
  187. package/internal/otel/meter.test.ts +231 -0
  188. package/internal/otel/meter.ts +332 -0
  189. package/internal/otel/propagator.test.ts +105 -0
  190. package/internal/otel/propagator.ts +68 -0
  191. package/internal/otel/provider.ts +23 -0
  192. package/internal/otel/request_context.ts +32 -0
  193. package/internal/otel/span.ts +252 -0
  194. package/internal/otel/tracer.test.ts +352 -0
  195. package/internal/otel/tracer.ts +104 -0
  196. package/internal/reqtrack/mod.ts +39 -0
  197. package/internal/runtime/meter.ts +26 -0
  198. package/internal/runtime/mod.ts +41 -0
  199. package/internal/runtime/napi/napi.cjs +122 -0
  200. package/internal/runtime/napi/napi.d.cts +601 -0
  201. package/internal/runtime/shutdown.test.ts +248 -0
  202. package/internal/runtime/shutdown.ts +64 -0
  203. package/internal/utils/constraints.ts +12 -0
  204. package/log/index.ts +107 -0
  205. package/log/logger.test.ts +194 -0
  206. package/log/logger.ts +308 -0
  207. package/log/runtime.test.ts +65 -0
  208. package/log/runtime.ts +40 -0
  209. package/log/stderr.test.ts +23 -0
  210. package/log/stderr.ts +24 -0
  211. package/metrics/export.test.ts +337 -0
  212. package/metrics/mod.test.ts +180 -0
  213. package/metrics/mod.ts +619 -0
  214. package/mod.ts +22 -0
  215. package/otel.test.ts +84 -0
  216. package/otel.ts +17 -0
  217. package/package.json +121 -5
  218. package/pubsub/encoding.ts +8 -0
  219. package/pubsub/mod.ts +14 -0
  220. package/pubsub/pubsub.test.ts +186 -0
  221. package/pubsub/refs.ts +22 -0
  222. package/pubsub/subscription.ts +180 -0
  223. package/pubsub/topic.test.ts +121 -0
  224. package/pubsub/topic.ts +212 -0
  225. package/req_meta.ts +75 -0
  226. package/services/index.test.ts +30 -0
  227. package/services/index.ts +16 -0
  228. package/shutdown/mod.ts +24 -0
  229. package/storage/objects/bucket.ts +338 -0
  230. package/storage/objects/error.ts +94 -0
  231. package/storage/objects/mod.ts +35 -0
  232. package/storage/objects/refs.ts +69 -0
  233. package/storage/postgres/database.integration.test.ts +175 -0
  234. package/storage/postgres/database.test.ts +241 -0
  235. package/storage/postgres/database.ts +497 -0
  236. package/storage/postgres/mod.ts +15 -0
  237. package/storage/redis/commands.ts +1221 -0
  238. package/storage/redis/database.ts +466 -0
  239. package/storage/redis/mod.ts +135 -0
  240. package/storage/redis/native.ts +11 -0
  241. package/storage/redis/options.ts +119 -0
  242. package/storage/redis/pubsub.ts +92 -0
  243. package/storage/redis/redis.test.ts +706 -0
  244. package/storage/redis/reply.ts +705 -0
  245. package/storage/redis/runtime.test.ts +233 -0
  246. package/storage/redis/scan.ts +125 -0
  247. package/storage/redis/script.ts +162 -0
  248. package/storage/redis/target.ts +131 -0
  249. package/storage/redis/wire.ts +369 -0
  250. package/types/mod.ts +92 -0
@@ -0,0 +1,1221 @@
1
+ /**
2
+ * The command groups: one per Redis data type, each holding the commands on
3
+ * it in three forms: the ones that run now, the ones of those that only read,
4
+ * and the ones that queue on a pipeline or a transaction.
5
+ *
6
+ * All three come from one list per group, below, after the Rust SDK's
7
+ * (`sdk/rs/encore/src/storage/redis/commands.rs`): the same commands, named in
8
+ * TypeScript's casing. A command is written once: whether it reads or writes,
9
+ * its parameters, the Redis command it builds, with the arguments that are
10
+ * keys marked, and what its reply is read as. Encore's parser holds the same
11
+ * list (`TS_TABLES.families` in `encore_parser`), which is how it knows what
12
+ * each call needs to be granted; a test there reads this file and fails when
13
+ * the two differ.
14
+ *
15
+ * Two are TypeScript's own, as they are Go's: `incrFloat` and `hincrFloat`,
16
+ * where Rust picks `INCRBY` or `INCRBYFLOAT` by the type of the amount.
17
+ *
18
+ * @module
19
+ */
20
+
21
+ import {
22
+ directionArg,
23
+ expiryArgs,
24
+ maxlenArgs,
25
+ radiusArgs,
26
+ setArgs,
27
+ type Direction,
28
+ type Expiry,
29
+ type RadiusOptions,
30
+ type ScanOptions,
31
+ type SetOptions,
32
+ type StreamMaxlen,
33
+ type Unit,
34
+ } from './options';
35
+ import {as, Reply, ReplyPromise} from './reply';
36
+ import {scanFields, scanKeys, scanMembers, scanScored} from './scan';
37
+ import {subscribe, type Subscription} from './pubsub';
38
+ import type {Target} from './target';
39
+ import {Cmd, type Pairs, type RedisArg, type Value} from './wire';
40
+
41
+ /** A score bound: a number, `"-inf"` or `"+inf"`, or exclusive as `"(1.5"`. */
42
+ export type ScoreBound = number | string;
43
+
44
+ /** A command: whether it writes, how it is built, and what its reply is read as. */
45
+ interface CommandDef<A extends unknown[], R, W extends boolean> {
46
+ readonly write: W;
47
+ readonly build: (...args: A) => Cmd;
48
+ readonly reply: (v: Value) => R;
49
+ }
50
+
51
+ /**
52
+ * What runs now and is no command of its own to queue: a scan, which returns
53
+ * an iteration, and a subscription.
54
+ */
55
+ interface RunDef<A extends unknown[], R, W extends boolean> {
56
+ readonly write: W;
57
+ readonly run: (t: Target, ...args: A) => R;
58
+ }
59
+
60
+ /** A command that only reads. */
61
+ function read<A extends unknown[], R>(
62
+ build: (...args: A) => Cmd,
63
+ reply: (v: Value) => R,
64
+ ): CommandDef<A, R, false> {
65
+ return {write: false, build, reply};
66
+ }
67
+
68
+ /** A command that writes. */
69
+ function write<A extends unknown[], R>(
70
+ build: (...args: A) => Cmd,
71
+ reply: (v: Value) => R,
72
+ ): CommandDef<A, R, true> {
73
+ return {write: true, build, reply};
74
+ }
75
+
76
+ /** What runs now and only reads. */
77
+ function readRun<A extends unknown[], R>(
78
+ run: (t: Target, ...args: A) => R,
79
+ ): RunDef<A, R, false> {
80
+ return {write: false, run};
81
+ }
82
+
83
+ /**
84
+ * The command Redis calls `name`. Its trace name and whether it writes are
85
+ * the definition's, filled in as the group is put together.
86
+ */
87
+ function cmd(name: string): Cmd {
88
+ return new Cmd('', name, false);
89
+ }
90
+
91
+ /** A bit as Redis takes one. */
92
+ const bitArg = (b: boolean) => (b ? 1 : 0);
93
+
94
+ const generic = {
95
+ /** Whether the key exists. */
96
+ exists: read((key: string) => cmd('EXISTS').key(key), as.bool),
97
+ /**
98
+ * The seconds until the key expires: -1 if it never does, -2 if it does not
99
+ * exist.
100
+ */
101
+ ttl: read((key: string) => cmd('TTL').key(key), as.number),
102
+ /**
103
+ * The milliseconds until the key expires: -1 if it never does, -2 if it does
104
+ * not exist.
105
+ */
106
+ pttl: read((key: string) => cmd('PTTL').key(key), as.number),
107
+ /**
108
+ * The type of the value at the key: `string`, `list`, `set`, `zset`, `hash`,
109
+ * `stream`, or `none` if it does not exist.
110
+ */
111
+ keyType: read((key: string) => cmd('TYPE').key(key), as.text),
112
+ /**
113
+ * The Unix time, in seconds, the key expires at: -1 if it never does, -2 if
114
+ * it does not exist.
115
+ */
116
+ expireTime: read((key: string) => cmd('EXPIRETIME').key(key), as.number),
117
+ /** Iterates over the database's keys. */
118
+ scan: readRun((t: Target, options?: ScanOptions) => scanKeys(t, options)),
119
+
120
+ /** Deletes the keys, returning how many existed. */
121
+ del: write((...keys: string[]) => cmd('DEL').keys(keys), as.number),
122
+ /**
123
+ * Deletes the keys, reclaiming their memory in the background. Returns how
124
+ * many existed.
125
+ */
126
+ unlink: write((...keys: string[]) => cmd('UNLINK').keys(keys), as.number),
127
+ /** Has the key expire in `seconds`. `false` if it does not exist. */
128
+ expire: write(
129
+ (key: string, seconds: number) => cmd('EXPIRE').key(key).arg(seconds),
130
+ as.bool,
131
+ ),
132
+ /** Has the key expire in `milliseconds`. `false` if it does not exist. */
133
+ pexpire: write(
134
+ (key: string, milliseconds: number) =>
135
+ cmd('PEXPIRE').key(key).arg(milliseconds),
136
+ as.bool,
137
+ ),
138
+ /**
139
+ * Has the key expire at a Unix time, in seconds. `false` if it does not
140
+ * exist.
141
+ */
142
+ expireAt: write(
143
+ (key: string, timestamp: number) => cmd('EXPIREAT').key(key).arg(timestamp),
144
+ as.bool,
145
+ ),
146
+ /** Removes the key's expiry. `false` if it had none, or does not exist. */
147
+ persist: write((key: string) => cmd('PERSIST').key(key), as.bool),
148
+ /** Renames the key, replacing `newKey` if it exists. */
149
+ rename: write(
150
+ (key: string, newKey: string) => cmd('RENAME').key(key).key(newKey),
151
+ as.none,
152
+ ),
153
+ /** Renames the key unless `newKey` exists. `false` if it does. */
154
+ renameNx: write(
155
+ (key: string, newKey: string) => cmd('RENAMENX').key(key).key(newKey),
156
+ as.bool,
157
+ ),
158
+ /**
159
+ * Copies the value at `source` to `destination`. `false` if `destination`
160
+ * exists.
161
+ */
162
+ copy: write(
163
+ (source: string, destination: string) =>
164
+ cmd('COPY').key(source).key(destination),
165
+ as.bool,
166
+ ),
167
+ /** Marks the keys as used just now, returning how many exist. */
168
+ touch: write((...keys: string[]) => cmd('TOUCH').keys(keys), as.number),
169
+ };
170
+
171
+ const string = {
172
+ /** The value at the key: nil if it does not exist. */
173
+ get: read((key: string) => cmd('GET').key(key), as.reply),
174
+ /**
175
+ * The values at the keys, in order, each missing one as nil: read it with
176
+ * {@link Reply.optionalTexts}.
177
+ */
178
+ mget: read((...keys: string[]) => cmd('MGET').keys(keys), as.reply),
179
+ /** The length of the value at the key, in bytes. */
180
+ strlen: read((key: string) => cmd('STRLEN').key(key), as.number),
181
+ /**
182
+ * The part of the value from byte `from` to byte `to`, inclusive. Negative
183
+ * offsets count from the end.
184
+ */
185
+ getrange: read(
186
+ (key: string, from: number, to: number) =>
187
+ cmd('GETRANGE').key(key).arg(from).arg(to),
188
+ as.reply,
189
+ ),
190
+
191
+ /** Sets the key to the value. */
192
+ set: write(
193
+ (key: string, value: RedisArg) => cmd('SET').key(key).arg(value),
194
+ as.none,
195
+ ),
196
+ /** Sets the key to the value, expiring in `seconds`. */
197
+ setEx: write(
198
+ (key: string, value: RedisArg, seconds: number) =>
199
+ cmd('SETEX').key(key).arg(seconds).arg(value),
200
+ as.none,
201
+ ),
202
+ /** Sets the key to the value unless the key exists. `false` if it does. */
203
+ setNx: write(
204
+ (key: string, value: RedisArg) => cmd('SETNX').key(key).arg(value),
205
+ as.bool,
206
+ ),
207
+ /**
208
+ * Sets the key to the value as `options` say: only if it exists or does
209
+ * not, with an expiry, replying the old value.
210
+ */
211
+ setOptions: write(
212
+ (key: string, value: RedisArg, options: SetOptions) =>
213
+ cmd('SET').key(key).arg(value).argsOf(setArgs(options)),
214
+ as.reply,
215
+ ),
216
+ /** Sets each key to its value, all at once. */
217
+ mset: write((items: Pairs<RedisArg>) => cmd('MSET').pairs(items), as.none),
218
+ /**
219
+ * Sets each key to its value unless any of the keys exists. `false` if one
220
+ * does, in which case none is set.
221
+ */
222
+ msetNx: write(
223
+ (items: Pairs<RedisArg>) => cmd('MSETNX').pairs(items),
224
+ as.bool,
225
+ ),
226
+ /** The value at the key, which is deleted. */
227
+ getDel: write((key: string) => cmd('GETDEL').key(key), as.reply),
228
+ /** The value at the key, whose expiry is set as `expiry` says. */
229
+ getEx: write(
230
+ (key: string, expiry: Expiry) =>
231
+ cmd('GETEX').key(key).argsOf(expiryArgs(expiry)),
232
+ as.reply,
233
+ ),
234
+ /** Appends to the value at the key, returning its new length. */
235
+ append: write(
236
+ (key: string, value: RedisArg) => cmd('APPEND').key(key).arg(value),
237
+ as.number,
238
+ ),
239
+ /** Overwrites the value from byte `offset`, returning its new length. */
240
+ setrange: write(
241
+ (key: string, offset: number, value: RedisArg) =>
242
+ cmd('SETRANGE').key(key).arg(offset).arg(value),
243
+ as.number,
244
+ ),
245
+ /**
246
+ * Adds `delta` to the integer at the key, returning the result. A missing
247
+ * key counts as 0.
248
+ */
249
+ incr: write(
250
+ (key: string, delta: number | bigint) => cmd('INCRBY').key(key).arg(delta),
251
+ as.number,
252
+ ),
253
+ /**
254
+ * Adds `delta` to the number at the key, returning the result. A missing key
255
+ * counts as 0.
256
+ */
257
+ incrFloat: write(
258
+ (key: string, delta: number) => cmd('INCRBYFLOAT').key(key).arg(delta),
259
+ as.number,
260
+ ),
261
+ /** Subtracts `delta` from the integer at the key, returning the result. */
262
+ decr: write(
263
+ (key: string, delta: number | bigint) => cmd('DECRBY').key(key).arg(delta),
264
+ as.number,
265
+ ),
266
+ };
267
+
268
+ const bitmap = {
269
+ /** The bit at `offset`. */
270
+ getbit: read(
271
+ (key: string, offset: number) => cmd('GETBIT').key(key).arg(offset),
272
+ as.bool,
273
+ ),
274
+ /** How many bits are set. */
275
+ bitcount: read((key: string) => cmd('BITCOUNT').key(key), as.number),
276
+ /** How many bits are set from byte `start` to byte `end`, inclusive. */
277
+ bitcountRange: read(
278
+ (key: string, start: number, end: number) =>
279
+ cmd('BITCOUNT').key(key).arg(start).arg(end),
280
+ as.number,
281
+ ),
282
+ /** The position of the first bit that is `bit`, or -1 if there is none. */
283
+ bitpos: read(
284
+ (key: string, bit: boolean) => cmd('BITPOS').key(key).arg(bitArg(bit)),
285
+ as.number,
286
+ ),
287
+
288
+ /** Sets the bit at `offset`, returning what it was. */
289
+ setbit: write(
290
+ (key: string, offset: number, value: boolean) =>
291
+ cmd('SETBIT').key(key).arg(offset).arg(bitArg(value)),
292
+ as.bool,
293
+ ),
294
+ /**
295
+ * Stores the AND of the sources at `destination`, returning its length in
296
+ * bytes.
297
+ */
298
+ bitAnd: write(
299
+ (destination: string, ...sources: string[]) =>
300
+ cmd('BITOP').arg('AND').key(destination).keys(sources),
301
+ as.number,
302
+ ),
303
+ /**
304
+ * Stores the OR of the sources at `destination`, returning its length in
305
+ * bytes.
306
+ */
307
+ bitOr: write(
308
+ (destination: string, ...sources: string[]) =>
309
+ cmd('BITOP').arg('OR').key(destination).keys(sources),
310
+ as.number,
311
+ ),
312
+ /**
313
+ * Stores the XOR of the sources at `destination`, returning its length in
314
+ * bytes.
315
+ */
316
+ bitXor: write(
317
+ (destination: string, ...sources: string[]) =>
318
+ cmd('BITOP').arg('XOR').key(destination).keys(sources),
319
+ as.number,
320
+ ),
321
+ /**
322
+ * Stores the inverse of the source at `destination`, returning its length in
323
+ * bytes.
324
+ */
325
+ bitNot: write(
326
+ (destination: string, source: string) =>
327
+ cmd('BITOP').arg('NOT').key(destination).key(source),
328
+ as.number,
329
+ ),
330
+ };
331
+
332
+ const hash = {
333
+ /** The value of a field: nil if it does not exist. */
334
+ hget: read(
335
+ (key: string, field: RedisArg) => cmd('HGET').key(key).arg(field),
336
+ as.reply,
337
+ ),
338
+ /**
339
+ * The values of the fields, in order, each missing one as nil: read it with
340
+ * {@link Reply.optionalTexts}.
341
+ */
342
+ hmget: read(
343
+ (key: string, ...fields: RedisArg[]) =>
344
+ cmd('HMGET').key(key).argsOf(fields),
345
+ as.reply,
346
+ ),
347
+ /** Every field and its value: read it with {@link Reply.textMap}. */
348
+ hgetall: read((key: string) => cmd('HGETALL').key(key), as.reply),
349
+ /** Whether the field exists. */
350
+ hexists: read(
351
+ (key: string, field: RedisArg) => cmd('HEXISTS').key(key).arg(field),
352
+ as.bool,
353
+ ),
354
+ /** How many fields the hash has. */
355
+ hlen: read((key: string) => cmd('HLEN').key(key), as.number),
356
+ /** The names of the fields. */
357
+ hkeys: read((key: string) => cmd('HKEYS').key(key), as.reply),
358
+ /** The values of the fields. */
359
+ hvals: read((key: string) => cmd('HVALS').key(key), as.reply),
360
+ /** The name of a field chosen at random. */
361
+ hrandfield: read((key: string) => cmd('HRANDFIELD').key(key), as.reply),
362
+ /** Iterates over the hash's fields, each with its value. */
363
+ hscan: readRun((t: Target, key: string, options?: ScanOptions) =>
364
+ scanFields(t, key, options),
365
+ ),
366
+
367
+ /** Sets a field, returning 1 if it is new and 0 if it was replaced. */
368
+ hset: write(
369
+ (key: string, field: RedisArg, value: RedisArg) =>
370
+ cmd('HSET').key(key).arg(field).arg(value),
371
+ as.number,
372
+ ),
373
+ /** Sets several fields, returning how many are new. */
374
+ hsetMultiple: write(
375
+ (key: string, items: Pairs<RedisArg>) => cmd('HSET').key(key).fields(items),
376
+ as.number,
377
+ ),
378
+ /** Sets a field unless it exists. `false` if it does. */
379
+ hsetNx: write(
380
+ (key: string, field: RedisArg, value: RedisArg) =>
381
+ cmd('HSETNX').key(key).arg(field).arg(value),
382
+ as.bool,
383
+ ),
384
+ /** Deletes the fields, returning how many existed. */
385
+ hdel: write(
386
+ (key: string, ...fields: RedisArg[]) => cmd('HDEL').key(key).argsOf(fields),
387
+ as.number,
388
+ ),
389
+ /** Adds `delta` to the integer in a field, returning the result. */
390
+ hincr: write(
391
+ (key: string, field: RedisArg, delta: number | bigint) =>
392
+ cmd('HINCRBY').key(key).arg(field).arg(delta),
393
+ as.number,
394
+ ),
395
+ /** Adds `delta` to the number in a field, returning the result. */
396
+ hincrFloat: write(
397
+ (key: string, field: RedisArg, delta: number) =>
398
+ cmd('HINCRBYFLOAT').key(key).arg(field).arg(delta),
399
+ as.number,
400
+ ),
401
+ };
402
+
403
+ const list = {
404
+ /**
405
+ * The elements from index `start` to index `stop`, inclusive. Negative
406
+ * indexes count from the end: `0, -1` is the whole list.
407
+ */
408
+ lrange: read(
409
+ (key: string, start: number, stop: number) =>
410
+ cmd('LRANGE').key(key).arg(start).arg(stop),
411
+ as.reply,
412
+ ),
413
+ /** The element at an index. */
414
+ lindex: read(
415
+ (key: string, index: number) => cmd('LINDEX').key(key).arg(index),
416
+ as.reply,
417
+ ),
418
+ /** How many elements the list has. */
419
+ llen: read((key: string) => cmd('LLEN').key(key), as.number),
420
+ /** The index of the first element equal to `value`: `null` if there is none. */
421
+ lpos: read(
422
+ (key: string, value: RedisArg) => cmd('LPOS').key(key).arg(value),
423
+ as.optionalNumber,
424
+ ),
425
+
426
+ /** Prepends the values, returning the list's new length. */
427
+ lpush: write(
428
+ (key: string, ...values: RedisArg[]) =>
429
+ cmd('LPUSH').key(key).argsOf(values),
430
+ as.number,
431
+ ),
432
+ /** Appends the values, returning the list's new length. */
433
+ rpush: write(
434
+ (key: string, ...values: RedisArg[]) =>
435
+ cmd('RPUSH').key(key).argsOf(values),
436
+ as.number,
437
+ ),
438
+ /** Prepends the values if the list exists, returning its length. */
439
+ lpushExists: write(
440
+ (key: string, ...values: RedisArg[]) =>
441
+ cmd('LPUSHX').key(key).argsOf(values),
442
+ as.number,
443
+ ),
444
+ /** Appends the values if the list exists, returning its length. */
445
+ rpushExists: write(
446
+ (key: string, ...values: RedisArg[]) =>
447
+ cmd('RPUSHX').key(key).argsOf(values),
448
+ as.number,
449
+ ),
450
+ /**
451
+ * Removes and returns the first element, or with a `count`, that many, as a
452
+ * list.
453
+ */
454
+ lpop: write(
455
+ (key: string, count?: number) =>
456
+ cmd('LPOP')
457
+ .key(key)
458
+ .argsOf(count === undefined ? [] : [count]),
459
+ as.reply,
460
+ ),
461
+ /**
462
+ * Removes and returns the last element, or with a `count`, that many, as a
463
+ * list.
464
+ */
465
+ rpop: write(
466
+ (key: string, count?: number) =>
467
+ cmd('RPOP')
468
+ .key(key)
469
+ .argsOf(count === undefined ? [] : [count]),
470
+ as.reply,
471
+ ),
472
+ /** Replaces the element at an index. */
473
+ lset: write(
474
+ (key: string, index: number, value: RedisArg) =>
475
+ cmd('LSET').key(key).arg(index).arg(value),
476
+ as.none,
477
+ ),
478
+ /** Keeps only the elements from index `start` to index `stop`, inclusive. */
479
+ ltrim: write(
480
+ (key: string, start: number, stop: number) =>
481
+ cmd('LTRIM').key(key).arg(start).arg(stop),
482
+ as.none,
483
+ ),
484
+ /**
485
+ * Inserts the value before the first element equal to `pivot`, returning
486
+ * the list's new length, or -1 if there is no such element.
487
+ */
488
+ linsertBefore: write(
489
+ (key: string, pivot: RedisArg, value: RedisArg) =>
490
+ cmd('LINSERT').key(key).arg('BEFORE').arg(pivot).arg(value),
491
+ as.number,
492
+ ),
493
+ /**
494
+ * Inserts the value after the first element equal to `pivot`, returning the
495
+ * list's new length, or -1 if there is no such element.
496
+ */
497
+ linsertAfter: write(
498
+ (key: string, pivot: RedisArg, value: RedisArg) =>
499
+ cmd('LINSERT').key(key).arg('AFTER').arg(pivot).arg(value),
500
+ as.number,
501
+ ),
502
+ /**
503
+ * Removes elements equal to `value`: the first `count` of them, the last
504
+ * `-count` if it is negative, or all if it is 0. Returns how many.
505
+ */
506
+ lrem: write(
507
+ (key: string, count: number, value: RedisArg) =>
508
+ cmd('LREM').key(key).arg(count).arg(value),
509
+ as.number,
510
+ ),
511
+ /**
512
+ * Moves an element from one end of `source` to one end of `destination`,
513
+ * returning it.
514
+ */
515
+ lmove: write(
516
+ (source: string, destination: string, from: Direction, to: Direction) =>
517
+ cmd('LMOVE')
518
+ .key(source)
519
+ .key(destination)
520
+ .arg(directionArg(from))
521
+ .arg(directionArg(to)),
522
+ as.reply,
523
+ ),
524
+ };
525
+
526
+ const set = {
527
+ /** Every member. */
528
+ smembers: read((key: string) => cmd('SMEMBERS').key(key), as.reply),
529
+ /** Whether the value is a member. */
530
+ sismember: read(
531
+ (key: string, member: RedisArg) => cmd('SISMEMBER').key(key).arg(member),
532
+ as.bool,
533
+ ),
534
+ /** Whether each of the values is a member, in order. */
535
+ smismember: read(
536
+ (key: string, ...members: RedisArg[]) =>
537
+ cmd('SMISMEMBER').key(key).argsOf(members),
538
+ as.bools,
539
+ ),
540
+ /** How many members the set has. */
541
+ scard: read((key: string) => cmd('SCARD').key(key), as.number),
542
+ /** A member chosen at random. */
543
+ srandmember: read((key: string) => cmd('SRANDMEMBER').key(key), as.reply),
544
+ /**
545
+ * `count` distinct members chosen at random, or with a negative `count` that
546
+ * many, possibly repeated.
547
+ */
548
+ srandmemberMultiple: read(
549
+ (key: string, count: number) => cmd('SRANDMEMBER').key(key).arg(count),
550
+ as.reply,
551
+ ),
552
+ /** The members of the first set that are in none of the others. */
553
+ sdiff: read((...keys: string[]) => cmd('SDIFF').keys(keys), as.reply),
554
+ /** The members that are in every one of the sets. */
555
+ sinter: read((...keys: string[]) => cmd('SINTER').keys(keys), as.reply),
556
+ /** The members that are in any of the sets. */
557
+ sunion: read((...keys: string[]) => cmd('SUNION').keys(keys), as.reply),
558
+ /** Iterates over the set's members. */
559
+ sscan: readRun((t: Target, key: string, options?: ScanOptions) =>
560
+ scanMembers(t, key, options),
561
+ ),
562
+
563
+ /** Adds the members, returning how many are new. */
564
+ sadd: write(
565
+ (key: string, ...members: RedisArg[]) =>
566
+ cmd('SADD').key(key).argsOf(members),
567
+ as.number,
568
+ ),
569
+ /** Removes the members, returning how many there were. */
570
+ srem: write(
571
+ (key: string, ...members: RedisArg[]) =>
572
+ cmd('SREM').key(key).argsOf(members),
573
+ as.number,
574
+ ),
575
+ /** Removes and returns a member chosen at random. */
576
+ spop: write((key: string) => cmd('SPOP').key(key), as.reply),
577
+ /**
578
+ * Moves a member from one set to another. `false` if it is not in `source`.
579
+ */
580
+ smove: write(
581
+ (source: string, destination: string, member: RedisArg) =>
582
+ cmd('SMOVE').key(source).key(destination).arg(member),
583
+ as.bool,
584
+ ),
585
+ /** Stores `sdiff` of the sets at `destination`, returning its size. */
586
+ sdiffstore: write(
587
+ (destination: string, ...keys: string[]) =>
588
+ cmd('SDIFFSTORE').key(destination).keys(keys),
589
+ as.number,
590
+ ),
591
+ /** Stores `sinter` of the sets at `destination`, returning its size. */
592
+ sinterstore: write(
593
+ (destination: string, ...keys: string[]) =>
594
+ cmd('SINTERSTORE').key(destination).keys(keys),
595
+ as.number,
596
+ ),
597
+ /** Stores `sunion` of the sets at `destination`, returning its size. */
598
+ sunionstore: write(
599
+ (destination: string, ...keys: string[]) =>
600
+ cmd('SUNIONSTORE').key(destination).keys(keys),
601
+ as.number,
602
+ ),
603
+ };
604
+
605
+ const sortedSet = {
606
+ /**
607
+ * The members ranked `start` to `stop`, inclusive, lowest score first.
608
+ * Negative ranks count from the highest: `0, -1` is every member.
609
+ */
610
+ zrange: read(
611
+ (key: string, start: number, stop: number) =>
612
+ cmd('ZRANGE').key(key).arg(start).arg(stop),
613
+ as.reply,
614
+ ),
615
+ /**
616
+ * `zrange`, each member with its score: read it with
617
+ * {@link Reply.scoredMembers}.
618
+ */
619
+ zrangeWithscores: read(
620
+ (key: string, start: number, stop: number) =>
621
+ cmd('ZRANGE').key(key).arg(start).arg(stop).arg('WITHSCORES'),
622
+ as.reply,
623
+ ),
624
+ /** The members ranked `start` to `stop`, inclusive, highest score first. */
625
+ zrevrange: read(
626
+ (key: string, start: number, stop: number) =>
627
+ cmd('ZREVRANGE').key(key).arg(start).arg(stop),
628
+ as.reply,
629
+ ),
630
+ /**
631
+ * `zrevrange`, each member with its score: read it with
632
+ * {@link Reply.scoredMembers}.
633
+ */
634
+ zrevrangeWithscores: read(
635
+ (key: string, start: number, stop: number) =>
636
+ cmd('ZREVRANGE').key(key).arg(start).arg(stop).arg('WITHSCORES'),
637
+ as.reply,
638
+ ),
639
+ /** The members with a score from `min` to `max`, lowest first. */
640
+ zrangebyscore: read(
641
+ (key: string, min: ScoreBound, max: ScoreBound) =>
642
+ cmd('ZRANGEBYSCORE').key(key).arg(min).arg(max),
643
+ as.reply,
644
+ ),
645
+ /**
646
+ * `zrangebyscore`, each member with its score: read it with
647
+ * {@link Reply.scoredMembers}.
648
+ */
649
+ zrangebyscoreWithscores: read(
650
+ (key: string, min: ScoreBound, max: ScoreBound) =>
651
+ cmd('ZRANGEBYSCORE').key(key).arg(min).arg(max).arg('WITHSCORES'),
652
+ as.reply,
653
+ ),
654
+ /** A member's score: `null` if it is not a member. */
655
+ zscore: read(
656
+ (key: string, member: RedisArg) => cmd('ZSCORE').key(key).arg(member),
657
+ as.optionalNumber,
658
+ ),
659
+ /**
660
+ * A member's rank, lowest score first, from 0: `null` if it is not a
661
+ * member.
662
+ */
663
+ zrank: read(
664
+ (key: string, member: RedisArg) => cmd('ZRANK').key(key).arg(member),
665
+ as.optionalNumber,
666
+ ),
667
+ /**
668
+ * A member's rank, highest score first, from 0: `null` if it is not a
669
+ * member.
670
+ */
671
+ zrevrank: read(
672
+ (key: string, member: RedisArg) => cmd('ZREVRANK').key(key).arg(member),
673
+ as.optionalNumber,
674
+ ),
675
+ /** How many members the set has. */
676
+ zcard: read((key: string) => cmd('ZCARD').key(key), as.number),
677
+ /** How many members have a score from `min` to `max`. */
678
+ zcount: read(
679
+ (key: string, min: ScoreBound, max: ScoreBound) =>
680
+ cmd('ZCOUNT').key(key).arg(min).arg(max),
681
+ as.number,
682
+ ),
683
+ /** A member chosen at random. */
684
+ zrandmember: read((key: string) => cmd('ZRANDMEMBER').key(key), as.reply),
685
+ /** Iterates over the sorted set's members, each with its score. */
686
+ zscan: readRun((t: Target, key: string, options?: ScanOptions) =>
687
+ scanScored(t, key, options),
688
+ ),
689
+
690
+ /**
691
+ * Adds a member with a score, or updates its score. Returns 1 if it is new.
692
+ */
693
+ zadd: write(
694
+ (key: string, member: RedisArg, score: number) =>
695
+ cmd('ZADD').key(key).arg(score).arg(member),
696
+ as.number,
697
+ ),
698
+ /**
699
+ * Adds several members, each with its score, returning how many are new.
700
+ */
701
+ zaddMultiple: write(
702
+ (key: string, items: Pairs<number>) => cmd('ZADD').key(key).scores(items),
703
+ as.number,
704
+ ),
705
+ /** Removes the members, returning how many there were. */
706
+ zrem: write(
707
+ (key: string, ...members: RedisArg[]) =>
708
+ cmd('ZREM').key(key).argsOf(members),
709
+ as.number,
710
+ ),
711
+ /** Adds `delta` to a member's score, returning the result. */
712
+ zincr: write(
713
+ (key: string, member: RedisArg, delta: number) =>
714
+ cmd('ZINCRBY').key(key).arg(delta).arg(member),
715
+ as.number,
716
+ ),
717
+ /**
718
+ * Removes and returns the `count` members with the lowest scores, each with
719
+ * its score: read it with {@link Reply.scoredMembers}.
720
+ */
721
+ zpopmin: write(
722
+ (key: string, count: number) => cmd('ZPOPMIN').key(key).arg(count),
723
+ as.reply,
724
+ ),
725
+ /**
726
+ * Removes and returns the `count` members with the highest scores, each
727
+ * with its score: read it with {@link Reply.scoredMembers}.
728
+ */
729
+ zpopmax: write(
730
+ (key: string, count: number) => cmd('ZPOPMAX').key(key).arg(count),
731
+ as.reply,
732
+ ),
733
+ /**
734
+ * Removes the members with a score from `min` to `max`, returning how many.
735
+ */
736
+ zrembyscore: write(
737
+ (key: string, min: ScoreBound, max: ScoreBound) =>
738
+ cmd('ZREMRANGEBYSCORE').key(key).arg(min).arg(max),
739
+ as.number,
740
+ ),
741
+ /** Removes the members ranked `start` to `stop`, returning how many. */
742
+ zremrangebyrank: write(
743
+ (key: string, start: number, stop: number) =>
744
+ cmd('ZREMRANGEBYRANK').key(key).arg(start).arg(stop),
745
+ as.number,
746
+ ),
747
+ /**
748
+ * Stores at `destination` the members in every one of the sets, their
749
+ * scores summed. Returns its size.
750
+ */
751
+ zinterstore: write(
752
+ (destination: string, ...keys: string[]) =>
753
+ cmd('ZINTERSTORE').key(destination).numkeys(keys),
754
+ as.number,
755
+ ),
756
+ /**
757
+ * Stores at `destination` the members in any of the sets, their scores
758
+ * summed. Returns its size.
759
+ */
760
+ zunionstore: write(
761
+ (destination: string, ...keys: string[]) =>
762
+ cmd('ZUNIONSTORE').key(destination).numkeys(keys),
763
+ as.number,
764
+ ),
765
+ };
766
+
767
+ const geo = {
768
+ /**
769
+ * The positions of the members, each missing one as nil: read it with
770
+ * {@link Reply.geoPositions}.
771
+ */
772
+ geoPos: read(
773
+ (key: string, ...members: RedisArg[]) =>
774
+ cmd('GEOPOS').key(key).argsOf(members),
775
+ as.reply,
776
+ ),
777
+ /** The distance between two members: `null` if either is not a member. */
778
+ geoDist: read(
779
+ (key: string, from: RedisArg, to: RedisArg, unit: Unit) =>
780
+ cmd('GEODIST').key(key).arg(from).arg(to).arg(unit),
781
+ as.optionalNumber,
782
+ ),
783
+ /** The geohash of each member. */
784
+ geoHash: read(
785
+ (key: string, ...members: RedisArg[]) =>
786
+ cmd('GEOHASH').key(key).argsOf(members),
787
+ as.reply,
788
+ ),
789
+ /**
790
+ * The members within `radius` of a position: read it with
791
+ * {@link Reply.geoRadius}.
792
+ */
793
+ geoRadius: read(
794
+ (
795
+ key: string,
796
+ longitude: number,
797
+ latitude: number,
798
+ radius: number,
799
+ unit: Unit,
800
+ options?: RadiusOptions,
801
+ ) =>
802
+ cmd('GEORADIUS_RO')
803
+ .key(key)
804
+ .arg(longitude)
805
+ .arg(latitude)
806
+ .arg(radius)
807
+ .arg(unit)
808
+ .argsOf(radiusArgs(options)),
809
+ as.reply,
810
+ ),
811
+ /**
812
+ * The members within `radius` of another member: read it with
813
+ * {@link Reply.geoRadius}.
814
+ */
815
+ geoRadiusByMember: read(
816
+ (
817
+ key: string,
818
+ member: RedisArg,
819
+ radius: number,
820
+ unit: Unit,
821
+ options?: RadiusOptions,
822
+ ) =>
823
+ cmd('GEORADIUSBYMEMBER_RO')
824
+ .key(key)
825
+ .arg(member)
826
+ .arg(radius)
827
+ .arg(unit)
828
+ .argsOf(radiusArgs(options)),
829
+ as.reply,
830
+ ),
831
+
832
+ /** Adds a member at a position, or moves it there. Returns 1 if it is new. */
833
+ geoAdd: write(
834
+ (key: string, longitude: number, latitude: number, member: RedisArg) =>
835
+ cmd('GEOADD').key(key).arg(longitude).arg(latitude).arg(member),
836
+ as.number,
837
+ ),
838
+ };
839
+
840
+ const stream = {
841
+ /**
842
+ * The entries with an id from `start` to `end`, oldest first. `"-"` and
843
+ * `"+"` are the lowest and highest ids. Read it with
844
+ * {@link Reply.streamEntries}.
845
+ */
846
+ xrange: read(
847
+ (key: string, start: string, end: string) =>
848
+ cmd('XRANGE').key(key).arg(start).arg(end),
849
+ as.reply,
850
+ ),
851
+ /** `xrange`, at most `count` entries. */
852
+ xrangeCount: read(
853
+ (key: string, start: string, end: string, count: number) =>
854
+ cmd('XRANGE').key(key).arg(start).arg(end).arg('COUNT').arg(count),
855
+ as.reply,
856
+ ),
857
+ /** The entries with an id from `end` down to `start`, newest first. */
858
+ xrevrange: read(
859
+ (key: string, end: string, start: string) =>
860
+ cmd('XREVRANGE').key(key).arg(end).arg(start),
861
+ as.reply,
862
+ ),
863
+ /** `xrevrange`, at most `count` entries. */
864
+ xrevrangeCount: read(
865
+ (key: string, end: string, start: string, count: number) =>
866
+ cmd('XREVRANGE').key(key).arg(end).arg(start).arg('COUNT').arg(count),
867
+ as.reply,
868
+ ),
869
+ /** How many entries the stream has. */
870
+ xlen: read((key: string) => cmd('XLEN').key(key), as.number),
871
+ /**
872
+ * The entries after an id in each of the streams, `ids` giving the id for
873
+ * each of `keys` in order. Returns at once with what is there. Read it with
874
+ * {@link Reply.streams}.
875
+ */
876
+ xread: read(
877
+ (keys: string[], ids: string[]) =>
878
+ cmd('XREAD').arg('STREAMS').keys(keys).argsOf(ids).withReplyKeys(),
879
+ as.reply,
880
+ ),
881
+ /**
882
+ * A summary of the entries a consumer group has been given and not
883
+ * acknowledged: read it with {@link Reply.streamPending}.
884
+ */
885
+ xpending: read(
886
+ (key: string, group: string) => cmd('XPENDING').key(key).arg(group),
887
+ as.reply,
888
+ ),
889
+ /**
890
+ * What the server knows about the stream: read it with
891
+ * {@link Reply.streamInfo}.
892
+ */
893
+ xinfoStream: read(
894
+ (key: string) => cmd('XINFO').arg('STREAM').key(key),
895
+ as.reply,
896
+ ),
897
+
898
+ /**
899
+ * Appends an entry with the given fields, replying its id. Pass `"*"` as
900
+ * the id to have the server choose it.
901
+ */
902
+ xadd: write(
903
+ (key: string, id: string, fields: Pairs<RedisArg>) =>
904
+ cmd('XADD').key(key).arg(id).fields(fields),
905
+ as.reply,
906
+ ),
907
+ /** `xadd`, trimming the stream to a maximum length. */
908
+ xaddMaxlen: write(
909
+ (key: string, maxlen: StreamMaxlen, id: string, fields: Pairs<RedisArg>) =>
910
+ cmd('XADD').key(key).argsOf(maxlenArgs(maxlen)).arg(id).fields(fields),
911
+ as.reply,
912
+ ),
913
+ /** Deletes the entries with the given ids, returning how many existed. */
914
+ xdel: write(
915
+ (key: string, ...ids: string[]) => cmd('XDEL').key(key).argsOf(ids),
916
+ as.number,
917
+ ),
918
+ /**
919
+ * Trims the stream to a maximum length, returning how many entries were
920
+ * removed.
921
+ */
922
+ xtrim: write(
923
+ (key: string, maxlen: StreamMaxlen) =>
924
+ cmd('XTRIM').key(key).argsOf(maxlenArgs(maxlen)),
925
+ as.number,
926
+ ),
927
+ /**
928
+ * Creates a consumer group that starts reading after `id`: `"$"` for only
929
+ * new entries, `"0"` for all. The stream must exist.
930
+ */
931
+ xgroupCreate: write(
932
+ (key: string, group: string, id: string) =>
933
+ cmd('XGROUP').arg('CREATE').key(key).arg(group).arg(id),
934
+ as.none,
935
+ ),
936
+ /** `xgroupCreate`, creating the stream if it does not exist. */
937
+ xgroupCreateMkstream: write(
938
+ (key: string, group: string, id: string) =>
939
+ cmd('XGROUP').arg('CREATE').key(key).arg(group).arg(id).arg('MKSTREAM'),
940
+ as.none,
941
+ ),
942
+ /** Deletes a consumer group. `false` if there was none. */
943
+ xgroupDestroy: write(
944
+ (key: string, group: string) =>
945
+ cmd('XGROUP').arg('DESTROY').key(key).arg(group),
946
+ as.bool,
947
+ ),
948
+ /**
949
+ * Reads entries as `consumer` of a group: `">"` as the id for the ones no
950
+ * consumer has been given yet. With a `count`, at most that many per
951
+ * stream. A write: it records them as given to the consumer. Read it with
952
+ * {@link Reply.streams}.
953
+ */
954
+ xreadGroup: write(
955
+ (
956
+ group: string,
957
+ consumer: string,
958
+ keys: string[],
959
+ ids: string[],
960
+ count?: number,
961
+ ) =>
962
+ cmd('XREADGROUP')
963
+ .arg('GROUP')
964
+ .arg(group)
965
+ .arg(consumer)
966
+ .argsOf(count === undefined ? [] : ['COUNT', count])
967
+ .arg('STREAMS')
968
+ .keys(keys)
969
+ .argsOf(ids)
970
+ .withReplyKeys(),
971
+ as.reply,
972
+ ),
973
+ /**
974
+ * Acknowledges entries a consumer group was given, returning how many were
975
+ * pending.
976
+ */
977
+ xack: write(
978
+ (key: string, group: string, ...ids: string[]) =>
979
+ cmd('XACK').key(key).arg(group).argsOf(ids),
980
+ as.number,
981
+ ),
982
+ /**
983
+ * Takes over pending entries that have been idle for `minIdleMs`, for
984
+ * `consumer`: read it with {@link Reply.streamEntries}.
985
+ */
986
+ xclaim: write(
987
+ (
988
+ key: string,
989
+ group: string,
990
+ consumer: string,
991
+ minIdleMs: number,
992
+ ...ids: string[]
993
+ ) =>
994
+ cmd('XCLAIM')
995
+ .key(key)
996
+ .arg(group)
997
+ .arg(consumer)
998
+ .arg(minIdleMs)
999
+ .argsOf(ids),
1000
+ as.reply,
1001
+ ),
1002
+ /**
1003
+ * Takes over the pending entries from id `start` that have been idle for
1004
+ * `minIdleMs`, for `consumer`: read it with {@link Reply.streamAutoClaim}.
1005
+ */
1006
+ xautoclaim: write(
1007
+ (
1008
+ key: string,
1009
+ group: string,
1010
+ consumer: string,
1011
+ minIdleMs: number,
1012
+ start: string,
1013
+ ) =>
1014
+ cmd('XAUTOCLAIM')
1015
+ .key(key)
1016
+ .arg(group)
1017
+ .arg(consumer)
1018
+ .arg(minIdleMs)
1019
+ .arg(start),
1020
+ as.reply,
1021
+ ),
1022
+ };
1023
+
1024
+ const pubsub = {
1025
+ /** Subscribes to one or more channels, on a connection of its own. */
1026
+ subscribe: readRun(
1027
+ (t: Target, ...channels: string[]): Promise<Subscription> =>
1028
+ subscribe(t, channels, []),
1029
+ ),
1030
+ /**
1031
+ * Subscribes to the channels matching one or more glob-style patterns, on a
1032
+ * connection of its own.
1033
+ */
1034
+ psubscribe: readRun(
1035
+ (t: Target, ...patterns: string[]): Promise<Subscription> =>
1036
+ subscribe(t, [], patterns),
1037
+ ),
1038
+
1039
+ /**
1040
+ * Publishes a message to a channel, returning how many subscribers received
1041
+ * it. A channel is prefixed like a key, so only this database's subscribers
1042
+ * hear it.
1043
+ */
1044
+ publish: write(
1045
+ (channel: string, message: RedisArg) =>
1046
+ cmd('PUBLISH').key(channel).arg(message),
1047
+ as.number,
1048
+ ),
1049
+ };
1050
+
1051
+ /** Every group, by the name it has on a database. */
1052
+ const groups = {
1053
+ generic,
1054
+ string,
1055
+ bitmap,
1056
+ hash,
1057
+ list,
1058
+ set,
1059
+ sortedSet,
1060
+ geo,
1061
+ stream,
1062
+ pubsub,
1063
+ };
1064
+
1065
+ type Groups = typeof groups;
1066
+
1067
+ /** A command run now: a reply for the caller to read, or one of one shape. */
1068
+ type RunForm<D> =
1069
+ D extends CommandDef<infer A, infer R, boolean>
1070
+ ? (...args: A) => R extends Reply ? ReplyPromise : Promise<R>
1071
+ : D extends RunDef<infer A, infer R, boolean>
1072
+ ? (...args: A) => R
1073
+ : never;
1074
+
1075
+ /** A command group's commands, run as they are called. */
1076
+ export type Commands<G> = {readonly [K in keyof G]: RunForm<G[K]>};
1077
+
1078
+ /** A command group's commands that only read. */
1079
+ export type ReadCommands<G> = {
1080
+ readonly [K in keyof G as G[K] extends {write: false} ? K : never]: RunForm<
1081
+ G[K]
1082
+ >;
1083
+ };
1084
+
1085
+ /**
1086
+ * A command group's commands, queued on a pipeline or a transaction. What
1087
+ * each replies is read from `exec`, in the order queued.
1088
+ */
1089
+ export type Queue<G> = {
1090
+ readonly [
1091
+ K in keyof G as G[K] extends CommandDef<any, any, boolean> ? K : never
1092
+ ]: G[K] extends CommandDef<infer A, any, boolean>
1093
+ ? (...args: A) => Queue<G>
1094
+ : never;
1095
+ };
1096
+
1097
+ /** Commands that work on a key of any type: expiry, deletion, renaming. */
1098
+ export type GenericCommands = Commands<Groups['generic']>;
1099
+ /** Commands on strings, which is what a plain value is. */
1100
+ export type StringCommands = Commands<Groups['string']>;
1101
+ /** Commands on the bits of a string. */
1102
+ export type BitmapCommands = Commands<Groups['bitmap']>;
1103
+ /** Commands on hashes: a key holding fields, each with a value. */
1104
+ export type HashCommands = Commands<Groups['hash']>;
1105
+ /** Commands on lists. */
1106
+ export type ListCommands = Commands<Groups['list']>;
1107
+ /** Commands on sets. */
1108
+ export type SetCommands = Commands<Groups['set']>;
1109
+ /** Commands on sorted sets: members ordered by a score. */
1110
+ export type SortedSetCommands = Commands<Groups['sortedSet']>;
1111
+ /** Commands on geospatial indexes. */
1112
+ export type GeoCommands = Commands<Groups['geo']>;
1113
+ /** Commands on streams. */
1114
+ export type StreamCommands = Commands<Groups['stream']>;
1115
+ /** Publishing to channels, and subscribing to them. */
1116
+ export type PubsubCommands = Commands<Groups['pubsub']>;
1117
+
1118
+ /** The command groups of a database, a writing handle and a connection. */
1119
+ export type CommandGroups = {
1120
+ readonly [G in keyof Groups]: Commands<Groups[G]>;
1121
+ };
1122
+ /** The command groups of a handle that can only read. */
1123
+ export type ReadCommandGroups = {
1124
+ readonly [G in keyof Groups]: ReadCommands<Groups[G]>;
1125
+ };
1126
+ /** The command groups of a pipeline or a transaction. */
1127
+ export type QueueGroups = {readonly [G in keyof Groups]: Queue<Groups[G]>};
1128
+
1129
+ /** `setEx` as a trace names it, after the Rust SDK: `set_ex`. */
1130
+ function operation(name: string): string {
1131
+ return name.replace(/[A-Z]/g, c => `_${c.toLowerCase()}`);
1132
+ }
1133
+
1134
+ type AnyDef = CommandDef<any[], any, boolean> | RunDef<any[], any, boolean>;
1135
+
1136
+ /** The command `def` builds from `args`, named and marked as it is. */
1137
+ function built(
1138
+ op: string,
1139
+ def: CommandDef<any[], any, boolean>,
1140
+ args: unknown[],
1141
+ ): Cmd {
1142
+ const c = def.build(...args);
1143
+ const out = new Cmd(op, c.name, def.write);
1144
+ out.args.push(...c.args);
1145
+ out.replyKeys = c.replyKeys;
1146
+ return out;
1147
+ }
1148
+
1149
+ function runForms(t: Target, readOnly: boolean) {
1150
+ const out: Record<
1151
+ string,
1152
+ Record<string, (...args: unknown[]) => unknown>
1153
+ > = {};
1154
+ for (const [groupName, group] of Object.entries(groups) as [
1155
+ string,
1156
+ Record<string, AnyDef>,
1157
+ ][]) {
1158
+ const forms: Record<string, (...args: unknown[]) => unknown> = {};
1159
+ for (const [name, def] of Object.entries(group)) {
1160
+ if (readOnly && def.write) continue;
1161
+ if ('run' in def) {
1162
+ forms[name] = (...args) => def.run(t, ...args);
1163
+ continue;
1164
+ }
1165
+ const op = operation(name);
1166
+ if (def.reply === as.reply) {
1167
+ // Any error, the command's or that of sending it, is the reply's to
1168
+ // report when read.
1169
+ forms[name] = (...args) =>
1170
+ new ReplyPromise(
1171
+ (async () => {
1172
+ try {
1173
+ return new Reply(await t.run(built(op, def, args)));
1174
+ } catch (err) {
1175
+ return Reply.failed(err);
1176
+ }
1177
+ })(),
1178
+ );
1179
+ } else {
1180
+ forms[name] = async (...args) =>
1181
+ def.reply(await t.run(built(op, def, args)));
1182
+ }
1183
+ }
1184
+ out[groupName] = Object.freeze(forms);
1185
+ }
1186
+ return Object.freeze(out);
1187
+ }
1188
+
1189
+ /** The command groups of `t`, each running its commands as they are called. */
1190
+ export function commandGroups(t: Target): CommandGroups {
1191
+ return runForms(t, false) as unknown as CommandGroups;
1192
+ }
1193
+
1194
+ /** The command groups of `t` that only read. */
1195
+ export function readCommandGroups(t: Target): ReadCommandGroups {
1196
+ return runForms(t, true) as unknown as ReadCommandGroups;
1197
+ }
1198
+
1199
+ /** The command groups of a pipeline or a transaction, queueing on `queue`. */
1200
+ export function queueGroups(queue: Cmd[]): QueueGroups {
1201
+ const out: Record<
1202
+ string,
1203
+ Record<string, (...args: unknown[]) => unknown>
1204
+ > = {};
1205
+ for (const [groupName, group] of Object.entries(groups) as [
1206
+ string,
1207
+ Record<string, AnyDef>,
1208
+ ][]) {
1209
+ const forms: Record<string, (...args: unknown[]) => unknown> = {};
1210
+ for (const [name, def] of Object.entries(group)) {
1211
+ if (!('build' in def)) continue;
1212
+ const op = operation(name);
1213
+ forms[name] = (...args) => {
1214
+ queue.push(built(op, def, args));
1215
+ return forms;
1216
+ };
1217
+ }
1218
+ out[groupName] = Object.freeze(forms);
1219
+ }
1220
+ return Object.freeze(out) as unknown as QueueGroups;
1221
+ }