@@ -3,14 +3,39 @@ import { createMiddleware } from "hono/factory";
33import { routePartykitRequest } from "partyserver" ;
44
55import type { Context , Env } from "hono" ;
6- import type { PartyServerOptions } from "partyserver" ;
6+ import type { Lobby , PartyServerOptions } from "partyserver" ;
7+
8+ /**
9+ * Extended options for the Hono middleware that pass the Hono context
10+ * to `onBeforeConnect` and `onBeforeRequest` as a third argument,
11+ * giving access to `c.env`, `c.var`, `c.get()`, etc.
12+ */
13+ export type HonoPartyServerOptions < E extends Env > = Omit <
14+ PartyServerOptions ,
15+ "onBeforeConnect" | "onBeforeRequest"
16+ > & {
17+ onBeforeConnect ?: (
18+ req : Request ,
19+ lobby : Lobby ,
20+ c : Context < E >
21+ ) => Response | Request | void | Promise < Response | Request | void > ;
22+ onBeforeRequest ?: (
23+ req : Request ,
24+ lobby : Lobby ,
25+ c : Context < E >
26+ ) =>
27+ | Response
28+ | Request
29+ | void
30+ | Promise < Response | Request | undefined | void > ;
31+ } ;
732
833/**
934 * Configuration options for the PartyServer middleware
1035 */
1136type PartyServerMiddlewareContext < E extends Env > = {
1237 /** PartyServer-specific configuration options */
13- options ?: PartyServerOptions < E > ;
38+ options ?: HonoPartyServerOptions < E > ;
1439 /** Optional error handler for caught errors */
1540 onError ?: ( error : Error ) => void ;
1641} ;
@@ -22,12 +47,12 @@ type PartyServerMiddlewareContext<E extends Env> = {
2247export function partyserverMiddleware < E extends Env = Env > (
2348 ctx ?: PartyServerMiddlewareContext < E >
2449) {
25- return createMiddleware ( async ( c , next ) => {
50+ return createMiddleware < E > ( async ( c , next ) => {
2651 try {
27- const handler = isWebSocketUpgrade ( c )
28- ? handleWebSocketUpgrade
29- : handleHttpRequest ;
30- const response = await handler ( c , ctx ?. options ) ;
52+ const options = wrapOptionsWithContext ( ctx ?. options , c ) ;
53+ const response = isWebSocketUpgrade ( c )
54+ ? await handleWebSocketUpgrade ( c , options )
55+ : await handleHttpRequest ( c , options ) ;
3156
3257 return response === null ? await next ( ) : response ;
3358 } catch ( error ) {
@@ -40,6 +65,30 @@ export function partyserverMiddleware<E extends Env = Env>(
4065 } ) ;
4166}
4267
68+ /**
69+ * Wraps the Hono-specific options into standard PartyServerOptions by
70+ * closing over the Hono context so callbacks receive it as a third arg.
71+ */
72+ function wrapOptionsWithContext < E extends Env > (
73+ options : HonoPartyServerOptions < E > | undefined ,
74+ c : Context < E >
75+ ) : PartyServerOptions | undefined {
76+ if ( ! options ) return undefined ;
77+
78+ const { onBeforeConnect, onBeforeRequest, ...rest } = options ;
79+ return {
80+ ...rest ,
81+ ...( onBeforeConnect && {
82+ onBeforeConnect : ( req : Request , lobby : Lobby ) =>
83+ onBeforeConnect ( req , lobby , c )
84+ } ) ,
85+ ...( onBeforeRequest && {
86+ onBeforeRequest : ( req : Request , lobby : Lobby ) =>
87+ onBeforeRequest ( req , lobby , c )
88+ } )
89+ } ;
90+ }
91+
4392/**
4493 * Checks if the incoming request is a WebSocket upgrade request
4594 * Looks for the 'upgrade' header with a value of 'websocket' (case-insensitive)
@@ -60,9 +109,9 @@ function createRequestFromContext(c: Context) {
60109 * Handles WebSocket upgrade requests
61110 * Returns a WebSocket upgrade response if successful, null otherwise
62111 */
63- async function handleWebSocketUpgrade < E extends Env > (
64- c : Context < E > ,
65- options ?: PartyServerOptions < E >
112+ async function handleWebSocketUpgrade (
113+ c : Context ,
114+ options ?: PartyServerOptions
66115) {
67116 const req = createRequestFromContext ( c ) ;
68117 const response = await routePartykitRequest ( req , env ( c ) , options ) ;
@@ -81,10 +130,7 @@ async function handleWebSocketUpgrade<E extends Env>(
81130 * Handles standard HTTP requests
82131 * Forwards the request to PartyServer and returns the response
83132 */
84- async function handleHttpRequest < E extends Env > (
85- c : Context < E > ,
86- options ?: PartyServerOptions < E >
87- ) {
133+ async function handleHttpRequest ( c : Context , options ?: PartyServerOptions ) {
88134 const req = createRequestFromContext ( c ) ;
89135 return routePartykitRequest ( req , env ( c ) , options ) ;
90136}
0 commit comments