xprezzo-raw-body
Advanced tools
Comparing version 1.0.4 to 1.0.5
173
index.js
@@ -17,5 +17,10 @@ /*! | ||
var HttpRawStream = require('./lib/HttpRawStream'); | ||
var createError = require('xprezzo-http-errors') | ||
var iconv = require('xprezzo-iconv') | ||
var onFinished = require('xprezzo-on-finished') | ||
var zlib = require('zlib') | ||
/** | ||
* Module exports. | ||
* Get the raw body of a stream (typically HTTP). | ||
* Wrap and protect the HttpRawStream | ||
* | ||
@@ -27,3 +32,3 @@ * @param {object} stream | ||
*/ | ||
module.exports = function (stream, options, callback) { | ||
var getBody = module.exports = function (stream, options, callback) { | ||
var done = callback | ||
@@ -80,1 +85,165 @@ var opts = options || {} | ||
} | ||
/** | ||
* Module exports. | ||
* | ||
* Reader | ||
* | ||
* Read a request into a buffer and parse. | ||
* | ||
* @param {object} req | ||
* @param {object} res | ||
* @param {function} next | ||
* @param {function} parse | ||
* @param {function} debug | ||
* @param {object} options | ||
* @private | ||
*/ | ||
module.exports.Reader = function (req, res, next, parse, debug, options) { | ||
var length | ||
var opts = options | ||
var stream | ||
// flag as parsed | ||
req._body = true | ||
// read options | ||
var encoding = opts.encoding !== null | ||
? opts.encoding | ||
: null | ||
var verify = opts.verify | ||
debug('typeof verify = ' + typeof verify) | ||
try { | ||
// get the content stream | ||
stream = contentstream(req, debug, opts.inflate) | ||
length = stream.length | ||
debug('length = ' + length) | ||
stream.length = undefined | ||
} catch (err) { | ||
return next(err) | ||
} | ||
// set xprezzo-raw-body options | ||
opts.length = length | ||
opts.encoding = verify | ||
? null | ||
: encoding | ||
debug('encoding = ' + encoding) | ||
debug('encoding exists=' + iconv.encodingExists(encoding)) | ||
// assert charset is supported | ||
if (opts.encoding === null && encoding !== null && !iconv.encodingExists(encoding)) { | ||
return next(createError(415, 'unsupported charset "' + encoding.toUpperCase() + '"', { | ||
charset: encoding.toLowerCase(), | ||
type: 'charset.unsupported' | ||
})) | ||
} | ||
// read body | ||
debug('read body') | ||
getBody(stream, opts, function (error, body) { | ||
if (error) { | ||
var _error | ||
if (error.type === 'encoding.unsupported') { | ||
// echo back charset | ||
_error = createError(415, 'unsupported charset "' + encoding.toUpperCase() + '"', { | ||
charset: encoding.toLowerCase(), | ||
type: 'charset.unsupported' | ||
}) | ||
} else { | ||
// set status code on error | ||
_error = createError(400, error) | ||
} | ||
// read off entire request | ||
stream.resume() | ||
onFinished(req, function onfinished () { | ||
next(createError(400, _error)) | ||
}) | ||
return | ||
} | ||
// verify | ||
if (verify) { | ||
try { | ||
debug('verify body') | ||
verify(req, res, body, encoding) | ||
} catch (err) { | ||
next(createError(403, err, { | ||
body: body, | ||
type: err.type || 'entity.verify.failed' | ||
})) | ||
return | ||
} | ||
} | ||
// parse | ||
var str = body | ||
try { | ||
debug('parse body') | ||
str = typeof body !== 'string' && encoding !== null | ||
? iconv.decode(body, encoding) | ||
: body | ||
req.body = parse(str) | ||
} catch (err) { | ||
next(createError(400, err, { | ||
body: str, | ||
type: err.type || 'entity.parse.failed' | ||
})) | ||
return | ||
} | ||
next() | ||
}) | ||
} | ||
/** | ||
* Get the content stream of the request. | ||
* | ||
* @param {object} req | ||
* @param {function} debug | ||
* @param {boolean} [inflate=true] | ||
* @return {object} | ||
* @api private | ||
*/ | ||
function contentstream (req, debug, inflate) { | ||
var encoding = (req.headers['content-encoding'] || 'identity').toLowerCase() | ||
var length = req.headers['content-length'] | ||
var stream | ||
debug('content-encoding "%s"', encoding) | ||
if (inflate === false && encoding !== 'identity') { | ||
throw createError(415, 'content encoding unsupported', { | ||
encoding: encoding, | ||
type: 'encoding.unsupported' | ||
}) | ||
} | ||
switch (encoding) { | ||
case 'deflate': | ||
stream = zlib.createInflate() | ||
debug('inflate body') | ||
req.pipe(stream) | ||
break | ||
case 'gzip': | ||
stream = zlib.createGunzip() | ||
debug('gunzip body') | ||
req.pipe(stream) | ||
break | ||
case 'identity': | ||
stream = req | ||
stream.length = length | ||
break | ||
default: | ||
throw createError(415, 'unsupported content encoding "' + encoding + '"', { | ||
encoding: encoding, | ||
type: 'encoding.unsupported' | ||
}) | ||
} | ||
return stream | ||
} |
@@ -18,2 +18,8 @@ /*! | ||
var unpipe = require('unpipe'); | ||
/** | ||
* Private class variables | ||
* @private | ||
*/ | ||
var count=0; | ||
/** | ||
@@ -30,23 +36,33 @@ * Module exports. | ||
*/ | ||
module.exports = function (stream, encoding, length, limit, callback) { | ||
var self = this; | ||
this.complete = false | ||
this.sync = true | ||
this.state = stream._readableState | ||
this.received = 0 | ||
this.decoder = ''; | ||
this.length = length; | ||
this.limit = limit; | ||
this.stream = stream; | ||
this.callback = callback; | ||
this.cleanup=function(){}; | ||
var entitySizeResult = checkEntitySize.call(this); | ||
if (entitySizeResult) return entitySizeResult; | ||
var streamEncodingResult = checkStreamEncoding.call(this); | ||
if(streamEncodingResult) return streamEncodingResult; | ||
var decoderResult = useDecoder.call(this, encoding); | ||
if(decoderResult) return decoderResult; | ||
setListeners.call(this); | ||
} | ||
module.exports = (function(){ | ||
/** | ||
* Private class variables | ||
* @private | ||
*/ | ||
var count2=0; | ||
return function (stream, encoding, length, limit, callback){ | ||
debug("Object Count : "+ ++count); | ||
debug("Object Count2 : "+ ++count2); | ||
var self = this; | ||
this.complete = false | ||
this.sync = true | ||
this.state = stream._readableState | ||
this.received = 0 | ||
this.decoder = ''; | ||
this.length = length; | ||
this.limit = limit; | ||
this.stream = stream; | ||
this.callback = callback; | ||
this.cleanup=function(){}; | ||
var entitySizeResult = checkEntitySize.call(this); | ||
if (entitySizeResult) return entitySizeResult; | ||
var streamEncodingResult = checkStreamEncoding.call(this); | ||
if(streamEncodingResult) return streamEncodingResult; | ||
var decoderResult = useDecoder.call(this, encoding); | ||
if(decoderResult) return decoderResult; | ||
setListeners.call(this); | ||
} | ||
})(); | ||
/** | ||
@@ -53,0 +69,0 @@ * Get the decoder for a given encoding. |
{ | ||
"name": "xprezzo-raw-body", | ||
"description": "Get and validate the raw body of a readable stream.", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"author": "Leolio Mcleon <info@leolio.page>", | ||
@@ -13,2 +13,3 @@ "license": "MIT", | ||
"xprezzo-iconv": "1.0.2", | ||
"xprezzo-on-finished": "1.0.0", | ||
"unpipe": "1.0.0" | ||
@@ -15,0 +16,0 @@ }, |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
20388
491
0
6
+ Addedxprezzo-on-finished@1.0.0
+ Addedee-first@1.1.1(transitive)
+ Addedxprezzo-on-finished@1.0.0(transitive)