Pragonauts Uploader
Busboy based utility, which helps us:
- Processes the
multipart/form-data
with busboy - Validates uploads (mime type and size)
- Transforms each upload to a Promise (by default to Buffer)
- After all resolves promise
Example
const express = ;const Uploader terminateStream = ; const app = ; app); moduleexports = app;
Using with Pragonauts validator
Uploader uses validation rules from Validator and runs validator with given data
const express = ;const Validator = ;const Uploader terminateStream = ; const app = ; const validator = ; validator ; // extracts buffer from the file app); moduleexports = app;
API
Classes
- Uploader
- LimitedStream ⇐
Transform
Functions
- terminateStream(readableStream) ⇒
Promise.<Buffer>
Converts a stream to buffer
Uploader
Kind: global class
- Uploader
- new Uploader()
- instance
- .addFile(field, [processor], [maxLength], [allowedMimes]) ⇒
this
- .process(req, [validatorContext]) ⇒
Promise.<object>
- .addFile(field, [processor], [maxLength], [allowedMimes]) ⇒
- static
new Uploader()
The NodeJs multipart/form-data processor
this
uploader.addFile(field, [processor], [maxLength], [allowedMimes]) ⇒ Add a file upload
Kind: instance method of Uploader
Param | Type | Default | Description |
---|---|---|---|
field | string |
field name | |
[processor] | function |
terminateStream |
the request processor |
[maxLength] | number |
|
size limit |
[allowedMimes] | Array.<string> |
|
accept only theese mime types (strings or RegExps) |
Example
// process upload on own using callback uploader
Promise.<object>
uploader.process(req, [validatorContext]) ⇒ Process the uploads
Kind: instance method of Uploader
Param | Type | Default | Description |
---|---|---|---|
req | any |
the request | |
[validatorContext] | string |
null |
the context to validator |
Uploader.Uploader
Kind: static class of Uploader
new Uploader([validator])
Creates an instance of Uploader.
Param | Type | Default | Description |
---|---|---|---|
[validator] | any |
|
instance of prg-validator |
Transform
LimitedStream ⇐ Kind: global class
Extends: Transform
new LimitedStream()
Stream which throws error, when size of stream exceeds allowed length
Example
const stream = maxLength: 1024 ; // 1Kbstream;
LimitedStream.LimitedStream
Kind: static class of LimitedStream
new LimitedStream(options)
Creates an instance of LimitedStream.
Param | Type | Description |
---|---|---|
options | Object |
|
[options.maxLength] | number |
the maximal length in bytes |
Promise.<Buffer>
terminateStream(readableStream) ⇒ Converts a stream to buffer
Kind: global function
Param | Type |
---|---|
readableStream | ReadableStream |