`url` attribute is `undefined` server-side
See original GitHub issue- I’ve searched for any related issues and avoided creating a duplicate issue.
Description
On server-side WebSocket instances, url
attribute is undefined. This is because it’s being set at https://github.com/websockets/ws/blob/237960e915b3f41b8c7aabfa5c938171976f2978/lib/websocket.js#L475-L481, that’s only being called when initAsClient()
function is being called, and this is detected if constructor was called setting address
as null
. This is fine, but for homogeneity and server-side operation, I would set url
attribute to the actual url being used by client-side to create the WebSocket instance, recreating the full URL by combining the request host, path and query (fragment is not send to the server). Main drawback would be that since now we would have an address, how to detect if it’s a client-side or server-side WebSocket instance. Not sure if setting it afterwards at WebSocket Server completeUpgrade() method, or by passing a custom option to flag the instance as server side.
Reproducible in:
- version: 7.3.1
- Node.js version(s): 15.0.1
- OS version(s): Ubuntu 20.04
Steps to reproduce:
- Create a WebSocket Server instance
- Connect from a client
- Read
socket.url
attribute at server-side
Expected result:
A string with the url used by the client to create the WebSocket connection
Actual result:
undefined
Issue Analytics
- State:
- Created 3 years ago
- Comments:11 (4 by maintainers)
and thanks 😄
It would be easy as
websocket.url
is already a getter so a simple check forthis._isServer
would do the job but throwing a new error is semver-major. I’ll add it to the todo list for the next major version.