import { Response } from 'express' ; import { WebSocket } from './types' ; type MiddlewareFns = ( req : WebSocket [ 'upgradeReq' ] , res : Response , resolve : ( r : { req : WebSocket ; } ) => unknown ) => void ; interface ReturnOnConnect { req : WebSocket [ 'upgradeReq' ] ; } const executeMiddlewares = ( middlewares , webSocket , resolve ) => { if ( middlewares . length === 0 ) { const { upgradeReq } = webSocket ; resolve ( { req : upgradeReq } ) ; } else { const nextMiddleware = middlewares [ 0 ] ; const remainingMiddlewares = middlewares . slice ( 1 ) ; const response = { } as Response ; nextMiddleware ( webSocket . upgradeReq , response , ( ) => executeMiddlewares ( remainingMiddlewares , webSocket , resolve ) ) ; } } ; const createSubscriptionOnConnect = < T extends ReturnOnConnect > ( middlewares ) => { const onConnect = ( connectionParams , webSocket ) => new Promise < T > ( ( resolve ) => executeMiddlewares ( middlewares , webSocket , resolve ) ) ; return onConnect ; } ; export default createSubscriptionOnConnect ;