easier on github servers
Massimo Melina committed
Oct 23, 2025 at 13:43 UTC
de462d2b539dc8c4afffb9e0a5431bbfc520df98
1 file changed
+9
-8
src/github.ts
+9
-8
@@ -2,7 +2,7 @@
2
3
import events from './events'
4
import {
5
- httpString, httpStream, unzip, AsapStream, debounceAsync, asyncGeneratorToArray, wait, popKey, onlyTruthy, HOUR
5
+ httpString, httpStream, unzip, AsapStream, debounceAsync, asyncGeneratorToArray, wait, popKey, onlyTruthy, HOUR, DAY
6
} from './misc'
7
import {
8
DISABLING_SUFFIX, enablePlugin, findPluginByRepo, getInactivePlugins, getPluginInfo, isPluginRunning, mapPlugins,
@@ -20,6 +20,7 @@ import { join } from 'path'
20
import fs from 'fs'
21
import { storedMap } from './persistence'
22
import { argv } from './argv'
23
+import { expiringCache } from './expiringCache'
24
25
const DIST_ROOT = 'dist'
26
@@ -34,16 +35,16 @@ function downloadProgress(repo: string, status: DownloadStatus) {
35
events.emit('pluginDownload', { repo, status })
36
}
37
37
-// determine default branch, possibly without consuming api quota
38
-async function getGithubDefaultBranch(repo: string) {
38
+const branchCache = expiringCache<Promise<string>>(DAY)
39
+function getGithubDefaultBranch(repo: string) {
40
if (!repo.includes('/'))
41
throw 'malformed repo'
41
- const test = await httpString(`https://github.com/${repo}/archive/refs/heads/main.zip`, { method: 'HEAD' }).then(() => 1, (err) => {
42
- if (err?.cause?.statusCode !== 404)
43
- throw err
44
- return 0
42
+ return branchCache.try(repo, async () => {
43
+ for (const b of ['main', 'master']) // try to not consume api quota
44
+ if (await httpString(`https://github.com/${repo}/raw/refs/heads/${b}/dist/plugin.js`, { method: 'HEAD', noRedirect: true }).then(() => 1, () => 0))
45
+ return b
46
+ return (await getRepoInfo(repo))?.default_branch as string
47
})
46
- return test ? 'main' : (await getRepoInfo(repo))?.default_branch as string
48
}
49
50
export async function downloadPlugin(repo: Repo, { branch='', overwrite=false }={}) {