333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
 
Description
===========
 
A very fast streaming multipart parser for node.js.
 
Benchmarks can be found [here](https://github.com/mscdex/dicer/wiki/Benchmarks).
 
 
Requirements
============
 
* [node.js](http://nodejs.org/) -- v0.8.0 or newer
 
 
Install
============
 
    npm install dicer
 
 
Examples
========
 
* Parse an HTTP form upload
 
```javascript
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.