@@ -66,10 +66,30 @@ function getLatestAsset(os, assets) {
6666 } ) ;
6767}
6868
69- async function getReleaseInfo ( os ) {
70- const cache = JSON . parse ( localStorage . getItem ( "artifacts" ) ) ;
69+ async function getReleaseInfo ( os , currentArch ) {
70+ let cache = JSON . parse ( localStorage . getItem ( "artifacts" ) ) ;
7171
72- if ( shouldRegenRequest ( cache ) || cache . platform !== os ) {
72+ // Check if cache is missing required fields
73+ let cacheInvalid = false ;
74+ if (
75+ ! cache ||
76+ ! cache . data ||
77+ ! cache . platform ||
78+ ! Array . isArray ( cache . data ) ||
79+ cache . data . some (
80+ ( r ) =>
81+ typeof r . version === "undefined" ||
82+ typeof r . url === "undefined" ||
83+ typeof r . ext === "undefined" ||
84+ ! ( "arch" in r ) ,
85+ )
86+ ) {
87+ cacheInvalid = true ;
88+ localStorage . removeItem ( "artifacts" ) ;
89+ cache = null ;
90+ }
91+
92+ if ( cacheInvalid || shouldRegenRequest ( cache ) || cache . platform !== os ) {
7393 const resp = await (
7494 await fetch ( "https://api.github.com/repos/Moosync/Moosync/releases" )
7595 ) . json ( ) ;
@@ -79,12 +99,32 @@ async function getReleaseInfo(os) {
7999 const downloadAssets = getLatestAsset ( os , latest . assets ) ;
80100 const ret = [ ] ;
81101 for ( const asset of downloadAssets ) {
82- ret . push ( {
83- version : latest . name . replace ( "Moosync" , "" ) . trim ( ) ,
84- url : asset . browser_download_url ,
85- ext : extractExtension ( asset . name ) ,
86- } ) ;
87- console . log ( latest . name . replace ( "Moosync" , "" ) . trim ( ) ) ;
102+ const ext = extractExtension ( asset . name ) ;
103+ let arch = null ;
104+ const url = asset . browser_download_url ;
105+
106+ // Special case for rpm: Moosync-11.0.2-1.aarch64.rpm
107+ let match = url . match ( / \. ( [ ^ . ] + ) \. r p m $ / i) ;
108+ if ( match && match [ 1 ] ) {
109+ arch = sanitizeArch ( match [ 1 ] ) ;
110+ } else if (
111+ ( match = url . match ( / M o o s y n c _ ( [ \w \d ] + ) \. a p p \. t a r \. g z $ / i) ) &&
112+ match [ 1 ]
113+ ) {
114+ arch = sanitizeArch ( match [ 1 ] ) ;
115+ } else if ( ( match = url . match ( / _ ( [ \w \d ] + ) (? = \. [ ^ . ] + $ ) / i) ) && match [ 1 ] ) {
116+ arch = sanitizeArch ( match [ 1 ] ) ;
117+ }
118+
119+ console . log ( arch , currentArch , ext , url ) ;
120+ if ( arch && arch === currentArch ) {
121+ ret . push ( {
122+ version : latest . name . replace ( "Moosync" , "" ) . trim ( ) ,
123+ url : url ,
124+ ext : ext ,
125+ arch : arch ,
126+ } ) ;
127+ }
88128 }
89129
90130 setCache ( "artifacts" , {
@@ -119,17 +159,73 @@ function extractExtension(fileName) {
119159 return split [ split . length - 2 ] + "." + split [ split . length - 1 ] ;
120160}
121161
122- function getSanitizedLinuxName ( ext ) {
162+ function sanitizeArch ( arch ) {
163+ if ( arch === "amd64" || arch === "x86_64" ) {
164+ return "x64" ;
165+ } else if ( arch === "aarch64" ) {
166+ return "armv8" ;
167+ }
168+ return arch ;
169+ }
170+
171+ function getCurrentArch ( ) {
172+ if ( navigator . userAgentData && navigator . userAgentData . architecture ) {
173+ return sanitizeArch ( navigator . userAgentData . architecture ) ;
174+ }
175+
176+ const ua = navigator . userAgent . toLowerCase ( ) ;
177+ const platform = navigator . platform . toLowerCase ( ) ;
178+
179+ if ( ua . includes ( "arm" ) || platform . includes ( "arm" ) ) {
180+ if ( ua . includes ( "aarch64" ) || platform . includes ( "aarch64" ) ) {
181+ return "armv8" ;
182+ }
183+ return "arm" ;
184+ }
185+ if (
186+ ua . includes ( "x86_64" ) ||
187+ ua . includes ( "win64" ) ||
188+ platform . includes ( "x86_64" ) ||
189+ platform . includes ( "win64" )
190+ ) {
191+ return "x64" ;
192+ }
193+ if ( ua . includes ( "amd64" ) || platform . includes ( "amd64" ) ) {
194+ return "x64" ;
195+ }
196+ if (
197+ ua . includes ( "i686" ) ||
198+ ua . includes ( "i386" ) ||
199+ platform . includes ( "i686" ) ||
200+ platform . includes ( "i386" )
201+ ) {
202+ return "x86" ;
203+ }
204+ return "unknown" ;
205+ }
206+
207+ function getSanitizedLinuxName ( release ) {
208+ let name ;
209+ let ext = release . ext ;
210+ let arch = release . arch ;
211+
123212 switch ( ext ) {
124213 case "deb" :
125- return "Debian (.deb)" ;
214+ name = "Debian (.deb)" ;
215+ break ;
126216 case "pacman" :
127- return "Arch Linux (.pacman)" ;
217+ name = "Arch Linux (.pacman)" ;
218+ break ;
128219 case "rpm" :
129- return "Fedora (.rpm)" ;
220+ name = "Fedora (.rpm)" ;
221+ break ;
222+ case "AppImage" :
223+ name = "AppImage" ;
224+ break ;
130225 default :
131- return ext ;
226+ name = ext ;
132227 }
228+ return arch ? `${ name } (${ arch } )` : name ;
133229}
134230
135231function getSanitizedName ( release ) {
@@ -138,7 +234,7 @@ function getSanitizedName(release) {
138234 release . url . includes ( "deb" ) ||
139235 release . url . includes ( "AppImage" )
140236 ) {
141- return getSanitizedLinuxName ( release . ext ) ;
237+ return getSanitizedLinuxName ( release ) ;
142238 }
143239
144240 const match = release . url . match ( / ^ .* \/ .* - \d + \. \d + \. \d + - ( [ ^ . ] * ) \. ( .* ) $ / ) ;
@@ -149,9 +245,10 @@ function getSanitizedName(release) {
149245export async function setupDownloadButton ( ) {
150246 const os = getOS ( ) ;
151247 const downloadParent = document . getElementById ( "downloads" ) ;
248+ const currentArch = getCurrentArch ( ) ;
152249
153250 if ( os && os !== OSEnum . UNDEFINED ) {
154- const releases = await getReleaseInfo ( os ) ;
251+ const releases = await getReleaseInfo ( os , currentArch ) ;
155252 const osReadable = getReaderFriendlyName ( os ) ;
156253
157254 if ( releases . length === 1 ) {
0 commit comments