From 6edaa4bb6cfb40bffefbdcc540b439a63e267186 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 6 May 2026 12:09:47 +0200 Subject: [PATCH] fix(request): use PassThrough as socket for IncomingMessage --- __tests__/unit.js | 8 ++++++++ src/request.js | 16 ++++++++-------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/__tests__/unit.js b/__tests__/unit.js index b4938cb9..a8c6bfda 100644 --- a/__tests__/unit.js +++ b/__tests__/unit.js @@ -168,6 +168,14 @@ test('getRequestResponse: without headers', async () => { }) }) +test('ServerlessRequest socket exposes EventEmitter methods', async () => { + const { request } = await getReqRes() + expect(typeof request.socket.on).toBe('function') + expect(typeof request.socket.once).toBe('function') + expect(typeof request.socket.emit).toBe('function') + expect(typeof request.socket.removeListener).toBe('function') +}) + describe('respondToEventSourceWithError', () => { test('responds with 500 status', () => { return new Promise( diff --git a/src/request.js b/src/request.js index 5d3484ec..afa599c1 100644 --- a/src/request.js +++ b/src/request.js @@ -1,19 +1,19 @@ // ATTRIBUTION: https://github.com/dougmoscrop/serverless-http const http = require('http') +const { PassThrough } = require('stream') const HTTPS_PORT = 443 module.exports = class ServerlessRequest extends http.IncomingMessage { constructor ({ method, url, headers, body, remoteAddress }) { - super({ - encrypted: true, - readable: true, - remoteAddress, - address: () => ({ port: HTTPS_PORT }), - end: Function.prototype, - destroy: Function.prototype - }) + const socket = new PassThrough() + socket.encrypted = true + socket.readable = true + socket.remoteAddress = remoteAddress + socket.address = () => ({ port: HTTPS_PORT }) + + super(socket) // IncomingMessage has a lot of logic for when to lowercase or alias well-known header names, // so we delegate to that logic here