@encore.dev/sdk 2.0.0-beta.6 → 2.0.0-beta.8

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 (356) hide show
  1. package/README.md +55 -0
  2. package/app_meta.ts +8 -0
  3. package/config/config.test.ts +10 -3
  4. package/config/config.ts +75 -21
  5. package/config/mod.ts +4 -3
  6. package/config/secrets.ts +72 -55
  7. package/cron/mod.ts +32 -0
  8. package/dist/app_meta.d.ts +7 -0
  9. package/dist/app_meta.d.ts.map +1 -0
  10. package/dist/app_meta.js +1 -0
  11. package/dist/app_meta.js.map +1 -1
  12. package/dist/config/config.d.ts +44 -18
  13. package/dist/config/config.d.ts.map +1 -0
  14. package/dist/config/config.js +43 -18
  15. package/dist/config/config.js.map +1 -1
  16. package/dist/config/mod.d.ts +5 -4
  17. package/dist/config/mod.d.ts.map +1 -0
  18. package/dist/config/mod.js +2 -2
  19. package/dist/config/mod.js.map +1 -1
  20. package/dist/config/secrets.d.ts +62 -39
  21. package/dist/config/secrets.d.ts.map +1 -0
  22. package/dist/config/secrets.js +21 -37
  23. package/dist/config/secrets.js.map +1 -1
  24. package/dist/config/value.d.ts +1 -0
  25. package/dist/config/value.d.ts.map +1 -0
  26. package/dist/cron/mod.d.ts +31 -0
  27. package/dist/cron/mod.d.ts.map +1 -0
  28. package/dist/cron/mod.js +23 -0
  29. package/dist/cron/mod.js.map +1 -1
  30. package/dist/gateway/errors.d.ts +101 -0
  31. package/dist/gateway/errors.d.ts.map +1 -0
  32. package/dist/gateway/errors.js +146 -0
  33. package/dist/gateway/errors.js.map +1 -0
  34. package/dist/gateway/match.d.ts +127 -0
  35. package/dist/gateway/match.d.ts.map +1 -0
  36. package/dist/gateway/match.js +206 -0
  37. package/dist/gateway/match.js.map +1 -0
  38. package/dist/gateway/middleware.d.ts +108 -0
  39. package/dist/gateway/middleware.d.ts.map +1 -0
  40. package/dist/gateway/middleware.js +9 -0
  41. package/dist/gateway/middleware.js.map +1 -0
  42. package/dist/gateway/mod.d.ts +129 -0
  43. package/dist/gateway/mod.d.ts.map +1 -0
  44. package/dist/gateway/mod.js +187 -0
  45. package/dist/gateway/mod.js.map +1 -0
  46. package/dist/gateway/request.d.ts +304 -0
  47. package/dist/gateway/request.d.ts.map +1 -0
  48. package/dist/gateway/request.js +403 -0
  49. package/dist/gateway/request.js.map +1 -0
  50. package/dist/gateway/serve.d.ts +114 -0
  51. package/dist/gateway/serve.d.ts.map +1 -0
  52. package/dist/gateway/serve.js +510 -0
  53. package/dist/gateway/serve.js.map +1 -0
  54. package/dist/internal/clients/call.d.ts +147 -0
  55. package/dist/internal/clients/call.d.ts.map +1 -0
  56. package/dist/internal/clients/call.js +274 -0
  57. package/dist/internal/clients/call.js.map +1 -0
  58. package/dist/internal/clients/grpc.d.ts +90 -0
  59. package/dist/internal/clients/grpc.d.ts.map +1 -0
  60. package/dist/internal/clients/grpc.js +355 -0
  61. package/dist/internal/clients/grpc.js.map +1 -0
  62. package/dist/internal/clients/mod.d.ts +81 -0
  63. package/dist/internal/clients/mod.d.ts.map +1 -0
  64. package/dist/internal/clients/mod.js +237 -0
  65. package/dist/internal/clients/mod.js.map +1 -0
  66. package/dist/internal/fetch.d.ts +54 -0
  67. package/dist/internal/fetch.d.ts.map +1 -0
  68. package/dist/internal/fetch.js +240 -0
  69. package/dist/internal/fetch.js.map +1 -0
  70. package/dist/internal/otel/backend.d.ts +2 -0
  71. package/dist/internal/otel/backend.d.ts.map +1 -0
  72. package/dist/internal/otel/context.d.ts +1 -0
  73. package/dist/internal/otel/context.d.ts.map +1 -0
  74. package/dist/internal/otel/context_manager.d.ts +1 -0
  75. package/dist/internal/otel/context_manager.d.ts.map +1 -0
  76. package/dist/internal/otel/encode.d.ts +2 -0
  77. package/dist/internal/otel/encode.d.ts.map +1 -0
  78. package/dist/internal/otel/index.d.ts +2 -2
  79. package/dist/internal/otel/index.d.ts.map +1 -0
  80. package/dist/internal/otel/index.js +1 -2
  81. package/dist/internal/otel/index.js.map +1 -1
  82. package/dist/internal/otel/meter.d.ts +1 -0
  83. package/dist/internal/otel/meter.d.ts.map +1 -0
  84. package/dist/internal/otel/propagator.d.ts +1 -0
  85. package/dist/internal/otel/propagator.d.ts.map +1 -0
  86. package/dist/internal/otel/provider.d.ts +1 -0
  87. package/dist/internal/otel/provider.d.ts.map +1 -0
  88. package/dist/internal/otel/span.d.ts +4 -0
  89. package/dist/internal/otel/span.d.ts.map +1 -0
  90. package/dist/internal/otel/span.js +5 -0
  91. package/dist/internal/otel/span.js.map +1 -1
  92. package/dist/internal/otel/tracer.d.ts +1 -0
  93. package/dist/internal/otel/tracer.d.ts.map +1 -0
  94. package/dist/internal/otel/tracer.js +11 -5
  95. package/dist/internal/otel/tracer.js.map +1 -1
  96. package/dist/internal/reqfields.d.ts +150 -0
  97. package/dist/internal/reqfields.d.ts.map +1 -0
  98. package/dist/internal/reqfields.js +312 -0
  99. package/dist/internal/reqfields.js.map +1 -0
  100. package/dist/internal/runtime/meter.d.ts +1 -0
  101. package/dist/internal/runtime/meter.d.ts.map +1 -0
  102. package/dist/internal/runtime/mod.d.ts +6 -0
  103. package/dist/internal/runtime/mod.d.ts.map +1 -0
  104. package/dist/internal/runtime/mod.js +32 -2
  105. package/dist/internal/runtime/mod.js.map +1 -1
  106. package/dist/internal/runtime/napi/napi.cjs +160 -16
  107. package/dist/internal/runtime/napi/napi.d.cts +474 -29
  108. package/dist/internal/runtime/shutdown.d.ts +5 -0
  109. package/dist/internal/runtime/shutdown.d.ts.map +1 -0
  110. package/dist/internal/runtime/shutdown.js +9 -1
  111. package/dist/internal/runtime/shutdown.js.map +1 -1
  112. package/dist/internal/testing/runner.d.ts +2 -0
  113. package/dist/internal/testing/runner.d.ts.map +1 -0
  114. package/dist/internal/testing/runner.js +10 -0
  115. package/dist/internal/testing/runner.js.map +1 -0
  116. package/dist/internal/testing/scope.d.ts +81 -0
  117. package/dist/internal/testing/scope.d.ts.map +1 -0
  118. package/dist/internal/testing/scope.js +237 -0
  119. package/dist/internal/testing/scope.js.map +1 -0
  120. package/dist/internal/utils/constraints.d.ts +1 -0
  121. package/dist/internal/utils/constraints.d.ts.map +1 -0
  122. package/dist/log/index.d.ts +1 -0
  123. package/dist/log/index.d.ts.map +1 -0
  124. package/dist/log/logger.d.ts +2 -0
  125. package/dist/log/logger.d.ts.map +1 -0
  126. package/dist/log/runtime.d.ts +2 -0
  127. package/dist/log/runtime.d.ts.map +1 -0
  128. package/dist/log/stderr.d.ts +1 -0
  129. package/dist/log/stderr.d.ts.map +1 -0
  130. package/dist/metrics/mod.d.ts +45 -118
  131. package/dist/metrics/mod.d.ts.map +1 -0
  132. package/dist/metrics/mod.js +44 -179
  133. package/dist/metrics/mod.js.map +1 -1
  134. package/dist/mod.d.ts +1 -2
  135. package/dist/mod.d.ts.map +1 -0
  136. package/dist/mod.js +0 -1
  137. package/dist/mod.js.map +1 -1
  138. package/dist/otel.d.ts +1 -0
  139. package/dist/otel.d.ts.map +1 -0
  140. package/dist/pubsub/encoding.d.ts +5 -0
  141. package/dist/pubsub/encoding.d.ts.map +1 -0
  142. package/dist/pubsub/encoding.js +10 -2
  143. package/dist/pubsub/encoding.js.map +1 -1
  144. package/dist/pubsub/mod.d.ts +4 -3
  145. package/dist/pubsub/mod.d.ts.map +1 -0
  146. package/dist/pubsub/mod.js +2 -2
  147. package/dist/pubsub/mod.js.map +1 -1
  148. package/dist/pubsub/refs.d.ts +8 -0
  149. package/dist/pubsub/refs.d.ts.map +1 -0
  150. package/dist/pubsub/refs.js.map +1 -1
  151. package/dist/pubsub/subscription.d.ts +104 -10
  152. package/dist/pubsub/subscription.d.ts.map +1 -0
  153. package/dist/pubsub/subscription.js +98 -16
  154. package/dist/pubsub/subscription.js.map +1 -1
  155. package/dist/pubsub/topic.d.ts +87 -23
  156. package/dist/pubsub/topic.d.ts.map +1 -0
  157. package/dist/pubsub/topic.js +143 -40
  158. package/dist/pubsub/topic.js.map +1 -1
  159. package/dist/reqctx/mod.d.ts +191 -0
  160. package/dist/reqctx/mod.d.ts.map +1 -0
  161. package/dist/reqctx/mod.js +160 -0
  162. package/dist/reqctx/mod.js.map +1 -0
  163. package/dist/services/index.d.ts +145 -4
  164. package/dist/services/index.d.ts.map +1 -0
  165. package/dist/services/index.js +155 -8
  166. package/dist/services/index.js.map +1 -1
  167. package/dist/shutdown/mod.d.ts +1 -0
  168. package/dist/shutdown/mod.d.ts.map +1 -0
  169. package/dist/storage/objects/bucket.d.ts +312 -7
  170. package/dist/storage/objects/bucket.d.ts.map +1 -0
  171. package/dist/storage/objects/bucket.js +305 -15
  172. package/dist/storage/objects/bucket.js.map +1 -1
  173. package/dist/storage/objects/error.d.ts +10 -0
  174. package/dist/storage/objects/error.d.ts.map +1 -0
  175. package/dist/storage/objects/error.js +25 -1
  176. package/dist/storage/objects/error.js.map +1 -1
  177. package/dist/storage/objects/mod.d.ts +4 -3
  178. package/dist/storage/objects/mod.d.ts.map +1 -0
  179. package/dist/storage/objects/mod.js +1 -1
  180. package/dist/storage/objects/mod.js.map +1 -1
  181. package/dist/storage/objects/refs.d.ts +20 -4
  182. package/dist/storage/objects/refs.d.ts.map +1 -0
  183. package/dist/storage/objects/refs.js +6 -0
  184. package/dist/storage/objects/refs.js.map +1 -1
  185. package/dist/storage/postgres/database.d.ts +81 -25
  186. package/dist/storage/postgres/database.d.ts.map +1 -0
  187. package/dist/storage/postgres/database.js +125 -37
  188. package/dist/storage/postgres/database.js.map +1 -1
  189. package/dist/storage/postgres/errors.d.ts +120 -0
  190. package/dist/storage/postgres/errors.d.ts.map +1 -0
  191. package/dist/storage/postgres/errors.js +162 -0
  192. package/dist/storage/postgres/errors.js.map +1 -0
  193. package/dist/storage/postgres/interval.d.ts +44 -0
  194. package/dist/storage/postgres/interval.d.ts.map +1 -0
  195. package/dist/storage/postgres/interval.js +153 -0
  196. package/dist/storage/postgres/interval.js.map +1 -0
  197. package/dist/storage/postgres/mod.d.ts +4 -1
  198. package/dist/storage/postgres/mod.d.ts.map +1 -0
  199. package/dist/storage/postgres/mod.js +2 -0
  200. package/dist/storage/postgres/mod.js.map +1 -1
  201. package/dist/storage/redis/bitfield.d.ts +67 -0
  202. package/dist/storage/redis/bitfield.d.ts.map +1 -0
  203. package/dist/storage/redis/bitfield.js +114 -0
  204. package/dist/storage/redis/bitfield.js.map +1 -0
  205. package/dist/storage/redis/command.d.ts +45 -0
  206. package/dist/storage/redis/command.d.ts.map +1 -0
  207. package/dist/storage/redis/command.js +78 -0
  208. package/dist/storage/redis/command.js.map +1 -0
  209. package/dist/storage/redis/commands.d.ts +223 -139
  210. package/dist/storage/redis/commands.d.ts.map +1 -0
  211. package/dist/storage/redis/commands.js +128 -6
  212. package/dist/storage/redis/commands.js.map +1 -1
  213. package/dist/storage/redis/database.d.ts +56 -22
  214. package/dist/storage/redis/database.d.ts.map +1 -0
  215. package/dist/storage/redis/database.js +62 -0
  216. package/dist/storage/redis/database.js.map +1 -1
  217. package/dist/storage/redis/mod.d.ts +31 -4
  218. package/dist/storage/redis/mod.d.ts.map +1 -0
  219. package/dist/storage/redis/mod.js +26 -1
  220. package/dist/storage/redis/mod.js.map +1 -1
  221. package/dist/storage/redis/native.d.ts +8 -0
  222. package/dist/storage/redis/native.d.ts.map +1 -0
  223. package/dist/storage/redis/native.js +10 -0
  224. package/dist/storage/redis/native.js.map +1 -1
  225. package/dist/storage/redis/options.d.ts +1 -0
  226. package/dist/storage/redis/options.d.ts.map +1 -0
  227. package/dist/storage/redis/pubsub.d.ts +2 -0
  228. package/dist/storage/redis/pubsub.d.ts.map +1 -0
  229. package/dist/storage/redis/reply.d.ts +34 -0
  230. package/dist/storage/redis/reply.d.ts.map +1 -0
  231. package/dist/storage/redis/reply.js +40 -1
  232. package/dist/storage/redis/reply.js.map +1 -1
  233. package/dist/storage/redis/scan.d.ts +1 -0
  234. package/dist/storage/redis/scan.d.ts.map +1 -0
  235. package/dist/storage/redis/script.d.ts +1 -0
  236. package/dist/storage/redis/script.d.ts.map +1 -0
  237. package/dist/storage/redis/target.d.ts +5 -3
  238. package/dist/storage/redis/target.d.ts.map +1 -0
  239. package/dist/storage/redis/target.js +39 -28
  240. package/dist/storage/redis/target.js.map +1 -1
  241. package/dist/storage/redis/wire.d.ts +7 -10
  242. package/dist/storage/redis/wire.d.ts.map +1 -0
  243. package/dist/storage/redis/wire.js +8 -32
  244. package/dist/storage/redis/wire.js.map +1 -1
  245. package/dist/testing/bun.d.ts +8 -0
  246. package/dist/testing/bun.d.ts.map +1 -0
  247. package/dist/testing/bun.js +13 -0
  248. package/dist/testing/bun.js.map +1 -0
  249. package/dist/testing/jest.d.ts +8 -0
  250. package/dist/testing/jest.d.ts.map +1 -0
  251. package/dist/testing/jest.js +11 -0
  252. package/dist/testing/jest.js.map +1 -0
  253. package/dist/testing/mod.d.ts +160 -0
  254. package/dist/testing/mod.d.ts.map +1 -0
  255. package/dist/testing/mod.js +227 -0
  256. package/dist/testing/mod.js.map +1 -0
  257. package/dist/testing/node.d.ts +9 -0
  258. package/dist/testing/node.d.ts.map +1 -0
  259. package/dist/testing/node.js +12 -0
  260. package/dist/testing/node.js.map +1 -0
  261. package/dist/testing/vitest.d.ts +8 -0
  262. package/dist/testing/vitest.d.ts.map +1 -0
  263. package/dist/testing/vitest.js +13 -0
  264. package/dist/testing/vitest.js.map +1 -0
  265. package/dist/tracing/mod.d.ts +19 -0
  266. package/dist/tracing/mod.d.ts.map +1 -0
  267. package/dist/tracing/mod.js +46 -0
  268. package/dist/tracing/mod.js.map +1 -0
  269. package/dist/tsconfig.build.tsbuildinfo +1 -1
  270. package/dist/types/mod.d.ts +1 -0
  271. package/dist/types/mod.d.ts.map +1 -0
  272. package/gateway/errors.ts +172 -0
  273. package/gateway/gateway.e2e.test.ts +297 -0
  274. package/gateway/gateway.test.ts +891 -0
  275. package/gateway/match.ts +239 -0
  276. package/gateway/middleware.ts +134 -0
  277. package/gateway/mod.ts +210 -0
  278. package/gateway/request.ts +578 -0
  279. package/gateway/serve.ts +738 -0
  280. package/internal/clients/call.ts +389 -0
  281. package/internal/clients/grpc.test.ts +368 -0
  282. package/internal/clients/grpc.ts +485 -0
  283. package/internal/clients/mod.test.ts +464 -0
  284. package/internal/clients/mod.ts +337 -0
  285. package/internal/fetch.ts +276 -0
  286. package/internal/otel/index.ts +1 -7
  287. package/internal/otel/span.ts +6 -0
  288. package/internal/otel/tracer.test.ts +43 -15
  289. package/internal/otel/tracer.ts +12 -5
  290. package/internal/reqfields.ts +390 -0
  291. package/internal/runtime/mod.ts +41 -2
  292. package/internal/runtime/napi/napi.cjs +160 -16
  293. package/internal/runtime/napi/napi.d.cts +474 -29
  294. package/internal/runtime/shutdown.ts +11 -1
  295. package/internal/testing/runner.ts +10 -0
  296. package/internal/testing/scope.ts +273 -0
  297. package/metrics/mod.test.ts +13 -16
  298. package/metrics/mod.ts +57 -226
  299. package/mod.ts +0 -8
  300. package/otel.test.ts +13 -6
  301. package/package.json +67 -15
  302. package/pubsub/encoding.ts +11 -1
  303. package/pubsub/mod.ts +10 -3
  304. package/pubsub/pubsub.test.ts +232 -14
  305. package/pubsub/refs.ts +16 -0
  306. package/pubsub/subscription.ts +194 -16
  307. package/pubsub/topic.test.ts +26 -0
  308. package/pubsub/topic.ts +220 -25
  309. package/reqctx/mod.ts +284 -0
  310. package/reqctx/reqctx.test.ts +193 -0
  311. package/scripts/postprocess-docs.test.ts +48 -0
  312. package/services/index.test.ts +341 -1
  313. package/services/index.ts +183 -8
  314. package/storage/objects/bucket.test.ts +573 -0
  315. package/storage/objects/bucket.ts +649 -20
  316. package/storage/objects/error.ts +28 -1
  317. package/storage/objects/mod.ts +15 -0
  318. package/storage/objects/refs.ts +51 -3
  319. package/storage/postgres/database.integration.test.ts +275 -0
  320. package/storage/postgres/database.test.ts +193 -6
  321. package/storage/postgres/database.ts +202 -55
  322. package/storage/postgres/errors.ts +186 -0
  323. package/storage/postgres/interval.test.ts +72 -0
  324. package/storage/postgres/interval.ts +170 -0
  325. package/storage/postgres/mod.ts +4 -0
  326. package/storage/redis/bitfield.ts +162 -0
  327. package/storage/redis/command.ts +84 -0
  328. package/storage/redis/commands.ts +271 -21
  329. package/storage/redis/database.ts +77 -23
  330. package/storage/redis/mod.ts +38 -2
  331. package/storage/redis/native.ts +11 -0
  332. package/storage/redis/redis.test.ts +170 -10
  333. package/storage/redis/reply.ts +76 -1
  334. package/storage/redis/runtime.test.ts +15 -0
  335. package/storage/redis/target.ts +47 -44
  336. package/storage/redis/wire.ts +8 -32
  337. package/testing/bun.ts +13 -0
  338. package/testing/jest.ts +11 -0
  339. package/testing/mod.ts +337 -0
  340. package/testing/node.ts +12 -0
  341. package/testing/vitest.ts +13 -0
  342. package/tracing/mod.ts +82 -0
  343. package/tracing/native.test.ts +170 -0
  344. package/tracing/tracing.test.ts +317 -0
  345. package/dist/internal/otel/request_context.d.ts +0 -11
  346. package/dist/internal/otel/request_context.js +0 -20
  347. package/dist/internal/otel/request_context.js.map +0 -1
  348. package/dist/internal/reqtrack/mod.d.ts +0 -6
  349. package/dist/internal/reqtrack/mod.js +0 -28
  350. package/dist/internal/reqtrack/mod.js.map +0 -1
  351. package/dist/req_meta.d.ts +0 -47
  352. package/dist/req_meta.js +0 -26
  353. package/dist/req_meta.js.map +0 -1
  354. package/internal/otel/request_context.ts +0 -32
  355. package/internal/reqtrack/mod.ts +0 -39
  356. package/req_meta.ts +0 -75
