https://github.com/idiocc/dicer
[fork] A Very Fast Streaming Multipart Parser For Node.JS Written In ES6 And Optimised With JavaScript Compiler.
https://github.com/idiocc/dicer
Last synced: about 1 year ago
JSON representation
[fork] A Very Fast Streaming Multipart Parser For Node.JS Written In ES6 And Optimised With JavaScript Compiler.
- Host: GitHub
- URL: https://github.com/idiocc/dicer
- Owner: idiocc
- License: other
- Created: 2019-06-29T08:16:06.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-12-24T06:07:53.000Z (over 6 years ago)
- Last Synced: 2025-05-11T10:04:30.051Z (about 1 year ago)
- Language: JavaScript
- Homepage: https://idio.cc
- Size: 75.2 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# @idio/dicer
[](https://www.npmjs.com/package/@idio/dicer)
`@idio/dicer` is a [fork](https://github.com/mscdex/dicer) of A Very Fast Streaming Multipart Parser For Node.JS Written In ES6 And Optimised With [JavaScript Compiler](https://compiler.page).
```sh
yarn add @idio/dicer
```
## Table Of Contents
- [Table Of Contents](#table-of-contents)
- [API](#api)
- [`class Dicer`](#class-dicer)
* [`_idio.DicerConfig`](#type-_idiodicerconfig)
* [`_idio.Dicer`](#type-_idiodicer)
- [Copyright](#copyright)
## API
The package is available by importing its default function:
```js
import Dicer from '@idio/dicer'
```
## `class Dicer`
Dicer is a _Writable_ stream.
`import('stream').WritableOptions` __`stream.WritableOptions`__
`_idio.DicerConfig` extends [`stream.WritableOptions`](#type-streamwritableoptions): Options for the program.
| Name | Type | Description | Default |
| -------------- | ---------------- | ---------------------------------------------------------------- | ------- |
| boundary | string | This is the boundary used to detect the beginning of a new part. | - |
| headerFirst | boolean | If true, preamble header parsing will be performed first. | `false` |
| partHwm | boolean | High watermark for parsing parts. | - |
| maxHeaderPairs | number | The maximum number of header key=>value pairs to parse. | `2000` |
| Name | Type | Description |
| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| __constructor__ | new (cfg?: !_idio.DicerConfig) => [_idio.Dicer](#type-_idiodicer) | Creates a new instance. |
| __setBoundary__ | (boundary: string) => void | Sets the boundary to use for parsing and performs some initialization needed for parsing. You should only need to use this if you set `headerFirst` to true in the constructor and are parsing the boundary from the preamble header. |
| ___ignore__ | () => void | Ignores current part. |
```js
import Dicer from '@idio/dicer'
import { createServer } from 'http'
import { inspect } from 'util'
const RE_BOUNDARY = /^multipart\/.+?(?:; boundary=(?:(?:"(.+)")|(?:([^\s]+))))$/i,
HTML = Buffer.from('\
\
\
\
\
\
'),
PORT = 8080
createServer(function(req, res) {
var m
if (req.method === 'POST'
&& req.headers['content-type']
&& (m = RE_BOUNDARY.exec(req.headers['content-type']))) {
var d = new Dicer({ boundary: m[1] || m[2] })
d.on('part', function(p) {
console.log('New part!')
p.on('header', function(header) {
for (var h in header) {
console.log('Part header: k: ' + inspect(h)
+ ', v: ' + inspect(header[h]))
}
})
p.on('data', function(data) {
console.log('Part data: ' + inspect(data.toString()))
})
p.on('end', function() {
console.log('End of part\n')
})
})
d.on('finish', function() {
console.log('End of parts')
res.writeHead(200)
res.end('Form submission successful!')
})
req.pipe(d)
} else if (req.method === 'GET' && req.url === '/') {
res.writeHead(200)
res.end(HTML)
} else {
res.writeHead(404)
res.end()
}
}).listen(PORT, function() {
console.log('Listening for requests on port ' + PORT)
this.close()
})
```
```
Listening for requests on port 8080
```
## Copyright
Original Work by [Brian White aka mscdex](https://github.com/mscdex/dicer).
---
© Art Deco for Idio 2019
Tech Nation Visa Sucks