express-http-proxy
Advanced tools
Comparing version 1.5.1 to 1.6.0
@@ -13,8 +13,20 @@ 'use strict'; | ||
return function(rspData, res) { | ||
return (isResGzipped(res) && rspData.length) ? zlib[method](rspData) : rspData; | ||
return new Promise(function (resolve, reject) { | ||
if (isResGzipped(res) && rspData.length) { | ||
zlib[method](rspData, function(err, buffer) { | ||
if(err) { | ||
reject(err); | ||
} else { | ||
resolve(buffer); | ||
} | ||
}); | ||
} else { | ||
resolve(rspData); | ||
} | ||
}); | ||
}; | ||
} | ||
var maybeUnzipResponse = zipOrUnzip('gunzipSync'); | ||
var maybeZipResponse = zipOrUnzip('gzipSync'); | ||
var maybeUnzipPromise = zipOrUnzip('gunzip'); | ||
var maybeZipPromise = zipOrUnzip('gzip'); | ||
@@ -44,6 +56,7 @@ function verifyBuffer(rspd, reject) { | ||
var proxyResData = maybeUnzipResponse(container.proxy.resData, container.proxy.res); | ||
var proxyResDataPromise = maybeUnzipPromise(container.proxy.resData, container.proxy.res); | ||
var proxyRes = container.proxy.res; | ||
var req = container.user.req; | ||
var res = container.user.res; | ||
var originalResData; | ||
@@ -55,4 +68,7 @@ if (res.statusCode === 304) { | ||
return Promise | ||
.resolve(resolverFn(proxyRes, proxyResData, req, res)) | ||
return proxyResDataPromise | ||
.then(function(proxyResData){ | ||
originalResData = proxyResData; | ||
return resolverFn(proxyRes, proxyResData, req, res); | ||
}) | ||
.then(function(modifiedResData) { | ||
@@ -62,5 +78,9 @@ return new Promise(function(resolve, reject) { | ||
verifyBuffer(rspd, reject); | ||
updateHeaders(res, proxyResData, rspd, reject); | ||
container.proxy.resData = maybeZipResponse(rspd, container.proxy.res); | ||
resolve(container); | ||
updateHeaders(res, originalResData, rspd, reject); | ||
maybeZipPromise(rspd, container.proxy.res).then(function(buffer) { | ||
container.proxy.resData = buffer; | ||
resolve(container); | ||
}).catch(function(error){ | ||
reject(error); | ||
}); | ||
}); | ||
@@ -67,0 +87,0 @@ }); |
@@ -6,3 +6,3 @@ 'use strict'; | ||
var resolverFn = container.options.userResHeaderDecorator; | ||
var headers = container.user.res._headers; | ||
var headers = container.user.res.getHeaders ? container.user.res.getHeaders() : container.user.res._headers; | ||
@@ -9,0 +9,0 @@ if (!resolverFn) { |
@@ -25,3 +25,3 @@ 'use strict'; | ||
'decorateRequest is REMOVED; use proxyReqOptDecorator and' + | ||
'proxyReqBodyDecorator instead. see README.md' | ||
' proxyReqBodyDecorator instead. see README.md' | ||
); | ||
@@ -33,3 +33,3 @@ } | ||
'forwardPath and forwardPathAsync are DEPRECATED' + | ||
'and should be replaced with proxyReqPathResolver' | ||
' and should be replaced with proxyReqPathResolver' | ||
); | ||
@@ -40,3 +40,3 @@ } | ||
console.warn( | ||
'DEPRECATED: intercept. Use decorateUseRes instead.' + | ||
'DEPRECATED: intercept. Use userResDecorator instead.' + | ||
' Please see README for more information.' | ||
@@ -43,0 +43,0 @@ ); |
{ | ||
"name": "express-http-proxy", | ||
"version": "1.5.1", | ||
"version": "1.6.0", | ||
"description": "http proxy middleware for express", | ||
@@ -37,3 +37,4 @@ "engines": { | ||
"mocha": "^5.2.0", | ||
"supertest": "^3.0.0" | ||
"nock": "^10.0.6", | ||
"supertest": "^3.4.2" | ||
}, | ||
@@ -40,0 +41,0 @@ "dependencies": { |
@@ -555,3 +555,3 @@ # express-http-proxy [![NPM version](https://badge.fury.io/js/express-http-proxy.svg)](http://badge.fury.io/js/express-http-proxy) [![Build Status](https://travis-ci.org/villadora/express-http-proxy.svg?branch=master)](https://travis-ci.org/villadora/express-http-proxy) [![Dependency Status](https://gemnasium.com/villadora/express-http-proxy.svg)](https://gemnasium.com/villadora/express-http-proxy) | ||
} | ||
}) | ||
})) | ||
``` | ||
@@ -564,2 +564,3 @@ | ||
| --- | --- | | ||
| 1.6.0 | Do gzip and gunzip aysyncronously. Test and documentation improvements, dependency updates. | | ||
| 1.5.1 | Fixes bug in stringifying debug messages. | | ||
@@ -566,0 +567,0 @@ | 1.5.0 | Fixes bug in `filter` signature. Fix bug in skipToNextHandler, add expressHttpProxy value to user res when skipped. Add tests for host as ip address. | |
@@ -50,3 +50,3 @@ 'use strict'; | ||
app.use(proxy('localhost:12346', { | ||
intercept: function (rsp, data, req) { | ||
userResDecorator: function (rsp, data, req) { | ||
assert(req.body); | ||
@@ -53,0 +53,0 @@ assert.equal(req.body.foo, 1); |
'use strict'; | ||
var assert = require('assert'); | ||
var bodyParser = require('body-parser'); | ||
var express = require('express'); | ||
var nock = require('nock'); | ||
var request = require('supertest'); | ||
var proxy = require('../'); | ||
var bodyParser = require('body-parser'); | ||
@@ -15,13 +16,2 @@ | ||
function createProxyApplicationServer() { | ||
var pTarget = express(); | ||
pTarget.use(bodyParser.json()); | ||
pTarget.use(bodyParser.urlencoded({ extended: true })); | ||
pTarget.use(function (req, res) { | ||
assert(req.body.name === 'tobi'); //, 'Assert that the value posted to the local server is passed to the proxy'); | ||
res.json(req.body); | ||
}); | ||
return pTarget.listen(12345); | ||
} | ||
describe('when proxy request is a POST', function () { | ||
@@ -32,11 +22,10 @@ | ||
var localServer; | ||
var proxyServer; | ||
beforeEach(function () { | ||
localServer = createLocalApplicationServer(); | ||
proxyServer = createProxyApplicationServer(); | ||
localServer.use(bodyParser.json()); | ||
}); | ||
afterEach(function () { | ||
proxyServer.close(); | ||
nock.cleanAll(); | ||
}); | ||
@@ -50,3 +39,33 @@ | ||
testCases.forEach(function (test) { | ||
it('should deliver the post query when ' + test.name, function (done) { | ||
var nockedPostWithEncoding = nock('http://127.0.0.1:12345') | ||
.post('/') | ||
.query({ name: 'tobi' }) | ||
.matchHeader('Content-Type', test.encoding) | ||
.reply(200, { | ||
name: 'tobi' | ||
}); | ||
localServer.use('/proxy', proxy('http://127.0.0.1:12345')); | ||
localServer.use(function (req, res) { res.sendStatus(200); }); | ||
localServer.use(function (err, req, res, next) { throw new Error(err, req, res, next); }); | ||
request(localServer) | ||
.post('/proxy') | ||
.query({ name: 'tobi' }) | ||
.set('Content-Type', test.encoding) | ||
.expect(function (res) { | ||
assert(res.body.name === 'tobi'); | ||
nockedPostWithEncoding.done(); | ||
}) | ||
.end(done); | ||
}); | ||
it('should deliver the post body when ' + test.name, function (done) { | ||
var nockedPostWithEncoding = nock('http://127.0.0.1:12345') | ||
.post('/', test.encoding.includes('json') ? { name: 'tobi' } : {}) | ||
.matchHeader('Content-Type', test.encoding) | ||
.reply(200, { | ||
name: 'tobi' | ||
}); | ||
@@ -63,2 +82,3 @@ localServer.use('/proxy', proxy('http://127.0.0.1:12345')); | ||
assert(res.body.name === 'tobi'); | ||
nockedPostWithEncoding.done(); | ||
}) | ||
@@ -69,2 +89,48 @@ .end(done); | ||
it('should deliver empty string post body', function (done) { | ||
var nockedPostWithoutBody = nock('http://127.0.0.1:12345') | ||
.post('/') | ||
.matchHeader('Content-Type', 'application/json') | ||
.reply(200, { | ||
name: 'tobi' | ||
}); | ||
localServer.use('/proxy', proxy('http://127.0.0.1:12345')); | ||
localServer.use(function (req, res) { res.sendStatus(200); }); | ||
localServer.use(function (err, req, res, next) { throw new Error(err, req, res, next); }); | ||
request(localServer) | ||
.post('/proxy') | ||
.send('') | ||
.set('Content-Type', 'application/json') | ||
.expect(function (res) { | ||
assert(res.body.name === 'tobi'); | ||
nockedPostWithoutBody.done(); | ||
}) | ||
.end(done); | ||
}); | ||
it('should deliver empty object post body', function (done) { | ||
var nockedPostWithoutBody = nock('http://127.0.0.1:12345') | ||
.post('/', {}) | ||
.matchHeader('Content-Type', 'application/json') | ||
.reply(200, { | ||
name: 'tobi' | ||
}); | ||
localServer.use('/proxy', proxy('http://127.0.0.1:12345')); | ||
localServer.use(function (req, res) { res.sendStatus(200); }); | ||
localServer.use(function (err, req, res, next) { throw new Error(err, req, res, next); }); | ||
request(localServer) | ||
.post('/proxy') | ||
.send({}) | ||
.set('Content-Type', 'application/json') | ||
.expect(function (res) { | ||
assert(res.body.name === 'tobi'); | ||
nockedPostWithoutBody.done(); | ||
}) | ||
.end(done); | ||
}); | ||
}); |
@@ -10,11 +10,25 @@ 'use strict'; | ||
/* This test is specifically written because of critical errors thrown while debug logging. */ | ||
describe.only('trace debugging does not cause the application to fail', function () { | ||
describe('trace debugging does not cause the application to fail', function () { | ||
var proxyServer; | ||
beforeEach(function () { debug.enable('express-http-proxy'); proxyServer = proxyTarget(3000); }); | ||
afterEach(function () { debug.disable('express-http-proxy'); proxyServer.close(); }); | ||
before(function () { | ||
proxyServer = proxyTarget(8109, 1000); | ||
}); | ||
after(function () { | ||
proxyServer.close(); | ||
}); | ||
beforeEach(function () { | ||
debug.enable('express-http-proxy'); | ||
}); | ||
afterEach(function () { | ||
debug.disable('express-http-proxy'); | ||
}); | ||
it('happy path', function (done) { | ||
debugger; | ||
var app = express(); | ||
app.use(proxy('localhost:3000')); | ||
app.use(proxy('localhost:8109')); | ||
request(app) | ||
@@ -30,3 +44,3 @@ .get('/get') | ||
var httpAgent = new HttpAgent({ keepAlive: true, keepAliveMsecs: 60e3 }); | ||
app.use(proxy('localhost:3000', { | ||
app.use(proxy('localhost:8109', { | ||
proxyReqOptDecorator: function (proxyReqOpts) { | ||
@@ -33,0 +47,0 @@ proxyReqOpts.agent = httpAgent; |
@@ -163,3 +163,3 @@ 'use strict'; | ||
userResDecorator: function (rsp, data, req, res) { | ||
var proxyReturnedLocation = res._headers.location; | ||
var proxyReturnedLocation = res.getHeaders ? res.getHeaders().location : res._headers.location; | ||
res.location(proxyReturnedLocation.replace(redirectingServerPort, preferredPort)); | ||
@@ -166,0 +166,0 @@ return data; |
@@ -27,3 +27,3 @@ 'use strict'; | ||
assert(/node-superagent/.test(res.body.headers['User-Agent'])); | ||
assert.equal(res.body.url, 'http://httpbin.org/get'); | ||
assert.equal(res.body.url, 'https://httpbin.org/get'); | ||
done(err); | ||
@@ -30,0 +30,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
113339
57
2726
593
8