@@ -61,8 +61,9 @@ function encode(r: R): Buffer {
61
61
  function decodeCommand(
62
62
  buf: Buffer,
63
63
  at = 0,
64
- ): [string, number, {write: boolean; op: string}] {
65
- const write = buf.readUInt8(at) === 1;
64
+ ): [string, number, {write: boolean; op: string; raw?: true; readOnly?: true}] {
65
+ const flags = buf.readUInt8(at);
66
+ const write = (flags & 1) === 1;
66
67
  at += 1;
67
68
  const str = () => {
68
69
  const n = buf.readUInt32LE(at);
@@ -83,7 +84,13 @@ function decodeCommand(
83
84
  : `0x${arg.toString('hex')}`;
84
85
  parts.push(key ? `{${text}}` : text);
85
86
  }
86
- return [parts.join(' '), at, {write, op}];
87
+ const meta: {write: boolean; op: string; raw?: true; readOnly?: true} = {
88
+ write,
89
+ op,
90
+ };
91
+ if (flags & 2) meta.raw = true;
92
+ if (flags & 4) meta.readOnly = true;
93
+ return [parts.join(' '), at, meta];
87
94
  }
88
95
 
89
96
  function decodeBatch(buf: Buffer): string[] {
@@ -128,6 +135,8 @@ const server = {
128
135
  subscribed: [] as string[],
129
136
  messages: [] as {channel: string; payload: string}[],
130
137
  subscriptionClosed: false,
138
+ /** Commands the runtime refuses, by name, and why. */
139
+ refuse: new Map<string, string>(),
131
140
  };
132
141
 
133
142
  function script(name: string, ...replies: R[]) {
@@ -145,6 +154,14 @@ function execute(cmd: Buffer): Promise<Buffer> {
145
154
  const [sent, , meta] = decodeCommand(cmd);
146
155
  server.sent.push(sent);
147
156
  server.meta.push(meta);
157
+ // The runtime refuses what Redis's command table says may not be sent, as
158
+ // an invalid argument.
159
+ const refusal = server.refuse.get(sent.split(' ')[0]);
160
+ if (refusal) {
161
+ return Promise.reject(
162
+ Object.assign(new Error(refusal), {code: 'InvalidArg'}),
163
+ );
164
+ }
148
165
  return Promise.resolve(encode(answer(sent)));
149
166
  }
150
167
 
@@ -212,6 +229,7 @@ mock.module('./native', () => ({
212
229
  opened.push(name);
213
230
  return native;
214
231
  },
232
+ testDatabase: () => null,
215
233
  }));
216
234
 
217
235
  const redis = await import('./mod');
@@ -230,6 +248,7 @@ beforeEach(() => {
230
248
  server.subscribed = [];
231
249
  server.messages = [];
232
250
  server.subscriptionClosed = false;
251
+ server.refuse = new Map();
233
252
  });
234
253
 
235
254
  describe('commands', () => {
@@ -435,14 +454,11 @@ describe('commands', () => {
435
454
  ]);
436
455
  });
437
456
 
438
- test("a stream read names its streams without the database's prefix", async () => {
457
+ // The runtime removes the prefix from the keys a reply names (the core's
458
+ // tests check that), so the reply comes without it.
459
+ test('a stream read names its streams', async () => {
439
460
  const entries = [['1-0', ['f', 'v']]];
440
- script(
441
- 'XREAD',
442
- [['p/orders', entries]],
443
- {map: [['p/orders', entries]]},
444
- null,
445
- );
461
+ script('XREAD', [['orders', entries]], {map: [['orders', entries]]}, null);
446
462
  const want = [{key: 'orders', entries: [{id: '1-0', fields: {f: 'v'}}]}];
447
463
  expect(await DB.stream.xread(['orders'], ['0']).streams()).toEqual(want);
448
464
  expect(await DB.stream.xread(['orders'], ['0']).streams()).toEqual(want);
@@ -704,3 +720,147 @@ describe('channels', () => {
704
720
  expect(server.sent).toEqual(['PUBLISH {orders} hi']);
705
721
  });
706
722
  });
723
+
724
+ describe('blocking commands', () => {
725
+ test('pop with the key they popped from, and null once the timeout passes', async () => {
726
+ script('BLPOP', ['jobs', 'send'], null);
727
+ script('BLMPOP', ['jobs', ['a', 'b']]);
728
+ script('BZPOPMIN', ['top', 'a', '1.5']);
729
+ script('BZMPOP', ['top', [['c', '3']]]);
730
+
731
+ const popped = await DB.list.blpop(['none', 'jobs'], 5);
732
+ expect(popped?.key).toBe('jobs');
733
+ expect(popped?.value.text()).toBe('send');
734
+ expect(await DB.list.blpop('jobs', 0.5)).toBeNull();
735
+ const many = await DB.list.blmpop(['jobs'], 'left', 2, 0);
736
+ expect(many?.values.map(v => v.text())).toEqual(['a', 'b']);
737
+ const scored = await DB.sortedSet.bzpopmin('top', 1);
738
+ expect([scored?.key, scored?.member.text(), scored?.score]).toEqual([
739
+ 'top',
740
+ 'a',
741
+ 1.5,
742
+ ]);
743
+ const members = await DB.sortedSet.bzmpop(['top'], 'max', 1, 1);
744
+ expect(members?.members.map(m => [m.member.text(), m.score])).toEqual([
745
+ ['c', 3],
746
+ ]);
747
+ await DB.list.blmove('a', 'b', 'left', 'right', 2);
748
+ await DB.stream.xreadBlock(['s'], ['$'], 100, 10);
749
+ await DB.stream.xreadGroupBlock('g', 'c', ['s'], ['>'], 0);
750
+
751
+ expect(server.sent).toEqual([
752
+ 'BLPOP {none} {jobs} 5',
753
+ 'BLPOP {jobs} 0.5',
754
+ 'BLMPOP 0 1 {jobs} LEFT COUNT 2',
755
+ 'BZPOPMIN {top} 1',
756
+ 'BZMPOP 1 1 {top} MAX COUNT 1',
757
+ 'BLMOVE {a} {b} LEFT RIGHT 2',
758
+ 'XREAD COUNT 10 BLOCK 100 STREAMS {s} $',
759
+ 'XREADGROUP GROUP g c BLOCK 0 STREAMS {s} >',
760
+ ]);
761
+ });
762
+
763
+ test('have no queued form, and a read handle has the reads among them', () => {
764
+ const pipe = DB.pipeline() as unknown as {list: Record<string, unknown>};
765
+ expect(pipe.list.blpop).toBeUndefined();
766
+ expect(pipe.list.lpop).toBeDefined();
767
+ const read = DB.read() as unknown as {
768
+ stream: Record<string, unknown>;
769
+ list: Record<string, unknown>;
770
+ };
771
+ expect(read.stream.xreadBlock).toBeDefined();
772
+ expect(read.list.blpop).toBeUndefined();
773
+ });
774
+ });
775
+
776
+ describe('hyperloglogs', () => {
777
+ test('add, count and merge', async () => {
778
+ script('PFADD', 1n);
779
+ script('PFCOUNT', 3n);
780
+ expect(await DB.hyperloglog.pfadd('day', 'a', 'b')).toBe(true);
781
+ expect(await DB.read().hyperloglog.pfcount('day', 'week')).toBe(3);
782
+ await DB.hyperloglog.pfmerge('week', 'mon', 'tue');
783
+ const pipe = DB.pipeline();
784
+ pipe.hyperloglog.pfadd('x', 1).pfcount('x');
785
+ await pipe.exec();
786
+ expect(server.sent).toEqual([
787
+ 'PFADD {day} a b',
788
+ 'PFCOUNT {day} {week}',
789
+ 'PFMERGE {week} {mon} {tue}',
790
+ 'PFADD {x} 1',
791
+ 'PFCOUNT {x}',
792
+ ]);
793
+ });
794
+ });
795
+
796
+ describe('bitfields', () => {
797
+ test('send their operations in order and reply one value each', async () => {
798
+ script('BITFIELD', [0n, 200n, null]);
799
+ script('BITFIELD_RO', [-56n], [2n ** 60n]);
800
+ expect(
801
+ await DB.bitmap
802
+ .bitfield('b')
803
+ .set('u8', 0, 200)
804
+ .get('u8', '#0')
805
+ .overflow('fail')
806
+ .incrBy('u8', 0, 100)
807
+ .invoke(),
808
+ ).toEqual([0, 200, null]);
809
+ expect(
810
+ await DB.read().bitmap.bitfieldRo('b').get('i8', 0).invoke(),
811
+ ).toEqual([-56]);
812
+ // A value a number can't hold exactly.
813
+ expect(DB.bitmap.bitfieldRo('b').get('i64', 0).invoke()).rejects.toThrow(
814
+ RangeError,
815
+ );
816
+
817
+ const tx = DB.transaction();
818
+ tx.bitmap.bitfield('b').incrBy('i5', 3, 1).queue();
819
+ await tx.exec();
820
+ expect(server.sent).toEqual([
821
+ 'BITFIELD {b} SET u8 0 200 GET u8 #0 OVERFLOW FAIL INCRBY u8 0 100',
822
+ 'BITFIELD_RO {b} GET i8 0',
823
+ 'BITFIELD_RO {b} GET i64 0',
824
+ 'BITFIELD {b} INCRBY i5 3 1',
825
+ ]);
826
+ expect(server.meta.slice(0, 2)).toEqual([
827
+ {write: true, op: 'bitfield'},
828
+ {write: false, op: 'bitfield_ro'},
829
+ ]);
830
+ });
831
+ });
832
+
833
+ describe('commands the groups lack', () => {
834
+ test('are sent by name, raw, with their keys marked', async () => {
835
+ script('OBJECT', 3n);
836
+ expect(
837
+ await DB.command('OBJECT').arg('FREQ').key('hot').invoke().number(),
838
+ ).toBe(3);
839
+ await DB.read().command('TYPE').key('k').invoke();
840
+ const pipe = DB.pipeline();
841
+ pipe.command('INCRBY').key('n').arg(2).queue();
842
+ await pipe.exec();
843
+ expect(server.sent).toEqual([
844
+ 'OBJECT FREQ {hot}',
845
+ 'TYPE {k}',
846
+ 'INCRBY {n} 2',
847
+ ]);
848
+ expect(server.meta).toEqual([
849
+ {write: true, op: '', raw: true},
850
+ {write: false, op: '', raw: true, readOnly: true},
851
+ ]);
852
+ });
853
+
854
+ test('the runtime refuses fail with a TypeError', async () => {
855
+ server.refuse.set(
856
+ 'FLUSHALL',
857
+ "FLUSHALL can't be sent as a command: it reaches beyond the database's own keys",
858
+ );
859
+ const err = await DB.command('FLUSHALL')
860
+ .invoke()
861
+ .value()
862
+ .catch((err: unknown) => err);
863
+ expect(err).toBeInstanceOf(TypeError);
864
+ expect((err as Error).message).toContain('reaches beyond');
865
+ });
866
+ });
@@ -79,6 +79,34 @@ export interface StreamAutoClaim {
79
79
  deleted: string[];
80
80
  }
81
81
 
82
+ /**
83
+ * What a blocking pop of one element replies: the key it came from, without
84
+ * the database's prefix, and the element.
85
+ */
86
+ export interface Popped {
87
+ key: string;
88
+ value: Reply;
89
+ }
90
+
91
+ /** What a blocking pop of several elements replies, in the order popped. */
92
+ export interface PoppedMany {
93
+ key: string;
94
+ values: Reply[];
95
+ }
96
+
97
+ /** What a blocking pop of a sorted set's member replies. */
98
+ export interface PoppedScored {
99
+ key: string;
100
+ member: Reply;
101
+ score: number;
102
+ }
103
+
104
+ /** What a blocking pop of several of a sorted set's members replies. */
105
+ export interface PoppedScoredMany {
106
+ key: string;
107
+ members: {member: Reply; score: number}[];
108
+ }
109
+
82
110
  /** A position on Earth. */
83
111
  export interface GeoPosition {
84
112
  longitude: number;
@@ -126,7 +154,7 @@ export function textOf(v: Value): string {
126
154
  throw typeErr(v, 'text');
127
155
  }
128
156
 
129
- function numberOf(v: Value): number {
157
+ export function numberOf(v: Value): number {
130
158
  v = ok(v);
131
159
  if (typeof v === 'number') return v;
132
160
  if (typeof v === 'bigint') return Number(v);
@@ -698,8 +726,55 @@ export const as = {
698
726
  bool: (v: Value): boolean => boolOf(ok(v) === null ? nilErr() : v),
699
727
  text: (v: Value): string => textOf(ok(v) === null ? nilErr() : v),
700
728
  bools: (v: Value): boolean[] => arrayOf(v).map(boolOf),
729
+ popped: (v: Value): Popped | null => {
730
+ const parts = popParts(v, 2);
731
+ return parts && {key: textOf(parts[0]), value: new Reply(parts[1])};
732
+ },
733
+ poppedMany: (v: Value): PoppedMany | null => {
734
+ const parts = popParts(v, 2);
735
+ return (
736
+ parts && {
737
+ key: textOf(parts[0]),
738
+ values: arrayOf(parts[1]).map(value => new Reply(value)),
739
+ }
740
+ );
741
+ },
742
+ poppedScored: (v: Value): PoppedScored | null => {
743
+ const parts = popParts(v, 3);
744
+ return (
745
+ parts && {
746
+ key: textOf(parts[0]),
747
+ member: new Reply(parts[1]),
748
+ score: numberOf(parts[2]),
749
+ }
750
+ );
751
+ },
752
+ poppedScoredMany: (v: Value): PoppedScoredMany | null => {
753
+ const parts = popParts(v, 2);
754
+ return (
755
+ parts && {
756
+ key: textOf(parts[0]),
757
+ members: arrayOf(parts[1]).map(pair => {
758
+ const [member, score] = arrayOf(pair);
759
+ return {member: new Reply(member), score: numberOf(score)};
760
+ }),
761
+ }
762
+ );
763
+ },
701
764
  };
702
765
 
766
+ /** The `n` parts of a pop's reply, or `null` for the nil of a timeout. */
767
+ function popParts(v: Value, n: number): Value[] | null {
768
+ if (ok(v) === null) return null;
769
+ const parts = arrayOf(v);
770
+ if (parts.length !== n) {
771
+ throw new TypeError(
772
+ `redis: a pop replied ${parts.length} values, not ${n}`,
773
+ );
774
+ }
775
+ return parts;
776
+ }
777
+
703
778
  function nilErr(): never {
704
779
  throw new TypeError('redis: the command replied nil');
705
780
  }
@@ -27,6 +27,8 @@ const replies: Record<string, string> = {
27
27
  XREAD:
28
28
  '*1\r\n*2\r\n$6\r\np/jobs\r\n*1\r\n*2\r\n$3\r\n1-0\r\n*2\r\n$1\r\nf\r\n$1\r\nv\r\n',
29
29
  SCAN: '*2\r\n$1\r\n0\r\n*1\r\n$6\r\np/name\r\n',
30
+ BLPOP: '*2\r\n$6\r\np/jobs\r\n$3\r\njob\r\n',
31
+ OBJECT: ':3\r\n',
30
32
  };
31
33
 
32
34
  const bulk = (s: string) => `$${Buffer.byteLength(s)}\r\n${s}\r\n`;
@@ -165,6 +167,11 @@ const probe = `
165
167
  out.error = err instanceof ServerError ? err.code : String(err);
166
168
  }
167
169
  out.streams = await DB.stream.xread(["jobs"], ["0"]).streams();
170
+ const popped = await DB.list.blpop("jobs", 1);
171
+ out.popped = [popped.key, popped.value.text()];
172
+ out.freq = await DB.command("OBJECT").arg("FREQ").key("hot").invoke().number();
173
+ const refused = await DB.command("FLUSHALL").invoke().value().catch((err) => err);
174
+ out.refused = [refused instanceof TypeError, refused.message];
168
175
  out.keys = [];
169
176
  for await (const key of DB.generic.scan()) out.keys.push(key);
170
177
  const tx = DB.transaction();
@@ -210,6 +217,12 @@ describe('the native runtime', () => {
210
217
  hgetall: {f: 'v'},
211
218
  error: 'WRONGTYPE',
212
219
  streams: [{key: 'jobs', entries: [{id: '1-0', fields: {f: 'v'}}]}],
220
+ popped: ['jobs', 'job'],
221
+ freq: 3,
222
+ refused: [
223
+ true,
224
+ "redis: FLUSHALL can't be sent as a command: it reaches beyond the database's own keys",
225
+ ],
213
226
  keys: ['name'],
214
227
  tx: ['OK', 2],
215
228
  message: ['events', 'hello'],
@@ -222,6 +235,8 @@ describe('the native runtime', () => {
222
235
  'HGETALL p/user',
223
236
  'LPUSH p/name x',
224
237
  'XREAD STREAMS p/jobs 0',
238
+ 'BLPOP p/jobs 1',
239
+ 'OBJECT FREQ p/hot',
225
240
  'SCAN 0 MATCH p/*',
226
241
  'MULTI',
227
242
  'SET p/a 1',
@@ -1,14 +1,7 @@
1
1
  import {activeCtxBytes} from '../../internal/otel/index';
2
2
  import type * as runtime from '../../internal/runtime/mod';
3
- import {openDatabase} from './native';
4
- import {
5
- Cmd,
6
- decodeReply,
7
- encodeBatch,
8
- encodeCommand,
9
- stripReplyKeys,
10
- type Value,
11
- } from './wire';
3
+ import {openDatabase, testDatabase} from './native';
4
+ import {Cmd, decodeReply, encodeBatch, encodeCommand, type Value} from './wire';
12
5
 
13
6
  /**
14
7
  * Where a command runs: on a connection from the database's pool, or on one
@@ -37,11 +30,7 @@ export interface Target {
37
30
  /**
38
31
  * The replies of a batch, one per command.
39
32
  */
40
- function replies(
41
- out: Buffer,
42
- cmds: readonly Cmd[],
43
- prefix: () => string | undefined,
44
- ): Value[] {
33
+ function replies(out: Buffer, cmds: readonly Cmd[]): Value[] {
45
34
  const v = decodeReply(out);
46
35
  if (!Array.isArray(v) || v.length !== cmds.length) {
47
36
  // The batch as a whole was rejected, as a transaction is for a command
@@ -51,49 +40,48 @@ function replies(
51
40
  `redis: ${cmds.length} commands got a reply of another shape`,
52
41
  );
53
42
  }
54
- cmds.forEach((cmd, i) => {
55
- if (cmd.replyKeys) stripReplyKeys(v[i], prefix());
56
- });
57
43
  return v;
58
44
  }
59
45
 
60
- /** A database's pool, its native handle made on first use. */
46
+ /** A database's pool, its native handle made on first use: in a test, the
47
+ * test's instance of the database. */
61
48
  export class PoolTarget implements Target {
62
49
  private impl?: runtime.RedisDatabase;
63
- /** `null` once known to have none. */
64
- private keyPrefix?: string | null;
50
+ /** Each native handle's key prefix: `null` once known to have none. */
51
+ private readonly keyPrefixes = new WeakMap<
52
+ runtime.RedisDatabase,
53
+ string | null
54
+ >();
65
55
 
66
56
  constructor(readonly name: string) {}
67
57
 
68
58
  database(): runtime.RedisDatabase {
69
- return (this.impl ??= openDatabase(this.name));
59
+ return testDatabase(this.name) ?? (this.impl ??= openDatabase(this.name));
70
60
  }
71
61
 
72
62
  prefix(): string | undefined {
73
- if (this.keyPrefix === undefined) {
74
- this.keyPrefix = this.database().keyPrefix() ?? null;
63
+ const db = this.database();
64
+ let prefix = this.keyPrefixes.get(db);
65
+ if (prefix === undefined) {
66
+ prefix = db.keyPrefix() ?? null;
67
+ this.keyPrefixes.set(db, prefix);
75
68
  }
76
- return this.keyPrefix ?? undefined;
69
+ return prefix ?? undefined;
77
70
  }
78
71
 
79
72
  async run(cmd: Cmd): Promise<Value> {
80
- const out = await this.database().execute(
81
- encodeCommand(cmd),
82
- activeCtxBytes(),
73
+ const out = await refused(
74
+ this.database().execute(encodeCommand(cmd), activeCtxBytes()),
83
75
  );
84
- const v = decodeReply(out);
85
- if (cmd.replyKeys) stripReplyKeys(v, this.prefix());
86
- return v;
76
+ return decodeReply(out);
87
77
  }
88
78
 
89
79
  async runAll(cmds: readonly Cmd[], atomic: boolean): Promise<Value[]> {
90
80
  if (cmds.length === 0) return [];
91
- const out = await this.database().executeEach(
92
- encodeBatch(cmds),
93
- atomic,
94
- activeCtxBytes(),
81
+ const out = await refused(
82
+ this.database().executeEach(encodeBatch(cmds), atomic, activeCtxBytes()),
95
83
  );
96
- return replies(out, cmds, () => this.prefix());
84
+ return replies(out, cmds);
97
85
  }
98
86
  }
99
87
 
@@ -113,19 +101,34 @@ export class ConnTarget implements Target {
113
101
  }
114
102
 
115
103
  async run(cmd: Cmd): Promise<Value> {
116
- const out = await this.conn.execute(encodeCommand(cmd), activeCtxBytes());
117
- const v = decodeReply(out);
118
- if (cmd.replyKeys) stripReplyKeys(v, this.prefix());
119
- return v;
104
+ const out = await refused(
105
+ this.conn.execute(encodeCommand(cmd), activeCtxBytes()),
106
+ );
107
+ return decodeReply(out);
120
108
  }
121
109
 
122
110
  async runAll(cmds: readonly Cmd[], atomic: boolean): Promise<Value[] | null> {
123
- const out = await this.conn.executeEach(
124
- encodeBatch(cmds),
125
- atomic,
126
- activeCtxBytes(),
111
+ const out = await refused(
112
+ this.conn.executeEach(encodeBatch(cmds), atomic, activeCtxBytes()),
127
113
  );
128
114
  if (out === null) return null;
129
- return replies(out, cmds, () => this.prefix());
115
+ return replies(out, cmds);
116
+ }
117
+ }
118
+
119
+ /**
120
+ * `p`, but a command the runtime refused before sending it rejects with a
121
+ * `TypeError` saying why: one the app named itself that Redis's command table
122
+ * says it may not send as it is, or one that blocks where it would hold up a
123
+ * connection. The runtime reports those as invalid arguments.
124
+ */
125
+ async function refused<T>(p: Promise<T>): Promise<T> {
126
+ try {
127
+ return await p;
128
+ } catch (err) {
129
+ if ((err as {code?: unknown})?.code === 'InvalidArg') {
130
+ throw new TypeError(`redis: ${(err as Error).message}`, {cause: err});
131
+ }
132
+ throw err;
130
133
  }
131
134
  }
@@ -24,10 +24,12 @@ interface Arg {
24
24
  export class Cmd {
25
25
  readonly args: Arg[] = [];
26
26
  /**
27
- * Whether the reply names keys, which then carry the database's prefix: a
28
- * stream read's, which says which stream each entry came from.
27
+ * Whether the app named the command itself, with `command()`: the runtime
28
+ * then checks it against Redis's command table before sending it.
29
29
  */
30
- replyKeys = false;
30
+ raw = false;
31
+ /** Whether it is sent through a handle that can only read. */
32
+ readOnly = false;
31
33
 
32
34
  /** A command Redis calls `name`, traced as `operation`. */
33
35
  constructor(
@@ -86,17 +88,12 @@ export class Cmd {
86
88
  return this;
87
89
  }
88
90
 
89
- /** Marks the reply as naming keys. */
90
- withReplyKeys(): this {
91
- this.replyKeys = true;
92
- return this;
93
- }
94
-
95
91
  /** A copy, to append more arguments to. */
96
92
  clone(): Cmd {
97
93
  const out = new Cmd(this.operation, this.name, this.write);
98
94
  out.args.push(...this.args);
99
- out.replyKeys = this.replyKeys;
95
+ out.raw = this.raw;
96
+ out.readOnly = this.readOnly;
100
97
  return out;
101
98
  }
102
99
  }
@@ -179,7 +176,7 @@ function commandSize(cmd: Cmd): number {
179
176
  }
180
177
 
181
178
  function writeCommand(w: Writer, cmd: Cmd) {
182
- w.u8(cmd.write ? 1 : 0);
179
+ w.u8((cmd.write ? 1 : 0) | (cmd.raw ? 2 : 0) | (cmd.readOnly ? 4 : 0));
183
180
  w.bytes(Buffer.from(cmd.operation, 'utf8'));
184
181
  w.bytes(Buffer.from(cmd.name, 'utf8'));
185
182
  w.u32(cmd.args.length);
@@ -346,24 +343,3 @@ export function decodeReply(buf: Buffer): Value {
346
343
  if (at !== buf.length) throw malformed();
347
344
  return out;
348
345
  }
349
-
350
- /**
351
- * Removes the database's key prefix from the stream names in a stream read's
352
- * reply, which is a list of (or a map from) stream name to entries.
353
- */
354
- export function stripReplyKeys(v: Value, prefix: string | undefined) {
355
- if (!prefix) return;
356
- const p = Buffer.from(prefix, 'utf8');
357
- const strip = (name: Value): Value =>
358
- Buffer.isBuffer(name) && name.subarray(0, p.length).equals(p)
359
- ? name.subarray(p.length)
360
- : name;
361
- if (Array.isArray(v)) {
362
- for (const stream of v) {
363
- if (Array.isArray(stream) && stream.length > 0)
364
- stream[0] = strip(stream[0]);
365
- }
366
- } else if (v instanceof MapReply) {
367
- for (const pair of v.entries) pair[0] = strip(pair[0]);
368
- }
369
- }
package/testing/bun.ts ADDED
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Per-test infrastructure under `bun test`: `[test] preload =
3
+ * ["@encore.dev/sdk/testing/bun"]` in `bunfig.toml`.
4
+ *
5
+ * @module
6
+ */
7
+ import '../internal/testing/runner';
8
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment -- an error in the build only, without bun's types
9
+ // @ts-ignore: bun's own module, there when bun runs this.
10
+ import {afterAll, afterEach, beforeEach} from 'bun:test';
11
+ import {setup} from './mod';
12
+
13
+ setup({beforeEach, afterEach, afterAll});
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Per-test infrastructure under jest: `setupFilesAfterEnv:
3
+ * ["@encore.dev/sdk/testing/jest"]` in the jest config.
4
+ *
5
+ * @module
6
+ */
7
+ import '../internal/testing/runner';
8
+ import {setup, type Hooks} from './mod';
9
+
10
+ // jest's hooks are globals.
11
+ setup(globalThis as unknown as Hooks);