Comparing version 1.2.3 to 1.3.0
@@ -0,1 +1,7 @@ | ||
1.3.0 / 2014-07-20 | ||
================== | ||
* Fully unpipe the stream on error | ||
- Fixes `Cannot switch to old mode now` error on Node.js 0.10+ | ||
1.2.3 / 2014-07-20 | ||
@@ -2,0 +8,0 @@ ================== |
96
index.js
@@ -40,10 +40,9 @@ var bytes = require('bytes') | ||
if (limit !== null && length !== null && length > limit) { | ||
if (typeof stream.pause === 'function') | ||
stream.pause() | ||
var err = makeError('request entity too large', 'entity.too.large') | ||
err.status = err.statusCode = 413 | ||
err.length = err.expected = length | ||
err.limit = limit | ||
cleanup() | ||
halt(stream) | ||
process.nextTick(function () { | ||
var err = makeError('request entity too large', 'entity.too.large') | ||
err.status = err.statusCode = 413 | ||
err.length = err.expected = length | ||
err.limit = limit | ||
done(err) | ||
@@ -61,10 +60,9 @@ }) | ||
if (stream._decoder || (state && (state.encoding || state.decoder))) { | ||
if (typeof stream.pause === 'function') | ||
stream.pause() | ||
// developer error | ||
var err = makeError('stream encoding should not be set', | ||
'stream.encoding.set') | ||
err.status = err.statusCode = 500 | ||
cleanup() | ||
halt(stream) | ||
process.nextTick(function () { | ||
var err = makeError('stream encoding should not be set', | ||
'stream.encoding.set') | ||
// developer error | ||
err.status = err.statusCode = 500 | ||
done(err) | ||
@@ -81,5 +79,4 @@ }) | ||
} catch (err) { | ||
if (typeof stream.pause === 'function') | ||
stream.pause() | ||
cleanup() | ||
halt(stream) | ||
process.nextTick(function () { | ||
@@ -114,4 +111,2 @@ done(err) | ||
if (limit !== null && received > limit) { | ||
if (typeof stream.pause === 'function') | ||
stream.pause() | ||
var err = makeError('request entity too large', 'entity.too.large') | ||
@@ -121,4 +116,5 @@ err.status = err.statusCode = 413 | ||
err.limit = limit | ||
cleanup() | ||
halt(stream) | ||
done(err) | ||
cleanup() | ||
} | ||
@@ -129,4 +125,4 @@ } | ||
if (err) { | ||
if (typeof stream.pause === 'function') | ||
stream.pause() | ||
cleanup() | ||
halt(stream) | ||
done(err) | ||
@@ -139,11 +135,11 @@ } else if (length !== null && received !== length) { | ||
err.length = err.expected = length | ||
cleanup() | ||
done(err) | ||
} else { | ||
done(null, decoder | ||
var string = decoder | ||
? buffer + (decoder.end() || '') | ||
: Buffer.concat(buffer) | ||
) | ||
cleanup() | ||
done(null, string) | ||
} | ||
cleanup() | ||
} | ||
@@ -174,2 +170,19 @@ | ||
/** | ||
* Halt a stream. | ||
* | ||
* @param {Object} stream | ||
* @api private | ||
*/ | ||
function halt(stream) { | ||
// unpipe everything from the stream | ||
unpipe(stream) | ||
// pause stream | ||
if (typeof stream.pause === 'function') { | ||
stream.pause() | ||
} | ||
} | ||
// to create serializable errors you must re-set message so | ||
@@ -189,1 +202,32 @@ // that it is enumerable and you must re configure the type | ||
} | ||
/** | ||
* Unpipe everything from a stream. | ||
* | ||
* @param {Object} stream | ||
* @api private | ||
*/ | ||
/* istanbul ignore next: implementation differs between versions */ | ||
function unpipe(stream) { | ||
if (typeof stream.unpipe === 'function') { | ||
// new-style | ||
stream.unpipe() | ||
return | ||
} | ||
// Node.js 0.8 hack | ||
var listener | ||
var listeners = stream.listeners('close') | ||
for (var i = 0; i < listeners.length; i++) { | ||
listener = listeners[i] | ||
if (listener.name !== 'cleanup' && listener.name !== 'onclose') { | ||
continue | ||
} | ||
// invoke the listener | ||
listener.call(stream) | ||
} | ||
} |
{ | ||
"name": "raw-body", | ||
"description": "Get and validate the raw body of a readable stream.", | ||
"version": "1.2.3", | ||
"version": "1.3.0", | ||
"author": "Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)", | ||
@@ -6,0 +6,0 @@ "contributors": [ |
@@ -76,3 +76,3 @@ # raw-body | ||
If an error occurs, the stream will be paused, | ||
If an error occurs, the stream will be paused, everything unpiped, | ||
and you are responsible for correctly disposing the stream. | ||
@@ -79,0 +79,0 @@ For HTTP requests, no handling is required if you send a response. |
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
11686
193