qwgit / lib / parse-params.js
http{/,s} git server
git clone http://git.nthia.dev/qwgit

let { Writable } = require('stream')

module.exports = function parseParams(max, cb) {
  let buffers = [], bytes = 0
  return new Writable({
    write(buf, enc, next) {
      bytes += buf.length
      if (bytes > max) {
        cb(new Error(`params exceed maximum size (${max} bytes)`))
        buffers = null
        this.destroy()
      } else {
        buffers.push(buf)
        next()
      }
    },
    final(next) {
      cb(null, new URLSearchParams(Buffer.concat(buffers).toString()))
      next()
    },
  })
}