@@ -3,79 +3,89 @@ import path from "node:path";
33import https from "node:https" ;
44import { execSync } from "node:child_process" ;
55
6+ const RELEASE_BASE = "https://github.com/cloudflare/cloudflared/releases/" ;
7+
68const LINUX_URL : Partial < Record < typeof process . arch , string > > = {
7- arm64 : "https://github.com/cloudflare/cloudflared/releases/latest/download/ cloudflared-linux-arm64" ,
8- arm : "https://github.com/cloudflare/cloudflared/releases/latest/download/ cloudflared-linux-arm" ,
9- x64 : "https://github.com/cloudflare/cloudflared/releases/latest/download/ cloudflared-linux-amd64" ,
10- ia32 : "https://github.com/cloudflare/cloudflared/releases/latest/download/ cloudflared-linux-386" ,
9+ arm64 : "cloudflared-linux-arm64" ,
10+ arm : "cloudflared-linux-arm" ,
11+ x64 : "cloudflared-linux-amd64" ,
12+ ia32 : "cloudflared-linux-386" ,
1113} ;
1214
1315const MACOS_URL : Partial < Record < typeof process . arch , string > > = {
14- arm64 : "https://github.com/cloudflare/cloudflared/releases/latest/download/ cloudflared-darwin-amd64.tgz" ,
15- x64 : "https://github.com/cloudflare/cloudflared/releases/latest/download/ cloudflared-darwin-amd64.tgz" ,
16+ arm64 : "cloudflared-darwin-amd64.tgz" ,
17+ x64 : "cloudflared-darwin-amd64.tgz" ,
1618} ;
1719
1820const WINDOWS_URL : Partial < Record < typeof process . arch , string > > = {
19- x64 : "https://github.com/cloudflare/cloudflared/releases/latest/download/ cloudflared-windows-amd64.exe" ,
20- ia32 : "https://github.com/cloudflare/cloudflared/releases/latest/download/ cloudflared-windows-386.exe" ,
21+ x64 : "cloudflared-windows-amd64.exe" ,
22+ ia32 : "cloudflared-windows-386.exe" ,
2123} ;
2224
25+ function resolve_base ( version : string ) : string {
26+ if ( version === "latest" ) {
27+ return `${ RELEASE_BASE } latest/download/` ;
28+ }
29+ return `${ RELEASE_BASE } download/${ version } /` ;
30+ }
31+
2332/**
2433 * Install cloudflared to the given path.
2534 * @param to The path to the binary to install.
35+ * @param version The version of cloudflared to install.
2636 * @returns The path to the binary that was installed.
2737 */
28- export async function install ( to : string ) : Promise < string > {
38+ export async function install ( to : string , version = "latest" ) : Promise < string > {
2939 if ( process . platform === "linux" ) {
30- return install_linux ( to ) ;
40+ return install_linux ( to , version ) ;
3141 } else if ( process . platform === "darwin" ) {
32- return install_macos ( to ) ;
42+ return install_macos ( to , version ) ;
3343 } else if ( process . platform === "win32" ) {
34- return install_windows ( to ) ;
44+ return install_windows ( to , version ) ;
3545 } else {
3646 console . error ( "Unsupported platform: " + process . platform ) ;
3747 process . exit ( 1 ) ;
3848 }
3949}
4050
41- export async function install_linux ( to : string ) : Promise < string > {
42- const url = LINUX_URL [ process . arch ] ;
51+ export async function install_linux ( to : string , version = "latest" ) : Promise < string > {
52+ const file = LINUX_URL [ process . arch ] ;
4353
44- if ( url === undefined ) {
54+ if ( file === undefined ) {
4555 console . error ( "Unsupported architecture: " + process . arch ) ;
4656 process . exit ( 1 ) ;
4757 }
4858
49- await download ( url , to ) ;
59+ await download ( resolve_base ( version ) + file , to ) ;
5060 await new Promise ( ( r ) => setTimeout ( r , 100 ) ) ;
5161 fs . chmodSync ( to , "755" ) ;
5262 return to ;
5363}
5464
55- export async function install_macos ( to : string ) : Promise < string > {
56- const url = MACOS_URL [ process . arch ] ;
65+ export async function install_macos ( to : string , version = "latest" ) : Promise < string > {
66+ const file = MACOS_URL [ process . arch ] ;
5767
58- if ( url === undefined ) {
68+ if ( file === undefined ) {
5969 console . error ( "Unsupported architecture: " + process . arch ) ;
6070 process . exit ( 1 ) ;
6171 }
6272
63- await download ( url , `${ to } .tgz` ) ;
73+ await download ( resolve_base ( version ) + file , `${ to } .tgz` ) ;
6474 process . env . VERBOSE && console . log ( `Extracting to ${ to } ` ) ;
6575 execSync ( `tar -xzf ${ path . basename ( `${ to } .tgz` ) } ` , { cwd : path . dirname ( to ) } ) ;
6676 fs . unlinkSync ( `${ to } .tgz` ) ;
6777 fs . renameSync ( `${ path . dirname ( to ) } /cloudflared` , to ) ;
6878 return to ;
6979}
70- export async function install_windows ( to : string ) : Promise < string > {
71- const url = WINDOWS_URL [ process . arch ] ;
80+ export async function install_windows ( to : string , version = "latest" ) : Promise < string > {
81+ const file = WINDOWS_URL [ process . arch ] ;
7282
73- if ( url === undefined ) {
83+ if ( file === undefined ) {
7484 console . error ( "Unsupported architecture: " + process . arch ) ;
7585 process . exit ( 1 ) ;
7686 }
7787
78- await download ( url , to ) ;
88+ await download ( resolve_base ( version ) + file , to ) ;
7989 return to ;
8090}
8191
0 commit comments