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

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