import http from 'http' ; import url from 'url' ; import { AnyRouter } from '../router' ; import { NodeHTTPCreateContextFnOptions , NodeHTTPHandlerOptions , nodeHTTPRequestHandler , } from './node-http' ; export type CreateHTTPHandlerOptions < TRouter extends AnyRouter > = NodeHTTPHandlerOptions < TRouter , http . IncomingMessage , http . ServerResponse > ; export type CreateHTTPContextOptions = NodeHTTPCreateContextFnOptions < http . IncomingMessage , http . ServerResponse > ; export function createHTTPHandler < TRouter extends AnyRouter > ( opts ) { return async ( req , res ) => { const endpoint = url . parse ( req . url ! ) . pathname ! . substr ( 1 ) ; await nodeHTTPRequestHandler ( { ... opts , req , res , path : endpoint } ) ; } ; } export function createHTTPServer < TRouter extends AnyRouter > ( opts ) { const handler = createHTTPHandler ( opts ) ; const server = http . createServer ( ( req , res ) => handler ( req , res ) ) ; return { server , listen ( port ? ) { server . listen ( port ) ; const actualPort = port === 0 ? ( ( server . address ( ) as any ) . port as number ) : port ; return { port : actualPort } ; } } ; }