@fieldnotes/sync-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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2026 Irakli Iremashvili
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Irakli Iremashvili
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -66,6 +66,79 @@ new RedisHubBackend(client, { keyPrefix: 'myapp:room:' });
66
66
  Persistence and shared state work with any number of relay instances — every instance reads and writes the
67
67
  same Redis, so room state survives restarts and is visible to all of them.
68
68
 
69
- However, for clients connected to **different** relay instances to see each other's **live** ops, you also
70
- need cross-instance fan-out (Redis pub/sub) — a planned follow-up. A **single** relay instance is fully live
71
- today.
69
+ For clients connected to **different** relay instances to see each other's **live** ops, you also need
70
+ cross-instance fan-out (Redis pub/sub) — available via `RedisHubFanout` (below). A **single** relay instance
71
+ is fully live on its own.
72
+
73
+ ## Cross-instance fan-out (`RedisHubFanout`)
74
+
75
+ `RedisHubFanout` is a `HubFanout` (from `@fieldnotes/sync-server`) over Redis pub/sub: each relay instance
76
+ publishes its live ops to a single channel and every other instance forwards them to its local connections.
77
+ Like the backend, it has **no Redis dependency of its own** — you inject two connections via the
78
+ `RedisPublisher` / `RedisSubscriber` seams.
79
+
80
+ > **The subscriber MUST be a dedicated connection.** Redis forbids any other command on a connection that is
81
+ > in subscribe mode, so publish and subscribe cannot share one connection — that is why `RedisHubFanout`
82
+ > takes two.
83
+
84
+ ### node-redis v4
85
+
86
+ ```ts
87
+ import { createClient } from 'redis';
88
+ import { createSyncServer } from '@fieldnotes/sync-server';
89
+ import { RedisHubBackend, RedisHubFanout } from '@fieldnotes/sync-redis';
90
+
91
+ const hash = createClient({ url: process.env.REDIS_URL });
92
+ await hash.connect(); // HASH backend (persistence)
93
+
94
+ const publisher = createClient({ url: process.env.REDIS_URL });
95
+ await publisher.connect();
96
+ const subscriber = publisher.duplicate();
97
+ await subscriber.connect(); // DEDICATED subscriber — a subscriber connection cannot also publish
98
+
99
+ createSyncServer({
100
+ port: 8080,
101
+ backend: new RedisHubBackend(hash),
102
+ fanout: new RedisHubFanout(publisher, subscriber),
103
+ });
104
+ ```
105
+
106
+ ### ioredis
107
+
108
+ ioredis lowercases `publish`/`subscribe` and delivers messages via a `'message'` event, so wrap each client:
109
+
110
+ ```ts
111
+ import Redis from 'ioredis';
112
+ import { RedisHubFanout } from '@fieldnotes/sync-redis';
113
+
114
+ const pubIo = new Redis(process.env.REDIS_URL); // publish connection
115
+ const subIo = new Redis(process.env.REDIS_URL); // SECOND, dedicated subscriber connection
116
+
117
+ const publisher = { publish: (c: string, m: string) => pubIo.publish(c, m) };
118
+ const subscriber = {
119
+ subscribe: (channel: string, listener: (m: string) => void) => {
120
+ subIo.on('message', (ch, msg) => {
121
+ if (ch === channel) listener(msg);
122
+ });
123
+ return subIo.subscribe(channel);
124
+ },
125
+ };
126
+
127
+ const fanout = new RedisHubFanout(publisher, subscriber);
128
+ ```
129
+
130
+ ### Channel
131
+
132
+ All instances must share one channel (default `fieldnotes:fanout`, configurable):
133
+
134
+ ```ts
135
+ new RedisHubFanout(publisher, subscriber, { channel: 'myapp:fanout' });
136
+ ```
137
+
138
+ ### Precondition: shared fanout AND shared backend
139
+
140
+ Multi-instance **live** sync requires **both** a shared fanout **and** a shared backend (both Redis). The
141
+ fanout forwards live ops; the shared backend keeps snapshots consistent. A shared fanout **without** a shared
142
+ backend leaves a new joiner's snapshot stale — it would catch up from whichever instance it happened to hit,
143
+ missing edits applied elsewhere. Pair `RedisHubFanout` with `RedisHubBackend` for full multi-instance
144
+ real-time sync.
package/dist/index.cjs CHANGED
@@ -20,7 +20,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
- RedisHubBackend: () => RedisHubBackend
23
+ RedisHubBackend: () => RedisHubBackend,
24
+ RedisHubFanout: () => RedisHubFanout
24
25
  });
