camoucore 0.3.0 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +4 -3
- package/dist/camoufox/__main__.d.ts +10 -0
- package/dist/camoufox/__main__.js +1175 -0
- package/dist/camoufox/__version__.d.ts +31 -0
- package/dist/camoufox/__version__.js +32 -0
- package/dist/camoufox/_humanize_playwright.d.ts +54 -0
- package/dist/camoufox/_humanize_playwright.js +429 -0
- package/dist/camoufox/addons.d.ts +21 -0
- package/dist/camoufox/addons.js +56 -0
- package/dist/camoufox/async_api.d.ts +9 -0
- package/dist/camoufox/async_api.js +9 -0
- package/dist/camoufox/browser-pin.d.ts +46 -0
- package/dist/camoufox/browser-pin.js +96 -0
- package/dist/camoufox/cli.d.ts +7 -0
- package/dist/camoufox/cli.js +20 -0
- package/dist/camoufox/coherence.d.ts +141 -0
- package/dist/camoufox/coherence.js +708 -0
- package/dist/camoufox/data-files/browser-pin.json +7 -0
- package/dist/camoufox/data-files/essential-fonts.json +5 -0
- package/dist/camoufox/data-files/fingerprint-presets-v150.json +14544 -0
- package/dist/camoufox/data-files/fingerprint-presets.json +5192 -0
- package/dist/camoufox/data-files/font-bases.json +1887 -0
- package/dist/camoufox/data-files/font-groups.json +716 -0
- package/dist/camoufox/data-files/fonts.json +240 -0
- package/dist/camoufox/data-files/fpgen.yml +44 -0
- package/dist/camoufox/data-files/launcher-constants.json +66 -0
- package/dist/camoufox/data-files/media-devices.json +148 -0
- package/dist/camoufox/data-files/repos.yml +40 -0
- package/dist/camoufox/data-files/voice-manifests.json +1864 -0
- package/dist/camoufox/data-files/voice-uris.json +188 -0
- package/dist/camoufox/data-files/warnings.yml +132 -0
- package/dist/camoufox/display_profile.d.ts +17 -0
- package/dist/camoufox/display_profile.js +72 -0
- package/dist/camoufox/driver_defaults.d.ts +25 -0
- package/dist/camoufox/driver_defaults.js +114 -0
- package/dist/camoufox/exceptions.d.ts +78 -0
- package/dist/camoufox/exceptions.js +135 -0
- package/dist/camoufox/fingerprints.d.ts +341 -0
- package/dist/camoufox/fingerprints.js +1682 -0
- package/dist/camoufox/gpudisplay.d.ts +158 -0
- package/dist/camoufox/gpudisplay.js +695 -0
- package/dist/camoufox/host_rendering.d.ts +38 -0
- package/dist/camoufox/host_rendering.js +180 -0
- package/dist/camoufox/humanize.d.ts +89 -0
- package/dist/camoufox/humanize.js +143 -0
- package/dist/camoufox/index.d.ts +21 -0
- package/dist/camoufox/index.js +21 -0
- package/dist/camoufox/ip.d.ts +45 -0
- package/dist/camoufox/ip.js +114 -0
- package/dist/camoufox/multiversion.d.ts +131 -0
- package/dist/camoufox/multiversion.js +489 -0
- package/dist/camoufox/node-floor.d.ts +5 -0
- package/dist/camoufox/node-floor.js +17 -0
- package/dist/camoufox/paths.d.ts +45 -0
- package/dist/camoufox/paths.js +59 -0
- package/dist/camoufox/pkgman.d.ts +331 -0
- package/dist/camoufox/pkgman.js +972 -0
- package/dist/camoufox/product.d.ts +8 -0
- package/dist/camoufox/product.js +8 -0
- package/dist/camoufox/pycompat.d.ts +97 -0
- package/dist/camoufox/pycompat.js +458 -0
- package/dist/camoufox/server.d.ts +36 -0
- package/dist/camoufox/server.js +82 -0
- package/dist/camoufox/sync_api.d.ts +77 -0
- package/dist/camoufox/sync_api.js +194 -0
- package/dist/camoufox/utils.d.ts +515 -0
- package/dist/camoufox/utils.js +1995 -0
- package/dist/camoufox/warnings.d.ts +16 -0
- package/dist/camoufox/warnings.js +65 -0
- package/dist/camoufox/webgl.d.ts +52 -0
- package/dist/camoufox/webgl.js +264 -0
- package/dist/camouwolf/__main__.d.ts +10 -0
- package/dist/camouwolf/__main__.js +1180 -0
- package/dist/camouwolf/__version__.d.ts +31 -0
- package/dist/camouwolf/__version__.js +32 -0
- package/dist/camouwolf/_humanize_playwright.d.ts +53 -0
- package/dist/camouwolf/_humanize_playwright.js +427 -0
- package/dist/camouwolf/addons.d.ts +42 -0
- package/dist/camouwolf/addons.js +113 -0
- package/dist/camouwolf/async_api.d.ts +9 -0
- package/dist/camouwolf/async_api.js +9 -0
- package/dist/camouwolf/browser-pin.d.ts +46 -0
- package/dist/camouwolf/browser-pin.js +96 -0
- package/dist/camouwolf/cli.d.ts +7 -0
- package/dist/camouwolf/cli.js +20 -0
- package/dist/camouwolf/clienthints.d.ts +61 -0
- package/dist/camouwolf/clienthints.js +176 -0
- package/dist/camouwolf/coherence.d.ts +152 -0
- package/dist/camouwolf/coherence.js +554 -0
- package/dist/camouwolf/data-files/browser-pin.json +7 -0
- package/dist/camouwolf/data-files/camouwolf.defaults.json +118 -0
- package/dist/camouwolf/data-files/data/bundle-fonts.json +1983 -0
- package/dist/camouwolf/data-files/data/chrome-languages.json +517 -0
- package/dist/camouwolf/data-files/data/properties.json +321 -0
- package/dist/camouwolf/data-files/data/system-colors.json +35 -0
- package/dist/camouwolf/data-files/data/ubo.json +6 -0
- package/dist/camouwolf/data-files/fpgen.yml +41 -0
- package/dist/camouwolf/data-files/launcher-constants.json +26 -0
- package/dist/camouwolf/data-files/repos.yml +23 -0
- package/dist/camouwolf/data-files/warnings.yml +96 -0
- package/dist/camouwolf/driver_defaults.d.ts +25 -0
- package/dist/camouwolf/driver_defaults.js +114 -0
- package/dist/camouwolf/exceptions.d.ts +95 -0
- package/dist/camouwolf/exceptions.js +150 -0
- package/dist/camouwolf/fingerprints.d.ts +227 -0
- package/dist/camouwolf/fingerprints.js +753 -0
- package/dist/camouwolf/gpudisplay.d.ts +171 -0
- package/dist/camouwolf/gpudisplay.js +711 -0
- package/dist/camouwolf/host_rendering.d.ts +18 -0
- package/dist/camouwolf/host_rendering.js +114 -0
- package/dist/camouwolf/humanize.d.ts +84 -0
- package/dist/camouwolf/humanize.js +128 -0
- package/dist/camouwolf/index.d.ts +22 -0
- package/dist/camouwolf/index.js +21 -0
- package/dist/camouwolf/multiversion.d.ts +131 -0
- package/dist/camouwolf/multiversion.js +491 -0
- package/dist/camouwolf/node-floor.d.ts +5 -0
- package/dist/camouwolf/node-floor.js +17 -0
- package/dist/camouwolf/paths.d.ts +34 -0
- package/dist/camouwolf/paths.js +58 -0
- package/dist/camouwolf/pkgman.d.ts +331 -0
- package/dist/camouwolf/pkgman.js +972 -0
- package/dist/camouwolf/product.d.ts +3 -0
- package/dist/camouwolf/product.js +3 -0
- package/dist/camouwolf/pycompat.d.ts +92 -0
- package/dist/camouwolf/pycompat.js +461 -0
- package/dist/camouwolf/server.d.ts +36 -0
- package/dist/camouwolf/server.js +81 -0
- package/dist/camouwolf/switches.d.ts +77 -0
- package/dist/camouwolf/switches.js +222 -0
- package/dist/camouwolf/sync_api.d.ts +84 -0
- package/dist/camouwolf/sync_api.js +173 -0
- package/dist/camouwolf/utils.d.ts +644 -0
- package/dist/camouwolf/utils.js +2057 -0
- package/dist/camouwolf/warnings.d.ts +14 -0
- package/dist/camouwolf/warnings.js +59 -0
- package/dist/camouwolf/webgl.d.ts +41 -0
- package/dist/camouwolf/webgl.js +198 -0
- package/dist/data.d.ts +1 -1
- package/dist/data.js +3 -3
- package/dist/download.js +1 -1
- package/dist/exceptions.d.ts +1 -1
- package/dist/exceptions.js +1 -1
- package/dist/geolocation.js +1 -1
- package/dist/host.js +1 -1
- package/dist/ip.js +1 -1
- package/dist/locales.js +1 -1
- package/dist/mcp/cli.d.ts +15 -0
- package/dist/mcp/cli.js +108 -0
- package/dist/mcp/index.d.ts +8 -0
- package/dist/mcp/index.js +8 -0
- package/dist/mcp/main.d.ts +2 -0
- package/dist/mcp/main.js +8 -0
- package/dist/mcp/secrets.d.ts +50 -0
- package/dist/mcp/secrets.js +193 -0
- package/dist/mcp/server.d.ts +48 -0
- package/dist/mcp/server.js +653 -0
- package/dist/mcp/tree.d.ts +10 -0
- package/dist/mcp/tree.js +36 -0
- package/dist/passthrough.d.ts +1 -1
- package/dist/product.js +1 -1
- package/dist/settings.js +1 -1
- package/dist/telemetry.d.ts +0 -1
- package/dist/telemetry.js +5 -4
- package/dist/virtdisplay.d.ts +1 -1
- package/dist/virtdisplay.js +1 -1
- package/dist/warnings.js +1 -1
- package/package.json +57 -13
|
@@ -0,0 +1,972 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser package management: version resolution, GitHub release discovery,
|
|
3
|
+
* download/extract, and path lookup.
|
|
4
|
+
*
|
|
5
|
+
* TypeScript twin of python/camoucore/src/camoucore/camouwolf/pkgman.py.
|
|
6
|
+
*/
|
|
7
|
+
import { execFileSync } from "node:child_process";
|
|
8
|
+
import * as fs from "node:fs";
|
|
9
|
+
import { createRequire } from "node:module";
|
|
10
|
+
import * as os from "node:os";
|
|
11
|
+
import * as path from "node:path";
|
|
12
|
+
import { githubHeaders, raiseForStatus, request, webdl } from "../download.js";
|
|
13
|
+
import { CONSTRAINTS, LIBRARY_VERSION } from "./__version__.js";
|
|
14
|
+
import { effectivePin, pinMatches, pinSpec, warnIfUnpaired, } from "./browser-pin.js";
|
|
15
|
+
import { CamouwolfNotInstalled, FileNotFoundError, MissingRelease, ProfileDirectoryError, UnsupportedArchitecture, UnsupportedOS, UnsupportedVersion, } from "./exceptions.js";
|
|
16
|
+
// pkgman and multiversion are mutually dependent, exactly as the Python twin's
|
|
17
|
+
// function-local imports are. Every use below sits inside a function body, so
|
|
18
|
+
// the ESM cycle resolves before any binding is read.
|
|
19
|
+
import { COMPAT_FLAG, findInstalledVersion, getActivePath, getDefaultChannel, installVersioned, loadConfig, loadRepoCache, } from "./multiversion.js";
|
|
20
|
+
import { ARCH_MAP, INSTALL_DIR, LAUNCH_FILE, loadDataFile, OS_ARCH_MATRIX, OS_MAP, OS_NAME, rprint, } from "./paths.js";
|
|
21
|
+
import { comparePyStr, ValueError } from "./pycompat.js";
|
|
22
|
+
import { warn } from "./warnings.js";
|
|
23
|
+
/**
|
|
24
|
+
* Ensure Chromium's Linux configuration directory exists before startup.
|
|
25
|
+
*
|
|
26
|
+
* Chromium probes ~/.config/camouwolf even when Playwright supplies a temporary
|
|
27
|
+
* profile.
|
|
28
|
+
* On a read-only HOME -- the normal shape for a container that bakes the bundle
|
|
29
|
+
* in as root and runs as a non-root user -- a missing directory makes startup
|
|
30
|
+
* stall with no diagnostic, surfacing as a launch timeout rather than as a
|
|
31
|
+
* permissions error. An existing directory may itself still be read-only, which
|
|
32
|
+
* is fine: Chromium only needs it to be there.
|
|
33
|
+
*/
|
|
34
|
+
export function ensureBrowserProfileDir(env) {
|
|
35
|
+
if (OS_NAME !== "lin")
|
|
36
|
+
return undefined;
|
|
37
|
+
const environment = env ?? process.env;
|
|
38
|
+
const configuredHome = environment.HOME;
|
|
39
|
+
const home = configuredHome
|
|
40
|
+
? expandUser(String(configuredHome))
|
|
41
|
+
: os.homedir();
|
|
42
|
+
const profileDir = path.join(home, ".config", "camouwolf");
|
|
43
|
+
if (fs.existsSync(profileDir) && fs.statSync(profileDir).isDirectory()) {
|
|
44
|
+
return profileDir;
|
|
45
|
+
}
|
|
46
|
+
try {
|
|
47
|
+
fs.mkdirSync(profileDir, { recursive: true, mode: 0o700 });
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
throw new ProfileDirectoryError(`Camouwolf requires '${profileDir}' to exist before launch, but it could ` +
|
|
51
|
+
"not be created. For a read-only runtime, create this directory " +
|
|
52
|
+
"before making HOME read-only.", { cause: error });
|
|
53
|
+
}
|
|
54
|
+
if (!fs.statSync(profileDir).isDirectory()) {
|
|
55
|
+
throw new ProfileDirectoryError(`Camouwolf requires '${profileDir}' to be a directory before launch.`);
|
|
56
|
+
}
|
|
57
|
+
return profileDir;
|
|
58
|
+
}
|
|
59
|
+
/** os.path.expanduser for the forms a HOME value can take. */
|
|
60
|
+
function expandUser(p) {
|
|
61
|
+
if (p === "~")
|
|
62
|
+
return os.homedir();
|
|
63
|
+
if (p.startsWith("~/"))
|
|
64
|
+
return path.join(os.homedir(), p.slice(2));
|
|
65
|
+
return p;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Parse a semver string into a comparable tuple.
|
|
69
|
+
*/
|
|
70
|
+
export function parseSemver(version) {
|
|
71
|
+
// A prerelease compares as its release: 0.5.8b1 (PEP 440) and 0.5.8-beta.1
|
|
72
|
+
// (semver) are both 0.5.8, exactly as in the Python twin.
|
|
73
|
+
const release = version
|
|
74
|
+
.replace(/^[\^~]+/, "")
|
|
75
|
+
.split(/(?<=\d)(?:[-+]|(?:a|b|rc|dev|\.dev|\.post)\d*)/)[0];
|
|
76
|
+
const parts = release.split(".");
|
|
77
|
+
// int() semantics: the whole part must be an integer ("1a" -> 0).
|
|
78
|
+
const out = parts.map((part) => /^\s*[+-]?\d+\s*$/.test(part) ? Number.parseInt(part, 10) : 0);
|
|
79
|
+
while (out.length < 3)
|
|
80
|
+
out.push(0);
|
|
81
|
+
return out;
|
|
82
|
+
}
|
|
83
|
+
function compareTuples(a, b) {
|
|
84
|
+
const len = Math.max(a.length, b.length);
|
|
85
|
+
for (let i = 0; i < len; i++) {
|
|
86
|
+
const x = a[i] ?? 0;
|
|
87
|
+
const y = b[i] ?? 0;
|
|
88
|
+
if (x < y)
|
|
89
|
+
return -1;
|
|
90
|
+
if (x > y)
|
|
91
|
+
return 1;
|
|
92
|
+
}
|
|
93
|
+
return 0;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* A comparable browser version string (up to 5 parts).
|
|
97
|
+
*/
|
|
98
|
+
export class Version {
|
|
99
|
+
build;
|
|
100
|
+
version;
|
|
101
|
+
sortedRel;
|
|
102
|
+
constructor(build, version) {
|
|
103
|
+
this.build = build;
|
|
104
|
+
this.version = version;
|
|
105
|
+
// Mirrors the Python twin: digits stay numeric, a leading letter becomes
|
|
106
|
+
// ord(c) - 1024 so "alpha" < "beta" < numeric builds sort sanely.
|
|
107
|
+
const parts = build
|
|
108
|
+
.split(".")
|
|
109
|
+
.map((x) => (/^\d+$/.test(x) ? Number(x) : x.charCodeAt(0) - 1024));
|
|
110
|
+
const padding = 5 - (build.split(".").length - 1);
|
|
111
|
+
for (let i = 0; i < padding; i++)
|
|
112
|
+
parts.push(0);
|
|
113
|
+
this.sortedRel = parts;
|
|
114
|
+
}
|
|
115
|
+
get fullString() {
|
|
116
|
+
return `${this.version}-${this.build}`;
|
|
117
|
+
}
|
|
118
|
+
/** Whether the build channel is alpha (like "alpha.26"). */
|
|
119
|
+
get isAlpha() {
|
|
120
|
+
return this.build.split(".")[0].toLowerCase() === "alpha";
|
|
121
|
+
}
|
|
122
|
+
compare(other) {
|
|
123
|
+
return compareTuples(this.sortedRel, other.sortedRel);
|
|
124
|
+
}
|
|
125
|
+
equals(other) {
|
|
126
|
+
return this.compare(other) === 0;
|
|
127
|
+
}
|
|
128
|
+
lessThan(other) {
|
|
129
|
+
return this.compare(other) < 0;
|
|
130
|
+
}
|
|
131
|
+
greaterOrEqual(other) {
|
|
132
|
+
return this.compare(other) >= 0;
|
|
133
|
+
}
|
|
134
|
+
isSupported() {
|
|
135
|
+
return (this.compare(effectiveVersionMin()) >= 0 && this.compare(VERSION_MAX) < 0);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Read the version from version.json at the given path.
|
|
139
|
+
*/
|
|
140
|
+
static fromPath(dir = INSTALL_DIR) {
|
|
141
|
+
const versionPath = path.join(dir, "version.json");
|
|
142
|
+
if (!fs.existsSync(versionPath)) {
|
|
143
|
+
throw new FileNotFoundError(`Version information not found at ${versionPath}. ` +
|
|
144
|
+
"Please run `camouwolf fetch` to install.");
|
|
145
|
+
}
|
|
146
|
+
const data = JSON.parse(fs.readFileSync(versionPath, "utf-8"));
|
|
147
|
+
// "release" and then "tag" win over "build", as the Python twin's pops do.
|
|
148
|
+
const build = "release" in data ? data.release : "tag" in data ? data.tag : data.build;
|
|
149
|
+
if (build === undefined) {
|
|
150
|
+
throw new Error(`KeyError: 'build' (in ${versionPath})`);
|
|
151
|
+
}
|
|
152
|
+
return new Version(build, data.version ?? undefined);
|
|
153
|
+
}
|
|
154
|
+
static buildMinMax() {
|
|
155
|
+
return [
|
|
156
|
+
new Version(CONSTRAINTS.MIN_VERSION),
|
|
157
|
+
new Version(CONSTRAINTS.MAX_VERSION),
|
|
158
|
+
];
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
export const [VERSION_MIN, VERSION_MAX] = Version.buildMinMax();
|
|
162
|
+
/**
|
|
163
|
+
* Seams the Python tests reach with monkeypatch (VERSION_MIN,
|
|
164
|
+
* _resolved_playwright_version, CamouwolfFetcher). Production code never
|
|
165
|
+
* reassigns these.
|
|
166
|
+
*/
|
|
167
|
+
export const pkgmanDeps = {
|
|
168
|
+
versionMin: () => VERSION_MIN,
|
|
169
|
+
resolvedPlaywrightVersion: () => resolvedPlaywrightVersion(),
|
|
170
|
+
/** The fetcher the auto-install path runs `install()` on. */
|
|
171
|
+
newFetcher: async () => new CamouwolfFetcher().init(),
|
|
172
|
+
};
|
|
173
|
+
/**
|
|
174
|
+
* The resolved playwright-core version string, or null when it cannot be read.
|
|
175
|
+
* The Python twin asks importlib.metadata for `playwright`; the npm package
|
|
176
|
+
* that plays that role here is playwright-core.
|
|
177
|
+
*/
|
|
178
|
+
export function resolvedPlaywrightVersionRaw() {
|
|
179
|
+
return installedPackageVersion("playwright-core");
|
|
180
|
+
}
|
|
181
|
+
/** An installed npm package's version, or null if it cannot be read. */
|
|
182
|
+
export function installedPackageVersion(name) {
|
|
183
|
+
try {
|
|
184
|
+
const require = createRequire(import.meta.url);
|
|
185
|
+
const pkg = JSON.parse(fs.readFileSync(require.resolve(`${name}/package.json`), "utf-8"));
|
|
186
|
+
return typeof pkg.version === "string" ? pkg.version : null;
|
|
187
|
+
}
|
|
188
|
+
catch {
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
/** The installed Playwright version, or null if it cannot be determined. */
|
|
193
|
+
export function resolvedPlaywrightVersion() {
|
|
194
|
+
const raw = resolvedPlaywrightVersionRaw();
|
|
195
|
+
return raw === null ? null : parseSemver(raw);
|
|
196
|
+
}
|
|
197
|
+
/** The installed Playwright version for messages ("the installed version"
|
|
198
|
+
* when it cannot be read). */
|
|
199
|
+
export function resolvedPlaywrightVersionStr() {
|
|
200
|
+
return resolvedPlaywrightVersionRaw() ?? "the installed version";
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* The lowest browser build this install can actually talk to.
|
|
204
|
+
*
|
|
205
|
+
* VERSION_MIN, raised by whatever the resolved Playwright requires. When the
|
|
206
|
+
* Playwright version cannot be read we fall back to VERSION_MIN rather than
|
|
207
|
+
* assuming the worst: a spurious forced re-download is worse than leaving a
|
|
208
|
+
* working install alone, and package.json caps Playwright anyway.
|
|
209
|
+
*/
|
|
210
|
+
export function effectiveVersionMin() {
|
|
211
|
+
let floor = pkgmanDeps.versionMin();
|
|
212
|
+
const playwrightVersion = pkgmanDeps.resolvedPlaywrightVersion();
|
|
213
|
+
if (playwrightVersion === null)
|
|
214
|
+
return floor;
|
|
215
|
+
for (const [requiredPlaywright, build,] of CONSTRAINTS.PLAYWRIGHT_BROWSER_FLOORS) {
|
|
216
|
+
if (compareTuples(playwrightVersion, [...requiredPlaywright]) >= 0 &&
|
|
217
|
+
floor.lessThan(new Version(build))) {
|
|
218
|
+
floor = new Version(build);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
return floor;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Find the browser build constraint for the current library version.
|
|
225
|
+
*/
|
|
226
|
+
function findVersionConstraints(versions, libraryVersion) {
|
|
227
|
+
const libParts = parseSemver(libraryVersion);
|
|
228
|
+
let newest;
|
|
229
|
+
let newestMin;
|
|
230
|
+
for (const entry of versions) {
|
|
231
|
+
const pyLib = entry.python_library ?? {};
|
|
232
|
+
const libMin = parseSemver(pyLib.min ?? "0");
|
|
233
|
+
const libMax = parseSemver(pyLib.max ?? "999");
|
|
234
|
+
if (compareTuples(libMin, libParts) <= 0 &&
|
|
235
|
+
compareTuples(libParts, libMax) < 0) {
|
|
236
|
+
return entry.browser;
|
|
237
|
+
}
|
|
238
|
+
if (newestMin === undefined || compareTuples(libMin, newestMin) > 0) {
|
|
239
|
+
newestMin = libMin;
|
|
240
|
+
newest = entry.browser;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return newest;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Get the min and max build bounds for a channel.
|
|
247
|
+
*/
|
|
248
|
+
function channelBounds(browser, channel) {
|
|
249
|
+
if (!browser)
|
|
250
|
+
return [undefined, undefined];
|
|
251
|
+
if ("stable" in browser || "prerelease" in browser) {
|
|
252
|
+
const section = browser[channel] ?? {};
|
|
253
|
+
return [section.min, section.max];
|
|
254
|
+
}
|
|
255
|
+
return [browser.min, browser.max];
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Configuration for a Camouwolf repository.
|
|
259
|
+
*/
|
|
260
|
+
export class RepoConfig {
|
|
261
|
+
repos;
|
|
262
|
+
name;
|
|
263
|
+
pattern;
|
|
264
|
+
stableMin;
|
|
265
|
+
stableMax;
|
|
266
|
+
prereleaseMin;
|
|
267
|
+
prereleaseMax;
|
|
268
|
+
constructor(init) {
|
|
269
|
+
this.repos = init.repos;
|
|
270
|
+
this.name = init.name;
|
|
271
|
+
this.pattern = init.pattern;
|
|
272
|
+
this.stableMin = init.stableMin;
|
|
273
|
+
this.stableMax = init.stableMax;
|
|
274
|
+
this.prereleaseMin = init.prereleaseMin;
|
|
275
|
+
this.prereleaseMax = init.prereleaseMax;
|
|
276
|
+
}
|
|
277
|
+
/** Primary GitHub repo. */
|
|
278
|
+
get repo() {
|
|
279
|
+
return this.repos[0];
|
|
280
|
+
}
|
|
281
|
+
static loadRepos(spoofLibraryVersion) {
|
|
282
|
+
const data = loadDataFile("repos.yml");
|
|
283
|
+
return (data.browsers ?? []).map((r) => RepoConfig.fromEntry(r, spoofLibraryVersion));
|
|
284
|
+
}
|
|
285
|
+
static getDefaultName() {
|
|
286
|
+
return loadDataFile("repos.yml").default?.browser ?? "Official";
|
|
287
|
+
}
|
|
288
|
+
static fromEntry(entry, spoofLibraryVersion) {
|
|
289
|
+
if (!("pattern" in entry) || entry.pattern == null) {
|
|
290
|
+
throw new Error(`Repo '${entry.name ?? "unknown"}' missing required pattern`);
|
|
291
|
+
}
|
|
292
|
+
let browser;
|
|
293
|
+
if (entry.versions?.length) {
|
|
294
|
+
browser = findVersionConstraints(entry.versions, spoofLibraryVersion || LIBRARY_VERSION);
|
|
295
|
+
}
|
|
296
|
+
const [stableMin, stableMax] = channelBounds(browser, "stable");
|
|
297
|
+
const [prereleaseMin, prereleaseMax] = channelBounds(browser, "prerelease");
|
|
298
|
+
// Parse comma separated repos list (primary + fallbacks)
|
|
299
|
+
const repos = typeof entry.repo === "string"
|
|
300
|
+
? entry.repo.split(",").map((r) => r.trim())
|
|
301
|
+
: entry.repo;
|
|
302
|
+
return new RepoConfig({
|
|
303
|
+
repos,
|
|
304
|
+
name: entry.name,
|
|
305
|
+
pattern: String(entry.pattern),
|
|
306
|
+
stableMin,
|
|
307
|
+
stableMax,
|
|
308
|
+
prereleaseMin,
|
|
309
|
+
prereleaseMax,
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
static getDefault() {
|
|
313
|
+
const found = RepoConfig.findByName(RepoConfig.getDefaultName());
|
|
314
|
+
return found ?? RepoConfig.loadRepos()[0];
|
|
315
|
+
}
|
|
316
|
+
static findByName(name) {
|
|
317
|
+
const lower = name.toLowerCase();
|
|
318
|
+
return RepoConfig.loadRepos().find((r) => r.name.toLowerCase() === lower);
|
|
319
|
+
}
|
|
320
|
+
getOsName(spoofOs) {
|
|
321
|
+
if (spoofOs)
|
|
322
|
+
return spoofOs;
|
|
323
|
+
const osName = OS_MAP[process.platform];
|
|
324
|
+
if (!osName) {
|
|
325
|
+
throw new UnsupportedOS(`OS ${process.platform} is not supported`);
|
|
326
|
+
}
|
|
327
|
+
return osName;
|
|
328
|
+
}
|
|
329
|
+
getArch(spoofArch) {
|
|
330
|
+
if (spoofArch)
|
|
331
|
+
return spoofArch;
|
|
332
|
+
const platArch = os.arch().toLowerCase();
|
|
333
|
+
const arch = ARCH_MAP[platArch];
|
|
334
|
+
if (!arch) {
|
|
335
|
+
throw new UnsupportedArchitecture(`Architecture ${platArch} is not supported`);
|
|
336
|
+
}
|
|
337
|
+
return arch;
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Build the asset regex from the config pattern string.
|
|
341
|
+
*/
|
|
342
|
+
buildPattern(spoofOs, spoofArch) {
|
|
343
|
+
const replacements = {
|
|
344
|
+
name: "(?<name>\\w+)",
|
|
345
|
+
version: "(?<version>[^-]+)",
|
|
346
|
+
build: "(?<build>[^-]+)",
|
|
347
|
+
os: escapeRegExp(this.getOsName(spoofOs)),
|
|
348
|
+
arch: escapeRegExp(this.getArch(spoofArch)),
|
|
349
|
+
};
|
|
350
|
+
const pattern = this.pattern.replace(/\./g, "\\.");
|
|
351
|
+
const regex = pattern.replace(/\{(\w+)\}/g, (match, key) => replacements[key] ?? match);
|
|
352
|
+
return new RegExp(`^${regex}`);
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Check if a build is within the supported range for its channel.
|
|
356
|
+
*/
|
|
357
|
+
isVersionSupported(version, isPrerelease = false) {
|
|
358
|
+
const buildMin = isPrerelease ? this.prereleaseMin : this.stableMin;
|
|
359
|
+
const buildMax = isPrerelease ? this.prereleaseMax : this.stableMax;
|
|
360
|
+
if (buildMin == null || buildMax == null) {
|
|
361
|
+
return true;
|
|
362
|
+
}
|
|
363
|
+
return (new Version(buildMin).compare(version) <= 0 &&
|
|
364
|
+
version.compare(new Version(buildMax)) <= 0);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
function escapeRegExp(value) {
|
|
368
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
369
|
+
}
|
|
370
|
+
/** Whether this library speaks a browser's declared interface. */
|
|
371
|
+
export function interfaceSupported(iface) {
|
|
372
|
+
return CONSTRAINTS.MIN_INTERFACE <= iface && iface <= CONSTRAINTS.INTERFACE;
|
|
373
|
+
}
|
|
374
|
+
const releaseInterfaces = new Map();
|
|
375
|
+
/**
|
|
376
|
+
* The browser interface a GitHub release declares in its manifest.json. A
|
|
377
|
+
* release without a manifest, or a manifest without the field, predates the
|
|
378
|
+
* field and is interface 1.
|
|
379
|
+
*/
|
|
380
|
+
export async function releaseInterface(release) {
|
|
381
|
+
const manifest = (release.assets ?? []).find((a) => a.name === "manifest.json");
|
|
382
|
+
if (!manifest)
|
|
383
|
+
return 1;
|
|
384
|
+
const key = manifest.id ?? manifest.browser_download_url;
|
|
385
|
+
let iface = releaseInterfaces.get(key);
|
|
386
|
+
if (iface === undefined) {
|
|
387
|
+
const url = manifest.browser_download_url;
|
|
388
|
+
const response = await request(url, {
|
|
389
|
+
signal: AbortSignal.timeout(20_000),
|
|
390
|
+
});
|
|
391
|
+
raiseForStatus(response, url);
|
|
392
|
+
iface = Number((await response.json()).interface ?? 1);
|
|
393
|
+
releaseInterfaces.set(key, iface);
|
|
394
|
+
}
|
|
395
|
+
return iface;
|
|
396
|
+
}
|
|
397
|
+
/** The interface recorded in an install's version.json at install time. */
|
|
398
|
+
export function installedInterface(installPath) {
|
|
399
|
+
const data = JSON.parse(fs.readFileSync(path.join(installPath, "version.json"), "utf-8"));
|
|
400
|
+
return Number(data.interface ?? 1);
|
|
401
|
+
}
|
|
402
|
+
/** Why a browser needs a newer package, and the command that gets one. */
|
|
403
|
+
export function upgradeMessage(version, iface) {
|
|
404
|
+
return (`Camouwolf v${version.fullString} needs a newer camouwolf package: it uses browser interface ` +
|
|
405
|
+
`${iface}, and this package supports ${CONSTRAINTS.MIN_INTERFACE}-` +
|
|
406
|
+
`${CONSTRAINTS.INTERFACE}. Upgrade with \`npm install camouwolf@latest\`.`);
|
|
407
|
+
}
|
|
408
|
+
export function incompatibleMessage(version, iface) {
|
|
409
|
+
return (`${upgradeMessage(version, iface)} ` +
|
|
410
|
+
"Or run `camouwolf fetch` to install a browser this package supports.");
|
|
411
|
+
}
|
|
412
|
+
let outdatedWarned = false;
|
|
413
|
+
/**
|
|
414
|
+
* Warn once when the last `camouwolf sync` found a browser release that needs
|
|
415
|
+
* a newer package. Reads the sync cache, so a launch makes no request.
|
|
416
|
+
*/
|
|
417
|
+
export function warnIfPackageOutdated() {
|
|
418
|
+
if (outdatedWarned)
|
|
419
|
+
return;
|
|
420
|
+
const newer = (loadRepoCache().incompatible ?? []);
|
|
421
|
+
if (!newer.length)
|
|
422
|
+
return;
|
|
423
|
+
outdatedWarned = true;
|
|
424
|
+
const versionOf = (v) => new Version(v.build, v.version);
|
|
425
|
+
const newest = newer.reduce((a, b) => versionOf(b).compare(versionOf(a)) > 0 ? b : a);
|
|
426
|
+
warn(upgradeMessage(versionOf(newest), newest.interface));
|
|
427
|
+
}
|
|
428
|
+
/** Test hook: forget that the warning was already issued. */
|
|
429
|
+
export function resetOutdatedWarning() {
|
|
430
|
+
outdatedWarned = false;
|
|
431
|
+
}
|
|
432
|
+
/** A GitHub repo's releases, newest first. */
|
|
433
|
+
async function githubReleases(repo) {
|
|
434
|
+
const apiUrl = `https://api.github.com/repos/${repo}/releases`;
|
|
435
|
+
const response = await request(apiUrl, {
|
|
436
|
+
headers: githubHeaders(apiUrl),
|
|
437
|
+
signal: AbortSignal.timeout(20_000),
|
|
438
|
+
});
|
|
439
|
+
raiseForStatus(response, apiUrl);
|
|
440
|
+
return (await response.json());
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* Information about an available Camouwolf version on GitHub.
|
|
444
|
+
*/
|
|
445
|
+
export class AvailableVersion {
|
|
446
|
+
version;
|
|
447
|
+
url;
|
|
448
|
+
isPrerelease;
|
|
449
|
+
assetId;
|
|
450
|
+
assetSize;
|
|
451
|
+
assetUpdatedAt;
|
|
452
|
+
sha256;
|
|
453
|
+
assetCreatedAt;
|
|
454
|
+
interface = 1;
|
|
455
|
+
constructor(init) {
|
|
456
|
+
Object.assign(this, init);
|
|
457
|
+
this.version = init.version;
|
|
458
|
+
this.url = init.url;
|
|
459
|
+
this.isPrerelease = init.isPrerelease;
|
|
460
|
+
}
|
|
461
|
+
/** First 8 hex chars of the sha256, or empty when unknown. */
|
|
462
|
+
get sha8() {
|
|
463
|
+
return (this.sha256 ?? "").slice(0, 8);
|
|
464
|
+
}
|
|
465
|
+
get display() {
|
|
466
|
+
const pre = this.isPrerelease ? " (prerelease)" : "";
|
|
467
|
+
return `v${this.version.fullString}${pre}`;
|
|
468
|
+
}
|
|
469
|
+
toMetadata() {
|
|
470
|
+
return {
|
|
471
|
+
version: this.version.version ?? null,
|
|
472
|
+
build: this.version.build,
|
|
473
|
+
prerelease: this.isPrerelease,
|
|
474
|
+
asset_id: this.assetId ?? null,
|
|
475
|
+
asset_size: this.assetSize ?? null,
|
|
476
|
+
asset_updated_at: this.assetUpdatedAt ?? null,
|
|
477
|
+
sha256: this.sha256 ?? null,
|
|
478
|
+
created_at: this.assetCreatedAt ?? null,
|
|
479
|
+
interface: this.interface,
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* Handles fetching and installing Camouwolf.
|
|
485
|
+
*/
|
|
486
|
+
export class CamouwolfFetcher {
|
|
487
|
+
githubRepos;
|
|
488
|
+
githubRepo;
|
|
489
|
+
isPrerelease = false;
|
|
490
|
+
repoConfig;
|
|
491
|
+
arch;
|
|
492
|
+
pattern;
|
|
493
|
+
installedSha256;
|
|
494
|
+
installedCreatedAt;
|
|
495
|
+
installedInterface = 1;
|
|
496
|
+
_versionObj;
|
|
497
|
+
_selectedVersion;
|
|
498
|
+
_url;
|
|
499
|
+
constructor(repoConfig, selectedVersion) {
|
|
500
|
+
this.repoConfig = repoConfig ?? RepoConfig.getDefault();
|
|
501
|
+
this.githubRepos = this.repoConfig.repos;
|
|
502
|
+
this.githubRepo = this.githubRepos[0];
|
|
503
|
+
this.arch = this.getPlatformArch();
|
|
504
|
+
this.pattern = this.repoConfig.buildPattern();
|
|
505
|
+
if (selectedVersion) {
|
|
506
|
+
this._selectedVersion = selectedVersion;
|
|
507
|
+
this._versionObj = selectedVersion.version;
|
|
508
|
+
this._url = selectedVersion.url;
|
|
509
|
+
this.isPrerelease = selectedVersion.isPrerelease;
|
|
510
|
+
this.installedSha256 = selectedVersion.sha256;
|
|
511
|
+
this.installedCreatedAt = selectedVersion.assetCreatedAt;
|
|
512
|
+
this.installedInterface = selectedVersion.interface;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* The Python constructor calls fetch_latest() inline; downloads are async in
|
|
517
|
+
* JS, so callers do `await new CamouwolfFetcher().init()` instead.
|
|
518
|
+
*/
|
|
519
|
+
async init() {
|
|
520
|
+
if (!this._versionObj) {
|
|
521
|
+
await this.fetchLatest();
|
|
522
|
+
}
|
|
523
|
+
return this;
|
|
524
|
+
}
|
|
525
|
+
/** First 8 hex chars of the installed asset sha, or empty. */
|
|
526
|
+
get installedSha8() {
|
|
527
|
+
return (this.installedSha256 ?? "").slice(0, 8);
|
|
528
|
+
}
|
|
529
|
+
/**
|
|
530
|
+
* Match a release asset against version constraints, OS, and arch.
|
|
531
|
+
*/
|
|
532
|
+
checkAsset(asset, release) {
|
|
533
|
+
const match = this.pattern.exec(asset.name);
|
|
534
|
+
if (!match?.groups)
|
|
535
|
+
return null;
|
|
536
|
+
const version = new Version(match.groups.build, match.groups.version);
|
|
537
|
+
// A released library installs exactly the browser it was released with
|
|
538
|
+
// (browser-pin.ts), unless the user explicitly chose something else.
|
|
539
|
+
const pin = effectivePin(loadConfig());
|
|
540
|
+
if (pin &&
|
|
541
|
+
!pinMatches(pin, this.repoConfig.name, match.groups.version, match.groups.build)) {
|
|
542
|
+
return null;
|
|
543
|
+
}
|
|
544
|
+
const isPrerelease = Boolean(release?.prerelease) || version.isAlpha;
|
|
545
|
+
if (!pin && !this.repoConfig.isVersionSupported(version, isPrerelease)) {
|
|
546
|
+
return null;
|
|
547
|
+
}
|
|
548
|
+
const digest = asset.digest ?? "";
|
|
549
|
+
if (digest.startsWith("sha256:")) {
|
|
550
|
+
this.installedSha256 = digest.slice("sha256:".length);
|
|
551
|
+
}
|
|
552
|
+
this.installedCreatedAt = asset.created_at;
|
|
553
|
+
return [version, asset.browser_download_url];
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* Whether a release whose asset checkAsset() matched may be used. The
|
|
557
|
+
* Python twin decides this inside check_asset(); reading a manifest is
|
|
558
|
+
* async here, so it is a separate step.
|
|
559
|
+
*/
|
|
560
|
+
async acceptRelease(release, [version]) {
|
|
561
|
+
const iface = await releaseInterface(release);
|
|
562
|
+
if (!interfaceSupported(iface)) {
|
|
563
|
+
rprint(incompatibleMessage(version, iface), "yellow");
|
|
564
|
+
return false;
|
|
565
|
+
}
|
|
566
|
+
this.installedInterface = iface;
|
|
567
|
+
return true;
|
|
568
|
+
}
|
|
569
|
+
missingAssetError() {
|
|
570
|
+
throw new MissingRelease(`No matching release found for ${OS_NAME} ${this.arch} in the ` +
|
|
571
|
+
"supported range. Please update the camouwolf package.");
|
|
572
|
+
}
|
|
573
|
+
getReleases(githubRepo) {
|
|
574
|
+
return githubReleases(githubRepo);
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
* Fetch the first matching release asset, trying fallback repos on failure.
|
|
578
|
+
*/
|
|
579
|
+
async getAsset() {
|
|
580
|
+
let lastError;
|
|
581
|
+
for (const repo of this.githubRepos) {
|
|
582
|
+
try {
|
|
583
|
+
const releases = await this.getReleases(repo);
|
|
584
|
+
for (const release of releases) {
|
|
585
|
+
for (const asset of release.assets ?? []) {
|
|
586
|
+
const data = this.checkAsset(asset, release);
|
|
587
|
+
if (data && (await this.acceptRelease(release, data))) {
|
|
588
|
+
this.githubRepo = repo;
|
|
589
|
+
this.isPrerelease = release.prerelease ?? false;
|
|
590
|
+
return data;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
catch (error) {
|
|
596
|
+
lastError = error;
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
if (lastError)
|
|
600
|
+
throw lastError;
|
|
601
|
+
this.missingAssetError();
|
|
602
|
+
}
|
|
603
|
+
getPlatformArch() {
|
|
604
|
+
const arch = (this.repoConfig ?? RepoConfig.getDefault()).getArch();
|
|
605
|
+
if (!OS_ARCH_MATRIX[OS_NAME].includes(arch)) {
|
|
606
|
+
throw new UnsupportedArchitecture(`Architecture ${arch} is not supported for ${OS_NAME}`);
|
|
607
|
+
}
|
|
608
|
+
return arch;
|
|
609
|
+
}
|
|
610
|
+
/**
|
|
611
|
+
* Fetch the latest camouwolf release for the current platform.
|
|
612
|
+
*/
|
|
613
|
+
async fetchLatest() {
|
|
614
|
+
const [versionObj, url] = await this.getAsset();
|
|
615
|
+
this._versionObj = versionObj;
|
|
616
|
+
this._url = url;
|
|
617
|
+
}
|
|
618
|
+
static async downloadFile(file, url) {
|
|
619
|
+
rprint(`Downloading package: ${url}`);
|
|
620
|
+
await webdl(url, undefined, true, file);
|
|
621
|
+
}
|
|
622
|
+
/**
|
|
623
|
+
* Download and install camouwolf to a versioned subdirectory.
|
|
624
|
+
*/
|
|
625
|
+
async install(replace = false) {
|
|
626
|
+
const installed = await installVersioned(this, replace);
|
|
627
|
+
ensureBrowserProfileDir();
|
|
628
|
+
return installed;
|
|
629
|
+
}
|
|
630
|
+
get url() {
|
|
631
|
+
if (!this._url) {
|
|
632
|
+
throw new ValueError("Url is not available. Make sure to run init() first.");
|
|
633
|
+
}
|
|
634
|
+
return this._url;
|
|
635
|
+
}
|
|
636
|
+
get version() {
|
|
637
|
+
if (!this._versionObj?.version) {
|
|
638
|
+
throw new ValueError("Version is not available. Make sure to run init() first.");
|
|
639
|
+
}
|
|
640
|
+
return this._versionObj.version;
|
|
641
|
+
}
|
|
642
|
+
get build() {
|
|
643
|
+
if (!this._versionObj) {
|
|
644
|
+
throw new ValueError("Build information is not available. Make sure to run init() first.");
|
|
645
|
+
}
|
|
646
|
+
return this._versionObj.build;
|
|
647
|
+
}
|
|
648
|
+
get verstr() {
|
|
649
|
+
if (!this._versionObj) {
|
|
650
|
+
throw new ValueError("Version is not available. Make sure to run init() first.");
|
|
651
|
+
}
|
|
652
|
+
return this._versionObj.fullString;
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
/**
|
|
656
|
+
* Fetch all supported versions from GitHub for the current platform. With
|
|
657
|
+
* includeIncompatible, builds that need a newer package are returned too
|
|
658
|
+
* (check `interfaceSupported(v.interface)`) instead of being reported.
|
|
659
|
+
*/
|
|
660
|
+
export async function listAvailableVersions(repoConfig, includePrerelease = true, spoofOs, spoofArch, includeIncompatible = false) {
|
|
661
|
+
const config = repoConfig ?? RepoConfig.getDefault();
|
|
662
|
+
const pattern = config.buildPattern(spoofOs, spoofArch);
|
|
663
|
+
const osName = spoofOs ?? OS_NAME;
|
|
664
|
+
const arch = config.getArch(spoofArch);
|
|
665
|
+
if (!(OS_ARCH_MATRIX[osName] ?? []).includes(arch)) {
|
|
666
|
+
throw new UnsupportedArchitecture(`Architecture ${arch} is not supported for ${osName}`);
|
|
667
|
+
}
|
|
668
|
+
let releases = [];
|
|
669
|
+
let lastError;
|
|
670
|
+
for (const repo of config.repos) {
|
|
671
|
+
try {
|
|
672
|
+
releases = await githubReleases(repo);
|
|
673
|
+
break;
|
|
674
|
+
}
|
|
675
|
+
catch (error) {
|
|
676
|
+
lastError = error;
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
if (!releases.length && lastError)
|
|
680
|
+
throw lastError;
|
|
681
|
+
const versions = [];
|
|
682
|
+
for (const release of releases) {
|
|
683
|
+
versions.push(...(await releaseVersions(release, config, pattern, includePrerelease, includeIncompatible)));
|
|
684
|
+
}
|
|
685
|
+
versions.sort((a, b) => {
|
|
686
|
+
const byVersion = b.version.compare(a.version);
|
|
687
|
+
if (byVersion !== 0)
|
|
688
|
+
return byVersion;
|
|
689
|
+
return comparePyStr(b.assetCreatedAt ?? "", a.assetCreatedAt ?? "");
|
|
690
|
+
});
|
|
691
|
+
return versions;
|
|
692
|
+
}
|
|
693
|
+
/**
|
|
694
|
+
* The supported builds the release tagged `tag` holds for this platform, read
|
|
695
|
+
* with one API call per repo of the source. Empty when no repo has the tag.
|
|
696
|
+
*/
|
|
697
|
+
export async function taggedVersions(repoConfig, tag) {
|
|
698
|
+
for (const repo of repoConfig.repos) {
|
|
699
|
+
const apiUrl = `https://api.github.com/repos/${repo}/releases/tags/${tag}`;
|
|
700
|
+
const response = await request(apiUrl, {
|
|
701
|
+
headers: githubHeaders(apiUrl),
|
|
702
|
+
signal: AbortSignal.timeout(20_000),
|
|
703
|
+
});
|
|
704
|
+
if (response.status === 404)
|
|
705
|
+
continue;
|
|
706
|
+
raiseForStatus(response, apiUrl);
|
|
707
|
+
return releaseVersions((await response.json()), repoConfig, repoConfig.buildPattern());
|
|
708
|
+
}
|
|
709
|
+
return [];
|
|
710
|
+
}
|
|
711
|
+
/**
|
|
712
|
+
* The release's assets that match `pattern` and the source's version range.
|
|
713
|
+
*/
|
|
714
|
+
async function releaseVersions(release, config, pattern, includePrerelease = true, includeIncompatible = false) {
|
|
715
|
+
const isPrerelease = release.prerelease ?? false;
|
|
716
|
+
if (isPrerelease && !includePrerelease)
|
|
717
|
+
return [];
|
|
718
|
+
const versions = [];
|
|
719
|
+
for (const asset of release.assets ?? []) {
|
|
720
|
+
const match = pattern.exec(asset.name);
|
|
721
|
+
if (!match?.groups)
|
|
722
|
+
continue;
|
|
723
|
+
const version = new Version(match.groups.build, match.groups.version);
|
|
724
|
+
const assetPrerelease = isPrerelease || version.isAlpha;
|
|
725
|
+
if (assetPrerelease && !includePrerelease)
|
|
726
|
+
continue;
|
|
727
|
+
if (!config.isVersionSupported(version, assetPrerelease))
|
|
728
|
+
continue;
|
|
729
|
+
const iface = await releaseInterface(release);
|
|
730
|
+
if (!interfaceSupported(iface) && !includeIncompatible) {
|
|
731
|
+
rprint(incompatibleMessage(version, iface), "yellow");
|
|
732
|
+
continue;
|
|
733
|
+
}
|
|
734
|
+
const digest = asset.digest ?? "";
|
|
735
|
+
const sha256 = digest.startsWith("sha256:")
|
|
736
|
+
? digest.slice("sha256:".length)
|
|
737
|
+
: undefined;
|
|
738
|
+
versions.push(new AvailableVersion({
|
|
739
|
+
version,
|
|
740
|
+
url: asset.browser_download_url,
|
|
741
|
+
isPrerelease: assetPrerelease,
|
|
742
|
+
assetId: asset.id,
|
|
743
|
+
assetSize: asset.size,
|
|
744
|
+
assetUpdatedAt: asset.updated_at,
|
|
745
|
+
sha256,
|
|
746
|
+
assetCreatedAt: asset.created_at,
|
|
747
|
+
interface: iface,
|
|
748
|
+
}));
|
|
749
|
+
}
|
|
750
|
+
return versions;
|
|
751
|
+
}
|
|
752
|
+
/**
|
|
753
|
+
* Get the full version string of the active install.
|
|
754
|
+
*/
|
|
755
|
+
export function installedVerStr(fromDir) {
|
|
756
|
+
// An explicit directory (the folder holding a sandbox/alt-version
|
|
757
|
+
// executable_path) reads that build's version.json instead of the active
|
|
758
|
+
// install's. Mac-bundle aware: an executable inside
|
|
759
|
+
// Camouwolf.app/Contents/MacOS/ belongs to an install whose version.json sits
|
|
760
|
+
// three levels up -- unless a deployment stamped one beside the binary.
|
|
761
|
+
if (fromDir) {
|
|
762
|
+
let dir = fromDir;
|
|
763
|
+
if (path.basename(dir) === "MacOS" &&
|
|
764
|
+
!fs.existsSync(path.join(dir, "version.json"))) {
|
|
765
|
+
dir = path.dirname(path.dirname(path.dirname(dir)));
|
|
766
|
+
}
|
|
767
|
+
return Version.fromPath(dir).fullString;
|
|
768
|
+
}
|
|
769
|
+
const active = getActivePath();
|
|
770
|
+
if (active === null)
|
|
771
|
+
throw notInstalledError();
|
|
772
|
+
const version = Version.fromPath(active);
|
|
773
|
+
if (path.basename(path.dirname(path.dirname(active))) === "browsers") {
|
|
774
|
+
warnIfUnpaired(path.basename(path.dirname(active)), version.version ?? "", version.build);
|
|
775
|
+
}
|
|
776
|
+
return version.fullString;
|
|
777
|
+
}
|
|
778
|
+
/**
|
|
779
|
+
* Whether INSTALL_DIR's root holds a supported build.
|
|
780
|
+
*
|
|
781
|
+
* Only the pre-multiversion flat layout wrote version.json at the root; the
|
|
782
|
+
* versioned layout keeps it under browsers/<repo>/<version>/. A missing root
|
|
783
|
+
* version.json means "no legacy install here", so the caller should fall
|
|
784
|
+
* through to a fetch rather than raise. The alpha.1 floor masked this: no
|
|
785
|
+
* install was ever unsupported, so this branch was never reached.
|
|
786
|
+
*/
|
|
787
|
+
export function rootInstallSupported() {
|
|
788
|
+
try {
|
|
789
|
+
return Version.fromPath().isSupported();
|
|
790
|
+
}
|
|
791
|
+
catch (error) {
|
|
792
|
+
if (error instanceof FileNotFoundError)
|
|
793
|
+
return false;
|
|
794
|
+
throw error;
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
/** What is missing -- the paired build, a pin, or a channel -- and how to get it. */
|
|
798
|
+
function notInstalledError() {
|
|
799
|
+
const config = loadConfig();
|
|
800
|
+
const pin = effectivePin(config);
|
|
801
|
+
const pinned = config.pinned;
|
|
802
|
+
const channel = config.channel || getDefaultChannel();
|
|
803
|
+
const missing = pin
|
|
804
|
+
? `${pin.repoName} ${pinSpec(pin)}, the browser this camouwolf release pairs with,`
|
|
805
|
+
: pinned
|
|
806
|
+
? `${channel}/${pinned}`
|
|
807
|
+
: channel;
|
|
808
|
+
return new CamouwolfNotInstalled(`${missing} is not installed. Please run \`camouwolf fetch\` to install.`);
|
|
809
|
+
}
|
|
810
|
+
/**
|
|
811
|
+
* The part of the Python twin's camouwolf_path() that runs before it would
|
|
812
|
+
* download: returns the install to use, or null when a fetch is needed (only
|
|
813
|
+
* possible with `downloadIfMissing`; otherwise the Python errors are raised).
|
|
814
|
+
*/
|
|
815
|
+
function resolveInstalledPath(downloadIfMissing) {
|
|
816
|
+
// Clean up incompatible old data directory
|
|
817
|
+
if (fs.existsSync(INSTALL_DIR) &&
|
|
818
|
+
fs.readdirSync(INSTALL_DIR).length > 0 &&
|
|
819
|
+
!fs.existsSync(COMPAT_FLAG)) {
|
|
820
|
+
rprint("Cleaning old data...", "yellow");
|
|
821
|
+
fs.rmSync(INSTALL_DIR, { recursive: true, force: true });
|
|
822
|
+
}
|
|
823
|
+
const active = getActivePath();
|
|
824
|
+
if (active) {
|
|
825
|
+
const installed = Version.fromPath(active);
|
|
826
|
+
const iface = installedInterface(active);
|
|
827
|
+
if (!interfaceSupported(iface)) {
|
|
828
|
+
throw new UnsupportedVersion(incompatibleMessage(installed, iface));
|
|
829
|
+
}
|
|
830
|
+
if (installed.isSupported())
|
|
831
|
+
return active;
|
|
832
|
+
}
|
|
833
|
+
if (!fs.existsSync(INSTALL_DIR) || fs.readdirSync(INSTALL_DIR).length === 0) {
|
|
834
|
+
if (!downloadIfMissing)
|
|
835
|
+
throw notInstalledError();
|
|
836
|
+
}
|
|
837
|
+
else if (rootInstallSupported()) {
|
|
838
|
+
return INSTALL_DIR;
|
|
839
|
+
}
|
|
840
|
+
else if (!downloadIfMissing) {
|
|
841
|
+
// Other builds are installed, but not the one this release pairs with.
|
|
842
|
+
if (effectivePin(loadConfig()))
|
|
843
|
+
throw notInstalledError();
|
|
844
|
+
throw new UnsupportedVersion("Camouwolf executable is outdated.");
|
|
845
|
+
}
|
|
846
|
+
return null;
|
|
847
|
+
}
|
|
848
|
+
/**
|
|
849
|
+
* Full path to the active camouwolf folder.
|
|
850
|
+
*
|
|
851
|
+
* This is the Python twin's camouwolf_path(download_if_missing=False): it is
|
|
852
|
+
* called from synchronous path helpers, and a download is asynchronous in JS.
|
|
853
|
+
* `ensureCamouwolfInstalled()` is the download_if_missing=True half;
|
|
854
|
+
* `launchOptions()` awaits it before any path lookup, so first-run
|
|
855
|
+
* auto-download behaviour is preserved.
|
|
856
|
+
*/
|
|
857
|
+
export function camouwolfPath() {
|
|
858
|
+
return resolveInstalledPath(false);
|
|
859
|
+
}
|
|
860
|
+
/**
|
|
861
|
+
* Resolve the active browser, downloading it first when nothing usable is
|
|
862
|
+
* installed. The async counterpart to the Python twin's camouwolf_path().
|
|
863
|
+
*/
|
|
864
|
+
export async function ensureCamouwolfInstalled() {
|
|
865
|
+
const found = resolveInstalledPath(true);
|
|
866
|
+
if (found !== null)
|
|
867
|
+
return found;
|
|
868
|
+
const fetcher = await pkgmanDeps.newFetcher();
|
|
869
|
+
await fetcher.install();
|
|
870
|
+
// Re-check rather than recurse.
|
|
871
|
+
//
|
|
872
|
+
// If the newest published build is still below the floor -- a library
|
|
873
|
+
// published ahead of its browser release, or a repos source that does not
|
|
874
|
+
// carry it -- install() is a no-op ("already installed") and recursing here
|
|
875
|
+
// spun ~1000 fetch attempts into a stack overflow, having hammered the
|
|
876
|
+
// GitHub API into a rate limit on the way. Say what is actually wrong.
|
|
877
|
+
const active = getActivePath();
|
|
878
|
+
if (active && Version.fromPath(active).isSupported()) {
|
|
879
|
+
return active;
|
|
880
|
+
}
|
|
881
|
+
if (fs.existsSync(INSTALL_DIR) && rootInstallSupported()) {
|
|
882
|
+
return INSTALL_DIR;
|
|
883
|
+
}
|
|
884
|
+
throw new UnsupportedVersion("No available Camouwolf build satisfies this library's minimum " +
|
|
885
|
+
`(${CONSTRAINTS.MIN_VERSION}). The matching browser release may not be ` +
|
|
886
|
+
"published yet; wait for it, or install an older camouwolf release.");
|
|
887
|
+
}
|
|
888
|
+
/**
|
|
889
|
+
* A file the build ships, found from its executable: beside it, or in
|
|
890
|
+
* Contents/Resources when the executable is a macOS app bundle's Contents/MacOS/.
|
|
891
|
+
*/
|
|
892
|
+
export function buildFile(executablePath, name) {
|
|
893
|
+
const directory = path.dirname(executablePath);
|
|
894
|
+
return path.basename(directory) === "MacOS"
|
|
895
|
+
? path.join(path.dirname(directory), "Resources", name)
|
|
896
|
+
: path.join(directory, name);
|
|
897
|
+
}
|
|
898
|
+
/** Get the path to a file in the camouwolf directory. */
|
|
899
|
+
export function getPath(file) {
|
|
900
|
+
if (OS_NAME === "mac") {
|
|
901
|
+
return path.resolve(camouwolfPath(), "Camouwolf.app", "Contents", "Resources", file);
|
|
902
|
+
}
|
|
903
|
+
return path.join(camouwolfPath(), file);
|
|
904
|
+
}
|
|
905
|
+
/**
|
|
906
|
+
* Get the path to the camouwolf executable.
|
|
907
|
+
*/
|
|
908
|
+
export function launchPath(browserPath) {
|
|
909
|
+
let execPath;
|
|
910
|
+
if (browserPath) {
|
|
911
|
+
execPath =
|
|
912
|
+
OS_NAME === "mac"
|
|
913
|
+
? path.resolve(browserPath, "Camouwolf.app", "Contents", "Resources", LAUNCH_FILE[OS_NAME])
|
|
914
|
+
: path.join(browserPath, LAUNCH_FILE[OS_NAME]);
|
|
915
|
+
}
|
|
916
|
+
else {
|
|
917
|
+
execPath = getPath(LAUNCH_FILE[OS_NAME]);
|
|
918
|
+
}
|
|
919
|
+
if (!fs.existsSync(execPath)) {
|
|
920
|
+
throw new CamouwolfNotInstalled(`Camouwolf is not installed at ${browserPath ?? camouwolfPath()}. ` +
|
|
921
|
+
"Please run `camouwolf fetch` to install.");
|
|
922
|
+
}
|
|
923
|
+
return execPath;
|
|
924
|
+
}
|
|
925
|
+
/**
|
|
926
|
+
* The executable of the installed build `specifier` names (the `browser`
|
|
927
|
+
* launch option). Like `camouwolf set`, it overrides the pairing: it never
|
|
928
|
+
* downloads, and it warns when the build is not the paired one.
|
|
929
|
+
*/
|
|
930
|
+
export function selectedLaunchPath(specifier) {
|
|
931
|
+
const installPath = findInstalledVersion(specifier);
|
|
932
|
+
if (installPath === null) {
|
|
933
|
+
throw new ValueError(`Browser version '${specifier}' not found. Run \`camouwolf list\` to see installed versions.`);
|
|
934
|
+
}
|
|
935
|
+
const version = Version.fromPath(installPath);
|
|
936
|
+
const iface = installedInterface(installPath);
|
|
937
|
+
if (!interfaceSupported(iface)) {
|
|
938
|
+
throw new UnsupportedVersion(incompatibleMessage(version, iface));
|
|
939
|
+
}
|
|
940
|
+
warnIfUnpaired(path.basename(path.dirname(installPath)), version.version ?? "", version.build);
|
|
941
|
+
return launchPath(installPath);
|
|
942
|
+
}
|
|
943
|
+
/**
|
|
944
|
+
* chmod -R 755 on POSIX so the freshly-extracted binaries are executable.
|
|
945
|
+
* (The zip does not carry usable permission bits on every platform.)
|
|
946
|
+
*/
|
|
947
|
+
export function makeExecutable(dir) {
|
|
948
|
+
if (OS_NAME === "win")
|
|
949
|
+
return;
|
|
950
|
+
try {
|
|
951
|
+
execFileSync("chmod", ["-R", "755", dir]);
|
|
952
|
+
}
|
|
953
|
+
catch (error) {
|
|
954
|
+
rprint(`Warning: could not chmod ${dir}: ${error}`, "yellow");
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
/**
|
|
958
|
+
* Format an asset timestamp as "Mon D", or "Mon D, YYYY" when the year differs.
|
|
959
|
+
*/
|
|
960
|
+
export function formatAssetDate(iso, now) {
|
|
961
|
+
if (!iso)
|
|
962
|
+
return "";
|
|
963
|
+
const dt = new Date(iso);
|
|
964
|
+
if (Number.isNaN(dt.getTime()))
|
|
965
|
+
return "";
|
|
966
|
+
const currentYear = (now ?? new Date()).getFullYear();
|
|
967
|
+
const month = dt.toLocaleString("en-US", { month: "short" });
|
|
968
|
+
if (dt.getFullYear() === currentYear) {
|
|
969
|
+
return `${month} ${dt.getDate()}`;
|
|
970
|
+
}
|
|
971
|
+
return `${month} ${dt.getDate()}, ${dt.getFullYear()}`;
|
|
972
|
+
}
|