Skip to content

fix: handle undefined remoteAddress when socket is disconnected#45

Open
Sumit-Mayani wants to merge 1 commit intojshttp:masterfrom
Sumit-Mayani:fix/disconnected-socket-undefined
Open

fix: handle undefined remoteAddress when socket is disconnected#45
Sumit-Mayani wants to merge 1 commit intojshttp:masterfrom
Sumit-Mayani:fix/disconnected-socket-undefined

Conversation

@Sumit-Mayani
Copy link
Copy Markdown

Fixes #12

Problem

getSocketAddr returned undefined in two cases:

  1. req.socket exists but remoteAddress is undefined (socket was disconnected)
  2. req.socket is falsy and req.connection is also absent — accessing .remoteAddress on undefined throws

The undefined then leaked into the addrs array returned by forwarded().

Fix

  • getSocketAddr: safely read remoteAddress from whichever of req.socket / req.connection is present; return undefined if neither has an address
  • forwarded(): skip the socket address when it is undefined rather than including it in the result

Testing

Added two new tests for the disconnected-socket cases. All 11 tests pass.

getSocketAddr returned undefined when req.socket existed but remoteAddress
was undefined (disconnected socket). Also hardened the fallback to safely
handle missing req.connection. forwarded() now omits the socket address
from the result array rather than including undefined.

Closes jshttp#12

Assisted-by AI: Claude (Anthropic)
@Sumit-Mayani Sumit-Mayani force-pushed the fix/disconnected-socket-undefined branch from 1360c26 to d9cb0de Compare April 22, 2026 10:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

getSocketAddr returned undefined, when the socket is disconnected

1 participant