Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce Plugins API, fix tests #545

Merged
merged 28 commits into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
5c5ca3b
feat: introduce Plugins API, fix tests
tunnckoCore Jan 29, 2020
0aab094
chore: update changelog
tunnckoCore Jan 29, 2020
7568d08
chore: lgtm tweaks?
tunnckoCore Jan 29, 2020
7fed544
start plugins tests; update exports
tunnckoCore Jan 29, 2020
aa0cc85
chore(custom-plugins-tests): make it really passing
tunnckoCore Jan 29, 2020
308333d
chore: more tests; add nyc test coverage
tunnckoCore Jan 29, 2020
f9d8cd4
chore: move to GitHub Actions CI
tunnckoCore Jan 29, 2020
a84a0f1
fix: respect form hash option on incoming octect/stream requests
tunnckoCore Jan 29, 2020
fb5ca03
chore: merge updates from master
tunnckoCore Jan 29, 2020
db1e1f0
fix: tests, use dezalgo and once for parse callback
tunnckoCore Jan 30, 2020
b44bcbd
chore: tweaks, publish `dev` dist-tag (with Plugins API)
tunnckoCore Jan 30, 2020
0c7d359
chore: merge master into plugins-api
tunnckoCore Jan 30, 2020
8120b6b
Merge branch 'master' into plugins-api
tunnckoCore Jan 31, 2020
2c399be
Merge branch 'master' into plugins-api
tunnckoCore Jan 31, 2020
d6cd110
chore: sync & tweak
tunnckoCore Jan 31, 2020
2c35e72
fix: check strictly for multipart/form-data in multipart parser
tunnckoCore Jan 31, 2020
6d47def
Merge branch 'master' into plugins-api
tunnckoCore Jan 31, 2020
3ebbbd0
chore: publish dev.2
tunnckoCore Jan 31, 2020
e02c69c
chore: merge
tunnckoCore Jan 31, 2020
3e3a27a
fix: tests updates
tunnckoCore Jan 31, 2020
019937e
Merge branch 'master' into plugins-api
tunnckoCore Jan 31, 2020
bc34459
fix: it seems we support multipart/related too
tunnckoCore Jan 31, 2020
282971d
chore: download badges
tunnckoCore Feb 5, 2020
8cbcc79
Merge branch 'master' into plugins-api
tunnckoCore Feb 6, 2020
db1d69a
Merge branch 'master' into plugins-api
tunnckoCore Feb 12, 2020
81c79fd
Update package.json
tunnckoCore Feb 12, 2020
e531213
chore: sync
tunnckoCore Feb 12, 2020
5b813a5
chore: update npm funding link, tag to canary [skip ci]
tunnckoCore Feb 12, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* fix(tests): include multipart and qs parser unit tests, part of [#415](https://github.com/node-formidable/node-formidable/issues/415)
* fix: reorganize exports + move parsers to `src/parsers/`
* 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))
* 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))
* respect form hash option on incoming octect/stream requests ([#407](https://github.com/node-formidable/node-formidable/pull/407))
* 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))

### v1.2.1 (2018-03-20)
Expand Down
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,68 @@ form.on('data', ({ name, key, value, buffer, start, end, ...more }) => {
});
```

### .use(plugin: Plugin)

A method that allows you to extend the Formidable library. By default we include
4 plugins, which esentially are adapters to plug the different built-in parsers.

**The plugins added by this method are always enabled.**

_See [src/plugins/](./src/plugins/) for more detailed look on default plugins._

The `plugin` param has such signature:

```typescript
function(formidable: Formidable, options: Options): void;
```

The architecture is simple. The `plugin` is a function that is passed with the
Formidable instance (the `form` across the README examples) and the options.

**Note:** the plugin function's `this` context is also the same instance.

```js
const formidable = require('formidable');

const form = formidable({ keepExtensions: true });

form.use((self, options) => {
// self === this === form
console.log('woohoo, custom plugin');
// do your stuff; check `src/plugins` for inspiration
});

form.parse(req, (error, fields, files) => {
console.log('done!');
});
```

**Important to note**, is that inside plugin `this.options`, `self.options` and
`options` MAY or MAY NOT be the same. General best practice is to always use the
`this`, so you can later test your plugin independently and more easily.

If you want to disable some parsing capabilities of Formidable, you can disable
the plugin which corresponds to the parser. For example, if you want to disable
multipart parsing (so the [src/parsers/Multipart.js](./src/parsers/Multipart.js)
which is used in [src/plugins/multipart.js](./src/plugins/multipart.js)), then
you can remove it from the `options.enabledPlugins`, like so

```js
const { Formidable } = require('formidable');

const form = new Formidable({
hash: 'sha1',
enabledPlugins: ['octetstream', 'querystring', 'json'],
});
```

**Be aware** that the order _MAY_ be important too. The names corresponds 1:1 to
files in [src/plugins/](./src/plugins) folder.

Pull requests for new built-in plugins MAY be accepted - for example, more
advanced querystring parser. Add your plugin as a new file in `src/plugins/`
folder (lowercased) and follow how the other plugins are made.

### form.onPart

If you want to use Formidable to only handle certain parts for you, you can do
Expand Down
13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
{
"name": "formidable",
"version": "2.0.0-canary.20200131.1",
"version": "2.0.0-dev.20200131.2",
"license": "MIT",
"description": "A node.js module for parsing form data, especially file uploads.",
"homepage": "https://github.com/node-formidable/node-formidable",
"funding": "https://ko-fi.com/tunnckoCore/commissions",
"repository": "node-formidable/node-formidable",
"main": "./src/index.js",
"files": [
"src"
"src",
"test"
],
"publishConfig": {
"access": "public",
"tag": "canary"
"tag": "dev"
},
"scripts": {
"bench": "node benchmark",
Expand All @@ -26,6 +27,10 @@
"pretest:ci": "yarn pretest",
"test:ci": "nyc node test/run.js"
},
"dependencies": {
"dezalgo": "^1.0.3",
"once": "^1.4.0"
},
"devDependencies": {
"@commitlint/cli": "^8.3.5",
"@commitlint/config-conventional": "^8.3.4",
Expand All @@ -37,11 +42,13 @@
"eslint-plugin-prettier": "^3.1.2",
"husky": "^4.2.1",
"jest": "^25.1.0",
"koa": "^2.11.0",
"lint-staged": "^10.0.6",
"nyc": "^15.0.0",
"prettier": "^1.19.1",
"prettier-plugin-pkgjson": "^0.2.0",
"request": "^2.88.0",
"supertest": "^4.0.2",
"urun": "^0.0.8",
"utest": "^0.0.8"
},
Expand Down