PluginProbe
Extendify / 0.2.0
Extendify v0.2.0
3.2.1 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 All 127 releases
extendify / src / middleware / index.js

index.js in Extendify 0.2.0, at src/middleware/index.js

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