Add authkey header for requests to the backend (#256)

With this PR, Electron will generate a new authorization key that the Go
backend will look for in any incoming requests. The Electron backend
will inject this header with all requests to the backend to ensure no
additional work is required on the frontend.

This also adds a `fetchutil` abstraction that will use the Electron
`net` module when calls are made from the Electron backend to the Go
backend. When using the `node:fetch` module, Electron can't inject
headers to requests. The Electron `net` module is also faster than the
Node module.

This also breaks out platform functions in emain into their own file so
other emain modules can import them.
This commit is contained in:
Evan Simkowitz
2024-08-21 15:04:39 -07:00
committed by GitHub
parent 23261a7a98
commit e527e2ab77
12 changed files with 206 additions and 75 deletions
+1
View File
@@ -10,6 +10,7 @@ import {
newLayoutNode,
} from "@/layout/index";
import { getWebServerEndpoint, getWSServerEndpoint } from "@/util/endpoints";
import { fetch } from "@/util/fetchutil";
import { produce } from "immer";
import * as jotai from "jotai";
import * as rxjs from "rxjs";
+1
View File
@@ -5,6 +5,7 @@
import { sendRpcCommand } from "@/app/store/wshrpc";
import { getWebServerEndpoint } from "@/util/endpoints";
import { fetch } from "@/util/fetchutil";
import * as jotai from "jotai";
import * as React from "react";
import { atoms, globalStore } from "./global";
+1
View File
@@ -52,6 +52,7 @@ declare global {
};
type ElectronApi = {
getAuthKey(): string;
getIsDev(): boolean;
getCursorPoint: () => Electron.Point;
+20
View File
@@ -0,0 +1,20 @@
// Copyright 2024, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
// Utility to abstract the fetch function so the Electron net module can be used when available.
let net: Electron.Net;
try {
import("electron").then(({ net: electronNet }) => (net = electronNet));
} catch (e) {
// do nothing
}
export function fetch(input: string | GlobalRequest | URL, init?: RequestInit): Promise<Response> {
if (net) {
return net.fetch(input.toString(), init);
} else {
return globalThis.fetch(input, init);
}
}