@dwp/govuk-casa
Version:
A framework for building GOVUK Collect-And-Submit-Applications
72 lines • 3.75 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = staticRouter;
const express_1 = __importDefault(require("express"));
const node_fs_1 = require("node:fs");
const node_url_1 = require("node:url");
const node_path_1 = require("node:path");
const node_module_1 = require("node:module");
const dirname_cjs_1 = __importDefault(require("./dirname.cjs"));
const MutableRouter_js_1 = __importDefault(require("../lib/MutableRouter.js"));
const utils_js_1 = require("../lib/utils.js");
const { static: ExpressStatic } = express_1.default; // CommonJS
/**
* @typedef {object} StaticOptions Options to configure static router
* @property {number} [maxAge=3600000] Cache TTL for all assets. Default is
* `3600000`
* @property {string} [cacheControl=private] Cache control headers. Default is
* `private`
*/
/**
* Create a router for serving CASA's static assets.
*
* @param {StaticOptions} options Options
* @returns {MutableRouter} ExpressJS Router instance
* @access private
*/
function staticRouter({ maxAge = 3600000, cacheControl = "no-cache, private", } = {}) {
const router = new MutableRouter_js_1.default();
const notFoundHandler = (req, res, next) => {
// Fall through to a general purpose error handler
next(new Error("404"));
};
const setHeaders = (req, res, next) => {
res.set("cache-control", cacheControl);
res.set("expires", new Date(Date.now() + maxAge).toUTCString());
const { pathname } = new node_url_1.URL(req?.originalUrl ?? "", "https://placeholder.test/");
if (pathname.substr(-4) === ".css") {
// Just needed for our in-memory CSS assets
res.set("content-type", "text/css");
}
next();
};
const staticConfig = {
etag: true,
lastModified: false,
maxAge,
setHeaders: (res) => {
setHeaders(null, res, () => { });
},
};
// The CASA CSS source contains the placeholder `~~~CASA_MOUNT_URL~~~` which
// must be replaced with the dynamic `mountUrl` to ensure govuk-frontend
// assets are served from the correct location.
/* eslint-disable security/detect-non-literal-fs-filename */
const casaCss = (0, node_fs_1.readFileSync)((0, node_path_1.resolve)(dirname_cjs_1.default, "../../dist/assets/css/casa.css"), { encoding: "utf8" });
/* eslint-enable security/detect-non-literal-fs-filename */
// The static middleware will only server GET/HEAD requests, so we can mount
// the middleware using `use()` rather than resorting to `get()`
const govukFrontendDirectory = (0, node_path_1.resolve)((0, node_module_1.createRequire)(dirname_cjs_1.default).resolve("govuk-frontend"), "../../");
router.use("/govuk/govuk-frontend.min.js", ExpressStatic(`${govukFrontendDirectory}/govuk/govuk-frontend.min.js`, staticConfig));
router.use("/govuk/govuk-frontend.min.js.map", ExpressStatic(`${govukFrontendDirectory}/govuk/govuk-frontend.min.js.map`, staticConfig));
router.use("/govuk/assets", ExpressStatic(`${govukFrontendDirectory}/govuk/assets`, staticConfig));
router.use("/govuk/assets", notFoundHandler);
router.get("/casa/assets/css/casa.css", setHeaders, (req, res) => res.send(casaCss.replace(/~~~CASA_MOUNT_URL~~~/g, (0, utils_js_1.validateUrlPath)(`${req.baseUrl}/`))));
router.use("/casa/assets/css/casa.css.map", ExpressStatic((0, node_path_1.resolve)(dirname_cjs_1.default, "../../dist/assets/css/casa.css.map")));
router.use("/casa/assets", notFoundHandler);
return router;
}
//# sourceMappingURL=static.js.map