Comparing version
165
lib/index.js
@@ -60,122 +60,99 @@ 'use strict'; | ||
exports.Dispenser = internals.Dispenser = function (options) { | ||
exports.Dispenser = internals.Dispenser = class extends Stream.Writable { | ||
Stream.Writable.call(this); | ||
constructor(options) { | ||
Hoek.assert(options !== null && typeof options === 'object', | ||
'options must be an object'); | ||
const settings = Hoek.applyToDefaults(internals.defaults, options); | ||
super(); | ||
this._boundary = settings.boundary; | ||
this._state = internals.state.preamble; | ||
this._held = ''; | ||
Hoek.assert(options !== null && typeof options === 'object', 'options must be an object'); | ||
const settings = Hoek.applyToDefaults(internals.defaults, options); | ||
this._stream = null; | ||
this._headers = {}; | ||
this._name = ''; | ||
this._pendingHeader = ''; | ||
this._error = null; | ||
this._bytes = 0; | ||
this._maxBytes = settings.maxBytes; | ||
this._boundary = settings.boundary; | ||
this._state = internals.state.preamble; | ||
this._held = ''; | ||
this._parts = new Nigel.Stream(Buffer.from('--' + settings.boundary)); | ||
this._lines = new Nigel.Stream(Buffer.from('\r\n')); | ||
this._stream = null; | ||
this._headers = {}; | ||
this._name = ''; | ||
this._pendingHeader = ''; | ||
this._error = null; | ||
this._bytes = 0; | ||
this._maxBytes = settings.maxBytes; | ||
this._parts.on('needle', () => { | ||
this._parts = new Nigel.Stream(Buffer.from('--' + settings.boundary)); | ||
this._lines = new Nigel.Stream(Buffer.from('\r\n')); | ||
this._onPartEnd(); | ||
}); | ||
this._parts.on('needle', () => this._onPartEnd()); | ||
this._parts.on('haystack', (chunk) => this._onPart(chunk)); | ||
this._lines.on('needle', () => this._onLineEnd()); | ||
this._lines.on('haystack', (chunk) => this._onLine(chunk)); | ||
this.once('finish', () => this._parts.end()); | ||
this._parts.once('close', () => this._lines.end()); | ||
this._parts.on('haystack', (chunk) => { | ||
let piper = null; | ||
let finish = (err) => { | ||
this._onPart(chunk); | ||
}); | ||
if (piper) { | ||
piper.removeListener('data', onReqData); | ||
piper.removeListener('error', finish); | ||
piper.removeListener('aborted', onReqAborted); | ||
} | ||
this._lines.on('needle', () => { | ||
if (err) { | ||
return this._abort(err); | ||
} | ||
this._onLineEnd(); | ||
}); | ||
this._emit('close'); | ||
}; | ||
this._lines.on('haystack', (chunk) => { | ||
finish = Hoek.once(finish); | ||
this._onLine(chunk); | ||
}); | ||
this._lines.once('close', () => { | ||
this.once('finish', () => { | ||
this._parts.end(); | ||
}); | ||
this._parts.once('close', () => { | ||
this._lines.end(); | ||
}); | ||
let piper = null; | ||
let finish = (err) => { | ||
if (piper) { | ||
piper.removeListener('data', onReqData); | ||
piper.removeListener('error', finish); | ||
piper.removeListener('aborted', onReqAborted); | ||
} | ||
if (err) { | ||
return this._abort(err); | ||
} | ||
this._emit('close'); | ||
}; | ||
finish = Hoek.once(finish); | ||
this._lines.once('close', () => { | ||
if (this._state === internals.state.epilogue) { | ||
if (this._held) { | ||
this._emit('epilogue', this._held); | ||
this._held = ''; | ||
if (this._state === internals.state.epilogue) { | ||
if (this._held) { | ||
this._emit('epilogue', this._held); | ||
this._held = ''; | ||
} | ||
} | ||
} | ||
else if (this._state === internals.state.boundary) { | ||
if (!this._held) { | ||
this._abort(Boom.badRequest('Missing end boundary')); | ||
else if (this._state === internals.state.boundary) { | ||
if (!this._held) { | ||
this._abort(Boom.badRequest('Missing end boundary')); | ||
} | ||
else if (this._held !== '--') { | ||
this._abort(Boom.badRequest('Only white space allowed after boundary at end')); | ||
} | ||
} | ||
else if (this._held !== '--') { | ||
this._abort(Boom.badRequest('Only white space allowed after boundary at end')); | ||
else { | ||
this._abort(Boom.badRequest('Incomplete multipart payload')); | ||
} | ||
} | ||
else { | ||
this._abort(Boom.badRequest('Incomplete multipart payload')); | ||
} | ||
setImmediate(finish); // Give pending events a chance to fire | ||
}); | ||
setImmediate(finish); // Give pending events a chance to fire | ||
}); | ||
const onReqAborted = () => { | ||
const onReqAborted = () => { | ||
finish(Boom.badRequest('Client request aborted')); | ||
}; | ||
finish(Boom.badRequest('Client request aborted')); | ||
}; | ||
const onReqData = (data) => { | ||
const onReqData = (data) => { | ||
this._bytes += Buffer.byteLength(data); | ||
this._bytes += Buffer.byteLength(data); | ||
if (this._bytes > this._maxBytes) { | ||
finish(Boom.entityTooLarge('Maximum size exceeded')); | ||
} | ||
}; | ||
if (this._bytes > this._maxBytes) { | ||
finish(Boom.entityTooLarge('Maximum size exceeded')); | ||
} | ||
}; | ||
this.once('pipe', (req) => { | ||
this.once('pipe', (req) => { | ||
piper = req; | ||
req.on('data', onReqData); | ||
req.once('error', finish); | ||
req.once('aborted', onReqAborted); | ||
}); | ||
piper = req; | ||
req.on('data', onReqData); | ||
req.once('error', finish); | ||
req.once('aborted', onReqAborted); | ||
}); | ||
} | ||
}; | ||
Hoek.inherits(internals.Dispenser, Stream.Writable); | ||
internals.Dispenser.prototype._write = function (buffer, encoding, next) { | ||
@@ -192,3 +169,3 @@ | ||
internals.Dispenser.prototype._emit = function () { | ||
internals.Dispenser.prototype._emit = function (...args) { | ||
@@ -199,3 +176,3 @@ if (this._error) { | ||
this.emit.apply(this, arguments); | ||
this.emit(...args); | ||
}; | ||
@@ -202,0 +179,0 @@ |
{ | ||
"name": "pez", | ||
"description": "Multipart parser", | ||
"version": "4.0.2", | ||
"version": "4.0.3", | ||
"repository": "git://github.com/hapijs/pez", | ||
@@ -13,3 +13,3 @@ "main": "lib/index.js", | ||
"engines": { | ||
"node": ">=8.9.0" | ||
"node": ">=8.12.0" | ||
}, | ||
@@ -26,3 +26,3 @@ "dependencies": { | ||
"form-data": "2.x.x", | ||
"lab": "15.x.x", | ||
"lab": "17.x.x", | ||
"teamwork": "3.x.x", | ||
@@ -32,3 +32,3 @@ "wreck": "14.x.x" | ||
"scripts": { | ||
"test": "lab -t 100 -v -L", | ||
"test": "lab -t 100 -L", | ||
"test-cov-html": "lab -a code -r html -o coverage.html" | ||
@@ -35,0 +35,0 @@ }, |
@@ -5,3 +5,2 @@ #pez | ||
[](https://travis-ci.org/hapijs/pez) | ||
 | ||
@@ -8,0 +7,0 @@ Multipart parser. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
12807
-0.32%266
-4.32%9
-10%