2024-06-12 02:42:10 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
import * as electron from "electron";
|
2024-06-17 18:58:28 +02:00
|
|
|
import fs from "fs";
|
2024-06-12 02:42:10 +02:00
|
|
|
import * as child_process from "node:child_process";
|
2024-06-17 18:58:28 +02:00
|
|
|
import os from "os";
|
2024-06-12 02:42:10 +02:00
|
|
|
import * as path from "path";
|
2024-06-13 04:33:44 +02:00
|
|
|
import * as readline from "readline";
|
2024-06-12 02:42:10 +02:00
|
|
|
import { debounce } from "throttle-debounce";
|
|
|
|
import * as services from "../frontend/app/store/services";
|
|
|
|
|
|
|
|
const electronApp = electron.app;
|
2024-06-14 01:49:25 +02:00
|
|
|
const isDev = process.env.WAVETERM_DEV;
|
|
|
|
const isDevServer = !electronApp.isPackaged && process.env.ELECTRON_RENDERER_URL;
|
2024-06-12 02:42:10 +02:00
|
|
|
|
|
|
|
const WaveAppPathVarName = "WAVETERM_APP_PATH";
|
|
|
|
const WaveDevVarName = "WAVETERM_DEV";
|
|
|
|
const WaveSrvReadySignalPidVarName = "WAVETERM_READY_SIGNAL_PID";
|
|
|
|
const AuthKeyFile = "waveterm.authkey";
|
|
|
|
const DevServerEndpoint = "http://127.0.0.1:8190";
|
|
|
|
const ProdServerEndpoint = "http://127.0.0.1:1719";
|
|
|
|
|
|
|
|
let waveSrvReadyResolve = (value: boolean) => {};
|
|
|
|
let waveSrvReady: Promise<boolean> = new Promise((resolve, _) => {
|
|
|
|
waveSrvReadyResolve = resolve;
|
|
|
|
});
|
2024-06-15 23:59:14 +02:00
|
|
|
|
2024-06-12 02:42:10 +02:00
|
|
|
let waveSrvProc: child_process.ChildProcessWithoutNullStreams | null = null;
|
|
|
|
electronApp.setName(isDev ? "NextWave (Dev)" : "NextWave");
|
|
|
|
const unamePlatform = process.platform;
|
|
|
|
let unameArch: string = process.arch;
|
|
|
|
if (unameArch == "x64") {
|
|
|
|
unameArch = "amd64";
|
|
|
|
}
|
|
|
|
|
|
|
|
function getBaseHostPort(): string {
|
|
|
|
if (isDev) {
|
|
|
|
return DevServerEndpoint;
|
|
|
|
}
|
|
|
|
return ProdServerEndpoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
// must match golang
|
|
|
|
function getWaveHomeDir() {
|
2024-06-15 23:59:14 +02:00
|
|
|
return path.join(os.homedir(), ".w2");
|
2024-06-12 02:42:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function getElectronAppBasePath(): string {
|
|
|
|
return path.dirname(__dirname);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getGoAppBasePath(): string {
|
|
|
|
const appDir = getElectronAppBasePath();
|
|
|
|
if (appDir.endsWith(".asar")) {
|
|
|
|
return `${appDir}.unpacked`;
|
|
|
|
} else {
|
|
|
|
return appDir;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getWaveSrvPath(): string {
|
|
|
|
return path.join(getGoAppBasePath(), "bin", "wavesrv");
|
|
|
|
}
|
|
|
|
|
2024-06-15 23:59:14 +02:00
|
|
|
function getWaveSrvPathWin(): string {
|
|
|
|
const appPath = path.join(getGoAppBasePath(), "bin", "wavesrv.exe");
|
2024-06-17 18:58:28 +02:00
|
|
|
return `& "${appPath}"`;
|
2024-06-15 23:59:14 +02:00
|
|
|
}
|
|
|
|
|
2024-06-12 02:42:10 +02:00
|
|
|
function getWaveSrvCwd(): string {
|
|
|
|
return getWaveHomeDir();
|
|
|
|
}
|
|
|
|
|
|
|
|
function runWaveSrv(): Promise<boolean> {
|
|
|
|
let pResolve: (value: boolean) => void;
|
|
|
|
let pReject: (reason?: any) => void;
|
|
|
|
const rtnPromise = new Promise<boolean>((argResolve, argReject) => {
|
|
|
|
pResolve = argResolve;
|
|
|
|
pReject = argReject;
|
|
|
|
});
|
|
|
|
const envCopy = { ...process.env };
|
|
|
|
envCopy[WaveAppPathVarName] = getGoAppBasePath();
|
|
|
|
if (isDev) {
|
|
|
|
envCopy[WaveDevVarName] = "1";
|
|
|
|
}
|
|
|
|
envCopy[WaveSrvReadySignalPidVarName] = process.pid.toString();
|
2024-06-17 18:58:28 +02:00
|
|
|
let waveSrvCmd: string;
|
|
|
|
if (process.platform === "win32") {
|
|
|
|
waveSrvCmd = getWaveSrvPathWin();
|
|
|
|
} else {
|
|
|
|
waveSrvCmd = getWaveSrvPath();
|
|
|
|
}
|
2024-06-12 02:42:10 +02:00
|
|
|
console.log("trying to run local server", waveSrvCmd);
|
2024-06-13 04:33:44 +02:00
|
|
|
const proc = child_process.spawn(getWaveSrvPath(), {
|
2024-06-12 02:42:10 +02:00
|
|
|
cwd: getWaveSrvCwd(),
|
|
|
|
env: envCopy,
|
|
|
|
});
|
|
|
|
proc.on("exit", (e) => {
|
|
|
|
console.log("wavesrv exited, shutting down");
|
|
|
|
electronApp.quit();
|
|
|
|
});
|
|
|
|
proc.on("spawn", (e) => {
|
2024-06-14 01:49:25 +02:00
|
|
|
console.log("spawned wavesrv");
|
2024-06-12 02:42:10 +02:00
|
|
|
waveSrvProc = proc;
|
|
|
|
pResolve(true);
|
|
|
|
});
|
|
|
|
proc.on("error", (e) => {
|
|
|
|
console.log("error running wavesrv", e);
|
|
|
|
pReject(e);
|
|
|
|
});
|
2024-06-13 04:33:44 +02:00
|
|
|
const rlStdout = readline.createInterface({
|
|
|
|
input: proc.stdout,
|
|
|
|
terminal: false,
|
2024-06-12 02:42:10 +02:00
|
|
|
});
|
2024-06-13 04:33:44 +02:00
|
|
|
rlStdout.on("line", (line) => {
|
|
|
|
console.log(line);
|
|
|
|
});
|
|
|
|
const rlStderr = readline.createInterface({
|
|
|
|
input: proc.stderr,
|
|
|
|
terminal: false,
|
|
|
|
});
|
|
|
|
rlStderr.on("line", (line) => {
|
2024-06-17 18:58:28 +02:00
|
|
|
if (line.includes("WAVESRV-ESTART")) {
|
|
|
|
waveSrvReadyResolve(true);
|
|
|
|
return;
|
|
|
|
}
|
2024-06-13 04:33:44 +02:00
|
|
|
console.log(line);
|
2024-06-12 02:42:10 +02:00
|
|
|
});
|
|
|
|
return rtnPromise;
|
|
|
|
}
|
|
|
|
|
2024-06-17 18:58:28 +02:00
|
|
|
async function mainResizeHandler(_: any, windowId: string, win: Electron.BrowserWindow) {
|
2024-06-12 02:42:10 +02:00
|
|
|
if (win == null || win.isDestroyed() || win.fullScreen) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const bounds = win.getBounds();
|
2024-06-17 18:58:28 +02:00
|
|
|
try {
|
|
|
|
await services.WindowService.SetWindowPosAndSize(
|
|
|
|
windowId,
|
|
|
|
{ x: bounds.x, y: bounds.y },
|
|
|
|
{ width: bounds.width, height: bounds.height }
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
console.log("error resizing window", e);
|
|
|
|
}
|
2024-06-12 02:42:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function shNavHandler(event: Electron.Event<Electron.WebContentsWillNavigateEventParams>, url: string) {
|
2024-06-14 20:10:54 +02:00
|
|
|
if (url.startsWith("http://127.0.0.1:5173/index.html")) {
|
2024-06-14 08:54:04 +02:00
|
|
|
// this is a dev-mode hot-reload, ignore it
|
|
|
|
console.log("allowing hot-reload of index.html");
|
|
|
|
return;
|
|
|
|
}
|
2024-06-12 02:42:10 +02:00
|
|
|
event.preventDefault();
|
|
|
|
if (url.startsWith("https://") || url.startsWith("http://") || url.startsWith("file://")) {
|
|
|
|
console.log("open external, shNav", url);
|
|
|
|
electron.shell.openExternal(url);
|
|
|
|
} else {
|
|
|
|
console.log("navigation canceled", url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function shFrameNavHandler(event: Electron.Event<Electron.WebContentsWillFrameNavigateEventParams>) {
|
|
|
|
if (!event.frame?.parent) {
|
|
|
|
// only use this handler to process iframe events (non-iframe events go to shNavHandler)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const url = event.url;
|
|
|
|
console.log(`frame-navigation url=${url} frame=${event.frame.name}`);
|
|
|
|
if (event.frame.name == "webview") {
|
|
|
|
// "webview" links always open in new window
|
|
|
|
// this will *not* effect the initial load because srcdoc does not count as an electron navigation
|
|
|
|
console.log("open external, frameNav", url);
|
|
|
|
event.preventDefault();
|
|
|
|
electron.shell.openExternal(url);
|
|
|
|
return;
|
|
|
|
}
|
2024-06-14 20:10:54 +02:00
|
|
|
if (
|
|
|
|
event.frame.name == "pdfview" &&
|
|
|
|
(url.startsWith("blob:file:///") || url.startsWith(getBaseHostPort() + "/wave/stream-file?"))
|
|
|
|
) {
|
2024-06-12 02:42:10 +02:00
|
|
|
// allowed
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
console.log("frame navigation canceled");
|
|
|
|
}
|
|
|
|
|
|
|
|
function createWindow(client: Client, waveWindow: WaveWindow): Electron.BrowserWindow {
|
2024-06-17 18:58:28 +02:00
|
|
|
const primaryDisplay = electron.screen.getPrimaryDisplay();
|
|
|
|
let winHeight = waveWindow.winsize.height;
|
|
|
|
let winWidth = waveWindow.winsize.width;
|
|
|
|
if (winHeight > primaryDisplay.workAreaSize.height) {
|
|
|
|
winHeight = primaryDisplay.workAreaSize.height;
|
|
|
|
}
|
|
|
|
if (winWidth > primaryDisplay.workAreaSize.width) {
|
|
|
|
winWidth = primaryDisplay.workAreaSize.width;
|
|
|
|
}
|
|
|
|
let winX = waveWindow.pos.x;
|
|
|
|
let winY = waveWindow.pos.y;
|
|
|
|
if (winX + winWidth > primaryDisplay.workAreaSize.width) {
|
|
|
|
winX = Math.floor((primaryDisplay.workAreaSize.width - winWidth) / 2);
|
|
|
|
}
|
|
|
|
if (winY + winHeight > primaryDisplay.workAreaSize.height) {
|
|
|
|
winY = Math.floor((primaryDisplay.workAreaSize.height - winHeight) / 2);
|
|
|
|
}
|
2024-06-12 02:42:10 +02:00
|
|
|
const win = new electron.BrowserWindow({
|
2024-06-17 18:58:28 +02:00
|
|
|
x: winX,
|
|
|
|
y: winY,
|
2024-06-12 02:42:10 +02:00
|
|
|
titleBarStyle: "hiddenInset",
|
2024-06-17 18:58:28 +02:00
|
|
|
width: winWidth,
|
|
|
|
height: winHeight,
|
2024-06-12 02:42:10 +02:00
|
|
|
minWidth: 500,
|
|
|
|
minHeight: 300,
|
|
|
|
icon:
|
|
|
|
unamePlatform == "linux"
|
|
|
|
? path.join(getElectronAppBasePath(), "public/logos/wave-logo-dark.png")
|
|
|
|
: undefined,
|
|
|
|
webPreferences: {
|
2024-06-14 01:49:25 +02:00
|
|
|
preload: path.join(getElectronAppBasePath(), "preload", "index.cjs"),
|
2024-06-12 02:42:10 +02:00
|
|
|
},
|
|
|
|
show: false,
|
|
|
|
autoHideMenuBar: true,
|
|
|
|
backgroundColor: "#000000",
|
|
|
|
});
|
|
|
|
win.once("ready-to-show", () => {
|
|
|
|
win.show();
|
|
|
|
});
|
|
|
|
// const indexHtml = isDev ? "index-dev.html" : "index.html";
|
|
|
|
let usp = new URLSearchParams();
|
|
|
|
usp.set("clientid", client.oid);
|
|
|
|
usp.set("windowid", waveWindow.oid);
|
|
|
|
const indexHtml = "index.html";
|
2024-06-14 01:49:25 +02:00
|
|
|
if (isDevServer) {
|
|
|
|
console.log("running as dev server");
|
|
|
|
win.loadURL(`${process.env.ELECTRON_RENDERER_URL}/index.html?${usp.toString()}`);
|
|
|
|
} else {
|
|
|
|
console.log("running as file");
|
|
|
|
win.loadFile(path.join(getElectronAppBasePath(), "frontend", indexHtml), { search: usp.toString() });
|
|
|
|
}
|
|
|
|
|
2024-06-12 02:42:10 +02:00
|
|
|
win.webContents.on("will-navigate", shNavHandler);
|
|
|
|
win.webContents.on("will-frame-navigate", shFrameNavHandler);
|
|
|
|
win.on(
|
|
|
|
"resize",
|
2024-06-17 18:58:28 +02:00
|
|
|
debounce(400, (e) => mainResizeHandler(e, waveWindow.oid, win))
|
2024-06-12 02:42:10 +02:00
|
|
|
);
|
|
|
|
win.on(
|
|
|
|
"move",
|
2024-06-17 18:58:28 +02:00
|
|
|
debounce(400, (e) => mainResizeHandler(e, waveWindow.oid, win))
|
2024-06-12 02:42:10 +02:00
|
|
|
);
|
|
|
|
win.webContents.on("zoom-changed", (e) => {
|
|
|
|
win.webContents.send("zoom-changed");
|
|
|
|
});
|
|
|
|
win.webContents.setWindowOpenHandler(({ url, frameName }) => {
|
|
|
|
if (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("file://")) {
|
|
|
|
console.log("openExternal fallback", url);
|
|
|
|
electron.shell.openExternal(url);
|
|
|
|
}
|
|
|
|
console.log("window-open denied", url);
|
|
|
|
return { action: "deny" };
|
|
|
|
});
|
|
|
|
return win;
|
|
|
|
}
|
|
|
|
|
2024-06-14 01:49:25 +02:00
|
|
|
electron.ipcMain.on("isDev", () => {
|
|
|
|
return isDev;
|
|
|
|
});
|
|
|
|
|
|
|
|
electron.ipcMain.on("isDevServer", () => {
|
|
|
|
return isDevServer;
|
|
|
|
});
|
|
|
|
|
2024-06-12 02:42:10 +02:00
|
|
|
(async () => {
|
|
|
|
const startTs = Date.now();
|
|
|
|
const instanceLock = electronApp.requestSingleInstanceLock();
|
|
|
|
if (!instanceLock) {
|
|
|
|
console.log("waveterm-app could not get single-instance-lock, shutting down");
|
|
|
|
electronApp.quit();
|
|
|
|
return;
|
|
|
|
}
|
2024-06-17 18:58:28 +02:00
|
|
|
const waveHomeDir = getWaveHomeDir();
|
|
|
|
if (!fs.existsSync(waveHomeDir)) {
|
|
|
|
fs.mkdirSync(waveHomeDir);
|
|
|
|
}
|
2024-06-12 02:42:10 +02:00
|
|
|
try {
|
|
|
|
await runWaveSrv();
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e.toString());
|
|
|
|
}
|
|
|
|
const ready = await waveSrvReady;
|
|
|
|
console.log("wavesrv ready signal received", ready, Date.now() - startTs, "ms");
|
|
|
|
|
2024-06-15 23:59:14 +02:00
|
|
|
console.log("get client data");
|
2024-06-17 18:58:28 +02:00
|
|
|
let clientData = (await services.ClientService.GetClientData().catch((e) => console.log(e))) as Client;
|
2024-06-15 23:59:14 +02:00
|
|
|
console.log("client data ready");
|
2024-06-12 02:42:10 +02:00
|
|
|
let windowData: WaveWindow = (await services.ObjectService.GetObject(
|
|
|
|
"window:" + clientData.mainwindowid
|
|
|
|
)) as WaveWindow;
|
|
|
|
await electronApp.whenReady();
|
2024-06-14 01:53:52 +02:00
|
|
|
createWindow(clientData, windowData);
|
2024-06-12 02:42:10 +02:00
|
|
|
|
|
|
|
electronApp.on("activate", () => {
|
|
|
|
if (electron.BrowserWindow.getAllWindows().length === 0) {
|
|
|
|
createWindow(clientData, windowData);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})();
|