camoufox-js 0.4.1 → 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/dist/pkgman.d.ts CHANGED
@@ -26,7 +26,9 @@ export declare class GitHubDownloader {
26
26
  constructor(githubRepo: string);
27
27
  checkAsset(asset: any): any;
28
28
  missingAssetError(): void;
29
- getAsset(): Promise<any>;
29
+ getAsset({ retries }?: {
30
+ retries: number;
31
+ }): Promise<any>;
30
32
  }
31
33
  export declare class CamoufoxFetcher extends GitHubDownloader {
32
34
  arch: string;
@@ -40,7 +42,7 @@ export declare class CamoufoxFetcher extends GitHubDownloader {
40
42
  static getPlatformArch(): string;
41
43
  fetchLatest(): Promise<void>;
42
44
  static downloadFile(url: string): Promise<Buffer>;
43
- extractZip(zipFile: Buffer): Promise<void>;
45
+ extractZip(zipFile: string | Buffer): Promise<void>;
44
46
  static cleanup(): boolean;
45
47
  setVersion(): void;
46
48
  install(): Promise<void>;
@@ -53,7 +55,9 @@ export declare function installedVerStr(): string;
53
55
  export declare function camoufoxPath(downloadIfMissing?: boolean): PathLike;
54
56
  export declare function getPath(file: string): string;
55
57
  export declare function launchPath(): string;
56
- export declare function webdl(url: string, desc?: string, bar?: boolean, buffer?: Writable | null): Promise<Buffer>;
58
+ export declare function webdl(url: string, desc?: string, bar?: boolean, buffer?: Writable | null, { retries }?: {
59
+ retries: number;
60
+ }): Promise<Buffer>;
57
61
  export declare function unzip(zipFile: Buffer, extractPath: string, desc?: string, bar?: boolean): Promise<void>;
58
62
  export declare function loadYaml(file: string): {
59
63
  [key: string]: any;
package/dist/pkgman.js CHANGED
@@ -7,6 +7,7 @@ import { CamoufoxNotInstalled, FileNotFoundError, MissingRelease, UnsupportedArc
7
7
  import AdmZip from 'adm-zip';
8
8
  import * as yaml from 'js-yaml';
9
9
  import ProgressBar from 'progress';
10
+ import { setTimeout } from 'timers/promises';
10
11
  const ARCH_MAP = {
11
12
  'x64': 'x86_64',
12
13
  'ia32': 'i686',
@@ -97,12 +98,25 @@ export class GitHubDownloader {
97
98
  missingAssetError() {
98
99
  throw new MissingRelease(`Could not find a release asset in ${this.githubRepo}.`);
99
100
  }
100
- async getAsset() {
101
- const resp = await fetch(this.apiUrl);
102
- if (!resp.ok) {
103
- throw new Error(`Failed to fetch releases from ${this.apiUrl}`);
101
+ async getAsset({ retries } = { retries: 5 }) {
102
+ let attempts = 0;
103
+ let response;
104
+ while (attempts < retries) {
105
+ try {
106
+ response = await fetch(this.apiUrl);
107
+ if (response.ok)
108
+ break;
109
+ }
110
+ catch (e) {
111
+ console.error(e, `retrying (${attempts + 1}/${retries})...`);
112
+ await setTimeout(5e3);
113
+ }
114
+ attempts++;
104
115
  }
105
- const releases = await resp.json();
116
+ if (!response || !response.ok) {
117
+ throw new Error(`Failed to fetch releases from ${this.apiUrl} after ${retries} attempts`);
118
+ }
119
+ const releases = await response.json();
106
120
  for (const release of releases) {
107
121
  for (const asset of release.assets) {
108
122
  const data = this.checkAsset(asset);
@@ -180,8 +194,12 @@ export class CamoufoxFetcher extends GitHubDownloader {
180
194
  await CamoufoxFetcher.cleanup();
181
195
  try {
182
196
  fs.mkdirSync(INSTALL_DIR, { recursive: true });
183
- const zipFile = await webdl(this.url, 'Downloading Camoufox...', true);
184
- await this.extractZip(zipFile);
197
+ const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'camoufox-'));
198
+ const tempFilePath = path.join(tempDir, 'camoufox.zip');
199
+ const tempFileStream = fs.createWriteStream(tempFilePath);
200
+ await webdl(this.url, 'Downloading Camoufox...', true, tempFileStream);
201
+ await new Promise(r => tempFileStream.close(r));
202
+ await this.extractZip(tempFilePath);
185
203
  this.setVersion();
186
204
  if (OS_NAME !== 'win') {
187
205
  execSync(`chmod -R 755 ${INSTALL_DIR}`);
@@ -266,10 +284,23 @@ export function launchPath() {
266
284
  }
267
285
  return launchPath;
268
286
  }
269
- export async function webdl(url, desc = '', bar = true, buffer = null) {
270
- const response = await fetch(url);
271
- if (!response.ok) {
272
- throw new Error(`Failed to download file from ${url}`);
287
+ export async function webdl(url, desc = '', bar = true, buffer = null, { retries } = { retries: 5 }) {
288
+ let attempts = 0;
289
+ let response;
290
+ while (attempts < retries) {
291
+ try {
292
+ response = await fetch(url);
293
+ if (response.ok)
294
+ break;
295
+ }
296
+ catch (e) {
297
+ console.error(e, `retrying (${attempts + 1}/${retries})...`);
298
+ await setTimeout(5e3);
299
+ }
300
+ attempts++;
301
+ }
302
+ if (!response || !response.ok) {
303
+ throw new Error(`Failed to download from ${url} after ${retries} attempts`);
273
304
  }
274
305
  const totalSize = parseInt(response.headers.get('content-length') || '0', 10);
275
306
  const progressBar = bar ? new ProgressBar(`${desc} [:bar] :percent :etas`, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "camoufox-js",
3
- "version": "0.4.1",
3
+ "version": "0.6.1",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {