@@ -7,11 +7,29 @@ import * as unzipper from 'unzipper';
77import PJ from "./package.json" ;
88
99interface BinaryConfig {
10- arch: 'arm64' | 'x64' ;
11- platform: 'darwin' | 'linux' | 'win32' ;
10+ arch: 'arm64' | 'amd64' ;
11+ platform: 'darwin' | 'linux' | 'windows' ;
12+ extension: '.tar.gz' | '.zip' ;
1213 baseUrl: string ;
1314}
1415
16+ const platformMap : Partial < Record < NodeJS . Platform , BinaryConfig [ 'platform' ] >> = {
17+ linux : "linux" ,
18+ darwin : "darwin" ,
19+ win32 : "windows"
20+ } ;
21+
22+ const archMap : Partial < Record < NodeJS . Architecture , BinaryConfig [ 'arch' ] >> = {
23+ arm64 : "arm64" ,
24+ x64 : "amd64" ,
25+ } ;
26+
27+ const extensionMap : Partial < Record < NodeJS . Platform , BinaryConfig [ 'extension' ] >> = {
28+ linux : ".tar.gz" ,
29+ darwin : ".tar.gz" ,
30+ win32 : ".zip" ,
31+ } ;
32+
1533class BinaryDownloader {
1634 private config : BinaryConfig ;
1735
@@ -20,20 +38,9 @@ class BinaryDownloader {
2038 }
2139
2240 private URL ( ) : string {
23- const { arch, platform, baseUrl } = this . config ;
24- let version = PJ . version . trim ( ) ;
25-
26- let archieveType = ""
27- switch ( platform ) {
28- case 'darwin' :
29- case 'linux' :
30- archieveType = '.tar.gz' ;
31- break ;
32- case 'win32' :
33- archieveType = '.exe' ;
34- break ;
35- }
36- return `${ baseUrl } /${ version } /qstash-server_${ version } _${ platform } _${ arch } ${ archieveType } ` ;
41+ const { arch, platform, baseUrl, extension } = this . config ;
42+ let version = PJ . version . trim ( )
43+ return `${ baseUrl } /${ version } /qstash-server_${ version } _${ platform } _${ arch } ${ extension } ` ;
3744 }
3845
3946 public async download ( ) : Promise < NodeJS . ReadableStream > {
@@ -54,16 +61,15 @@ class BinaryDownloader {
5461 public async extract ( stream : NodeJS . ReadableStream ) : Promise < void > {
5562 return new Promise ( ( resolve , reject ) => {
5663 const bin = path . resolve ( "./bin" ) ;
57- switch ( this . config . platform ) {
58- case "darwin" :
59- case "linux" :
64+ switch ( this . config . extension ) {
65+ case ".tar.gz" :
6066 const untar = tar . extract ( { cwd : bin } ) ;
6167 stream
6268 . pipe ( untar )
6369 . on ( 'close' , ( ) => resolve ( ) )
6470 . on ( 'error' , reject )
6571 break ;
66- case "win32 " :
72+ case ".zip " :
6773 stream
6874 . pipe ( unzipper . Extract ( { path : bin } ) )
6975 . on ( 'close' , ( ) => resolve ( ) )
@@ -73,24 +79,34 @@ class BinaryDownloader {
7379 }
7480}
7581
76- function getSysInfo ( ) : { arch: BinaryConfig [ 'arch' ] , platform : BinaryConfig [ 'platform' ] } {
77- const arch = os . arch ( ) === 'arm64' ? 'arm64' : 'x64' ;
78- const platform = os . platform ( ) as BinaryConfig [ 'platform' ] ;
82+ function getSysInfo ( ) : { arch: BinaryConfig [ 'arch' ] , platform : BinaryConfig [ 'platform' ] , extension : BinaryConfig [ 'extension' ] } {
83+ const arch = archMap [ process . arch ]
84+ const platform = platformMap [ process . platform ]
85+ const extension = extensionMap [ process . platform ]
86+
87+ if ( ! platform ) {
88+ throw new Error ( `Unsupported platform: ${ process . platform } ` ) ;
89+ }
90+
91+ if ( ! arch ) {
92+ throw new Error ( `Unsupported architecture: ${ process . arch } ` ) ;
93+ }
7994
80- if ( ! [ 'darwin' , 'linux' , 'win32' ] . includes ( platform ) ) {
81- throw new Error ( `Unsupported platform : ${ platform } ` ) ;
95+ if ( ! extension ) {
96+ throw new Error ( `Unsupported extension : ${ process . platform } ` ) ;
8297 }
8398
84- return { arch, platform } ;
99+ return { arch, platform, extension } ;
85100}
86101
87102( async ( ) => {
88103 try {
89- const { arch, platform } = getSysInfo ( ) ;
104+ const { arch, platform, extension } = getSysInfo ( ) ;
90105
91106 const downloader = new BinaryDownloader ( {
92107 arch,
93108 platform,
109+ extension,
94110 baseUrl : 'https://artifacts.upstash.com/qstash/versions'
95111 } ) ;
96112 const stream = await downloader . download ( ) ;
0 commit comments