Skip to content

Commit 654f4e3

Browse files
authored
Feat/#28 Binary version (#29)
* chore: upgrade turbo * feat: handle specific binary version * docs(changeset): - Feat/#28 Specific binary installation
1 parent bf8012d commit 654f4e3

9 files changed

Lines changed: 141 additions & 119 deletions

File tree

.changeset/shaky-times-pick.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@lightpanda/browser": minor
3+
---
4+
5+
- Feat/#28 Specific binary installation

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"devDependencies": {
2727
"@changesets/changelog-github": "^0.5.2",
2828
"@changesets/cli": "^2.29.8",
29-
"turbo": "^2.8.5"
29+
"turbo": "^2.10.12"
3030
},
3131
"engines": {
3232
"node": "22.16",

packages/browser/cli/main.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@ yargs(hideBin(process.argv))
3030
},
3131
)
3232
.command(
33-
'install',
33+
'install [lp_version]',
3434
'Download the browser to the latest nightly version',
35-
() => {},
36-
_ => {
37-
download()
35+
yargs =>
36+
yargs.positional('lp_version', {
37+
describe: 'Version of the binary',
38+
}),
39+
argv => {
40+
download(argv.lp_version as string)
3841
},
3942
)
4043
.command(

packages/browser/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@
4848
"dependencies": {
4949
"yargs": "^18.0.0"
5050
}
51-
}
51+
}

packages/browser/src/download.ts

Lines changed: 26 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import { arch, exit, platform } from 'node:process'
1919
import {
2020
DEFAULT_CACHE_FOLDER,
2121
DEFAULT_EXECUTABLE_PATH,
22-
GITHUB_RELEASE_DATA_URL,
2322
USER_EXECUTABLE_PATH,
2423
checksumFile,
24+
getBinaryAttributes,
2525
} from './utils.js'
2626

