|
16 | 16 | import { constants, chmodSync, createWriteStream, existsSync, mkdirSync } from 'node:fs' |
17 | 17 | import https from 'node:https' |
18 | 18 | import { arch, exit, platform } from 'node:process' |
19 | | -import { DEFAULT_CACHE_FOLDER, DEFAULT_EXECUTABLE_PATH, USER_EXECUTABLE_PATH } from './utils' |
| 19 | +import { |
| 20 | + DEFAULT_CACHE_FOLDER, |
| 21 | + DEFAULT_EXECUTABLE_PATH, |
| 22 | + GITHUB_RELEASE_DATA_URL, |
| 23 | + USER_EXECUTABLE_PATH, |
| 24 | + checksumFile, |
| 25 | +} from './utils' |
| 26 | + |
| 27 | +type GH_ASSET = { |
| 28 | + name: string |
| 29 | + digest: string |
| 30 | +} |
20 | 31 |
|
21 | 32 | const PLATFORMS = { |
22 | 33 | darwin: { |
@@ -66,18 +77,45 @@ export const download = async (): Promise<void> => { |
66 | 77 | return new Promise((resolve, reject) => get(url, resolve, reject)) |
67 | 78 | } |
68 | 79 |
|
| 80 | + const getGithubHash = async (path: string) => { |
| 81 | + try { |
| 82 | + const f = await fetch(path) |
| 83 | + const data = await f.json() |
| 84 | + |
| 85 | + const asset: GH_ASSET = data.assets.find( |
| 86 | + (a: GH_ASSET) => a.name === `lightpanda-${platformPath}`, |
| 87 | + ) |
| 88 | + |
| 89 | + if (asset) { |
| 90 | + return asset.digest |
| 91 | + } |
| 92 | + |
| 93 | + return '' |
| 94 | + } catch (e) { |
| 95 | + throw new Error(e) |
| 96 | + } |
| 97 | + } |
| 98 | + |
69 | 99 | if (platformPath) { |
70 | 100 | if (USER_EXECUTABLE_PATH) { |
71 | 101 | console.info('$LIGHTPANDA_EXECUTABLE_PATH found, skipping binary download…') |
72 | 102 | exit(0) |
73 | 103 | } |
74 | 104 |
|
75 | 105 | try { |
76 | | - console.info('⏳ Downloading latest version of Lightpanda browser…') |
77 | | - |
| 106 | + console.info('⏳ Downloading latest version of Lightpanda browser…', '\n') |
78 | 107 | await downloadBinary( |
79 | 108 | `https://github.com/lightpanda-io/browser/releases/download/nightly/lightpanda-${platformPath}`, |
80 | 109 | ) |
| 110 | + |
| 111 | + console.info('🔐 Getting and comparing checksums…', '\n') |
| 112 | + const ghChecksum = await getGithubHash(GITHUB_RELEASE_DATA_URL) |
| 113 | + const lpChecksum = await checksumFile(DEFAULT_EXECUTABLE_PATH) |
| 114 | + |
| 115 | + if (ghChecksum !== lpChecksum) { |
| 116 | + throw new Error("🚫 Checksums don't match!") |
| 117 | + } |
| 118 | + |
81 | 119 | chmodSync(DEFAULT_EXECUTABLE_PATH, constants.S_IRWXU) |
82 | 120 |
|
83 | 121 | console.info('✅ Done!') |
|
0 commit comments