express-interceptor
Advanced tools
Comparing version
@@ -35,2 +35,1 @@ var express = require('express'); | ||
module.exports = app; | ||
@@ -21,2 +21,1 @@ var express = require('express'); | ||
module.exports = app; | ||
@@ -1,1 +0,1 @@ | ||
{"interceptor": "A tiny interceptor for Express responses"} | ||
{"interceptor": "A tiny interceptor for Express responses"} |
78
index.js
@@ -1,12 +0,1 @@ | ||
function validateParams(methods){ | ||
var ACCEPT = ['isInterceptable', 'intercept', 'afterSend']; | ||
for(var k in methods){ | ||
if(ACCEPT.indexOf(k) < 0){ | ||
throw(new Error(k+' isn\'t a valid param ('+ACCEPT.join(', ')+')')); | ||
} | ||
} | ||
if(!('isInterceptable' in methods)){ | ||
throw('isInterceptable is a required param (function)'); | ||
} | ||
} | ||
@@ -16,5 +5,5 @@ module.exports = function(fn) { | ||
return function(req,res,next){ | ||
var methods = fn(req,res); | ||
validateParams(methods); | ||
return function(req, res, next) { | ||
var methods = fn(req, res); | ||
_validateParams(methods); | ||
@@ -27,17 +16,27 @@ var originalEnd = res.end; | ||
function intercept(chunk, encoding){ | ||
if(isFirstWrite){ | ||
function intercept(rawChunk, encoding) { | ||
if (isFirstWrite) { | ||
isFirstWrite = false; | ||
isIntercepting = methods.isInterceptable(); | ||
} | ||
debug('isIntercepting? %s', isIntercepting); | ||
if (isIntercepting){ | ||
if (isIntercepting) { | ||
// collect all the parts of a response | ||
if(chunk){ | ||
if (rawChunk) { | ||
var chunk = rawChunk | ||
if (rawChunk !== null && !Buffer.isBuffer(chunk) && encoding !== 'buffer') { | ||
if (!encoding) { | ||
chunk = new Buffer(rawChunk) | ||
} else { | ||
chunk = new Buffer(rawChunk, encoding) | ||
} | ||
} | ||
chunks.push(chunk); | ||
} | ||
if(typeof cb === 'function'){ | ||
if (typeof cb === 'function') { | ||
cb(); | ||
} | ||
} | ||
return isIntercepting; | ||
@@ -48,3 +47,3 @@ } | ||
debug('write called'); | ||
if( !intercept(chunk,encoding) ){ | ||
if(!intercept(chunk,encoding)) { | ||
originalWrite.apply(res, arguments); | ||
@@ -54,6 +53,6 @@ } | ||
function afterSend(oldBody, newBody){ | ||
if(typeof methods.afterSend === 'function'){ | ||
function afterSend(oldBody, newBody) { | ||
if (typeof methods.afterSend === 'function') { | ||
process.nextTick(function() { | ||
debug('methods.afterSend running now, body size: %s %s',oldBody.length, newBody.length); | ||
debug('methods.afterSend running now, body size: %s %s', oldBody.length, newBody.length); | ||
methods.afterSend(oldBody, newBody); | ||
@@ -67,9 +66,12 @@ }); | ||
var args = Array.prototype.slice.call(arguments); | ||
if( intercept(chunk,encoding) ){ | ||
if (intercept(chunk,encoding)) { | ||
isIntercepting = false; | ||
var oldBody = Buffer.concat(chunks).toString('utf-8'); | ||
if (methods.intercept){ | ||
if(typeof methods.intercept !== 'function'){ | ||
if (methods.intercept) { | ||
if (typeof methods.intercept !== 'function') { | ||
throw new Error('`send` must be a function with the body to be sent as the only param'); | ||
} | ||
res.removeHeader('Content-Length'); | ||
@@ -84,11 +86,27 @@ // allow the user to re-write response | ||
debug(' methods.send isnt defined'); | ||
afterSend(oldBody,oldBody); | ||
originalEnd.apply(res,args); | ||
afterSend(oldBody, oldBody); | ||
originalEnd.apply(res, args); | ||
} | ||
} else { | ||
originalEnd.apply(res,args); | ||
originalEnd.apply(res, args); | ||
} | ||
}; | ||
next(); | ||
}; | ||
}; | ||
}; | ||
var VALID_PARAMS = ['isInterceptable', 'intercept', 'afterSend']; | ||
function _validateParams(methods) { | ||
for (var k in methods) { | ||
if (VALID_PARAMS.indexOf(k) < 0) { | ||
throw(new Error(k+' isn\'t a valid param (' + VALID_PARAMS.join(', ') + ')')); | ||
} | ||
} | ||
if (!('isInterceptable' in methods)) { | ||
throw('isInterceptable is a required param (function)'); | ||
} | ||
} |
{ | ||
"name": "express-interceptor", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "A tiny interceptor for Express responses", | ||
@@ -10,3 +10,3 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "./node_modules/mocha/bin/mocha -R spec --recursive tests" | ||
"test": "mocha" | ||
}, | ||
@@ -49,8 +49,5 @@ "author": "AxiomZen (https://axiomzen.co)", | ||
"jade": "^1.10.0", | ||
"magicpen-media": "1.5.0", | ||
"messy": "6.5.0", | ||
"mocha": "^2.2.5", | ||
"unexpected": "9.2.1", | ||
"unexpected-express": "7.3.0", | ||
"unexpected-messy": "4.6.0" | ||
"unexpected": "9.5.0", | ||
"unexpected-express": "7.7.0" | ||
}, | ||
@@ -57,0 +54,0 @@ "dependencies": { |
@@ -96,5 +96,6 @@ # express-interceptor | ||
* [express-jsxtransform](https://github.com/gustavnikolaj/express-jsxtransform) | ||
* [express-autoprefixer](https://github.com/gustavnikolaj/express-autoprefixer) | ||
* [express-compiless](https://github.com/papandreou/express-compiless) | ||
* [node-stylus-require](https://www.npmjs.com/package/node-stylus-require) | ||
* [express-html-minify](https://www.npmjs.com/package/express-html-minify) | ||
* [express-jare](https://www.npmjs.com/package/express-jare) | ||
* [hospitalrun-server-routes](https://www.npmjs.com/package/hospitalrun-server-routes) | ||
@@ -106,3 +107,6 @@ ## Similar to | ||
- [tamper](https://www.npmjs.com/package/tamper) | ||
- [hijackresponse](https://github.com/gustavnikolaj/hijackresponse) | ||
Attempting to solve same problems than `express-hijackresponse`. Different API using pipes, recommended if you have problems with [streaming](https://github.com/axiomzen/express-interceptor/issues/15) or if you need more control over responses. | ||
- [tamper](https://github.com/fgnass/tamper) | ||
Similar functionality but different internals and API. | ||
@@ -120,2 +124,4 @@ | ||
This project adheres to the [Open Code of Conduct](http://todogroup.org/opencodeofconduct/#express-interceptor/ipinoraul@gmail.com). By participating, you are expected to honor this code. | ||
## Author | ||
@@ -122,0 +128,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
26004
16.89%6
-33.33%27
22.73%516
24.94%136
4.62%8
14.29%1
Infinity%