You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The #692 PR introduced a bug when using with express and an async middleware: the IncomingMessage/Request (stream) is marked as not readable and fails when used with body-parser (for example).
Affected version: 4.16.0
Express version: 4 (didn't test it on 5)
Body parser version: 1.20.3
For the code attached, running with 4.15.0 yields:
{
statusCode: 500,
body: '<!DOCTYPE html>\n' +
'<html lang="en">\n' +
'<head>\n' +
'<meta charset="utf-8">\n' +
'<title>Error</title>\n' +
'</head>\n' +
'<body>\n' +
'<pre>InternalServerError: stream is not readable<br> at readStream (node_modules/raw-body/index.js:185:17)<br> at getRawBody ... (same as below)' +
'</body>\n' +
'</html>\n',
isBase64Encoded: false,
headers: {
'x-powered-by': 'Express',
'content-security-policy': "default-src 'none'",
'x-content-type-options': 'nosniff',
'content-type': 'text/html; charset=utf-8',
'content-length': 1262
}
}
InternalServerError: stream is not readable
at readStream (node_modules/raw-body/index.js:185:17)
at getRawBody (node_modules/raw-body/index.js:116:12)
at read (node_modules/body-parser/lib/read.js:79:3)
at jsonParser (node_modules/body-parser/lib/types/json.js:138:5)
at Layer.handle [as handle_request] (node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (node_modules/express/lib/router/index.js:328:13)
at node_modules/express/lib/router/index.js:286:9
at Function.process_params (node_modules/express/lib/router/index.js:346:12)
at next (node_modules/express/lib/router/index.js:280:10)
at src/example.ts:10:3
Code to reproduce:
importserverlessExpressfrom'@codegenie/serverless-express';importexpressfrom'express';importbodyParserfrom'body-parser';import{promisify}from'node:util';constapp=express();app.use(async(req,res,next)=>{// Remove this line to "remove the problem"awaitpromisify(setTimeout)(1000);next();});app.use(bodyParser.json());app.use((req,res)=>{res.send('Hello World!');});constserverlessExpressInstance=serverlessExpress({ app });exportasyncfunctionhandler(event: any,context: any,callback: any){returnserverlessExpressInstance(event,context,callback);}handler({version: '2.0',rawPath: '/path/to/resource',headers: {"Content-Type": "application/json","Content-Length": "2",},requestContext: {http: {method: 'POST',path: '/path/to/resource',protocol: 'HTTP/1.1',sourceIp: '192.168.0.1/32',userAgent: 'agent',},},body: "{}",isBase64Encoded: false,},{}asany,()=>{},).then(console.log.bind(console)).catch(console.error.bind(console));
I believe this is the culprit. Express doesn't return a promise from the handle function, so if there's a middleware that starts an async operation the function will return and the stream will be marked as not readable.
awaitframework.sendRequest({ app, request, response })markHttpRequestAsCompleted(request)// <-- maybe this should be moved down to after the the stream completes?awaitwaitForStreamComplete(response)
Small note, initially I had trouble finding this because I thought we were actually using vendia's serverless-express version. After fiddling with it for a couple of hours I realized it was actually "exporting" codegenie's version. So leaving this note here, in case someone else thinks they're using vendia's.
The text was updated successfully, but these errors were encountered:
The #692 PR introduced a bug when using with express and an async middleware: the IncomingMessage/Request (stream) is marked as not readable and fails when used with body-parser (for example).
Affected version: 4.16.0
Express version: 4 (didn't test it on 5)
Body parser version: 1.20.3
For the code attached, running with 4.15.0 yields:
Running with 4.16.0 yields:
Code to reproduce:
I believe this is the culprit. Express doesn't return a promise from the handle function, so if there's a middleware that starts an async operation the function will return and the stream will be marked as not readable.
Small note, initially I had trouble finding this because I thought we were actually using vendia's serverless-express version. After fiddling with it for a couple of hours I realized it was actually "exporting" codegenie's version. So leaving this note here, in case someone else thinks they're using vendia's.
The text was updated successfully, but these errors were encountered: