UNPKG

verdaccio

Version:

A lightweight private npm proxy registry

70 lines (69 loc) 2.98 kB
import { logger } from "../../lib/logger/index.mjs"; import api_default from "./api/index.mjs"; import _ from "lodash"; import { WebUrlsNamespace, renderWebMiddleware, setSecurityWebHeaders, validateName, validatePackage } from "@verdaccio/middleware"; import { PLUGIN_CATEGORY } from "@verdaccio/core"; import createDebug from "debug"; import express, { Router } from "express"; import { asyncLoadPlugin } from "@verdaccio/loaders"; import defaultTheme from "@verdaccio/ui-theme"; //#region src/api/web/index.ts var debug = createDebug("verdaccio:web"); var PLUGIN_UI_PREFIX = "verdaccio-theme"; var DEFAULT_PLUGIN_UI_THEME = "@verdaccio/ui-theme"; async function loadTheme(config) { if (_.isNil(config.theme) === false) { const plugin = await asyncLoadPlugin(config.theme, { config, logger }, function(plugin) { /** * - `staticPath`: is the same data returned in Verdaccio 5. - `manifest`: A webpack manifest object. - `manifestFiles`: A object with one property `js` and the array (order matters) of the manifest id to be loaded in the template dynamically. */ return plugin.staticPath && plugin.manifest && plugin.manifestFiles; }, true, config?.serverSettings?.pluginPrefix ?? "verdaccio-theme", PLUGIN_CATEGORY.THEME); if (plugin.length > 1) logger.warn("multiple ui themes are not supported; only the first plugin is used"); return _.head(plugin); } } function webAPIMiddleware(tokenMiddleware, webEndpointsApi) { const route = Router(); route.param("package", validatePackage); route.param("filename", validateName); route.param("version", validateName); route.use(express.urlencoded({ extended: false })); route.use(setSecurityWebHeaders); if (typeof tokenMiddleware === "function") route.use(tokenMiddleware); if (typeof webEndpointsApi === "function") route.use(webEndpointsApi); return route; } function webMiddleware(config, middlewares, pluginOptions) { const router = express.Router(); const { tokenMiddleware, webEndpointsApi } = middlewares; router.use(WebUrlsNamespace.root, renderWebMiddleware(config, tokenMiddleware, pluginOptions)); router.use(WebUrlsNamespace.endpoints, webAPIMiddleware(tokenMiddleware, webEndpointsApi)); return router; } var web_default = async (config, auth, storage, logger) => { let pluginOptions = await loadTheme(config); if (!pluginOptions) { debug("no theme plugin found, using default theme"); pluginOptions = defaultTheme(config.web); logger.info({ name: DEFAULT_PLUGIN_UI_THEME, pluginCategory: PLUGIN_CATEGORY.THEME }, "plugin @{name} successfully loaded (@{pluginCategory})"); } const router = Router(); router.use(webMiddleware(config, { tokenMiddleware: auth.webUIJWTmiddleware(), webEndpointsApi: api_default(auth, storage, config) }, pluginOptions)); return router; }; //#endregion export { DEFAULT_PLUGIN_UI_THEME, PLUGIN_UI_PREFIX, web_default as default, loadTheme, webAPIMiddleware, webMiddleware }; //# sourceMappingURL=index.mjs.map