Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dicer

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dicer - npm Package Compare versions

Comparing version 0.1.3 to 0.1.4

bench/multiparty-bench-multipart-parser.js

63

lib/Dicer.js

@@ -1,2 +0,3 @@

var WritableStream = require('readable-stream').Writable,
var WritableStream = require('stream').Writable
|| require('readable-stream').Writable,
inherits = require('util').inherits;

@@ -10,3 +11,4 @@

var B_ONEDASH = new Buffer('-'),
B_CRLF = new Buffer('\r\n');
B_CRLF = new Buffer('\r\n'),
EMPTY_READFN = function(n) {};

@@ -16,5 +18,5 @@ function Dicer(cfg) {

return new Dicer(cfg);
WritableStream.call(this);
WritableStream.call(this, cfg);
if (!cfg.headerFirst && typeof cfg.boundary !== 'string')
if (!cfg || (!cfg.headerFirst && typeof cfg.boundary !== 'string'))
throw new TypeError('Boundary required');

@@ -32,2 +34,4 @@

this._dashes = 0;
this._parts = 0;
this._finished = false;
this._isPreamble = true;

@@ -38,2 +42,7 @@ this._justMatched = false;

this._part = undefined;
this._cb = undefined;
this._partOpts = (typeof cfg.partHwm === 'number'
? { highWaterMark: cfg.partHwm }
: {});
this._pause = false;

@@ -50,5 +59,7 @@ this._hparser = new HeaderParser(cfg);

Dicer.prototype._write = function(data, encoding, cb) {
var self = this;
if (this._headerFirst && this._isPreamble) {
if (!this._part) {
this._part = new PartStream();
this._part = new PartStream(this._partOpts);
this.emit('preamble', this._part);

@@ -71,3 +82,6 @@ }

cb();
if (this._pause)
this._cb = cb;
else
cb();
};

@@ -90,6 +104,5 @@

Dicer.prototype._oninfo = function(isMatch, data, start, end) {
var buf, self = this;
var buf, self = this, i = 0, r, shouldWriteMore = true;
if (!this._part && this._justMatched && data) {
var i = 0;
while (this._dashes < 2 && (start + i) < end) {

@@ -110,3 +123,4 @@ if (data[start + i] === 45) {

this.reset();
process.nextTick(function() { self.emit('end'); });
this._finished = true;
//process.nextTick(function() { self.emit('end'); });
}

@@ -119,3 +133,13 @@ if (this._dashes)

if (!this._part) {
this._part = new PartStream();
this._part = new PartStream(this._partOpts);
this._part._read = function(n) {
if (!self._pause)
return;
self._pause = false;
if (self._cb) {
var cb = self._cb;
self._cb = undefined;
cb();
}
};
this.emit(this._isPreamble ? 'preamble' : 'part', this._part);

@@ -128,8 +152,10 @@ if (!this._isPreamble)

if (buf)
this._part.push(buf);
this._part.push(data.slice(start, end));
shouldWriteMore = this._part.push(buf);
shouldWriteMore = this._part.push(data.slice(start, end));
if (!shouldWriteMore)
this._pause = true;
} else if (!this._isPreamble && this._inHeader) {
if (buf)
this._hparser.push(buf);
var r = this._hparser.push(data.slice(start, end));
r = this._hparser.push(data.slice(start, end));
if (!this._inHeader && r !== undefined && r < end)

@@ -140,5 +166,14 @@ this._oninfo(false, data, start + r, end);

if (isMatch) {
this._hparser.reset();
if (this._isPreamble)
this._isPreamble = false;
this._hparser.reset();
else {
++this._parts;
this._part.on('end', function() {
if (self._finished && --self._parts === 0) {
self._finished = false;
self.emit('end');
}
});
}
this._part.push(null);

@@ -145,0 +180,0 @@ this._part = undefined;

@@ -1,8 +0,8 @@

var inherits = require('util').inherits;
var ReadableStream = require('readable-stream');
var inherits = require('util').inherits,
ReadableStream = require('stream').Readable || require('readable-stream');
function PartStream() {
function PartStream(opts) {
if (!(this instanceof PartStream))
return new PartStream();
ReadableStream.call(this);
return new PartStream(opts);
ReadableStream.call(this, opts);
}

@@ -9,0 +9,0 @@ inherits(PartStream, ReadableStream);

{ "name": "dicer",
"version": "0.1.3",
"version": "0.1.4",
"author": "Brian White <mscdex@mscdex.net>",

@@ -8,3 +8,3 @@ "description": "A very fast streaming multipart parser for node.js",

"streamsearch": "0.1.2",
"readable-stream": "*"
"readable-stream": "1.1.x"
},

@@ -11,0 +11,0 @@ "scripts": {

@@ -8,3 +8,5 @@ var Dicer = require('..');

[
var t = 0;
var tests = [
{ source: 'nested',

@@ -28,12 +30,21 @@ opts: { boundary: 'AaB03x' },

},
].forEach(function(v) {
var fd, n = 0, buffer = new Buffer(v.chsize),
];
function next() {
if (t === tests.length)
return;
var v = tests[t],
fd,
n = 0,
buffer = new Buffer(v.chsize),
errPrefix = '[' + v.what + ']: ',
state = { done: false, parts: [], preamble: undefined };
fd = fs.openSync(FIXTURES_ROOT + v.source + '/original', 'r')
fd = fs.openSync(FIXTURES_ROOT + v.source + '/original', 'r');
var dicer = new Dicer(v.opts);
dicer.on('preamble', function(p) {
var preamble = { body: undefined, bodylen: 0, header: undefined };
p.on('header', function(h) {

@@ -62,2 +73,3 @@ preamble.header = h;

var part = { body: undefined, bodylen: 0, header: undefined };
p.on('header', function(h) {

@@ -84,20 +96,2 @@ part.header = h;

.on('end', function() {
state.done = true;
});
while (true) {
n = fs.readSync(fd, buffer, 0, buffer.length, null);
if (n === 0) {
dicer.end();
break;
}
dicer.write(n === buffer.length ? buffer : buffer.slice(0, n));
}
fs.closeSync(fd);
// nextTick is required since changing over to streams2, which defers 'end'
// event emitting for readable streams until the next tick
process.nextTick(function() {
assert(state.done, errPrefix + 'Parser did not finish');
var preamble;

@@ -115,3 +109,2 @@ if (fs.existsSync(FIXTURES_ROOT + v.source + '/preamble')) {

if (fs.existsSync(FIXTURES_ROOT + v.source + '/preamble.header')) {
var prehead = JSON.parse(fs.readFileSync(FIXTURES_ROOT + v.source

@@ -154,3 +147,20 @@ + '/preamble.header', 'binary'));

}
++t;
next();
});
});
while (true) {
n = fs.readSync(fd, buffer, 0, buffer.length, null);
if (n === 0) {
dicer.end();
break;
}
dicer.write(n === buffer.length ? buffer : buffer.slice(0, n));
}
fs.closeSync(fd);
}
next();
process.on('exit', function() {
assert(t === tests.length, 'Only ran ' + t + '/' + tests.length + ' tests');
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc