Skip to content
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

feat(options): added options for follow redirects with max body length and max redirects #984

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,14 @@ export function verifyConfig<TReq, TRes>(options: Options<TReq, TRes>): void {
if (!options.target && !options.router) {
throw new Error(ERRORS.ERR_CONFIG_FACTORY_TARGET_MISSING);
}
if (options.followRedirects && options.followRedirectsOpts) {
if (
(options.followRedirectsOpts.maxRedirects !== undefined &&
options.followRedirectsOpts.maxRedirects <= 0) ||
(options.followRedirectsOpts.maxBodyLength !== undefined &&
options.followRedirectsOpts.maxBodyLength <= 0)
) {
throw new Error(ERRORS.ERR_FOLLOW_REDIRECT_INVALID_OPTIONS);
}
}
}
1 change: 1 addition & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export enum ERRORS {
ERR_CONTEXT_MATCHER_GENERIC = '[HPM] Invalid context. Expecting something like: "/api" or ["/api", "/ajax"]',
ERR_CONTEXT_MATCHER_INVALID_ARRAY = '[HPM] Invalid pathFilter. Expecting something like: ["/api", "/ajax"] or ["/api/**", "!**.html"]',
ERR_PATH_REWRITER_CONFIG = '[HPM] Invalid pathRewrite config. Expecting object with pathRewrite config or a rewrite function',
ERR_FOLLOW_REDIRECT_INVALID_OPTIONS = '[HPM] Invalid followRedirectsOpts config',
}
26 changes: 26 additions & 0 deletions src/http-proxy-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export class HttpProxyMiddleware<TReq, TRes> {

this.pathRewriter = PathRewriter.createPathRewriter(this.proxyOptions.pathRewrite); // returns undefined when "pathRewrite" is not provided

this.setFollowRedirectsOptions(options);

// https://github.com/chimurai/http-proxy-middleware/issues/19
// expose function to upgrade externally
this.middleware.upgrade = (req, socket, head) => {
Expand Down Expand Up @@ -84,6 +86,30 @@ export class HttpProxyMiddleware<TReq, TRes> {
});
}

/**
Sets follow-redirects module global options used internally by http-proxy. When followRedirects is true, http-proxy uses the http and https agents of the follow-redirects module https://github.com/http-party/node-http-proxy/blob/9b96cd725127a024dabebec6c7ea8c807272223d/lib/http-proxy/passes/web-incoming.js#L105
*/
private setFollowRedirectsOptions = (options: Options<TReq, TRes>) => {
if (!options.followRedirectsOpts) {
return;
}
// eslint-disable-next-line @typescript-eslint/no-var-requires
const followRedirects = require('follow-redirects'); // Since there is no way to access the module from http-proxy and also given that http-proxy is unmaintained.
if (!followRedirects) {
return;
}
// Sets follow-redirects global options according to https://github.com/follow-redirects/follow-redirects?tab=readme-ov-file#global-options
// This is a workaround, the options are set globally, ideally follow redirect options should be set through the http-proxy.
if (options.followRedirectsOpts.maxRedirects) {
followRedirects.maxRedirects = options.followRedirectsOpts.maxRedirects;
debug('set followRedirects.maxRedirects globally', followRedirects.maxRedirects);
}
if (options.followRedirectsOpts.maxBodyLength) {
followRedirects.maxBodyLength = options.followRedirectsOpts.maxBodyLength;
debug('set followRedirects.maxBodyLength globally', followRedirects.maxBodyLength);
}
};

private catchUpgradeRequest = (server: https.Server) => {
if (!this.wsInternalSubscribed) {
debug('subscribing to server upgrade event');
Expand Down
11 changes: 11 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,15 @@ export interface Options<TReq = http.IncomingMessage, TRes = http.ServerResponse
* @since v3.0.0
*/
logger?: Logger | any;

/**
* Configures options for follow-redirects when followRedirects is true.
* @property {number} [maxRedirects] - The maximum number of redirects to follow.
* @property {number} [maxBodyLength] - The maximum allowed size of the response body in bytes.
* @see {@link https://github.com/follow-redirects/follow-redirects?tab=readme-ov-file#global-options}
*/
followRedirectsOpts?: {
maxRedirects?: number;
maxBodyLength?: number;
};
}