then-busboy
Advanced tools
Comparing version 2.0.0-beta.10 to 2.0.0
@@ -44,2 +44,4 @@ "use strict"; | ||
* @constructor | ||
* | ||
* @api private | ||
*/ | ||
@@ -97,2 +99,5 @@ constructor(options) { | ||
this.__path = (0, _path.join)((0, _os.tmpdir)(), `${(0, _nanoid2.default)()}_${this.filename}`); | ||
this.toJSON = this.toJSON.bind(this); | ||
this.inspect = this.inspect.bind(this); | ||
} | ||
@@ -149,4 +154,9 @@ | ||
// TODO: Add a test for this method. | ||
toJSON() { | ||
return this.inspect(); | ||
} | ||
inspect() { | ||
return `[File: ${this.filename}]`; | ||
return `<File: ${this.filename}>`; | ||
} | ||
@@ -153,0 +163,0 @@ } |
@@ -17,2 +17,5 @@ "use strict"; | ||
/** | ||
* @api private | ||
*/ | ||
const onField = (options, cb) => (fieldname, value) => { | ||
@@ -19,0 +22,0 @@ try { |
@@ -19,2 +19,7 @@ "use strict"; | ||
/** | ||
* Get a file from part and push it to entries array | ||
* | ||
* @api private | ||
*/ | ||
const onFile = (options, cb) => (fieldname, stream, filename, enc, mime) => { | ||
@@ -28,5 +33,5 @@ try { | ||
const onData = ch => contents.push(ch); | ||
const onData = ch => void contents.push(ch); | ||
const onEnd = () => { | ||
function onEnd() { | ||
contents.push(null); | ||
@@ -37,5 +42,7 @@ | ||
cb(null, [path, file]); | ||
}; | ||
} | ||
stream.on("data", onData).on("end", onEnd); | ||
// Busboy doesn't emit an "end" event while file stream have not been read. | ||
// So, we just read the stream and push it to the other [Readable] one. o_O | ||
stream.on("error", cb).on("data", onData).on("end", onEnd); | ||
} catch (err) { | ||
@@ -42,0 +49,0 @@ return cb(err); |
@@ -61,2 +61,4 @@ "use strict"; | ||
* @return {Promise<Object>} | ||
* | ||
* @api public | ||
*/ | ||
@@ -63,0 +65,0 @@ };const thenBusboy = (request, options = {}) => new Promise((resolve, reject) => { |
@@ -30,5 +30,6 @@ "use strict"; | ||
* | ||
* @return {array<string|number>} | ||
* @return {Array<string|number>} | ||
* | ||
* @throws {TypeError} | ||
* @throws {TypeError} when given fieldname is not a string | ||
* @throws {Error} on unexpected fieldname format | ||
* | ||
@@ -35,0 +36,0 @@ * @example |
@@ -8,2 +8,11 @@ "use strict"; | ||
/** | ||
* Get a string with type name of the given value | ||
* | ||
* @param {any} val | ||
* | ||
* @return {string} | ||
* | ||
* @api private | ||
*/ | ||
function getType(val) { | ||
@@ -10,0 +19,0 @@ const type = Object.prototype.toString.call(val).slice(8, -1); |
@@ -13,2 +13,11 @@ "use strict"; | ||
/** | ||
* Check if given values is a File | ||
* | ||
* @param {any} val | ||
* | ||
* @return {boolean} | ||
* | ||
* @api public | ||
*/ | ||
const isFile = val => val instanceof _File2.default; | ||
@@ -15,0 +24,0 @@ |
@@ -6,2 +6,11 @@ "use strict"; | ||
}); | ||
/** | ||
* Check if given values is NaN | ||
* | ||
* @param {any} val | ||
* | ||
* @return {boolean} | ||
* | ||
* @api private | ||
*/ | ||
const isNaN = val => Number.isNaN(Number(val)); | ||
@@ -8,0 +17,0 @@ |
@@ -8,2 +8,5 @@ "use strict"; | ||
/** | ||
* @api private | ||
*/ | ||
const leadToLowerCase = str => `${toLowerCase(str.charAt(0))}${str.slice(1)}`; | ||
@@ -10,0 +13,0 @@ |
@@ -19,14 +19,14 @@ "use strict"; | ||
* @param {object} listeners | ||
* @param {function} callee | ||
* @param {function} fn | ||
* | ||
* @api private | ||
*/ | ||
function mapListeners(listeners, callee) { | ||
function mapListeners(listeners, fn) { | ||
const res = {}; | ||
for (const [key, fn] of entries(listeners)) { | ||
for (const [key, value] of entries(listeners)) { | ||
const name = key.startsWith("on") ? (0, _leadToLowerCase2.default)(key.slice(2)) : key; | ||
if (!["error", "end"].includes(name)) { | ||
res[name] = callee(fn, name); | ||
res[name] = fn(value, name); | ||
} | ||
@@ -33,0 +33,0 @@ } |
@@ -19,2 +19,4 @@ "use strict"; | ||
* @return {any} | ||
* | ||
* @api private | ||
*/ | ||
@@ -21,0 +23,0 @@ function restoreType(value) { |
@@ -6,3 +6,3 @@ "use strict"; | ||
}); | ||
exports.File = exports.isFile = exports.default = undefined; | ||
exports.isFile = exports.default = undefined; | ||
@@ -17,6 +17,2 @@ var _thenBusboy = require("./lib/then-busboy"); | ||
var _File2 = require("./lib/File"); | ||
var _File3 = _interopRequireDefault(_File2); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -27,4 +23,2 @@ | ||
exports.isFile = _isFile3.default; // eslint-disable-line | ||
exports.File = _File3.default; // eslint-disable-line | ||
//# sourceMappingURL=main.js.map |
{ | ||
"name": "then-busboy", | ||
"version": "2.0.0-beta.10", | ||
"version": "2.0.0", | ||
"description": "Promise-based wrapper around Busboy. Process multipart/form-data content and returns it as a single object.", | ||
@@ -14,3 +14,7 @@ "repository": "octet-stream/then-busboy", | ||
"async", | ||
"formdata" | ||
"formdata", | ||
"file", | ||
"upload", | ||
"form-data", | ||
"body" | ||
], | ||
@@ -17,0 +21,0 @@ "scripts": { |
@@ -10,3 +10,3 @@ # then-busboy | ||
Note: The current documentation is for 2.x versions of then-busboy. | ||
Note: The current documentation is for 2.x version of then-busboy. | ||
If you're looking for a previous version, check out the [1.x branch](https://github.com/octet-stream/then-busboy/tree/1.x). | ||
@@ -16,5 +16,5 @@ | ||
Use can install `then-busboy` from npm: | ||
You can install `then-busboy` from npm: | ||
```bash | ||
``` | ||
npm install --save then-busboy | ||
@@ -25,3 +25,3 @@ ``` | ||
```bash | ||
``` | ||
yarn add then-busboy | ||
@@ -51,23 +51,23 @@ ``` | ||
A file contents Readable stream. | ||
File contents Readable stream. | ||
##### `stream` | ||
An alias for [contents](#contents) | ||
Alias for [contents](#contents) | ||
##### `filename` | ||
A full name of the file | ||
Full name of the file | ||
##### `basename` | ||
A name of the file without extension | ||
Name of the file without extension | ||
##### `extname` | ||
A file extension | ||
File extension | ||
##### `mime` | ||
A file mime type | ||
File mime type | ||
@@ -97,2 +97,8 @@ ##### `enc` | ||
### `isFile(value) -> boolean` | ||
Check if given value is a File instance. | ||
- **any** value – a value to verify | ||
## Fields format | ||
@@ -148,3 +154,4 @@ | ||
Just import `then-busboy` and pass `request` object as first argument. | ||
then-busboy works fine even with a pure Node.js HTTP server. | ||
Let's take a look to the tiny example: | ||
@@ -157,5 +164,7 @@ ```js | ||
// Get result from then-busboy | ||
function onFulfilled(data) { | ||
function onFulfilled(body) { | ||
res.writeHead("Content-Type", "application/json") | ||
res.end(JSON.stringify(data)) | ||
// You can also do something with each file and a field. | ||
res.end(JSON.stringify(body)) | ||
} | ||
@@ -177,12 +186,52 @@ | ||
`then-busboy` always returns a Promise, so you can use it with | ||
[asynchronous function](https://github.com/tc39/ecmascript-asyncawait) syntax: | ||
**Note:** You can use [asynchronous function](https://github.com/tc39/ecmascript-asyncawait) syntax, | ||
because then-busboy always returns a Promise. | ||
So, let's see on a simple middleware example for Koa.js: | ||
```js | ||
// Some of your awesome code with async workflow... | ||
const data = await busboy(req) | ||
import busboy from "then-busboy" | ||
// Not a real module. Just for an example. | ||
import deep from "whatever-deep-async-map" | ||
const toLowerCase = string => String.prototype.toLowerCase.call(string) | ||
const multipart = () => async (ctx, next) => { | ||
if (["post", "put"].includes(toLowerCase(ctx.method)) === false) { | ||
return await next() | ||
} | ||
if (ctx.is("multipart/form-data") === false) { | ||
return await next() | ||
} | ||
ctx.request.body = await busboy(ctx.req) | ||
await next() | ||
} | ||
export default multipart | ||
``` | ||
You can check if some value is an instance of File class using `isFile`. | ||
This function may help you if you're wanted to do something | ||
with received files automatically. | ||
```js | ||
import busboy, {isFile} from "then-busboy" | ||
let body = await busboy(request) | ||
body = await deepMapObject( | ||
body, async val => ( | ||
isFile(val) // check if current element is a File | ||
? await processFile(val) // do somethig with a file | ||
: val // ...or just return a field | ||
) | ||
) | ||
``` | ||
## License | ||
[MIT](https://github.com/octet-stream/then-busboy/blob/master/LICENSE) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
26052
507
1
230