plugins: removed hardcoded branch name

Massimo Melina committed May 13, 2022 at 23:09 UTC 8f6934862b2271543d05d1de4d64a159e12c4961
1 file changed +12 -9
server/src/api.plugins.ts
+12 -9
@@ -43,7 +43,7 @@ const apis: ApiHandlers = {
43 const repo2id = getRepo2id()
44 for (const repo in repo2id)
45 try {
46 - const online = await readOnlinePlugin(repo)
46 + const online = await readOnlinePlugin(await getRepoInfo(repo))
47 if (!online.apiRequired || online.badApi) continue
48 const id = repo2id[repo]
49 const disk = getPluginInfo(id)
@@ -89,7 +89,7 @@ const apis: ApiHandlers = {
89 apiGithub('search/repositories?q=topic:hfs-plugin+' + encodeURI(text)).then(async res => {
90 for (const it of res.items) {
91 const repo = it.full_name
92 - const pl = await readOnlinePlugin(repo)
92 + const pl = await readOnlinePlugin(it)
93 if (!pl.apiRequired || pl.badApi) continue
94 Object.assign(pl, { // inject some extra useful fields
95 downloading: downloading[repo],
@@ -156,7 +156,7 @@ function downloadProgress(id: string, status: DownloadStatus) {
156
157 async function downloadPlugin(repo: string, overwrite?: boolean) {
158 downloadProgress(repo, true)
159 - const rec = await apiGithub('repos/'+repo)
159 + const rec = await getRepoInfo(repo)
160 const url = `https://github.com/${repo}/archive/${rec.default_branch}.zip`
161 const res = await httpsStream(url)
162 const repo2 = repo.split('/')[1] // second part, repo without the owner
@@ -198,12 +198,15 @@ async function apiGithub(uri: string) {
198 return JSON.parse(res.body)
199 }
200
201 -function readOnlinePlugin(repo: string) {
202 - return httpsString(`https://raw.githubusercontent.com/${repo}/master/${DIST_ROOT}plugin.js`).then(res => {
203 - if (!res.ok)
204 - throw res.statusCode
205 - return parsePluginSource(repo, res.body) // use 'repo' as 'id' client-side
206 - })
201 +function getRepoInfo(id: string) {
202 + return apiGithub('repos/'+id)
203 +}
204 +
205 +async function readOnlinePlugin(repo: { full_name: string, default_branch: string }) {
206 + const res = await httpsString(`https://raw.githubusercontent.com/${repo.full_name}/${repo.default_branch}/${DIST_ROOT}plugin.js`)
207 + if (!res.ok)
208 + throw res.statusCode
209 + return parsePluginSource(repo.full_name, res.body) // use 'repo' as 'id' client-side
210 }
211
212 function getRepo2id() {