built-in central.json

Massimo Melina committed Aug 15, 2023 at 09:33 UTC f2929917786b4aa62b5f523d8956f69dc4aa0abb
4 files changed +20 -12
central.json new
+1
@@ -0,0 +1 @@
1 +{}
\ No newline at end of file
package.json
+4 -3
@@ -25,10 +25,10 @@
25 "pub": "cd dist && npm publish",
26 "dist": "npm run build-all && npm run dist-bin",
27 "dist-bin": "npm run dist-modules && cd dist && pkg . --public -C gzip && mv -f hfs-win-x64.exe hfs.exe && zip hfs-windows.zip hfs.exe -r plugins && cp -f hfs-linux-x64 hfs && zip hfs-linux.zip hfs -r plugins && cp -f hfs-macos-x64 hfs && zip hfs-mac.zip hfs -r plugins && cp -f hfs-macos-arm64 hfs && zip hfs-mac-arm.zip hfs -r plugins && rm hfs",
28 - "dist-modules": "cp package*.json dist && cd dist && npm ci --omit=dev && npm run dist-crclib && rm package-lock.json && cd .. && node prune_modules",
28 + "dist-modules": "cp package*.json central.json dist && cd dist && npm ci --omit=dev && npm run dist-crclib && rm package-lock.json && cd .. && node prune_modules",
29 "dist-crclib": "npm i -f --no-save --omit=dev @node-rs/crc32-win32-x64-msvc @node-rs/crc32-darwin-arm64 @node-rs/crc32-darwin-x64 @node-rs/crc32-linux-x64-gnu",
30 - "dist-win": "cp package*.json dist && cd dist && npm ci --omit=dev && npm i -f --no-save --omit=dev @node-rs/crc32-win32 && pkg . --public -C gzip -t node16-win-x64",
31 - "dist-mac": "cp package*.json dist && cd dist && npm ci --omit=dev && pkg . --public -C gzip -t node16-macos-arm64",
30 + "dist-win": "npm run dist-modules && cd dist && pkg . --public -C gzip -t node16-win-x64",
31 + "dist-mac": "npm run dist-modules && cd dist && pkg . --public -C gzip -t node16-macos-arm64",
32 "dist-node": "npm run dist-modules && cd dist && zip hfs-node.zip -r * -x *.zip *.exe hfs-* *.log logs"
33 },
34 "engines": {
@@ -49,6 +49,7 @@
49 },
50 "pkg": {
51 "assets": [
52 + "central.json",
53 "admin/**/*",
54 "frontend/**/*"
55 ],
src/apiMiddleware.ts
+2 -2
@@ -47,8 +47,8 @@ export function apiMiddleware(apis: ApiHandlers) : Koa.Middleware {
47 (v as string[]).forEach((x,i) => fixUri(v,i))
48 res = await apiFun(params, ctx)
49
50 - function fixUri(o: any, k: string | number) {
51 - o[k] = removeStarting(ctx.state.revProxyPath, o[k])
50 + function fixUri(obj: any, k: string | number) {
51 + obj[k] = removeStarting(ctx.state.revProxyPath, obj[k])
52 }
53 }
54 catch(e) {
src/github.ts
+13 -7
@@ -14,6 +14,7 @@ import _ from 'lodash'
14 import { DAY, HFS_REPO, HTTP_BAD_REQUEST, HTTP_CONFLICT } from './const'
15 import { rename, rm } from 'fs/promises'
16 import { join } from 'path'
17 +import { readFileSync } from 'fs'
18
19 const DIST_ROOT = 'dist'
20
@@ -148,12 +149,17 @@ export async function* searchPlugins(text='') {
149
150 // centralized hosted information, to be used as little as possible
151 let cache
152 +const FN = 'central.json'
153 +let latest = JSON.parse(readFileSync(join(__dirname, '..', FN), 'utf8')) // initially built-in is our latest
154 export function getProjectInfo() {
152 - return cache ||= readGithubFile(HFS_REPO + '/main/central.json').then(x => {
153 - if (!x) throw x // go catch
154 - setTimeout(() => cache = null, DAY) // invalidate cache
155 - return JSON.parse(x)
156 - }).catch(() => { // schedule next attempt
157 - setTimeout(() => cache = null, 10_000)
158 - })
155 + return cache ||= readGithubFile(HFS_REPO + '/main/' + FN)
156 + .catch(() => latest) // fall back to latest
157 + .then(x => {
158 + if (!x) throw x // go catch
159 + setTimeout(() => cache = undefined, DAY) // invalidate cache
160 + return latest = JSON.parse(x)
161 + }).catch(() => { // schedule next attempt
162 + setTimeout(() => cache = undefined, 10_000) // invalidate cache sooner on errors
163 + return latest
164 + })
165 }
\ No newline at end of file