From 98f3fbf9451e82ec0e7386a3ad0251311af5452c Mon Sep 17 00:00:00 2001 From: Andrei Dumitrescu <5057797+andreidmt@users.noreply.github.com> Date: Wed, 28 Oct 2020 23:15:08 +0100 Subject: [PATCH] fix: Dont wait for "data" event if .body is already parsed --- src/middleware/req-body.js | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/middleware/req-body.js b/src/middleware/req-body.js index 7989047..500bd42 100644 --- a/src/middleware/req-body.js +++ b/src/middleware/req-body.js @@ -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") @@ -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":