Skip to content

Adding Promise support for "getHost". #285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,11 @@ E.g.,
```js

function coinToss() { return Math.random() > .5 }
function getHost() { return coinToss() ? 'http://yahoo.com' : 'http://google.com' }

function getHost() {
var host = coinToss() ? 'http://yahoo.com' : 'http://google.com';
return coinToss() ? Promise.resolve(host) : host;
}

app.use(proxy(getHost, {
memoizeHost: false
}))
Expand Down Expand Up @@ -451,7 +454,7 @@ app.use('/', proxy('internalhost.example.com', {
proxyReqOpts.ca = [caCert, intermediaryCert]
return proxyReqOpts;
}
})
}))
```


Expand Down
16 changes: 9 additions & 7 deletions app/steps/resolveProxyHost.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@
var requestOptions = require('../../lib/requestOptions');

function resolveProxyHost(container) {
var parsedHost;
var promise;

if (container.options.memoizeHost && container.options.memoizedHost) {
parsedHost = container.options.memoizedHost;
promise = Promise.resolve(container.options.memoizedHost);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first glance, this appears to conflict with the goal of memoizing the host.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a case where an external server defines which users should go to server A and which should go to server B. In that case, every incoming request would query that external server. I plan to cache the results (MRU + expiration), but I still need an async support to determine the target host url. Does it make sense?

} else {
parsedHost = requestOptions.parseHost(container);
promise = Promise.resolve(requestOptions.parseHost(container));
}

container.proxy.reqBuilder.host = parsedHost.host;
container.proxy.reqBuilder.port = container.options.port || parsedHost.port;
container.proxy.requestModule = parsedHost.module;
return Promise.resolve(container);
return promise.then(function(parsedHost) {
container.proxy.reqBuilder.host = parsedHost.host;
container.proxy.reqBuilder.port = container.options.port || parsedHost.port;
container.proxy.requestModule = parsedHost.module;
return container;
});
}

module.exports = resolveProxyHost;
51 changes: 28 additions & 23 deletions lib/requestOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,34 @@ function parseHost(Container) {
var host = Container.params.host;
var req = Container.user.req;
var options = Container.options;
host = (typeof host === 'function') ? host(req) : host.toString();

if (!host) {
return new Error('Empty host parameter');
}

if (!/http(s)?:\/\//.test(host)) {
host = 'http://' + host;
}

var parsed = url.parse(host);

if (!parsed.hostname) {
return new Error('Unable to parse hostname, possibly missing protocol://?');
}

var ishttps = options.https || parsed.protocol === 'https:';

return {
host: parsed.hostname,
port: parsed.port || (ishttps ? 443 : 80),
module: ishttps ? https : http,
};
host = (typeof host === 'function') ? host(req) : host;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dropped toString() here. investigate why.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the toString() is needed. Anyways, I moved it to line 35 to support backward compatibility.


return Promise
.resolve(host)
.then(function(host) {
if (!host) {
throw new Error('Empty host parameter');
}
host = host.toString();

if (!/http(s)?:\/\//.test(host)) {
host = 'http://' + host;
}

var parsed = url.parse(host);

if (!parsed.hostname) {
return new Error('Unable to parse hostname, possibly missing protocol://?');
}

var ishttps = options.https || parsed.protocol === 'https:';

return {
host: parsed.hostname,
port: parsed.port || (ishttps ? 443 : 80),
module: ishttps ? https : http,
};
});
}

function reqHeaders(req, options) {
Expand Down
26 changes: 26 additions & 0 deletions test/host.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,29 @@ describe('host can be a dynamic function', function() {
});
});
});

describe('host is a promise', function() {

this.timeout(10000);

var app = express();
var proxyApp = express();
var port = Math.floor(Math.random() * 10000);

var hostFn = function(req) {
return Promise.resolve('localhost:' + req.params.port);
};

app.use('/proxy-promise/:port', proxy(hostFn, { memoizeHost: false }));

proxyApp
.get('/', function(req, res) { res.sendStatus(201); })
.listen(port);

it('host returns a promise that should be resolved and its value is the host URL.', function(done) {
request(app)
.get('/proxy-promise/' + port)
.expect(201)
.end(done);
});
});