From d742086226f980787f6340a50627000e044a5cbc Mon Sep 17 00:00:00 2001 From: Cyril Walle Date: Wed, 26 Jan 2022 23:21:01 +0100 Subject: [PATCH 1/2] refactor: plugins return again --- src/Formidable.js | 10 +++++++--- src/plugins/json.js | 2 ++ src/plugins/multipart.js | 1 + src/plugins/octetstream.js | 1 + src/plugins/querystring.js | 1 + 5 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/Formidable.js b/src/Formidable.js index 4a03d667..8512f373 100644 --- a/src/Formidable.js +++ b/src/Formidable.js @@ -407,9 +407,11 @@ class IncomingForm extends EventEmitter { new DummyParser(this, this.options); - this._plugins.forEach((plugin, idx) => { + const results = []; + this._plugins.forEach((plugin, idx) => { + let pluginReturn = null; try { - plugin(this, this.options) || this; + pluginReturn = plugin(this, this.options) || this; } catch (err) { // directly throw from the `form.parse` method; // there is no other better way, except a handle through options @@ -421,10 +423,12 @@ class IncomingForm extends EventEmitter { error.idx = idx; throw error; } + Object.assign(this, pluginReturn); // todo: use Set/Map and pass plugin name instead of the `idx` index - this.emit('plugin', idx); + this.emit('plugin', idx, pluginReturn); }); + this.emit('pluginsResults', results); } _error(err, eventName = 'error') { diff --git a/src/plugins/json.js b/src/plugins/json.js index c8d623db..7155ad74 100644 --- a/src/plugins/json.js +++ b/src/plugins/json.js @@ -14,6 +14,8 @@ export default function plugin(formidable, options) { if (/json/i.test(self.headers['content-type'])) { init.call(self, self, options); } + + return self; }; // Note that it's a good practice (but it's up to you) to use the `this.options` instead diff --git a/src/plugins/multipart.js b/src/plugins/multipart.js index 1df84c33..dfb54651 100644 --- a/src/plugins/multipart.js +++ b/src/plugins/multipart.js @@ -33,6 +33,7 @@ export default function plugin(formidable, options) { self._error(err); } } + return self; } // Note that it's a good practice (but it's up to you) to use the `this.options` instead diff --git a/src/plugins/octetstream.js b/src/plugins/octetstream.js index 8c93ba82..33c23291 100644 --- a/src/plugins/octetstream.js +++ b/src/plugins/octetstream.js @@ -14,6 +14,7 @@ export default function plugin(formidable, options) { if (/octet-stream/i.test(self.headers['content-type'])) { init.call(self, self, options); } + return self; } // Note that it's a good practice (but it's up to you) to use the `this.options` instead diff --git a/src/plugins/querystring.js b/src/plugins/querystring.js index fcb28552..ef435aa7 100644 --- a/src/plugins/querystring.js +++ b/src/plugins/querystring.js @@ -15,6 +15,7 @@ export default function plugin(formidable, options) { if (/urlencoded/i.test(self.headers['content-type'])) { init.call(self, self, options); } + return self; }; // Note that it's a good practice (but it's up to you) to use the `this.options` instead From 938706ca44c403c8b6eca0de4d72ff9ce33fe4c7 Mon Sep 17 00:00:00 2001 From: Cyril Walle Date: Wed, 26 Jan 2022 23:21:16 +0100 Subject: [PATCH 2/2] docs: update changelog and version --- CHANGELOG.md | 4 ++++ package.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4681b3bd..acc1ce59 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Changelog +### 3.2.2 + + * refactor: ([#801](https://github.com/node-formidable/formidable/pull/801)) + ### 3.2.1 diff --git a/package.json b/package.json index 4164d0c4..0828d103 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "formidable", - "version": "3.2.1", + "version": "3.2.2", "license": "MIT", "description": "A node.js module for parsing form data, especially file uploads.", "homepage": "https://github.com/node-formidable/formidable",