| 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 |
|