Merge pull request #5949 from backstage/dependabot/npm_and_yarn/http-proxy-middleware-2.0.0

chore(deps): bump http-proxy-middleware from 0.19.2 to 2.0.0
This commit is contained in:
Fredrik Adelöw
2021-06-14 10:30:28 +02:00
committed by GitHub
6 changed files with 69 additions and 62 deletions
+1 -1
View File
@@ -33,7 +33,7 @@
"@types/express": "^4.17.6",
"express": "^4.17.1",
"express-promise-router": "^4.1.0",
"http-proxy-middleware": "^0.19.1",
"http-proxy-middleware": "^2.0.0",
"morgan": "^1.10.0",
"uuid": "^8.0.0",
"winston": "^3.2.1",
@@ -14,25 +14,19 @@
* limitations under the License.
*/
import { buildMiddleware, createRouter } from './router';
import {
getVoidLogger,
loadBackendConfig,
SingleHostDiscovery,
} from '@backstage/backend-common';
import createProxyMiddleware, {
Config as ProxyMiddlewareConfig,
Proxy,
} from 'http-proxy-middleware';
import { Request, Response } from 'express';
import * as http from 'http';
import { createProxyMiddleware, Options } from 'http-proxy-middleware';
import { buildMiddleware, createRouter } from './router';
jest.mock('http-proxy-middleware', () => {
return jest.fn().mockImplementation(
(): Proxy => {
return () => undefined;
},
);
});
jest.mock('http-proxy-middleware', () => ({
createProxyMiddleware: jest.fn(() => () => undefined),
}));
const mockCreateProxyMiddleware = createProxyMiddleware as jest.MockedFunction<
typeof createProxyMiddleware
@@ -66,7 +60,7 @@ describe('buildMiddleware', () => {
const [filter, fullConfig] = mockCreateProxyMiddleware.mock.calls[0] as [
(pathname: string, req: Partial<http.IncomingMessage>) => boolean,
ProxyMiddlewareConfig,
Options,
];
expect(filter('', { method: 'GET', headers: {} })).toBe(true);
expect(filter('', { method: 'POST', headers: {} })).toBe(true);
@@ -86,7 +80,7 @@ describe('buildMiddleware', () => {
const [filter, fullConfig] = mockCreateProxyMiddleware.mock.calls[0] as [
(pathname: string, req: Partial<http.IncomingMessage>) => boolean,
ProxyMiddlewareConfig,
Options,
];
expect(filter('', { method: 'GET', headers: {} })).toBe(true);
expect(filter('', { method: 'POST', headers: {} })).toBe(true);
@@ -106,7 +100,7 @@ describe('buildMiddleware', () => {
const [filter, fullConfig] = mockCreateProxyMiddleware.mock.calls[0] as [
(pathname: string, req: Partial<http.IncomingMessage>) => boolean,
ProxyMiddlewareConfig,
Options,
];
expect(filter('', { method: 'GET', headers: {} })).toBe(true);
expect(filter('', { method: 'POST', headers: {} })).toBe(true);
@@ -129,7 +123,7 @@ describe('buildMiddleware', () => {
const [filter, fullConfig] = mockCreateProxyMiddleware.mock.calls[0] as [
(pathname: string, req: Partial<http.IncomingMessage>) => boolean,
ProxyMiddlewareConfig,
Options,
];
expect(filter('', { method: 'GET', headers: {} })).toBe(true);
expect(filter('', { method: 'POST', headers: {} })).toBe(false);
@@ -254,8 +248,7 @@ describe('buildMiddleware', () => {
expect(createProxyMiddleware).toHaveBeenCalledTimes(1);
const config = mockCreateProxyMiddleware.mock
.calls[0][1] as ProxyMiddlewareConfig;
const config = mockCreateProxyMiddleware.mock.calls[0][1] as Options;
const testClientResponse = {
headers: {
@@ -275,8 +268,8 @@ describe('buildMiddleware', () => {
config.onProxyRes!(
testClientResponse as http.IncomingMessage,
{} as http.IncomingMessage,
{} as http.ServerResponse,
{} as Request,
{} as Response,
);
expect(Object.keys(testClientResponse.headers!)).toEqual([
@@ -298,8 +291,7 @@ describe('buildMiddleware', () => {
expect(createProxyMiddleware).toHaveBeenCalledTimes(1);
const config = mockCreateProxyMiddleware.mock
.calls[0][1] as ProxyMiddlewareConfig;
const config = mockCreateProxyMiddleware.mock.calls[0][1] as Options;
const testClientResponse = {
headers: {
@@ -313,8 +305,8 @@ describe('buildMiddleware', () => {
config.onProxyRes!(
testClientResponse as http.IncomingMessage,
{} as http.IncomingMessage,
{} as http.ServerResponse,
{} as Request,
{} as Response,
);
expect(Object.keys(testClientResponse.headers!)).toEqual(['set-cookie']);
+11 -7
View File
@@ -17,9 +17,10 @@
import { Config } from '@backstage/config';
import express from 'express';
import Router from 'express-promise-router';
import createProxyMiddleware, {
Config as ProxyMiddlewareConfig,
Proxy,
import {
createProxyMiddleware,
Options,
RequestHandler,
} from 'http-proxy-middleware';
import { Logger } from 'winston';
import http from 'http';
@@ -52,7 +53,7 @@ export interface RouterOptions {
discovery: PluginEndpointDiscovery;
}
export interface ProxyConfig extends ProxyMiddlewareConfig {
export interface ProxyConfig extends Options {
allowedMethods?: string[];
allowedHeaders?: string[];
}
@@ -64,14 +65,17 @@ export function buildMiddleware(
logger: Logger,
route: string,
config: string | ProxyConfig,
): Proxy {
): RequestHandler {
const fullConfig =
typeof config === 'string' ? { target: config } : { ...config };
// Validate that target is a valid URL.
if (typeof fullConfig.target !== 'string') {
throw new Error(`Proxy target must be a string`);
}
try {
// eslint-disable-next-line no-new
new URL(fullConfig.target!);
new URL(fullConfig.target! as string);
} catch {
throw new Error(
`Proxy target is not a valid URL: ${fullConfig.target ?? ''}`,
@@ -131,7 +135,7 @@ export function buildMiddleware(
// 2. Only permit the allowed HTTP methods if configured
//
// We are filtering the proxy request headers here rather than in
// `onProxyReq` becuase when global-agent is enabled then `onProxyReq`
// `onProxyReq` because when global-agent is enabled then `onProxyReq`
// fires _after_ the agent has already sent the headers to the proxy
// target, causing a ERR_HTTP_HEADERS_SENT crash
const filter = (_pathname: string, req: http.IncomingMessage): boolean => {