@crvouga/mockingbird-service-redis 2.3.0 → 2.4.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.
@@ -1652,6 +1652,75 @@ function runLua(source, env) {
1652
1652
  return flow.k === "return" ? flow.values : [null];
1653
1653
  }
1654
1654
 
1655
+ // src/portable.ts
1656
+ var encodePortable = (value) => {
1657
+ if (typeof value === "bigint") return { $redis: "bigint", value: String(value) };
1658
+ if (value instanceof Uint8Array) return { $redis: "bytes", value: Array.from(value) };
1659
+ if (value instanceof Map)
1660
+ return {
1661
+ $redis: "map",
1662
+ value: [...value].map(([k, v]) => [encodePortable(k), encodePortable(v)])
1663
+ };
1664
+ if (Array.isArray(value)) return value.map(encodePortable);
1665
+ if (value && typeof value === "object")
1666
+ return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, encodePortable(v)]));
1667
+ return value;
1668
+ };
1669
+ var decodePortable = (value, depth = 0) => {
1670
+ if (depth > 100) throw new Error("Redis archive is too deeply nested");
1671
+ const decode2 = (v) => decodePortable(v, depth + 1);
1672
+ if (Array.isArray(value)) return value.map(decode2);
1673
+ if (value && typeof value === "object") {
1674
+ const record = value;
1675
+ if (record.$redis === "bigint" && typeof record.value === "string" && /^-?\d+$/.test(record.value))
1676
+ return BigInt(record.value);
1677
+ if (record.$redis === "bytes" && Array.isArray(record.value) && record.value.every((v) => Number.isInteger(v) && v >= 0 && v <= 255))
1678
+ return new Uint8Array(record.value);
1679
+ if (record.$redis === "map" && Array.isArray(record.value) && record.value.every((p) => Array.isArray(p) && p.length === 2))
1680
+ return new Map(record.value.map((p) => [decode2(p[0]), decode2(p[1])]));
1681
+ if (record.$redis !== void 0) throw new Error("invalid Redis archive tag");
1682
+ return Object.fromEntries(Object.entries(record).map(([k, v]) => [k, decode2(v)]));
1683
+ }
1684
+ return value;
1685
+ };
1686
+ function assertPortableDatabase(value) {
1687
+ const object = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
1688
+ const stringMap = (v) => v instanceof Map && [...v.keys()].every((k) => typeof k === "string");
1689
+ const bytes = (v) => v instanceof Uint8Array;
1690
+ const byteList = (v) => Array.isArray(v) && v.every(bytes);
1691
+ const numericMap = (v) => stringMap(v) && [...v.values()].every((n) => typeof n === "number" && Number.isFinite(n));
1692
+ if (!object(value) || !stringMap(value.values) || !numericMap(value.expires) || !numericMap(value.gens) || !numericMap(value.lru) || !Array.isArray(value.order) || !value.order.every((k) => typeof k === "string") || !Number.isSafeInteger(value.tick))
1693
+ throw new Error("invalid Redis database archive");
1694
+ for (const row of value.values.values()) {
1695
+ if (!object(row)) throw new Error("invalid Redis archived value");
1696
+ if (row.kind === "string" && bytes(row.bytes) || row.kind === "list" && byteList(row.items))
1697
+ continue;
1698
+ if (row.kind === "set" && stringMap(row.members) && [...row.members.values()].every(bytes))
1699
+ continue;
1700
+ if (row.kind === "hash" && stringMap(row.fields) && [...row.fields.values()].every(bytes))
1701
+ continue;
1702
+ if (row.kind === "zset" && stringMap(row.members) && [...row.members.values()].every(
1703
+ (m) => object(m) && bytes(m.member) && typeof m.score === "number" && !Number.isNaN(m.score)
1704
+ ))
1705
+ continue;
1706
+ if (row.kind === "stream" && object(row.stream)) {
1707
+ const stream = row.stream;
1708
+ if (!Array.isArray(stream.entries) || !stream.entries.every(
1709
+ (e) => object(e) && typeof e.id === "string" && typeof e.ms === "bigint" && typeof e.seq === "bigint" && byteList(e.fields)
1710
+ ) || typeof stream.lastMs !== "bigint" || typeof stream.lastSeq !== "bigint" || !stringMap(stream.groups))
1711
+ throw new Error("invalid Redis archived stream");
1712
+ for (const group of stream.groups.values()) {
1713
+ if (!object(group) || typeof group.name !== "string" || typeof group.lastId !== "string" || !stringMap(group.consumers) || !stringMap(group.pel) || ![...group.consumers.values()].every((c) => object(c) && typeof c.seenAt === "number") || ![...group.pel.values()].every(
1714
+ (p) => object(p) && typeof p.id === "string" && typeof p.consumer === "string" && Number.isSafeInteger(p.deliveries) && typeof p.deliveredAt === "number"
1715
+ ))
1716
+ throw new Error("invalid Redis archived consumer group");
1717
+ }
1718
+ continue;
1719
+ }
1720
+ throw new Error("invalid Redis archived value type");
1721
+ }
1722
+ }
1723
+
1655
1724
  // src/store.ts
