@@ -92,20 +92,24 @@ export class uServer extends BaseServer {
9292 } ) ;
9393 }
9494
95- override _applyMiddlewares ( req : any , res : any , callback : ( ) => void ) : void {
95+ override _applyMiddlewares (
96+ req : any ,
97+ res : any ,
98+ callback : ( err ?: any ) => void
99+ ) : void {
96100 if ( this . middlewares . length === 0 ) {
97101 return callback ( ) ;
98102 }
99103
100104 // needed to buffer headers until the status is computed
101105 req . res = new ResponseWrapper ( res ) ;
102106
103- super . _applyMiddlewares ( req , req . res , ( ) => {
107+ super . _applyMiddlewares ( req , req . res , ( err ) => {
104108 // some middlewares (like express-session) wait for the writeHead() call to flush their headers
105109 // see https://github.com/expressjs/session/blob/1010fadc2f071ddf2add94235d72224cf65159c6/index.js#L220-L244
106110 req . res . writeHead ( ) ;
107111
108- callback ( ) ;
112+ callback ( err ) ;
109113 } ) ;
110114 }
111115
@@ -118,28 +122,34 @@ export class uServer extends BaseServer {
118122
119123 req . res = res ;
120124
121- this . _applyMiddlewares ( req , res , ( ) => {
122- this . verify ( req , false , ( errorCode , errorContext ) => {
123- if ( errorCode !== undefined ) {
124- this . emit ( "connection_error" , {
125- req,
126- code : errorCode ,
127- message : Server . errorMessages [ errorCode ] ,
128- context : errorContext ,
129- } ) ;
130- this . abortRequest ( req . res , errorCode , errorContext ) ;
131- return ;
132- }
125+ const callback = ( errorCode , errorContext ) => {
126+ if ( errorCode !== undefined ) {
127+ this . emit ( "connection_error" , {
128+ req,
129+ code : errorCode ,
130+ message : Server . errorMessages [ errorCode ] ,
131+ context : errorContext ,
132+ } ) ;
133+ this . abortRequest ( req . res , errorCode , errorContext ) ;
134+ return ;
135+ }
136+
137+ if ( req . _query . sid ) {
138+ debug ( "setting new request for existing client" ) ;
139+ this . clients [ req . _query . sid ] . transport . onRequest ( req ) ;
140+ } else {
141+ const closeConnection = ( errorCode , errorContext ) =>
142+ this . abortRequest ( res , errorCode , errorContext ) ;
143+ this . handshake ( req . _query . transport , req , closeConnection ) ;
144+ }
145+ } ;
133146
134- if ( req . _query . sid ) {
135- debug ( "setting new request for existing client" ) ;
136- this . clients [ req . _query . sid ] . transport . onRequest ( req ) ;
137- } else {
138- const closeConnection = ( errorCode , errorContext ) =>
139- this . abortRequest ( res , errorCode , errorContext ) ;
140- this . handshake ( req . _query . transport , req , closeConnection ) ;
141- }
142- } ) ;
147+ this . _applyMiddlewares ( req , res , ( err ) => {
148+ if ( err ) {
149+ callback ( Server . errors . BAD_REQUEST , { name : "MIDDLEWARE_FAILURE" } ) ;
150+ } else {
151+ this . verify ( req , false , callback ) ;
152+ }
143153 } ) ;
144154 }
145155
@@ -154,63 +164,69 @@ export class uServer extends BaseServer {
154164
155165 req . res = res ;
156166
157- this . _applyMiddlewares ( req , res , ( ) => {
158- this . verify ( req , true , async ( errorCode , errorContext ) => {
159- if ( errorCode ) {
160- this . emit ( "connection_error" , {
161- req,
162- code : errorCode ,
163- message : Server . errorMessages [ errorCode ] ,
164- context : errorContext ,
165- } ) ;
166- this . abortRequest ( res , errorCode , errorContext ) ;
167+ const callback = async ( errorCode , errorContext ) => {
168+ if ( errorCode ) {
169+ this . emit ( "connection_error" , {
170+ req,
171+ code : errorCode ,
172+ message : Server . errorMessages [ errorCode ] ,
173+ context : errorContext ,
174+ } ) ;
175+ this . abortRequest ( res , errorCode , errorContext ) ;
176+ return ;
177+ }
178+
179+ const id = req . _query . sid ;
180+ let transport ;
181+
182+ if ( id ) {
183+ const client = this . clients [ id ] ;
184+ if ( ! client ) {
185+ debug ( "upgrade attempt for closed client" ) ;
186+ res . close ( ) ;
187+ } else if ( client . upgrading ) {
188+ debug ( "transport has already been trying to upgrade" ) ;
189+ res . close ( ) ;
190+ } else if ( client . upgraded ) {
191+ debug ( "transport had already been upgraded" ) ;
192+ res . close ( ) ;
193+ } else {
194+ debug ( "upgrading existing transport" ) ;
195+ transport = this . createTransport ( req . _query . transport , req ) ;
196+ client . maybeUpgrade ( transport ) ;
197+ }
198+ } else {
199+ transport = await this . handshake (
200+ req . _query . transport ,
201+ req ,
202+ ( errorCode , errorContext ) =>
203+ this . abortRequest ( res , errorCode , errorContext )
204+ ) ;
205+ if ( ! transport ) {
167206 return ;
168207 }
208+ }
169209
170- const id = req . _query . sid ;
171- let transport ;
172-
173- if ( id ) {
174- const client = this . clients [ id ] ;
175- if ( ! client ) {
176- debug ( "upgrade attempt for closed client" ) ;
177- res . close ( ) ;
178- } else if ( client . upgrading ) {
179- debug ( "transport has already been trying to upgrade" ) ;
180- res . close ( ) ;
181- } else if ( client . upgraded ) {
182- debug ( "transport had already been upgraded" ) ;
183- res . close ( ) ;
184- } else {
185- debug ( "upgrading existing transport" ) ;
186- transport = this . createTransport ( req . _query . transport , req ) ;
187- client . maybeUpgrade ( transport ) ;
188- }
189- } else {
190- transport = await this . handshake (
191- req . _query . transport ,
192- req ,
193- ( errorCode , errorContext ) =>
194- this . abortRequest ( res , errorCode , errorContext )
195- ) ;
196- if ( ! transport ) {
197- return ;
198- }
199- }
210+ // calling writeStatus() triggers the flushing of any header added in a middleware
211+ req . res . writeStatus ( "101 Switching Protocols" ) ;
200212
201- // calling writeStatus() triggers the flushing of any header added in a middleware
202- req . res . writeStatus ( "101 Switching Protocols" ) ;
203-
204- res . upgrade (
205- {
206- transport,
207- } ,
208- req . getHeader ( "sec-websocket-key" ) ,
209- req . getHeader ( "sec-websocket-protocol" ) ,
210- req . getHeader ( "sec-websocket-extensions" ) ,
211- context
212- ) ;
213- } ) ;
213+ res . upgrade (
214+ {
215+ transport,
216+ } ,
217+ req . getHeader ( "sec-websocket-key" ) ,
218+ req . getHeader ( "sec-websocket-protocol" ) ,
219+ req . getHeader ( "sec-websocket-extensions" ) ,
220+ context
221+ ) ;
222+ } ;
223+
224+ this . _applyMiddlewares ( req , res , ( err ) => {
225+ if ( err ) {
226+ callback ( Server . errors . BAD_REQUEST , { name : "MIDDLEWARE_FAILURE" } ) ;
227+ } else {
228+ this . verify ( req , true , callback ) ;
229+ }
214230 } ) ;
215231 }
216232
0 commit comments