PluginProbe
Extendify / 0.9.2
Extendify v0.9.2
3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 0.7.0 All 126 releases
extendify / src / Library / middleware / index.js

index.js in Extendify 0.9.2, at src/Library/middleware/index.js

45 lines 1.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { hasPluginsActivated } from './hasPluginsActivated'
2 import { hasRequiredPlugins } from './hasRequiredPlugins'
3
4 export const Middleware = (middleware = []) => {
5 return {
6 hasRequiredPlugins: hasRequiredPlugins,
7 hasPluginsActivated: hasPluginsActivated,
8 stack: [],
9 async check(template) {
10 for (const m of middleware) {
11 const cb = await this[`${m}`](template)
12 this.stack.push(cb.pass ? cb.allow : cb.deny)
13 }
14 },
15 reset() {
16 this.stack = []
17 },
18 }
19 }
20
21 export async function AuthorizationCheck(middleware) {
22 const middlewareGenerator = MiddlewareGenerator(middleware.stack)
23 while (true) {
24 let result
25 try {
26 result = await middlewareGenerator.next()
27 } catch {
28 // Reset the stack and exit the middleware
29 // This is used if you want to have the user cancel
30 middleware.reset()
31 throw 'Middleware exited'
32 }
33
34 // TODO: Could probably have a check for errors here
35 if (result.done) {
36 break
37 }
38 }
39 }
40 export async function* MiddlewareGenerator(middleware) {
41 for (const m of middleware) {
42 yield await m()
43 }
44 }
45