@nifrajs/workers 2.2.0 → 2.4.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/README.md +3 -3
- package/package.json +3 -3
- package/src/index.ts +5 -5
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# @nifrajs/workers
|
|
2
2
|
|
|
3
|
-
Cloudflare Workers helpers for nifra
|
|
3
|
+
Cloudflare Workers helpers for nifra - a Durable Object hub for cross-connection WebSocket broadcast.
|
|
4
4
|
|
|
5
|
-
Part of the **[nifra](https://nifra.dev)** full-stack TypeScript framework
|
|
5
|
+
Part of the **[nifra](https://nifra.dev)** full-stack TypeScript framework - one core, five UI libraries, every runtime. Scaffold a new app with `bun create nifra`.
|
|
6
6
|
|
|
7
7
|
## Install
|
|
8
8
|
|
|
@@ -19,7 +19,7 @@ MIT
|
|
|
19
19
|
|
|
20
20
|
## For AI agents
|
|
21
21
|
|
|
22
|
-
Start with [`LLM.md`](./LLM.md)
|
|
22
|
+
Start with [`LLM.md`](./LLM.md) - this package's contract card (the exports you call + its footguns),
|
|
23
23
|
one cheap read instead of the whole corpus. For the wider framework: the repo's
|
|
24
24
|
[`AGENTS.md`](../../AGENTS.md) is the copy-paste quick reference, and
|
|
25
25
|
[`llms-full.txt`](../../llms-full.txt) is the full machine-readable corpus. Run `nifra check` as the
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nifrajs/workers",
|
|
3
|
-
"version": "2.
|
|
4
|
-
"description": "Cloudflare Workers helpers for nifra
|
|
3
|
+
"version": "2.4.0",
|
|
4
|
+
"description": "Cloudflare Workers helpers for nifra - a Durable Object hub for cross-connection WebSocket broadcast.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"bun",
|
|
7
7
|
"typescript",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"access": "public"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@nifrajs/core": "2.
|
|
32
|
+
"@nifrajs/core": "2.4.0"
|
|
33
33
|
},
|
|
34
34
|
"repository": {
|
|
35
35
|
"type": "git",
|
package/src/index.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Cloudflare Workers helpers for nifra.
|
|
3
3
|
*
|
|
4
4
|
* On Workers, a WebSocket lives in a single isolate, so a stateless `fetch` can't broadcast across
|
|
5
|
-
* connections
|
|
5
|
+
* connections - `app.publish(topic, data)` only reaches sockets in the same long-lived process. A
|
|
6
6
|
* **Durable Object** is that long-lived process: {@link createWebSocketHub} builds a DO class that
|
|
7
7
|
* holds every connection and runs the app's pub/sub registry, so `ws.subscribe` / `app.publish` work
|
|
8
8
|
* with their normal nifra semantics. Route upgrades to it with `toFetchHandler(app, { webSocketHub })`.
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
import type { StandardWebSocket, WebSocketUpgradeOutcome } from "@nifrajs/core/server"
|
|
34
34
|
import { attachWebSocket } from "@nifrajs/core/ws"
|
|
35
35
|
|
|
36
|
-
/** The nifra-app surface the hub needs
|
|
36
|
+
/** The nifra-app surface the hub needs - every `server()` app satisfies it. */
|
|
37
37
|
export interface WebSocketHubApp<Env = unknown> {
|
|
38
38
|
resolveWebSocketUpgrade(
|
|
39
39
|
request: Request,
|
|
@@ -46,7 +46,7 @@ interface DurableObjectStateLike {
|
|
|
46
46
|
waitUntil?(promise: Promise<unknown>): void
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
/** Cloudflare's `WebSocketPair` (a Workers global)
|
|
49
|
+
/** Cloudflare's `WebSocketPair` (a Workers global) - structurally typed to avoid a CF types dependency. */
|
|
50
50
|
type WebSocketPairCtor = new () => {
|
|
51
51
|
readonly 0: unknown
|
|
52
52
|
readonly 1: StandardWebSocket & { accept(): void }
|
|
@@ -61,7 +61,7 @@ export type WebSocketHubClass<Env> = new (
|
|
|
61
61
|
/**
|
|
62
62
|
* Build a Durable Object class that serves an app's `app.ws()` routes with **cross-connection
|
|
63
63
|
* broadcast**. Every WebSocket accepted here lives in the DO's isolate, and the app's `TopicRegistry`
|
|
64
|
-
* lives there too
|
|
64
|
+
* lives there too - so `ws.subscribe(topic)` and `app.publish(topic, data)` (called from the WS
|
|
65
65
|
* lifecycle) reach every connected client. Route upgrades to it via
|
|
66
66
|
* `toFetchHandler(app, { webSocketHub: (env) => env.YOUR_BINDING })`.
|
|
67
67
|
*
|
|
@@ -90,7 +90,7 @@ export function createWebSocketHub<Env = unknown>(
|
|
|
90
90
|
const outcome = await app.resolveWebSocketUpgrade(request, platform)
|
|
91
91
|
if (outcome.kind === "reject") return outcome.response
|
|
92
92
|
if (outcome.kind !== "upgrade") {
|
|
93
|
-
// Not a registered WS route (or not an upgrade)
|
|
93
|
+
// Not a registered WS route (or not an upgrade) - nothing for the hub to do.
|
|
94
94
|
return new Response("Upgrade Required", { status: 426 })
|
|
95
95
|
}
|
|
96
96
|
const Pair = (globalThis as { WebSocketPair?: WebSocketPairCtor }).WebSocketPair
|