@crvouga/mockingbird-service-redis

中文文档
2.10.9发布于 2026/11/03MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 1.6万

In-process Redis RESP server for tests: strings, hashes, lists, sets, sorted sets, streams, transactions, pub/sub, and a Lua subset, with an injected clock. Not an HTTP emulator.

原始使用文档
文档目录4 节
  1. Install
  2. Usage
  3. API
  4. Deliberately not modelled

@crvouga/mockingbird-service-redis

Local emulators. Real API contracts. Part of Mockingbird.

In-process Redis for tests. createRedis() is a pure TypeScript RESP store: call commands on redis.client(), or speak RESP to the TCP server (mockingbird-redis, or serve from @crvouga/mockingbird-service-redis/server). It is not an HTTP API emulator and has no createRuntime. ./server also exports serveTarget, so serve --config can boot it: GET /__admin/health reports service: redis, and RESP listens on an ephemeral redis://127.0.0.1:<port> printed at startup. mockingbird-redis still binds 6379 unless --port is set.

Replies follow the Redis command reference for a single standalone node and were spot-checked against Redis 8.4.0. The advertised version is Redis 7.2.4 so clients do not probe Redis 8 modules. This package is a work in progress.

Install

npm install -D @crvouga/mockingbird-service-redis

Usage

import { Redis } from "ioredis"
import { createRedis, manualClock } from "@crvouga/mockingbird-service-redis"
import { serve } from "@crvouga/mockingbird-service-redis/server"

// Test setup: an emulator on a manual clock, listening on a free port. `as never` is needed
// because the two entry points bundle separate declarations of the `Redis` type.
const emulator = createRedis({ clock: manualClock(1_700_000_000_000) })
const server = await serve(emulator as never, { port: 0 })

// Application code: the ioredis client it already uses, connected to the emulator.
const redis = new Redis({ host: server.host, port: server.port })
console.log(await redis.set("lock", "token", "PX", 1000)) // OK

// Test control: expire the lock without sleeping.
emulator.advance(1000)
console.log(await redis.get("lock")) // null

await redis.quit()
await server.close()

Two createRedis() instances never share keys. SELECT isolates logical databases 0–15 inside one instance. emulator.advance(ms) moves an injected clock. emulator.inspect(), emulator.fault(), emulator.pauseConsumers(), emulator.reset(), and emulator.waiterCount() are the test controls.

ioredis connects to the TCP server unchanged. Cluster MOVED / ASK is not implemented: CLUSTER and READONLY answer that cluster support is disabled.

mockingbird-redis serve --config mockingbird.json supervises mixed HTTP/Postgres/Redis fleets. See fleet configuration and controls for ephemeral discovery, namespace endpoints, readiness, shutdown and coordinated state controls.

API

  • createRedis(options?) returns a Redis instance.
  • Redis holds the store, the clock, and the test controls (client, advance, fault, inspect, pauseConsumers, reset, waiterCount, close).
  • RedisClient is one connection: call, raw, pipeline, quit, onPush, and protocol.
  • RedisPipeline queues calls and runs them in exec.
  • RedisConnectionError is a dropped connection, a timeout fault, or a closed server.
  • RedisReplyError is a Redis error reply raised by call.
  • manualClock(start) moves only when advance is called. wallClock follows Date.now().
  • encodeReply(reply, protocol) writes a RESP2 or RESP3 frame.

Deliberately not modelled

  • Cluster redirects (MOVED, ASK) and replica replication. ROLE stays master.
  • Redis 8 modules, and the full Redis command set. ACL SETUSER/GETUSER/DELUSER/CAT/LOG/ GENPASS/DRYRUN/LOAD/SAVE, MEMORY DOCTOR/STATS/MALLOC-STATS, COMMAND GETKEYSANDFLAGS and COMMAND HELP return an explicit "not supported by the Mockingbird Redis emulator" error.
  • Best guess, not oracle-verified: MEMORY USAGE byte counts, OBJECT ENCODING thresholds, the HSCAN/SSCAN/ZSCAN cursor of collections too large for a compact encoding, and a SLOWLOG that never records. MONITOR is supported; see SUPPORT.md.
  • Lua coroutines (coroutine.* exists and raises an explicit "not supported" error), redis.setresp(3) and a script time limit (SCRIPT KILL cannot interrupt a script that never returns). The rest of Lua 5.1 as Redis ships it (standard libraries, cjson, cmsgpack, struct, bit) is implemented and differentially checked against redis-server 8.4.0; see SUPPORT.md. BullMQ 5.76.2 queue/worker flows are covered by the public-client suite.
  • XADD / XTRIM MAXLEN ~ trims exactly.
  • Set members are returned in byte order so results stay deterministic.

版本与发布记录

@crvouga/mockingbird-service-redis 共发布 26 个版本,首次发布于 2026/10/01;过去 12 个月发布了 26 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.10.9latest2026/11/038 天前
2.10.82026/11/038 天前
2.10.72026/11/038 天前
2.10.62026/11/038 天前
2.10.52026/11/038 天前
2.10.42026/11/038 天前

关于 @crvouga/mockingbird-service-redis 的常见问题

@crvouga/mockingbird-service-redis 是什么?

In-process Redis RESP server for tests: strings, hashes, lists, sets, sorted sets, streams, transactions, pub/sub, and a Lua subset, with an injected clock. Not an HTTP emulator.

如何安装 @crvouga/mockingbird-service-redis?

在项目目录运行 npm install @crvouga/mockingbird-service-redis,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 mockingbird-redis,可以用 npx mockingbird-redis 直接运行。

@crvouga/mockingbird-service-redis 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@crvouga/mockingbird-service-redis 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@crvouga/mockingbird-service-redis 可以商用吗?

@crvouga/mockingbird-service-redis 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@crvouga/mockingbird-service-redis 的下载量有多少?

根据 npm 官方下载统计,@crvouga/mockingbird-service-redis 在 2026/10/11 – 2026/11/09 这 30 天内被下载 15,757 次,日均约 525 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@crvouga/mockingbird-service-redis 还在维护吗?

最新版本 2.10.9 发布于 2026/11/03(8 天前);至今共发布 26 个版本,其中过去 12 个月发布了 26 个;首次发布于 2026/10/01。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。