better code: moved params and others from ctx to ctx.state
Massimo Melina committed
Jan 5, 2024 at 11:48 UTC
88a8e0cb8d789878866c4e2dfa790030b2651214
6 files changed
+16
-13
dev-plugins.md
+2
-1
@@ -366,11 +366,12 @@ Where `h` is just `import { createElement as h } from 'react'`.
366
367
## API version history
368
369
-- 8.64 (v0.51.0)
369
+- 8.65 (v0.51.0)
370
- plugin's own hfs-lang files
371
- props.can_overwrite
372
- ctx.state.considerAsGui
373
- new event: userPanelAfterInfo
374
+ - breaking: moved custom properties from ctx to ctx.state
375
- 8.5 (v0.49.0)
376
- new event: entry
377
- exports.onDirEntry: entry.icon
src/apiMiddleware.ts
+2
-2
@@ -24,8 +24,8 @@ export function apiMiddleware(apis: ApiHandlers) : Koa.Middleware {
24
return async (ctx) => {
25
if (!logApi.get())
26
ctx.state.dontLog = true
27
- const isPost = ctx.params
28
- const params = isPost ? ctx.params || {} : ctx.query
27
+ const isPost = ctx.state.params
28
+ const params = isPost ? ctx.state.params || {} : ctx.query
29
const apiName = ctx.path
30
console.debug('API', ctx.method, apiName, { ...params })
31
const safe = isPost && ctx.get('x-hfs-anti-csrf') // POST is safe because browser will enforce SameSite cookie
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.64
10
+export const API_VERSION = 8.65
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/middlewares.ts
+2
-4
@@ -253,10 +253,8 @@ export const prepareState: Koa.Middleware = async (ctx, next) => {
253
}
254
255
declare module "koa" {
256
- interface BaseContext {
257
- params: Record<string, any>
258
- }
256
interface DefaultState {
257
+ params: Record<string, any>
258
account?: Account // user logged in
259
revProxyPath: string
260
connection: Connection
@@ -267,7 +265,7 @@ declare module "koa" {
265
}
266
}
267
export const paramsDecoder: Koa.Middleware = async (ctx, next) => {
270
- ctx.params = ctx.method === 'POST' && ctx.originalUrl.startsWith(API_URI)
268
+ ctx.state.params = ctx.method === 'POST' && ctx.originalUrl.startsWith(API_URI)
269
&& (tryJson(await stream2string(ctx.req)) || {})
270
await next()
271
}
src/roots.ts
+2
-2
@@ -18,13 +18,13 @@ const rootsMandatory = defineConfig(CFG.roots_mandatory, false)
18
19
export const rootsMiddleware: Koa.Middleware = (ctx, next) =>
20
(() => {
21
- let params: undefined | typeof ctx.params | typeof ctx.query // undefined if we are not going to work on api parameters
21
+ let params: undefined | typeof ctx.state.params | typeof ctx.query // undefined if we are not going to work on api parameters
22
if (ctx.path.startsWith(SPECIAL_URI)) { // special uris should be excluded...
23
if (!ctx.path.startsWith(API_URI)) return // ...unless it's an api
24
let { referer } = ctx.headers
25
referer &&= new URL(referer).pathname
26
if (referer?.startsWith(ctx.state.revProxyPath + ADMIN_URI)) return // exclude apis for admin-panel
27
- params = ctx.params || ctx.query // for api we'll translate params
27
+ params = ctx.state.params || ctx.query // for api we'll translate params
28
}
29
if (_.isEmpty(roots.get())) return
30
const host2root = roots.compiled()
src/serveFile.ts
+7
-3
@@ -33,7 +33,8 @@ export async function serveFileNode(ctx: Koa.Context, node: VfsNode) {
33
return ctx.status = HTTP_FORBIDDEN
34
}
35
36
- ctx.vfsNode = node // useful to tell service files from files shared by the user
36
+ ctx.vfsNode = // legacy pre-0.51 (download-quota)
37
+ ctx.state.vfsNode = node // useful to tell service files from files shared by the user
38
if ('dl' in ctx.query) // please, download
39
ctx.attachment(name)
40
await serveFile(ctx, source||'', mimeString)
@@ -72,8 +73,10 @@ export async function serveFile(ctx: Koa.Context, source:string, mime?:string, c
73
try {
74
const stats = await promisify(stat)(source) // using fs's function instead of fs/promises, because only the former is supported by pkg
75
ctx.set('Last-Modified', stats.mtime.toUTCString())
75
- ctx.fileSource = source
76
- ctx.fileStats = stats
76
+ ctx.fileSource = // legacy pre-0.51
77
+ ctx.state.fileSource = source
78
+ ctx.fileStats = // legacy pre-0.51
79
+ ctx.state.fileStats = stats
80
ctx.status = HTTP_OK
81
if (ctx.fresh) {
82
updateConnection(ctx.state.connection, { ctx, op: 'cache' })
@@ -141,6 +144,7 @@ export function getRange(ctx: Koa.Context, totalSize: number) {
144
145
declare module "koa" {
146
interface DefaultState {
147
+ vfsNode?: VfsNode
148
includesLastByte?: boolean
149
}
150
}