1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Fix resolve system proxy error when no windows available (#7375)

* fix resolve system proxy error when no windows available

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* fix electronBrowserWindowInjectable id

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

---------

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
Signed-off-by: Gabriel <gaccettola@mirantis.com>
This commit is contained in:
Jari Kolehmainen 2023-03-17 21:03:55 +02:00 committed by Gabriel
parent 87572703ca
commit d322b9511a
6 changed files with 70 additions and 84 deletions

View File

@ -22,7 +22,6 @@ import electronQuitAndInstallUpdateInjectable from "./electron-app/features/elec
import electronUpdaterIsActiveInjectable from "./electron-app/features/electron-updater-is-active.injectable"; import electronUpdaterIsActiveInjectable from "./electron-app/features/electron-updater-is-active.injectable";
import setUpdateOnQuitInjectable from "./electron-app/features/set-update-on-quit.injectable"; import setUpdateOnQuitInjectable from "./electron-app/features/set-update-on-quit.injectable";
import waitUntilBundledExtensionsAreLoadedInjectable from "./start-main-application/lens-window/application-window/wait-until-bundled-extensions-are-loaded.injectable"; import waitUntilBundledExtensionsAreLoadedInjectable from "./start-main-application/lens-window/application-window/wait-until-bundled-extensions-are-loaded.injectable";
import electronInjectable from "./utils/resolve-system-proxy/electron.injectable";
import initializeClusterManagerInjectable from "./cluster/initialize-manager.injectable"; import initializeClusterManagerInjectable from "./cluster/initialize-manager.injectable";
import type { GlobalOverride } from "@k8slens/test-utils"; import type { GlobalOverride } from "@k8slens/test-utils";
import { getOverrideFsWithFakes } from "../test-utils/override-fs-with-fakes"; import { getOverrideFsWithFakes } from "../test-utils/override-fs-with-fakes";
@ -56,7 +55,6 @@ export function getDiForUnitTesting() {
di.override(globalOverride.injectable, globalOverride.overridingInstantiate); di.override(globalOverride.injectable, globalOverride.overridingInstantiate);
} }
di.override(electronInjectable, () => ({}));
di.override(waitUntilBundledExtensionsAreLoadedInjectable, () => async () => {}); di.override(waitUntilBundledExtensionsAreLoadedInjectable, () => async () => {});
overrideRunnablesHavingSideEffects(di); overrideRunnablesHavingSideEffects(di);

View File

@ -0,0 +1,19 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getGlobalOverride } from "@k8slens/test-utils";
import type { BrowserWindow, Session, WebContents } from "electron";
import electronBrowserWindowInjectable from "./electron-browser-window.injectable";
export default getGlobalOverride(
electronBrowserWindowInjectable,
() => () => ({
webContents: {
session: {
resolveProxy: () => "DIRECT",
} as unknown as Session,
} as unknown as WebContents,
} as unknown as BrowserWindow),
);

View File

@ -0,0 +1,19 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import type { BrowserWindowConstructorOptions } from "electron";
import { BrowserWindow } from "electron";
const electronBrowserWindowInjectable = getInjectable({
id: "electron-browser-window",
instantiate: () => {
return (opts: BrowserWindowConstructorOptions) => {
return new BrowserWindow(opts);
};
},
causesSideEffects: true,
});
export default electronBrowserWindowInjectable;

View File

@ -1,14 +0,0 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import electron from "electron";
const electronInjectable = getInjectable({
id: "electron",
instantiate: () => electron,
causesSideEffects: true,
});
export default electronInjectable;

View File

@ -3,28 +3,22 @@
* Licensed under MIT License. See LICENSE in root directory for more information. * Licensed under MIT License. See LICENSE in root directory for more information.
*/ */
import { getInjectable } from "@ogre-tools/injectable"; import { getInjectable } from "@ogre-tools/injectable";
import electronInjectable from "./electron.injectable"; import electronBrowserWindowInjectable from "./electron-browser-window.injectable";
import withErrorLoggingInjectable from "../../../common/utils/with-error-logging/with-error-logging.injectable"; import withErrorLoggingInjectable from "../../../common/utils/with-error-logging/with-error-logging.injectable";
const resolveSystemProxyFromElectronInjectable = getInjectable({ const resolveSystemProxyFromElectronInjectable = getInjectable({
id: "resolve-system-proxy-from-electron", id: "resolve-system-proxy-from-electron",
instantiate: (di) => { instantiate: (di) => {
const electron = di.inject(electronInjectable); const browserWindow = di.inject(electronBrowserWindowInjectable);
const withErrorLoggingFor = di.inject(withErrorLoggingInjectable); const withErrorLoggingFor = di.inject(withErrorLoggingInjectable);
const withErrorLogging = withErrorLoggingFor(() => "Error resolving proxy"); const withErrorLogging = withErrorLoggingFor(() => "Error resolving proxy");
const hiddenWindow = browserWindow({
show: false,
});
return withErrorLogging(async (url: string) => { return withErrorLogging(async (url: string) => {
const webContent = electron.webContents return await hiddenWindow.webContents.session.resolveProxy(url);
.getAllWebContents()
.find((x) => !x.isDestroyed());
if (!webContent) {
throw new Error(`Tried to resolve proxy for "${url}", but no browser window was available`);
}
return await webContent.session.resolveProxy(url);
}); });
}, },
}); });

