@objectstack/service-cluster-redis 16.1.0 → 17.0.0-rc.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/src/kv.ts DELETED
@@ -1,187 +0,0 @@
1
- // Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2
-
3
- import type { Redis } from 'ioredis';
4
- import type { IKV, KVEntry, KVSetOptions } from '@objectstack/spec/contracts';
5
-
6
- /**
7
- * Stored payload shape: `{v: <value>, ver: <bigint as string>}`.
8
- * Versions are stored as strings because Redis values are bytes and
9
- * bigints can exceed JSON-safe-integer range.
10
- */
11
- interface StoredKV {
12
- v: unknown;
13
- ver: string;
14
- }
15
-
16
- export class VersionMismatchError extends Error {
17
- constructor(
18
- public readonly key: string,
19
- public readonly expected: bigint,
20
- public readonly actual: bigint,
21
- ) {
22
- super(
23
- `KV version mismatch on "${key}": expected v${expected}, found v${actual}`,
24
- );
25
- this.name = 'VersionMismatchError';
26
- }
27
- }
28
-
29
- export interface RedisKVOptions {
30
- client: Redis;
31
- keyPrefix?: string;
32
- }
33
-
34
- /**
35
- * Redis-backed coordination KV with optimistic concurrency via WATCH/MULTI.
36
- *
37
- * Each `set()` performs:
38
- * 1. WATCH key
39
- * 2. GET key → read current version
40
- * 3. compare to ifVersion (if provided)
41
- * 4. MULTI / SET / [PEXPIRE] / EXEC
42
- *
43
- * If a competing writer modifies the key between WATCH and EXEC the
44
- * transaction aborts and we throw `VersionMismatchError`, matching
45
- * memory driver semantics.
46
- *
47
- * **Not** a cache — uses small JSON envelopes and per-key versioning.
48
- * Use service-cache for high-throughput caching.
49
- */
50
- export class RedisKV implements IKV {
51
- private readonly client: Redis;
52
- private readonly keyPrefix: string;
53
- private closed = false;
54
-
55
- constructor(opts: RedisKVOptions) {
56
- this.client = opts.client;
57
- this.keyPrefix = opts.keyPrefix ?? 'os:';
58
- }
59
-
60
- async get<T = unknown>(key: string): Promise<KVEntry<T> | undefined> {
61
- const raw = await this.client.get(this.kvKey(key));
62
- if (raw === null) return undefined;
63
- const parsed = this.parse(raw);
64
- if (!parsed) return undefined;
65
- const pttl = await this.client.pttl(this.kvKey(key));
66
- const expiresAt = pttl > 0 ? Date.now() + pttl : undefined;
67
- return {
68
- key,
69
- value: parsed.v as T,
70
- version: BigInt(parsed.ver),
71
- expiresAt,
72
- };
73
- }
74
-
75
- async set<T = unknown>(
76
- key: string,
77
- value: T,
78
- opts: KVSetOptions = {},
79
- ): Promise<KVEntry<T>> {
80
- if (this.closed) throw new Error('RedisKV is closed');
81
- const physical = this.kvKey(key);
82
- const ttlMs = opts.ttl && opts.ttl > 0 ? opts.ttl * 1000 : undefined;
83
-
84
- // Optimistic-concurrency loop — Redis WATCH aborts the MULTI on
85
- // any intervening write.
86
- // eslint-disable-next-line no-constant-condition
87
- while (true) {
88
- await this.client.watch(physical);
89
- const raw = await this.client.get(physical);
90
- const existing = raw ? this.parse(raw) : undefined;
91
- const existingVersion = existing ? BigInt(existing.ver) : 0n;
92
-
93
- if (opts.ifVersion !== undefined && opts.ifVersion !== existingVersion) {
94
- await this.client.unwatch();
95
- throw new VersionMismatchError(key, opts.ifVersion, existingVersion);
96
- }
97
-
98
- const newVersion = existingVersion + 1n;
99
- const payload: StoredKV = { v: value, ver: newVersion.toString() };
100
- const encoded = JSON.stringify(payload);
101
-
102
- const multi = this.client.multi();
103
- if (ttlMs) {
104
- multi.set(physical, encoded, 'PX', ttlMs);
105
- } else {
106
- multi.set(physical, encoded);
107
- }
108
- const result = await multi.exec();
109
- // exec() returns null when WATCH detected a concurrent change.
110
- if (result === null) continue;
111
-
112
- return {
113
- key,
114
- value,
115
- version: newVersion,
116
- expiresAt: ttlMs ? Date.now() + ttlMs : undefined,
117
- };
118
- }
119
- }
120
-
121
- async delete(key: string, opts: { ifVersion?: bigint } = {}): Promise<boolean> {
122
- if (this.closed) throw new Error('RedisKV is closed');
123
- const physical = this.kvKey(key);
124
-
125
- if (opts.ifVersion === undefined) {
126
- const removed = await this.client.del(physical);
127
- return removed > 0;
128
- }
129
-
130
- // eslint-disable-next-line no-constant-condition
131
- while (true) {
132
- await this.client.watch(physical);
133
- const raw = await this.client.get(physical);
134
- if (!raw) {
135
- await this.client.unwatch();
136
- return false;
137
- }
138
- const parsed = this.parse(raw);
139
- if (!parsed) {
140
- await this.client.unwatch();
141
- return false;
142
- }
143
- const currentVersion = BigInt(parsed.ver);
144
- if (opts.ifVersion !== currentVersion) {
145
- await this.client.unwatch();
146
- throw new VersionMismatchError(key, opts.ifVersion, currentVersion);
147
- }
148
- const multi = this.client.multi();
149
- multi.del(physical);
150
- const result = await multi.exec();
151
- if (result === null) continue;
152
- return (result[0]?.[1] as number) > 0;
153
- }
154
- }
155
-
156
- async cas<T = unknown>(
157
- key: string,
158
- expectedVersion: bigint,
159
- next: T,
160
- opts: Omit<KVSetOptions, 'ifVersion'> = {},
161
- ): Promise<KVEntry<T> | undefined> {
162
- try {
163
- return await this.set(key, next, { ...opts, ifVersion: expectedVersion });
164
- } catch (err) {
165
- if (err instanceof VersionMismatchError) return undefined;
166
- throw err;
167
- }
168
- }
169
-
170
- async close(): Promise<void> {
171
- this.closed = true;
172
- }
173
-
174
- private kvKey(key: string): string {
175
- return `${this.keyPrefix}kv:${key}`;
176
- }
177
-
178
- private parse(raw: string): StoredKV | undefined {
179
- try {
180
- const obj = JSON.parse(raw) as StoredKV;
181
- if (typeof obj.ver !== 'string') return undefined;
182
- return obj;
183
- } catch {
184
- return undefined;
185
- }
186
- }
187
- }
package/src/lock.ts DELETED
@@ -1,205 +0,0 @@
1
- // Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2
-
3
- import type { Redis } from 'ioredis';
4
- import type {
5
- ILock,
6
- LockAcquireOptions,
7
- LockHandle,
8
- } from '@objectstack/spec/contracts';
9
-
10
- const DEFAULT_TTL_MS = 15_000;
11
- const POLL_INTERVAL_MS = 50;
12
-
13
- /**
14
- * Lua script for safe release: only DEL when the value matches our
15
- * fencing token. Prevents a delayed release from kicking out the
16
- * legitimate next holder.
17
- */
18
- const RELEASE_SCRIPT = `
19
- if redis.call("GET", KEYS[1]) == ARGV[1] then
20
- return redis.call("DEL", KEYS[1])
21
- else
22
- return 0
23
- end
24
- `;
25
-
26
- /**
27
- * Lua script for renew: PEXPIRE only when we still hold the lock.
28
- * Returns 1 on success, 0 if the lock was lost.
29
- */
30
- const RENEW_SCRIPT = `
31
- if redis.call("GET", KEYS[1]) == ARGV[1] then
32
- return redis.call("PEXPIRE", KEYS[1], ARGV[2])
33
- else
34
- return 0
35
- end
36
- `;
37
-
38
- export interface RedisLockOptions {
39
- client: Redis;
40
- /** Counter client for fencing-token allocation. Same Redis is fine. */
41
- counterClient?: Redis;
42
- keyPrefix?: string;
43
- defaultTtlMs?: number;
44
- /** Stable node id baked into lock values for debugging. */
45
- nodeId?: string;
46
- }
47
-
48
- /**
49
- * Redis-backed distributed lock with TTL fencing.
50
- *
51
- * Algorithm (single-instance Redis, NOT Redlock — adequate for
52
- * typical ObjectStack deployments with one Redis primary):
53
- *
54
- * acquire = SET key {nodeId}:{token} NX PX ttl
55
- * release = Lua: GET == expected ? DEL : 0
56
- * renew = Lua: GET == expected ? PEXPIRE : 0
57
- *
58
- * Fencing tokens come from a Redis counter (INCR on `{prefix}fence:{key}`)
59
- * so that two clients in a split-brain see strictly increasing tokens
60
- * downstream.
61
- *
62
- * For multi-master Redis (Sentinel failover, etc.) consider switching to
63
- * a Redlock variant — not implemented yet.
64
- */
65
- export class RedisLock implements ILock {
66
- private readonly client: Redis;
67
- private readonly counterClient: Redis;
68
- private readonly keyPrefix: string;
69
- private readonly defaultTtlMs: number;
70
- private readonly nodeId: string;
71
- private closed = false;
72
-
73
- constructor(opts: RedisLockOptions) {
74
- this.client = opts.client;
75
- this.counterClient = opts.counterClient ?? opts.client;
76
- this.keyPrefix = opts.keyPrefix ?? 'os:';
77
- this.defaultTtlMs = opts.defaultTtlMs ?? DEFAULT_TTL_MS;
78
- this.nodeId = opts.nodeId ?? 'node';
79
- }
80
-
81
- async acquire(key: string, opts: LockAcquireOptions = {}): Promise<LockHandle | null> {
82
- if (this.closed) throw new Error('RedisLock is closed');
83
- const ttlMs = opts.ttlMs ?? this.defaultTtlMs;
84
- const waitMs = opts.waitMs ?? 0;
85
- const deadline = Date.now() + waitMs;
86
- const lockKey = this.lockKey(key);
87
-
88
- // Allocate a fresh fencing token up-front.
89
- const fencingToken = BigInt(
90
- await this.counterClient.incr(this.fenceKey(key)),
91
- );
92
- const value = `${this.nodeId}:${fencingToken}`;
93
-
94
- // First attempt — fast path.
95
- if (await this.trySet(lockKey, value, ttlMs)) {
96
- return this.makeHandle(key, lockKey, value, fencingToken, ttlMs);
97
- }
98
- if (waitMs <= 0) return null;
99
-
100
- // Polling loop: simple, correct, good enough for typical
101
- // contention. Future: pub/sub-driven wakeup on release.
102
- while (Date.now() < deadline) {
103
- await sleep(Math.min(POLL_INTERVAL_MS, Math.max(1, deadline - Date.now())));
104
- if (await this.trySet(lockKey, value, ttlMs)) {
105
- return this.makeHandle(key, lockKey, value, fencingToken, ttlMs);
106
- }
107
- }
108
- return null;
109
- }
110
-
111
- async withLock<T>(
112
- key: string,
113
- fn: (h: LockHandle) => Promise<T>,
114
- opts?: LockAcquireOptions,
115
- ): Promise<T | null> {
116
- const handle = await this.acquire(key, opts);
117
- if (!handle) return null;
118
- try {
119
- return await fn(handle);
120
- } finally {
121
- await handle.release();
122
- }
123
- }
124
-
125
- async close(): Promise<void> {
126
- this.closed = true;
127
- }
128
-
129
- private async trySet(lockKey: string, value: string, ttlMs: number): Promise<boolean> {
130
- // SET key value NX PX ttlMs — atomic acquire.
131
- const res = await this.client.set(lockKey, value, 'PX', ttlMs, 'NX');
132
- return res === 'OK';
133
- }
134
-
135
- private makeHandle(
136
- logicalKey: string,
137
- lockKey: string,
138
- value: string,
139
- fencingToken: bigint,
140
- ttlMs: number,
141
- ): LockHandle {
142
- const self = this;
143
- let released = false;
144
- let currentTtl = ttlMs;
145
- // Local expiry timer — mirrors memory driver so `isHeld()` flips
146
- // on TTL without a Redis roundtrip. Not authoritative (clocks may
147
- // drift), but matches contract test expectations.
148
- let timer: NodeJS.Timeout | undefined = setTimeout(() => {
149
- released = true;
150
- }, ttlMs);
151
-
152
- return {
153
- key: logicalKey,
154
- fencingToken,
155
- isHeld(): boolean {
156
- return !released;
157
- },
158
- async renew(extendMs?: number): Promise<void> {
159
- if (released) {
160
- throw new Error(`Lock "${logicalKey}" already released`);
161
- }
162
- const next = extendMs ?? currentTtl;
163
- const result = await self.client.eval(
164
- RENEW_SCRIPT,
165
- 1,
166
- lockKey,
167
- value,
168
- String(next),
169
- );
170
- if (result !== 1) {
171
- released = true;
172
- if (timer) { clearTimeout(timer); timer = undefined; }
173
- throw new Error(
174
- `Lock "${logicalKey}" no longer held (fence=${fencingToken})`,
175
- );
176
- }
177
- currentTtl = next;
178
- if (timer) clearTimeout(timer);
179
- timer = setTimeout(() => { released = true; }, next);
180
- },
181
- async release(): Promise<void> {
182
- if (released) return;
183
- released = true;
184
- if (timer) { clearTimeout(timer); timer = undefined; }
185
- try {
186
- await self.client.eval(RELEASE_SCRIPT, 1, lockKey, value);
187
- } catch {
188
- /* swallow — release is best-effort */
189
- }
190
- },
191
- };
192
- }
193
-
194
- private lockKey(key: string): string {
195
- return `${this.keyPrefix}lock:${key}`;
196
- }
197
-
198
- private fenceKey(key: string): string {
199
- return `${this.keyPrefix}fence:${key}`;
200
- }
201
- }
202
-
203
- function sleep(ms: number): Promise<void> {
204
- return new Promise((r) => setTimeout(r, ms));
205
- }
package/src/pubsub.ts DELETED
@@ -1,188 +0,0 @@
1
- // Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2
-
3
- import type { Redis } from 'ioredis';
4
- import type {
5
- IPubSub,
6
- PubSubHandler,
7
- PublishOptions,
8
- SubscribeOptions,
9
- Unsubscribe,
10
- } from '@objectstack/spec/contracts';
11
- import { duplicateForPubSub } from './client.js';
12
-
13
- /**
14
- * Wire-format envelope sent over Redis. Adds `fromNode` and
15
- * `publishedAt` so subscribers see the same surface as the memory
16
- * driver. The user payload is nested under `p` to avoid colliding with
17
- * reserved keys.
18
- */
19
- interface RedisPubSubEnvelope {
20
- n?: string;
21
- t: number;
22
- p: unknown;
23
- }
24
-
25
- export interface RedisPubSubOptions {
26
- /** Already-connected client used for PUBLISH. */
27
- client: Redis;
28
- /** Optional node id surfaced as `fromNode` on every delivered message. */
29
- nodeId?: string;
30
- /** Key namespace prefix applied to every channel (default: 'os:'). */
31
- keyPrefix?: string;
32
- /** Error sink for subscriber handler exceptions. */
33
- onError?: (err: unknown, channel: string) => void;
34
- }
35
-
36
- /**
37
- * Redis pub/sub implementation of {@link IPubSub}.
38
- *
39
- * Uses two ioredis clients under the hood:
40
- * - `publisher` (caller-provided) — runs PUBLISH commands
41
- * - `subscriber` (auto-duplicated) — held in subscribe mode, can't run
42
- * regular commands per Redis protocol
43
- *
44
- * Delivery semantics match Redis pub/sub: at-most-once, fire-and-forget,
45
- * no persistence. For at-least-once + replay use the planned `streams`
46
- * adapter (separate driver).
47
- *
48
- * Channel names are prefixed with `keyPrefix` before being sent to
49
- * Redis, so the same Redis instance can host multiple isolated
50
- * ObjectStack deployments.
51
- */
52
- export class RedisPubSub implements IPubSub {
53
- private readonly publisher: Redis;
54
- private readonly subscriber: Redis;
55
- private readonly nodeId?: string;
56
- private readonly keyPrefix: string;
57
- private readonly onError: (err: unknown, channel: string) => void;
58
- private readonly subs = new Map<string, Set<PubSubHandler<unknown>>>();
59
- private closed = false;
60
-
61
- constructor(opts: RedisPubSubOptions) {
62
- this.publisher = opts.client;
63
- this.subscriber = duplicateForPubSub(opts.client);
64
- this.nodeId = opts.nodeId;
65
- this.keyPrefix = opts.keyPrefix ?? 'os:';
66
- this.onError =
67
- opts.onError ??
68
- ((err, channel) => {
69
- // eslint-disable-next-line no-console
70
- console.error(`[RedisPubSub] handler error on "${channel}":`, err);
71
- });
72
-
73
- this.subscriber.on('message', (raw: string, data: string) => {
74
- this.dispatch(raw, data);
75
- });
76
- }
77
-
78
- /**
79
- * Durability contract (P1-5): this awaits the Redis `PUBLISH` command (so the
80
- * message left this node), but Redis pub/sub is **at-most-once** — there is no
81
- * delivery guarantee to subscribers and no replay for a node that was down or
82
- * slow at publish time. This is acceptable **only** for events that are pure
83
- * cache-invalidation hints, never the source of truth.
84
- *
85
- * In particular `metadata.changed` is such a hint: the durable record of every
86
- * metadata mutation is the transactional write to `sys_metadata`
87
- * (+ `sys_metadata_history`). A subscriber that misses the event keeps serving
88
- * its cached schema until its next reload and **loses no data** — it self-heals
89
- * on restart / reload against the DB. Do not route any state that must be
90
- * delivered exactly-once through this channel; use a durable outbox instead.
91
- */
92
- async publish<T = unknown>(
93
- channel: string,
94
- payload: T,
95
- _opts?: PublishOptions,
96
- ): Promise<void> {
97
- if (this.closed) throw new Error('RedisPubSub is closed');
98
- const envelope: RedisPubSubEnvelope = {
99
- n: this.nodeId,
100
- t: Date.now(),
101
- p: payload,
102
- };
103
- await this.publisher.publish(this.prefixed(channel), JSON.stringify(envelope));
104
- }
105
-
106
- subscribe<T = unknown>(
107
- channel: string,
108
- handler: PubSubHandler<T>,
109
- _opts?: SubscribeOptions,
110
- ): Unsubscribe {
111
- if (this.closed) throw new Error('RedisPubSub is closed');
112
- const prefixed = this.prefixed(channel);
113
- let bucket = this.subs.get(prefixed);
114
- if (!bucket) {
115
- bucket = new Set();
116
- this.subs.set(prefixed, bucket);
117
- // Fire-and-forget; if subscribe fails the next publish will
118
- // simply not deliver — caller can resubscribe.
119
- void this.subscriber.subscribe(prefixed).catch((err) => {
120
- this.onError(err, channel);
121
- });
122
- }
123
- const wrapped = handler as PubSubHandler<unknown>;
124
- bucket.add(wrapped);
125
-
126
- let disposed = false;
127
- return () => {
128
- if (disposed) return;
129
- disposed = true;
130
- const b = this.subs.get(prefixed);
131
- if (!b) return;
132
- b.delete(wrapped);
133
- if (b.size === 0) {
134
- this.subs.delete(prefixed);
135
- void this.subscriber.unsubscribe(prefixed).catch(() => { /* swallow */ });
136
- }
137
- };
138
- }
139
-
140
- async close(): Promise<void> {
141
- if (this.closed) return;
142
- this.closed = true;
143
- this.subs.clear();
144
- try { await this.subscriber.quit(); } catch { /* swallow */ }
145
- // We don't quit `publisher` — caller owns it.
146
- }
147
-
148
- private prefixed(channel: string): string {
149
- return `${this.keyPrefix}ps:${channel}`;
150
- }
151
-
152
- private dispatch(prefixedChannel: string, data: string): void {
153
- const bucket = this.subs.get(prefixedChannel);
154
- if (!bucket || bucket.size === 0) return;
155
-
156
- let envelope: RedisPubSubEnvelope;
157
- try {
158
- envelope = JSON.parse(data) as RedisPubSubEnvelope;
159
- } catch (err) {
160
- this.onError(err, prefixedChannel);
161
- return;
162
- }
163
-
164
- // Strip our keyPrefix so the handler sees the logical channel.
165
- const logical = prefixedChannel.startsWith(`${this.keyPrefix}ps:`)
166
- ? prefixedChannel.slice(`${this.keyPrefix}ps:`.length)
167
- : prefixedChannel;
168
-
169
- const snapshot = Array.from(bucket);
170
- for (const handler of snapshot) {
171
- try {
172
- const result = handler({
173
- channel: logical,
174
- payload: envelope.p,
175
- publishedAt: envelope.t,
176
- fromNode: envelope.n,
177
- });
178
- if (result && typeof (result as Promise<void>).then === 'function') {
179
- (result as Promise<void>).catch((err) =>
180
- this.onError(err, logical),
181
- );
182
- }
183
- } catch (err) {
184
- this.onError(err, logical);
185
- }
186
- }
187
- }
188
- }