mirror of
https://github.com/jason5ng32/MyIP.git
synced 2026-11-04 08:55:47 +00:00
common/ had become the backend's library: 30 of its 41 modules were imported by nothing in the frontend, so the name no longer said what belonged there. It now holds only what the frontend or the build imports too; backend-only code moved to server/, flat except for datasets/ (the dataset engine and its readers). The *-db/ dataset directories stay in common/: self-hosting docs and Docker volumes mount those paths. frontend/utils/ keeps its app-wide modules at the root and gains ip/ (IP / ASN shaping and validation), report/ (the report pipeline) and features/ (the pure layer of a single feature). Paths only — no logic changes. The Dockerfile copies server/, and the AGENTS.md files say where a new module goes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
40 lines
1.8 KiB
JavaScript
40 lines
1.8 KiB
JavaScript
// Edge-cache middleware for /api routes (attached in backend-server.js).
|
|
// Every /api response defaults to no-store; a route wrapped in
|
|
// cacheable(maxAge) gets `public, max-age=…` on its 2xx JSON answers only —
|
|
// the edge must never cache a 4xx / 5xx.
|
|
//
|
|
// maxAge seconds, or a `(req) => seconds` resolver for routes whose
|
|
// TTL depends on the request (the /api/cfradar views); a
|
|
// falsy resolution keeps the no-store default
|
|
// cacheIf(body) optional veto for a 2xx body that answers a degraded
|
|
// upstream (an /api/asn-profile section in error)
|
|
// degradedMaxAge optional TTL for a body cacheIf vetoes, as a number or a
|
|
// `() => seconds` resolver read when the body is sent;
|
|
// falsy = no-store, the default
|
|
//
|
|
// The full-TTL header is also stashed on res.locals.cacheControl, so
|
|
// binary-stream handlers (which bypass res.json) can apply it on their own
|
|
// 2xx path.
|
|
|
|
const resolve = (value, ...args) => (typeof value === 'function' ? value(...args) : value);
|
|
|
|
export const cacheable = (maxAge, { cacheIf, degradedMaxAge } = {}) => (req, res, next) => {
|
|
const maxAgeSeconds = resolve(maxAge, req);
|
|
if (maxAgeSeconds) {
|
|
res.locals.cacheControl = `public, max-age=${maxAgeSeconds}`;
|
|
const originalJson = res.json.bind(res);
|
|
res.json = (body) => {
|
|
if (res.statusCode < 400) {
|
|
if (!cacheIf || cacheIf(body)) {
|
|
res.setHeader('Cache-Control', res.locals.cacheControl);
|
|
} else {
|
|
const degradedSeconds = resolve(degradedMaxAge);
|
|
if (degradedSeconds) res.setHeader('Cache-Control', `public, max-age=${degradedSeconds}`);
|
|
}
|
|
}
|
|
return originalJson(body);
|
|
};
|
|
}
|
|
next();
|
|
};
|