Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/plugin-utils/lib/BrowserEngine.ts
1028 views
1
import { existsSync } from 'fs';
2
import IBrowserEngine from '@secret-agent/interfaces/IBrowserEngine';
3
import IBrowserEngineOption from '@secret-agent/interfaces/IBrowserEngineOption';
4
import IBrowserEngineFetcher from '@secret-agent/interfaces/IBrowserEngineFetcher';
5
import ChromeApp from '@secret-agent/chrome-app';
6
import { IBrowserEmulatorClass } from '@secret-agent/interfaces/ICorePlugin';
7
8
export default class BrowserEngine implements IBrowserEngine {
9
public name: string;
10
public fullVersion: string;
11
public executablePath: string;
12
public executablePathEnvVar: string;
13
public userDataDir?: string;
14
15
public readonly launchArguments: string[] = [];
16
17
public isHeaded?: boolean;
18
public isInstalled: boolean;
19
20
private engineOption: IBrowserEngineOption;
21
private readonly engineFetcher: IBrowserEngineFetcher;
22
private readonly browserEmulator: IBrowserEmulatorClass;
23
24
constructor(browserEmulator: IBrowserEmulatorClass, browserEngineOption: IBrowserEngineOption) {
25
// figure out which one is closest to installed?
26
this.browserEmulator = browserEmulator;
27
this.engineFetcher = this.loadEngineFetcher(browserEngineOption);
28
if (this.engineFetcher.launchArgs?.length) {
29
this.launchArguments.push(...this.engineFetcher.launchArgs);
30
}
31
this.engineOption = browserEngineOption;
32
this.name = browserEngineOption.name;
33
this.fullVersion = this.engineFetcher.fullVersion;
34
this.executablePath = this.engineFetcher.executablePath;
35
this.executablePathEnvVar = this.engineFetcher.executablePathEnvVar;
36
this.isInstalled = this.engineFetcher.isInstalled;
37
}
38
39
public async verifyLaunchable(): Promise<void> {
40
if (!existsSync(this.executablePath)) {
41
let remedyMessage = `No executable exists at "${this.executablePath}"`;
42
43
const isCustomInstall = this.executablePathEnvVar && process.env[this.executablePathEnvVar];
44
if (!isCustomInstall) {
45
remedyMessage = `Please re-install the browser engine:
46
-------------------------------------------------
47
-------------- NPM INSTALL ----------------------
48
-------------------------------------------------
49
50
npm install @secret-agent/${this.engineOption.id}
51
52
-------------------------------------------------
53
`;
54
}
55
throw new Error(`Failed to launch ${this.name} ${this.fullVersion}:
56
57
${remedyMessage}`);
58
}
59
// exists, validate that host requirements exist
60
await this.engineFetcher.validateHostRequirements();
61
}
62
63
public beforeLaunch(launchSettings: {
64
showBrowser?: boolean;
65
disableGpu?: boolean;
66
disableDevtools?: boolean;
67
}): void {
68
this.browserEmulator.onBrowserWillLaunch(this, launchSettings);
69
}
70
71
private loadEngineFetcher(option: IBrowserEngineOption): IBrowserEngineFetcher {
72
if (option.name !== 'chrome') {
73
throw new Error(`Invalid browser engine requested ${option.name}`);
74
}
75
try {
76
// eslint-disable-next-line global-require,import/no-dynamic-require
77
let ChromeAtMajorVersion = require(`@secret-agent/${option.id}`);
78
if (ChromeAtMajorVersion.default) {
79
ChromeAtMajorVersion = ChromeAtMajorVersion.default;
80
}
81
82
return new ChromeAtMajorVersion();
83
} catch (err) {
84
/* no op */
85
}
86
87
return new ChromeApp(option.fullVersion);
88
}
89
}
90
91