@crvouga/mockingbird-service-redis 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -39,6 +39,10 @@ one instance. `redis.advance(ms)` moves an injected clock. `redis.inspect()`, `r
39
39
  `ioredis` can connect to the TCP server unchanged. Cluster `MOVED` / `ASK` is not implemented:
40
40
  `CLUSTER` and `READONLY` answer that cluster support is disabled.
41
41
 
42
+ `mockingbird-redis serve --config mockingbird.json` supervises mixed HTTP/Postgres/Redis fleets.
43
+ See [fleet configuration and controls](../../../docs/FLEETS.md) for ephemeral discovery, namespace
44
+ endpoints, readiness, shutdown and coordinated state controls.
45
+
42
46
  ## API
43
47
 
44
48
  - `createRedis(options?)` returns a `Redis` instance.
@@ -54,6 +58,7 @@ one instance. `redis.advance(ms)` moves an injected clock. `redis.inspect()`, `r
54
58
 
55
59
  - Cluster redirects (`MOVED`, `ASK`) and replica replication. `ROLE` stays `master`.
56
60
  - Redis 8 modules, and the full Redis command set.
57
- - Full Lua 5.1, `cmsgpack`, and BullMQ beyond the script subset (`cjson`, `redis.call` / `pcall`).
61
+ - Full Lua 5.1 and arbitrary untested scripts. BullMQ 5.67.1 queue/worker flows, including
62
+ `cmsgpack`, are covered by the public-client suite; see [SUPPORT.md](SUPPORT.md).
58
63
  - `XADD` / `XTRIM` `MAXLEN ~` trims exactly.
59
64
  - Set members are returned in byte order so results stay deterministic.
