Skip to content

Commit 037a738

Browse files
feat: introduce Plugins API, fix tests (#545)
* feat: introduce Plugins API, fix tests * chore: update changelog * chore: lgtm tweaks? * start plugins tests; update exports * chore(custom-plugins-tests): make it really passing * chore: more tests; add nyc test coverage * chore: move to GitHub Actions CI * fix: respect form hash option on incoming octect/stream requests close #407 * fix: tests, use dezalgo and once for parse callback - it is recommended plugins to return the this/self/formidable instance - add 2 deps: `once` and `dezalgo` for ensurance of the `cb` param of .parse() + otherwise it COULD (and happened already few times) be called multiple times - update unit tests to use Jest * chore: tweaks, publish `dev` dist-tag (with Plugins API) * chore: sync & tweak * fix: check strictly for multipart/form-data in multipart parser * chore: publish dev.2 * fix: tests updates * fix: it seems we support multipart/related too * chore: download badges * Update package.json * chore: sync Signed-off-by: Charlike Mike Reagent <[email protected]> Co-authored-by: Claudio Poli <[email protected]>
1 parent d413c13 commit 037a738

23 files changed

+1116
-317
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
* fix(tests): include multipart and qs parser unit tests, part of [#415](https://github.com/node-formidable/node-formidable/issues/415)
2121
* fix: reorganize exports + move parsers to `src/parsers/`
2222
* fix: update docs and examples [#544](https://github.com/node-formidable/node-formidable/pull/544) ([#248](https://github.com/node-formidable/node-formidable/issues/248), [#335](https://github.com/node-formidable/node-formidable/issues/335), [#371](https://github.com/node-formidable/node-formidable/issues/371), [#372](https://github.com/node-formidable/node-formidable/issues/372), [#387](https://github.com/node-formidable/node-formidable/issues/387), partly [#471](https://github.com/node-formidable/node-formidable/issues/471), [#535](https://github.com/node-formidable/node-formidable/issues/535))
23+
* feat: introduce Plugins API, fix silent failing tests ([#545](https://github.com/node-formidable/node-formidable/pull/545), [#391](https://github.com/node-formidable/node-formidable/pull/391), [#407](https://github.com/node-formidable/node-formidable/pull/407), [#386](https://github.com/node-formidable/node-formidable/pull/386), [#374](https://github.com/node-formidable/node-formidable/pull/374), [#521](https://github.com/node-formidable/node-formidable/pull/521), [#267](https://github.com/node-formidable/node-formidable/pull/267))
24+
* respect form hash option on incoming octect/stream requests ([#407](https://github.com/node-formidable/node-formidable/pull/407))
2325
* fix: exposing file writable stream errors ([#520](https://github.com/node-formidable/node-formidable/pull/520), [#316](https://github.com/node-formidable/node-formidable/pull/316), [#469](https://github.com/node-formidable/node-formidable/pull/469), [#470](https://github.com/node-formidable/node-formidable/pull/470))
2426

2527
### v1.2.1 (2018-03-20)

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,68 @@ form.on('data', ({ name, key, value, buffer, start, end, ...more }) => {
290290
});
291291
```
292292

293+
### .use(plugin: Plugin)
294+
295+
A method that allows you to extend the Formidable library. By default we include
296+
4 plugins, which esentially are adapters to plug the different built-in parsers.
297+
298+
**The plugins added by this method are always enabled.**
299+
300+
_See [src/plugins/](./src/plugins/) for more detailed look on default plugins._
301+
302+
The `plugin` param has such signature:
303+
304+
```typescript
305+
function(formidable: Formidable, options: Options): void;
306+
```
307+
308+
The architecture is simple. The `plugin` is a function that is passed with the
309+
Formidable instance (the `form` across the README examples) and the options.
310+
311+
**Note:** the plugin function's `this` context is also the same instance.
312+
313+
```js
314+
const formidable = require('formidable');
315+
316+
const form = formidable({ keepExtensions: true });
317+
318+
form.use((self, options) => {
319+
// self === this === form
320+
console.log('woohoo, custom plugin');
321+
// do your stuff; check `src/plugins` for inspiration
322+
});
323+
324+
form.parse(req, (error, fields, files) => {
325+
console.log('done!');
326+
});
327+
```
328+
329+
**Important to note**, is that inside plugin `this.options`, `self.options` and
330+
`options` MAY or MAY NOT be the same. General best practice is to always use the
331+
`this`, so you can later test your plugin independently and more easily.
332+
333+
If you want to disable some parsing capabilities of Formidable, you can disable
334+
the plugin which corresponds to the parser. For example, if you want to disable
335+
multipart parsing (so the [src/parsers/Multipart.js](./src/parsers/Multipart.js)
336+
which is used in [src/plugins/multipart.js](./src/plugins/multipart.js)), then
337+
you can remove it from the `options.enabledPlugins`, like so
338+
339+
```js
340+
const { Formidable } = require('formidable');
341+
342+
const form = new Formidable({
343+
hash: 'sha1',
344+
enabledPlugins: ['octetstream', 'querystring', 'json'],
345+
});
346+
```
347+
348+
**Be aware** that the order _MAY_ be important too. The names corresponds 1:1 to
349+
files in [src/plugins/](./src/plugins) folder.
350+
351+
Pull requests for new built-in plugins MAY be accepted - for example, more
352+
advanced querystring parser. Add your plugin as a new file in `src/plugins/`
353+
folder (lowercased) and follow how the other plugins are made.
354+
293355
### form.onPart
294356

295357
If you want to use Formidable to only handle certain parts for you, you can do
@@ -509,4 +571,5 @@ Formidable is licensed under the [MIT License][license-url].
509571
[npm-monthly-img]: https://badgen.net/npm/dm/formidable?icon=npm&cache=300
510572
[npm-yearly-img]: https://badgen.net/npm/dy/formidable?icon=npm&cache=300
511573
[npm-alltime-img]: https://badgen.net/npm/dt/formidable?icon=npm&cache=300
574+
512575
<!-- prettier-ignore-end -->

package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
"license": "MIT",
55
"description": "A node.js module for parsing form data, especially file uploads.",
66
"homepage": "https://github.com/node-formidable/node-formidable",
7-
"funding": "https://ko-fi.com/tunnckoCore/commissions",
7+
"funding": "https://tidelift.com/funding/github/npm/formidable",
88
"repository": "node-formidable/node-formidable",
99
"main": "./src/index.js",
1010
"files": [
11-
"src"
11+
"src",
12+
"test"
1213
],
1314
"publishConfig": {
1415
"access": "public",
@@ -26,6 +27,10 @@
2627
"pretest:ci": "yarn pretest",
2728
"test:ci": "nyc node test/run.js"
2829
},
30+
"dependencies": {
31+
"dezalgo": "^1.0.3",
32+
"once": "^1.4.0"
33+
},
2934
"devDependencies": {
3035
"@commitlint/cli": "^8.3.5",
3136
"@commitlint/config-conventional": "^8.3.4",
@@ -37,11 +42,13 @@
3742
"eslint-plugin-prettier": "^3.1.2",
3843
"husky": "^4.2.2",
3944
"jest": "^25.1.0",
45+
"koa": "^2.11.0",
4046
"lint-staged": "^10.0.7",
4147
"nyc": "^15.0.0",
4248
"prettier": "^1.19.1",
4349
"prettier-plugin-pkgjson": "^0.2.3",
4450
"request": "^2.88.2",
51+
"supertest": "^4.0.2",
4552
"urun": "^0.0.8",
4653
"utest": "^0.0.8"
4754
},

0 commit comments

Comments
 (0)