diff --git a/src/listener.ts b/src/listener.ts index 7d8448c..5bbf3ee 100644 --- a/src/listener.ts +++ b/src/listener.ts @@ -1,5 +1,7 @@ -import type { IncomingMessage, ServerResponse, OutgoingHttpHeaders } from 'node:http' +import { IncomingMessage, ServerResponse } from 'node:http' +import type { OutgoingHttpHeaders } from 'node:http' import type { Http2ServerRequest, Http2ServerResponse } from 'node:http2' +import type { Socket } from 'node:net' import { Readable } from 'node:stream' import type { FetchCallback } from './types' import './globals' @@ -9,6 +11,16 @@ import { Env } from 'hono' const regBuffer = /^no$/i const regContentType = /^(application\/json\b|text\/(?!event-stream\b))/i +type RequestListener = Awaited> + +export const getUpgradeListener = (requestListener: RequestListener) => { + return async (incoming: IncomingMessage, socket: Socket, head: Buffer, env?: Env['Bindings']) => { + const outgoing = new ServerResponse(incoming) + outgoing.assignSocket(socket) + return requestListener(incoming, outgoing, { incoming, socket, head, ...env }) + } +} + export const getRequestListener = (fetchCallback: FetchCallback) => { return async ( incoming: IncomingMessage | Http2ServerRequest,