package.json
8.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
{
"_args": [
[
{
"raw": "co-busboy",
"scope": null,
"escapedName": "co-busboy",
"name": "co-busboy",
"rawSpec": "",
"spec": "latest",
"type": "tag"
},
"/Users/fzy/project/koa2_Sequelize_project"
]
],
"_from": "co-busboy@latest",
"_id": "co-busboy@1.3.1",
"_inCache": true,
"_location": "/co-busboy",
"_nodeVersion": "4.1.0",
"_npmUser": {
"name": "fengmk2",
"email": "fengmk2@gmail.com"
},
"_npmVersion": "2.14.3",
"_phantomChildren": {},
"_requested": {
"raw": "co-busboy",
"scope": null,
"escapedName": "co-busboy",
"name": "co-busboy",
"rawSpec": "",
"spec": "latest",
"type": "tag"
},
"_requiredBy": [
"#USER",
"/"
],
"_resolved": "https://registry.npmjs.org/co-busboy/-/co-busboy-1.3.1.tgz",
"_shasum": "4faa511a7bd5535b291fb0aa29663cfeaf672806",
"_shrinkwrap": null,
"_spec": "co-busboy",
"_where": "/Users/fzy/project/koa2_Sequelize_project",
"author": {
"name": "Jonathan Ong",
"email": "me@jongleberry.com",
"url": "http://jongleberry.com"
},
"bugs": {
"url": "https://github.com/cojs/co-busboy/issues"
},
"dependencies": {
"black-hole-stream": "~0.0.1",
"busboy": "^0.2.8",
"chan": "^0.6.1"
},
"description": "Busboy multipart parser as a yieldable",
"devDependencies": {
"co": "*",
"formstream": "~1.0.0",
"istanbul": "*",
"mocha": "*"
},
"directories": {},
"dist": {
"shasum": "4faa511a7bd5535b291fb0aa29663cfeaf672806",
"tarball": "https://registry.npmjs.org/co-busboy/-/co-busboy-1.3.1.tgz"
},
"files": [
"index.js"
],
"gitHead": "2a5976a549c245b84dbf5f20231714e7ec12356e",
"homepage": "https://github.com/cojs/co-busboy#readme",
"license": "MIT",
"maintainers": [
{
"name": "jongleberry",
"email": "jonathanrichardong@gmail.com"
},
{
"name": "eivifj",
"email": "eivind.fjeldstad@gmail.com"
},
{
"name": "fengmk2",
"email": "fengmk2@gmail.com"
},
{
"name": "tjholowaychuk",
"email": "tj@vision-media.ca"
},
{
"name": "dead_horse",
"email": "dead_horse@qq.com"
},
{
"name": "coderhaoxin",
"email": "coderhaoxin@outlook.com"
}
],
"name": "co-busboy",
"optionalDependencies": {},
"readme": "# co busboy\n\n[![NPM version][npm-image]][npm-url]\n[![build status][travis-image]][travis-url]\n[![Test coverage][codecov-image]][codecov-url]\n[![David deps][david-image]][david-url]\n[![npm download][download-image]][download-url]\n\n[npm-image]: https://img.shields.io/npm/v/co-busboy.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/co-busboy\n[travis-image]: https://img.shields.io/travis/cojs/busboy.svg?style=flat-square\n[travis-url]: https://travis-ci.org/cojs/busboy\n[codecov-image]: https://codecov.io/github/cojs/busboy/coverage.svg?branch=master\n[codecov-url]: https://codecov.io/github/cojs/busboy?branch=master\n[david-image]: https://img.shields.io/david/cojs/busboy.svg?style=flat-square\n[david-url]: https://david-dm.org/cojs/busboy\n[download-image]: https://img.shields.io/npm/dm/co-busboy.svg?style=flat-square\n[download-url]: https://npmjs.org/package/co-busboy\n\n[busboy](http://github.com/mscdex/busboy) multipart parser using `co` or `koa`.\n\n## Example\n\n```js\nvar parse = require('co-busboy')\n\napp.use(function* (next) {\n // the body isn't multipart, so busboy can't parse it\n if (!this.request.is('multipart/*')) return yield next\n\n var parts = parse(this)\n var part\n while (part = yield parts) {\n if (part.length) {\n // arrays are busboy fields\n console.log('key: ' + part[0])\n console.log('value: ' + part[1])\n } else {\n // otherwise, it's a stream\n part.pipe(fs.createWriteStream('some file.txt'))\n }\n }\n console.log('and we are done parsing the form!')\n})\n```\n\nNote that parts will be delievered in the order they are defined in the form.\nPut your CSRF token first in the form and your larger files last.\n\nIf you want `co-busboy` to automatically handle the fields,\nset the `autoFields: true` option.\nNow all the parts will be streams and a field object and array will automatically be populated.\n\n```js\nvar parse = require('co-busboy')\n\napp.use(function* (next) {\n var parts = parse(this, {\n autoFields: true\n })\n var part\n while (part = yield parts) {\n // it's a stream\n part.pipe(fs.createWriteStream('some file.txt'))\n }\n console.log('and we are done parsing the form!')\n // .field holds all the fields in key/value form\n console.log(parts.field._csrf)\n // .fields holds all the fields in [key, value] form\n console.log(parts.fields[0])\n})\n```\n\n### Example for csrf check\n\nUse `options.checkField` hook `function(name, val, fieldnameTruncated, valTruncated)`\ncan handle fields check.\n\n```js\nvar parse = require('co-busboy')\n\napp.use(function* (next) {\n var ctx = this\n var parts = parse(this, {\n checkField: function (name, value) {\n if (name === '_csrf' && !checkCSRF(ctx, value)) {\n var err = new Error('invalid csrf token')\n err.status = 400\n return err\n }\n }\n })\n var part\n while (part = yield parts) {\n // ...\n }\n})\n```\n\n### Example for filename extension check\n\nUse `options.checkFile` hook `function(fieldname, file, filename, encoding, mimetype)`\ncan handle filename check.\n\n```js\nvar parse = require('co-busboy')\nvar path = require('path')\n\napp.use(function* (next) {\n var ctx = this\n var parts = parse(this, {\n // only allow upload `.jpg` files\n checkFile: function (fieldname, file, filename) {\n if (path.extname(filename) !== '.jpg') {\n var err = new Error('invalid jpg image')\n err.status = 400\n return err\n }\n }\n })\n var part\n while (part = yield parts) {\n // ...\n }\n})\n```\n\n## API\n\n### parts = parse(stream, [options])\n\n```js\nvar parse = require('co-busboy')\nvar parts = parse(stream, {\n autoFields: true\n})\n```\n\n`options` are passed to [busboy](https://github.com/mscdex/busboy).\nThe only additional option is `autoFields`.\n\n**Note**: If busboy events `partsLimit`, `filesLimit`, `fieldsLimit` is emitted, will throw an error.\n\n### part = yield parts\n\nYield the next part.\nIf `autoFields: true`, this will always be a file stream.\nOtherwise, it will be a [field](https://github.com/mscdex/busboy#busboy-special-events) as an array.\n\n- Readable Stream\n\n - `fieldname`\n - `filename`\n - `transferEncoding` or `encoding`\n - `mimeType` or `mime`\n\n- Field[]\n\n 0. `fieldname`\n 1. `value`\n 2. `valueTruncated` - `Boolean`\n 3. `fieldnameTruncated` - Boolean\n\nIf falsey, then the parser is done.\n\n### parts.field{}\n\nIf `autoFields: true`, this object will be populated with key/value pairs.\n\n### parts.fields[]\n\nIf `autoFields: true`, this array will be populated with all fields.\n\n## License\n\nThe MIT License (MIT)\n\nCopyright (c) 2013 Jonathan Ong me@jongleberry.com\nCopyright (c) 2015 cojs and other contributors\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n",
"readmeFilename": "README.md",
"repository": {
"type": "git",
"url": "git+https://github.com/cojs/co-busboy.git"
},
"scripts": {
"test": "make test",
"test-cov": "make test-cov"
},
"version": "1.3.1"
}