https://github.com/yoshuawuyts/multipart-read-stream
Read a multipart stream over HTTP
https://github.com/yoshuawuyts/multipart-read-stream
multipart node pez stream
Last synced: about 11 hours ago
JSON representation
Read a multipart stream over HTTP
- Host: GitHub
- URL: https://github.com/yoshuawuyts/multipart-read-stream
- Owner: yoshuawuyts
- License: mit
- Created: 2016-10-20T14:16:12.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-06-21T18:51:42.000Z (almost 8 years ago)
- Last Synced: 2025-01-03T12:24:29.279Z (5 months ago)
- Topics: multipart, node, pez, stream
- Language: JavaScript
- Size: 15.6 KB
- Stars: 13
- Watchers: 5
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# multipart-read-stream [![stability][0]][1]
[![npm version][2]][3] [![build status][4]][5] [![test coverage][6]][7]
[![downloads][8]][9] [![js-standard-style][10]][11]Read a multipart stream over HTTP. Built on top of [pez][pez].
## Usage
```js
var multipart = require('multipart-read-stream')
var pump = require('pump')
var http = require('http')http.createServer(function (req, res) {
var multipartStream = multipart(req.headers, handler)pump(req, multipartStream, function (err) {
if (err) res.end('server error')
res.end()
})function handler (fieldname, file, filename) {
console.log('reading file ' + filename + ' from field ' + fieldname)
var fileStream = fs.createWriteStream(path.join('/tmp', filename))
pump(file, fileStream)
}
}).listen(8080)
```## API
### readableStream = multipart(headers, [options], fileHandler)
Create a new multipart stream handler. Takes the following arguments:
- __headers:__ an object containing request headers (typically: `req.headers`)
- __options:__ an object that is passed directly to [pez][pez]
- __filehandler(fieldname, file, filename, encoding, mimetype):__ handle a
file. Each `file` is a `readableStream`### Events
multipart-read-stream returns an instance (from `pez.Dispenser`) which
emits a number of multipart specific events:#### readableStream.on('part', cb(stream))
The `part` event drives the `fileHandler` callback for the main API.
The difference is it supplies a single parameter, the read stream of the
file data of a multipart section.#### readableStream.on('field', cb(name, value))
A field event is emitted for partitions containing key-value data
(instead of file data).#### readableStream.on('preamble', cb(str))
Multipart data *may* have a preamble section, which is typically
ignored by parsers. However it's sometimes used as an area to
contain hints/meta information.#### readableStream.on('epilogue', cb(str))
As with the preamble section, the epilogue section essentially
has the same role (ignored, but can be used for meta data), except
it will be parsed after the body rather than before.## Installation
```sh
$ npm install --save multipart-read-stream
```## License
[MIT](https://tldrlegal.com/license/mit-license)[0]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square
[1]: https://nodejs.org/api/documentation.html#documentation_stability_index
[2]: https://img.shields.io/npm/v/multipart-read-stream.svg?style=flat-square
[3]: https://npmjs.org/package/multipart-read-stream
[4]: https://img.shields.io/travis/yoshuawuyts/multipart-read-stream/master.svg?style=flat-square
[5]: https://travis-ci.org/yoshuawuyts/multipart-read-stream
[6]: https://img.shields.io/codecov/c/github/yoshuawuyts/multipart-read-stream/master.svg?style=flat-square
[7]: https://codecov.io/github/yoshuawuyts/multipart-read-stream
[8]: http://img.shields.io/npm/dm/multipart-read-stream.svg?style=flat-square
[9]: https://npmjs.org/package/multipart-read-stream
[10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
[11]: https://github.com/feross/standard
[pez]: https://github.com/hapijs/pez