package/SUPPORT.md CHANGED
@@ -10,14 +10,21 @@ spot checks. The server reports `redis_version` 7.2.4 and `redis_mode:standalone
10
10
  | expiration | `EXPIRE`, `PEXPIRE`, `EXPIREAT`, `PEXPIREAT`, `TTL`, `PTTL`, `PERSIST` | Injected clock. Negative `EXPIRE` deletes an existing key. |
11
11
  | keys | `DEL`, `EXISTS`, `TYPE`, `SCAN`, `KEYS`, `RENAME`, `FLUSHDB`, `FLUSHALL` | `SCAN` cursor is a bulk string. |
12
12
  | hashes | `HSET`, `HGET`, `HMGET`, `HGETALL`, `HDEL`, `HINCRBY` | |
13
- | lists | `LPUSH`, `RPUSH`, `LPOP`, `RPOP`, `LRANGE`, `LMOVE`, `BLPOP`, `BRPOP` | Blocking waits release the command queue. |
13
+ | lists | `LPUSH`, `RPUSH`, `LPOP`, `RPOP`, `LRANGE`, `LPOS`, `LMOVE`, `BLPOP`, `BRPOP` | Blocking waits release the command queue; LPOS supports RANK, COUNT and MAXLEN. |
14
14
  | sets | `SADD`, `SREM`, `SMEMBERS`, `SISMEMBER`, `SPOP` | Members are ordered bytewise. |
15
15
  | sorted sets | `ZADD`, `ZRANGE`, `ZPOPMIN`, `BZPOPMIN`, `ZINCRBY` | Integer scores encode without a trailing `.0`. |
16
16
  | streams | `XADD`, `XREAD`, `XGROUP`, `XREADGROUP`, `XACK`, `XPENDING`, `XCLAIM`, `XAUTOCLAIM` | Consumer-group pending entries and claim. |
17
17
  | transactions | `MULTI`, `EXEC`, `DISCARD`, `WATCH`, `UNWATCH` | A watched key change makes `EXEC` a null array. |
18
18
  | pub/sub | `SUBSCRIBE`, `UNSUBSCRIBE`, `PSUBSCRIBE`, `PUBLISH`, `PUBSUB` | |
19
- | scripts | `EVAL`, `EVALSHA`, `SCRIPT LOAD`, `SCRIPT EXISTS`, `SCRIPT FLUSH` | Lua subset plus `cjson`. No `cmsgpack`. |
19
+ | scripts | `EVAL`, `EVALSHA`, `SCRIPT LOAD`, `SCRIPT EXISTS`, `SCRIPT FLUSH` | Lua subset, `cjson` and `cmsgpack.pack`/`unpack`. |
20
20
  | cluster | `CLUSTER`, `READONLY`, `ASKING` | Single node: cluster support disabled. |
21
21
 
22
- Out of scope for this pass: Redis Cluster, Redis 8 modules, a complete Lua 5.1 runtime, and full
23
- BullMQ compatibility.
22
+ BullMQ 5.67.1 is exercised through unmodified Queue, Worker, QueueEvents and FlowProducer clients:
23
+ completion, delays/backoff, retry and unrecoverable errors, explicit delayed transitions,
24
+ deduplication/unique IDs, auto removal, rate limits, repeatable schedulers, global concurrency, lock ownership/renewal, stalled recovery,
25
+ parent dependencies, and SCRIPT FLUSH/NOSCRIPT reload. Both ioredis 5.9.2 and 5.11.1 are tested.
26
+ See `test/bullmq.sdk.test.ts`. `bun run parity:bullmq` runs the identical public-client contract
27
+ against the mock and an installed local `redis-server` (verified with Redis 8.4.0).
28
+
29
+ Out of scope: Redis Cluster, Redis 8 modules and a complete Lua 5.1 runtime. Compatibility claims
30
+ are limited to the tested BullMQ flows rather than every arbitrary Lua program.
@@ -1,30 +1,52 @@
1
1
  // src/clock.ts
2
2
  function manualClock(start = 0) {
3
- let current = start;
4
- const listeners = /* @__PURE__ */ new Set();
5
- return {
6
- manual: true,
7
- now: () => current,
8
- advance(ms) {
9
- current += ms;
10
- for (const listener of [...listeners]) listener();
11
- },
12
- subscribe(listener) {
13
- listeners.add(listener);
14
- return () => listeners.delete(listener);
15
- }
16
- };
3
+ return controlledClock(start);
17
4
  }
18
5
  function wallClock() {
6
+ return controlledClock();
7
+ }
8
+ function controlledClock(start) {
19
9
  let offset = 0;
10
+ let frozen = start;
20
11
  const listeners = /* @__PURE__ */ new Set();
12
+ const now = () => frozen ?? Date.now() + offset;
13
+ const notify = () => {
14
+ for (const listener of [...listeners]) listener();
15
+ };
21
16
  return {
22
- manual: false,
23
- now: () => Date.now() + offset,
17
+ get manual() {
18
+ return frozen !== void 0;
19
+ },
20
+ now,
21
+ set(ms) {
22
+ if (!Number.isFinite(ms)) throw new Error("invalid clock instant");
23
+ if (frozen !== void 0) frozen = ms;
24
+ else offset = ms - Date.now();
25
+ notify();
26
+ },
24
27
  advance(ms) {
25
- offset += ms;
26
- for (const listener of [...listeners]) listener();
28
+ if (!Number.isFinite(ms)) throw new Error("invalid clock advance");
29
+ if (frozen !== void 0) frozen += ms;
30
+ else offset += ms;
31
+ notify();
32
+ },
33
+ freeze() {
34
+ frozen = now();
35
+ notify();
36
+ },
37
+ unfreeze() {
38
+ if (frozen !== void 0) {
39
+ offset = frozen - Date.now();
40
+ frozen = void 0;
41
+ }
42
+ notify();
43
+ },
44
+ reset() {
45
+ offset = 0;
46
+ frozen = start;
47
+ notify();
27
48
  },
49
+ state: () => ({ now: now(), frozen: frozen !== void 0, offsetMs: offset }),
28
50
  subscribe(listener) {
29
51
  listeners.add(listener);
30
52
  return () => listeners.delete(listener);
@@ -454,6 +476,9 @@ function asCommand(reply) {
454
476
  return { name: latin1(name.v), args };
455
477
  }
456
478
 
479
+ // src/lua.ts
480
+ import { decode, encode } from "@msgpack/msgpack";
481
+
457
482
  // src/sha1.ts
458
483
  var K = [1518500249, 1859775393, 2400959708, 3395469782];
459
484
  function rotl(value, bits) {
@@ -1513,6 +1538,10 @@ function luaGlobals(host) {
1513
1538
  return [fromJson(JSON.parse(text2))];
1514
1539
  });
1515
1540
  env.define("cjson", cjson);
1541
+ const cmsgpack = new LuaTable();
1542
+ cmsgpack.set("unpack", (args) => [fromJson(decode(fromLatin1(asString(args[0] ?? ""))))]);
1543
+ cmsgpack.set("pack", (args) => [latin1(encode(toJson(args[0] ?? null)))]);
1544
+ env.define("cmsgpack", cmsgpack);
1516
1545
  env.define("tonumber", (args) => {
1517
1546
  const value = args[0] ?? null;
1518
1547
  if (typeof value === "number") return [value];
@@ -1657,6 +1686,25 @@ var Database = class {
1657
1686
  now() {
1658
1687
  return this.clock.now();
1659
1688
  }
1689
+ snapshot() {
1690
+ return structuredClone({
1691
+ values: this.values,
1692
+ expires: this.expires,
1693
+ gens: this.gens,
1694
+ lru: this.lru,
1695
+ order: this.order,
1696
+ tick: this.tick
1697
+ });
1698
+ }
1699
+ restore(snapshot) {
1700
+ const point = structuredClone(snapshot);
1701
+ this.values = point.values;
1702
+ this.expires = point.expires;
1703
+ this.gens = point.gens;
1704
+ this.lru = point.lru;
1705
+ this.order = point.order;
1706
+ this.tick = point.tick;
1707
+ }
1660
1708
  bump(key) {
1661
1709
  this.gens.set(key, (this.gens.get(key) ?? 0) + 1);
1662
1710
  }
@@ -2076,6 +2124,7 @@ var Redis = class {
2076
2124
  maxmemory;
2077
2125
  policy;
2078
2126
  rngState;
2127
+ initialRngState;
2079
2128
  faultValue = null;
2080
2129
  tail = Promise.resolve();
2081
2130
  sessions = /* @__PURE__ */ new Set();
@@ -2088,6 +2137,10 @@ var Redis = class {
2088
2137
  scriptDepth = 0;
2089
2138
  commandCount = 0;
2090
2139
  closed = false;
2140
+ controlPaused = false;
2141
+ pendingCommands = 0;
2142
+ pendingLatency = /* @__PURE__ */ new Set();
2143
+ snapshots = /* @__PURE__ */ new WeakMap();
2091
2144
  consumersPaused = false;
2092
2145
  disconnectListeners = /* @__PURE__ */ new Set();
2093
2146
  constructor(options = {}) {
@@ -2099,7 +2152,8 @@ var Redis = class {
2099
2152
  this.authRequired = options.password !== void 0 || this.users.length > 0;
2100
2153
  this.maxmemory = options.maxmemory ?? 0;
2101
2154
  this.policy = options.maxmemoryPolicy ?? "noeviction";
2102
- this.rngState = options.seed ?? 1;
2155
+ this.initialRngState = options.seed ?? 1;
2156
+ this.rngState = this.initialRngState;
2103
2157
  }
2104
2158
  client() {
2105
2159
  return new RedisClient(this, this.createSession());
@@ -2121,6 +2175,70 @@ var Redis = class {
2121
2175
  waiterCount() {
2122
2176
  return this.waiters.length;
2123
2177
  }
2178
+ connections() {
2179
+ return this.sessions.size;
2180
+ }
2181
+ pendingJobs() {
2182
+ let count = 0;
2183
+ for (const db of this.dbs)
2184
+ for (const key of db.keys()) {
2185
+ if (!/:(?:wait|paused|delayed|active|prioritized)$/.test(key)) continue;
2186
+ const value = db.get(key);
2187
+ if (value?.kind === "list") count += value.items.length;
2188
+ if (value?.kind === "zset") count += value.members.size;
2189
+ }
2190
+ return count;
2191
+ }
2192
+ activeCommands() {
2193
+ return Math.max(0, this.pendingCommands - this.waiters.length) + [...this.sessions].filter((session) => session.multi !== null || session.watching.length > 0).length;
2194
+ }
2195
+ activeJobs() {
2196
+ let count = 0;
2197
+ for (const db of this.dbs)
2198
+ for (const key of db.keys()) {
2199
+ if (!key.endsWith(":active")) continue;
2200
+ const value = db.get(key);
2201
+ if (value?.kind === "list") count += value.items.length;
2202
+ }
2203
+ return count;
2204
+ }
2205
+ fence() {
2206
+ if (this.controlPaused) throw new Error("Redis control in progress");
2207
+ this.controlPaused = true;
2208
+ return () => {
2209
+ this.controlPaused = false;
2210
+ };
2211
+ }
2212
+ snapshot() {
2213
+ if (this.pendingCommands > 0) throw new Error("Redis is not quiescent");
2214
+ const handle = {};
2215
+ this.snapshots.set(handle, {
2216
+ dbs: this.dbs.map((db) => db.snapshot()),
2217
+ scripts: new Map(this.scripts),
2218
+ rng: this.rngState,
2219
+ fault: structuredClone(this.faultValue),
2220
+ clock: this.clock.state(),
2221
+ paused: this.consumersPaused
2222
+ });
2223
+ return handle;
2224
+ }
2225
+ restore(handle) {
2226
+ const point = typeof handle === "object" && handle !== null ? this.snapshots.get(handle) : void 0;
2227
+ if (!point || this.pendingCommands > 0) throw new Error("invalid Redis restore");
2228
+ this.dbs.forEach((db, i) => {
2229
+ db.restore(point.dbs[i]);
2230
+ });
2231
+ this.scripts = new Map(point.scripts);
2232
+ this.rngState = point.rng;
2233
+ this.faultValue = structuredClone(point.fault);
2234
+ this.consumersPaused = point.paused;
2235
+ this.clock.freeze();
2236
+ this.clock.set(point.clock.now);
2237
+ if (!point.clock.frozen) this.clock.unfreeze();
2238
+ }
2239
+ disconnect(session) {
2240
+ if (this.sessions.has(session)) this.dropSession(session, "disconnect");
2241
+ }
2124
2242
  inspect(db = 0) {
2125
2243
  const database = this.database(db);
2126
2244
  return database.keys().map((key) => ({
@@ -2130,12 +2248,17 @@ var Redis = class {
2130
2248
  }));
2131
2249
  }
2132
2250
  reset() {
2251
+ this.rngState = this.initialRngState;
2133
2252
  for (const database of this.dbs) database.clear();
2134
2253
  this.scripts.clear();
2254
+ this.faultValue = null;
2255
+ this.clock.reset();
2256
+ this.consumersPaused = false;
2135
2257
  for (const waiter of [...this.waiters]) {
2136
2258
  this.finishWaiter(waiter, nilArray());
2137
2259
  }
2138
2260
  for (const session of this.sessions) {
2261
+ this.leavePubsub(session);
2139
2262
  session.multi = null;
2140
2263
  session.multiFailed = false;
2141
2264
  session.watching = [];
@@ -2143,10 +2266,11 @@ var Redis = class {
2143
2266
  }
2144
2267
  close() {
2145
2268
  this.closed = true;
2269
+ for (const cancel of [...this.pendingLatency]) cancel();
2146
2270
  for (const waiter of [...this.waiters]) {
2147
2271
  this.failWaiter(waiter, new RedisConnectionError("Connection is closed"));
2148
2272
  }
2149
- for (const session of [...this.sessions]) session.dead = true;
2273
+ for (const session of [...this.sessions]) this.disconnect(session);
2150
2274
  }
2151
2275
  onDisconnect(listener) {
2152
2276
  this.disconnectListeners.add(listener);
@@ -2177,9 +2301,11 @@ var Redis = class {
2177
2301
  return session;
2178
2302
  }
2179
2303
  perform(session, name, args) {
2304
+ if (this.controlPaused) return Promise.resolve(err("BUSY fleet control in progress"));
2180
2305
  if (session.dead || this.closed) {
2181
2306
  return Promise.reject(new RedisConnectionError("Connection is closed"));
2182
2307
  }
2308
+ this.pendingCommands++;
2183
2309
  return new Promise((resolve, reject) => {
2184
2310
  this.enqueue(async () => {
2185
2311
  try {
@@ -2208,6 +2334,8 @@ var Redis = class {
2208
2334
  reject(error instanceof Error ? error : new Error(String(error)));
2209
2335
  }
2210
2336
  });
2337
+ }).finally(() => {
2338
+ this.pendingCommands--;
2211
2339
  });
2212
2340
  }
2213
2341
  enqueue(task) {
@@ -2225,10 +2353,18 @@ var Redis = class {
2225
2353
  const finish = () => {
2226
2354
  if (this.clock.now() < target) return;
2227
2355
  unsub();
2356
+ this.pendingLatency.delete(cancel);
2228
2357
  if (timer !== void 0) clearTimeout(timer);
2229
2358
  resolve();
2230
2359
  };
2360
+ const cancel = () => {
2361
+ unsub();
2362
+ if (timer !== void 0) clearTimeout(timer);
2363
+ this.pendingLatency.delete(cancel);
2364
+ resolve();
2365
+ };
2231
2366
  const unsub = this.clock.subscribe(finish);
2367
+ this.pendingLatency.add(cancel);
2232
2368
  if (!this.clock.manual) timer = setTimeout(finish, fault.latencyMs);
2233
2369
  finish();
2234
2370
  });
@@ -2434,6 +2570,7 @@ var Redis = class {
2434
2570
  case "lpop":
2435
2571
  case "rpop":
2436
2572
  case "llen":
2573
+ case "lpos":
2437
2574
  case "lrange":
2438
2575
  case "lindex":
2439
2576
  case "lset":
@@ -3194,6 +3331,46 @@ var Redis = class {
3194
3331
  cmdList(session, command, args, apply) {
3195
3332
  const database = this.database(session.db);
3196
3333
  const key = text(args[0]);
3334
+ if (command === "lpos") {
3335
+ if (args.length < 2) return arityError(command);
3336
+ let rank = 1;
3337
+ let count;
3338
+ let maxlen = 0;
3339
+ for (let i = 2; i < args.length; i += 2) {
3340
+ const option = upper(args[i]);
3341
+ const value = parseI64(args[i + 1] ?? utf8(""));
3342
+ if (value === null) return notInteger();
3343
+ if (option === "RANK") {
3344
+ rank = Number(value);
3345
+ if (rank === 0)
3346
+ return err(
3347
+ "ERR RANK can't be zero: use 1 to start from the first match, 2 from the second ..."
3348
+ );
3349
+ } else if (option === "COUNT") {
3350
+ count = Number(value);
3351
+ if (count < 0) return err("ERR COUNT can't be negative");
3352
+ } else if (option === "MAXLEN") {
3353
+ maxlen = Number(value);
3354
+ if (maxlen < 0) return err("ERR MAXLEN can't be negative");
3355
+ } else return syntaxError();
3356
+ }
3357
+ const list = this.requireList(database, key, false);
3358
+ if (list === null) return count === void 0 ? nil() : arr([]);
3359
+ if (!("items" in list)) return list;
3360
+ const target = args[1] ?? utf8("");
3361
+ const positions = [];
3362
+ let matches = 0;
3363
+ const length = maxlen === 0 ? list.items.length : Math.min(maxlen, list.items.length);
3364
+ for (let i = 0; i < length; i++) {
3365
+ const index = rank < 0 ? list.items.length - i - 1 : i;
3366
+ const item = list.items[index];
3367
+ if (!item || compareBytes(item, target) !== 0) continue;
3368
+ if (++matches < Math.abs(rank)) continue;
3369
+ positions.push(index);
3370
+ if (count === void 0 || count > 0 && positions.length >= count) break;
3371
+ }
3372
+ return count === void 0 ? positions[0] === void 0 ? nil() : int(positions[0]) : arr(positions.map(int));
3373
+ }
3197
3374
  if (command === "lpush" || command === "rpush") {
3198
3375
  if (args.length < 2) return arityError(command);
3199
3376
  const existing = this.requireList(database, key, false);
@@ -4450,6 +4627,10 @@ var Redis = class {
4450
4627
  return ok();
4451
4628
  }
4452
4629
  cmdEval(session, command, args, apply) {
4630
+ if (command === "evalsha" && this.faultValue === "noscript") {
4631
+ this.faultValue = null;
4632
+ return err("NOSCRIPT No matching script. Please use EVAL.");
4633
+ }
4453
4634
  if (args.length < 2) return arityError(command);
4454
4635
  const source = command === "eval" ? text(args[0]) : this.scripts.get(text(args[0]).toLowerCase());
4455
4636
  if (command === "evalsha" && source === void 0) return noScript();
@@ -4462,7 +4643,10 @@ var Redis = class {
4462
4643
  if (!apply || source === void 0) return queued();
4463
4644
  const keys = args.slice(2, 2 + Number(numkeys));
4464
4645
  const argv = args.slice(2 + Number(numkeys));
4465
- return this.runScript(session, source, keys, argv);
4646
+ const result = this.runScript(session, source, keys, argv);
4647
+ if (!(result.t === "error" && result.v.includes("Error compiling script:")))
4648
+ this.scripts.set(sha1Hex(utf8(source)), source);
4649
+ return result;
4466
4650
  }
4467
4651
  cmdScript(args, apply) {
4468
4652
  const sub = upper(args[0]);
@@ -4764,4 +4948,4 @@ export {
4764
4948
  RedisPipeline,
4765
4949
  createRedis
4766
4950
  };
4767
- //# sourceMappingURL=chunk-4HDEOTYJ.js.map
4951
+ //# sourceMappingURL=chunk-IWEHAJ2L.js.map