25
26
  module.exports = __toCommonJS(index_exports);
26
27
 
@@ -58,8 +59,45 @@ var RedisHubBackend = class {
58
59
  else if (op.kind === "clear") await this.client.del(key);
59
60
  }
60
61
  };
62
+
63
+ // src/redis-hub-fanout.ts
64
+ var RedisHubFanout = class {
65
+ publisher;
66
+ subscriber;
67
+ channel;
68
+ onError;
69
+ handlers = /* @__PURE__ */ new Set();
70
+ subscribed = false;
71
+ constructor(publisher, subscriber, options = {}) {
72
+ this.publisher = publisher;
73
+ this.subscriber = subscriber;
74
+ this.channel = options.channel ?? "fieldnotes:fanout";
75
+ this.onError = options.onError ?? (() => void 0);
76
+ }
77
+ publish(payload) {
78
+ Promise.resolve(this.publisher.publish(this.channel, payload)).catch(this.onError);
79
+ }
80
+ subscribe(handler) {
81
+ this.handlers.add(handler);
82
+ if (!this.subscribed) {
83
+ this.subscribed = true;
84
+ Promise.resolve(
85
+ this.subscriber.subscribe(this.channel, (message) => {
86
+ for (const h of this.handlers) {
87
+ try {
88
+ h(message);
89
+ } catch {
90
+ }
91
+ }
92
+ })
93
+ ).catch(this.onError);
94
+ }
95
+ return () => this.handlers.delete(handler);
96
+ }
97
+ };
61
98
  // Annotate the CommonJS export names for ESM import in node:
62
99
  0 && (module.exports = {
63
- RedisHubBackend
100
+ RedisHubBackend,
101
+ RedisHubFanout
64
102
  });