2727
type GH_ASSET = {
@@ -46,12 +46,17 @@ const CURRENT_ARCH = arch as 'arm64' | 'x64'
4646
* Download Lightpanda's binary
4747
* @returns {Promise<void>}
4848
*/
49-
export const download = async (): Promise<void> => {
49+
export const download = async (version: 'nightly' | string = 'nightly'): Promise<void> => {
5050
if (!['linux', 'darwin'].includes(platform)) {
5151
throw new Error('Architecture or platform is not compatible with Lightpanda')
5252
}
5353

54-
const platformPath = PLATFORMS?.[CURRENT_PLATFORM]?.[CURRENT_ARCH]
54+
const platformArch = PLATFORMS?.[CURRENT_PLATFORM]?.[CURRENT_ARCH]
55+
if (!platformArch) {
56+
console.warn("Lightpanda package doesn't ship with prebuilt binaries for your platform yet. ")
57+
exit(1)
58+
}
59+
const binaryAttributes = await getBinaryAttributes(version, platformArch)
5560

5661
if (!existsSync(DEFAULT_CACHE_FOLDER)) {
5762
mkdirSync(DEFAULT_CACHE_FOLDER, { recursive: true })
@@ -83,56 +88,30 @@ export const download = async (): Promise<void> => {
8388
return new Promise((resolve, reject) => get(url, resolve, reject))
8489
}
8590

86-
const getGithubHash = async (path: string) => {
87-
try {
88-
const f = await fetch(path)
89-
const data = await f.json()
90-
91-
const asset: GH_ASSET = data.assets.find(
92-
(a: GH_ASSET) => a.name === `lightpanda-${platformPath}`,
93-
)
94-
95-
if (asset) {
96-
return asset.digest
97-
}
98-
99-
return ''
100-
} catch (e: any) {
101-
throw new Error(e)
102-
}
91+
if (USER_EXECUTABLE_PATH) {
92+
console.info('$LIGHTPANDA_EXECUTABLE_PATH found, skipping binary download…')
93+
exit(0)
10394
}
10495

105-
if (platformPath) {
106-
if (USER_EXECUTABLE_PATH) {
107-
console.info('$LIGHTPANDA_EXECUTABLE_PATH found, skipping binary download…')
108-
exit(0)
109-
}
110-
111-
try {
112-
console.info('⏳ Downloading latest version of Lightpanda browser…', '\n')
113-
await downloadBinary(
114-
`https://github.com/lightpanda-io/browser/releases/download/nightly/lightpanda-${platformPath}`,
115-
)
96+
try {
97+
console.info(`⏳ Downloading version ${binaryAttributes.version} of Lightpanda browser…`, '\n')
98+
await downloadBinary(binaryAttributes.url)
11699

117-
console.info('🔐 Getting and comparing checksums…', '\n')
118-
const ghChecksum = await getGithubHash(GITHUB_RELEASE_DATA_URL)
119-
const lpChecksum = await checksumFile(DEFAULT_EXECUTABLE_PATH)
100+
console.info('🔐 Getting and comparing checksums…', '\n')
101+
const ghChecksum = binaryAttributes.checksum
102+
const lpChecksum = await checksumFile(DEFAULT_EXECUTABLE_PATH)
120103

121-
if (ghChecksum !== lpChecksum) {
122-
throw new Error("🚫 Checksums don't match!")
123-
}
104+
if (ghChecksum !== lpChecksum) {
105+
throw new Error("🚫 Checksums don't match!")
106+
}
124107

125-
chmodSync(DEFAULT_EXECUTABLE_PATH, constants.S_IRWXU)
108+
chmodSync(DEFAULT_EXECUTABLE_PATH, constants.S_IRWXU)
126109

127-
console.info('✅ Done!')
128-
exit(0)
129-
} catch (e) {
130-
console.log('error', e)
131-
console.warn(`Lightpanda's failed to download the binary file "${platformPath}".`)
132-
exit(1)
133-
}
134-
} else {
135-
console.warn("Lightpanda package doesn't ship with prebuilt binaries for your platform yet. ")
110+
console.info('✅ Done!')
111+
exit(0)
112+
} catch (e) {
113+
console.log(e)
114+
console.warn(`Lightpanda's failed to download the binary file for "${platformArch}".`)
136115
exit(1)
137116
}
138117
}

packages/browser/src/types.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
type Semver = `${number}.${number}.${number}`
2+
3+
type VersionArchItemType = {
4+
download_url: string
5+
shasum: string
6+
size: string
7+
}
8+
9+
type VersionArchType = Record<string, VersionArchItemType> & {
10+
date: string
11+
version: Semver
12+
}
13+
14+
export type VersionType = Record<Semver | 'nightly', VersionArchType>

packages/browser/src/utils.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
import crypto, { type BinaryToTextEncoding } from 'node:crypto'
1818
import fs from 'node:fs'
1919
import os from 'node:os'
20+
import { exit } from 'node:process'
21+
import type { VersionType } from './types.js'
2022

2123
export const DEFAULT_CACHE_FOLDER = `${os.homedir()}/.cache/lightpanda-node`
2224
export const BINARY_NAME = 'lightpanda'
2325

2426
export const USER_EXECUTABLE_PATH = process.env.LIGHTPANDA_EXECUTABLE_PATH
2527
export const DEFAULT_EXECUTABLE_PATH = `${DEFAULT_CACHE_FOLDER}/${BINARY_NAME}`
26-
27-
export const GITHUB_RELEASE_DATA_URL =
28-
'https://api.github.com/repos/lightpanda-io/browser/releases/tags/nightly'
28+
export const VERSIONS_PATH = 'https://get.lightpanda.io/versions.json'
2929

3030
/**
3131
* Validate a URL structure
@@ -70,18 +70,39 @@ export const getExecutablePath = () => {
7070
return USER_EXECUTABLE_PATH
7171
}
7272

73-
throw process.emitWarning(
73+
console.warn(
7474
'⚠️ Lightpanda binary not found, please check your $LIGHTPANDA_EXECUTABLE_PATH environment variable.',
7575
)
76+
exit(1)
7677
}
7778

7879
if (fs.existsSync(DEFAULT_EXECUTABLE_PATH)) {
7980
return DEFAULT_EXECUTABLE_PATH
8081
}
8182

82-
throw process.emitWarning(
83-
'⚠️ Lightpanda binary not installed, please run `npx @lightpanda/browser install`',
83+
console.warn(
84+
'⚠️ Lightpanda binary not installed, please run `npx @lightpanda/browser install <version>`',
8485
)
86+
exit(1)
87+
}
88+
89+
/**
90+
* Get binary download path
91+
*/
92+
export const getBinaryAttributes = async (v: 'nightly' | string, platformArch: string) => {
93+
const f = await fetch(VERSIONS_PATH)
94+
const versionsList: VersionType = (await f.json()) as VersionType
95+
96+
if (versionsList?.[v]?.[platformArch]) {
97+
return {
98+
url: versionsList[v][platformArch].download_url,
99+
checksum: `sha256:${versionsList[v][platformArch].shasum}`,
100+
version: versionsList[v].version,
101+
}
102+
}
103+
104+
console.warn(`\x1b[33m⚠️ Provided version \`${v}\` not found\x1b[0m`)
105+
exit(1)
85106
}
86107

87108
/**

turbo.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"$schema": "https://turbo.build/schema.json",
2+
"$schema": "https://v2-10-12.turborepo.dev/schema.json",
33
"tasks": {
44
"build": {
55
"dependsOn": ["^build"],

yarn.lock

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ __metadata:
644644
dependencies:
645645
"@changesets/changelog-github": "npm:^0.5.2"
646646
"@changesets/cli": "npm:^2.29.8"
647-
turbo: "npm:^2.8.5"
647+
turbo: "npm:^2.10.12"
648648
languageName: unknown
649649
linkType: soft
650650

@@ -870,6 +870,48 @@ __metadata:
870870
languageName: node
871871
linkType: hard
872872

873+
"@turbo/darwin-64@npm:2.10.12":
874+
version: 2.10.12
875+
resolution: "@turbo/darwin-64@npm:2.10.12"
876+
conditions: os=darwin & cpu=x64
877+
languageName: node
878+
linkType: hard
879+
880+
"@turbo/darwin-arm64@npm:2.10.12":
881+
version: 2.10.12
882+
resolution: "@turbo/darwin-arm64@npm:2.10.12"
883+
conditions: os=darwin & cpu=arm64
884+
languageName: node
885+
linkType: hard
886+
887+
"@turbo/linux-64@npm:2.10.12":
888+
version: 2.10.12
889+
resolution: "@turbo/linux-64@npm:2.10.12"
890+
conditions: (os=android | os=linux) & cpu=x64
891+
languageName: node
892+
linkType: hard
893+
894+
"@turbo/linux-arm64@npm:2.10.12":
895+
version: 2.10.12
896+
resolution: "@turbo/linux-arm64@npm:2.10.12"
897+
conditions: (os=android | os=linux) & cpu=arm64
898+
languageName: node
899+
linkType: hard
900+
901+
"@turbo/windows-64@npm:2.10.12":
902+
version: 2.10.12
903+
resolution: "@turbo/windows-64@npm:2.10.12"
904+
conditions: os=win32 & cpu=x64
905+
languageName: node
906+
linkType: hard
907+
908+
"@turbo/windows-arm64@npm:2.10.12":
909+
version: 2.10.12
910+
resolution: "@turbo/windows-arm64@npm:2.10.12"
911+
conditions: os=win32 & cpu=arm64
912+
languageName: node
913+
linkType: hard
914+
873915
"@types/estree@npm:1.0.8":
874916
version: 1.0.8
875917
resolution: "@types/estree@npm:1.0.8"
@@ -2736,74 +2778,32 @@ __metadata:
27362778
languageName: node
27372779
linkType: hard
27382780

2739-
"turbo-darwin-64@npm:2.8.5":
2740-
version: 2.8.5
2741-
resolution: "turbo-darwin-64@npm:2.8.5"
2742-
conditions: os=darwin & cpu=x64
2743-
languageName: node
2744-
linkType: hard
2745-
2746-
"turbo-darwin-arm64@npm:2.8.5":
2747-
version: 2.8.5
2748-
resolution: "turbo-darwin-arm64@npm:2.8.5"
2749-
conditions: os=darwin & cpu=arm64
2750-
languageName: node
2751-
linkType: hard
2752-
2753-
"turbo-linux-64@npm:2.8.5":
2754-
version: 2.8.5
2755-
resolution: "turbo-linux-64@npm:2.8.5"
2756-
conditions: os=linux & cpu=x64
2757-
languageName: node
2758-
linkType: hard
2759-
2760-
"turbo-linux-arm64@npm:2.8.5":
2761-
version: 2.8.5
2762-
resolution: "turbo-linux-arm64@npm:2.8.5"
2763-
conditions: os=linux & cpu=arm64
2764-
languageName: node
2765-
linkType: hard
2766-
2767-
"turbo-windows-64@npm:2.8.5":
2768-
version: 2.8.5
2769-
resolution: "turbo-windows-64@npm:2.8.5"
2770-
conditions: os=win32 & cpu=x64
2771-
languageName: node
2772-
linkType: hard
2773-
2774-
"turbo-windows-arm64@npm:2.8.5":
2775-
version: 2.8.5
2776-
resolution: "turbo-windows-arm64@npm:2.8.5"
2777-
conditions: os=win32 & cpu=arm64
2778-
languageName: node
2779-
linkType: hard
2780-
2781-
"turbo@npm:^2.8.5":
2782-
version: 2.8.5
2783-
resolution: "turbo@npm:2.8.5"
2781+
"turbo@npm:^2.10.12":
2782+
version: 2.10.12
2783+
resolution: "turbo@npm:2.10.12"
27842784
dependencies:
2785-
turbo-darwin-64: "npm:2.8.5"
2786-
turbo-darwin-arm64: "npm:2.8.5"
2787-
turbo-linux-64: "npm:2.8.5"
2788-
turbo-linux-arm64: "npm:2.8.5"
2789-
turbo-windows-64: "npm:2.8.5"
2790-
turbo-windows-arm64: "npm:2.8.5"
2785+
"@turbo/darwin-64": "npm:2.10.12"
2786+
"@turbo/darwin-arm64": "npm:2.10.12"
2787+
"@turbo/linux-64": "npm:2.10.12"
2788+
"@turbo/linux-arm64": "npm:2.10.12"
2789+
"@turbo/windows-64": "npm:2.10.12"
2790+
"@turbo/windows-arm64": "npm:2.10.12"
27912791
dependenciesMeta:
2792-
turbo-darwin-64:
2792+
"@turbo/darwin-64":
27932793
optional: true
2794-
turbo-darwin-arm64:
2794+
"@turbo/darwin-arm64":
27952795
optional: true
2796-
turbo-linux-64:
2796+
"@turbo/linux-64":
27972797
optional: true
2798-
turbo-linux-arm64:
2798+
"@turbo/linux-arm64":
27992799
optional: true
2800-
turbo-windows-64:
2800+
"@turbo/windows-64":
28012801
optional: true
2802-
turbo-windows-arm64:
2802+
"@turbo/windows-arm64":
28032803
optional: true
28042804
bin:
28052805
turbo: bin/turbo
2806-
checksum: 10c0/6eaae92af681da416d29943da65c2a8b38eaf314d2125d134cd548fb832c5191ccdfc66e56fb272cc173a28dfc6674f0c0666d823366d505ae3f26b64d821173
2806+
checksum: 10c0/bc912180b8c00f88be0672223c6cf8b61e05f113a93574d3bcb7343f9391ee03c115afc4468c76c4dfcbd9ee4a6564b025762dad9aefe53a91e02ff62613d9be
28072807
languageName: node
28082808
linkType: hard
28092809

0 commit comments

Comments
 (0)