@weft-js/realtime-nats 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 +59 -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-nats
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> **Early release (0.1.0).** APIs will change between minor versions.
|
|
4
|
+
|
|
5
|
+
The realtime.bus provider for core NATS.
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @weft-js/realtime-nats
|
|
9
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weft-js/realtime-nats",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "@weft-js/realtime-nats: the realtime.bus provider for core NATS",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.ts",
|
|
9
|
+
"./plugin": "./plugin.mjs"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"@nats-io/transport-node": "3.4.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
|
+
// nats() 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 nats = definePlugin({
|
|
8
|
+
name: '@weft-js/realtime-nats',
|
|
9
|
+
version: '0.1.0',
|
|
10
|
+
weft: '*',
|
|
11
|
+
permissions: { net: ['env:NATS_URL'], env: ['NATS_URL', 'WEFT_BUILD'] },
|
|
12
|
+
contribute: options => ({
|
|
13
|
+
provides: [provide(slot, { sides: { server: { module: '@weft-js/realtime-nats', export: 'bus', options } } })],
|
|
14
|
+
agents: { title: 'Realtime bus (NATS)', text: '`realtime.bus` is NATS, at `NATS_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: 'NATS' }],
|
|
16
|
+
}),
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
export default nats
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// Core NATS (research 32 §6): at-most-once, like every bus. Channels come from tags and keys, and a subject can't hold
|
|
2
|
+
// `*`, `>` or a space, so a subject is `weft.<base64url of the channel>`. A reconnect is the `gap`.
|
|
3
|
+
|
|
4
|
+
import { connect, type NatsConnection } from '@nats-io/transport-node'
|
|
5
|
+
import type { Bus } from '@weft-js/start/bus'
|
|
6
|
+
import { StartError } from '@weft-js/start/data'
|
|
7
|
+
|
|
8
|
+
/** Options of `natsBus()`: the servers, and the credentials if they ask for any. */
|
|
9
|
+
export interface NatsBusOptions {
|
|
10
|
+
/** `nats://host:4222`, or several. */
|
|
11
|
+
servers: string | string[]
|
|
12
|
+
token?: string
|
|
13
|
+
user?: string
|
|
14
|
+
pass?: string
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const subject = (channel: string) => `weft.${Buffer.from(channel).toString('base64url')}`
|
|
18
|
+
|
|
19
|
+
/** A `Bus` over core NATS, connecting on first use. Throws Q013 without `servers`, except during a build. */
|
|
20
|
+
export function natsBus(options: NatsBusOptions): Bus {
|
|
21
|
+
if (!options.servers?.length && process.env.WEFT_BUILD) options = { ...options, servers: 'nats://localhost:4222' } // a build never connects
|
|
22
|
+
if (!options.servers?.length)
|
|
23
|
+
throw new StartError('Q013', '`natsBus` needs `servers`', 'set the URL, or leave `bus` out for the memory bus on one instance')
|
|
24
|
+
let nc: Promise<NatsConnection> | undefined
|
|
25
|
+
const gaps = new Set<() => void>()
|
|
26
|
+
const client = () => (nc ??= connect({ ...options, maxReconnectAttempts: -1, reconnectTimeWait: 500 }).then(c => {
|
|
27
|
+
void (async () => {
|
|
28
|
+
for await (const s of c.status()) if (s.type === 'reconnect') for (const gap of gaps) gap()
|
|
29
|
+
})()
|
|
30
|
+
return c
|
|
31
|
+
}, e => {
|
|
32
|
+
nc = undefined // the next use tries again
|
|
33
|
+
throw e
|
|
34
|
+
}))
|
|
35
|
+
return {
|
|
36
|
+
name: 'nats',
|
|
37
|
+
async publish(channel, message) {
|
|
38
|
+
(await client()).publish(subject(channel), message)
|
|
39
|
+
},
|
|
40
|
+
async subscribe(channel, on, gap) {
|
|
41
|
+
const c = await client()
|
|
42
|
+
const sub = c.subscribe(subject(channel), { callback: (e, m) => { if (!e) on(m.string()) } })
|
|
43
|
+
await c.flush() // the server has the subscription: a publish after this resolves is delivered
|
|
44
|
+
if (gap) gaps.add(gap)
|
|
45
|
+
return () => {
|
|
46
|
+
sub.unsubscribe()
|
|
47
|
+
if (gap) gaps.delete(gap)
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
async close() {
|
|
51
|
+
const c = await nc?.catch(() => undefined)
|
|
52
|
+
nc = undefined
|
|
53
|
+
await c?.close()
|
|
54
|
+
},
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The `realtime.bus` provider: `NATS_URL` unless the plugin was given `servers`. */
|
|
59
|
+
export const bus = (options: Partial<NatsBusOptions> = {}) => natsBus({ ...options, servers: options.servers ?? process.env.NATS_URL ?? '' })
|