代码拉取完成,页面将自动刷新
'use strict';
class middleware {
constructor (options = {}) {
this.chain = [];
this.stack = [];
this.cache = false;
this.init();
if (options && typeof options === 'object') {
if (options.cache !== undefined) {
this.cache = options.cache;
}
}
}
init () {
this.chain = [];
let first = async (ctx) => {
if (ctx && typeof ctx === 'object' && ctx.exec && typeof ctx.exec === 'function') {
return await ctx.exec(ctx);
}
};
this.chain.push(first);
}
add (midcall, filter = null) {
if (typeof midcall !== 'function' || midcall.constructor.name !== 'AsyncFunction') {
throw new Error(`middleware must be a async function`);
}
let last = this.chain.length - 1;
let nextcall = this.chain[last];
let realmid = function () {
if (filter !== null && typeof filter === 'function') {
return async (ctx) => {
if ( false === await filter(ctx) ) {
return await nextcall(ctx);
}
return await midcall(ctx, nextcall.bind(null, ctx));
};
}
return async (ctx) => {
return await midcall(ctx, nextcall.bind(null, ctx));
};
};
this.chain.push(realmid());
}
loadstack () {
this.init();
for (let i = this.stack.length - 1; i >= 0; i--) {
this.add(
this.stack[i].midcall,
this.stack[i].filter
);
}
}
use (midcall, filter = null) {
this.stack.push({
midcall: midcall,
filter: filter
});
if (this.cache === false) {
this.loadstack();
}
}
async run (ctx) {
let last = this.chain.length - 1;
return await this.chain[last](ctx);
}
}
module.exports = middleware;
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。