@@ -215,9 +215,10 @@ const register = async (server, pluginOptions) => {
215
215
delete headers [ "accept-encoding" ]
216
216
217
217
/* optionally inject an empty initial message */
218
+ let initially ;
218
219
if ( routeOptions . initially ) {
219
220
/* inject incoming WebSocket message as a simulated HTTP request */
220
- server . inject ( {
221
+ initially = server . inject ( {
221
222
/* simulate the hard-coded POST request */
222
223
method : "POST" ,
223
224
@@ -236,14 +237,17 @@ const register = async (server, pluginOptions) => {
236
237
} ) . then ( response => {
237
238
/* any HTTP redirection, client error or server error response
238
239
leads to an immediate WebSocket connection drop */
239
- if ( response . statusCode >= 300 ) {
240
+ if ( response . statusCode < 300 ) {
241
+ return true ;
242
+ } else {
240
243
const annotation = `(HAPI handler responded with HTTP status ${ response . statusCode } )`
241
244
if ( response . statusCode < 400 )
242
245
ws . close ( 1002 , `Protocol Error ${ annotation } ` )
243
246
else if ( response . statusCode < 500 )
244
247
ws . close ( 1008 , `Policy Violation ${ annotation } ` )
245
248
else
246
249
ws . close ( 1011 , `Server Error ${ annotation } ` )
250
+ return false ;
247
251
}
248
252
} ) ;
249
253
}
@@ -252,6 +256,10 @@ const register = async (server, pluginOptions) => {
252
256
if ( routeOptions . frame === true ) {
253
257
/* framed WebSocket communication (correlated request/reply) */
254
258
wsf . on ( "message" , async ( ev ) => {
259
+ if ( initially && ! ( await initially ) ) {
260
+ return ;
261
+ }
262
+
255
263
/* allow application to hook into raw WebSocket frame processing */
256
264
routeOptions . frameMessage . call ( ctx , { ctx, wss, ws, wsf, req, peers } , ev . frame )
257
265
@@ -294,6 +302,10 @@ const register = async (server, pluginOptions) => {
294
302
else {
295
303
/* plain WebSocket communication (uncorrelated request/response) */
296
304
ws . on ( "message" , async ( message ) => {
305
+ if ( initially && ! ( await initially ) ) {
306
+ return ;
307
+ }
308
+
297
309
/* inject incoming WebSocket message as a simulated HTTP request */
298
310
const response = await server . inject ( {
299
311
/* simulate the hard-coded POST request */
@@ -322,6 +334,7 @@ const register = async (server, pluginOptions) => {
322
334
/* hook into WebSocket disconnection */
323
335
ws . on ( "close" , ( ) => {
324
336
/* allow application to hook into WebSocket disconnection */
337
+ /* note that this is done even if the `initially` handler closes the connection */
325
338
routeOptions . disconnect . call ( ctx , { ctx, wss, ws, wsf, req, peers } )
326
339
327
340
/* stop tracking the peer */
@@ -330,6 +343,7 @@ const register = async (server, pluginOptions) => {
330
343
} )
331
344
332
345
/* allow application to hook into WebSocket error processing */
346
+ /* note that this is done even if the `initially` handler closes the connection */
333
347
ws . on ( "error" , ( error ) => {
334
348
routeOptions . error . call ( ctx , { ctx, wss, ws, wsf, req, peers } , error )
335
349
} )
0 commit comments