@weft-js/realtime-redis 0.0.0-stage → 0.1.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 +16 -0
- package/README.md +8 -2
- package/package.json +32 -4
- package/plugin.d.mts +5 -0
- package/plugin.mjs +19 -0
- package/src/index.ts +76 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ravonus
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
|
6
|
+
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
|
|
7
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
8
|
+
persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
|
|
11
|
+
Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
|
|
14
|
+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
15
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
16
|
+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @weft-js/realtime-redis
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> **Early release (0.1.0).** APIs will change between minor versions.
|
|
4
|
+
|
|
5
|
+
The realtime.bus provider for Redis pub/sub.
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @weft-js/realtime-redis
|
|
9
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weft-js/realtime-redis",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "@weft-js/realtime-redis: the realtime.bus provider for Redis pub/sub",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.ts",
|
|
9
|
+
"./plugin": "./plugin.mjs"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"ioredis": "6.0.0"
|
|
13
|
+
},
|
|
14
|
+
"peerDependencies": {
|
|
15
|
+
"@weft-js/start": "^0.1.0",
|
|
16
|
+
"@weft-js/core": "^0.1.0"
|
|
17
|
+
},
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">=26"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [
|
|
22
|
+
"weft"
|
|
23
|
+
],
|
|
24
|
+
"files": [
|
|
25
|
+
"src",
|
|
26
|
+
"plugin.mjs",
|
|
27
|
+
"plugin.d.mts",
|
|
28
|
+
"README.md",
|
|
29
|
+
"LICENSE"
|
|
30
|
+
],
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
}
|
|
34
|
+
}
|
package/plugin.d.mts
ADDED
package/plugin.mjs
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// redis() for weft.config.ts: this package as the `realtime.bus` provider. Listed here, it replaces the memory bus, and
|
|
2
|
+
// a bus set in `src/realtime.server.ts` still wins.
|
|
3
|
+
import { definePlugin, defineSlot, provide } from '@weft-js/core/plugin'
|
|
4
|
+
|
|
5
|
+
const slot = defineSlot({ id: 'realtime.bus', version: 1, cardinality: 'one', sides: ['server'] })
|
|
6
|
+
|
|
7
|
+
export const redis = definePlugin({
|
|
8
|
+
name: '@weft-js/realtime-redis',
|
|
9
|
+
version: '0.1.0',
|
|
10
|
+
weft: '*',
|
|
11
|
+
permissions: { net: ['env:REDIS_URL'], env: ['REDIS_URL', 'WEFT_BUILD'] },
|
|
12
|
+
contribute: options => ({
|
|
13
|
+
provides: [provide(slot, { sides: { server: { module: '@weft-js/realtime-redis', export: 'bus', options } } })],
|
|
14
|
+
agents: { title: 'Realtime bus (Redis)', text: '`realtime.bus` is Redis, at `REDIS_URL`. Rooms, shared state, `.live` and streams reach every instance. It is at-most-once: anything that must survive is a mutation, and `collect` plus a query covers a gap.' },
|
|
15
|
+
map: [{ want: 'Change the realtime bus', call: 'replace this plugin in `plugins`, or set `bus` in `src/realtime.server.ts`', have: 'Redis' }],
|
|
16
|
+
}),
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
export default redis
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// Redis pub/sub (research 32 §6): two connections, because a subscriber can't publish. ioredis resubscribes after a
|
|
2
|
+
// reconnect, and whatever was published meanwhile is gone: every `ready` after the first is the `gap`.
|
|
3
|
+
|
|
4
|
+
import { Redis } from 'ioredis'
|
|
5
|
+
import type { Bus } from '@weft-js/start/bus'
|
|
6
|
+
import { StartError } from '@weft-js/start/data'
|
|
7
|
+
|
|
8
|
+
/** Options of `redisBus()`. */
|
|
9
|
+
export interface RedisBusOptions {
|
|
10
|
+
/** `redis://…` or `rediss://…`. */
|
|
11
|
+
url: string
|
|
12
|
+
/** Prefix of every Redis channel. Default `weft:`: several apps can share a server. */
|
|
13
|
+
prefix?: string
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** A `Bus` over Redis pub/sub, connecting on first use. Throws Q013 without a `redis://` URL, except during a build. */
|
|
17
|
+
export function redisBus({ url, prefix = 'weft:' }: RedisBusOptions): Bus {
|
|
18
|
+
if (!url && process.env.WEFT_BUILD) url = 'redis://localhost' // a build loads this module and never connects
|
|
19
|
+
if (!/^rediss?:\/\//.test(url ?? ''))
|
|
20
|
+
throw new StartError('Q013', `\`redisBus\` needs a \`redis://\` URL, not \`${(url ?? '').replace(/\/\/.*@/, '//…@')}\``, 'set the URL, or leave `bus` out for the memory bus on one instance')
|
|
21
|
+
let pub: Redis | undefined, sub: Redis | undefined
|
|
22
|
+
const listeners = new Map<string, Set<(message: string) => void>>()
|
|
23
|
+
const gaps = new Set<() => void>()
|
|
24
|
+
const opts = { maxRetriesPerRequest: null, lazyConnect: false }
|
|
25
|
+
const subscriber = () => {
|
|
26
|
+
if (sub) return sub
|
|
27
|
+
let connected = false
|
|
28
|
+
sub = new Redis(url, opts)
|
|
29
|
+
sub.on('error', () => {}) // ioredis retries; an unhandled `error` event would end the process
|
|
30
|
+
sub.on('ready', () => {
|
|
31
|
+
if (connected) for (const gap of gaps) gap()
|
|
32
|
+
connected = true
|
|
33
|
+
})
|
|
34
|
+
sub.on('message', (channel: string, message: string) => {
|
|
35
|
+
const list = listeners.get(channel)
|
|
36
|
+
if (list) for (const on of list) on(message)
|
|
37
|
+
})
|
|
38
|
+
return sub
|
|
39
|
+
}
|
|
40
|
+
const publisher = () => {
|
|
41
|
+
if (pub) return pub
|
|
42
|
+
pub = new Redis(url, opts)
|
|
43
|
+
pub.on('error', () => {})
|
|
44
|
+
return pub
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
name: 'redis',
|
|
48
|
+
async publish(channel, message) {
|
|
49
|
+
await publisher().publish(prefix + channel, message)
|
|
50
|
+
},
|
|
51
|
+
async subscribe(channel, on, gap) {
|
|
52
|
+
const name = prefix + channel
|
|
53
|
+
let list = listeners.get(name)
|
|
54
|
+
if (!list) listeners.set(name, (list = new Set()))
|
|
55
|
+
list.add(on)
|
|
56
|
+
if (gap) gaps.add(gap)
|
|
57
|
+
if (list.size === 1) await subscriber().subscribe(name)
|
|
58
|
+
return () => {
|
|
59
|
+
list.delete(on)
|
|
60
|
+
if (gap) gaps.delete(gap)
|
|
61
|
+
if (list.size) return
|
|
62
|
+
listeners.delete(name)
|
|
63
|
+
void sub?.unsubscribe(name).catch(() => {})
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
close() {
|
|
67
|
+
sub?.disconnect()
|
|
68
|
+
pub?.disconnect()
|
|
69
|
+
sub = pub = undefined
|
|
70
|
+
return Promise.resolve()
|
|
71
|
+
},
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** The `realtime.bus` provider: `REDIS_URL` unless the plugin was given a `url`. */
|
|
76
|
+
export const bus = (options: Partial<RedisBusOptions> = {}) => redisBus({ ...options, url: options.url ?? process.env.REDIS_URL ?? '' })
|