Comments (3)
I have to read the entire body into memory before passing it to msgpack.decode, which seems inefficient for large bodies.
Correct. Why don't you use createDecodeStream
?
A sample code below is not tested however.
function parseMsgpackBody(req, res, next) {
if(req.headers['content-type'] === 'application/msgpack') {
var decoder = msgpack.createDecodeStream();
req.pipe(decoder).once("data", function() {
req.body = data;
next();
}).once("error", function(err) {
res.sendStatus(400);
});
return;
}
// call the next middleware
next()
}
from msgpack-lite.
It looks like that will only read the first MessagePack object from the body, which should work for conforming clients. I suppose if I wanted to restrict the API to reject bodies with more than one MessagePack object, I could detect if there is a second one and reject the request.
function parseMsgpackBody(req, res, next) {
// only parse when Content-Type: application/msgpack
if(req.headers['content-type'] === 'application/msgpack') {
// stream decode the message rather than loading all data
// into memory then decoding the buffer
const decoder = msgpack.createDecodeStream()
// track whether we've read one msgpack object
let messageDecoded = false
req.pipe(decoder).on('data', function(data) {
// if we've already read a msgpack object, reject the request
if(messageDecoded) {
res.sendStatus(400)
}
// set the request's body property to the decoded msgpack object
req.body = data
messageDecoded = true
}).on('error', function(err) {
res.sendStatus(400) // invalid msgpack object
}).on('end', function() {
// when the entire stream has been read, call the next middleware
next()
})
} else {
next()
}
}
const app = express()
app.use(parseMsgPackBody)
Thanks for the clarification! I knew about createDecodeStream
, but I didn't think about using it to decode only one object and reject reading a second object.
from msgpack-lite.
req.pipe(decoder).on('data', function(data) {
// if we've already read a msgpack object, reject the request
if(req.body) {
err=400;
res.sendStatus(400)
return
}
// set the request's body property to the decoded msgpack object
req.body = data
messageDecoded = true
}).on('error', function(err) {
err=400;
res.sendStatus(400) // invalid msgpack object
}).on('end', function() {
// when the entire stream has been read, call the next middleware
if (!messageDecoded) res.sendStatus(400) // empty request
if (!err) next()
})
from msgpack-lite.
Related Issues (20)
- Require cycle: node_modules/msgpack-lite/lib... HOT 1
- question: how would I en/decode js set type?
- usemaps gives undefined instead
- docs: msgpack-lite is much more efficient for large strings HOT 1
- decode only first item HOT 1
- here is the CDN:
- how to decode 4 digit buffers. HOT 1
- Slow moving, would funding help? HOT 1
- es modules for modern apps
- better error message - BUFFER_SHORTAGE HOT 2
- Is it possible for msgpack-lite to decode data as it is streamed in?
- use native array.isArray instead
- Report amount of decoded data HOT 1
- Discrepancy for binary keys between browser and nodejs
- Compression issues with large Inputs HOT 4
- Unable to serialise maps and UInt8Arrays at the same time HOT 3
- BigInt / BigInt64Array / BigUInt64Array HOT 2
- encoding number types should not use both Int and Float families
- [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead. HOT 2
- Some tests fails when minified by [email protected] HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from msgpack-lite.