introduced api version
Massimo Melina committed
Apr 17, 2022 at 11:19 UTC
76c81e4da057973a68db6112554dcd38b18babf6
6 files changed
+25
-13
README.md
+3
-2
@@ -175,12 +175,13 @@ The `api` object you get as parameter of the `init` contains the following:
175
- `require: function` use this instead of standard `require` function to access modules already loaded by HFS.
176
177
- `getConfig(key: string): any` get config's value set up by using `exports.config`.
178
+
179
+ - `const: object` all constants of the `const.ts` file are exposed here. E.g. BUILD_TIMESTAMP, API_VERSION, etc.
180
181
- `srcDir: string` this can be useful if you need to import some extra function not available in `api`.
182
```js
183
exports.init = api => {
182
- const { BUILD_TIMESTAMP } = api.require(api.srcDir + '/const')
183
- console.log(BUILD_TIMESTAMP)
184
+ const { watchLoad } = api.require(api.srcDir + '/watchLoad')
185
}
186
```
187
You *should* try to keep this kind of behavior at its minimum, as name of sources and of elements in them are subject to change.
plugins/antibrute/plugin.js
+2
-2
@@ -1,5 +1,6 @@
1
exports.version = 1
2
exports.description = "Introduce increasing delays between login attempts."
3
+exports.apiRequired = 1
4
5
// these settings will grant 4 attempts in first minute, 2 in second minute, and 1 from the third one on
6
const INCREMENT = 5_000
@@ -8,8 +9,7 @@ const CAP = 60_000
9
const byIp = {}
10
11
exports.init = api => {
11
- const { API_URI } = api.require(api.srcDir + '/const')
12
- const LOGIN_URI = API_URI + 'loginSrp1'
12
+ const LOGIN_URI = api.const.API_URI + 'loginSrp1'
13
return ({
14
async middleware(ctx) {
15
if (ctx.path !== LOGIN_URI) return
plugins/redirect-root/plugin.js
+4
-6
@@ -7,12 +7,10 @@ exports.config = {
7
8
exports.init = api => ({
9
middleware(ctx) {
10
- if (ctx.path === '/') {
11
- const url = api.getConfig('url')
12
- if (url) {
13
- ctx.redirect(url)
14
- return true
15
- }
10
+ const url = api.getConfig('url')
11
+ if (url && ctx.path === '/') {
12
+ ctx.redirect(url)
13
+ return true
14
}
15
}
16
})
server/src/adminApis.ts
+4
-1
@@ -3,7 +3,8 @@
3
import { ApiError, ApiHandlers } from './apiMiddleware'
4
import { defineConfig, getConfig, getWholeConfig, setConfig } from './config'
5
import { getStatus, getUrls } from './listen'
6
-import { BUILD_TIMESTAMP, CFG_PLUGINS_CONFIG, FORBIDDEN, HFS_STARTED, IS_WINDOWS, VERSION } from './const'
6
+import { API_VERSION, BUILD_TIMESTAMP, CFG_PLUGINS_CONFIG,
7
+ COMPATIBLE_API_VERSION, FORBIDDEN, HFS_STARTED, IS_WINDOWS, VERSION } from './const'
8
import vfsApis from './api.vfs'
9
import accountsApis from './api.accounts'
10
import { Connection, getConnections } from './connections'
@@ -49,6 +50,8 @@ export const adminApis: ApiHandlers = {
50
started: HFS_STARTED,
51
build: BUILD_TIMESTAMP,
52
version: VERSION,
53
+ apiVersion: API_VERSION,
54
+ compatibleApiVersion: COMPATIBLE_API_VERSION,
55
http: serverStatus(st.httpSrv, getConfig('port')),
56
https: serverStatus(st.httpsSrv, getConfig('https_port')),
57
urls: getUrls(),
server/src/const.ts
+3
@@ -12,6 +12,9 @@ export const VERSION = ''
12
export const SESSION_DURATION = 30*60_000
13
export const DAY = 86_400_000
14
15
+export const API_VERSION = 1
16
+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
17
+
18
export const SPECIAL_URI = '/~/'
19
export const FRONTEND_URI = SPECIAL_URI + 'frontend/'
20
export const ADMIN_URI = SPECIAL_URI + 'admin/'
server/src/plugins.ts
+9
-2
@@ -4,7 +4,8 @@ import glob from 'fast-glob'
4
import { watchLoad } from './watchLoad'
5
import _ from 'lodash'
6
import { resolve } from 'path'
7
-import { CFG_PLUGINS_CONFIG, PLUGINS_PUB_URI } from './const'
7
+import { API_VERSION, CFG_PLUGINS_CONFIG, COMPATIBLE_API_VERSION, PLUGINS_PUB_URI } from './const'
8
+import * as Const from './const'
9
import Koa from 'koa'
10
import { debounceAsync, getOrSet, onProcessExit, wantArray, watchDir } from './misc'
11
import { getConfig, subscribeConfig } from './config'
@@ -130,7 +131,7 @@ type PluginMiddleware = (ctx:Koa.Context) => void | Stop | CallMeAfter
131
type Stop = true
132
type CallMeAfter = ()=>void
133
133
-let availablePlugins: Record<string, { id: string, description?: string, version?: number }> = {}
134
+let availablePlugins: Record<string, { id: string, description?: string, version?: number, apiRequired?: number }> = {}
135
136
export function getAvailablePlugins() {
137
return Object.values(availablePlugins)
@@ -150,6 +151,7 @@ async function rescan() {
151
const source = await readFile(f, 'utf8')
152
pl.description = /exports.description *= *"([^"]*)"/.exec(source)?.[1]
153
pl.version = Number(/exports.version *= *(\d+)/.exec(source)?.[1]) || undefined
154
+ pl.apiRequired = Number(/exports.apiRequired *= *(\d+)/.exec(source)?.[1]) || undefined
155
}
156
catch {}
157
continue
@@ -164,8 +166,13 @@ async function rescan() {
166
const { init, ...data } = await import(f)
167
delete data.default
168
deleteModule(require.resolve(f)) // avoid caching
169
+ if (data.apiRequired > API_VERSION)
170
+ console.log('plugin', id, 'may not work correctly as it is designed for a newer version of HFS')
171
+ if (data.apiRequired < COMPATIBLE_API_VERSION)
172
+ console.log('plugin', id, 'may not work correctly as it is designed for an older version of HFS')
173
const res = await init?.call(null, {
174
srcDir: __dirname,
175
+ const: Const,
176
require,
177
getConfig: (cfgKey: string) =>
178
getConfig(CFG_PLUGINS_CONFIG)?.[id]?.[cfgKey]