Skip to content

fix(request): use PassThrough as socket for IncomingMessage#714

Open
alex-all3dp wants to merge 1 commit intoCodeGenieApp:mainlinefrom
alex-all3dp:fix/socket-event-emitter
Open

fix(request): use PassThrough as socket for IncomingMessage#714
alex-all3dp wants to merge 1 commit intoCodeGenieApp:mainlinefrom
alex-all3dp:fix/socket-event-emitter

Conversation

@alex-all3dp
Copy link
Copy Markdown

Problem

@codegenie/serverless-express >= 4.16.0 with Express 5 throws TypeError: ee.on is not a function whenever a request reaches Express's finalhandler (e.g. unmatched routes). Reported in #698; still present in v5.0.0.

Stack from production:

TypeError: ee.on is not a function
    at first (ee-first/index.js:43:10)
    at onSocket (on-finished/index.js:115:16)
    at attachFinishedListener (on-finished/index.js:120:5)
    ...
    at send (finalhandler/index.js:291:3)

Root cause

ServerlessRequest (src/request.js) extends http.IncomingMessage and passes a plain object as the socket. When Express 5's finalhandler reaches an unmatched route and the request body wasn't consumed (typical for GETs), it calls onFinished(req), which uses ee-first to attach error and close listeners on req.socket. The plain object has no .on, so it throws.

Fix

Use a PassThrough stream as the socket. PassThrough inherits the EventEmitter interface via Duplex/Stream, so .on, .once, .emit, and .removeListener are all present. The end/destroy Function.prototype stubs are dropped since PassThrough provides real implementations.

This is the same fix that landed upstream in dougmoscrop/serverless-http#306 (merged Sep 2025), the library this file was forked from per the attribution comment at the top.

Closes #698

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.

Bug Report: ee.on is not a function Error with @codegenie/serverless-express 4.16.0

1 participant