Skip to content

Commit

Permalink
Refactor code; update package version to 3.2.6
Browse files Browse the repository at this point in the history
  • Loading branch information
FlameWolf committed Apr 8, 2024
1 parent 741a9ae commit d8b5b7a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 25 deletions.
18 changes: 10 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
13 changes: 6 additions & 7 deletions test/multifile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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();
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion test/no-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ tap.test("should parse fields as strings when there is no schema", async t => {
} catch (err) {
console.log(err);
}
});
});
12 changes: 6 additions & 6 deletions test/setup-multifile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ const requestSchema = {
format: "binary"
},
files: {
type: "array",
items: {
type: "string",
format: "binary"
}
},
type: "array",
items: {
type: "string",
format: "binary"
}
}
}
}
};
Expand Down

0 comments on commit d8b5b7a

Please sign in to comment.