@samitouri / QOSami-HFS / commits / fe06c130

fix: admin/plugins: updates list could hang

Massimo Melina committed Sep 28, 2023 at 00:01 UTC fe06c130881b3546d10d78f5652f90da55a7c9e0
4 files changed +10 -10
src/api.plugins.ts
+6 -6
@@ -7,7 +7,7 @@ import {
7 } from './plugins'
8 import _ from 'lodash'
9 import assert from 'assert'
10 -import { Callback, newObj, onlyTruthy, onOff, waitFor } from './misc'
10 +import { Callback, newObj, onOff, waitFor } from './misc'
11 import { ApiError, ApiHandlers, SendListReadable } from './apiMiddleware'
12 import events from './events'
13 import { rm } from 'fs/promises'
@@ -31,7 +31,8 @@ const apis: ApiHandlers = {
31 async get_plugin_updates() {
32 return new SendListReadable({
33 async doAtStart(list) {
34 - const errs = await Promise.all(_.map(getFolder2repo(), async (repo, folder) => {
34 + const errs: string[] = []
35 + await Promise.allSettled(_.map(getFolder2repo(), async (repo, folder) => {
36 try {
37 if (!repo) return
38 //TODO shouldn't we consider other branches here?
@@ -45,12 +46,11 @@ const apis: ApiHandlers = {
46 list.add(online)
47 }
48 } catch (err: any) {
48 - if (err.message === '404') // the plugin is declaring a wrong repo
49 - return
50 - return err.code || err.message
49 + if (err.message !== '404') // the plugin is declaring a wrong repo
50 + errs.push(err.code || err.message)
51 }
52 }))
53 - for (const x of _.uniq(onlyTruthy(errs)))
53 + for (const x of _.uniq(errs))
54 list.error(x)
55 list.close()
56 }
src/const.ts
+1
@@ -31,6 +31,7 @@ export const PLUGINS_PUB_URI = SPECIAL_URI + 'plugins/'
31 export const HTTP_OK = 200
32 export const HTTP_NO_CONTENT = 204
33 export const HTTP_PARTIAL_CONTENT = 206
34 +export const HTTP_MOVED_PERMANENTLY = 301
35 export const HTTP_TEMPORARY_REDIRECT = 302
36 export const HTTP_NOT_MODIFIED = 304
37 export const HTTP_BAD_REQUEST = 400
src/github.ts
+2 -2
@@ -39,8 +39,8 @@ function downloadProgress(id: string, status: DownloadStatus) {
39
40 // determine default branch, possibly without consuming api quota
41 async function getGithubDefaultBranch(repo: string) {
42 - return await httpString(`https://github.com/${repo}/archive/refs/heads/main.zip`, { method: 'HEAD' }) ? 'main'
43 - : (await getRepoInfo(repo))?.default_branch as string
42 + const test = await httpString(`https://github.com/${repo}/archive/refs/heads/main.zip`, { method: 'HEAD' }).then(() => 1, () => 0)
43 + return test ? 'main' : (await getRepoInfo(repo))?.default_branch as string
44 }
45
46 export async function downloadPlugin(repo: Repo, { branch='', overwrite=false }={}) {
src/util-http.ts
+1 -2
@@ -3,7 +3,6 @@
3 import { RequestOptions } from 'https'
4 import http, { IncomingMessage } from 'node:http'
5 import https from 'node:https'
6 -import { HTTP_TEMPORARY_REDIRECT } from './const'
6 import _ from 'lodash'
7
8 // in case the response is not 2xx, it will throw and the error object is the Response object
@@ -31,7 +30,7 @@ export function httpStream(url: string, { body, ...options }:XRequestOptions ={}
30 console.debug("http responded", res.statusCode, "to", url)
31 if (!res.statusCode || res.statusCode >= 400)
32 return reject(new Error(String(res.statusCode), { cause: res }))
34 - if (res.statusCode === HTTP_TEMPORARY_REDIRECT && res.headers.location)
33 + if (res.headers.location)
34 return resolve(httpStream(res.headers.location, options))
35 resolve(res)
36 }).on('error', e => {