Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2021-06-08 14:17:42 +02:00
parent 3db3b67f5f
commit 6ffcf9ed85
5 changed files with 51 additions and 50 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-proxy-backend': patch
---
Bump http-proxy-middleware from 0.19.2 to 2.0.0
+1 -1
View File
@@ -139,7 +139,7 @@
"@types/rollup-plugin-postcss": "^2.0.0",
"@types/tar": "^4.0.3",
"@types/webpack": "^4.41.7",
"@types/webpack-dev-server": "3.11.0",
"@types/webpack-dev-server": "^3.11.0",
"@types/yarnpkg__lockfile": "^1.1.4",
"del": "^5.1.0",
"mock-fs": "^4.13.0",
@@ -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 => {
+18 -18
View File
@@ -6182,7 +6182,7 @@
dependencies:
"@types/node" "*"
"@types/http-proxy-middleware@*", "@types/http-proxy-middleware@^0.19.3":
"@types/http-proxy-middleware@^0.19.3":
version "0.19.3"
resolved "https://registry.npmjs.org/@types/http-proxy-middleware/-/http-proxy-middleware-0.19.3.tgz#b2eb96fbc0f9ac7250b5d9c4c53aade049497d03"
integrity sha512-lnBTx6HCOUeIJMLbI/LaL5EmdKLhczJY5oeXZpX/cXE4rRqb3RmV7VcMpiEfYkmTjipv3h7IAyIINe4plEv7cA==
@@ -7025,27 +7025,16 @@
resolved "https://registry.npmjs.org/@types/uuid/-/uuid-8.3.0.tgz#215c231dff736d5ba92410e6d602050cce7e273f"
integrity sha512-eQ9qFW/fhfGJF8WKHGEHZEyVWfZxrT+6CLIJGBcZPfxUh/+BnEj+UCGYMlr9qZuX/2AltsvwrGqp0LhEW8D0zQ==
"@types/webpack-dev-server@*":
version "3.11.1"
resolved "https://registry.npmjs.org/@types/webpack-dev-server/-/webpack-dev-server-3.11.1.tgz#f8f4dac1da226d530bd15a1d5dc34b23ba766ccb"
integrity sha512-rIb+LtUkKnh7+oIJm3WiMJONd71Q0lZuqGLcSqhZ5qjN9gV/CNmZe7Bai+brnBPZ/KVYOsr+4bFLiNZwjBicLw==
"@types/webpack-dev-server@*", "@types/webpack-dev-server@^3.11.0":
version "3.11.4"
resolved "https://registry.npmjs.org/@types/webpack-dev-server/-/webpack-dev-server-3.11.4.tgz#90d47dd660b696d409431ab8c1e9fa3615103a07"
integrity sha512-DCKORHjqNNVuMIDWFrlljftvc9CL0+09p3l7lBpb8dRqgN5SmvkWCY4MPKxoI6wJgdRqohmoNbptkxqSKAzLRg==
dependencies:
"@types/connect-history-api-fallback" "*"
"@types/express" "*"
"@types/http-proxy-middleware" "*"
"@types/serve-static" "*"
"@types/webpack" "*"
"@types/webpack-dev-server@3.11.0":
version "3.11.0"
resolved "https://registry.npmjs.org/@types/webpack-dev-server/-/webpack-dev-server-3.11.0.tgz#bcc3b85e7dc6ac2db25330610513f2228c2fcfb2"
integrity sha512-3+86AgSzl18n5P1iUP9/lz3G3GMztCp+wxdDvVuNhx1sr1jE79GpYfKHL8k+Vht3N74K2n98CuAEw4YPJCYtDA==
dependencies:
"@types/connect-history-api-fallback" "*"
"@types/express" "*"
"@types/http-proxy-middleware" "*"
"@types/serve-static" "*"
"@types/webpack" "*"
"@types/webpack" "^4"
http-proxy-middleware "^1.0.0"
"@types/webpack-env@^1.15.2", "@types/webpack-env@^1.15.3":
version "1.16.0"
@@ -15181,6 +15170,17 @@ http-proxy-middleware@0.19.1:
lodash "^4.17.11"
micromatch "^3.1.10"
http-proxy-middleware@^1.0.0:
version "1.3.1"
resolved "https://registry.npmjs.org/http-proxy-middleware/-/http-proxy-middleware-1.3.1.tgz#43700d6d9eecb7419bf086a128d0f7205d9eb665"
integrity sha512-13eVVDYS4z79w7f1+NPllJtOQFx/FdUW4btIvVRMaRlUY9VGstAbo5MOhLEuUgZFRHn3x50ufn25zkj/boZnEg==
dependencies:
"@types/http-proxy" "^1.17.5"
http-proxy "^1.18.1"
is-glob "^4.0.1"
is-plain-obj "^3.0.0"
micromatch "^4.0.2"
http-proxy-middleware@^2.0.0:
version "2.0.0"
resolved "https://registry.npmjs.org/http-proxy-middleware/-/http-proxy-middleware-2.0.0.tgz#20d1ac3409199c83e5d0383ba6436b04e7acb9fe"