sensai
Version:
Because even AI needs a master
55 lines (54 loc) • 1.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "default", {
enumerable: true,
get: function() {
return _default;
}
});
const _nodepath = require("node:path");
const _default = ()=>{
const map = {};
return {
/**
* Add middleware.
*
* @notes please note this module is private and we trust the parent will pass the right middleware
* files.
*/ add (midPath) {
const dirPath = (0, _nodepath.dirname)(midPath);
const middlewares = map[dirPath] || [];
if (!middlewares.includes(midPath)) {
middlewares.push(midPath);
map[dirPath] = middlewares.sort();
}
},
/**
* Return list of middlewares that apply to a given file.
*/ get (filePath) {
const middlewares = [];
const segments = (0, _nodepath.dirname)(filePath).split(_nodepath.sep).slice(1);
let dirPath = _nodepath.sep;
for (const segment of segments){
dirPath = (0, _nodepath.join)(dirPath, segment);
middlewares.push(...map[dirPath] || []);
}
return middlewares;
},
/**
* Remove middleware.
*/ remove (midPath) {
const dirPath = (0, _nodepath.dirname)(midPath);
const middlewares = map[dirPath];
if (middlewares) {
const index = middlewares.indexOf(midPath);
if (index > -1) {
middlewares.splice(index, 1);
}
}
//delete map[dirname(midPath)]
}
};
};