You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.6 KiB

  1. Description
  2. ===========
  3. A very fast streaming multipart parser for node.js.
  4. Benchmarks can be found [here](https://github.com/mscdex/dicer/wiki/Benchmarks).
  5. Requirements
  6. ============
  7. * [node.js](http://nodejs.org/) -- v4.5.0 or newer
  8. Install
  9. ============
  10. npm install dicer
  11. Examples
  12. ========
  13. * Parse an HTTP form upload
  14. ```javascript
  15. var inspect = require('util').inspect,
  16. http = require('http');
  17. var Dicer = require('dicer');
  18. // quick and dirty way to parse multipart boundary
  19. var RE_BOUNDARY = /^multipart\/.+?(?:; boundary=(?:(?:"(.+)")|(?:([^\s]+))))$/i,
  20. HTML = Buffer.from('<html><head></head><body>\
  21. <form method="POST" enctype="multipart/form-data">\
  22. <input type="text" name="textfield"><br />\
  23. <input type="file" name="filefield"><br />\
  24. <input type="submit">\
  25. </form>\
  26. </body></html>'),
  27. PORT = 8080;
  28. http.createServer(function(req, res) {
  29. var m;
  30. if (req.method === 'POST'
  31. && req.headers['content-type']
  32. && (m = RE_BOUNDARY.exec(req.headers['content-type']))) {
  33. var d = new Dicer({ boundary: m[1] || m[2] });
  34. d.on('part', function(p) {
  35. console.log('New part!');
  36. p.on('header', function(header) {
  37. for (var h in header) {
  38. console.log('Part header: k: ' + inspect(h)
  39. + ', v: ' + inspect(header[h]));
  40. }
  41. });
  42. p.on('data', function(data) {
  43. console.log('Part data: ' + inspect(data.toString()));
  44. });
  45. p.on('end', function() {
  46. console.log('End of part\n');
  47. });
  48. });
  49. d.on('finish', function() {
  50. console.log('End of parts');
  51. res.writeHead(200);
  52. res.end('Form submission successful!');
  53. });
  54. req.pipe(d);
  55. } else if (req.method === 'GET' && req.url === '/') {
  56. res.writeHead(200);
  57. res.end(HTML);
  58. } else {
  59. res.writeHead(404);
  60. res.end();
  61. }
  62. }).listen(PORT, function() {
  63. console.log('Listening for requests on port ' + PORT);
  64. });
  65. ```
  66. API
  67. ===
  68. _Dicer_ is a _WritableStream_
  69. Dicer (special) events
  70. ----------------------
  71. * **finish**() - Emitted when all parts have been parsed and the Dicer instance has been ended.
  72. * **part**(< _PartStream_ >stream) - Emitted when a new part has been found.
  73. * **preamble**(< _PartStream_ >stream) - Emitted for preamble if you should happen to need it (can usually be ignored).
  74. * **trailer**(< _Buffer_ >data) - Emitted when trailing data was found after the terminating boundary (as with the preamble, this can usually be ignored too).
  75. Dicer methods
  76. -------------
  77. * **(constructor)**(< _object_ >config) - Creates and returns a new Dicer instance with the following valid `config` settings:
  78. * **boundary** - _string_ - This is the boundary used to detect the beginning of a new part.
  79. * **headerFirst** - _boolean_ - If true, preamble header parsing will be performed first.
  80. * **maxHeaderPairs** - _integer_ - The maximum number of header key=>value pairs to parse **Default:** 2000 (same as node's http).
  81. * **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.
  82. _PartStream_ is a _ReadableStream_
  83. PartStream (special) events
  84. ---------------------------
  85. * **header**(< _object_ >header) - An object containing the header for this particular part. Each property value is an _array_ of one or more string values.