Fix script deletion during execution on desktop
This commit fixes an issue seen on certain Windows environments (Windows 10 22H2 and 11 23H2 Pro Azure VMs) where scripts were being deleted during execution due to temporary directory usage. To resolve this, scripts are now stored in a persistent directory, enhancing reliability for long-running scripts and improving auditability along with troubleshooting. Key changes: - Move script execution logic to the `main` process from `preloader` to utilize Electron's `app.getPath`. - Improve runtime environment detection for non-browser environments to allow its usage in Electron main process. - Introduce a secure module to expose IPC channels from the main process to the renderer via the preloader process. Supporting refactorings include: - Simplify `CodeRunner` interface by removing the `tempScriptFolderName` parameter. - Rename `NodeSystemOperations` to `NodeElectronSystemOperations` as it now wraps electron APIs too, and convert it to class for simplicity. - Rename `TemporaryFileCodeRunner` to `ScriptFileCodeRunner` to reflect its new functinoality. - Rename `SystemOperations` folder to `System` for simplicity. - Rename `HostRuntimeEnvironment` to `BrowserRuntimeEnvironment` for clarity. - Refactor main Electron process configuration to align with latest Electron documentation/recommendations. - Refactor unit tests `BrowserRuntimeEnvironment` to simplify singleton workaround. - Use alias imports like `electron/main` and `electron/common` for better clarity.
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
import { FunctionKeys } from '@/TypeHelpers';
|
||||
|
||||
export interface IpcChannel<T> {
|
||||
readonly namespace: string;
|
||||
readonly accessibleMembers: readonly FunctionKeys<T>[]; // Property keys are not yet supported
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { FunctionKeys } from '@/TypeHelpers';
|
||||
import { CodeRunner } from '@/application/CodeRunner';
|
||||
import { IpcChannel } from './IpcChannel';
|
||||
|
||||
export const IpcChannelDefinitions = {
|
||||
CodeRunner: defineElectronIpcChannel<CodeRunner>('code-run', ['runCode']),
|
||||
} as const;
|
||||
|
||||
function defineElectronIpcChannel<T>(
|
||||
name: string,
|
||||
functionNames: readonly FunctionKeys<T>[],
|
||||
): IpcChannel<T> {
|
||||
return {
|
||||
namespace: name,
|
||||
accessibleMembers: functionNames,
|
||||
};
|
||||
}
|
||||
48
src/presentation/electron/shared/IpcBridging/IpcProxy.ts
Normal file
48
src/presentation/electron/shared/IpcBridging/IpcProxy.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { ipcMain } from 'electron/main';
|
||||
import { ipcRenderer } from 'electron/renderer';
|
||||
import { isFunction } from '@/TypeHelpers';
|
||||
import { IpcChannel } from './IpcChannel';
|
||||
|
||||
export function createIpcConsumerProxy<T>(
|
||||
channel: IpcChannel<T>,
|
||||
electronIpcRenderer: Electron.IpcRenderer = ipcRenderer,
|
||||
): AsyncMethods<T> {
|
||||
const facade: Partial<T> = {};
|
||||
channel.accessibleMembers.forEach((member) => {
|
||||
const functionKey = member as string;
|
||||
const ipcChannel = getIpcChannelIdentifier(channel.namespace, functionKey);
|
||||
facade[functionKey] = ((...args: unknown[]) => {
|
||||
return electronIpcRenderer.invoke(ipcChannel, ...args);
|
||||
}) as AsyncMethods<T>[keyof T];
|
||||
});
|
||||
return facade as AsyncMethods<T>;
|
||||
}
|
||||
|
||||
export function registerIpcChannel<T>(
|
||||
channel: IpcChannel<T>,
|
||||
originalObject: T,
|
||||
electronIpcMain: Electron.IpcMain = ipcMain,
|
||||
) {
|
||||
channel.accessibleMembers.forEach((functionKey) => {
|
||||
const originalFunction = originalObject[functionKey];
|
||||
if (!isFunction(originalFunction)) {
|
||||
throw new Error('Non-function members are not yet supported');
|
||||
}
|
||||
const ipcChannel = getIpcChannelIdentifier(channel.namespace, functionKey as string);
|
||||
electronIpcMain.handle(ipcChannel, (_event, ...args: unknown[]) => {
|
||||
return originalFunction.apply(originalObject, args);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function getIpcChannelIdentifier(namespace: string, key: string) {
|
||||
return `proxy:${namespace}:${key}`;
|
||||
}
|
||||
|
||||
type AsyncMethods<T> = {
|
||||
[P in keyof T]: T[P] extends (...args: infer Args) => infer R
|
||||
? R extends Promise<unknown>
|
||||
? (...args: Args) => R
|
||||
: (...args: Args) => Promise<R>
|
||||
: never;
|
||||
};
|
||||
17
src/presentation/electron/shared/IpcBridging/README.md
Normal file
17
src/presentation/electron/shared/IpcBridging/README.md
Normal file
@@ -0,0 +1,17 @@
|
||||
# IPC bridging
|
||||
|
||||
This module introduces structured and type-safe inter-process communication (IPC) to Electron applications,
|
||||
enhancing the development and maintenance of complex features.
|
||||
|
||||
## Benefits
|
||||
|
||||
- **Type safety**: Ensures reliable data exchange between processes and prevents runtime errors through enforced
|
||||
type checks in IPC communication.
|
||||
- **Maintainability**: Facilitates easy tracking and management of inter-process contracts using defined and clear
|
||||
interfaces.
|
||||
- **Security**: Implements the least-privilege principle by defining which members are accessible in proxy objects,
|
||||
enhancing the security of IPC interactions.
|
||||
- **Simplicity**: Simplifies IPC calls by abstracting the underlying complexity, providing a more straightforward
|
||||
interface for developers.
|
||||
- **Scalability**: Structured IPC management supports effective scaling and reduces the challenges of ad-hoc
|
||||
IPC implementations.
|
||||
Reference in New Issue
Block a user