dicer

WebJar for dicer

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

dicer
Last Version

Last Version

0.3.0
Release Date

Release Date

Type

Type

jar
Description

Description

dicer
WebJar for dicer
Project URL

Project URL

https://www.webjars.org
Source Code Management

Source Code Management

https://github.com/mscdex/dicer

Download dicer

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/dicer/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>dicer</artifactId>
    <version>0.3.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/dicer/
implementation 'org.webjars.npm:dicer:0.3.0'
// https://jarcasting.com/artifacts/org.webjars.npm/dicer/
implementation ("org.webjars.npm:dicer:0.3.0")
'org.webjars.npm:dicer:jar:0.3.0'
<dependency org="org.webjars.npm" name="dicer" rev="0.3.0">
  <artifact name="dicer" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='dicer', version='0.3.0')
)
libraryDependencies += "org.webjars.npm" % "dicer" % "0.3.0"
[org.webjars.npm/dicer "0.3.0"]

Dependencies

compile (1)

Group / Artifact Type Version
org.webjars.npm : streamsearch jar [0.1.2]

Project Modules

There are no modules declared in this project.

Description

A very fast streaming multipart parser for node.js.

Benchmarks can be found here.

Requirements

Install

npm install dicer

Examples

  • Parse an HTTP form upload
var inspect = require('util').inspect,
    http = require('http');

var Dicer = require('dicer');

    // quick and dirty way to parse multipart boundary
var RE_BOUNDARY = /^multipart\/.+?(?:; boundary=(?:(?:"(.+)")|(?:([^\s]+))))$/i,
    HTML = Buffer.from('<html><head></head><body>\
                        <form method="POST" enctype="multipart/form-data">\
                         <input type="text" name="textfield"><br />\
                         <input type="file" name="filefield"><br />\
                         <input type="submit">\
                        </form>\
                        </body></html>'),
    PORT = 8080;

http.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);
});

API

Dicer is a WritableStream

Dicer (special) events

  • finish() - Emitted when all parts have been parsed and the Dicer instance has been ended.

  • part(< PartStream >stream) - Emitted when a new part has been found.

  • preamble(< PartStream >stream) - Emitted for preamble if you should happen to need it (can usually be ignored).

  • trailer(< Buffer >data) - Emitted when trailing data was found after the terminating boundary (as with the preamble, this can usually be ignored too).

Dicer methods

  • (constructor)(< object >config) - Creates and returns a new Dicer instance with the following valid config settings:

    • 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.

    • maxHeaderPairs - integer - The maximum number of header key=>value pairs to parse Default: 2000 (same as node's http).

  • setBoundary(< string >boundary) - (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.

PartStream is a ReadableStream

PartStream (special) events

  • header(< object >header) - An object containing the header for this particular part. Each property value is an array of one or more string values.

Versions

Version
0.3.0
0.2.5