@kerf-js/realtime-durable-object 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 +249 -2
- package/package.json +41 -3
- package/plugin.d.mts +5 -0
- package/plugin.mjs +18 -0
- package/src/index.ts +115 -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,250 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="https://github.com/Ravonus/kerf">
|
|
3
|
+
<img src="https://raw.githubusercontent.com/Ravonus/kerf/main/site/public/logo.svg" width="88" height="88" alt="Kerf">
|
|
4
|
+
</a>
|
|
5
|
+
</p>
|
|
2
6
|
|
|
3
|
-
|
|
7
|
+
<h1 align="center">@kerf-js/realtime-durable-object</h1>
|
|
8
|
+
|
|
9
|
+
<p align="center">
|
|
10
|
+
<strong>A Cloudflare Durable Object as Kerf's realtime bus.</strong><br>
|
|
11
|
+
Live data, channels and shared state across every Durable Object hub on Cloudflare, with no Redis or NATS to run.
|
|
12
|
+
</p>
|
|
13
|
+
|
|
14
|
+
<p align="center">
|
|
15
|
+
<a href="https://www.npmjs.com/package/@kerf-js/realtime-durable-object"><img src="https://img.shields.io/npm/v/@kerf-js/realtime-durable-object?color=145c3c&label=npm" alt="npm version"></a>
|
|
16
|
+
<a href="https://github.com/Ravonus/kerf/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-145c3c" alt="MIT license"></a>
|
|
17
|
+
<a href="https://github.com/Ravonus/kerf/actions"><img src="https://img.shields.io/github/actions/workflow/status/Ravonus/kerf/quality.yml?branch=main&label=checks" alt="checks"></a>
|
|
18
|
+
</p>
|
|
19
|
+
|
|
20
|
+
<p align="center">
|
|
21
|
+
<a href="#quick-start">Quick start</a> ·
|
|
22
|
+
<a href="https://github.com/Ravonus/kerf/tree/main/docs">Docs</a> ·
|
|
23
|
+
<a href="https://github.com/Ravonus/kerf/tree/main/examples">Examples</a> ·
|
|
24
|
+
<a href="https://github.com/Ravonus/kerf/blob/main/CHANGELOG.md">Changelog</a> ·
|
|
25
|
+
<a href="https://github.com/Ravonus/kerf">GitHub</a>
|
|
26
|
+
</p>
|
|
27
|
+
|
|
28
|
+
> [!IMPORTANT]
|
|
29
|
+
> **Early release (0.1.x).** APIs can change between minor versions. Pin the version and read the
|
|
30
|
+
> [changelog](https://github.com/Ravonus/kerf/blob/main/CHANGELOG.md) before you upgrade.
|
|
31
|
+
|
|
32
|
+
On Cloudflare, Kerf's realtime hub runs in many places at once, and a Worker can't hold a socket open past the request
|
|
33
|
+
that started it. This package gives the hubs a meeting point that can: one Durable Object per channel, `KerfBusObject`,
|
|
34
|
+
which holds a WebSocket to every hub listening on that channel and writes each publish to all of them. It's the bus for
|
|
35
|
+
Kerf apps deployed with Nitro's `cloudflare_durable` preset.
|
|
36
|
+
|
|
37
|
+
## Highlights
|
|
38
|
+
|
|
39
|
+
- **One object per channel.** Each channel name maps to its own `KerfBusObject` through `idFromName`, so busy channels
|
|
40
|
+
don't share an object.
|
|
41
|
+
- **Hibernation API.** The object accepts sockets with `state.acceptWebSocket`, so Cloudflare can evict it from memory
|
|
42
|
+
between messages without dropping the sockets.
|
|
43
|
+
- **Holds nothing but sockets.** No storage, no history: a publish is a POST that the object writes to every socket it
|
|
44
|
+
holds.
|
|
45
|
+
- **Redials with backoff.** A lost socket is reported to the hub as a gap, then redialled after 500 ms, doubling up to
|
|
46
|
+
30 s, until it's back or the subscription stops.
|
|
47
|
+
- **No runtime imports.** The source imports only types, so workerd runs the file as it is. An app doesn't need
|
|
48
|
+
`@cloudflare/workers-types` for it.
|
|
49
|
+
- **Checked by the build.** `kerf build` refuses this bus on any preset other than `cloudflare_durable` (R002), where it
|
|
50
|
+
would silently lose messages.
|
|
51
|
+
- **Tested on workerd.** The tests run the object and two hubs in Miniflare, with no Cloudflare account.
|
|
52
|
+
|
|
53
|
+
## Install
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
# npm
|
|
57
|
+
npm install @kerf-js/realtime-durable-object
|
|
58
|
+
|
|
59
|
+
# pnpm
|
|
60
|
+
pnpm add @kerf-js/realtime-durable-object
|
|
61
|
+
|
|
62
|
+
# bun
|
|
63
|
+
bun add @kerf-js/realtime-durable-object
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Or let the `kerf` CLI install it and add it to `kerf.config.ts` in one step:
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
npx kerf add realtime-durable-object
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
You also need a Cloudflare account and `wrangler` to deploy. `kerf deploy` writes a `wrangler.toml` for the
|
|
74
|
+
`cloudflare` target and builds with `kerf build --preset cloudflare_durable`.
|
|
75
|
+
|
|
76
|
+
## Quick start
|
|
77
|
+
|
|
78
|
+
Unlike `@kerf-js/realtime-redis` and `@kerf-js/realtime-nats`, this bus can't be built from plugin options: it needs a
|
|
79
|
+
Durable Object namespace binding, and a binding only exists at run time. So the plugin in `kerf.config.ts` only
|
|
80
|
+
declares the binding, and the bus itself is set in `src/realtime.server.ts`. There are four steps.
|
|
81
|
+
|
|
82
|
+
**1. The plugin.** `kerf add` writes this:
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
// kerf.config.ts
|
|
86
|
+
import { defineConfig } from '@kerf-js/core/config'
|
|
87
|
+
import realtimeDurableObject from
|
|
88
|
+
'@kerf-js/realtime-durable-object/plugin'
|
|
89
|
+
|
|
90
|
+
export default defineConfig({
|
|
91
|
+
plugins: [realtimeDurableObject()],
|
|
92
|
+
})
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
**2. The bus.** Pass the `KERF_BUS` binding to `durableObjectBus`:
|
|
96
|
+
|
|
97
|
+
<!-- example: skip reaching `env` depends on how your worker exposes bindings -->
|
|
98
|
+
```ts
|
|
99
|
+
// src/realtime.server.ts
|
|
100
|
+
import { realtime } from '@kerf-js/start'
|
|
101
|
+
import { durableObjectBus } from '@kerf-js/realtime-durable-object'
|
|
102
|
+
import { env } from 'cloudflare:workers'
|
|
103
|
+
|
|
104
|
+
export default realtime({
|
|
105
|
+
bus: durableObjectBus({ namespace: env.KERF_BUS }),
|
|
106
|
+
})
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
> [!TIP]
|
|
110
|
+
> `cloudflare:workers` is Cloudflare's module for reading bindings outside a request handler. Any way of reaching the
|
|
111
|
+
> binding works: `durableObjectBus` only needs an object with `idFromName(name)` and `get(id)`.
|
|
112
|
+
|
|
113
|
+
**3. The class.** Export `KerfBusObject` from the worker entry, so Cloudflare can create it:
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
export { KerfBusObject } from '@kerf-js/realtime-durable-object'
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
**4. The binding and migration.** Add them to `wrangler.toml`:
|
|
120
|
+
|
|
121
|
+
```toml
|
|
122
|
+
[[durable_objects.bindings]]
|
|
123
|
+
name = "KERF_BUS"
|
|
124
|
+
class_name = "KerfBusObject"
|
|
125
|
+
|
|
126
|
+
[[migrations]]
|
|
127
|
+
tag = "kerf-bus-v1"
|
|
128
|
+
new_sqlite_classes = ["KerfBusObject"]
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Then build and deploy:
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
npx kerf build --preset cloudflare_durable
|
|
135
|
+
cd .output && npx wrangler deploy
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## How it plugs into Kerf
|
|
139
|
+
|
|
140
|
+
Kerf's realtime hub keeps everything that has state (tags, topics, replay rings, cursors, presence) and hands the bus
|
|
141
|
+
three things to do: publish a string to a channel, subscribe to a channel, and close. That is the `Bus` interface in
|
|
142
|
+
[`@kerf-js/start/bus`](https://github.com/Ravonus/kerf/blob/main/packages/start/src/bus.ts). The bus is at-most-once
|
|
143
|
+
and ordered per publisher.
|
|
144
|
+
|
|
145
|
+
```text
|
|
146
|
+
publishing hub ──POST message──▶ KerfBusObject for channel "c"
|
|
147
|
+
│
|
|
148
|
+
ws.send
|
|
149
|
+
▼
|
|
150
|
+
every hub subscribed to "c"
|
|
151
|
+
(each holds a WebSocket to the object)
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
- **`publish(channel, message)`** gets the channel's object with `namespace.get(namespace.idFromName(channel))` and
|
|
155
|
+
POSTs the message to it. The object answers `204` after writing it to every socket. Any other status rejects the
|
|
156
|
+
publish with `durable object bus: publish answered <status>`.
|
|
157
|
+
- **`subscribe(channel, on, gap)`** asks the same object for a WebSocket (an `upgrade: websocket` request), accepts it,
|
|
158
|
+
and calls `on` for each message. It resolves once the socket is open.
|
|
159
|
+
- **A lost socket** calls `gap`, then redials after `min(30 s, 500 ms × 2^attempt)`. A failed redial is another gap and
|
|
160
|
+
a longer wait. A successful one resets the backoff.
|
|
161
|
+
- **`close()`** stops every subscription this bus opened, closing their sockets with code 1000.
|
|
162
|
+
|
|
163
|
+
### Why it needs `cloudflare_durable`
|
|
164
|
+
|
|
165
|
+
A hub's subscription is a WebSocket that must stay open for as long as the hub listens. A stateless Worker's I/O
|
|
166
|
+
belongs to the request that opened it, so the socket would close with that request. Nitro's `cloudflare_durable`
|
|
167
|
+
preset runs the hub inside a Durable Object, which lives long enough to hold it. On any other preset `kerf build`
|
|
168
|
+
fails with R002.
|
|
169
|
+
|
|
170
|
+
`kerf deploy` recognises this bus from the source: a `durableObjectBus(` call, an import of
|
|
171
|
+
`@kerf-js/realtime-durable-object` or the plugin in `kerf.config.ts` counts as a shared bus, so E009 (the memory bus on
|
|
172
|
+
a many-instance host) doesn't fire.
|
|
173
|
+
|
|
174
|
+
### What the plugin declares
|
|
175
|
+
|
|
176
|
+
The plugin provides no slot. It declares the binding the bus reaches and gives agents the setup steps.
|
|
177
|
+
|
|
178
|
+
| Permission | Value |
|
|
179
|
+
| --- | --- |
|
|
180
|
+
| `net` | `binding:KERF_BUS` |
|
|
181
|
+
|
|
182
|
+
## API reference
|
|
183
|
+
|
|
184
|
+
| Export | From | What it is |
|
|
185
|
+
| --- | --- | --- |
|
|
186
|
+
| `durableObjectBus({ namespace })` | `@kerf-js/realtime-durable-object` | The `Bus` for `src/realtime.server.ts`: one `KerfBusObject` per channel, reached through `namespace`. Must be used from a Durable Object hub. |
|
|
187
|
+
| `KerfBusObject` | `@kerf-js/realtime-durable-object` | The Durable Object class. Export it from the worker and bind it as `KERF_BUS` with a `new_sqlite_classes` migration. |
|
|
188
|
+
| `DurableObjectNamespaceLike` | `@kerf-js/realtime-durable-object` | Type: the part of a namespace binding the bus uses, `idFromName(name)` and `get(id)`. `env.KERF_BUS` fits it. |
|
|
189
|
+
| default export, `durableObject` | `@kerf-js/realtime-durable-object/plugin` | The plugin for `kerf.config.ts`. Takes no options; declares `binding:KERF_BUS`. |
|
|
190
|
+
|
|
191
|
+
### `durableObjectBus` options
|
|
192
|
+
|
|
193
|
+
| Option | Type | Default | What it does |
|
|
194
|
+
| --- | --- | --- | --- |
|
|
195
|
+
| `namespace` | `DurableObjectNamespaceLike` | required | The Durable Object namespace bound to `KerfBusObject`, usually `env.KERF_BUS`. |
|
|
196
|
+
|
|
197
|
+
### `KerfBusObject`
|
|
198
|
+
|
|
199
|
+
| Method | What it does |
|
|
200
|
+
| --- | --- |
|
|
201
|
+
| `fetch(request)` | With `upgrade: websocket`: accepts a socket through the hibernation API and answers `101`. Otherwise: reads the body as the message, sends it to every socket, answers `204`. |
|
|
202
|
+
| `webSocketMessage()` | Ignores messages from hubs. Traffic only flows out of the object. |
|
|
203
|
+
| `webSocketClose(ws)` | Closes its end with code 1000. |
|
|
204
|
+
|
|
205
|
+
## Limits
|
|
206
|
+
|
|
207
|
+
- **At-most-once.** The object stores nothing. A hub that was disconnected misses what was published meanwhile, and
|
|
208
|
+
gets a gap instead.
|
|
209
|
+
- **`cloudflare_durable` only.** Not for Node, Bun, Deno or other serverless hosts; use Redis, NATS or `postgresBus`
|
|
210
|
+
there.
|
|
211
|
+
- **One object per channel.** Each object's sockets and request rate are subject to Cloudflare's Durable Object limits.
|
|
212
|
+
- **Messages are strings.** The hub serializes; the bus never parses. A message over 1 MiB is refused by the hub (R008).
|
|
213
|
+
|
|
214
|
+
## Troubleshooting
|
|
215
|
+
|
|
216
|
+
| Symptom | Cause | Fix |
|
|
217
|
+
| --- | --- | --- |
|
|
218
|
+
| `R002` "`durableObjectBus` holds a socket open, and on the `<preset>` preset that socket dies…" | Built for a preset other than `cloudflare_durable` | `kerf build --preset cloudflare_durable`. |
|
|
219
|
+
| `durable object bus: publish answered 500` | The object threw, or the binding points at another class | Check the binding's `class_name` is `KerfBusObject` and the class is exported from the worker. |
|
|
220
|
+
| `durable object bus: subscribe answered <status>` | The object didn't upgrade the request | Same as above; a route or middleware in front of the object can also strip the upgrade. |
|
|
221
|
+
| `env.KERF_BUS` is undefined | The binding is missing from `wrangler.toml`, or has another name | Add the `[[durable_objects.bindings]]` entry named `KERF_BUS`. |
|
|
222
|
+
| Cloudflare refuses the deploy over the class | No migration creates it | Add a `[[migrations]]` entry with `new_sqlite_classes = ["KerfBusObject"]`. |
|
|
223
|
+
| Pages on one hub miss messages after an object restart | The socket dropped: a gap, then a redial | Expected. What must survive goes through a mutation; clients refetch after a gap. |
|
|
224
|
+
|
|
225
|
+
## Related packages
|
|
226
|
+
|
|
227
|
+
| Package | What it is |
|
|
228
|
+
| --- | --- |
|
|
229
|
+
| [`@kerf-js/start`](https://www.npmjs.com/package/@kerf-js/start) | The full-stack runtime: the realtime hub, `realtime()`, `memoryBus` and `postgresBus`. |
|
|
230
|
+
| [`@kerf-js/realtime-redis`](https://www.npmjs.com/package/@kerf-js/realtime-redis) | The `realtime.bus` slot on Redis pub/sub, for server hosts. |
|
|
231
|
+
| [`@kerf-js/realtime-nats`](https://www.npmjs.com/package/@kerf-js/realtime-nats) | The `realtime.bus` slot on core NATS, for server hosts. |
|
|
232
|
+
| [`@kerf-js/multiplayer`](https://www.npmjs.com/package/@kerf-js/multiplayer) | Presence, cursors and a relay on `channel`. Its traffic crosses hubs through this bus. |
|
|
233
|
+
| [`@kerf-js/cli`](https://www.npmjs.com/package/@kerf-js/cli) | `kerf build`, `kerf deploy` and the R002 and E009 checks. |
|
|
234
|
+
| [`@kerf-js/core`](https://www.npmjs.com/package/@kerf-js/core) | The compiler and reactive runtime, the Vite plugin, the plugin system and test utilities. |
|
|
235
|
+
|
|
236
|
+
## Contributing
|
|
237
|
+
|
|
238
|
+
See [CONTRIBUTING.md](https://github.com/Ravonus/kerf/blob/main/CONTRIBUTING.md). To test this package:
|
|
239
|
+
|
|
240
|
+
```bash
|
|
241
|
+
pnpm --filter @kerf-js/realtime-durable-object test
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
The tests run `KerfBusObject` and two Durable Object hubs on workerd through Miniflare: a message crossing from one hub
|
|
245
|
+
to another, a refused publish, and a lost socket becoming a gap, a redial and a clean `close`. No account or deploy is
|
|
246
|
+
needed.
|
|
247
|
+
|
|
248
|
+
## License
|
|
249
|
+
|
|
250
|
+
[MIT](https://github.com/Ravonus/kerf/blob/main/LICENSE)
|
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kerf-js/realtime-durable-object",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "@kerf-js/realtime-durable-object: a Cloudflare Durable Object as the realtime bus, on cloudflare_durable",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/Ravonus/kerf.git",
|
|
9
|
+
"directory": "packages/realtime-durable-object"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/Ravonus/kerf/tree/main/packages/realtime-durable-object#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/Ravonus/kerf/issues"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": "./src/index.ts",
|
|
18
|
+
"./plugin": "./plugin.mjs"
|
|
19
|
+
},
|
|
20
|
+
"peerDependencies": {
|
|
21
|
+
"@kerf-js/start": "^0.1.0",
|
|
22
|
+
"@kerf-js/core": "^0.1.0"
|
|
23
|
+
},
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=24"
|
|
26
|
+
},
|
|
27
|
+
"keywords": [
|
|
28
|
+
"kerf"
|
|
29
|
+
],
|
|
30
|
+
"files": [
|
|
31
|
+
"src",
|
|
32
|
+
"plugin.mjs",
|
|
33
|
+
"plugin.d.mts",
|
|
34
|
+
"README.md",
|
|
35
|
+
"LICENSE"
|
|
36
|
+
],
|
|
37
|
+
"overrides": {
|
|
38
|
+
"undici": "7.30.1",
|
|
39
|
+
"sharp": "0.35.5"
|
|
40
|
+
},
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
}
|
|
6
44
|
}
|
package/plugin.d.mts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { PluginInstance } from '@kerf-js/core/plugin'
|
|
2
|
+
|
|
3
|
+
declare const plugin: (options?: Record<string, unknown>) => PluginInstance<unknown>
|
|
4
|
+
/** `durableObject()` for `kerf.config.ts`: declares the `KERF_BUS` binding. The bus itself is set in `src/realtime.server.ts`. */
|
|
5
|
+
export default plugin
|
package/plugin.mjs
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// durableObject() for kerf.config.ts: declares what the bus reaches. A binding only exists at run time, so unlike the
|
|
2
|
+
// other buses it can't be built from options: `src/realtime.server.ts` sets `durableObjectBus({ namespace })`.
|
|
3
|
+
import { definePlugin } from '@kerf-js/core/plugin'
|
|
4
|
+
|
|
5
|
+
export const durableObject = definePlugin({
|
|
6
|
+
name: '@kerf-js/realtime-durable-object',
|
|
7
|
+
version: '0.1.0',
|
|
8
|
+
kerf: '*',
|
|
9
|
+
permissions: { net: ['binding:KERF_BUS'] },
|
|
10
|
+
contribute: () => ({
|
|
11
|
+
agents: {
|
|
12
|
+
title: 'Realtime bus (Durable Object)',
|
|
13
|
+
text: "Set `bus: durableObjectBus({ namespace: env.KERF_BUS })` in `src/realtime.server.ts`, and export `KerfBusObject` from the worker with a `new_sqlite_classes` migration. It needs the `cloudflare_durable` preset (R002): a stateless Worker can't hold the socket.",
|
|
14
|
+
},
|
|
15
|
+
}),
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
export default durableObject
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// A Durable Object per channel (research 32 §6). `KerfBusObject` holds the sockets, through the hibernation API, and
|
|
2
|
+
// nothing else: a publish is a POST it writes to all of them. A hub's subscribe is a WebSocket to the object, and a
|
|
3
|
+
// lost one is a `gap`. No runtime imports, so workerd can run this file as it is.
|
|
4
|
+
//
|
|
5
|
+
// A Worker's I/O belongs to the request that opened it, so a stateless Worker can't hold that socket: this needs a hub
|
|
6
|
+
// that lives in a Durable Object, Nitro's `cloudflare_durable` preset (`kerf build` refuses the others, R002).
|
|
7
|
+
|
|
8
|
+
import type { Bus } from '@kerf-js/start/bus'
|
|
9
|
+
|
|
10
|
+
/** The parts of Workers' API used here, so an app doesn't need `@cloudflare/workers-types` for this file. */
|
|
11
|
+
interface Stub {
|
|
12
|
+
fetch(url: string, init?: RequestInit): Promise<Response & { webSocket?: CfSocket | null }>
|
|
13
|
+
}
|
|
14
|
+
/** A Durable Object namespace binding, as far as the bus uses one: `env.KERF_BUS` fits it. */
|
|
15
|
+
export interface DurableObjectNamespaceLike {
|
|
16
|
+
idFromName(name: string): unknown
|
|
17
|
+
get(id: unknown): Stub
|
|
18
|
+
}
|
|
19
|
+
interface CfSocket {
|
|
20
|
+
accept(): void
|
|
21
|
+
send(m: string): void
|
|
22
|
+
close(code?: number): void
|
|
23
|
+
addEventListener(t: string, f: (e: { data?: unknown }) => void): void
|
|
24
|
+
}
|
|
25
|
+
interface CfState {
|
|
26
|
+
acceptWebSocket(ws: CfSocket): void
|
|
27
|
+
getWebSockets(): CfSocket[]
|
|
28
|
+
}
|
|
29
|
+
declare const WebSocketPair: new () => [CfSocket, CfSocket]
|
|
30
|
+
|
|
31
|
+
const BASE = 'https://kerf-bus/'
|
|
32
|
+
|
|
33
|
+
/** Export it from the worker entry and bind it: `new_sqlite_classes: ['KerfBusObject']`, binding `KERF_BUS`. */
|
|
34
|
+
export class KerfBusObject {
|
|
35
|
+
constructor(
|
|
36
|
+
private state: CfState,
|
|
37
|
+
_env?: unknown,
|
|
38
|
+
) {}
|
|
39
|
+
|
|
40
|
+
async fetch(request: Request): Promise<Response> {
|
|
41
|
+
if (request.headers.get('upgrade')?.toLowerCase() === 'websocket') {
|
|
42
|
+
const pair = new WebSocketPair()
|
|
43
|
+
this.state.acceptWebSocket(pair[1])
|
|
44
|
+
return new Response(null, { status: 101, webSocket: pair[0] } as ResponseInit)
|
|
45
|
+
}
|
|
46
|
+
const message = await request.text()
|
|
47
|
+
for (const ws of this.state.getWebSockets()) {
|
|
48
|
+
try {
|
|
49
|
+
ws.send(message)
|
|
50
|
+
} catch {} // a socket mid-close: its own `gap` covers it
|
|
51
|
+
}
|
|
52
|
+
return new Response(null, { status: 204 })
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
webSocketMessage() {}
|
|
56
|
+
webSocketClose(ws: CfSocket) {
|
|
57
|
+
try {
|
|
58
|
+
ws.close(1000)
|
|
59
|
+
} catch {}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** The `realtime.bus` for `src/realtime.server.ts`: one `KerfBusObject` per channel, which must be reached from a Durable Object hub. */
|
|
64
|
+
export function durableObjectBus({ namespace }: { namespace: DurableObjectNamespaceLike }): Bus {
|
|
65
|
+
const stub = (channel: string) => namespace.get(namespace.idFromName(channel))
|
|
66
|
+
const open = new Set<() => void>()
|
|
67
|
+
return {
|
|
68
|
+
name: 'durable-object',
|
|
69
|
+
async publish(channel, message) {
|
|
70
|
+
const res = await stub(channel).fetch(BASE, { method: 'POST', body: message })
|
|
71
|
+
if (!res.ok) throw new Error(`durable object bus: publish answered ${res.status}`)
|
|
72
|
+
},
|
|
73
|
+
async subscribe(channel, on, gap) {
|
|
74
|
+
let stopped = false,
|
|
75
|
+
attempt = 0,
|
|
76
|
+
ws: CfSocket | undefined
|
|
77
|
+
const dial = async (): Promise<void> => {
|
|
78
|
+
const res = await stub(channel).fetch(BASE, { headers: { upgrade: 'websocket' } })
|
|
79
|
+
ws = res.webSocket ?? undefined
|
|
80
|
+
if (!ws) throw new Error(`durable object bus: subscribe answered ${res.status}`)
|
|
81
|
+
ws.accept()
|
|
82
|
+
if (stopped) return ws.close(1000) // stopped while this redial was out
|
|
83
|
+
ws.addEventListener('message', (e) => on(String(e.data)))
|
|
84
|
+
const lost = () => {
|
|
85
|
+
ws = undefined
|
|
86
|
+
if (stopped) return
|
|
87
|
+
gap?.()
|
|
88
|
+
setTimeout(
|
|
89
|
+
() =>
|
|
90
|
+
void dial().then(() => {
|
|
91
|
+
attempt = 0
|
|
92
|
+
}, lost),
|
|
93
|
+
Math.min(30_000, 500 * 2 ** attempt++),
|
|
94
|
+
)
|
|
95
|
+
}
|
|
96
|
+
ws.addEventListener('close', lost)
|
|
97
|
+
ws.addEventListener('error', () => {})
|
|
98
|
+
}
|
|
99
|
+
await dial()
|
|
100
|
+
const stop = () => {
|
|
101
|
+
stopped = true
|
|
102
|
+
open.delete(stop)
|
|
103
|
+
try {
|
|
104
|
+
ws?.close(1000)
|
|
105
|
+
} catch {}
|
|
106
|
+
}
|
|
107
|
+
open.add(stop)
|
|
108
|
+
return stop
|
|
109
|
+
},
|
|
110
|
+
close() {
|
|
111
|
+
for (const stop of open) stop()
|
|
112
|
+
return Promise.resolve()
|
|
113
|
+
},
|
|
114
|
+
}
|
|
115
|
+
}
|