plugins: apiRequired in form of [min,max]

Massimo Melina committed May 14, 2022 at 01:27 UTC 2d123acc9efc3ad99ab6bc6e1506bea4cb30e224
2 files changed +15 -8
dev-plugins.md
+1
@@ -29,6 +29,7 @@ All the following properties are essentially optional.
29
30 - `description: string` try to explain what this plugin is for. This must go in `exports` and use "double quotes".
31 - `version: number` use progressive numbers to distinguish each release. This must go in `exports`.
32 +- `apiRequired: number | [min:number,max:number]` declare version(s) for which the plugin is designed for. You'll find api version in `src/const.ts`. This must go in `exports`.
33 - `frontend_css: string | string[]` path to one or more css files that you want the frontend to load. These are to be placed in the `public` folder (refer below).
34 - `frontend_js: string | string[]` path to one or more js files that you want the frontend to load. These are to be placed in the `public` folder (refer below).
35 - `middleware: (Context) => void | true | function` a function that will be used as a middleware: it can interfere with http activity.
server/src/plugins.ts
+14 -8
@@ -160,7 +160,7 @@ export interface AvailablePlugin {
160 id: string
161 description?: string
162 version?: number
163 - apiRequired?: number
163 + apiRequired?: number | [number,number]
164 repo?: string
165 branch?: string
166 badApi?: string
@@ -278,19 +278,25 @@ onProcessExit(() =>
278
279 export function parsePluginSource(id: string, source: string) {
280 const pl: AvailablePlugin = { id }
281 - const v = pl.description = /exports.description *= *"(.*)"/.exec(source)?.[1]
282 - if (v)
283 - try { pl.description = JSON.parse(`"${v}"`) }
284 - catch {}
281 + pl.description = tryJson(/exports.description *= *(".*")/.exec(source)?.[1])
282 pl.repo = /exports.repo *= *"(.*)"/.exec(source)?.[1]
283 pl.version = Number(/exports.version *= *(\d*\.?\d+)/.exec(source)?.[1]) ?? undefined
287 - pl.apiRequired = Number(/exports.apiRequired *= *(\d*\.?\d+)/.exec(source)?.[1]) ?? undefined
284 + pl.apiRequired = tryJson(/exports.apiRequired *= *([ \d.,[\]]+)/.exec(source)?.[1]) ?? undefined
285 + if (Array.isArray(pl.apiRequired) && (pl.apiRequired.length !== 2 || !pl.apiRequired.every(_.isFinite))) // validate [from,to] form
286 + pl.apiRequired = undefined
287 calculateBadApi(pl)
288 return pl
289 }
290
291 +function tryJson(s?: string) {
292 + try { return s && JSON.parse(s) }
293 + catch {}
294 +}
295 +
296 function calculateBadApi(data: AvailablePlugin) {
293 - data.badApi = data.apiRequired! > API_VERSION ? "may not work correctly as it is designed for a newer version of HFS"
294 - : data.apiRequired! < COMPATIBLE_API_VERSION ? "may not work correctly as it is designed for an older version of HFS"
297 + const r = data.apiRequired
298 + const [min, max] = Array.isArray(r) ? r : [r, r] // normalize data type
299 + data.badApi = min! > API_VERSION ? "may not work correctly as it is designed for a newer version of HFS - check for updates"
300 + : max! < COMPATIBLE_API_VERSION ? "may not work correctly as it is designed for an older version of HFS - check for updates"
301 : undefined
302 }