camoucore 0.1.2
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 +26 -0
- package/THIRD_PARTY_NOTICES.md +64 -0
- package/dist/cpu_affinity.d.ts +49 -0
- package/dist/cpu_affinity.js +210 -0
- package/dist/data-files/fpgen-model.json +22 -0
- package/dist/data-files/geoip.yml +40 -0
- package/dist/data-files/territoryInfo.xml +2024 -0
- package/dist/data-files/warnings.yml +14 -0
- package/dist/data.d.ts +5 -0
- package/dist/data.js +22 -0
- package/dist/display.d.ts +18 -0
- package/dist/display.js +129 -0
- package/dist/download.d.ts +60 -0
- package/dist/download.js +233 -0
- package/dist/exceptions.d.ts +46 -0
- package/dist/exceptions.js +91 -0
- package/dist/geolocation.d.ts +82 -0
- package/dist/geolocation.js +355 -0
- package/dist/host.d.ts +9 -0
- package/dist/host.js +49 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/ip.d.ts +27 -0
- package/dist/ip.js +302 -0
- package/dist/locales.d.ts +89 -0
- package/dist/locales.js +343 -0
- package/dist/product.d.ts +27 -0
- package/dist/product.js +36 -0
- package/dist/virtdisplay.d.ts +74 -0
- package/dist/virtdisplay.js +310 -0
- package/dist/warnings.d.ts +32 -0
- package/dist/warnings.js +68 -0
- package/package.json +61 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
no_region: >-
|
|
2
|
+
Because you did not pass in a locale region, one will be generated for you.
|
|
3
|
+
This can cause suspicion if your IP does not match your locale region.
|
|
4
|
+
|
|
5
|
+
geoip_deprecated: >-
|
|
6
|
+
The GeoIP source "{name}" is deprecated and will be removed in a future release.
|
|
7
|
+
"{default}" is the default and resolves locations more accurately.
|
|
8
|
+
Run `camoufox set --geoip` or `camouwolf set --geoip` to switch (both share one
|
|
9
|
+
GeoIP cache), or stop passing `geoip_db`.
|
|
10
|
+
|
|
11
|
+
geoip_frozen: >-
|
|
12
|
+
The newest "{name}" GeoIP database was built {days} days ago, so its source may have
|
|
13
|
+
stopped publishing and locations may be out of date. The sources are listed in
|
|
14
|
+
camoucore's geoip.yml.
|
package/dist/data.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const DATA_FILES: readonly string[];
|
|
2
|
+
/** dist/data-files/ in a built package, else python/src/camoucore/ itself. */
|
|
3
|
+
export declare const LOCAL_DATA: string;
|
|
4
|
+
/** A data file, parsed as YAML or JSON by its extension. */
|
|
5
|
+
export declare function loadDataFile<T = any>(file: string): T;
|
package/dist/data.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The data files camoucore ships. They live in python/src/camoucore/, the one
|
|
3
|
+
* copy in the repository; `pnpm build` copies them into dist/data-files/.
|
|
4
|
+
*/
|
|
5
|
+
import * as fs from "node:fs";
|
|
6
|
+
import * as path from "node:path";
|
|
7
|
+
import { parse as parseYaml } from "yaml";
|
|
8
|
+
export const DATA_FILES = [
|
|
9
|
+
"fpgen-model.json",
|
|
10
|
+
"geoip.yml",
|
|
11
|
+
"territoryInfo.xml",
|
|
12
|
+
"warnings.yml",
|
|
13
|
+
];
|
|
14
|
+
/** dist/data-files/ in a built package, else python/src/camoucore/ itself. */
|
|
15
|
+
export const LOCAL_DATA = fs.existsSync(path.join(import.meta.dirname, "data-files"))
|
|
16
|
+
? path.join(import.meta.dirname, "data-files")
|
|
17
|
+
: path.resolve(import.meta.dirname, "..", "..", "python", "src", "camoucore");
|
|
18
|
+
/** A data file, parsed as YAML or JSON by its extension. */
|
|
19
|
+
export function loadDataFile(file) {
|
|
20
|
+
const text = fs.readFileSync(path.join(LOCAL_DATA, file), "utf-8");
|
|
21
|
+
return (file.endsWith(".yml") ? parseYaml(text) : JSON.parse(text));
|
|
22
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** Size of a monitor in CSS pixels. */
|
|
2
|
+
export interface DisplaySize {
|
|
3
|
+
width: number;
|
|
4
|
+
height: number;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Whether the host has a desktop session for the browser's window to open on.
|
|
8
|
+
*
|
|
9
|
+
* DISPLAY / WAYLAND_DISPLAY only ever exist on Linux, so they cannot be the
|
|
10
|
+
* sole probe: keying off DISPLAY alone skips the screen constraints entirely on
|
|
11
|
+
* Windows and macOS, where a session is always present.
|
|
12
|
+
*/
|
|
13
|
+
export declare function hasDisplay(env: Record<string, string | number | boolean | undefined>): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Size of the roomiest attached monitor in CSS pixels, or null when the display
|
|
16
|
+
* cannot be probed (no monitors, or enumeration failed).
|
|
17
|
+
*/
|
|
18
|
+
export declare function largestDisplay(): DisplaySize | null;
|
package/dist/display.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host display geometry in CSS pixels, the unit the browser sizes windows in: a
|
|
3
|
+
* physical-pixel size opens a scaled Windows window off-screen (#425). Twin of
|
|
4
|
+
* display.py; a failed probe returns null, and the screen constraint is skipped.
|
|
5
|
+
*/
|
|
6
|
+
import { execFileSync } from "node:child_process";
|
|
7
|
+
import { OS_NAME } from "./host.js";
|
|
8
|
+
/**
|
|
9
|
+
* Whether the host has a desktop session for the browser's window to open on.
|
|
10
|
+
*
|
|
11
|
+
* DISPLAY / WAYLAND_DISPLAY only ever exist on Linux, so they cannot be the
|
|
12
|
+
* sole probe: keying off DISPLAY alone skips the screen constraints entirely on
|
|
13
|
+
* Windows and macOS, where a session is always present.
|
|
14
|
+
*/
|
|
15
|
+
export function hasDisplay(env) {
|
|
16
|
+
if (OS_NAME !== "lin") {
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
return Boolean(env.DISPLAY || env.WAYLAND_DISPLAY);
|
|
20
|
+
}
|
|
21
|
+
function run(command, args) {
|
|
22
|
+
try {
|
|
23
|
+
return execFileSync(command, args, {
|
|
24
|
+
encoding: "utf-8",
|
|
25
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
26
|
+
timeout: 5000,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/** Every connected monitor's resolution, or [] when it can't be probed. */
|
|
34
|
+
function enumerateMonitors() {
|
|
35
|
+
if (OS_NAME === "lin")
|
|
36
|
+
return enumerateLinux();
|
|
37
|
+
if (OS_NAME === "mac")
|
|
38
|
+
return enumerateMac();
|
|
39
|
+
return enumerateWindows();
|
|
40
|
+
}
|
|
41
|
+
function enumerateLinux() {
|
|
42
|
+
// `xrandr --current` avoids a mode probe and is safe to call repeatedly.
|
|
43
|
+
// Connected outputs carry a "<w>x<h>+<x>+<y>" geometry token.
|
|
44
|
+
const out = run("xrandr", ["--current"]);
|
|
45
|
+
if (!out)
|
|
46
|
+
return [];
|
|
47
|
+
const monitors = [];
|
|
48
|
+
for (const line of out.split("\n")) {
|
|
49
|
+
if (!/\bconnected\b/.test(line))
|
|
50
|
+
continue;
|
|
51
|
+
const match = line.match(/\b(\d+)x(\d+)\+\d+\+\d+/);
|
|
52
|
+
if (!match)
|
|
53
|
+
continue;
|
|
54
|
+
monitors.push({
|
|
55
|
+
width: Number.parseInt(match[1], 10),
|
|
56
|
+
height: Number.parseInt(match[2], 10),
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
return monitors;
|
|
60
|
+
}
|
|
61
|
+
function enumerateMac() {
|
|
62
|
+
const out = run("system_profiler", ["-json", "SPDisplaysDataType"]);
|
|
63
|
+
if (!out)
|
|
64
|
+
return [];
|
|
65
|
+
try {
|
|
66
|
+
const data = JSON.parse(out);
|
|
67
|
+
const monitors = [];
|
|
68
|
+
for (const gpu of data.SPDisplaysDataType ?? []) {
|
|
69
|
+
for (const display of gpu.spdisplays_ndrvs ?? []) {
|
|
70
|
+
// e.g. "2560 x 1440" or "2560 x 1440 @ 60.00Hz"
|
|
71
|
+
const raw = display._spdisplays_resolution ?? display.spdisplays_resolution ?? "";
|
|
72
|
+
const match = raw.match(/(\d+)\s*x\s*(\d+)/);
|
|
73
|
+
if (!match)
|
|
74
|
+
continue;
|
|
75
|
+
monitors.push({
|
|
76
|
+
width: Number.parseInt(match[1], 10),
|
|
77
|
+
height: Number.parseInt(match[2], 10),
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return monitors;
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
return [];
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function enumerateWindows() {
|
|
88
|
+
// Screen.AllScreens reports DPI-*unaware* bounds for a non-manifested
|
|
89
|
+
// process, which is exactly the CSS-pixel figure the browser lays out in --
|
|
90
|
+
// so unlike the Python twin no scale-factor correction is needed here.
|
|
91
|
+
const script = "Add-Type -AssemblyName System.Windows.Forms; " +
|
|
92
|
+
"[System.Windows.Forms.Screen]::AllScreens | " +
|
|
93
|
+
'ForEach-Object { "$($_.Bounds.Width)x$($_.Bounds.Height)" }';
|
|
94
|
+
const out = run("powershell", ["-NoProfile", "-Command", script]);
|
|
95
|
+
if (!out)
|
|
96
|
+
return [];
|
|
97
|
+
const monitors = [];
|
|
98
|
+
for (const line of out.split("\n")) {
|
|
99
|
+
const match = line.trim().match(/^(\d+)x(\d+)$/);
|
|
100
|
+
if (!match)
|
|
101
|
+
continue;
|
|
102
|
+
monitors.push({
|
|
103
|
+
width: Number.parseInt(match[1], 10),
|
|
104
|
+
height: Number.parseInt(match[2], 10),
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
return monitors;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Size of the roomiest attached monitor in CSS pixels, or null when the display
|
|
111
|
+
* cannot be probed (no monitors, or enumeration failed).
|
|
112
|
+
*/
|
|
113
|
+
export function largestDisplay() {
|
|
114
|
+
let monitors;
|
|
115
|
+
try {
|
|
116
|
+
monitors = enumerateMonitors();
|
|
117
|
+
}
|
|
118
|
+
catch {
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
if (!monitors.length)
|
|
122
|
+
return null;
|
|
123
|
+
// max() keeps the FIRST of equally large monitors, as Python's max() does.
|
|
124
|
+
const monitor = monitors.reduce((prev, curr) => curr.width * curr.height > prev.width * prev.height ? curr : prev);
|
|
125
|
+
return {
|
|
126
|
+
width: Math.max(1, Math.trunc(monitor.width)),
|
|
127
|
+
height: Math.max(1, Math.trunc(monitor.height)),
|
|
128
|
+
};
|
|
129
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { Writable } from "node:stream";
|
|
2
|
+
/** GITHUB_TOKEN, as the Python twin reads it: once, at import. */
|
|
3
|
+
export declare const GITHUB_TOKEN: string | undefined;
|
|
4
|
+
/** Bearer auth for GitHub API calls only (never for asset downloads). */
|
|
5
|
+
export declare function githubHeaders(url: string): Record<string, string>;
|
|
6
|
+
/**
|
|
7
|
+
* fetch(), retried when the request fails without any response, and naming
|
|
8
|
+
* the URL and cause when it gives up. Node's own error is only "fetch failed",
|
|
9
|
+
* which says neither what was being downloaded nor why (DNS, refused, reset,
|
|
10
|
+
* timeout).
|
|
11
|
+
*
|
|
12
|
+
* Unlike requests.get(), which opens a connection per call, fetch() reuses
|
|
13
|
+
* pooled keep-alive connections, and a server may close one just as it is
|
|
14
|
+
* reused ("other side closed"). On GitHub's runners that failed a launcher's
|
|
15
|
+
* `fetch` right after the browser download, before any byte of the GeoIP
|
|
16
|
+
* request was answered. HTTP errors are responses, never retried.
|
|
17
|
+
*/
|
|
18
|
+
export declare function request(url: string, init: RequestInit): Promise<Response>;
|
|
19
|
+
/** requests' raise_for_status() message, so callers can match "404". */
|
|
20
|
+
export declare function raiseForStatus(response: Response, url: string): void;
|
|
21
|
+
/** click.style: ANSI-styled text, plain when stdout is not a colour terminal. */
|
|
22
|
+
export declare function style(text: string, { fg, bold, dim }?: {
|
|
23
|
+
fg?: string | null;
|
|
24
|
+
bold?: boolean;
|
|
25
|
+
dim?: boolean;
|
|
26
|
+
}): string;
|
|
27
|
+
/** Print a bold, optionally coloured message, as the Python twin's `rich` print. */
|
|
28
|
+
/** Print a bold, optionally coloured message, as the Python twin's `rich` print. */
|
|
29
|
+
export declare function rprint(msg: string, fg?: string, nl?: boolean): void;
|
|
30
|
+
export type ProgressCallback = (downloaded: number, total: number) => void;
|
|
31
|
+
/**
|
|
32
|
+
* Download a file from the given URL. Streams into `buffer` when one is given,
|
|
33
|
+
* otherwise accumulates and returns the bytes.
|
|
34
|
+
*
|
|
35
|
+
* An HTTP error raises requests' "<status> Client Error: ... for url: ..."
|
|
36
|
+
* message, like the Python twin; see request() for network failures.
|
|
37
|
+
*/
|
|
38
|
+
export declare function webdl(url: string, desc?: string, bar?: boolean, buffer?: Writable | null, { progressCallback }?: {
|
|
39
|
+
progressCallback?: ProgressCallback;
|
|
40
|
+
}): Promise<Buffer>;
|
|
41
|
+
/**
|
|
42
|
+
* Check a downloaded file against its expected sha256 digest.
|
|
43
|
+
*
|
|
44
|
+
* Takes the downloaded bytes or the path of the file they were written to.
|
|
45
|
+
* Raises CorruptedDownload on mismatch. Skips (with a warning) when no digest
|
|
46
|
+
* is known, so installs from sources that publish no digest still work.
|
|
47
|
+
*/
|
|
48
|
+
export declare function verifySha256(buffer: Buffer | Uint8Array | string, expected: string | null | undefined, desc?: string): void;
|
|
49
|
+
/**
|
|
50
|
+
* Extract a zip file to the given path.
|
|
51
|
+
*/
|
|
52
|
+
export declare function unzip(zipFile: Buffer | string, extractPath: string, desc?: string, bar?: boolean): void;
|
|
53
|
+
/**
|
|
54
|
+
* Move the contents of an archive's single top-level directory up.
|
|
55
|
+
*
|
|
56
|
+
* A release zip can hold one directory (camouwolf-<version>-<release>/); the
|
|
57
|
+
* launcher expects the binary and version.json directly in the install
|
|
58
|
+
* directory.
|
|
59
|
+
*/
|
|
60
|
+
export declare function flattenSingleRoot(extractPath: string): void;
|
package/dist/download.js
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Downloads, checksums and archives, for every launcher's browser and the
|
|
3
|
+
* GeoIP databases.
|
|
4
|
+
*
|
|
5
|
+
* TypeScript twin of python/src/camoucore/download.py.
|
|
6
|
+
*/
|
|
7
|
+
import { createHash } from "node:crypto";
|
|
8
|
+
import { once } from "node:events";
|
|
9
|
+
import * as fs from "node:fs";
|
|
10
|
+
import * as path from "node:path";
|
|
11
|
+
import AdmZip from "adm-zip";
|
|
12
|
+
import cliProgress from "cli-progress";
|
|
13
|
+
import prettyBytes from "pretty-bytes";
|
|
14
|
+
import { CorruptedDownload } from "./exceptions.js";
|
|
15
|
+
/** GITHUB_TOKEN, as the Python twin reads it: once, at import. */
|
|
16
|
+
export const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
|
|
17
|
+
/** Bearer auth for GitHub API calls only (never for asset downloads). */
|
|
18
|
+
export function githubHeaders(url) {
|
|
19
|
+
return url.includes("api.github") && GITHUB_TOKEN
|
|
20
|
+
? { Authorization: `Bearer ${GITHUB_TOKEN}` }
|
|
21
|
+
: {};
|
|
22
|
+
}
|
|
23
|
+
/** Delays before each retry of a request that failed without a response. */
|
|
24
|
+
const NETWORK_RETRY_DELAYS_MS = [1000, 2000];
|
|
25
|
+
/**
|
|
26
|
+
* fetch(), retried when the request fails without any response, and naming
|
|
27
|
+
* the URL and cause when it gives up. Node's own error is only "fetch failed",
|
|
28
|
+
* which says neither what was being downloaded nor why (DNS, refused, reset,
|
|
29
|
+
* timeout).
|
|
30
|
+
*
|
|
31
|
+
* Unlike requests.get(), which opens a connection per call, fetch() reuses
|
|
32
|
+
* pooled keep-alive connections, and a server may close one just as it is
|
|
33
|
+
* reused ("other side closed"). On GitHub's runners that failed a launcher's
|
|
34
|
+
* `fetch` right after the browser download, before any byte of the GeoIP
|
|
35
|
+
* request was answered. HTTP errors are responses, never retried.
|
|
36
|
+
*/
|
|
37
|
+
export async function request(url, init) {
|
|
38
|
+
for (let attempt = 0;; attempt++) {
|
|
39
|
+
try {
|
|
40
|
+
return await fetch(url, init);
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
if (attempt < NETWORK_RETRY_DELAYS_MS.length) {
|
|
44
|
+
await new Promise((resolve) => setTimeout(resolve, NETWORK_RETRY_DELAYS_MS[attempt]));
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
const cause = error
|
|
48
|
+
.cause;
|
|
49
|
+
const detail = cause
|
|
50
|
+
? `${cause.code ?? ""} ${cause.message ?? ""}`.trim()
|
|
51
|
+
: "";
|
|
52
|
+
throw new Error(`Could not download ${url}: ${error.message}${detail ? ` (${detail})` : ""}`, { cause: error });
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/** requests' raise_for_status() message, so callers can match "404". */
|
|
57
|
+
export function raiseForStatus(response, url) {
|
|
58
|
+
if (response.ok)
|
|
59
|
+
return;
|
|
60
|
+
const kind = response.status < 500 ? "Client Error" : "Server Error";
|
|
61
|
+
throw new Error(`${response.status} ${kind}: ${response.statusText} for url: ${url}`);
|
|
62
|
+
}
|
|
63
|
+
const ANSI = {
|
|
64
|
+
red: "31",
|
|
65
|
+
green: "32",
|
|
66
|
+
yellow: "33",
|
|
67
|
+
blue: "34",
|
|
68
|
+
cyan: "36",
|
|
69
|
+
bright_black: "90",
|
|
70
|
+
};
|
|
71
|
+
/** click.style: ANSI-styled text, plain when stdout is not a colour terminal. */
|
|
72
|
+
export function style(text, { fg, bold, dim } = {}) {
|
|
73
|
+
if (!process.stdout.isTTY || process.env.NO_COLOR)
|
|
74
|
+
return text;
|
|
75
|
+
const codes = [];
|
|
76
|
+
if (bold)
|
|
77
|
+
codes.push("1");
|
|
78
|
+
if (dim)
|
|
79
|
+
codes.push("2");
|
|
80
|
+
if (fg && ANSI[fg])
|
|
81
|
+
codes.push(ANSI[fg]);
|
|
82
|
+
return codes.length ? `\x1b[${codes.join(";")}m${text}\x1b[0m` : text;
|
|
83
|
+
}
|
|
84
|
+
/** Print a bold, optionally coloured message, as the Python twin's `rich` print. */
|
|
85
|
+
/** Print a bold, optionally coloured message, as the Python twin's `rich` print. */
|
|
86
|
+
export function rprint(msg, fg, nl = true) {
|
|
87
|
+
process.stdout.write(style(msg, { fg, bold: true }) + (nl ? "\n" : ""));
|
|
88
|
+
}
|
|
89
|
+
const formatBytes = (v, _, type) => type === "total" || type === "value" ? prettyBytes(v) : String(v);
|
|
90
|
+
/**
|
|
91
|
+
* Download a file from the given URL. Streams into `buffer` when one is given,
|
|
92
|
+
* otherwise accumulates and returns the bytes.
|
|
93
|
+
*
|
|
94
|
+
* An HTTP error raises requests' "<status> Client Error: ... for url: ..."
|
|
95
|
+
* message, like the Python twin; see request() for network failures.
|
|
96
|
+
*/
|
|
97
|
+
export async function webdl(url, desc, bar = true, buffer = null, { progressCallback } = {}) {
|
|
98
|
+
const response = await request(url, { headers: githubHeaders(url) });
|
|
99
|
+
raiseForStatus(response, url);
|
|
100
|
+
const totalSize = Number.parseInt(response.headers.get("content-length") || "0", 10);
|
|
101
|
+
let progressBar = null;
|
|
102
|
+
if (!progressCallback && bar) {
|
|
103
|
+
progressBar = new cliProgress.SingleBar({
|
|
104
|
+
format: `${desc || "Downloading"} [{bar}] {percentage}% | ETA: {eta_formatted} | {value}/{total}`,
|
|
105
|
+
formatValue: formatBytes,
|
|
106
|
+
noTTYOutput: true,
|
|
107
|
+
}, cliProgress.Presets.shades_classic);
|
|
108
|
+
progressBar.start(totalSize, 0);
|
|
109
|
+
}
|
|
110
|
+
const chunks = [];
|
|
111
|
+
let downloaded = 0;
|
|
112
|
+
let lastUpdate = 0;
|
|
113
|
+
try {
|
|
114
|
+
if (!response.body) {
|
|
115
|
+
throw new Error(`Response from ${url} had no body`);
|
|
116
|
+
}
|
|
117
|
+
for await (const chunk of response.body) {
|
|
118
|
+
if (buffer) {
|
|
119
|
+
// A stream that already failed (disk full) never drains: surface
|
|
120
|
+
// its error instead of waiting on it. When it is merely slow,
|
|
121
|
+
// wait for it, or the whole archive ends up queued in memory.
|
|
122
|
+
if (buffer.errored)
|
|
123
|
+
throw buffer.errored;
|
|
124
|
+
if (!buffer.write(chunk))
|
|
125
|
+
await once(buffer, "drain");
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
chunks.push(chunk);
|
|
129
|
+
}
|
|
130
|
+
downloaded += chunk.length;
|
|
131
|
+
if (progressCallback) {
|
|
132
|
+
if (downloaded - lastUpdate >= 65536 || downloaded === totalSize) {
|
|
133
|
+
progressCallback(downloaded, totalSize);
|
|
134
|
+
lastUpdate = downloaded;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
else if (progressBar) {
|
|
138
|
+
progressBar.increment(chunk.length);
|
|
139
|
+
}
|
|
140
|
+
else if (totalSize) {
|
|
141
|
+
const pct = (downloaded / totalSize) * 100;
|
|
142
|
+
process.stdout.write(`\r${desc}: ${pct.toFixed(0)}%`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
finally {
|
|
147
|
+
progressBar?.stop();
|
|
148
|
+
}
|
|
149
|
+
if (!progressCallback && !bar) {
|
|
150
|
+
process.stdout.write(desc ? `\r${desc}: Complete\n` : "\n");
|
|
151
|
+
}
|
|
152
|
+
return Buffer.concat(chunks);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Check a downloaded file against its expected sha256 digest.
|
|
156
|
+
*
|
|
157
|
+
* Takes the downloaded bytes or the path of the file they were written to.
|
|
158
|
+
* Raises CorruptedDownload on mismatch. Skips (with a warning) when no digest
|
|
159
|
+
* is known, so installs from sources that publish no digest still work.
|
|
160
|
+
*/
|
|
161
|
+
export function verifySha256(buffer, expected, desc = "asset") {
|
|
162
|
+
if (!expected) {
|
|
163
|
+
rprint(`Warning: no sha256 published for ${desc}; skipping verification.`, "yellow");
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
const digest = createHash("sha256");
|
|
167
|
+
if (typeof buffer === "string") {
|
|
168
|
+
const fd = fs.openSync(buffer, "r");
|
|
169
|
+
try {
|
|
170
|
+
const block = Buffer.alloc(1024 * 1024);
|
|
171
|
+
let read = fs.readSync(fd, block, 0, block.length, null);
|
|
172
|
+
while (read > 0) {
|
|
173
|
+
digest.update(block.subarray(0, read));
|
|
174
|
+
read = fs.readSync(fd, block, 0, block.length, null);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
finally {
|
|
178
|
+
fs.closeSync(fd);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
digest.update(buffer);
|
|
183
|
+
}
|
|
184
|
+
const actual = digest.digest("hex");
|
|
185
|
+
if (actual !== expected.toLowerCase()) {
|
|
186
|
+
throw new CorruptedDownload(`Checksum mismatch for ${desc}.\n` +
|
|
187
|
+
` expected sha256: ${expected.toLowerCase()}\n` +
|
|
188
|
+
` actual sha256: ${actual}\n` +
|
|
189
|
+
"The download was corrupted or tampered with. Installation aborted.");
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Extract a zip file to the given path.
|
|
194
|
+
*/
|
|
195
|
+
export function unzip(zipFile, extractPath, desc, bar = true) {
|
|
196
|
+
const zip = new AdmZip(zipFile);
|
|
197
|
+
const entries = zip.getEntries();
|
|
198
|
+
if (bar) {
|
|
199
|
+
rprint(desc || "Extracting");
|
|
200
|
+
for (const entry of entries) {
|
|
201
|
+
zip.extractEntryTo(entry, extractPath, true, true);
|
|
202
|
+
}
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
entries.forEach((entry, i) => {
|
|
206
|
+
zip.extractEntryTo(entry, extractPath, true, true);
|
|
207
|
+
if (desc) {
|
|
208
|
+
const pct = ((i + 1) / entries.length) * 100;
|
|
209
|
+
process.stdout.write(`\r${desc}: ${pct.toFixed(0)}%`);
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
if (desc)
|
|
213
|
+
process.stdout.write(`\r${desc}: Complete\n`);
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Move the contents of an archive's single top-level directory up.
|
|
217
|
+
*
|
|
218
|
+
* A release zip can hold one directory (camouwolf-<version>-<release>/); the
|
|
219
|
+
* launcher expects the binary and version.json directly in the install
|
|
220
|
+
* directory.
|
|
221
|
+
*/
|
|
222
|
+
export function flattenSingleRoot(extractPath) {
|
|
223
|
+
const entries = fs.readdirSync(extractPath);
|
|
224
|
+
if (entries.length !== 1)
|
|
225
|
+
return;
|
|
226
|
+
const inner = path.join(extractPath, entries[0]);
|
|
227
|
+
if (!fs.statSync(inner).isDirectory() || path.extname(inner) === ".app")
|
|
228
|
+
return;
|
|
229
|
+
for (const child of fs.readdirSync(inner)) {
|
|
230
|
+
fs.renameSync(path.join(inner, child), path.join(extractPath, child));
|
|
231
|
+
}
|
|
232
|
+
fs.rmdirSync(inner);
|
|
233
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TypeScript twin of python/src/camoucore/exceptions.py.
|
|
3
|
+
*/
|
|
4
|
+
export declare class CorruptedDownload extends Error {
|
|
5
|
+
constructor(message?: string);
|
|
6
|
+
}
|
|
7
|
+
export declare class UnsupportedOS extends Error {
|
|
8
|
+
constructor(message?: string);
|
|
9
|
+
}
|
|
10
|
+
export declare class LocaleError extends Error {
|
|
11
|
+
constructor(message?: string);
|
|
12
|
+
}
|
|
13
|
+
export declare class InvalidIP extends Error {
|
|
14
|
+
constructor(message?: string, options?: ErrorOptions);
|
|
15
|
+
}
|
|
16
|
+
export declare class InvalidProxy extends Error {
|
|
17
|
+
constructor(message?: string, options?: ErrorOptions);
|
|
18
|
+
}
|
|
19
|
+
export declare class UnknownIPLocation extends LocaleError {
|
|
20
|
+
constructor(message?: string);
|
|
21
|
+
}
|
|
22
|
+
export declare class InvalidLocale extends LocaleError {
|
|
23
|
+
constructor(message?: string);
|
|
24
|
+
static invalidInput(locale: string): InvalidLocale;
|
|
25
|
+
}
|
|
26
|
+
export declare class UnknownTerritory extends InvalidLocale {
|
|
27
|
+
constructor(message?: string);
|
|
28
|
+
}
|
|
29
|
+
export declare class UnknownLanguage extends InvalidLocale {
|
|
30
|
+
constructor(message?: string);
|
|
31
|
+
}
|
|
32
|
+
export declare class NotInstalledGeoIPExtra extends Error {
|
|
33
|
+
constructor(message?: string);
|
|
34
|
+
}
|
|
35
|
+
export declare class VirtualDisplayError extends Error {
|
|
36
|
+
constructor(message?: string);
|
|
37
|
+
}
|
|
38
|
+
export declare class CannotFindXvfb extends VirtualDisplayError {
|
|
39
|
+
constructor(message?: string);
|
|
40
|
+
}
|
|
41
|
+
export declare class CannotExecuteXvfb extends VirtualDisplayError {
|
|
42
|
+
constructor(message?: string);
|
|
43
|
+
}
|
|
44
|
+
export declare class VirtualDisplayNotSupported extends VirtualDisplayError {
|
|
45
|
+
constructor(message?: string);
|
|
46
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TypeScript twin of python/src/camoucore/exceptions.py.
|
|
3
|
+
*/
|
|
4
|
+
export class CorruptedDownload extends Error {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message ??
|
|
7
|
+
"A downloaded asset does not match its expected sha256 digest.");
|
|
8
|
+
this.name = "CorruptedDownload";
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export class UnsupportedOS extends Error {
|
|
12
|
+
constructor(message) {
|
|
13
|
+
super(message ?? "The OS is not supported.");
|
|
14
|
+
this.name = "UnsupportedOS";
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export class LocaleError extends Error {
|
|
18
|
+
constructor(message) {
|
|
19
|
+
super(message ?? "The locale is invalid.");
|
|
20
|
+
this.name = "LocaleError";
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export class InvalidIP extends Error {
|
|
24
|
+
constructor(message, options) {
|
|
25
|
+
super(message ?? "An IP address is invalid.", options);
|
|
26
|
+
this.name = "InvalidIP";
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export class InvalidProxy extends Error {
|
|
30
|
+
constructor(message, options) {
|
|
31
|
+
super(message ?? "A proxy is invalid.", options);
|
|
32
|
+
this.name = "InvalidProxy";
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
export class UnknownIPLocation extends LocaleError {
|
|
36
|
+
constructor(message) {
|
|
37
|
+
super(message ?? "The location of an IP is unknown.");
|
|
38
|
+
this.name = "UnknownIPLocation";
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
export class InvalidLocale extends LocaleError {
|
|
42
|
+
constructor(message) {
|
|
43
|
+
super(message ?? "The locale input is invalid.");
|
|
44
|
+
this.name = "InvalidLocale";
|
|
45
|
+
}
|
|
46
|
+
static invalidInput(locale) {
|
|
47
|
+
return new InvalidLocale(`Invalid locale: '${locale}'. Must be either a region, language, language-region, or language-script-region.`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
export class UnknownTerritory extends InvalidLocale {
|
|
51
|
+
constructor(message) {
|
|
52
|
+
super(message ?? "The territory is unknown.");
|
|
53
|
+
this.name = "UnknownTerritory";
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
export class UnknownLanguage extends InvalidLocale {
|
|
57
|
+
constructor(message) {
|
|
58
|
+
super(message ?? "The language is unknown.");
|
|
59
|
+
this.name = "UnknownLanguage";
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
export class NotInstalledGeoIPExtra extends Error {
|
|
63
|
+
constructor(message) {
|
|
64
|
+
super(message ?? "The GeoIP database reader is not available.");
|
|
65
|
+
this.name = "NotInstalledGeoIPExtra";
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
export class VirtualDisplayError extends Error {
|
|
69
|
+
constructor(message) {
|
|
70
|
+
super(message ?? "There is an error with the virtual display.");
|
|
71
|
+
this.name = "VirtualDisplayError";
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
export class CannotFindXvfb extends VirtualDisplayError {
|
|
75
|
+
constructor(message) {
|
|
76
|
+
super(message ?? "Xvfb cannot be found.");
|
|
77
|
+
this.name = "CannotFindXvfb";
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
export class CannotExecuteXvfb extends VirtualDisplayError {
|
|
81
|
+
constructor(message) {
|
|
82
|
+
super(message ?? "Xvfb cannot be executed.");
|
|
83
|
+
this.name = "CannotExecuteXvfb";
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
export class VirtualDisplayNotSupported extends VirtualDisplayError {
|
|
87
|
+
constructor(message) {
|
|
88
|
+
super(message ?? "The user tried to use a virtual display on a non-Linux OS.");
|
|
89
|
+
this.name = "VirtualDisplayNotSupported";
|
|
90
|
+
}
|
|
91
|
+
}
|