65
103
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/redis-hub-backend.ts"],"sourcesContent":["export { RedisHubBackend } from './redis-hub-backend';\nexport type { RedisHubBackendOptions } from './redis-hub-backend';\nexport type { RedisHashClient } from './redis-hash-client';\n","import { isValidElement, type SyncOp } from '@fieldnotes/sync';\nimport type { CanvasElement } from '@fieldnotes/core';\nimport type { HubBackend } from '@fieldnotes/sync-server';\nimport type { RedisHashClient } from './redis-hash-client';\n\nexport interface RedisHubBackendOptions {\n keyPrefix?: string; // default 'fieldnotes:room:'\n}\n\nexport class RedisHubBackend implements HubBackend {\n private readonly client: RedisHashClient;\n private readonly keyPrefix: string;\n\n constructor(client: RedisHashClient, options: RedisHubBackendOptions = {}) {\n this.client = client;\n this.keyPrefix = options.keyPrefix ?? 'fieldnotes:room:';\n }\n\n private key(room: string): string {\n return `${this.keyPrefix}${room}`;\n }\n\n async snapshot(room: string): Promise<CanvasElement[]> {\n const map = await this.client.hGetAll(this.key(room));\n const out: CanvasElement[] = [];\n for (const value of Object.values(map)) {\n let parsed: unknown;\n try {\n parsed = JSON.parse(value);\n } catch {\n continue; // skip a corrupt stored value rather than throwing the whole snapshot\n }\n if (isValidElement(parsed)) out.push(parsed);\n }\n return out;\n }\n\n async apply(room: string, op: SyncOp): Promise<void> {\n const key = this.key(room);\n if (op.kind === 'upsert')\n await this.client.hSet(key, op.element.id, JSON.stringify(op.element));\n else if (op.kind === 'remove') await this.client.hDel(key, op.id);\n else if (op.kind === 'clear') await this.client.del(key);\n // request-snapshot/snapshot never reach apply (the hub only applies data ops)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,kBAA4C;AASrC,IAAM,kBAAN,MAA4C;AAAA,EAChC;AAAA,EACA;AAAA,EAEjB,YAAY,QAAyB,UAAkC,CAAC,GAAG;AACzE,SAAK,SAAS;AACd,SAAK,YAAY,QAAQ,aAAa;AAAA,EACxC;AAAA,EAEQ,IAAI,MAAsB;AAChC,WAAO,GAAG,KAAK,SAAS,GAAG,IAAI;AAAA,EACjC;AAAA,EAEA,MAAM,SAAS,MAAwC;AACrD,UAAM,MAAM,MAAM,KAAK,OAAO,QAAQ,KAAK,IAAI,IAAI,CAAC;AACpD,UAAM,MAAuB,CAAC;AAC9B,eAAW,SAAS,OAAO,OAAO,GAAG,GAAG;AACtC,UAAI;AACJ,UAAI;AACF,iBAAS,KAAK,MAAM,KAAK;AAAA,MAC3B,QAAQ;AACN;AAAA,MACF;AACA,cAAI,4BAAe,MAAM,EAAG,KAAI,KAAK,MAAM;AAAA,IAC7C;AACA,WAAO;AAAA,EACT;AAAA,EAEA,MAAM,MAAM,MAAc,IAA2B;AACnD,UAAM,MAAM,KAAK,IAAI,IAAI;AACzB,QAAI,GAAG,SAAS;AACd,YAAM,KAAK,OAAO,KAAK,KAAK,GAAG,QAAQ,IAAI,KAAK,UAAU,GAAG,OAAO,CAAC;AAAA,aAC9D,GAAG,SAAS,SAAU,OAAM,KAAK,OAAO,KAAK,KAAK,GAAG,EAAE;AAAA,aACvD,GAAG,SAAS,QAAS,OAAM,KAAK,OAAO,IAAI,GAAG;AAAA,EAEzD;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts","../src/redis-hub-backend.ts","../src/redis-hub-fanout.ts"],"sourcesContent":["export { RedisHubBackend } from './redis-hub-backend';\nexport type { RedisHubBackendOptions } from './redis-hub-backend';\nexport type { RedisHashClient } from './redis-hash-client';\nexport { RedisHubFanout } from './redis-hub-fanout';\nexport type { RedisHubFanoutOptions } from './redis-hub-fanout';\nexport type { RedisPublisher, RedisSubscriber } from './redis-fanout-client';\n","import { isValidElement, type SyncOp } from '@fieldnotes/sync';\r\nimport type { CanvasElement } from '@fieldnotes/core';\r\nimport type { HubBackend } from '@fieldnotes/sync-server';\r\nimport type { RedisHashClient } from './redis-hash-client';\r\n\r\nexport interface RedisHubBackendOptions {\r\n keyPrefix?: string; // default 'fieldnotes:room:'\r\n}\r\n\r\nexport class RedisHubBackend implements HubBackend {\r\n private readonly client: RedisHashClient;\r\n private readonly keyPrefix: string;\r\n\r\n constructor(client: RedisHashClient, options: RedisHubBackendOptions = {}) {\r\n this.client = client;\r\n this.keyPrefix = options.keyPrefix ?? 'fieldnotes:room:';\r\n }\r\n\r\n private key(room: string): string {\r\n return `${this.keyPrefix}${room}`;\r\n }\r\n\r\n async snapshot(room: string): Promise<CanvasElement[]> {\r\n const map = await this.client.hGetAll(this.key(room));\r\n const out: CanvasElement[] = [];\r\n for (const value of Object.values(map)) {\r\n let parsed: unknown;\r\n try {\r\n parsed = JSON.parse(value);\r\n } catch {\r\n continue; // skip a corrupt stored value rather than throwing the whole snapshot\r\n }\r\n if (isValidElement(parsed)) out.push(parsed);\r\n }\r\n return out;\r\n }\r\n\r\n async apply(room: string, op: SyncOp): Promise<void> {\r\n const key = this.key(room);\r\n if (op.kind === 'upsert')\r\n await this.client.hSet(key, op.element.id, JSON.stringify(op.element));\r\n else if (op.kind === 'remove') await this.client.hDel(key, op.id);\r\n else if (op.kind === 'clear') await this.client.del(key);\r\n // request-snapshot/snapshot never reach apply (the hub only applies data ops)\r\n }\r\n}\r\n","import type { HubFanout } from '@fieldnotes/sync-server';\nimport type { RedisPublisher, RedisSubscriber } from './redis-fanout-client';\n\nexport interface RedisHubFanoutOptions {\n channel?: string;\n onError?: (err: unknown) => void;\n}\n\nexport class RedisHubFanout implements HubFanout {\n private readonly publisher: RedisPublisher;\n private readonly subscriber: RedisSubscriber;\n private readonly channel: string;\n private readonly onError: (err: unknown) => void;\n private readonly handlers = new Set<(payload: string) => void>();\n private subscribed = false;\n\n constructor(\n publisher: RedisPublisher,\n subscriber: RedisSubscriber,\n options: RedisHubFanoutOptions = {},\n ) {\n this.publisher = publisher;\n this.subscriber = subscriber;\n this.channel = options.channel ?? 'fieldnotes:fanout';\n this.onError = options.onError ?? (() => undefined);\n }\n\n publish(payload: string): void {\n Promise.resolve(this.publisher.publish(this.channel, payload)).catch(this.onError);\n }\n\n subscribe(handler: (payload: string) => void): () => void {\n this.handlers.add(handler);\n if (!this.subscribed) {\n this.subscribed = true;\n Promise.resolve(\n this.subscriber.subscribe(this.channel, (message) => {\n for (const h of this.handlers) {\n try {\n h(message);\n } catch {\n /* isolate handlers */\n }\n }\n }),\n ).catch(this.onError);\n }\n return () => this.handlers.delete(handler);\n }\n}\n"],"mappings":";;;;;;;;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QAAQ,YAAY,MAAM;AAAA,EAC3C;AAAA,EAEA,QAAQ,SAAuB;AAC7B,YAAQ,QAAQ,KAAK,UAAU,QAAQ,KAAK,SAAS,OAAO,CAAC,EAAE,MAAM,KAAK,OAAO;AAAA,EACnF;AAAA,EAEA,UAAU,SAAgD;AACxD,SAAK,SAAS,IAAI,OAAO;AACzB,QAAI,CAAC,KAAK,YAAY;AACpB,WAAK,aAAa;AAClB,cAAQ;AAAA,QACN,KAAK,WAAW,UAAU,KAAK,SAAS,CAAC,YAAY;AACnD,qBAAW,KAAK,KAAK,UAAU;AAC7B,gBAAI;AACF,gBAAE,OAAO;AAAA,YACX,QAAQ;AAAA,YAER;AAAA,UACF;AAAA,QACF,CAAC;AAAA,MACH,EAAE,MAAM,KAAK,OAAO;AAAA,IACtB;AACA,WAAO,MAAM,KAAK,SAAS,OAAO,OAAO;AAAA,EAC3C;AACF;","names":[]}
package/dist/index.d.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { SyncOp } from '@fieldnotes/sync';
2
2
  import { CanvasElement } from '@fieldnotes/core';
3
- import { HubBackend } from '@fieldnotes/sync-server';
3
+ import { HubBackend, HubFanout } from '@fieldnotes/sync-server';
4
4
 
5
5
  interface RedisHashClient {
6
6
  hGetAll(key: string): Promise<Record<string, string>>;
@@ -21,4 +21,27 @@ declare class RedisHubBackend implements HubBackend {
21
21
  apply(room: string, op: SyncOp): Promise<void>;
22
22
  }
23
23
 
24
- export { type RedisHashClient, RedisHubBackend, type RedisHubBackendOptions };
24
+ interface RedisPublisher {
25
+ publish(channel: string, message: string): Promise<unknown> | unknown;
26
+ }
27
+ interface RedisSubscriber {
28
+ subscribe(channel: string, listener: (message: string) => void): Promise<unknown> | unknown;
29
+ }
30
+
31
+ interface RedisHubFanoutOptions {
32
+ channel?: string;
33
+ onError?: (err: unknown) => void;
34
+ }
35
+ declare class RedisHubFanout implements HubFanout {
36
+ private readonly publisher;
37
+ private readonly subscriber;
38
+ private readonly channel;
39
+ private readonly onError;
40
+ private readonly handlers;
41
+ private subscribed;
42
+ constructor(publisher: RedisPublisher, subscriber: RedisSubscriber, options?: RedisHubFanoutOptions);
43
+ publish(payload: string): void;
44
+ subscribe(handler: (payload: string) => void): () => void;
45
+ }
46
+
47
+ export { type RedisHashClient, RedisHubBackend, type RedisHubBackendOptions, RedisHubFanout, type RedisHubFanoutOptions, type RedisPublisher, type RedisSubscriber };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { SyncOp } from '@fieldnotes/sync';
2
2
  import { CanvasElement } from '@fieldnotes/core';
3
- import { HubBackend } from '@fieldnotes/sync-server';
3
+ import { HubBackend, HubFanout } from '@fieldnotes/sync-server';
4
4
 
5
5
  interface RedisHashClient {
6
6
  hGetAll(key: string): Promise<Record<string, string>>;
@@ -21,4 +21,27 @@ declare class RedisHubBackend implements HubBackend {
21
21
  apply(room: string, op: SyncOp): Promise<void>;
22
22
  }
23
23
 
24
- export { type RedisHashClient, RedisHubBackend, type RedisHubBackendOptions };
24
+ interface RedisPublisher {
25
+ publish(channel: string, message: string): Promise<unknown> | unknown;
26
+ }
27
+ interface RedisSubscriber {
28
+ subscribe(channel: string, listener: (message: string) => void): Promise<unknown> | unknown;
29
+ }
30
+
31
+ interface RedisHubFanoutOptions {
32
+ channel?: string;
33
+ onError?: (err: unknown) => void;
34
+ }
35
+ declare class RedisHubFanout implements HubFanout {
36
+ private readonly publisher;
37
+ private readonly subscriber;
38
+ private readonly channel;
39
+ private readonly onError;
40
+ private readonly handlers;
41
+ private subscribed;
42
+ constructor(publisher: RedisPublisher, subscriber: RedisSubscriber, options?: RedisHubFanoutOptions);
43
+ publish(payload: string): void;
44
+ subscribe(handler: (payload: string) => void): () => void;
45
+ }
46
+
47
+ export { type RedisHashClient, RedisHubBackend, type RedisHubBackendOptions, RedisHubFanout, type RedisHubFanoutOptions, type RedisPublisher, type RedisSubscriber };
package/dist/index.js CHANGED
@@ -32,7 +32,44 @@ var RedisHubBackend = class {
32
32
  else if (op.kind === "clear") await this.client.del(key);
33
33
  }
34
34
  };
35
+
36
+ // src/redis-hub-fanout.ts
37
+ var RedisHubFanout = class {
38
+ publisher;
39
+ subscriber;
40
+ channel;
41
+ onError;
42
+ handlers = /* @__PURE__ */ new Set();
43
+ subscribed = false;
44
+ constructor(publisher, subscriber, options = {}) {
45
+ this.publisher = publisher;
46
+ this.subscriber = subscriber;
47
+ this.channel = options.channel ?? "fieldnotes:fanout";
48
+ this.onError = options.onError ?? (() => void 0);
49
+ }
50
+ publish(payload) {
51
+ Promise.resolve(this.publisher.publish(this.channel, payload)).catch(this.onError);
52
+ }
53
+ subscribe(handler) {
54
+ this.handlers.add(handler);
55
+ if (!this.subscribed) {
56
+ this.subscribed = true;
57
+ Promise.resolve(
58
+ this.subscriber.subscribe(this.channel, (message) => {
59
+ for (const h of this.handlers) {
60
+ try {
61
+ h(message);
62
+ } catch {
63
+ }
64
+ }
65
+ })
66
+ ).catch(this.onError);
67
+ }
68
+ return () => this.handlers.delete(handler);
69
+ }
70
+ };
35
71
  export {
36
- RedisHubBackend
72
+ RedisHubBackend,
73
+ RedisHubFanout
37
74
  };
38
75
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/redis-hub-backend.ts"],"sourcesContent":["import { isValidElement, type SyncOp } from '@fieldnotes/sync';\nimport type { CanvasElement } from '@fieldnotes/core';\nimport type { HubBackend } from '@fieldnotes/sync-server';\nimport type { RedisHashClient } from './redis-hash-client';\n\nexport interface RedisHubBackendOptions {\n keyPrefix?: string; // default 'fieldnotes:room:'\n}\n\nexport class RedisHubBackend implements HubBackend {\n private readonly client: RedisHashClient;\n private readonly keyPrefix: string;\n\n constructor(client: RedisHashClient, options: RedisHubBackendOptions = {}) {\n this.client = client;\n this.keyPrefix = options.keyPrefix ?? 'fieldnotes:room:';\n }\n\n private key(room: string): string {\n return `${this.keyPrefix}${room}`;\n }\n\n async snapshot(room: string): Promise<CanvasElement[]> {\n const map = await this.client.hGetAll(this.key(room));\n const out: CanvasElement[] = [];\n for (const value of Object.values(map)) {\n let parsed: unknown;\n try {\n parsed = JSON.parse(value);\n } catch {\n continue; // skip a corrupt stored value rather than throwing the whole snapshot\n }\n if (isValidElement(parsed)) out.push(parsed);\n }\n return out;\n }\n\n async apply(room: string, op: SyncOp): Promise<void> {\n const key = this.key(room);\n if (op.kind === 'upsert')\n await this.client.hSet(key, op.element.id, JSON.stringify(op.element));\n else if (op.kind === 'remove') await this.client.hDel(key, op.id);\n else if (op.kind === 'clear') await this.client.del(key);\n // request-snapshot/snapshot never reach apply (the hub only applies data ops)\n }\n}\n"],"mappings":";AAAA,SAAS,sBAAmC;AASrC,IAAM,kBAAN,MAA4C;AAAA,EAChC;AAAA,EACA;AAAA,EAEjB,YAAY,QAAyB,UAAkC,CAAC,GAAG;AACzE,SAAK,SAAS;AACd,SAAK,YAAY,QAAQ,aAAa;AAAA,EACxC;AAAA,EAEQ,IAAI,MAAsB;AAChC,WAAO,GAAG,KAAK,SAAS,GAAG,IAAI;AAAA,EACjC;AAAA,EAEA,MAAM,SAAS,MAAwC;AACrD,UAAM,MAAM,MAAM,KAAK,OAAO,QAAQ,KAAK,IAAI,IAAI,CAAC;AACpD,UAAM,MAAuB,CAAC;AAC9B,eAAW,SAAS,OAAO,OAAO,GAAG,GAAG;AACtC,UAAI;AACJ,UAAI;AACF,iBAAS,KAAK,MAAM,KAAK;AAAA,MAC3B,QAAQ;AACN;AAAA,MACF;AACA,UAAI,eAAe,MAAM,EAAG,KAAI,KAAK,MAAM;AAAA,IAC7C;AACA,WAAO;AAAA,EACT;AAAA,EAEA,MAAM,MAAM,MAAc,IAA2B;AACnD,UAAM,MAAM,KAAK,IAAI,IAAI;AACzB,QAAI,GAAG,SAAS;AACd,YAAM,KAAK,OAAO,KAAK,KAAK,GAAG,QAAQ,IAAI,KAAK,UAAU,GAAG,OAAO,CAAC;AAAA,aAC9D,GAAG,SAAS,SAAU,OAAM,KAAK,OAAO,KAAK,KAAK,GAAG,EAAE;AAAA,aACvD,GAAG,SAAS,QAAS,OAAM,KAAK,OAAO,IAAI,GAAG;AAAA,EAEzD;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/redis-hub-backend.ts","../src/redis-hub-fanout.ts"],"sourcesContent":["import { isValidElement, type SyncOp } from '@fieldnotes/sync';\r\nimport type { CanvasElement } from '@fieldnotes/core';\r\nimport type { HubBackend } from '@fieldnotes/sync-server';\r\nimport type { RedisHashClient } from './redis-hash-client';\r\n\r\nexport interface RedisHubBackendOptions {\r\n keyPrefix?: string; // default 'fieldnotes:room:'\r\n}\r\n\r\nexport class RedisHubBackend implements HubBackend {\r\n private readonly client: RedisHashClient;\r\n private readonly keyPrefix: string;\r\n\r\n constructor(client: RedisHashClient, options: RedisHubBackendOptions = {}) {\r\n this.client = client;\r\n this.keyPrefix = options.keyPrefix ?? 'fieldnotes:room:';\r\n }\r\n\r\n private key(room: string): string {\r\n return `${this.keyPrefix}${room}`;\r\n }\r\n\r\n async snapshot(room: string): Promise<CanvasElement[]> {\r\n const map = await this.client.hGetAll(this.key(room));\r\n const out: CanvasElement[] = [];\r\n for (const value of Object.values(map)) {\r\n let parsed: unknown;\r\n try {\r\n parsed = JSON.parse(value);\r\n } catch {\r\n continue; // skip a corrupt stored value rather than throwing the whole snapshot\r\n }\r\n if (isValidElement(parsed)) out.push(parsed);\r\n }\r\n return out;\r\n }\r\n\r\n async apply(room: string, op: SyncOp): Promise<void> {\r\n const key = this.key(room);\r\n if (op.kind === 'upsert')\r\n await this.client.hSet(key, op.element.id, JSON.stringify(op.element));\r\n else if (op.kind === 'remove') await this.client.hDel(key, op.id);\r\n else if (op.kind === 'clear') await this.client.del(key);\r\n // request-snapshot/snapshot never reach apply (the hub only applies data ops)\r\n }\r\n}\r\n","import type { HubFanout } from '@fieldnotes/sync-server';\nimport type { RedisPublisher, RedisSubscriber } from './redis-fanout-client';\n\nexport interface RedisHubFanoutOptions {\n channel?: string;\n onError?: (err: unknown) => void;\n}\n\nexport class RedisHubFanout implements HubFanout {\n private readonly publisher: RedisPublisher;\n private readonly subscriber: RedisSubscriber;\n private readonly channel: string;\n private readonly onError: (err: unknown) => void;\n private readonly handlers = new Set<(payload: string) => void>();\n private subscribed = false;\n\n constructor(\n publisher: RedisPublisher,\n subscriber: RedisSubscriber,\n options: RedisHubFanoutOptions = {},\n ) {\n this.publisher = publisher;\n this.subscriber = subscriber;\n this.channel = options.channel ?? 'fieldnotes:fanout';\n this.onError = options.onError ?? (() => undefined);\n }\n\n publish(payload: string): void {\n Promise.resolve(this.publisher.publish(this.channel, payload)).catch(this.onError);\n }\n\n subscribe(handler: (payload: string) => void): () => void {\n this.handlers.add(handler);\n if (!this.subscribed) {\n this.subscribed = true;\n Promise.resolve(\n this.subscriber.subscribe(this.channel, (message) => {\n for (const h of this.handlers) {\n try {\n h(message);\n } catch {\n /* isolate handlers */\n }\n }\n }),\n ).catch(this.onError);\n }\n return () => this.handlers.delete(handler);\n }\n}\n"],"mappings":";AAAA,SAAS,sBAAmC;AASrC,IAAM,kBAAN,MAA4C;AAAA,EAChC;AAAA,EACA;AAAA,EAEjB,YAAY,QAAyB,UAAkC,CAAC,GAAG;AACzE,SAAK,SAAS;AACd,SAAK,YAAY,QAAQ,aAAa;AAAA,EACxC;AAAA,EAEQ,IAAI,MAAsB;AAChC,WAAO,GAAG,KAAK,SAAS,GAAG,IAAI;AAAA,EACjC;AAAA,EAEA,MAAM,SAAS,MAAwC;AACrD,UAAM,MAAM,MAAM,KAAK,OAAO,QAAQ,KAAK,IAAI,IAAI,CAAC;AACpD,UAAM,MAAuB,CAAC;AAC9B,eAAW,SAAS,OAAO,OAAO,GAAG,GAAG;AACtC,UAAI;AACJ,UAAI;AACF,iBAAS,KAAK,MAAM,KAAK;AAAA,MAC3B,QAAQ;AACN;AAAA,MACF;AACA,UAAI,eAAe,MAAM,EAAG,KAAI,KAAK,MAAM;AAAA,IAC7C;AACA,WAAO;AAAA,EACT;AAAA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",
3
+ "version": "0.2.0",
4
4
  "description": "Redis-backed HubBackend for Field Notes real-time sync relay",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -42,8 +42,8 @@
42
42
  "@vitest/coverage-v8": "^4.1.0",
43
43
  "tsup": "^8.5.1",
44
44
  "vitest": "^4.1.0",
45
- "@fieldnotes/core": "0.46.0",
46
- "@fieldnotes/sync-server": "0.1.0"
45
+ "@fieldnotes/sync-server": "0.2.0",
46
+ "@fieldnotes/core": "0.46.0"
47
47
  },
48
48
  "scripts": {
49
49
  "build": "tsup",