@objectstack/service-cluster-redis 5.1.1
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/.turbo/turbo-build.log +22 -0
- package/CHANGELOG.md +11 -0
- package/LICENSE +202 -0
- package/dist/index.cjs +460 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +248 -0
- package/dist/index.d.ts +248 -0
- package/dist/index.js +460 -0
- package/dist/index.js.map +1 -0
- package/package.json +45 -0
- package/src/client.ts +75 -0
- package/src/counter.ts +63 -0
- package/src/index.ts +111 -0
- package/src/kv.ts +187 -0
- package/src/lock.ts +205 -0
- package/src/pubsub.ts +174 -0
- package/src/redis.contract.test.ts +159 -0
- package/tsconfig.json +10 -0
- package/tsup.config.ts +14 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/home/runner/work/framework/framework/packages/services/service-cluster-redis/dist/index.cjs","../src/index.ts","../src/client.ts","../src/pubsub.ts","../src/lock.ts","../src/kv.ts","../src/counter.ts"],"names":[],"mappings":"AAAA;ACwBA;AACI;AACA;AAAA,8DAEG;ADvBP;AACA;AEJA,kCAAyC;AAkDlC,SAAS,iBAAA,CAAkB,KAAA,EAA2B,CAAC,CAAA,EAAU;AACpE,EAAA,GAAA,CAAI,IAAA,CAAK,QAAA,EAAU,OAAO,IAAA,CAAK,QAAA;AAE/B,EAAA,MAAM,KAAA,EAAqB;AAAA,IACvB,WAAA,mBAAa,IAAA,CAAK,WAAA,UAAe,MAAA;AAAA,IACjC,oBAAA,EAAsB,CAAA;AAAA,IACtB,oBAAA,EAAsB;AAAA,EAC1B,CAAA;AAEA,EAAA,GAAA,CAAI,IAAA,CAAK,GAAA,EAAK;AACV,IAAA,OAAO,IAAI,mBAAA,CAAM,IAAA,CAAK,GAAA,EAAK,EAAE,GAAG,IAAA,EAAM,GAAG,IAAA,CAAK,QAAQ,CAAC,CAAA;AAAA,EAC3D;AACA,EAAA,OAAO,IAAI,mB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Ec,EAAA;AACH,IAAA;AACX,EAAA;AAEc,EAAA;AACN,IAAA;AACM,MAAA;AACF,MAAA;AACJ,MAAA;AACI,IAAA;AACJ,MAAA;AACJ,IAAA;AACJ,EAAA;AACJ;ALuLY;AACA;AM/VC;AAKT,EAAA;AAFQ,IAAA;AAGC,IAAA;AACA,IAAA;AACT,EAAA;AAEM,EAAA;AACE,IAAA;AACE,IAAA;AACA,IAAA;AACC,IAAA;AACX,EAAA;AAEM,EAAA;AACI,IAAA;AACF,IAAA;AACA,IAAA;AACA,MAAA;AACI,IAAA;AACJ,MAAA;AACJ,IAAA;AACJ,EAAA;AAEM,EAAA;AACE,IAAA;AACA,IAAA;AACM,MAAA;AACN,MAAA;AACJ,IAAA;AACM,IAAA;AACV,EAAA;AAEM,EAAA;AACG,IAAA;AACT,EAAA;AAEe,EAAA;AACJ,IAAA;AACX,EAAA;AACJ;ANyVY;AACA;ACzVH;AACC,EAAA;AACA,EAAA;AACA,EAAA;AAIA,EAAA;AACA,EAAA;AAEA,EAAA;AACF,IAAA;AACQ,IAAA;AACR,IAAA;AACA,IAAA;AACH,EAAA;AACK,EAAA;AACF,IAAA;AACA,IAAA;AACA,IAAA;AACQ,IAAA;AACX,EAAA;AACK,EAAA;AACA,EAAA;AAEA,EAAA;AACK,IAAA;AACP,IAAA;AACA,IAAA;AACA,IAAA;AACA,IAAA;AACA,IAAA;AACJ,EAAA;AAGM,EAAA;AACC,EAAA;AACG,IAAA;AACF,IAAA;AACI,MAAA;AAAE,QAAA;AAAqB,MAAA;AAAwB,MAAA;AACvD,IAAA;AACJ,EAAA;AAEO,EAAA;AACX;AAGA;ADoVY;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA","file":"/home/runner/work/framework/framework/packages/services/service-cluster-redis/dist/index.cjs","sourcesContent":[null,"// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.\n\n/**\n * @objectstack/service-cluster-redis\n *\n * Redis driver for `@objectstack/service-cluster`. Import this package\n * once at process start to register the `'redis'` driver:\n *\n * ```ts\n * import '@objectstack/service-cluster-redis';\n * import { defineCluster } from '@objectstack/service-cluster';\n *\n * const cluster = defineCluster({\n * driver: 'redis',\n * url: 'redis://localhost:6379',\n * nodeId: 'web-1',\n * });\n * ```\n *\n * The driver also exports raw constructors so callers who already own\n * an ioredis client can compose primitives by hand.\n */\n\nimport type { Redis } from 'ioredis';\nimport {\n ComposedClusterService,\n registerClusterDriver,\n type DriverFactoryConfig,\n} from '@objectstack/service-cluster';\nimport { createRedisClient } from './client.js';\nimport { RedisPubSub } from './pubsub.js';\nimport { RedisLock } from './lock.js';\nimport { RedisKV } from './kv.js';\nimport { RedisCounter } from './counter.js';\n\n// Re-exports for advanced composition.\nexport { createRedisClient, duplicateForPubSub, type CreateRedisOptions } from './client.js';\nexport { RedisPubSub, type RedisPubSubOptions } from './pubsub.js';\nexport { RedisLock, type RedisLockOptions } from './lock.js';\nexport { RedisKV, VersionMismatchError, type RedisKVOptions } from './kv.js';\nexport { RedisCounter, type RedisCounterOptions } from './counter.js';\n\n/**\n * Driver-specific options accepted under `driverOptions` in\n * `ClusterCapabilityConfig`. All are optional — supply a `client` to\n * share a Redis pool with other services (e.g. `service-cache`).\n */\nexport interface RedisDriverOptions {\n /** Pre-built ioredis client. When provided, `url` is ignored. */\n client?: Redis;\n /** Key prefix applied to every Redis key (default: 'os:'). */\n keyPrefix?: string;\n /** Default lock TTL in ms. Overridden by `LockAcquireOptions.ttlMs`. */\n lockTtlMs?: number;\n /** Pub/sub subscriber error sink. */\n onError?: (err: unknown, channel: string) => void;\n}\n\n/**\n * Factory consumed by `defineCluster({ driver: 'redis' })`. Builds an\n * `IClusterService` backed by Redis. Owns the underlying ioredis client\n * iff it created it (i.e. when caller didn't pass `driverOptions.client`).\n */\nfunction redisDriverFactory(config: DriverFactoryConfig) {\n const driverOpts = (config.driverOptions ?? {}) as RedisDriverOptions;\n const ownsClient = !driverOpts.client;\n const client =\n driverOpts.client ??\n createRedisClient({ url: config.url });\n\n const keyPrefix = driverOpts.keyPrefix ?? 'os:';\n const ttlMs = config.lockTtlMs ?? driverOpts.lockTtlMs;\n\n const pubsub = new RedisPubSub({\n client,\n nodeId: config.nodeId,\n keyPrefix,\n onError: driverOpts.onError,\n });\n const lock = new RedisLock({\n client,\n keyPrefix,\n defaultTtlMs: ttlMs,\n nodeId: config.nodeId,\n });\n const kv = new RedisKV({ client, keyPrefix });\n const counter = new RedisCounter({ client, keyPrefix });\n\n const facade = new ComposedClusterService(\n config.nodeId,\n 'redis',\n pubsub,\n lock,\n kv,\n counter,\n );\n\n // Wrap close() so we also tear down the publisher client if we own it.\n const originalClose = facade.close.bind(facade);\n facade.close = async () => {\n await originalClose();\n if (ownsClient) {\n try { await client.quit(); } catch { /* swallow */ }\n }\n };\n\n return facade;\n}\n\n// Module-load registration — importing this package is enough.\nregisterClusterDriver('redis', redisDriverFactory);\n","// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.\n\nimport { Redis, type RedisOptions } from 'ioredis';\n\n/**\n * Options accepted by {@link createRedisClient}.\n *\n * Either pass `url` (e.g. `redis://user:pass@host:6379/0`) or a full\n * ioredis `options` bag. The two are merged with `options` taking\n * precedence so callers can override individual fields parsed from the\n * URL.\n *\n * This factory is intended to be the single Redis-client construction\n * point across ObjectStack — `service-cluster-redis` uses it for the\n * driver, and `service-cache` will eventually share the same connection\n * pool via {@link CreateRedisOptions.existing}.\n */\nexport interface CreateRedisOptions {\n /** Connection URL. Ignored when `existing` is provided. */\n url?: string;\n /** Extra ioredis options merged on top of `url`. */\n options?: RedisOptions;\n /**\n * Bring-your-own client. When provided, this exact instance is\n * returned and `url`/`options` are ignored — useful for sharing one\n * pool across cluster + cache.\n */\n existing?: Redis;\n /**\n * If true, the returned client is created in lazyConnect mode so the\n * TCP connection happens on first command rather than at construction.\n * Default: true (matches ObjectStack's \"fail at usage, not boot\" stance).\n */\n lazyConnect?: boolean;\n}\n\n/**\n * Construct (or reuse) an ioredis client with ObjectStack defaults.\n *\n * Defaults applied when `existing` is absent:\n * - `lazyConnect: true` — don't crash boot if Redis is briefly down\n * - `maxRetriesPerRequest: 3` — bound the failure window before surfacing\n * - `enableAutoPipelining: true` — modest throughput boost for batch workloads\n *\n * @example\n * const client = createRedisClient({ url: 'redis://localhost:6379' });\n *\n * @example reuse one pool across services\n * const shared = createRedisClient({ url });\n * const clusterClient = createRedisClient({ existing: shared });\n * const cacheClient = createRedisClient({ existing: shared });\n */\nexport function createRedisClient(opts: CreateRedisOptions = {}): Redis {\n if (opts.existing) return opts.existing;\n\n const base: RedisOptions = {\n lazyConnect: opts.lazyConnect ?? true,\n maxRetriesPerRequest: 3,\n enableAutoPipelining: true,\n };\n\n if (opts.url) {\n return new Redis(opts.url, { ...base, ...opts.options });\n }\n return new Redis({ ...base, ...opts.options });\n}\n\n/**\n * Duplicate a Redis client so that pub/sub commands (which monopolize\n * the connection) don't block regular commands. Mirrors the standard\n * ioredis \"two clients\" pattern.\n */\nexport function duplicateForPubSub(client: Redis): Redis {\n return client.duplicate();\n}\n","// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.\n\nimport type { Redis } from 'ioredis';\nimport type {\n IPubSub,\n PubSubHandler,\n PublishOptions,\n SubscribeOptions,\n Unsubscribe,\n} from '@objectstack/spec/contracts';\nimport { duplicateForPubSub } from './client.js';\n\n/**\n * Wire-format envelope sent over Redis. Adds `fromNode` and\n * `publishedAt` so subscribers see the same surface as the memory\n * driver. The user payload is nested under `p` to avoid colliding with\n * reserved keys.\n */\ninterface RedisPubSubEnvelope {\n n?: string;\n t: number;\n p: unknown;\n}\n\nexport interface RedisPubSubOptions {\n /** Already-connected client used for PUBLISH. */\n client: Redis;\n /** Optional node id surfaced as `fromNode` on every delivered message. */\n nodeId?: string;\n /** Key namespace prefix applied to every channel (default: 'os:'). */\n keyPrefix?: string;\n /** Error sink for subscriber handler exceptions. */\n onError?: (err: unknown, channel: string) => void;\n}\n\n/**\n * Redis pub/sub implementation of {@link IPubSub}.\n *\n * Uses two ioredis clients under the hood:\n * - `publisher` (caller-provided) — runs PUBLISH commands\n * - `subscriber` (auto-duplicated) — held in subscribe mode, can't run\n * regular commands per Redis protocol\n *\n * Delivery semantics match Redis pub/sub: at-most-once, fire-and-forget,\n * no persistence. For at-least-once + replay use the planned `streams`\n * adapter (separate driver).\n *\n * Channel names are prefixed with `keyPrefix` before being sent to\n * Redis, so the same Redis instance can host multiple isolated\n * ObjectStack deployments.\n */\nexport class RedisPubSub implements IPubSub {\n private readonly publisher: Redis;\n private readonly subscriber: Redis;\n private readonly nodeId?: string;\n private readonly keyPrefix: string;\n private readonly onError: (err: unknown, channel: string) => void;\n private readonly subs = new Map<string, Set<PubSubHandler<unknown>>>();\n private closed = false;\n\n constructor(opts: RedisPubSubOptions) {\n this.publisher = opts.client;\n this.subscriber = duplicateForPubSub(opts.client);\n this.nodeId = opts.nodeId;\n this.keyPrefix = opts.keyPrefix ?? 'os:';\n this.onError =\n opts.onError ??\n ((err, channel) => {\n // eslint-disable-next-line no-console\n console.error(`[RedisPubSub] handler error on \"${channel}\":`, err);\n });\n\n this.subscriber.on('message', (raw: string, data: string) => {\n this.dispatch(raw, data);\n });\n }\n\n async publish<T = unknown>(\n channel: string,\n payload: T,\n _opts?: PublishOptions,\n ): Promise<void> {\n if (this.closed) throw new Error('RedisPubSub is closed');\n const envelope: RedisPubSubEnvelope = {\n n: this.nodeId,\n t: Date.now(),\n p: payload,\n };\n await this.publisher.publish(this.prefixed(channel), JSON.stringify(envelope));\n }\n\n subscribe<T = unknown>(\n channel: string,\n handler: PubSubHandler<T>,\n _opts?: SubscribeOptions,\n ): Unsubscribe {\n if (this.closed) throw new Error('RedisPubSub is closed');\n const prefixed = this.prefixed(channel);\n let bucket = this.subs.get(prefixed);\n if (!bucket) {\n bucket = new Set();\n this.subs.set(prefixed, bucket);\n // Fire-and-forget; if subscribe fails the next publish will\n // simply not deliver — caller can resubscribe.\n void this.subscriber.subscribe(prefixed).catch((err) => {\n this.onError(err, channel);\n });\n }\n const wrapped = handler as PubSubHandler<unknown>;\n bucket.add(wrapped);\n\n let disposed = false;\n return () => {\n if (disposed) return;\n disposed = true;\n const b = this.subs.get(prefixed);\n if (!b) return;\n b.delete(wrapped);\n if (b.size === 0) {\n this.subs.delete(prefixed);\n void this.subscriber.unsubscribe(prefixed).catch(() => { /* swallow */ });\n }\n };\n }\n\n async close(): Promise<void> {\n if (this.closed) return;\n this.closed = true;\n this.subs.clear();\n try { await this.subscriber.quit(); } catch { /* swallow */ }\n // We don't quit `publisher` — caller owns it.\n }\n\n private prefixed(channel: string): string {\n return `${this.keyPrefix}ps:${channel}`;\n }\n\n private dispatch(prefixedChannel: string, data: string): void {\n const bucket = this.subs.get(prefixedChannel);\n if (!bucket || bucket.size === 0) return;\n\n let envelope: RedisPubSubEnvelope;\n try {\n envelope = JSON.parse(data) as RedisPubSubEnvelope;\n } catch (err) {\n this.onError(err, prefixedChannel);\n return;\n }\n\n // Strip our keyPrefix so the handler sees the logical channel.\n const logical = prefixedChannel.startsWith(`${this.keyPrefix}ps:`)\n ? prefixedChannel.slice(`${this.keyPrefix}ps:`.length)\n : prefixedChannel;\n\n const snapshot = Array.from(bucket);\n for (const handler of snapshot) {\n try {\n const result = handler({\n channel: logical,\n payload: envelope.p,\n publishedAt: envelope.t,\n fromNode: envelope.n,\n });\n if (result && typeof (result as Promise<void>).then === 'function') {\n (result as Promise<void>).catch((err) =>\n this.onError(err, logical),\n );\n }\n } catch (err) {\n this.onError(err, logical);\n }\n }\n }\n}\n","// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.\n\nimport type { Redis } from 'ioredis';\nimport type {\n ILock,\n LockAcquireOptions,\n LockHandle,\n} from '@objectstack/spec/contracts';\n\nconst DEFAULT_TTL_MS = 15_000;\nconst POLL_INTERVAL_MS = 50;\n\n/**\n * Lua script for safe release: only DEL when the value matches our\n * fencing token. Prevents a delayed release from kicking out the\n * legitimate next holder.\n */\nconst RELEASE_SCRIPT = `\nif redis.call(\"GET\", KEYS[1]) == ARGV[1] then\n return redis.call(\"DEL\", KEYS[1])\nelse\n return 0\nend\n`;\n\n/**\n * Lua script for renew: PEXPIRE only when we still hold the lock.\n * Returns 1 on success, 0 if the lock was lost.\n */\nconst RENEW_SCRIPT = `\nif redis.call(\"GET\", KEYS[1]) == ARGV[1] then\n return redis.call(\"PEXPIRE\", KEYS[1], ARGV[2])\nelse\n return 0\nend\n`;\n\nexport interface RedisLockOptions {\n client: Redis;\n /** Counter client for fencing-token allocation. Same Redis is fine. */\n counterClient?: Redis;\n keyPrefix?: string;\n defaultTtlMs?: number;\n /** Stable node id baked into lock values for debugging. */\n nodeId?: string;\n}\n\n/**\n * Redis-backed distributed lock with TTL fencing.\n *\n * Algorithm (single-instance Redis, NOT Redlock — adequate for\n * typical ObjectStack deployments with one Redis primary):\n *\n * acquire = SET key {nodeId}:{token} NX PX ttl\n * release = Lua: GET == expected ? DEL : 0\n * renew = Lua: GET == expected ? PEXPIRE : 0\n *\n * Fencing tokens come from a Redis counter (INCR on `{prefix}fence:{key}`)\n * so that two clients in a split-brain see strictly increasing tokens\n * downstream.\n *\n * For multi-master Redis (Sentinel failover, etc.) consider switching to\n * a Redlock variant — not implemented yet.\n */\nexport class RedisLock implements ILock {\n private readonly client: Redis;\n private readonly counterClient: Redis;\n private readonly keyPrefix: string;\n private readonly defaultTtlMs: number;\n private readonly nodeId: string;\n private closed = false;\n\n constructor(opts: RedisLockOptions) {\n this.client = opts.client;\n this.counterClient = opts.counterClient ?? opts.client;\n this.keyPrefix = opts.keyPrefix ?? 'os:';\n this.defaultTtlMs = opts.defaultTtlMs ?? DEFAULT_TTL_MS;\n this.nodeId = opts.nodeId ?? 'node';\n }\n\n async acquire(key: string, opts: LockAcquireOptions = {}): Promise<LockHandle | null> {\n if (this.closed) throw new Error('RedisLock is closed');\n const ttlMs = opts.ttlMs ?? this.defaultTtlMs;\n const waitMs = opts.waitMs ?? 0;\n const deadline = Date.now() + waitMs;\n const lockKey = this.lockKey(key);\n\n // Allocate a fresh fencing token up-front.\n const fencingToken = BigInt(\n await this.counterClient.incr(this.fenceKey(key)),\n );\n const value = `${this.nodeId}:${fencingToken}`;\n\n // First attempt — fast path.\n if (await this.trySet(lockKey, value, ttlMs)) {\n return this.makeHandle(key, lockKey, value, fencingToken, ttlMs);\n }\n if (waitMs <= 0) return null;\n\n // Polling loop: simple, correct, good enough for typical\n // contention. Future: pub/sub-driven wakeup on release.\n while (Date.now() < deadline) {\n await sleep(Math.min(POLL_INTERVAL_MS, Math.max(1, deadline - Date.now())));\n if (await this.trySet(lockKey, value, ttlMs)) {\n return this.makeHandle(key, lockKey, value, fencingToken, ttlMs);\n }\n }\n return null;\n }\n\n async withLock<T>(\n key: string,\n fn: (h: LockHandle) => Promise<T>,\n opts?: LockAcquireOptions,\n ): Promise<T | null> {\n const handle = await this.acquire(key, opts);\n if (!handle) return null;\n try {\n return await fn(handle);\n } finally {\n await handle.release();\n }\n }\n\n async close(): Promise<void> {\n this.closed = true;\n }\n\n private async trySet(lockKey: string, value: string, ttlMs: number): Promise<boolean> {\n // SET key value NX PX ttlMs — atomic acquire.\n const res = await this.client.set(lockKey, value, 'PX', ttlMs, 'NX');\n return res === 'OK';\n }\n\n private makeHandle(\n logicalKey: string,\n lockKey: string,\n value: string,\n fencingToken: bigint,\n ttlMs: number,\n ): LockHandle {\n const self = this;\n let released = false;\n let currentTtl = ttlMs;\n // Local expiry timer — mirrors memory driver so `isHeld()` flips\n // on TTL without a Redis roundtrip. Not authoritative (clocks may\n // drift), but matches contract test expectations.\n let timer: NodeJS.Timeout | undefined = setTimeout(() => {\n released = true;\n }, ttlMs);\n\n return {\n key: logicalKey,\n fencingToken,\n isHeld(): boolean {\n return !released;\n },\n async renew(extendMs?: number): Promise<void> {\n if (released) {\n throw new Error(`Lock \"${logicalKey}\" already released`);\n }\n const next = extendMs ?? currentTtl;\n const result = await self.client.eval(\n RENEW_SCRIPT,\n 1,\n lockKey,\n value,\n String(next),\n );\n if (result !== 1) {\n released = true;\n if (timer) { clearTimeout(timer); timer = undefined; }\n throw new Error(\n `Lock \"${logicalKey}\" no longer held (fence=${fencingToken})`,\n );\n }\n currentTtl = next;\n if (timer) clearTimeout(timer);\n timer = setTimeout(() => { released = true; }, next);\n },\n async release(): Promise<void> {\n if (released) return;\n released = true;\n if (timer) { clearTimeout(timer); timer = undefined; }\n try {\n await self.client.eval(RELEASE_SCRIPT, 1, lockKey, value);\n } catch {\n /* swallow — release is best-effort */\n }\n },\n };\n }\n\n private lockKey(key: string): string {\n return `${this.keyPrefix}lock:${key}`;\n }\n\n private fenceKey(key: string): string {\n return `${this.keyPrefix}fence:${key}`;\n }\n}\n\nfunction sleep(ms: number): Promise<void> {\n return new Promise((r) => setTimeout(r, ms));\n}\n","// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.\n\nimport type { Redis } from 'ioredis';\nimport type { IKV, KVEntry, KVSetOptions } from '@objectstack/spec/contracts';\n\n/**\n * Stored payload shape: `{v: <value>, ver: <bigint as string>}`.\n * Versions are stored as strings because Redis values are bytes and\n * bigints can exceed JSON-safe-integer range.\n */\ninterface StoredKV {\n v: unknown;\n ver: string;\n}\n\nexport class VersionMismatchError extends Error {\n constructor(\n public readonly key: string,\n public readonly expected: bigint,\n public readonly actual: bigint,\n ) {\n super(\n `KV version mismatch on \"${key}\": expected v${expected}, found v${actual}`,\n );\n this.name = 'VersionMismatchError';\n }\n}\n\nexport interface RedisKVOptions {\n client: Redis;\n keyPrefix?: string;\n}\n\n/**\n * Redis-backed coordination KV with optimistic concurrency via WATCH/MULTI.\n *\n * Each `set()` performs:\n * 1. WATCH key\n * 2. GET key → read current version\n * 3. compare to ifVersion (if provided)\n * 4. MULTI / SET / [PEXPIRE] / EXEC\n *\n * If a competing writer modifies the key between WATCH and EXEC the\n * transaction aborts and we throw `VersionMismatchError`, matching\n * memory driver semantics.\n *\n * **Not** a cache — uses small JSON envelopes and per-key versioning.\n * Use service-cache for high-throughput caching.\n */\nexport class RedisKV implements IKV {\n private readonly client: Redis;\n private readonly keyPrefix: string;\n private closed = false;\n\n constructor(opts: RedisKVOptions) {\n this.client = opts.client;\n this.keyPrefix = opts.keyPrefix ?? 'os:';\n }\n\n async get<T = unknown>(key: string): Promise<KVEntry<T> | undefined> {\n const raw = await this.client.get(this.kvKey(key));\n if (raw === null) return undefined;\n const parsed = this.parse(raw);\n if (!parsed) return undefined;\n const pttl = await this.client.pttl(this.kvKey(key));\n const expiresAt = pttl > 0 ? Date.now() + pttl : undefined;\n return {\n key,\n value: parsed.v as T,\n version: BigInt(parsed.ver),\n expiresAt,\n };\n }\n\n async set<T = unknown>(\n key: string,\n value: T,\n opts: KVSetOptions = {},\n ): Promise<KVEntry<T>> {\n if (this.closed) throw new Error('RedisKV is closed');\n const physical = this.kvKey(key);\n const ttlMs = opts.ttl && opts.ttl > 0 ? opts.ttl * 1000 : undefined;\n\n // Optimistic-concurrency loop — Redis WATCH aborts the MULTI on\n // any intervening write.\n // eslint-disable-next-line no-constant-condition\n while (true) {\n await this.client.watch(physical);\n const raw = await this.client.get(physical);\n const existing = raw ? this.parse(raw) : undefined;\n const existingVersion = existing ? BigInt(existing.ver) : 0n;\n\n if (opts.ifVersion !== undefined && opts.ifVersion !== existingVersion) {\n await this.client.unwatch();\n throw new VersionMismatchError(key, opts.ifVersion, existingVersion);\n }\n\n const newVersion = existingVersion + 1n;\n const payload: StoredKV = { v: value, ver: newVersion.toString() };\n const encoded = JSON.stringify(payload);\n\n const multi = this.client.multi();\n if (ttlMs) {\n multi.set(physical, encoded, 'PX', ttlMs);\n } else {\n multi.set(physical, encoded);\n }\n const result = await multi.exec();\n // exec() returns null when WATCH detected a concurrent change.\n if (result === null) continue;\n\n return {\n key,\n value,\n version: newVersion,\n expiresAt: ttlMs ? Date.now() + ttlMs : undefined,\n };\n }\n }\n\n async delete(key: string, opts: { ifVersion?: bigint } = {}): Promise<boolean> {\n if (this.closed) throw new Error('RedisKV is closed');\n const physical = this.kvKey(key);\n\n if (opts.ifVersion === undefined) {\n const removed = await this.client.del(physical);\n return removed > 0;\n }\n\n // eslint-disable-next-line no-constant-condition\n while (true) {\n await this.client.watch(physical);\n const raw = await this.client.get(physical);\n if (!raw) {\n await this.client.unwatch();\n return false;\n }\n const parsed = this.parse(raw);\n if (!parsed) {\n await this.client.unwatch();\n return false;\n }\n const currentVersion = BigInt(parsed.ver);\n if (opts.ifVersion !== currentVersion) {\n await this.client.unwatch();\n throw new VersionMismatchError(key, opts.ifVersion, currentVersion);\n }\n const multi = this.client.multi();\n multi.del(physical);\n const result = await multi.exec();\n if (result === null) continue;\n return (result[0]?.[1] as number) > 0;\n }\n }\n\n async cas<T = unknown>(\n key: string,\n expectedVersion: bigint,\n next: T,\n opts: Omit<KVSetOptions, 'ifVersion'> = {},\n ): Promise<KVEntry<T> | undefined> {\n try {\n return await this.set(key, next, { ...opts, ifVersion: expectedVersion });\n } catch (err) {\n if (err instanceof VersionMismatchError) return undefined;\n throw err;\n }\n }\n\n async close(): Promise<void> {\n this.closed = true;\n }\n\n private kvKey(key: string): string {\n return `${this.keyPrefix}kv:${key}`;\n }\n\n private parse(raw: string): StoredKV | undefined {\n try {\n const obj = JSON.parse(raw) as StoredKV;\n if (typeof obj.ver !== 'string') return undefined;\n return obj;\n } catch {\n return undefined;\n }\n }\n}\n","// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.\n\nimport type { Redis } from 'ioredis';\nimport type { ICounter, CounterIncrOptions } from '@objectstack/spec/contracts';\n\nexport interface RedisCounterOptions {\n client: Redis;\n keyPrefix?: string;\n}\n\n/**\n * Redis-backed monotonic counter using INCRBY. Each counter key lives at\n * `{prefix}ctr:{key}` so the same Redis instance can host multiple\n * tenants without collision.\n *\n * Values are stored as ASCII decimals (Redis convention). We surface\n * them as `bigint` to match the contract — INCRBY accepts up to a\n * signed 64-bit range, well beyond Number.MAX_SAFE_INTEGER.\n */\nexport class RedisCounter implements ICounter {\n private readonly client: Redis;\n private readonly keyPrefix: string;\n private closed = false;\n\n constructor(opts: RedisCounterOptions) {\n this.client = opts.client;\n this.keyPrefix = opts.keyPrefix ?? 'os:';\n }\n\n async incr(key: string, opts: CounterIncrOptions = {}): Promise<bigint> {\n if (this.closed) throw new Error('RedisCounter is closed');\n const by = opts.by ?? 1;\n const next = await this.client.incrby(this.ctrKey(key), by);\n return BigInt(next);\n }\n\n async peek(key: string): Promise<bigint> {\n const raw = await this.client.get(this.ctrKey(key));\n if (raw === null) return 0n;\n try {\n return BigInt(raw);\n } catch {\n return 0n;\n }\n }\n\n async reset(key: string, value: bigint = 0n): Promise<void> {\n if (this.closed) throw new Error('RedisCounter is closed');\n if (value === 0n) {\n await this.client.del(this.ctrKey(key));\n return;\n }\n await this.client.set(this.ctrKey(key), value.toString());\n }\n\n async close(): Promise<void> {\n this.closed = true;\n }\n\n private ctrKey(key: string): string {\n return `${this.keyPrefix}ctr:${key}`;\n }\n}\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { RedisOptions, Redis } from 'ioredis';
|
|
2
|
+
import { IPubSub, PublishOptions, PubSubHandler, SubscribeOptions, Unsubscribe, ILock, LockAcquireOptions, LockHandle, IKV, KVEntry, KVSetOptions, ICounter, CounterIncrOptions } from '@objectstack/spec/contracts';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Options accepted by {@link createRedisClient}.
|
|
6
|
+
*
|
|
7
|
+
* Either pass `url` (e.g. `redis://user:pass@host:6379/0`) or a full
|
|
8
|
+
* ioredis `options` bag. The two are merged with `options` taking
|
|
9
|
+
* precedence so callers can override individual fields parsed from the
|
|
10
|
+
* URL.
|
|
11
|
+
*
|
|
12
|
+
* This factory is intended to be the single Redis-client construction
|
|
13
|
+
* point across ObjectStack — `service-cluster-redis` uses it for the
|
|
14
|
+
* driver, and `service-cache` will eventually share the same connection
|
|
15
|
+
* pool via {@link CreateRedisOptions.existing}.
|
|
16
|
+
*/
|
|
17
|
+
interface CreateRedisOptions {
|
|
18
|
+
/** Connection URL. Ignored when `existing` is provided. */
|
|
19
|
+
url?: string;
|
|
20
|
+
/** Extra ioredis options merged on top of `url`. */
|
|
21
|
+
options?: RedisOptions;
|
|
22
|
+
/**
|
|
23
|
+
* Bring-your-own client. When provided, this exact instance is
|
|
24
|
+
* returned and `url`/`options` are ignored — useful for sharing one
|
|
25
|
+
* pool across cluster + cache.
|
|
26
|
+
*/
|
|
27
|
+
existing?: Redis;
|
|
28
|
+
/**
|
|
29
|
+
* If true, the returned client is created in lazyConnect mode so the
|
|
30
|
+
* TCP connection happens on first command rather than at construction.
|
|
31
|
+
* Default: true (matches ObjectStack's "fail at usage, not boot" stance).
|
|
32
|
+
*/
|
|
33
|
+
lazyConnect?: boolean;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Construct (or reuse) an ioredis client with ObjectStack defaults.
|
|
37
|
+
*
|
|
38
|
+
* Defaults applied when `existing` is absent:
|
|
39
|
+
* - `lazyConnect: true` — don't crash boot if Redis is briefly down
|
|
40
|
+
* - `maxRetriesPerRequest: 3` — bound the failure window before surfacing
|
|
41
|
+
* - `enableAutoPipelining: true` — modest throughput boost for batch workloads
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* const client = createRedisClient({ url: 'redis://localhost:6379' });
|
|
45
|
+
*
|
|
46
|
+
* @example reuse one pool across services
|
|
47
|
+
* const shared = createRedisClient({ url });
|
|
48
|
+
* const clusterClient = createRedisClient({ existing: shared });
|
|
49
|
+
* const cacheClient = createRedisClient({ existing: shared });
|
|
50
|
+
*/
|
|
51
|
+
declare function createRedisClient(opts?: CreateRedisOptions): Redis;
|
|
52
|
+
/**
|
|
53
|
+
* Duplicate a Redis client so that pub/sub commands (which monopolize
|
|
54
|
+
* the connection) don't block regular commands. Mirrors the standard
|
|
55
|
+
* ioredis "two clients" pattern.
|
|
56
|
+
*/
|
|
57
|
+
declare function duplicateForPubSub(client: Redis): Redis;
|
|
58
|
+
|
|
59
|
+
interface RedisPubSubOptions {
|
|
60
|
+
/** Already-connected client used for PUBLISH. */
|
|
61
|
+
client: Redis;
|
|
62
|
+
/** Optional node id surfaced as `fromNode` on every delivered message. */
|
|
63
|
+
nodeId?: string;
|
|
64
|
+
/** Key namespace prefix applied to every channel (default: 'os:'). */
|
|
65
|
+
keyPrefix?: string;
|
|
66
|
+
/** Error sink for subscriber handler exceptions. */
|
|
67
|
+
onError?: (err: unknown, channel: string) => void;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Redis pub/sub implementation of {@link IPubSub}.
|
|
71
|
+
*
|
|
72
|
+
* Uses two ioredis clients under the hood:
|
|
73
|
+
* - `publisher` (caller-provided) — runs PUBLISH commands
|
|
74
|
+
* - `subscriber` (auto-duplicated) — held in subscribe mode, can't run
|
|
75
|
+
* regular commands per Redis protocol
|
|
76
|
+
*
|
|
77
|
+
* Delivery semantics match Redis pub/sub: at-most-once, fire-and-forget,
|
|
78
|
+
* no persistence. For at-least-once + replay use the planned `streams`
|
|
79
|
+
* adapter (separate driver).
|
|
80
|
+
*
|
|
81
|
+
* Channel names are prefixed with `keyPrefix` before being sent to
|
|
82
|
+
* Redis, so the same Redis instance can host multiple isolated
|
|
83
|
+
* ObjectStack deployments.
|
|
84
|
+
*/
|
|
85
|
+
declare class RedisPubSub implements IPubSub {
|
|
86
|
+
private readonly publisher;
|
|
87
|
+
private readonly subscriber;
|
|
88
|
+
private readonly nodeId?;
|
|
89
|
+
private readonly keyPrefix;
|
|
90
|
+
private readonly onError;
|
|
91
|
+
private readonly subs;
|
|
92
|
+
private closed;
|
|
93
|
+
constructor(opts: RedisPubSubOptions);
|
|
94
|
+
publish<T = unknown>(channel: string, payload: T, _opts?: PublishOptions): Promise<void>;
|
|
95
|
+
subscribe<T = unknown>(channel: string, handler: PubSubHandler<T>, _opts?: SubscribeOptions): Unsubscribe;
|
|
96
|
+
close(): Promise<void>;
|
|
97
|
+
private prefixed;
|
|
98
|
+
private dispatch;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
interface RedisLockOptions {
|
|
102
|
+
client: Redis;
|
|
103
|
+
/** Counter client for fencing-token allocation. Same Redis is fine. */
|
|
104
|
+
counterClient?: Redis;
|
|
105
|
+
keyPrefix?: string;
|
|
106
|
+
defaultTtlMs?: number;
|
|
107
|
+
/** Stable node id baked into lock values for debugging. */
|
|
108
|
+
nodeId?: string;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Redis-backed distributed lock with TTL fencing.
|
|
112
|
+
*
|
|
113
|
+
* Algorithm (single-instance Redis, NOT Redlock — adequate for
|
|
114
|
+
* typical ObjectStack deployments with one Redis primary):
|
|
115
|
+
*
|
|
116
|
+
* acquire = SET key {nodeId}:{token} NX PX ttl
|
|
117
|
+
* release = Lua: GET == expected ? DEL : 0
|
|
118
|
+
* renew = Lua: GET == expected ? PEXPIRE : 0
|
|
119
|
+
*
|
|
120
|
+
* Fencing tokens come from a Redis counter (INCR on `{prefix}fence:{key}`)
|
|
121
|
+
* so that two clients in a split-brain see strictly increasing tokens
|
|
122
|
+
* downstream.
|
|
123
|
+
*
|
|
124
|
+
* For multi-master Redis (Sentinel failover, etc.) consider switching to
|
|
125
|
+
* a Redlock variant — not implemented yet.
|
|
126
|
+
*/
|
|
127
|
+
declare class RedisLock implements ILock {
|
|
128
|
+
private readonly client;
|
|
129
|
+
private readonly counterClient;
|
|
130
|
+
private readonly keyPrefix;
|
|
131
|
+
private readonly defaultTtlMs;
|
|
132
|
+
private readonly nodeId;
|
|
133
|
+
private closed;
|
|
134
|
+
constructor(opts: RedisLockOptions);
|
|
135
|
+
acquire(key: string, opts?: LockAcquireOptions): Promise<LockHandle | null>;
|
|
136
|
+
withLock<T>(key: string, fn: (h: LockHandle) => Promise<T>, opts?: LockAcquireOptions): Promise<T | null>;
|
|
137
|
+
close(): Promise<void>;
|
|
138
|
+
private trySet;
|
|
139
|
+
private makeHandle;
|
|
140
|
+
private lockKey;
|
|
141
|
+
private fenceKey;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
declare class VersionMismatchError extends Error {
|
|
145
|
+
readonly key: string;
|
|
146
|
+
readonly expected: bigint;
|
|
147
|
+
readonly actual: bigint;
|
|
148
|
+
constructor(key: string, expected: bigint, actual: bigint);
|
|
149
|
+
}
|
|
150
|
+
interface RedisKVOptions {
|
|
151
|
+
client: Redis;
|
|
152
|
+
keyPrefix?: string;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Redis-backed coordination KV with optimistic concurrency via WATCH/MULTI.
|
|
156
|
+
*
|
|
157
|
+
* Each `set()` performs:
|
|
158
|
+
* 1. WATCH key
|
|
159
|
+
* 2. GET key → read current version
|
|
160
|
+
* 3. compare to ifVersion (if provided)
|
|
161
|
+
* 4. MULTI / SET / [PEXPIRE] / EXEC
|
|
162
|
+
*
|
|
163
|
+
* If a competing writer modifies the key between WATCH and EXEC the
|
|
164
|
+
* transaction aborts and we throw `VersionMismatchError`, matching
|
|
165
|
+
* memory driver semantics.
|
|
166
|
+
*
|
|
167
|
+
* **Not** a cache — uses small JSON envelopes and per-key versioning.
|
|
168
|
+
* Use service-cache for high-throughput caching.
|
|
169
|
+
*/
|
|
170
|
+
declare class RedisKV implements IKV {
|
|
171
|
+
private readonly client;
|
|
172
|
+
private readonly keyPrefix;
|
|
173
|
+
private closed;
|
|
174
|
+
constructor(opts: RedisKVOptions);
|
|
175
|
+
get<T = unknown>(key: string): Promise<KVEntry<T> | undefined>;
|
|
176
|
+
set<T = unknown>(key: string, value: T, opts?: KVSetOptions): Promise<KVEntry<T>>;
|
|
177
|
+
delete(key: string, opts?: {
|
|
178
|
+
ifVersion?: bigint;
|
|
179
|
+
}): Promise<boolean>;
|
|
180
|
+
cas<T = unknown>(key: string, expectedVersion: bigint, next: T, opts?: Omit<KVSetOptions, 'ifVersion'>): Promise<KVEntry<T> | undefined>;
|
|
181
|
+
close(): Promise<void>;
|
|
182
|
+
private kvKey;
|
|
183
|
+
private parse;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
interface RedisCounterOptions {
|
|
187
|
+
client: Redis;
|
|
188
|
+
keyPrefix?: string;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Redis-backed monotonic counter using INCRBY. Each counter key lives at
|
|
192
|
+
* `{prefix}ctr:{key}` so the same Redis instance can host multiple
|
|
193
|
+
* tenants without collision.
|
|
194
|
+
*
|
|
195
|
+
* Values are stored as ASCII decimals (Redis convention). We surface
|
|
196
|
+
* them as `bigint` to match the contract — INCRBY accepts up to a
|
|
197
|
+
* signed 64-bit range, well beyond Number.MAX_SAFE_INTEGER.
|
|
198
|
+
*/
|
|
199
|
+
declare class RedisCounter implements ICounter {
|
|
200
|
+
private readonly client;
|
|
201
|
+
private readonly keyPrefix;
|
|
202
|
+
private closed;
|
|
203
|
+
constructor(opts: RedisCounterOptions);
|
|
204
|
+
incr(key: string, opts?: CounterIncrOptions): Promise<bigint>;
|
|
205
|
+
peek(key: string): Promise<bigint>;
|
|
206
|
+
reset(key: string, value?: bigint): Promise<void>;
|
|
207
|
+
close(): Promise<void>;
|
|
208
|
+
private ctrKey;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* @objectstack/service-cluster-redis
|
|
213
|
+
*
|
|
214
|
+
* Redis driver for `@objectstack/service-cluster`. Import this package
|
|
215
|
+
* once at process start to register the `'redis'` driver:
|
|
216
|
+
*
|
|
217
|
+
* ```ts
|
|
218
|
+
* import '@objectstack/service-cluster-redis';
|
|
219
|
+
* import { defineCluster } from '@objectstack/service-cluster';
|
|
220
|
+
*
|
|
221
|
+
* const cluster = defineCluster({
|
|
222
|
+
* driver: 'redis',
|
|
223
|
+
* url: 'redis://localhost:6379',
|
|
224
|
+
* nodeId: 'web-1',
|
|
225
|
+
* });
|
|
226
|
+
* ```
|
|
227
|
+
*
|
|
228
|
+
* The driver also exports raw constructors so callers who already own
|
|
229
|
+
* an ioredis client can compose primitives by hand.
|
|
230
|
+
*/
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Driver-specific options accepted under `driverOptions` in
|
|
234
|
+
* `ClusterCapabilityConfig`. All are optional — supply a `client` to
|
|
235
|
+
* share a Redis pool with other services (e.g. `service-cache`).
|
|
236
|
+
*/
|
|
237
|
+
interface RedisDriverOptions {
|
|
238
|
+
/** Pre-built ioredis client. When provided, `url` is ignored. */
|
|
239
|
+
client?: Redis;
|
|
240
|
+
/** Key prefix applied to every Redis key (default: 'os:'). */
|
|
241
|
+
keyPrefix?: string;
|
|
242
|
+
/** Default lock TTL in ms. Overridden by `LockAcquireOptions.ttlMs`. */
|
|
243
|
+
lockTtlMs?: number;
|
|
244
|
+
/** Pub/sub subscriber error sink. */
|
|
245
|
+
onError?: (err: unknown, channel: string) => void;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export { type CreateRedisOptions, RedisCounter, type RedisCounterOptions, type RedisDriverOptions, RedisKV, type RedisKVOptions, RedisLock, type RedisLockOptions, RedisPubSub, type RedisPubSubOptions, VersionMismatchError, createRedisClient, duplicateForPubSub };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { RedisOptions, Redis } from 'ioredis';
|
|
2
|
+
import { IPubSub, PublishOptions, PubSubHandler, SubscribeOptions, Unsubscribe, ILock, LockAcquireOptions, LockHandle, IKV, KVEntry, KVSetOptions, ICounter, CounterIncrOptions } from '@objectstack/spec/contracts';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Options accepted by {@link createRedisClient}.
|
|
6
|
+
*
|
|
7
|
+
* Either pass `url` (e.g. `redis://user:pass@host:6379/0`) or a full
|
|
8
|
+
* ioredis `options` bag. The two are merged with `options` taking
|
|
9
|
+
* precedence so callers can override individual fields parsed from the
|
|
10
|
+
* URL.
|
|
11
|
+
*
|
|
12
|
+
* This factory is intended to be the single Redis-client construction
|
|
13
|
+
* point across ObjectStack — `service-cluster-redis` uses it for the
|
|
14
|
+
* driver, and `service-cache` will eventually share the same connection
|
|
15
|
+
* pool via {@link CreateRedisOptions.existing}.
|
|
16
|
+
*/
|
|
17
|
+
interface CreateRedisOptions {
|
|
18
|
+
/** Connection URL. Ignored when `existing` is provided. */
|
|
19
|
+
url?: string;
|
|
20
|
+
/** Extra ioredis options merged on top of `url`. */
|
|
21
|
+
options?: RedisOptions;
|
|
22
|
+
/**
|
|
23
|
+
* Bring-your-own client. When provided, this exact instance is
|
|
24
|
+
* returned and `url`/`options` are ignored — useful for sharing one
|
|
25
|
+
* pool across cluster + cache.
|
|
26
|
+
*/
|
|
27
|
+
existing?: Redis;
|
|
28
|
+
/**
|
|
29
|
+
* If true, the returned client is created in lazyConnect mode so the
|
|
30
|
+
* TCP connection happens on first command rather than at construction.
|
|
31
|
+
* Default: true (matches ObjectStack's "fail at usage, not boot" stance).
|
|
32
|
+
*/
|
|
33
|
+
lazyConnect?: boolean;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Construct (or reuse) an ioredis client with ObjectStack defaults.
|
|
37
|
+
*
|
|
38
|
+
* Defaults applied when `existing` is absent:
|
|
39
|
+
* - `lazyConnect: true` — don't crash boot if Redis is briefly down
|
|
40
|
+
* - `maxRetriesPerRequest: 3` — bound the failure window before surfacing
|
|
41
|
+
* - `enableAutoPipelining: true` — modest throughput boost for batch workloads
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* const client = createRedisClient({ url: 'redis://localhost:6379' });
|
|
45
|
+
*
|
|
46
|
+
* @example reuse one pool across services
|
|
47
|
+
* const shared = createRedisClient({ url });
|
|
48
|
+
* const clusterClient = createRedisClient({ existing: shared });
|
|
49
|
+
* const cacheClient = createRedisClient({ existing: shared });
|
|
50
|
+
*/
|
|
51
|
+
declare function createRedisClient(opts?: CreateRedisOptions): Redis;
|
|
52
|
+
/**
|
|
53
|
+
* Duplicate a Redis client so that pub/sub commands (which monopolize
|
|
54
|
+
* the connection) don't block regular commands. Mirrors the standard
|
|
55
|
+
* ioredis "two clients" pattern.
|
|
56
|
+
*/
|
|
57
|
+
declare function duplicateForPubSub(client: Redis): Redis;
|
|
58
|
+
|
|
59
|
+
interface RedisPubSubOptions {
|
|
60
|
+
/** Already-connected client used for PUBLISH. */
|
|
61
|
+
client: Redis;
|
|
62
|
+
/** Optional node id surfaced as `fromNode` on every delivered message. */
|
|
63
|
+
nodeId?: string;
|
|
64
|
+
/** Key namespace prefix applied to every channel (default: 'os:'). */
|
|
65
|
+
keyPrefix?: string;
|
|
66
|
+
/** Error sink for subscriber handler exceptions. */
|
|
67
|
+
onError?: (err: unknown, channel: string) => void;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Redis pub/sub implementation of {@link IPubSub}.
|
|
71
|
+
*
|
|
72
|
+
* Uses two ioredis clients under the hood:
|
|
73
|
+
* - `publisher` (caller-provided) — runs PUBLISH commands
|
|
74
|
+
* - `subscriber` (auto-duplicated) — held in subscribe mode, can't run
|
|
75
|
+
* regular commands per Redis protocol
|
|
76
|
+
*
|
|
77
|
+
* Delivery semantics match Redis pub/sub: at-most-once, fire-and-forget,
|
|
78
|
+
* no persistence. For at-least-once + replay use the planned `streams`
|
|
79
|
+
* adapter (separate driver).
|
|
80
|
+
*
|
|
81
|
+
* Channel names are prefixed with `keyPrefix` before being sent to
|
|
82
|
+
* Redis, so the same Redis instance can host multiple isolated
|
|
83
|
+
* ObjectStack deployments.
|
|
84
|
+
*/
|
|
85
|
+
declare class RedisPubSub implements IPubSub {
|
|
86
|
+
private readonly publisher;
|
|
87
|
+
private readonly subscriber;
|
|
88
|
+
private readonly nodeId?;
|
|
89
|
+
private readonly keyPrefix;
|
|
90
|
+
private readonly onError;
|
|
91
|
+
private readonly subs;
|
|
92
|
+
private closed;
|
|
93
|
+
constructor(opts: RedisPubSubOptions);
|
|
94
|
+
publish<T = unknown>(channel: string, payload: T, _opts?: PublishOptions): Promise<void>;
|
|
95
|
+
subscribe<T = unknown>(channel: string, handler: PubSubHandler<T>, _opts?: SubscribeOptions): Unsubscribe;
|
|
96
|
+
close(): Promise<void>;
|
|
97
|
+
private prefixed;
|
|
98
|
+
private dispatch;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
interface RedisLockOptions {
|
|
102
|
+
client: Redis;
|
|
103
|
+
/** Counter client for fencing-token allocation. Same Redis is fine. */
|
|
104
|
+
counterClient?: Redis;
|
|
105
|
+
keyPrefix?: string;
|
|
106
|
+
defaultTtlMs?: number;
|
|
107
|
+
/** Stable node id baked into lock values for debugging. */
|
|
108
|
+
nodeId?: string;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Redis-backed distributed lock with TTL fencing.
|
|
112
|
+
*
|
|
113
|
+
* Algorithm (single-instance Redis, NOT Redlock — adequate for
|
|
114
|
+
* typical ObjectStack deployments with one Redis primary):
|
|
115
|
+
*
|
|
116
|
+
* acquire = SET key {nodeId}:{token} NX PX ttl
|
|
117
|
+
* release = Lua: GET == expected ? DEL : 0
|
|
118
|
+
* renew = Lua: GET == expected ? PEXPIRE : 0
|
|
119
|
+
*
|
|
120
|
+
* Fencing tokens come from a Redis counter (INCR on `{prefix}fence:{key}`)
|
|
121
|
+
* so that two clients in a split-brain see strictly increasing tokens
|
|
122
|
+
* downstream.
|
|
123
|
+
*
|
|
124
|
+
* For multi-master Redis (Sentinel failover, etc.) consider switching to
|
|
125
|
+
* a Redlock variant — not implemented yet.
|
|
126
|
+
*/
|
|
127
|
+
declare class RedisLock implements ILock {
|
|
128
|
+
private readonly client;
|
|
129
|
+
private readonly counterClient;
|
|
130
|
+
private readonly keyPrefix;
|
|
131
|
+
private readonly defaultTtlMs;
|
|
132
|
+
private readonly nodeId;
|
|
133
|
+
private closed;
|
|
134
|
+
constructor(opts: RedisLockOptions);
|
|
135
|
+
acquire(key: string, opts?: LockAcquireOptions): Promise<LockHandle | null>;
|
|
136
|
+
withLock<T>(key: string, fn: (h: LockHandle) => Promise<T>, opts?: LockAcquireOptions): Promise<T | null>;
|
|
137
|
+
close(): Promise<void>;
|
|
138
|
+
private trySet;
|
|
139
|
+
private makeHandle;
|
|
140
|
+
private lockKey;
|
|
141
|
+
private fenceKey;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
declare class VersionMismatchError extends Error {
|
|
145
|
+
readonly key: string;
|
|
146
|
+
readonly expected: bigint;
|
|
147
|
+
readonly actual: bigint;
|
|
148
|
+
constructor(key: string, expected: bigint, actual: bigint);
|
|
149
|
+
}
|
|
150
|
+
interface RedisKVOptions {
|
|
151
|
+
client: Redis;
|
|
152
|
+
keyPrefix?: string;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Redis-backed coordination KV with optimistic concurrency via WATCH/MULTI.
|
|
156
|
+
*
|
|
157
|
+
* Each `set()` performs:
|
|
158
|
+
* 1. WATCH key
|
|
159
|
+
* 2. GET key → read current version
|
|
160
|
+
* 3. compare to ifVersion (if provided)
|
|
161
|
+
* 4. MULTI / SET / [PEXPIRE] / EXEC
|
|
162
|
+
*
|
|
163
|
+
* If a competing writer modifies the key between WATCH and EXEC the
|
|
164
|
+
* transaction aborts and we throw `VersionMismatchError`, matching
|
|
165
|
+
* memory driver semantics.
|
|
166
|
+
*
|
|
167
|
+
* **Not** a cache — uses small JSON envelopes and per-key versioning.
|
|
168
|
+
* Use service-cache for high-throughput caching.
|
|
169
|
+
*/
|
|
170
|
+
declare class RedisKV implements IKV {
|
|
171
|
+
private readonly client;
|
|
172
|
+
private readonly keyPrefix;
|
|
173
|
+
private closed;
|
|
174
|
+
constructor(opts: RedisKVOptions);
|
|
175
|
+
get<T = unknown>(key: string): Promise<KVEntry<T> | undefined>;
|
|
176
|
+
set<T = unknown>(key: string, value: T, opts?: KVSetOptions): Promise<KVEntry<T>>;
|
|
177
|
+
delete(key: string, opts?: {
|
|
178
|
+
ifVersion?: bigint;
|
|
179
|
+
}): Promise<boolean>;
|
|
180
|
+
cas<T = unknown>(key: string, expectedVersion: bigint, next: T, opts?: Omit<KVSetOptions, 'ifVersion'>): Promise<KVEntry<T> | undefined>;
|
|
181
|
+
close(): Promise<void>;
|
|
182
|
+
private kvKey;
|
|
183
|
+
private parse;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
interface RedisCounterOptions {
|
|
187
|
+
client: Redis;
|
|
188
|
+
keyPrefix?: string;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Redis-backed monotonic counter using INCRBY. Each counter key lives at
|
|
192
|
+
* `{prefix}ctr:{key}` so the same Redis instance can host multiple
|
|
193
|
+
* tenants without collision.
|
|
194
|
+
*
|
|
195
|
+
* Values are stored as ASCII decimals (Redis convention). We surface
|
|
196
|
+
* them as `bigint` to match the contract — INCRBY accepts up to a
|
|
197
|
+
* signed 64-bit range, well beyond Number.MAX_SAFE_INTEGER.
|
|
198
|
+
*/
|
|
199
|
+
declare class RedisCounter implements ICounter {
|
|
200
|
+
private readonly client;
|
|
201
|
+
private readonly keyPrefix;
|
|
202
|
+
private closed;
|
|
203
|
+
constructor(opts: RedisCounterOptions);
|
|
204
|
+
incr(key: string, opts?: CounterIncrOptions): Promise<bigint>;
|
|
205
|
+
peek(key: string): Promise<bigint>;
|
|
206
|
+
reset(key: string, value?: bigint): Promise<void>;
|
|
207
|
+
close(): Promise<void>;
|
|
208
|
+
private ctrKey;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* @objectstack/service-cluster-redis
|
|
213
|
+
*
|
|
214
|
+
* Redis driver for `@objectstack/service-cluster`. Import this package
|
|
215
|
+
* once at process start to register the `'redis'` driver:
|
|
216
|
+
*
|
|
217
|
+
* ```ts
|
|
218
|
+
* import '@objectstack/service-cluster-redis';
|
|
219
|
+
* import { defineCluster } from '@objectstack/service-cluster';
|
|
220
|
+
*
|
|
221
|
+
* const cluster = defineCluster({
|
|
222
|
+
* driver: 'redis',
|
|
223
|
+
* url: 'redis://localhost:6379',
|
|
224
|
+
* nodeId: 'web-1',
|
|
225
|
+
* });
|
|
226
|
+
* ```
|
|
227
|
+
*
|
|
228
|
+
* The driver also exports raw constructors so callers who already own
|
|
229
|
+
* an ioredis client can compose primitives by hand.
|
|
230
|
+
*/
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Driver-specific options accepted under `driverOptions` in
|
|
234
|
+
* `ClusterCapabilityConfig`. All are optional — supply a `client` to
|
|
235
|
+
* share a Redis pool with other services (e.g. `service-cache`).
|
|
236
|
+
*/
|
|
237
|
+
interface RedisDriverOptions {
|
|
238
|
+
/** Pre-built ioredis client. When provided, `url` is ignored. */
|
|
239
|
+
client?: Redis;
|
|
240
|
+
/** Key prefix applied to every Redis key (default: 'os:'). */
|
|
241
|
+
keyPrefix?: string;
|
|
242
|
+
/** Default lock TTL in ms. Overridden by `LockAcquireOptions.ttlMs`. */
|
|
243
|
+
lockTtlMs?: number;
|
|
244
|
+
/** Pub/sub subscriber error sink. */
|
|
245
|
+
onError?: (err: unknown, channel: string) => void;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export { type CreateRedisOptions, RedisCounter, type RedisCounterOptions, type RedisDriverOptions, RedisKV, type RedisKVOptions, RedisLock, type RedisLockOptions, RedisPubSub, type RedisPubSubOptions, VersionMismatchError, createRedisClient, duplicateForPubSub };
|