Skip to content

Commit

Permalink
fix: Dont wait for "data" event if .body is already parsed
Browse files Browse the repository at this point in the history
  • Loading branch information
andreidmt committed Oct 28, 2020
1 parent e4d312e commit 98f3fbf
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/middleware/req-body.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const cuid = require("cuid")
const Busboy = require("busboy")
const slugify = require("@sindresorhus/slugify")
const { tmpdir } = require("os")
const { pipe, isEmpty } = require("@asd14/m")
const { pipe, is, isEmpty, clone } = require("@asd14/m")
const { createWriteStream } = require("fs")
const { extname, basename, join } = require("path")

Expand Down Expand Up @@ -59,15 +59,23 @@ const handleForm = (req, { onParse, onError }) => {

module.exports = ({ QueryParser }) => (req, res, next) => {
switch (req.headers["x-content-type"]) {
//
case "application/json":
return handleText(req, {
onParse: source => {
req.ctx.body = isEmpty(source) ? {} : JSON.parse(source)
next()
},
onError: error =>
next(new InputError("Invalid JSON string in body", error)),
})
if (is(req.body)) {
req.ctx.body = clone(req.body)
next()
} else {
handleText(req, {
onParse: source => {
req.ctx.body = isEmpty(source) ? {} : JSON.parse(source)
next()
},
onError: error =>
next(new InputError("Invalid JSON string in body", error)),
})
}

break

//
case "application/x-www-form-urlencoded":
Expand Down

0 comments on commit 98f3fbf

Please sign in to comment.