h2non / rocky

Full-featured, middleware-oriented, programmatic HTTP and WebSocket proxy for node.js (deprecated)
MIT License
371 stars 24 forks source link

Using 'body-parser' middleware causes TypeError #102

Closed ghost closed 7 years ago

ghost commented 7 years ago

Using the rocky proxy with body-parser seems to cause a strange issue. On a simple proxy.get(...) scenario, the following error occurs, IIF body-parser middleware has been included in the express app, or the proxy, as in:

//app.js //... app.use(bodyParser.json()); app.use(bodyParser.urlencoded({extended: false}));

//console upon performing a GET TypeError: Invalid non-string/buffer chunk at chunkInvalid (_stream_readable.js:380:10) at readableAddChunk (_stream_readable.js:125:12) at Readable.push (_stream_readable.js:111:10) at createBodyStream (/usr/src/app/node_modules/rocky/lib/protocols/http/passes/forward.js:165:10) at useRequestBody (/usr/src/app/node_modules/rocky/lib/protocols/http/passes/forward.js:143:19) at forwardRequest (/usr/src/app/node_modules/rocky/lib/protocols/http/passes/forward.js:118:3) at forwardStrategy (/usr/src/app/node_modules/rocky/lib/protocols/http/passes/forward.js:58:5) at forwarder (/usr/src/app/node_modules/rocky/lib/protocols/http/passes/forward.js:40:5) at next (/usr/src/app/node_modules/midware/midware.js:53:26) at run (/usr/src/app/node_modules/midware-pool/lib/pool.js:84:29)

Simply removing the body-parser middleware prevents the error. I'm pretty green, and I don't even know if I'm going to need body-parser middleware, but I was curious as to why this would occur.

Thanks!

ghost commented 7 years ago

I'm encountering this issue because I'm using passport-saml middleware to attempt to authenticate the REST endpoints. Requiring authentication and accessing routes handled directly by Express work fine; if I access a route that should forward somewhere using Rocky, the request body is "{}" by the time it gets to 'forwarder'.

ghost commented 7 years ago

I was able to workaround this, but I need some idea that it's not a crazy fix. In forward.js:useRequestBody(...) I've added a check to see that req.body isn't empty.

// If body is present, use it as buffer stream if (body && Object.keys(body).length !== 0) { opts.buffer = createBodyStream(body) }

ghost commented 7 years ago

This issue is fixed as of pull request #104. Thanks @h2non!!!