UNPKG

verdaccio

Version:

A lightweight private npm proxy registry

55 lines (54 loc) 1.77 kB
import { HTTP_STATUS } from "../lib/constants.mjs"; import _ from "lodash"; import createDebug from "debug"; import Path from "path"; import fs from "fs"; import { fileURLToPath } from "url"; import { isURL } from "@verdaccio/url"; //#region src/api/middleware.ts var debug = createDebug("verdaccio:middleware:favicon"); function serveFavicon(config) { return function(_req, res) { try { const logoConf = config?.web?.favicon; if (logoConf === "") { debug("favicon disabled"); res.status(404); } else if (!_.isEmpty(logoConf)) { debug("custom favicon"); if (isURL(logoConf, { require_host: true, require_valid_protocol: true })) { debug("redirect to %o", logoConf); res.redirect(logoConf); return; } else { const faviconPath = Path.normalize(logoConf); debug("serving favicon from %o", faviconPath); fs.access(faviconPath, fs.constants.R_OK, (err) => { if (err) { debug("no read permissions to read: %o, reason:", logoConf, err?.message); return res.status(HTTP_STATUS.NOT_FOUND).end(); } else { res.setHeader("content-type", "image/x-icon"); fs.createReadStream(faviconPath).pipe(res); debug("rendered custom ico"); } }); } } else { res.setHeader("content-type", "image/x-icon"); const moduleDir = typeof __dirname === "undefined" ? Path.dirname(fileURLToPath(import.meta.url)) : __dirname; fs.createReadStream(Path.join(moduleDir, "./web/html/favicon.ico")).pipe(res); debug("rendered ico"); } } catch (err) { debug("error triggered, favicon not found %s", err?.message); res.status(HTTP_STATUS.NOT_FOUND).end(); } }; } //#endregion export { serveFavicon }; //# sourceMappingURL=middleware.mjs.map