Skip to content
Open
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
20 changes: 8 additions & 12 deletions app/steps/resolveProxyHost.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@
var requestOptions = require('../../lib/requestOptions');

function resolveProxyHost(container) {
var parsedHost;

if (container.options.memoizeHost && container.options.memoizedHost) {
parsedHost = container.options.memoizedHost;
} else {
parsedHost = 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.resolve(container.options.memoizeHost && container.options.memoizedHost ?
container.options.memoizedHost :
requestOptions.parseHost(container)).then(parsedHost => {
container.proxy.reqBuilder.host = parsedHost.host;
container.proxy.reqBuilder.port = container.options.port || parsedHost.port;
container.proxy.requestModule = parsedHost.module;
return Promise.resolve(container);
});
}

module.exports = resolveProxyHost;
64 changes: 64 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Type definitions for express-http-proxy 1.5
// Project: https://github.com/villadora/express-http-proxy#readme
// Definitions by: ulrichb <https://github.com/ulrichb>
// Daniel Schopf <https://github.com/Danscho>
// Gabriel Fournier <https://github.com/carboneater>
// Niek van Bennekom <https://github.com/niekvb>
// John L. Singleton <https://github.com/jsinglet>
// Lindsay Wardell <https://github.com/lindsaykwardell>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3

import { RequestHandler, Request, Response, NextFunction } from "express";
import { RequestOptions, IncomingHttpHeaders, OutgoingHttpHeaders } from "http";

declare namespace proxy {
interface ProxyOptions {
/**
* The byte limit of the body. This is the number of bytes or any string
* format supported by `bytes`, for example `1000`, `'500kb'` or `'3mb'`.
* See https://github.com/stream-utils/raw-body/blob/master/index.d.ts
*/
limit?: number | string;
proxyErrorHandler?: (
err: any,
res: Response,
next: NextFunction
) => any;
proxyReqPathResolver?: (req: Request) => string;
proxyReqOptDecorator?: (
proxyReqOpts: RequestOptions,
srcReq: Request
) => RequestOptions | Promise<RequestOptions>;
userResHeaderDecorator?: (
headers: IncomingHttpHeaders,
userReq: Request,
userRes: Response,
proxyReq: Request,
proxyRes: Response
) => OutgoingHttpHeaders;
userResDecorator?: (
proxyRes: Response,
proxyResData: any,
userReq: Request,
userRes: Response
) => Buffer | string | Promise<Buffer | string>;
filter?: (req: Request, res: Response) => boolean;
skipToNextHandlerFilter?: (proxyRes: Response) => boolean;
proxyReqBodyDecorator?: (bodyContent: any, srcReq: Request) => any;
preserveHostHdr?: boolean;
parseReqBody?: boolean;
memoizeHost?: boolean;
https?: boolean;
reqAsBuffer?: boolean;
reqBodyEncoding?: string | null;
timeout?: number;
}
}

declare function proxy(
host: string | ((req: Request) => PromiseLike<string> | string),
options?: proxy.ProxyOptions
): RequestHandler;

export = proxy;
37 changes: 20 additions & 17 deletions lib/requestOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,32 @@ 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');
}
return Promise.resolve(typeof host === 'string' ? host : host(req)).then(host => {
if (!host) {
//return Promise.reject('Empty host parameter');
return new Error('Empty host parameter');
}

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

var parsed = url.parse(host);
var parsed = url.parse(host);

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

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

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

function reqHeaders(req, options) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"node": ">=6.0.0"
},
"main": "index.js",
"types": "index.d.ts",
"scripts": {
"test": "npm -s run mocha && npm run -s lint",
"test:debug": "mocha debug -R spec test --recursive --exit",
Expand Down
Loading