plugins_blacklist
Massimo Melina committed
Apr 14, 2023 at 16:08 UTC
a8b1693b1bed8b434a75acf5868ca7c31310a5e7
4 files changed
+27
-11
src/const.ts
+2
@@ -20,6 +20,8 @@ export const SESSION_DURATION = Number(process.env.SESSION_DURATION) * 1000 || D
20
export const API_VERSION = 8 // entry.uri + script.plugin
21
export const COMPATIBLE_API_VERSION = 1 // while changes in the api are not breaking, this number stays the same, otherwise is made equal to API_VERSION
22
23
+export const HFS_REPO = 'rejetto/hfs'
24
+
25
export const SPECIAL_URI = '/~/'
26
export const FRONTEND_URI = SPECIAL_URI + 'frontend/'
27
export const ADMIN_URI = SPECIAL_URI + 'admin/'
src/github.ts
+22
-6
@@ -7,7 +7,7 @@ import { getAvailablePlugins, mapPlugins, parsePluginSource, PATH as PLUGINS_PAT
7
import unzipper from 'unzip-stream'
8
import { ApiError } from './apiMiddleware'
9
import _ from 'lodash'
10
-import { HTTP_BAD_REQUEST, HTTP_CONFLICT } from './const'
10
+import { DAY, HFS_REPO, HTTP_BAD_REQUEST, HTTP_CONFLICT } from './const'
11
12
const DIST_ROOT = 'dist/'
13
@@ -51,12 +51,14 @@ export function getRepoInfo(id: string) {
51
return apiGithub('repos/'+id)
52
}
53
54
+export function readGithubFile(uri: string) {
55
+ return httpsString('https://raw.githubusercontent.com/' + uri)
56
+ .then(res => res.body)
57
+}
58
+
59
export async function readOnlinePlugin(repoInfo: { full_name: string, default_branch: string }, branch='') {
55
- const url = `https://raw.githubusercontent.com/${repoInfo.full_name}/${branch || repoInfo.default_branch}/${DIST_ROOT}plugin.js`
56
- const res = await httpsString(url)
57
- if (!res.ok)
58
- throw res.statusCode
59
- const pl = parsePluginSource(repoInfo.full_name, res.body) // use 'repo' as 'id' client-side
60
+ const res = await readGithubFile(`${repoInfo.full_name}/${branch || repoInfo.default_branch}/${DIST_ROOT}plugin.js`)
61
+ const pl = parsePluginSource(repoInfo.full_name, res) // use 'repo' as 'id' client-side
62
pl.branch = branch || undefined
63
return pl
64
}
@@ -80,9 +82,11 @@ async function apiGithub(uri: string) {
82
}
83
84
export async function* searchPlugins(text='') {
85
+ const projectInfo = await getProjectInfo()
86
const res = await apiGithub('search/repositories?q=topic:hfs-plugin+' + encodeURI(text))
87
for (const it of res.items) {
88
const repo = it.full_name
89
+ if (projectInfo?.plugins_blacklist?.includes(repo)) continue
90
let pl = await readOnlinePlugin(it)
91
if (!pl.apiRequired) continue // mandatory field
92
if (pl.badApi) { // we try other branches (starting with 'api')
@@ -107,3 +111,15 @@ export async function* searchPlugins(text='') {
111
yield pl
112
}
113
}
114
+
115
+// centralized hosted information, to be used as little as possible
116
+let cache
117
+export function getProjectInfo() {
118
+ return cache ||= readGithubFile(HFS_REPO + '/main/central.json').then(x => {
119
+ if (!x) throw x // go catch
120
+ setTimeout(() => cache = null, DAY) // invalidate cache
121
+ return JSON.parse(x)
122
+ }).catch(() => { // schedule next attempt
123
+ setTimeout(() => cache = null, 10_000)
124
+ })
125
+}
\ No newline at end of file
src/update.ts
+1
-3
@@ -1,7 +1,7 @@
1
// This file is part of HFS - Copyright 2021-2023, Massimo Melina <a@rejetto.com> - License https://www.gnu.org/licenses/gpl-3.0.txt
2
3
import { getRepoInfo } from './github'
4
-import { argv, IS_BINARY, IS_WINDOWS, VERSION } from './const'
4
+import { argv, HFS_REPO, IS_BINARY, IS_WINDOWS, VERSION } from './const'
5
import { basename, dirname, join } from 'path'
6
import { spawn } from 'child_process'
7
import { httpsStream, onProcessExit, unzip } from './misc'
@@ -9,8 +9,6 @@ import { renameSync, unlinkSync } from 'fs'
9
import { pluginsWatcher } from './plugins'
10
import { chmod, stat } from 'fs/promises'
11
12
-const HFS_REPO = 'rejetto/hfs'
13
-
12
export async function getUpdate() {
13
const [latest] = await getRepoInfo(HFS_REPO + '/releases?per_page=1')
14
if (latest.name === VERSION)
tsconfig.json
+2
-2
@@ -12,8 +12,8 @@
12
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
13
14
/* Language and Environment */
15
- "target": "es2018", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
16
- "lib": ["es5","es2018"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
15
+ "target": "es2022", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
16
+ "lib": ["es2022"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
17
// "jsx": "preserve", /* Specify what JSX code is generated. */
18
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
19
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */