From 6f29d8bfa6a84b2f299c8eaac1f4bf30078277e3 Mon Sep 17 00:00:00 2001 From: Matthew Lin Date: Wed, 27 Aug 2025 14:20:39 -0700 Subject: [PATCH] Make ServerlessRequest readable --- lib/request.js | 15 +++++++++------ test/spec.js | 5 +++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/request.js b/lib/request.js index 1472758..bf2bef9 100644 --- a/lib/request.js +++ b/lib/request.js @@ -1,18 +1,22 @@ 'use strict'; const http = require('http'); +const stream = require('stream'); module.exports = class ServerlessRequest extends http.IncomingMessage { constructor({ method, url, headers, body, remoteAddress }) { - super({ + const socket = new stream.Readable({ read: Function.prototype }); + + Object.assign(socket, { encrypted: true, - readable: false, remoteAddress, address: () => ({ port: 443 }), end: Function.prototype, destroy: Function.prototype }); + super(socket); + if (typeof headers['content-length'] === 'undefined') { headers['content-length'] = Buffer.byteLength(body); } @@ -29,10 +33,9 @@ module.exports = class ServerlessRequest extends http.IncomingMessage { url, }); - this._read = () => { - this.push(body); - this.push(null); - }; + this._read = Function.prototype; + this.push(body); + this.push(null); } } diff --git a/test/spec.js b/test/spec.js index 5828339..eb3ef0e 100644 --- a/test/spec.js +++ b/test/spec.js @@ -62,6 +62,7 @@ describe('spec', () => { called = true; res.end(''); }); + req.resume(); }); await expect(handler(null)).to.be.fulfilled; @@ -300,6 +301,10 @@ describe('spec', () => { delete requestV2.requestContext; delete requestV1.apiGateway; delete requestV2.apiGateway; + requestV1._readableState.buffer = []; + requestV1._readableState.length = 0; + requestV2._readableState.buffer = []; + requestV2._readableState.length = 0; expect(JSON.stringify(requestV1)).to.equal(JSON.stringify(requestV2)); });