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.
93 lines
2.9 KiB
TypeScript
93 lines
2.9 KiB
TypeScript
import { OperatingSystem } from '@/domain/OperatingSystem';
|
|
import { assertInRange } from '@/application/Common/Enum';
|
|
import { BrowserEnvironment, BrowserOsDetector } from './BrowserOsDetector';
|
|
import { BrowserCondition, TouchSupportExpectation } from './BrowserCondition';
|
|
import { BrowserConditions } from './BrowserConditions';
|
|
|
|
export class ConditionBasedOsDetector implements BrowserOsDetector {
|
|
constructor(private readonly conditions: readonly BrowserCondition[] = BrowserConditions) {
|
|
validateConditions(conditions);
|
|
}
|
|
|
|
public detect(environment: BrowserEnvironment): OperatingSystem | undefined {
|
|
if (!environment.userAgent) {
|
|
return undefined;
|
|
}
|
|
for (const condition of this.conditions) {
|
|
if (satisfiesCondition(condition, environment)) {
|
|
return condition.operatingSystem;
|
|
}
|
|
}
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
function satisfiesCondition(
|
|
condition: BrowserCondition,
|
|
browserEnvironment: BrowserEnvironment,
|
|
): boolean {
|
|
const { userAgent } = browserEnvironment;
|
|
if (condition.touchSupport !== undefined) {
|
|
if (!satisfiesTouchExpectation(condition.touchSupport, browserEnvironment)) {
|
|
return false;
|
|
}
|
|
}
|
|
if (condition.existingPartsInSameUserAgent.some((part) => !userAgent.includes(part))) {
|
|
return false;
|
|
}
|
|
if (condition.notExistingPartsInUserAgent?.some((part) => userAgent.includes(part))) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function satisfiesTouchExpectation(
|
|
expectation: TouchSupportExpectation,
|
|
browserEnvironment: BrowserEnvironment,
|
|
): boolean {
|
|
switch (expectation) {
|
|
case TouchSupportExpectation.MustExist:
|
|
if (!browserEnvironment.isTouchSupported) {
|
|
return false;
|
|
}
|
|
break;
|
|
case TouchSupportExpectation.MustNotExist:
|
|
if (browserEnvironment.isTouchSupported) {
|
|
return false;
|
|
}
|
|
break;
|
|
default:
|
|
throw new Error(`Unsupported touch support expectation: ${TouchSupportExpectation[expectation]}`);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function validateConditions(conditions: readonly BrowserCondition[]) {
|
|
if (!conditions.length) {
|
|
throw new Error('empty conditions');
|
|
}
|
|
for (const condition of conditions) {
|
|
validateCondition(condition);
|
|
}
|
|
}
|
|
|
|
function validateCondition(condition: BrowserCondition) {
|
|
if (!condition.existingPartsInSameUserAgent.length) {
|
|
throw new Error('Each condition must include at least one identifiable part of the user agent string.');
|
|
}
|
|
const duplicates = getDuplicates([
|
|
...condition.existingPartsInSameUserAgent,
|
|
...(condition.notExistingPartsInUserAgent ?? []),
|
|
]);
|
|
if (duplicates.length > 0) {
|
|
throw new Error(`Found duplicate entries in user agent parts: ${duplicates.join(', ')}. Each part should be unique.`);
|
|
}
|
|
if (condition.touchSupport !== undefined) {
|
|
assertInRange(condition.touchSupport, TouchSupportExpectation);
|
|
}
|
|
}
|
|
|
|
function getDuplicates(texts: readonly string[]): string[] {
|
|
return texts.filter((text, index) => texts.indexOf(text) !== index);
|
|
}
|