Coder Social home page Coder Social logo

Comments (3)

kawanet avatar kawanet commented on September 4, 2024

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.

jordanbtucker avatar jordanbtucker commented on September 4, 2024

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.

kawanet avatar kawanet commented on September 4, 2024
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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.