Skip to content

Commit 2318ca4

Browse files
authored
feat(GitHub): Add PAT support for increased rate limit (#87)
1 parent aeaa54f commit 2318ca4

File tree

4 files changed

+28
-19
lines changed

4 files changed

+28
-19
lines changed

.env.dist

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ RELEASE_TYPE=stable
55
UPDATE_VT_CRON=0 * * * *
66
USE_INSECURE_SSL=false
77
SKIP_X_FORWARD_HEADERS=false
8-
VUETORRENT_PATH=/vuetorrent
8+
VUETORRENT_PATH=/vuetorrent
9+
GITHUB_AUTH=*****

docker-compose.gluetun.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,7 @@ services:
5858
# - USE_INSECURE_SSL=true
5959
# Only enable if backend container is behind a proxy server which already add x-forward headers
6060
# - SKIP_X_FORWARD_HEADERS=true
61+
# Create a Github PAT with your account to increase API rate limit, fine-grained with only public repo access is enough
62+
# - GITHUB_AUTH=${GITHUB_AUTH}
6163
volumes:
6264
- "./data/config:/config"

docker-compose.simple.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ services:
4040
# - USE_INSECURE_SSL=true
4141
# Only enable if backend container is behind a proxy server which already add x-forward headers
4242
# - SKIP_X_FORWARD_HEADERS=true
43+
# Create a Github PAT with your account to increase API rate limit
44+
# fine-grained token with public repo access is enough
45+
# https://github.com/settings/personal-access-tokens/new
46+
# - GITHUB_AUTH=${GITHUB_AUTH}
4347
ports:
4448
- "8080:8080"
4549
volumes:

src/routines/update.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import path from 'path'
77
config()
88

99

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/'
1212
const STABLE_BRANCH_NAME = 'latest-release'
1313
const DEV_BRANCH_NAME = 'nightly-release'
1414
const VERSION_PATTERN = /^v?(?<version>[0-9.]+)(-(?<commits>\d+)-g(?<sha>[0-9a-f]+))?$/
@@ -19,6 +19,14 @@ const WEBUI_PATH = `${ BASE_FS_PATH }/vuetorrent`
1919
const VERSION_FILE_PATH = `${ WEBUI_PATH }/version.txt`
2020
const WEBUI_OLD_PATH = `${ BASE_FS_PATH }/vuetorrent-old`
2121

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+
2230
function extractVersion(version) {
2331
const match = version.match(VERSION_PATTERN)
2432
return match?.groups
@@ -41,21 +49,17 @@ function getInstalledVersion() {
4149
}
4250
}
4351

44-
async function getLatestVersion(url) {
52+
async function getLatestVersion(ref) {
4553
/** @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)
5155

5256
// Extract from base64
5357
return extractVersion(atob(response.data.content.trim()))
5458
}
5559

5660
async function downloadFile(url, outputPath) {
5761
const writer = fs.createWriteStream(outputPath)
58-
const response = await axios({
62+
const response = await githubClient({
5963
url,
6064
method: 'GET',
6165
responseType: 'stream'
@@ -85,13 +89,13 @@ async function unzipFile(zipPath, extractTo) {
8589
}))
8690
}
8791

88-
async function downloadUpdate(link) {
92+
async function downloadUpdate(ref) {
8993
if (!fs.existsSync(BASE_FS_PATH)) {
9094
fs.mkdirSync(BASE_FS_PATH, { recursive: true })
9195
}
9296

9397
// Download zip file
94-
await downloadFile(link, TEMP_ZIP_PATH)
98+
await downloadFile(BASE_URL_ZIPBALL + ref, TEMP_ZIP_PATH)
9599

96100
// Backup current install if it exists
97101
if (fs.existsSync(WEBUI_OLD_PATH)) {
@@ -114,26 +118,24 @@ async function downloadUpdate(link) {
114118
}
115119

116120
export async function checkForUpdate() {
117-
let versionUrl, downloadUrl
121+
let branchName
118122
switch (process.env.RELEASE_TYPE) {
119123
case 'dev':
120-
versionUrl = BASE_URL_VERSION + DEV_BRANCH_NAME
121-
downloadUrl = BASE_URL_ZIPBALL + DEV_BRANCH_NAME
124+
branchName = DEV_BRANCH_NAME
122125
break
123126
case 'stable':
124127
default:
125-
versionUrl = BASE_URL_VERSION + STABLE_BRANCH_NAME
126-
downloadUrl = BASE_URL_ZIPBALL + STABLE_BRANCH_NAME
128+
branchName = STABLE_BRANCH_NAME
127129
break
128130
}
129131

130132
const installedVersion = getInstalledVersion()
131-
const latestVersion = await getLatestVersion(versionUrl)
133+
const latestVersion = await getLatestVersion(branchName)
132134

133135
if (installedVersion?.version !== latestVersion?.version
134136
|| installedVersion?.commits !== latestVersion?.commits
135137
|| installedVersion?.sha !== latestVersion?.sha) {
136-
await downloadUpdate(downloadUrl)
138+
await downloadUpdate(branchName)
137139
return `Update successful from ${ formatVersion(installedVersion) } to ${ formatVersion(latestVersion) }`
138140
}
139141
return 'Already using the latest version'

0 commit comments

Comments
 (0)