Comparing version 2.1.2 to 2.1.3
@@ -0,1 +1,7 @@ | ||
2.1.3 / 2015-09-12 | ||
================== | ||
* Fix sync callback when attaching data listener causes sync read | ||
- Node.js 0.10 compatibility issue | ||
2.1.2 / 2015-07-05 | ||
@@ -2,0 +8,0 @@ ================== |
108
index.js
@@ -39,6 +39,4 @@ /*! | ||
} catch (e) { | ||
throw makeError('specified encoding unsupported', 'encoding.unsupported', { | ||
encoding: encoding, | ||
status: 415, | ||
statusCode: 415 | ||
throw createError(415, 'specified encoding unsupported', 'encoding.unsupported', { | ||
encoding: encoding | ||
}) | ||
@@ -133,2 +131,3 @@ } | ||
* | ||
* @param {number} status | ||
* @param {string} message | ||
@@ -140,7 +139,7 @@ * @param {string} type | ||
function makeError(message, type, props) { | ||
function createError(status, message, type, props) { | ||
var error = new Error() | ||
// capture stack trace | ||
Error.captureStackTrace(error, makeError) | ||
Error.captureStackTrace(error, createError) | ||
@@ -155,2 +154,6 @@ // set free-form properties | ||
// set status | ||
error.status = status | ||
error.statusCode = status | ||
// set type | ||
@@ -179,2 +182,5 @@ Object.defineProperty(error, 'type', { | ||
function readStream(stream, encoding, length, limit, callback) { | ||
var complete = false | ||
var sync = true | ||
// check the length and limit options. | ||
@@ -184,13 +190,7 @@ // note: we intentionally leave the stream paused, | ||
if (limit !== null && length !== null && length > limit) { | ||
var err = makeError('request entity too large', 'entity.too.large', { | ||
return done(createError(413, 'request entity too large', 'entity.too.large', { | ||
expected: length, | ||
length: length, | ||
limit: limit, | ||
status: 413, | ||
statusCode: 413 | ||
}) | ||
return process.nextTick(function () { | ||
done(err) | ||
}) | ||
limit: limit | ||
})) | ||
} | ||
@@ -206,10 +206,3 @@ | ||
// developer error | ||
var err = makeError('stream encoding should not be set', 'stream.encoding.set', { | ||
status: 500, | ||
statusCode: 500 | ||
}) | ||
return process.nextTick(function () { | ||
done(err) | ||
}) | ||
return done(createError(500, 'stream encoding should not be set', 'stream.encoding.set')) | ||
} | ||
@@ -223,5 +216,3 @@ | ||
} catch (err) { | ||
return process.nextTick(function () { | ||
done(err) | ||
}) | ||
return done(err) | ||
} | ||
@@ -233,27 +224,49 @@ | ||
// attach listeners | ||
stream.on('aborted', onAborted) | ||
stream.on('close', cleanup) | ||
stream.on('data', onData) | ||
stream.once('end', onEnd) | ||
stream.once('error', onEnd) | ||
stream.once('close', cleanup) | ||
stream.on('end', onEnd) | ||
stream.on('error', onEnd) | ||
function done(err) { | ||
cleanup() | ||
// mark sync section complete | ||
sync = false | ||
if (err) { | ||
// halt the stream on error | ||
halt(stream) | ||
function done() { | ||
var args = new Array(arguments.length) | ||
// copy arguments | ||
for (var i = 0; i < args.length; i++) { | ||
args[i] = arguments[i] | ||
} | ||
callback.apply(this, arguments) | ||
// mark complete | ||
complete = true | ||
if (sync) { | ||
process.nextTick(invokeCallback) | ||
} else { | ||
invokeCallback() | ||
} | ||
function invokeCallback() { | ||
cleanup() | ||
if (args[0]) { | ||
// halt the stream on error | ||
halt(stream) | ||
} | ||
callback.apply(null, args) | ||
} | ||
} | ||
function onAborted() { | ||
done(makeError('request aborted', 'request.aborted', { | ||
if (complete) return | ||
done(createError(400, 'request aborted', 'request.aborted', { | ||
code: 'ECONNABORTED', | ||
expected: length, | ||
length: length, | ||
received: received, | ||
status: 400, | ||
statusCode: 400 | ||
received: received | ||
})) | ||
@@ -263,2 +276,4 @@ } | ||
function onData(chunk) { | ||
if (complete) return | ||
received += chunk.length | ||
@@ -270,7 +285,5 @@ decoder | ||
if (limit !== null && received > limit) { | ||
done(makeError('request entity too large', 'entity.too.large', { | ||
done(createError(413, 'request entity too large', 'entity.too.large', { | ||
limit: limit, | ||
received: received, | ||
status: 413, | ||
statusCode: 413 | ||
received: received | ||
})) | ||
@@ -281,11 +294,10 @@ } | ||
function onEnd(err) { | ||
if (complete) return | ||
if (err) return done(err) | ||
if (length !== null && received !== length) { | ||
done(makeError('request size did not match content length', 'request.size.invalid', { | ||
done(createError(400, 'request size did not match content length', 'request.size.invalid', { | ||
expected: length, | ||
length: length, | ||
received: received, | ||
status: 400, | ||
statusCode: 400 | ||
received: received | ||
})) | ||
@@ -302,3 +314,3 @@ } else { | ||
function cleanup() { | ||
received = buffer = null | ||
buffer = null | ||
@@ -305,0 +317,0 @@ stream.removeListener('aborted', onAborted) |
{ | ||
"name": "raw-body", | ||
"description": "Get and validate the raw body of a readable stream.", | ||
"version": "2.1.2", | ||
"version": "2.1.3", | ||
"author": "Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)", | ||
@@ -18,6 +18,6 @@ "contributors": [ | ||
"devDependencies": { | ||
"bluebird": "2.9.32", | ||
"istanbul": "0.3.17", | ||
"bluebird": "2.10.0", | ||
"istanbul": "0.3.19", | ||
"mocha": "2.2.5", | ||
"readable-stream": "2.0.1", | ||
"readable-stream": "2.0.2", | ||
"through2": "2.0.0" | ||
@@ -24,0 +24,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16099
255