1656
1725
  function emptyStream() {
1657
1726
  return { entries: [], groups: /* @__PURE__ */ new Map(), lastMs: -1n, lastSeq: 0n };
@@ -2192,6 +2261,23 @@ var Redis = class {
2192
2261
  activeCommands() {
2193
2262
  return Math.max(0, this.pendingCommands - this.waiters.length) + [...this.sessions].filter((session) => session.multi !== null || session.watching.length > 0).length;
2194
2263
  }
2264
+ nextEvent() {
2265
+ let earliest;
2266
+ const consider = (time) => {
2267
+ if (earliest === void 0 || time < earliest) earliest = time;
2268
+ };
2269
+ for (const db of this.dbs) {
2270
+ const point = db.snapshot();
2271
+ for (const time of point.expires.values()) consider(time);
2272
+ for (const [key, value] of point.values)
2273
+ if (key.endsWith(":delayed") && value.kind === "zset")
2274
+ for (const member of value.members.values()) consider(Math.floor(member.score / 4096));
2275
+ }
2276
+ return earliest;
2277
+ }
2278
+ settleTimers() {
2279
+ for (const db of this.dbs) db.keys();
2280
+ }
2195
2281
  activeJobs() {
2196
2282
  let count = 0;
2197
2283
  for (const db of this.dbs)
@@ -2222,6 +2308,26 @@ var Redis = class {
2222
2308
  });
2223
2309
  return handle;
2224
2310
  }
2311
+ exportSnapshot(handle) {
2312
+ const point = typeof handle === "object" && handle !== null ? this.snapshots.get(handle) : void 0;
2313
+ if (!point) throw new Error("invalid Redis checkpoint");
2314
+ return { format: "mockingbird.redis/v1", data: encodePortable(point) };
2315
+ }
2316
+ importSnapshot(document) {
2317
+ if (!document || typeof document !== "object" || document.format !== "mockingbird.redis/v1")
2318
+ throw new Error("invalid Redis archive");
2319
+ const point = decodePortable(document.data);
2320
+ if (!point || !Array.isArray(point.dbs) || point.dbs.length !== this.dbs.length || !(point.scripts instanceof Map) || !Number.isFinite(point.rng) || typeof point.paused !== "boolean" || !point.clock || !Number.isFinite(point.clock.now) || typeof point.clock.frozen !== "boolean")
2321
+ throw new Error("invalid Redis archive state");
2322
+ for (const db of point.dbs) assertPortableDatabase(db);
2323
+ if (!Number.isSafeInteger(point.rng) || point.rng < 0 || point.rng > 4294967295 || [...point.scripts].some(
2324
+ ([key, script]) => typeof key !== "string" || typeof script !== "string"
2325
+ ))
2326
+ throw new Error("invalid Redis random/script state");
2327
+ const handle = {};
2328
+ this.snapshots.set(handle, point);
2329
+ return handle;
2330
+ }
2225
2331
  restore(handle) {
2226
2332
  const point = typeof handle === "object" && handle !== null ? this.snapshots.get(handle) : void 0;
2227
2333
  if (!point || this.pendingCommands > 0) throw new Error("invalid Redis restore");
@@ -2301,7 +2407,8 @@ var Redis = class {
2301
2407
  return session;
2302
2408
  }
2303
2409
  perform(session, name, args) {
2304
- if (this.controlPaused) return Promise.resolve(err("BUSY fleet control in progress"));
2410
+ if (this.controlPaused && !session.controlPrivileged)
2411
+ return Promise.resolve(err("BUSY fleet control in progress"));
2305
2412
  if (session.dead || this.closed) {
2306
2413
  return Promise.reject(new RedisConnectionError("Connection is closed"));
2307
2414
  }
@@ -4948,4 +5055,4 @@ export {
4948
5055
  RedisPipeline,
4949
5056
  createRedis
4950
5057
  };
4951
- //# sourceMappingURL=chunk-IWEHAJ2L.js.map
5058
+ //# sourceMappingURL=chunk-Z3XQGVTX.js.map