Split out use of httpsProxy preference (#6771)

* Split out use of httpsProxy preference

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Rename file

Signed-off-by: Sebastian Malton <sebastian@malton.name>

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton authored and GitHub committed 2022-12-20 11:00:15 -05:00
1 parent 443081493b
commit 24240655c0
11 files changed
+130 -91

No files matched your search

@@ -1,55 +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 type { RequestInit, Response } from "node-fetch";
import type { AsyncResult } from "../utils/async-result";
import fetchInjectable from "./fetch.injectable";
export interface DownloadJsonOptions {
signal?: AbortSignal | null | undefined;
}
export type DownloadJson = (url: string, opts?: DownloadJsonOptions) => Promise<AsyncResult<unknown, string>>;
const downloadJsonInjectable = getInjectable({
id: "download-json",
instantiate: (di): DownloadJson => {
const fetch = di.inject(fetchInjectable);
return async (url, opts) => {
let result: Response;
try {
result = await fetch(url, opts as RequestInit);
} catch (error) {
return {
callWasSuccessful: false,
error: String(error),
};
}
if (result.status < 200 || 300 <= result.status) {
return {
callWasSuccessful: false,
error: result.statusText,
};
}
try {
return {
callWasSuccessful: true,
response: await result.json(),
};
} catch (error) {
return {
callWasSuccessful: false,
error: String(error),
};
}
};
},
});
export default downloadJsonInjectable;
+46
View File
@@ -0,0 +1,46 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { AsyncResult } from "../../utils/async-result";
import type { Fetch } from "../fetch.injectable";
import type { RequestInit, Response } from "node-fetch";
export interface DownloadJsonOptions {
signal?: AbortSignal | null | undefined;
}
export type DownloadJson = (url: string, opts?: DownloadJsonOptions) => Promise<AsyncResult<unknown, string>>;
export const downloadJsonWith = (fetch: Fetch): DownloadJson => async (url, opts) => {
let result: Response;
try {
result = await fetch(url, opts as RequestInit);
} catch (error) {
return {
callWasSuccessful: false,
error: String(error),
};
}
if (result.status < 200 || 300 <= result.status) {
return {
callWasSuccessful: false,
error: result.statusText,
};
}
try {
return {
callWasSuccessful: true,
response: await result.json(),
};
} catch (error) {
return {
callWasSuccessful: false,
error: String(error),
};
}
};
@@ -0,0 +1,14 @@
/**
* 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 fetchInjectable from "../fetch.injectable";
import { downloadJsonWith } from "./impl";
const downloadJsonInjectable = getInjectable({
id: "download-json",
instantiate: (di) => downloadJsonWith(di.inject(fetchInjectable)),
});
export default downloadJsonInjectable;
@@ -0,0 +1,14 @@
/**
* 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 proxyFetchInjectable from "../proxy-fetch.injectable";
import { downloadJsonWith } from "./impl";
const proxyDownloadJsonInjectable = getInjectable({
id: "proxy-download-json",
instantiate: (di) => downloadJsonWith(di.inject(proxyFetchInjectable)),
});
export default proxyDownloadJsonInjectable;
@@ -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 * as FetchModule from "node-fetch";
const { NodeFetch } = require("../../../build/webpack/node-fetch.bundle") as { NodeFetch: typeof FetchModule };
/**
* NOTE: while using this module can cause side effects, this specific injectable is not marked as
* such since sometimes the request can be wholely within the perview of unit test
*/
const nodeFetchModuleInjectable = getInjectable({
id: "node-fetch-module",
instantiate: () => NodeFetch,
});
export default nodeFetchModuleInjectable;
+4 -19
View File
@@ -3,32 +3,17 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { HttpsProxyAgent } from "hpagent";
import type * as FetchModule from "node-fetch";
import userStoreInjectable from "../user-store/user-store.injectable";
const { NodeFetch: { default: fetch }} = require("../../../build/webpack/node-fetch.bundle") as { NodeFetch: typeof FetchModule };
type Response = FetchModule.Response;
type RequestInit = FetchModule.RequestInit;
import type { RequestInit, Response } from "node-fetch";
import nodeFetchModuleInjectable from "./fetch-module.injectable";
export type Fetch = (url: string, init?: RequestInit) => Promise<Response>;
const fetchInjectable = getInjectable({
id: "fetch",
instantiate: (di): Fetch => {
const { httpsProxy, allowUntrustedCAs } = di.inject(userStoreInjectable);
const agent = httpsProxy
? new HttpsProxyAgent({
proxy: httpsProxy,
rejectUnauthorized: !allowUntrustedCAs,
})
: undefined;
const { default: fetch } = di.inject(nodeFetchModuleInjectable);
return (url, init = {}) => fetch(url, {
agent,
...init,
});
return (url, init) => fetch(url, init);
},
causesSideEffects: true,
});
@@ -0,0 +1,30 @@
/**
* 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 { HttpsProxyAgent } from "hpagent";
import userStoreInjectable from "../user-store/user-store.injectable";
import type { Fetch } from "./fetch.injectable";
import fetchInjectable from "./fetch.injectable";
const proxyFetchInjectable = getInjectable({
id: "proxy-fetch",
instantiate: (di): Fetch => {
const fetch = di.inject(fetchInjectable);
const { httpsProxy, allowUntrustedCAs } = di.inject(userStoreInjectable);
const agent = httpsProxy
? new HttpsProxyAgent({
proxy: httpsProxy,
rejectUnauthorized: !allowUntrustedCAs,
})
: undefined;
return (url, init = {}) => fetch(url, {
agent,
...init,
});
},
});
export default proxyFetchInjectable;