Skip to content

Commit 1caba7e

Browse files
committed
Fix setting request info properties
remoteAddress and remotePort are implemented as property getters, so trying to set them causes errors in strict mode and does nothing in non-strict mode. Instead, use `Object.defineProperties` to override them. See https://github.com/hapijs/hapi/blob/v20.2.1/lib/request.js#L657
1 parent 223ff64 commit 1caba7e

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

hapi-plugin-websocket.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,16 @@ const register = async (server, pluginOptions) => {
371371
/* make available to HAPI request the remote WebSocket information */
372372
server.ext({ type: "onRequest", method: (request, h) => {
373373
if (isRequestWebSocketDriven(request)) {
374-
request.info.remoteAddress = request.plugins.websocket.req.socket.remoteAddress
375-
request.info.remotePort = request.plugins.websocket.req.socket.remotePort
374+
/* RequestInfo's remoteAddress and remotePort use getters and are not
375+
settable, so we have to replace them. */
376+
Object.defineProperties(request.info, {
377+
remoteAddress: {
378+
value: request.plugins.websocket.req.socket.remoteAddress
379+
},
380+
remotePort: {
381+
value: request.plugins.websocket.req.socket.remotePort
382+
}
383+
});
376384
}
377385
return h.continue
378386
} })

0 commit comments

Comments
 (0)