file-routing-expressjs
Version:
A dependency-free, flexible, system-based file routing for Express.
114 lines • 3.64 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
var cache_exports = {};
__export(cache_exports, {
default: () => cache_default
});
module.exports = __toCommonJS(cache_exports);
const store = /* @__PURE__ */ new Map();
const CACHE_KEY = Symbol("cache-key");
const CACHE_BODY = Symbol("cache-body");
const cachePlugin = {
name: "cache",
validateConfig(config) {
if (!config || typeof config.ttl !== "number" || config.ttl <= 0) {
throw new Error(`[cache] "ttl" must be a positive number`);
}
if (config.max !== void 0 && (typeof config.max !== "number" || config.max <= 0)) {
throw new Error(`[cache] "max" must be a positive number`);
}
},
onRequest(ctx, next) {
return __async(this, null, function* () {
const key = `${ctx.req.method}:${ctx.req.originalUrl}`;
const entry = store.get(key);
if (entry && entry.expires > Date.now()) {
store.delete(key);
store.set(key, entry);
ctx.res.status(entry.status);
ctx.res.send(entry.body);
return;
}
ctx.state[CACHE_KEY] = key;
return next();
});
},
onResponse(ctx) {
return __async(this, null, function* () {
var _a, _b, _c, _d;
const key = ctx.state[CACHE_KEY];
if (!key) return;
const ttl = (_b = (_a = ctx.config) == null ? void 0 : _a.ttl) != null ? _b : 0;
if (!ttl) return;
const body = ctx.req[CACHE_BODY];
if (body === void 0) return;
const entry = {
expires: Date.now() + ttl,
status: ctx.res.statusCode,
body
};
if (store.has(key)) {
store.delete(key);
}
store.set(key, entry);
const max = (_d = (_c = ctx.config) == null ? void 0 : _c.max) != null ? _d : 500;
if (max && store.size > max) {
const oldestKey = store.keys().next().value;
if (oldestKey) store.delete(oldestKey);
}
});
},
wrap(handler) {
return (req, res, next) => __async(this, null, function* () {
let body;
const originalSend = res.send.bind(res);
res.send = (data) => {
body = data;
return originalSend(data);
};
try {
yield handler(req, res, next);
} finally {
req[CACHE_BODY] = body;
}
});
}
};
var cache_default = cachePlugin;
//# sourceMappingURL=cache.js.map