From 3ee138dbe1ddb665e0abd9b7f03478ae249439f3 Mon Sep 17 00:00:00 2001 From: tomstrong64 Date: Tue, 23 Apr 2024 09:21:01 +0100 Subject: [PATCH 1/2] Update formidable --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 8ea21001..24906411 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "debug": "^4.3.4", "fast-safe-stringify": "^2.1.1", "form-data": "^4.0.0", - "formidable": "^2.1.2", + "formidable": "^3.5.1", "methods": "^1.1.2", "mime": "2.6.0", "qs": "^6.11.0", @@ -38,8 +38,8 @@ "@babel/runtime": "^7.20.13", "@commitlint/cli": "17", "@commitlint/config-conventional": "17", - "Base64": "^1.1.0", "babelify": "^10.0.0", + "Base64": "^1.1.0", "basic-auth-connect": "^1.0.0", "body-parser": "^1.20.1", "browserify": "^17.0.0", From b9c78379d5fcba3743bf7c2aaeda1c62958d92ac Mon Sep 17 00:00:00 2001 From: tomstrong64 Date: Tue, 23 Apr 2024 12:25:17 +0100 Subject: [PATCH 2/2] fix: formidable v3 multipart form contents mapped to expected format --- src/node/index.js | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/node/index.js b/src/node/index.js index e6dc73cc..a44357a1 100644 --- a/src/node/index.js +++ b/src/node/index.js @@ -469,7 +469,6 @@ Request.prototype._pipeContinue = function (stream, options) { res.pipe(stream, options); res.once('end', () => this.emit('end')); } - }); return stream; }; @@ -1093,7 +1092,7 @@ Request.prototype._end = function () { parser = exports.parse.image; // It's actually a generic Buffer buffer = true; } else if (multipart) { - const form = formidable(); + const form = formidable.formidable(); parser = form.parse.bind(form); buffer = true; } else if (isBinary(mime)) { @@ -1162,6 +1161,31 @@ Request.prototype._end = function () { } if (parserHandlesEnd) { + if (multipart) { + // formidable v3 always returns an array with the value in it + // so we need to flatten it + if (object) { + for (const key in object) { + const value = object[key]; + if (Array.isArray(value) && value.length === 1) { + object[key] = value[0]; + } else { + object[key] = value; + } + } + } + + if (files) { + for (const key in files) { + const value = files[key]; + if (Array.isArray(value) && value.length === 1) { + files[key] = value[0]; + } else { + files[key] = value; + } + } + } + } this.emit('end'); this.callback(null, this._emitResponse(object, files)); }