-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathresolve-browser-executable.mjs
More file actions
78 lines (67 loc) · 1.88 KB
/
Copy pathresolve-browser-executable.mjs
File metadata and controls
78 lines (67 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env node
import fs from "node:fs";
const [engineArgument] = process.argv.slice(2);
const engine =
engineArgument === undefined
? process.env.BROWSER_ENGINE ?? "chromium"
: engineArgument;
function redactSecret(message) {
const licenseKey = process.env.CLOAKBROWSER_LICENSE_KEY;
return licenseKey ? message.split(licenseKey).join("[redacted]") : message;
}
function fail(message) {
process.stderr.write(
`CloakBrowser executable resolution failed: ${redactSecret(message)}\n`,
);
process.exitCode = 1;
}
function printExecutable(executable) {
if (typeof executable !== "string" || executable.length === 0) {
fail("ensureBinary() did not return an executable path");
return;
}
process.stdout.write(`${executable}\n`);
}
function isExecutableFile(filePath) {
try {
return (
fs.statSync(filePath).isFile() &&
fs.accessSync(filePath, fs.constants.X_OK) === undefined
);
} catch {
return false;
}
}
async function resolveExecutable() {
if (engine === "chromium") {
printExecutable(process.env.CHROMIUM_BINARY || "chromium");
return;
}
if (engine !== "cloakbrowser") {
fail(`unsupported browser engine: ${engine}`);
return;
}
const override = process.env.CLOAKBROWSER_BINARY_PATH;
if (override) {
if (!isExecutableFile(override)) {
fail("configured CLOAKBROWSER_BINARY_PATH is not an executable file");
return;
}
printExecutable(override);
return;
}
for (const method of ["log", "info", "warn", "error"]) {
console[method] = () => {};
}
try {
const { ensureBinary } = await import(
"/opt/cloakbrowser/node_modules/cloakbrowser/dist/index.js"
);
printExecutable(await ensureBinary());
} catch {
fail("unable to resolve the CloakBrowser executable");
}
}
resolveExecutable().catch(() => {
fail("unable to resolve the CloakBrowser executable");
});