Skip to content

Commit

Permalink
refactor: Plugins return again (#820)
Browse files Browse the repository at this point in the history
* refactor: plugins return again

* docs: update changelog and version
  • Loading branch information
GrosSacASac committed Feb 3, 2022
1 parent 2815e91 commit accd1f6
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog


### 3.2.2

* refactor: ([#801](https://github.com/node-formidable/formidable/pull/801))

### 3.2.1


Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
10 changes: 7 additions & 3 deletions src/Formidable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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') {
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/plugins/multipart.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/plugins/octetstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/plugins/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit accd1f6

Please sign in to comment.