@@ -7,8 +7,8 @@ import path from 'path'
7
7
config ( )
8
8
9
9
10
- const BASE_URL_VERSION = 'https://api.github.com /repos/VueTorrent/VueTorrent/contents/version.txt?ref='
11
- const BASE_URL_ZIPBALL = 'https://api.github.com /repos/VueTorrent/VueTorrent/zipball/'
10
+ const BASE_URL_VERSION = '/repos/VueTorrent/VueTorrent/contents/version.txt?ref='
11
+ const BASE_URL_ZIPBALL = '/repos/VueTorrent/VueTorrent/zipball/'
12
12
const STABLE_BRANCH_NAME = 'latest-release'
13
13
const DEV_BRANCH_NAME = 'nightly-release'
14
14
const VERSION_PATTERN = / ^ v ? (?< version > [ 0 - 9 . ] + ) ( - (?< commits > \d + ) - g (?< sha > [ 0 - 9 a - f ] + ) ) ? $ /
@@ -19,6 +19,14 @@ const WEBUI_PATH = `${ BASE_FS_PATH }/vuetorrent`
19
19
const VERSION_FILE_PATH = `${ WEBUI_PATH } /version.txt`
20
20
const WEBUI_OLD_PATH = `${ BASE_FS_PATH } /vuetorrent-old`
21
21
22
+ const githubClient = axios . create ( {
23
+ baseURL : 'https://api.github.com' ,
24
+ headers : {
25
+ Accept : 'application/vnd.github.v3+json' ,
26
+ Authorization : process . env . GITHUB_AUTH ? `Bearer ${ process . env . GITHUB_AUTH } ` : undefined ,
27
+ }
28
+ } )
29
+
22
30
function extractVersion ( version ) {
23
31
const match = version . match ( VERSION_PATTERN )
24
32
return match ?. groups
@@ -41,21 +49,17 @@ function getInstalledVersion() {
41
49
}
42
50
}
43
51
44
- async function getLatestVersion ( url ) {
52
+ async function getLatestVersion ( ref ) {
45
53
/** @type {AxiosResponse<Record<string, any>> } */
46
- const response = await axios . get ( url , {
47
- headers : {
48
- Accept : 'application/vnd.github.v3+json'
49
- }
50
- } )
54
+ const response = await githubClient . get ( BASE_URL_VERSION + ref )
51
55
52
56
// Extract from base64
53
57
return extractVersion ( atob ( response . data . content . trim ( ) ) )
54
58
}
55
59
56
60
async function downloadFile ( url , outputPath ) {
57
61
const writer = fs . createWriteStream ( outputPath )
58
- const response = await axios ( {
62
+ const response = await githubClient ( {
59
63
url,
60
64
method : 'GET' ,
61
65
responseType : 'stream'
@@ -85,13 +89,13 @@ async function unzipFile(zipPath, extractTo) {
85
89
} ) )
86
90
}
87
91
88
- async function downloadUpdate ( link ) {
92
+ async function downloadUpdate ( ref ) {
89
93
if ( ! fs . existsSync ( BASE_FS_PATH ) ) {
90
94
fs . mkdirSync ( BASE_FS_PATH , { recursive : true } )
91
95
}
92
96
93
97
// Download zip file
94
- await downloadFile ( link , TEMP_ZIP_PATH )
98
+ await downloadFile ( BASE_URL_ZIPBALL + ref , TEMP_ZIP_PATH )
95
99
96
100
// Backup current install if it exists
97
101
if ( fs . existsSync ( WEBUI_OLD_PATH ) ) {
@@ -114,26 +118,24 @@ async function downloadUpdate(link) {
114
118
}
115
119
116
120
export async function checkForUpdate ( ) {
117
- let versionUrl , downloadUrl
121
+ let branchName
118
122
switch ( process . env . RELEASE_TYPE ) {
119
123
case 'dev' :
120
- versionUrl = BASE_URL_VERSION + DEV_BRANCH_NAME
121
- downloadUrl = BASE_URL_ZIPBALL + DEV_BRANCH_NAME
124
+ branchName = DEV_BRANCH_NAME
122
125
break
123
126
case 'stable' :
124
127
default :
125
- versionUrl = BASE_URL_VERSION + STABLE_BRANCH_NAME
126
- downloadUrl = BASE_URL_ZIPBALL + STABLE_BRANCH_NAME
128
+ branchName = STABLE_BRANCH_NAME
127
129
break
128
130
}
129
131
130
132
const installedVersion = getInstalledVersion ( )
131
- const latestVersion = await getLatestVersion ( versionUrl )
133
+ const latestVersion = await getLatestVersion ( branchName )
132
134
133
135
if ( installedVersion ?. version !== latestVersion ?. version
134
136
|| installedVersion ?. commits !== latestVersion ?. commits
135
137
|| installedVersion ?. sha !== latestVersion ?. sha ) {
136
- await downloadUpdate ( downloadUrl )
138
+ await downloadUpdate ( branchName )
137
139
return `Update successful from ${ formatVersion ( installedVersion ) } to ${ formatVersion ( latestVersion ) } `
138
140
}
139
141
return 'Already using the latest version'
0 commit comments