2021-03-06 12:56:45 -05:00
|
|
|
import buildDebug from 'debug';
|
|
|
|
import LRU from 'lru-cache';
|
2021-10-29 10:33:05 -05:00
|
|
|
import { URL } from 'url';
|
|
|
|
|
|
|
|
import { WEB_TITLE } from '@verdaccio/config';
|
2021-09-25 17:08:00 -05:00
|
|
|
import { HEADERS } from '@verdaccio/core';
|
2022-03-27 14:42:52 -05:00
|
|
|
import { TemplateUIOptions } from '@verdaccio/types';
|
2021-03-06 12:56:45 -05:00
|
|
|
import { getPublicUrl } from '@verdaccio/url';
|
|
|
|
|
|
|
|
import renderTemplate from './template';
|
2021-10-29 10:33:05 -05:00
|
|
|
import { hasLogin, validatePrimaryColor } from './utils/web-utils';
|
2021-03-06 12:56:45 -05:00
|
|
|
|
|
|
|
const pkgJSON = require('../package.json');
|
|
|
|
const DEFAULT_LANGUAGE = 'es-US';
|
|
|
|
const cache = new LRU({ max: 500, maxAge: 1000 * 60 * 60 });
|
|
|
|
|
|
|
|
const debug = buildDebug('verdaccio:web:render');
|
|
|
|
|
|
|
|
const defaultManifestFiles = {
|
|
|
|
js: ['runtime.js', 'vendors.js', 'main.js'],
|
|
|
|
ico: 'favicon.ico',
|
|
|
|
};
|
|
|
|
|
2021-04-02 08:59:47 -05:00
|
|
|
export default function renderHTML(config, manifest, manifestFiles, req, res) {
|
2021-03-06 12:56:45 -05:00
|
|
|
const { url_prefix } = config;
|
2021-04-02 08:59:47 -05:00
|
|
|
const base = getPublicUrl(config?.url_prefix, req);
|
|
|
|
const basename = new URL(base).pathname;
|
2021-03-06 12:56:45 -05:00
|
|
|
const language = config?.i18n?.web ?? DEFAULT_LANGUAGE;
|
2022-04-14 01:29:39 -05:00
|
|
|
const needHtmlCache = [undefined, null].includes(config?.web?.html_cache)
|
|
|
|
? true
|
|
|
|
: config.web.html_cache;
|
2021-03-06 12:56:45 -05:00
|
|
|
const darkMode = config?.web?.darkMode ?? false;
|
|
|
|
const title = config?.web?.title ?? WEB_TITLE;
|
2021-05-05 16:23:03 -05:00
|
|
|
const login = hasLogin(config);
|
2021-03-06 12:56:45 -05:00
|
|
|
const scope = config?.web?.scope ?? '';
|
2021-06-13 09:23:48 -05:00
|
|
|
const logoURI = config?.web?.logo ?? '';
|
2021-05-04 14:15:07 -05:00
|
|
|
const pkgManagers = config?.web?.pkgManagers ?? ['yarn', 'pnpm', 'npm'];
|
2021-03-06 12:56:45 -05:00
|
|
|
const version = pkgJSON.version;
|
2022-03-27 14:42:52 -05:00
|
|
|
const flags = {
|
|
|
|
...config.flags,
|
|
|
|
};
|
2021-03-06 12:56:45 -05:00
|
|
|
const primaryColor = validatePrimaryColor(config?.web?.primary_color) ?? '#4b5e40';
|
2022-06-24 15:09:46 -05:00
|
|
|
const {
|
|
|
|
scriptsBodyAfter,
|
|
|
|
metaScripts,
|
|
|
|
scriptsbodyBefore,
|
|
|
|
showInfo,
|
|
|
|
showSettings,
|
|
|
|
showThemeSwitch,
|
|
|
|
showFooter,
|
|
|
|
showSearch,
|
|
|
|
showDownloadTarball,
|
|
|
|
} = Object.assign(
|
2021-04-02 08:59:47 -05:00
|
|
|
{},
|
|
|
|
{
|
|
|
|
scriptsBodyAfter: [],
|
|
|
|
bodyBefore: [],
|
|
|
|
metaScripts: [],
|
|
|
|
},
|
|
|
|
config?.web
|
|
|
|
);
|
2022-03-27 14:42:52 -05:00
|
|
|
const options: TemplateUIOptions = {
|
2022-06-24 15:09:46 -05:00
|
|
|
showInfo,
|
|
|
|
showSettings,
|
|
|
|
showThemeSwitch,
|
|
|
|
showFooter,
|
|
|
|
showSearch,
|
|
|
|
showDownloadTarball,
|
2021-03-06 12:56:45 -05:00
|
|
|
darkMode,
|
|
|
|
url_prefix,
|
|
|
|
basename,
|
2021-04-02 08:59:47 -05:00
|
|
|
base,
|
2021-03-06 12:56:45 -05:00
|
|
|
primaryColor,
|
|
|
|
version,
|
|
|
|
logoURI,
|
2022-03-27 14:42:52 -05:00
|
|
|
flags,
|
2021-05-05 16:23:03 -05:00
|
|
|
login,
|
2021-05-04 14:15:07 -05:00
|
|
|
pkgManagers,
|
2021-03-06 12:56:45 -05:00
|
|
|
title,
|
|
|
|
scope,
|
|
|
|
language,
|
|
|
|
};
|
|
|
|
|
|
|
|
let webPage;
|
|
|
|
|
|
|
|
try {
|
|
|
|
webPage = cache.get('template');
|
|
|
|
if (!webPage) {
|
|
|
|
webPage = renderTemplate(
|
|
|
|
{
|
|
|
|
manifest: manifestFiles ?? defaultManifestFiles,
|
|
|
|
options,
|
|
|
|
scriptsBodyAfter,
|
|
|
|
metaScripts,
|
2021-04-02 08:59:47 -05:00
|
|
|
scriptsbodyBefore,
|
2021-03-06 12:56:45 -05:00
|
|
|
},
|
|
|
|
manifest
|
|
|
|
);
|
2022-04-14 01:29:39 -05:00
|
|
|
if (needHtmlCache) {
|
|
|
|
cache.set('template', webPage);
|
|
|
|
debug('set template cache');
|
|
|
|
}
|
2021-03-06 12:56:45 -05:00
|
|
|
} else {
|
|
|
|
debug('reuse template cache');
|
|
|
|
}
|
2021-08-30 01:19:08 -05:00
|
|
|
} catch (error: any) {
|
2021-03-06 12:56:45 -05:00
|
|
|
throw new Error(`theme could not be load, stack ${error.stack}`);
|
|
|
|
}
|
|
|
|
res.setHeader('Content-Type', HEADERS.TEXT_HTML);
|
|
|
|
res.send(webPage);
|
2022-06-24 15:09:46 -05:00
|
|
|
debug('web rendered');
|
2021-03-06 12:56:45 -05:00
|
|
|
}
|