npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@godmodelabs/dicer

v0.2.5

Published

A very fast streaming multipart parser for node.js

Downloads

9

Readme

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 = new Buffer('<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.