@samitouri / QOSami-HFS / commits / 918e6f63

plugins: handier way to support older hfs versions with ctx.stop()

Massimo Melina committed Jun 19, 2024 at 19:29 UTC 918e6f63c2fda00aa1980903398a0a0e34f3f076
3 files changed +7 -4
dev-plugins.md
+4 -1
@@ -90,6 +90,9 @@ used must be strictly JSON (thus, no single quotes, only double quotes for strin
90 ```
91 You'll find more examples by studying plugins like `antidos` or `antibrute`.
92 To interrupt other middlewares on this http request, call `ctx.stop()`.
93 + In past versions stop() was not available, and to get the same effect you'd `return true`, therefore a possible way
94 + to be compatible with older versions is to `return ctx.stop?.() || true`.
95 +
96 If you want to execute something in the "upstream" of middlewares, return a function. This function can be async.
97 You can read more in [the ctx object](#the-ctx-object) section.
98
@@ -599,7 +602,7 @@ If you want to override a text regardless of the language, use the special langu
602
603 ## API version history
604
602 -- 8.88 (v0.53.0)
605 +- 8.89 (v0.53.0)
606 - api.openDb
607 - frontend event: menuZip
608 - config.type:username
src/const.ts
+1 -1
@@ -7,7 +7,7 @@ import { mkdirSync } from 'fs'
7 import { basename, dirname, join } from 'path'
8 export * from './cross-const'
9
10 -export const API_VERSION = 8.88
10 +export const API_VERSION = 8.89
11 export const COMPATIBLE_API_VERSION = 1 // while changes in the api are not breaking, this number stays the same, otherwise it is made equal to API_VERSION
12 export const HFS_REPO = 'rejetto/hfs'
13
src/plugins.ts
+2 -2
@@ -137,7 +137,7 @@ export const pluginsMiddleware: Koa.Middleware = async (ctx, next) => {
137 lastStatus = ctx.status
138 lastBody = ctx.body
139 }
140 - if (res === true) { // true for legacy pre-0.53
140 + if (res === true && !ctx.isStopped) { // legacy pre-0.53
141 ctx.stop()
142 warnOnce(`plugin ${id} is using deprecated API (return true on middleware) and may not work with future versions (check for an update to "${id}")`)
143 }
@@ -183,7 +183,7 @@ declare module "koa" {
183 }
184 events.once('app', () => Object.assign(app.context, {
185 isStopped: false,
186 - stop() { this.isStopped = true }
186 + stop() { return this.isStopped = true }
187 }))
188
189 // return false to ask to exclude this entry from results