-
Notifications
You must be signed in to change notification settings - Fork 40
/
utils.ts
66 lines (57 loc) · 1.78 KB
/
utils.ts
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
import os from 'node:os'
import util from 'node:util'
import fs from 'node:fs/promises'
import decamelize from 'decamelize'
import fetch, { type RequestInit } from 'node-fetch'
import { GECKODRIVER_DOWNLOAD_PATH } from './constants.js'
import type { GeckodriverParameters } from './types.js'
const RETRY_DELAY = 100
export async function hasAccess(filePath: string) {
return fs.access(filePath).then(() => true, () => false)
}
export function getDownloadUrl(version: string) {
const platformIdentifier = os.platform() === 'win32'
? 'win'
: os.platform() === 'darwin'
? 'macos'
: 'linux'
const arch = os.arch() === 'arm64'
? '-aarch64'
: platformIdentifier === 'macos'
? ''
: os.arch() === 'x64'
? '64'
: '32'
const ext = os.platform() === 'win32' ? '.zip' : '.tar.gz'
return util.format(GECKODRIVER_DOWNLOAD_PATH, version, version, platformIdentifier, arch, ext)
}
const EXCLUDED_PARAMS = ['version', 'help']
export function parseParams(params: GeckodriverParameters) {
return Object.entries(params)
.filter(([key,]) => !EXCLUDED_PARAMS.includes(key))
.map(([key, val]) => {
if (typeof val === 'boolean' && !val) {
return ''
}
const values = Array.isArray(val) ? val : [val]
return values.map((v) => `--${decamelize(key, { separator: '-' })}${typeof v === 'boolean' ? '' : `=${v}`}`)
})
.flat()
.filter(Boolean)
}
export async function retryFetch(url: string, opts: RequestInit, retry: number = 3) {
while (retry > 0) {
try {
return await fetch(url, opts)
} catch (e) {
retry = retry - 1
if (retry === 0) {
throw e
}
await sleep(RETRY_DELAY)
}
}
}
function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms))
}