diff --git a/index.js b/index.js index c051ea9..77119d3 100644 --- a/index.js +++ b/index.js @@ -21,12 +21,13 @@ const formDataParser = async (instance, options) => { const bus = busboy({ headers: message.headers, limits, defParamCharset: "utf8" }); bus.on("file", (name, stream, info) => { results.push(storage.process(name, stream, info)); - if(!body[name]) { + const fileProp = body[name]; + if (!fileProp) { body[name] = JSON.stringify(info); - } else if (body[name] && !Array.isArray(body[name])) { - body[name] = [body[name], JSON.stringify(info)]; + } else if (!Array.isArray(fileProp)) { + fileProp = [fileProp, JSON.stringify(info)]; } else { - body[name].push(JSON.stringify(info)); + fileProp.push(JSON.stringify(info)); } }); bus.on("field", (name, value) => { @@ -48,12 +49,13 @@ const formDataParser = async (instance, options) => { for (const file of files) { const field = file.field; delete file.field; - if (!newBody[field]) { + const fileProp = newBody[field]; + if (!fileProp) { newBody[field] = file; - } else if (newBody[field] && !Array.isArray(newBody[field])) { - newBody[field] = [newBody[field], file]; + } else if (!Array.isArray(fileProp)) { + fileProp = [fileProp, file]; } else { - newBody[field].push(file); + fileProp.push(file); } } Object.assign(body, newBody); diff --git a/package-lock.json b/package-lock.json index d04af25..210261b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "formzilla", - "version": "3.2.5", + "version": "3.2.6", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "formzilla", - "version": "3.2.5", + "version": "3.2.6", "license": "ISC", "dependencies": { "busboy": "^1.6.0", diff --git a/package.json b/package.json index 421e887..f8fdbbc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "formzilla", - "version": "3.2.5", + "version": "3.2.6", "description": "Fastify plugin for parsing multipart/form data", "main": "index.js", "scripts": { diff --git a/test/multifile.js b/test/multifile.js index bc24cb0..835c548 100644 --- a/test/multifile.js +++ b/test/multifile.js @@ -9,9 +9,8 @@ const http = require("http"); const path = require("path"); const fs = require("fs"); - async function sendRequest(instance) { - const form = new formData(); + const form = new formData(); const req = http.request({ protocol: "http:", hostname: "localhost", @@ -23,10 +22,10 @@ async function sendRequest(instance) { const filePath = path.join(__dirname, "chequer.png"); form.append("file", fs.createReadStream(filePath)); form.append("files", fs.createReadStream(filePath)); - form.append("files", fs.createReadStream(filePath)); - form.append("files", fs.createReadStream(filePath)); + form.append("files", fs.createReadStream(filePath)); + form.append("files", fs.createReadStream(filePath)); return form.pipe(req); -}; +} tap.test("should allow multiple files in one field", async t => { const instance = require("fastify").fastify(); @@ -38,11 +37,11 @@ tap.test("should allow multiple files in one field", async t => { const requestBody = request.body; t.isArray(requestBody.files); t.ok(requestBody.files[0].stream instanceof Readable); - t.ok(requestBody.files[1].stream instanceof Readable); + t.ok(requestBody.files[1].stream instanceof Readable); t.equal(reply.statusCode, 200); }); await setup(instance, undefined, false); - const req = await sendRequest(instance); + const req = await sendRequest(instance); const [res] = await once(req, "response"); res.resume(); } catch (err) { diff --git a/test/no-schema.js b/test/no-schema.js index e35122d..8839add 100644 --- a/test/no-schema.js +++ b/test/no-schema.js @@ -25,4 +25,4 @@ tap.test("should parse fields as strings when there is no schema", async t => { } catch (err) { console.log(err); } -}); +}); \ No newline at end of file diff --git a/test/setup-multifile.js b/test/setup-multifile.js index fb58cfa..72d0298 100644 --- a/test/setup-multifile.js +++ b/test/setup-multifile.js @@ -12,12 +12,12 @@ const requestSchema = { format: "binary" }, files: { - type: "array", - items: { - type: "string", - format: "binary" - } - }, + type: "array", + items: { + type: "string", + format: "binary" + } + } } } };