plugins: doc for ctx.state

Massimo Melina committed May 30, 2024 at 19:04 UTC 19e076120497e1ba748423c0a5b169c84de46a6a
1 file changed +34 -4
dev-plugins.md
+34 -4
@@ -88,12 +88,10 @@ used must be strictly JSON (thus, no single quotes, only double quotes for strin
88 ctx.status = 404
89 }
90 ```
91 - You'll find more examples by studying plugins like `vhosting` or `antibrute`.
92 - This API is based on [Koa](https://koajs.com), because that's what HFS is using.
93 - To know what the Context object contains please refer to [Koa documentation](https://github.com/koajs/koa/blob/master/docs/api/context.md).
94 - You don't get the `next` parameter as in standard Koa middlewares because this is different, but we are now explaining how to achieve the same results.
91 + You'll find more examples by studying plugins like `antidos` or `antibrute`.
92 To interrupt other middlewares on this http request, return `true`.
93 If you want to execute something in the "upstream" of middlewares, return a function.
94 + You can read more in [the ctx object](#the-ctx-object) section.
95
96 - `unload: function` called when unloading a plugin. This is a good place for example to clearInterval().
97 - `onDirEntry: ({ entry: DirEntry, listUri: string }) => Promisable<void | false>` by providing this callback you can manipulate
@@ -420,6 +418,38 @@ This section is still partially documented, and you may need to have a look at t
418 - return: callback to call when upload is finished
419 - `uploadFinished`
420
421 +# The `ctx` object
422 +
423 +HFS is currently based on [Koa](https://koajs.com), so you'll see some things related to it in the backend API.
424 +The most prominent is the `ctx` object, short for "context".
425 +To know what the Context object contains please refer to [Koa documentation](https://github.com/koajs/koa/blob/master/docs/api/context.md).
426 +
427 +HFS adds a few useful properties in the `ctx.state` object. Some of it may turn to be useful,
428 +so we prepared this list as a quick reference, but beware that it may become out of date and needs double check.
429 +If so, please report, and we'll do our best to update it asap.
430 +Where information is too little, you'll have to consult the source code, sorry.
431 +
432 + originalPath: string // before roots is applied
433 + browsing?: string // for admin/monitoring
434 + dontLog?: boolean // don't log this request
435 + logExtra?: object
436 + completed?: Promise<unknown>
437 + spam?: boolean // this request was marked as spam
438 + params: Record<string, any>
439 + account?: Account // user logged in
440 + revProxyPath: string
441 + connection: Connection
442 + skipFilters?: boolean
443 + vfsNode?: VfsNode
444 + includesLastByte?: boolean
445 + serveApp?: boolean // please, serve the frontend app
446 + uploadPath?: string // current one
447 + uploads?: string[] // in case of request with potentially multiple uploads (POST), we register all filenames (no full path)
448 + length?: number
449 + originalStream?: typeof ctx.body
450 + uploadDestinationPath?: string
451 + archive?: string
452 +
453 ## Other files
454
455 Together with the main file (plugin.js), you can have other files, both for data and javascript to include with `require('./other-file')`.