express-http-proxy
Advanced tools
Comparing version 0.3.0 to 0.3.1
36
index.js
@@ -43,3 +43,3 @@ var assert = require('assert'); | ||
return function handleProxy(req, res, next) { | ||
if (filter && !filter(req, res)) next(); | ||
if (filter && !filter(req, res)) return next(); | ||
@@ -97,11 +97,26 @@ var headers = options.headers || {}; | ||
if (intercept) { | ||
intercept(rspData, req, res, function(err, rsp, sent) { | ||
intercept(rspData, req, res, function(err, rspd, sent) { | ||
if (err) { | ||
return next(err); | ||
} | ||
var encode = 'utf8'; | ||
if (rsp.headers && rsp.headers['content-type']) { | ||
var contentType = rsp.headers['content-type']; | ||
if (/charset=/.test(contentType)) { | ||
var attrs = contentType.split(';').map(function(str) { return str.trim(); }); | ||
for(var i = 0, len = attrs.length; i < len; i++) { | ||
var attr = attrs[i]; | ||
if (/charset=/.test(attr)) { | ||
// encode = attr.split('=')[1]; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
if (typeof rspd == 'string') | ||
rspd = new Buffer(rspd, encode); | ||
if (typeof rsp == 'string') | ||
rsp = new Buffer(rsp, 'utf8'); | ||
if (!Buffer.isBuffer(rsp)) { | ||
if (!Buffer.isBuffer(rspd)) { | ||
next(new Error("intercept should return string or buffer as data")); | ||
@@ -111,9 +126,10 @@ } | ||
if (!res.headersSent) | ||
res.set('content-length', rsp.length); | ||
else if (rsp.length != rspData.length) { | ||
res.set('content-length', rspd.length); | ||
else if (rspd.length != rspData.length) { | ||
next(new Error("'Content-Length' is already sent, the length of response data can not be changed")); | ||
} | ||
if (!sent) | ||
res.send(rsp); | ||
if (!sent) { | ||
res.send(rspd); | ||
} | ||
}); | ||
@@ -120,0 +136,0 @@ } else { |
{ | ||
"name": "express-http-proxy", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "http proxy middleware for express", | ||
@@ -30,4 +30,5 @@ "main": "index.js", | ||
"express": "^4.3.1", | ||
"iconv": "^2.1.4", | ||
"jshint": "^2.5.5", | ||
"mocha": "^1.19.0", | ||
"mocha": "^2.1.0", | ||
"supertest": "^0.13.0" | ||
@@ -39,17 +40,23 @@ }, | ||
}, | ||
"contributors": [{ | ||
"contributors": [ | ||
{ | ||
"name": "Liam Bennett" | ||
}, { | ||
}, | ||
{ | ||
"name": "eldereal", | ||
"url": "https://github.com/eldereal" | ||
}, { | ||
}, | ||
{ | ||
"name": "Saulius Menkevičius", | ||
"url": "https://github.com/razzmatazz" | ||
}, { | ||
}, | ||
{ | ||
"name": "Jérémy Lal", | ||
"email": "kapouer@melix.org" | ||
}, { | ||
}, | ||
{ | ||
"name": "Wei Gao", | ||
"email": "jky239@gmail.com" | ||
}] | ||
} | ||
] | ||
} |
@@ -68,3 +68,39 @@ var assert = require('assert'); | ||
it('test intercept on html response',function(done) { | ||
var app = express(); | ||
app.use(proxy('httpbin.org', { | ||
intercept: function(data, req, res, cb) { | ||
data = data.toString().replace('Oh','<strong>Hey</strong>'); | ||
assert(data !== ""); | ||
cb(null, data); | ||
} | ||
})); | ||
request(app) | ||
.get('/html') | ||
.end(function(err, res) { | ||
if (err) return done(err); | ||
assert(res.text.indexOf('<strong>Hey</strong>') > -1); | ||
done(); | ||
}); | ||
}); | ||
it('test github api', function(done) { | ||
var app = express(); | ||
app.use(proxy('https://api.github.com', { | ||
intercept: function(data, req, res, cb) { | ||
var Iconv = require('iconv').Iconv; | ||
var iconv = new Iconv('UTF-8', 'utf8'); | ||
cb(null, data); | ||
} | ||
})); | ||
request(app) | ||
.get('/') | ||
.end(function(err, res) { | ||
if (err) return done(err); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
@@ -71,0 +107,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
14010
308
5
8