@@ -88,18 +88,18 @@ const register = async (server, pluginOptions) => {
88
88
let route = null
89
89
90
90
/* determine request parameters */
91
- let url = URI . parse ( req . url )
92
- let host = typeof req . headers . host === "string" ? req . headers . host : undefined
93
- let path = url . path
94
- let protos = ( req . headers [ "sec-websocket-protocol" ] || "" ) . split ( / , * / )
91
+ const url = URI . parse ( req . url )
92
+ const host = typeof req . headers . host === "string" ? req . headers . host : undefined
93
+ const path = url . path
94
+ const protos = ( req . headers [ "sec-websocket-protocol" ] || "" ) . split ( / , * / )
95
95
96
96
/* find a matching route */
97
- let matched = server . match ( "POST" , path , host )
97
+ const matched = server . match ( "POST" , path , host )
98
98
if ( matched ) {
99
99
/* we accept only WebSocket-enabled ones */
100
100
if ( isRouteWebSocketEnabled ( matched ) ) {
101
101
/* optionally, we accept only the correct WebSocket subprotocol */
102
- let routeOptions = fetchRouteOptions ( matched )
102
+ const routeOptions = fetchRouteOptions ( matched )
103
103
if ( ! ( routeOptions . subprotocol !== null
104
104
&& protos . indexOf ( routeOptions . subprotocol ) === - 1 ) ) {
105
105
/* take this route */
@@ -137,7 +137,7 @@ const register = async (server, pluginOptions) => {
137
137
138
138
/* ensure that incoming WebSocket requests have a corresponding HAPI route */
139
139
verifyClient : ( { req } , result ) => {
140
- let route = findRoute ( req )
140
+ const route = findRoute ( req )
141
141
if ( route )
142
142
result ( true )
143
143
else
@@ -147,18 +147,18 @@ const register = async (server, pluginOptions) => {
147
147
pluginOptions . create ( wss )
148
148
149
149
/* per-route peer (aka client) tracking */
150
- let routePeers = { }
150
+ const routePeers = { }
151
151
152
152
/* per-route timers */
153
- let routeTimers = { }
153
+ const routeTimers = { }
154
154
155
155
/* on WebSocket connection (actually HTTP upgrade events)... */
156
156
wss . on ( "connection" , async ( ws , req ) => {
157
157
/* find the (previously already successfully matched) HAPI route */
158
- let route = findRoute ( req )
158
+ const route = findRoute ( req )
159
159
160
160
/* fetch the per-route options */
161
- let routeOptions = fetchRouteOptions ( route )
161
+ const routeOptions = fetchRouteOptions ( route )
162
162
163
163
/* determine a route-specific identifier */
164
164
let routeId = `${ route . method } :${ route . path } `
@@ -170,7 +170,7 @@ const register = async (server, pluginOptions) => {
170
170
/* track the peer per-route */
171
171
if ( routePeers [ routeId ] === undefined )
172
172
routePeers [ routeId ] = [ ]
173
- let peers = routePeers [ routeId ]
173
+ const peers = routePeers [ routeId ]
174
174
peers . push ( ws )
175
175
176
176
/* optionally enable automatic WebSocket PING messages */
@@ -203,21 +203,21 @@ const register = async (server, pluginOptions) => {
203
203
wsf = new WSF ( ws , routeOptions . frameEncoding )
204
204
205
205
/* provide a local context */
206
- let ctx = { }
206
+ const ctx = { }
207
207
208
208
/* allow application to hook into WebSocket connection */
209
209
routeOptions . connect . call ( ctx , { ctx, wss, ws, wsf, req, peers } )
210
210
211
211
/* determine HTTP headers for simulated HTTP request:
212
212
take headers of initial HTTP upgrade request, but explicitly remove Accept-Encoding,
213
213
because it could lead HAPI to compress the payload (which we cannot post-process) */
214
- let headers = Object . assign ( { } , req . headers )
214
+ const headers = Object . assign ( { } , req . headers )
215
215
delete headers [ "accept-encoding" ]
216
216
217
217
/* optionally inject an empty initial message */
218
218
if ( routeOptions . initially ) {
219
219
/* inject incoming WebSocket message as a simulated HTTP request */
220
- let response = await server . inject ( {
220
+ const response = await server . inject ( {
221
221
/* simulate the hard-coded POST request */
222
222
method : "POST" ,
223
223
@@ -238,7 +238,7 @@ const register = async (server, pluginOptions) => {
238
238
/* any HTTP redirection, client error or server error response
239
239
leads to an immediate WebSocket connection drop */
240
240
if ( response . statusCode >= 300 ) {
241
- let annotation = `(HAPI handler reponded with HTTP status ${ response . statusCode } )`
241
+ const annotation = `(HAPI handler reponded with HTTP status ${ response . statusCode } )`
242
242
if ( response . statusCode < 400 )
243
243
ws . close ( 1002 , `Protocol Error ${ annotation } ` )
244
244
else if ( response . statusCode < 500 )
@@ -258,10 +258,10 @@ const register = async (server, pluginOptions) => {
258
258
/* process frame of expected type only */
259
259
if ( ev . frame . type === routeOptions . frameRequest ) {
260
260
/* re-encode data as JSON as HAPI want to decode it */
261
- let message = JSON . stringify ( ev . frame . data )
261
+ const message = JSON . stringify ( ev . frame . data )
262
262
263
263
/* inject incoming WebSocket message as a simulated HTTP request */
264
- let response = await server . inject ( {
264
+ const response = await server . inject ( {
265
265
/* simulate the hard-coded POST request */
266
266
method : "POST" ,
267
267
@@ -282,8 +282,8 @@ const register = async (server, pluginOptions) => {
282
282
/* transform simulated HTTP response into an outgoing WebSocket message */
283
283
if ( response . statusCode !== 204 && ws . readyState === WS . OPEN ) {
284
284
/* decode data from JSON as HAPI has already encoded it */
285
- let type = routeOptions . frameResponse
286
- let data = JSON . parse ( response . payload )
285
+ const type = routeOptions . frameResponse
286
+ const data = JSON . parse ( response . payload )
287
287
288
288
/* send as framed data */
289
289
wsf . send ( { type, data } , ev . frame )
@@ -295,7 +295,7 @@ const register = async (server, pluginOptions) => {
295
295
/* plain WebSocket communication (uncorrelated request/reponse) */
296
296
ws . on ( "message" , async ( message ) => {
297
297
/* inject incoming WebSocket message as a simulated HTTP request */
298
- let response = await server . inject ( {
298
+ const response = await server . inject ( {
299
299
/* simulate the hard-coded POST request */
300
300
method : "POST" ,
301
301
@@ -325,7 +325,7 @@ const register = async (server, pluginOptions) => {
325
325
routeOptions . disconnect . call ( ctx , { ctx, wss, ws, wsf, req, peers } )
326
326
327
327
/* stop tracking the peer */
328
- let idx = routePeers [ routeId ] . indexOf ( ws )
328
+ const idx = routePeers [ routeId ] . indexOf ( ws )
329
329
routePeers [ routeId ] . splice ( idx , 1 )
330
330
} )
331
331
@@ -395,15 +395,15 @@ const register = async (server, pluginOptions) => {
395
395
/* handle request/response hooks */
396
396
server . ext ( { type : "onPostAuth" , method : ( request , h ) => {
397
397
if ( isRouteWebSocketEnabled ( request . route ) && isRequestWebSocketDriven ( request ) ) {
398
- let routeOptions = fetchRouteOptions ( request . route )
398
+ const routeOptions = fetchRouteOptions ( request . route )
399
399
return routeOptions . request . call ( request . plugins . websocket . ctx ,
400
400
request . plugins . websocket , request , h )
401
401
}
402
402
return h . continue
403
403
} } )
404
404
server . ext ( { type : "onPostHandler" , method : ( request , h ) => {
405
405
if ( isRouteWebSocketEnabled ( request . route ) && isRequestWebSocketDriven ( request ) ) {
406
- let routeOptions = fetchRouteOptions ( request . route )
406
+ const routeOptions = fetchRouteOptions ( request . route )
407
407
return routeOptions . response . call ( request . plugins . websocket . ctx ,
408
408
request . plugins . websocket , request , h )
409
409
}
0 commit comments