mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Fix win-ca failing on electron renderer on windows
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
9bad482237
commit
2702ed7468
@ -3,26 +3,40 @@
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import wincaAPI from "win-ca/api";
|
||||
import execFileInjectable from "../fs/exec-file.injectable";
|
||||
import { requestSystemCAsInjectionToken } from "./request-system-cas-token";
|
||||
|
||||
const pemEncoding = (hexEncodedCert: String) => {
|
||||
const certData = Buffer.from(hexEncodedCert, "hex").toString('base64');
|
||||
const lines = ['-----BEGIN CERTIFICATE-----'];
|
||||
|
||||
for (let i = 0; i < certData.length; i += 64) {
|
||||
lines.push(certData.substring(i, i + 64));
|
||||
}
|
||||
|
||||
lines.push('-----END CERTIFICATE-----', '');
|
||||
|
||||
return lines.join("\r\n");
|
||||
}
|
||||
|
||||
const requestSystemCAsInjectable = getInjectable({
|
||||
id: "request-system-cas",
|
||||
instantiate: () => {
|
||||
return () => new Promise<string[]>((resolve) => {
|
||||
const CAs: string[] = [];
|
||||
instantiate: (di) => {
|
||||
const wincaRootsExePath: string = __non_webpack_require__.resolve("win-ca/lib/roots.exe");
|
||||
const execFile = di.inject(execFileInjectable);
|
||||
|
||||
wincaAPI({
|
||||
format: wincaAPI.der2.pem,
|
||||
inject: false,
|
||||
ondata: (ca: string) => {
|
||||
CAs.push(ca);
|
||||
},
|
||||
onend: () => {
|
||||
resolve(CAs);
|
||||
},
|
||||
});
|
||||
});
|
||||
return async () => {
|
||||
/**
|
||||
* This needs to be done manually because for some reason calling the api from "win-ca"
|
||||
* directly fails to load "child_process" correctly on renderer
|
||||
*/
|
||||
const output = await execFile(wincaRootsExePath);
|
||||
|
||||
return output
|
||||
.split("\r\n")
|
||||
.filter(Boolean)
|
||||
.map(pemEncoding);
|
||||
};
|
||||
},
|
||||
causesSideEffects: true,
|
||||
injectionToken: requestSystemCAsInjectionToken,
|
||||
|
||||
@ -7,7 +7,11 @@ import type { ExecFileOptions } from "child_process";
|
||||
import { execFile } from "child_process";
|
||||
import { promisify } from "util";
|
||||
|
||||
export type ExecFile = (filePath: string, args: string[], options: ExecFileOptions) => Promise<string>;
|
||||
export interface ExecFile {
|
||||
(filePath: string): Promise<string>;
|
||||
(filePath: string, argsOrOptions: string[] | ExecFileOptions): Promise<string>;
|
||||
(filePath: string, args: string[], options: ExecFileOptions): Promise<string>;
|
||||
}
|
||||
|
||||
const execFileInjectable = getInjectable({
|
||||
id: "exec-file",
|
||||
@ -15,8 +19,22 @@ const execFileInjectable = getInjectable({
|
||||
instantiate: (): ExecFile => {
|
||||
const asyncExecFile = promisify(execFile);
|
||||
|
||||
return async (filePath, args, options) => {
|
||||
const result = await asyncExecFile(filePath, args, options);
|
||||
return async (filePath: string, argsOrOptions?: string[] | ExecFileOptions, maybeOptions?: ExecFileOptions) => {
|
||||
let args: string[];
|
||||
let options: ExecFileOptions;
|
||||
|
||||
if (Array.isArray(argsOrOptions)) {
|
||||
args = argsOrOptions;
|
||||
options = maybeOptions ?? {};
|
||||
} else {
|
||||
args = [];
|
||||
options = maybeOptions ?? argsOrOptions ?? {};
|
||||
}
|
||||
|
||||
const result = await asyncExecFile(filePath, args, {
|
||||
encoding: "utf-8",
|
||||
...options,
|
||||
});
|
||||
|
||||
return result.stdout;
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user