content-length
Advanced tools
Comparing version 1.0.0 to 1.0.1
18
index.js
module.exports = function (done) { | ||
if (!done || typeof done !== 'function') throw new Error('Must provide a callback for content-length'); | ||
if (!done || typeof done !== 'function') throw new Error('Must provide a callback for content-length'); | ||
return function (req, res, next) { | ||
@@ -8,21 +8,19 @@ var byteLength = 0; | ||
var write = res.write; | ||
res.write = function (payload) { | ||
if (payload) byteLength += Buffer.byteLength(payload.toString(), 'utf8'); | ||
res.write = write; | ||
res.write.apply(res, arguments); | ||
}; | ||
res.end = function (payload) { | ||
if (payload) byteLength += Buffer.byteLength(payload.toString(), 'utf8'); | ||
res.end = end; | ||
res.end.apply(res, arguments); | ||
done(null, byteLength); | ||
}; | ||
next(); | ||
} | ||
} | ||
} |
{ | ||
"name": "content-length", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Express/Connect middleware to calculate the response payload size", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -10,11 +10,11 @@ var contentLength = require('../'); | ||
var responseLength = 0; | ||
app.use(contentLength(function (err, len) { | ||
responseLength = len; | ||
})); | ||
app.use(function (req, res) { | ||
res.send('Hi'); | ||
}); | ||
request(app) | ||
@@ -31,15 +31,15 @@ .get('/') | ||
}); | ||
it('gives a 0 content length if there is no response', function (done) { | ||
var app = express(); | ||
var responseLength = 0; | ||
app.use(contentLength(function (err, len) { | ||
responseLength = len; | ||
})); | ||
app.use(function (req, res) { | ||
res.send(); | ||
}); | ||
request(app) | ||
@@ -56,6 +56,6 @@ .get('/') | ||
}); | ||
it('throws an error if no callback is given', function () { | ||
var app = express(); | ||
expect(function () { | ||
@@ -65,6 +65,6 @@ app.use(contentLength()); | ||
}); | ||
it('throws an error if the callback is not a function', function () { | ||
var app = express(); | ||
expect(function () { | ||
@@ -74,2 +74,2 @@ app.use(contentLength('test')); | ||
}); | ||
}); | ||
}); |
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
77
5079