View File

@ -5,13 +5,13 @@
import { getDiForUnitTesting } from "../../getDiForUnitTesting"; import { getDiForUnitTesting } from "../../getDiForUnitTesting";
import resolveSystemProxyFromElectronInjectable from "./resolve-system-proxy-from-electron.injectable"; import resolveSystemProxyFromElectronInjectable from "./resolve-system-proxy-from-electron.injectable";
import electronInjectable from "./electron.injectable"; import electronBrowserWindowInjectable from "./electron-browser-window.injectable";
import type { AsyncFnMock } from "@async-fn/jest"; import type { AsyncFnMock } from "@async-fn/jest";
import asyncFn from "@async-fn/jest"; import asyncFn from "@async-fn/jest";
import type electron from "electron";
import { getPromiseStatus } from "@k8slens/test-utils"; import { getPromiseStatus } from "@k8slens/test-utils";
import logErrorInjectable from "../../../common/log-error.injectable"; import logErrorInjectable from "../../../common/log-error.injectable";
import type { DiContainer } from "@ogre-tools/injectable"; import type { DiContainer } from "@ogre-tools/injectable";
import type { BrowserWindow, Session, WebContents } from "electron";
describe("technical: resolve-system-proxy-from-electron", () => { describe("technical: resolve-system-proxy-from-electron", () => {
let resolveSystemProxyMock: AsyncFnMock<(url: string) => Promise<string>>; let resolveSystemProxyMock: AsyncFnMock<(url: string) => Promise<string>>;
@ -26,44 +26,19 @@ describe("technical: resolve-system-proxy-from-electron", () => {
di.override(logErrorInjectable, () => logErrorMock); di.override(logErrorInjectable, () => logErrorMock);
}); });
describe("given there are non-destroyed Lens windows, when called with URL", () => { describe("given there are no unexpected issues, when called with URL", () => {
beforeEach(() => { beforeEach(() => {
resolveSystemProxyMock = asyncFn(); resolveSystemProxyMock = asyncFn();
di.override( di.override(
electronInjectable, electronBrowserWindowInjectable,
() => () => ({
() =>
({
webContents: { webContents: {
getAllWebContents: () => [
{
isDestroyed: () => true,
session: { session: {
resolveProxy: () => { resolveProxy: resolveSystemProxyMock,
throw new Error("should never come here"); } as unknown as Session,
}, } as unknown as WebContents,
}, } as unknown as BrowserWindow),
},
{
isDestroyed: () => false,
session: { resolveProxy: resolveSystemProxyMock },
},
{
isDestroyed: () => false,
session: {
resolveProxy: () => {
throw new Error("should never come here");
},
},
},
],
},
} as unknown as typeof electron),
); );
const resolveSystemProxyFromElectron = di.inject( const resolveSystemProxyFromElectron = di.inject(
@ -73,7 +48,7 @@ describe("technical: resolve-system-proxy-from-electron", () => {
actualPromise = resolveSystemProxyFromElectron("some-url"); actualPromise = resolveSystemProxyFromElectron("some-url");
}); });
it("calls to resolve proxy from the first window", () => { it("calls to resolve proxy from the browser window", () => {
expect(resolveSystemProxyMock).toHaveBeenCalledWith("some-url"); expect(resolveSystemProxyMock).toHaveBeenCalledWith("some-url");
}); });
@ -90,28 +65,23 @@ describe("technical: resolve-system-proxy-from-electron", () => {
}); });
}); });
describe("given there are only destroyed Lens windows, when called with URL", () => { describe("given there are unexpected issues, when called with URL", () => {
let error: any; let error: any;
beforeEach(async () => { beforeEach(async () => {
di.override( resolveSystemProxyMock = asyncFn();
electronInjectable,
() =>
({
webContents: {
getAllWebContents: () => [
{
isDestroyed: () => true,
di.override(
electronBrowserWindowInjectable,
() => () => ({
webContents: {
session: { session: {
resolveProxy: () => { resolveProxy: () => {
throw new Error("should never come here"); throw new Error("unexpected error");
}, },
}, } as unknown as Session,
}, } as unknown as WebContents,
], } as unknown as BrowserWindow),
},
} as unknown as typeof electron),
); );
resolveSystemProxyMock = asyncFn(); resolveSystemProxyMock = asyncFn();
@ -128,7 +98,7 @@ describe("technical: resolve-system-proxy-from-electron", () => {
}); });
it("throws error", () => { it("throws error", () => {
expect(error.message).toBe('Tried to resolve proxy for "some-url", but no browser window was available'); expect(error.message).toBe("unexpected error");
}); });
it("logs error", () => { it("logs error", () => {