@nifrajs/workers 5.0.0 → 5.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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/index.ts +11 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nifrajs/workers",
3
- "version": "5.0.0",
3
+ "version": "5.1.0",
4
4
  "description": "Cloudflare Workers helpers for nifra - a Durable Object hub for cross-connection WebSocket broadcast.",
5
5
  "keywords": [
6
6
  "bun",
@@ -34,7 +34,7 @@
34
34
  "access": "public"
35
35
  },
36
36
  "dependencies": {
37
- "@nifrajs/core": "5.0.0"
37
+ "@nifrajs/core": "5.1.0"
38
38
  },
39
39
  "repository": {
40
40
  "type": "git",
package/src/index.ts CHANGED
@@ -39,6 +39,12 @@ export interface WebSocketHubApp<Env = unknown> {
39
39
  request: Request,
40
40
  platform?: { readonly env: Env; readonly waitUntil: (promise: Promise<unknown>) => void },
41
41
  ): WebSocketUpgradeOutcome | Promise<WebSocketUpgradeOutcome>
42
+ /** Answers an upgrade request for a path with no WebSocket route, as `fetch` does on every other
43
+ * runtime. An app without it gets `426`. */
44
+ fetch?(
45
+ request: Request,
46
+ platform?: { readonly env: Env; readonly waitUntil: (promise: Promise<unknown>) => void },
47
+ ): Response | Promise<Response>
42
48
  }
43
49
 
44
50
  /** Structural view of a Durable Object's state (only `waitUntil` is used). */
@@ -90,8 +96,11 @@ export function createWebSocketHub<Env = unknown>(
90
96
  const outcome = await app.resolveWebSocketUpgrade(request, platform)
91
97
  if (outcome.kind === "reject") return outcome.response
92
98
  if (outcome.kind !== "upgrade") {
93
- // Not a registered WS route (or not an upgrade) - nothing for the hub to do.
94
- return new Response("Upgrade Required", { status: 426 })
99
+ // Not a WebSocket route: its HTTP route answers, with this same request so the app's
100
+ // onRequest hooks, which the handshake check already ran, do not run again.
101
+ return app.fetch === undefined
102
+ ? new Response("Upgrade Required", { status: 426 })
103
+ : app.fetch(request, platform)
95
104
  }
96
105
  const Pair = (globalThis as { WebSocketPair?: WebSocketPairCtor }).WebSocketPair
97
106
  if (Pair === undefined) {