@fieldnotes/sync-redis 0.2.0 → 0.3.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/README.md +148 -144
- package/dist/index.cjs +10 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,144 +1,148 @@
|
|
|
1
|
-
# @fieldnotes/sync-redis
|
|
2
|
-
|
|
3
|
-
A `HubBackend` that persists Field Notes relay room state in Redis — state survives relay restarts and is
|
|
4
|
-
shared across relay instances.
|
|
5
|
-
|
|
6
|
-
## Install
|
|
7
|
-
|
|
8
|
-
```bash
|
|
9
|
-
pnpm add @fieldnotes/sync-redis
|
|
10
|
-
# plus your Redis client of choice, e.g.:
|
|
11
|
-
pnpm add redis # node-redis v4
|
|
12
|
-
# or
|
|
13
|
-
pnpm add ioredis
|
|
14
|
-
```
|
|
15
|
-
|
|
16
|
-
`@fieldnotes/sync-redis` has **no Redis dependency of its own** — you inject a client that satisfies a
|
|
17
|
-
minimal `RedisHashClient` interface (`hGetAll` / `hSet` / `hDel` / `del`).
|
|
18
|
-
|
|
19
|
-
## node-redis v4 (direct)
|
|
20
|
-
|
|
21
|
-
node-redis v4's client already matches `RedisHashClient`, so you can pass it straight through:
|
|
22
|
-
|
|
23
|
-
```ts
|
|
24
|
-
import { createClient } from 'redis';
|
|
25
|
-
import { createSyncServer } from '@fieldnotes/sync-server';
|
|
26
|
-
import { RedisHubBackend } from '@fieldnotes/sync-redis';
|
|
27
|
-
|
|
28
|
-
const client = createClient({ url: process.env.REDIS_URL });
|
|
29
|
-
await client.connect();
|
|
30
|
-
createSyncServer({ port: 8080, backend: new RedisHubBackend(client) });
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
## ioredis (shim)
|
|
34
|
-
|
|
35
|
-
ioredis uses lowercased method names, so wrap it in a small adapter:
|
|
36
|
-
|
|
37
|
-
```ts
|
|
38
|
-
import Redis from 'ioredis';
|
|
39
|
-
import { RedisHubBackend } from '@fieldnotes/sync-redis';
|
|
40
|
-
|
|
41
|
-
const io = new Redis(process.env.REDIS_URL);
|
|
42
|
-
const client = {
|
|
43
|
-
hGetAll: (k: string) => io.hgetall(k),
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
- **
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
is
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
const
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
1
|
+
# @fieldnotes/sync-redis
|
|
2
|
+
|
|
3
|
+
A `HubBackend` that persists Field Notes relay room state in Redis — state survives relay restarts and is
|
|
4
|
+
shared across relay instances.
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
pnpm add @fieldnotes/sync-redis
|
|
10
|
+
# plus your Redis client of choice, e.g.:
|
|
11
|
+
pnpm add redis # node-redis v4
|
|
12
|
+
# or
|
|
13
|
+
pnpm add ioredis
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
`@fieldnotes/sync-redis` has **no Redis dependency of its own** — you inject a client that satisfies a
|
|
17
|
+
minimal `RedisHashClient` interface (`hGetAll` / `hGet` / `hSet` / `hDel` / `del`).
|
|
18
|
+
|
|
19
|
+
## node-redis v4 (direct)
|
|
20
|
+
|
|
21
|
+
node-redis v4's client already matches `RedisHashClient`, so you can pass it straight through:
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { createClient } from 'redis';
|
|
25
|
+
import { createSyncServer } from '@fieldnotes/sync-server';
|
|
26
|
+
import { RedisHubBackend } from '@fieldnotes/sync-redis';
|
|
27
|
+
|
|
28
|
+
const client = createClient({ url: process.env.REDIS_URL });
|
|
29
|
+
await client.connect();
|
|
30
|
+
createSyncServer({ port: 8080, backend: new RedisHubBackend(client) });
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## ioredis (shim)
|
|
34
|
+
|
|
35
|
+
ioredis uses lowercased method names, so wrap it in a small adapter:
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import Redis from 'ioredis';
|
|
39
|
+
import { RedisHubBackend } from '@fieldnotes/sync-redis';
|
|
40
|
+
|
|
41
|
+
const io = new Redis(process.env.REDIS_URL);
|
|
42
|
+
const client = {
|
|
43
|
+
hGetAll: (k: string) => io.hgetall(k),
|
|
44
|
+
hGet: (k: string, f: string) => io.hget(k, f),
|
|
45
|
+
hSet: (k: string, f: string, v: string) => io.hset(k, f, v),
|
|
46
|
+
hDel: (k: string, f: string) => io.hdel(k, f),
|
|
47
|
+
del: (k: string) => io.del(k),
|
|
48
|
+
};
|
|
49
|
+
const backend = new RedisHubBackend(client);
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Key schema
|
|
53
|
+
|
|
54
|
+
Each room is stored as a Redis **HASH** at `{keyPrefix}{room}` (default prefix `fieldnotes:room:`):
|
|
55
|
+
|
|
56
|
+
- **field** = element id
|
|
57
|
+
- **value** = `JSON.stringify(element)`
|
|
58
|
+
|
|
59
|
+
node-redis v4 conforms to `RedisHashClient` directly (it has `hGet`); `RedisHubBackend.get(room, id)`
|
|
60
|
+
(via `HGET`) powers the relay's ownership lookups for write authorization (D2).
|
|
61
|
+
|
|
62
|
+
The prefix is configurable:
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
new RedisHubBackend(client, { keyPrefix: 'myapp:room:' });
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Single vs. multiple relay instances
|
|
69
|
+
|
|
70
|
+
Persistence and shared state work with any number of relay instances — every instance reads and writes the
|
|
71
|
+
same Redis, so room state survives restarts and is visible to all of them.
|
|
72
|
+
|
|
73
|
+
For clients connected to **different** relay instances to see each other's **live** ops, you also need
|
|
74
|
+
cross-instance fan-out (Redis pub/sub) — available via `RedisHubFanout` (below). A **single** relay instance
|
|
75
|
+
is fully live on its own.
|
|
76
|
+
|
|
77
|
+
## Cross-instance fan-out (`RedisHubFanout`)
|
|
78
|
+
|
|
79
|
+
`RedisHubFanout` is a `HubFanout` (from `@fieldnotes/sync-server`) over Redis pub/sub: each relay instance
|
|
80
|
+
publishes its live ops to a single channel and every other instance forwards them to its local connections.
|
|
81
|
+
Like the backend, it has **no Redis dependency of its own** — you inject two connections via the
|
|
82
|
+
`RedisPublisher` / `RedisSubscriber` seams.
|
|
83
|
+
|
|
84
|
+
> **The subscriber MUST be a dedicated connection.** Redis forbids any other command on a connection that is
|
|
85
|
+
> in subscribe mode, so publish and subscribe cannot share one connection — that is why `RedisHubFanout`
|
|
86
|
+
> takes two.
|
|
87
|
+
|
|
88
|
+
### node-redis v4
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
import { createClient } from 'redis';
|
|
92
|
+
import { createSyncServer } from '@fieldnotes/sync-server';
|
|
93
|
+
import { RedisHubBackend, RedisHubFanout } from '@fieldnotes/sync-redis';
|
|
94
|
+
|
|
95
|
+
const hash = createClient({ url: process.env.REDIS_URL });
|
|
96
|
+
await hash.connect(); // HASH backend (persistence)
|
|
97
|
+
|
|
98
|
+
const publisher = createClient({ url: process.env.REDIS_URL });
|
|
99
|
+
await publisher.connect();
|
|
100
|
+
const subscriber = publisher.duplicate();
|
|
101
|
+
await subscriber.connect(); // DEDICATED subscriber — a subscriber connection cannot also publish
|
|
102
|
+
|
|
103
|
+
createSyncServer({
|
|
104
|
+
port: 8080,
|
|
105
|
+
backend: new RedisHubBackend(hash),
|
|
106
|
+
fanout: new RedisHubFanout(publisher, subscriber),
|
|
107
|
+
});
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### ioredis
|
|
111
|
+
|
|
112
|
+
ioredis lowercases `publish`/`subscribe` and delivers messages via a `'message'` event, so wrap each client:
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
import Redis from 'ioredis';
|
|
116
|
+
import { RedisHubFanout } from '@fieldnotes/sync-redis';
|
|
117
|
+
|
|
118
|
+
const pubIo = new Redis(process.env.REDIS_URL); // publish connection
|
|
119
|
+
const subIo = new Redis(process.env.REDIS_URL); // SECOND, dedicated subscriber connection
|
|
120
|
+
|
|
121
|
+
const publisher = { publish: (c: string, m: string) => pubIo.publish(c, m) };
|
|
122
|
+
const subscriber = {
|
|
123
|
+
subscribe: (channel: string, listener: (m: string) => void) => {
|
|
124
|
+
subIo.on('message', (ch, msg) => {
|
|
125
|
+
if (ch === channel) listener(msg);
|
|
126
|
+
});
|
|
127
|
+
return subIo.subscribe(channel);
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const fanout = new RedisHubFanout(publisher, subscriber);
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Channel
|
|
135
|
+
|
|
136
|
+
All instances must share one channel (default `fieldnotes:fanout`, configurable):
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
new RedisHubFanout(publisher, subscriber, { channel: 'myapp:fanout' });
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Precondition: shared fanout AND shared backend
|
|
143
|
+
|
|
144
|
+
Multi-instance **live** sync requires **both** a shared fanout **and** a shared backend (both Redis). The
|
|
145
|
+
fanout forwards live ops; the shared backend keeps snapshots consistent. A shared fanout **without** a shared
|
|
146
|
+
backend leaves a new joiner's snapshot stale — it would catch up from whichever instance it happened to hit,
|
|
147
|
+
missing edits applied elsewhere. Pair `RedisHubFanout` with `RedisHubBackend` for full multi-instance
|
|
148
|
+
real-time sync.
|
package/dist/index.cjs
CHANGED
|
@@ -51,6 +51,16 @@ var RedisHubBackend = class {
|
|
|
51
51
|
}
|
|
52
52
|
return out;
|
|
53
53
|
}
|
|
54
|
+
async get(room, id) {
|
|
55
|
+
const value = await this.client.hGet(this.key(room), id);
|
|
56
|
+
if (value == null) return void 0;
|
|
57
|
+
try {
|
|
58
|
+
const parsed = JSON.parse(value);
|
|
59
|
+
return (0, import_sync.isValidElement)(parsed) ? parsed : void 0;
|
|
60
|
+
} catch {
|
|
61
|
+
return void 0;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
54
64
|
async apply(room, op) {
|
|
55
65
|
const key = this.key(room);
|
|
56
66
|
if (op.kind === "upsert")
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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":";;;;;;;;;;;;;;;;;;;;AAAA;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;;;
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/redis-hub-backend.ts","../src/redis-hub-fanout.ts"],"sourcesContent":["export { RedisHubBackend } from './redis-hub-backend';\r\nexport type { RedisHubBackendOptions } from './redis-hub-backend';\r\nexport type { RedisHashClient } from './redis-hash-client';\r\nexport { RedisHubFanout } from './redis-hub-fanout';\r\nexport type { RedisHubFanoutOptions } from './redis-hub-fanout';\r\nexport type { RedisPublisher, RedisSubscriber } from './redis-fanout-client';\r\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 get(room: string, id: string): Promise<CanvasElement | undefined> {\r\n const value = await this.client.hGet(this.key(room), id);\r\n if (value == null) return undefined;\r\n try {\r\n const parsed: unknown = JSON.parse(value);\r\n return isValidElement(parsed) ? parsed : undefined;\r\n } catch {\r\n return undefined;\r\n }\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';\r\nimport type { RedisPublisher, RedisSubscriber } from './redis-fanout-client';\r\n\r\nexport interface RedisHubFanoutOptions {\r\n channel?: string;\r\n onError?: (err: unknown) => void;\r\n}\r\n\r\nexport class RedisHubFanout implements HubFanout {\r\n private readonly publisher: RedisPublisher;\r\n private readonly subscriber: RedisSubscriber;\r\n private readonly channel: string;\r\n private readonly onError: (err: unknown) => void;\r\n private readonly handlers = new Set<(payload: string) => void>();\r\n private subscribed = false;\r\n\r\n constructor(\r\n publisher: RedisPublisher,\r\n subscriber: RedisSubscriber,\r\n options: RedisHubFanoutOptions = {},\r\n ) {\r\n this.publisher = publisher;\r\n this.subscriber = subscriber;\r\n this.channel = options.channel ?? 'fieldnotes:fanout';\r\n this.onError = options.onError ?? (() => undefined);\r\n }\r\n\r\n publish(payload: string): void {\r\n Promise.resolve(this.publisher.publish(this.channel, payload)).catch(this.onError);\r\n }\r\n\r\n subscribe(handler: (payload: string) => void): () => void {\r\n this.handlers.add(handler);\r\n if (!this.subscribed) {\r\n this.subscribed = true;\r\n Promise.resolve(\r\n this.subscriber.subscribe(this.channel, (message) => {\r\n for (const h of this.handlers) {\r\n try {\r\n h(message);\r\n } catch {\r\n /* isolate handlers */\r\n }\r\n }\r\n }),\r\n ).catch(this.onError);\r\n }\r\n return () => this.handlers.delete(handler);\r\n }\r\n}\r\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;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,IAAI,MAAc,IAAgD;AACtE,UAAM,QAAQ,MAAM,KAAK,OAAO,KAAK,KAAK,IAAI,IAAI,GAAG,EAAE;AACvD,QAAI,SAAS,KAAM,QAAO;AAC1B,QAAI;AACF,YAAM,SAAkB,KAAK,MAAM,KAAK;AACxC,iBAAO,4BAAe,MAAM,IAAI,SAAS;AAAA,IAC3C,QAAQ;AACN,aAAO;AAAA,IACT;AAAA,EACF;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;;;AChDO,IAAM,iBAAN,MAA0C;AAAA,EAC9B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,WAAW,oBAAI,IAA+B;AAAA,EACvD,aAAa;AAAA,EAErB,YACE,WACA,YACA,UAAiC,CAAC,GAClC;AACA,SAAK,YAAY;AACjB,SAAK,aAAa;AAClB,SAAK,UAAU,QAAQ,WAAW;AAClC,SAAK,UAAU,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
|
@@ -4,6 +4,7 @@ import { HubBackend, HubFanout } from '@fieldnotes/sync-server';
|
|
|
4
4
|
|
|
5
5
|
interface RedisHashClient {
|
|
6
6
|
hGetAll(key: string): Promise<Record<string, string>>;
|
|
7
|
+
hGet(key: string, field: string): Promise<string | null>;
|
|
7
8
|
hSet(key: string, field: string, value: string): Promise<unknown>;
|
|
8
9
|
hDel(key: string, field: string): Promise<unknown>;
|
|
9
10
|
del(key: string): Promise<unknown>;
|
|
@@ -18,6 +19,7 @@ declare class RedisHubBackend implements HubBackend {
|
|
|
18
19
|
constructor(client: RedisHashClient, options?: RedisHubBackendOptions);
|
|
19
20
|
private key;
|
|
20
21
|
snapshot(room: string): Promise<CanvasElement[]>;
|
|
22
|
+
get(room: string, id: string): Promise<CanvasElement | undefined>;
|
|
21
23
|
apply(room: string, op: SyncOp): Promise<void>;
|
|
22
24
|
}
|
|
23
25
|
|
package/dist/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { HubBackend, HubFanout } from '@fieldnotes/sync-server';
|
|
|
4
4
|
|
|
5
5
|
interface RedisHashClient {
|
|
6
6
|
hGetAll(key: string): Promise<Record<string, string>>;
|
|
7
|
+
hGet(key: string, field: string): Promise<string | null>;
|
|
7
8
|
hSet(key: string, field: string, value: string): Promise<unknown>;
|
|
8
9
|
hDel(key: string, field: string): Promise<unknown>;
|
|
9
10
|
del(key: string): Promise<unknown>;
|
|
@@ -18,6 +19,7 @@ declare class RedisHubBackend implements HubBackend {
|
|
|
18
19
|
constructor(client: RedisHashClient, options?: RedisHubBackendOptions);
|
|
19
20
|
private key;
|
|
20
21
|
snapshot(room: string): Promise<CanvasElement[]>;
|
|
22
|
+
get(room: string, id: string): Promise<CanvasElement | undefined>;
|
|
21
23
|
apply(room: string, op: SyncOp): Promise<void>;
|
|
22
24
|
}
|
|
23
25
|
|
package/dist/index.js
CHANGED
|
@@ -24,6 +24,16 @@ var RedisHubBackend = class {
|
|
|
24
24
|
}
|
|
25
25
|
return out;
|
|
26
26
|
}
|
|
27
|
+
async get(room, id) {
|
|
28
|
+
const value = await this.client.hGet(this.key(room), id);
|
|
29
|
+
if (value == null) return void 0;
|
|
30
|
+
try {
|
|
31
|
+
const parsed = JSON.parse(value);
|
|
32
|
+
return isValidElement(parsed) ? parsed : void 0;
|
|
33
|
+
} catch {
|
|
34
|
+
return void 0;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
27
37
|
async apply(room, op) {
|
|
28
38
|
const key = this.key(room);
|
|
29
39
|
if (op.kind === "upsert")
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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,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;;;
|
|
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 get(room: string, id: string): Promise<CanvasElement | undefined> {\r\n const value = await this.client.hGet(this.key(room), id);\r\n if (value == null) return undefined;\r\n try {\r\n const parsed: unknown = JSON.parse(value);\r\n return isValidElement(parsed) ? parsed : undefined;\r\n } catch {\r\n return undefined;\r\n }\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';\r\nimport type { RedisPublisher, RedisSubscriber } from './redis-fanout-client';\r\n\r\nexport interface RedisHubFanoutOptions {\r\n channel?: string;\r\n onError?: (err: unknown) => void;\r\n}\r\n\r\nexport class RedisHubFanout implements HubFanout {\r\n private readonly publisher: RedisPublisher;\r\n private readonly subscriber: RedisSubscriber;\r\n private readonly channel: string;\r\n private readonly onError: (err: unknown) => void;\r\n private readonly handlers = new Set<(payload: string) => void>();\r\n private subscribed = false;\r\n\r\n constructor(\r\n publisher: RedisPublisher,\r\n subscriber: RedisSubscriber,\r\n options: RedisHubFanoutOptions = {},\r\n ) {\r\n this.publisher = publisher;\r\n this.subscriber = subscriber;\r\n this.channel = options.channel ?? 'fieldnotes:fanout';\r\n this.onError = options.onError ?? (() => undefined);\r\n }\r\n\r\n publish(payload: string): void {\r\n Promise.resolve(this.publisher.publish(this.channel, payload)).catch(this.onError);\r\n }\r\n\r\n subscribe(handler: (payload: string) => void): () => void {\r\n this.handlers.add(handler);\r\n if (!this.subscribed) {\r\n this.subscribed = true;\r\n Promise.resolve(\r\n this.subscriber.subscribe(this.channel, (message) => {\r\n for (const h of this.handlers) {\r\n try {\r\n h(message);\r\n } catch {\r\n /* isolate handlers */\r\n }\r\n }\r\n }),\r\n ).catch(this.onError);\r\n }\r\n return () => this.handlers.delete(handler);\r\n }\r\n}\r\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,IAAI,MAAc,IAAgD;AACtE,UAAM,QAAQ,MAAM,KAAK,OAAO,KAAK,KAAK,IAAI,IAAI,GAAG,EAAE;AACvD,QAAI,SAAS,KAAM,QAAO;AAC1B,QAAI;AACF,YAAM,SAAkB,KAAK,MAAM,KAAK;AACxC,aAAO,eAAe,MAAM,IAAI,SAAS;AAAA,IAC3C,QAAQ;AACN,aAAO;AAAA,IACT;AAAA,EACF;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;;;AChDO,IAAM,iBAAN,MAA0C;AAAA,EAC9B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,WAAW,oBAAI,IAA+B;AAAA,EACvD,aAAa;AAAA,EAErB,YACE,WACA,YACA,UAAiC,CAAC,GAClC;AACA,SAAK,YAAY;AACjB,SAAK,aAAa;AAClB,SAAK,UAAU,QAAQ,WAAW;AAClC,SAAK,UAAU,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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fieldnotes/sync-redis",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Redis-backed HubBackend for Field Notes real-time sync relay",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -36,14 +36,14 @@
|
|
|
36
36
|
"fieldnotes"
|
|
37
37
|
],
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@fieldnotes/sync": "0.
|
|
39
|
+
"@fieldnotes/sync": "0.7.1"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@vitest/coverage-v8": "^4.1.0",
|
|
43
43
|
"tsup": "^8.5.1",
|
|
44
44
|
"vitest": "^4.1.0",
|
|
45
|
-
"@fieldnotes/
|
|
46
|
-
"@fieldnotes/
|
|
45
|
+
"@fieldnotes/core": "0.50.1",
|
|
46
|
+
"@fieldnotes/sync-server": "0.8.1"
|
|
47
47
|
},
|
|
48
48
|
"scripts": {
|
|
49
49
|
"build": "tsup",
|