UNPKG

@verdaccio/web

Version:
124 lines (121 loc) 4.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "$NextFunctionVer", { enumerable: true, get: function () { return _middleware.$NextFunctionVer; } }); Object.defineProperty(exports, "$RequestExtend", { enumerable: true, get: function () { return _middleware.$RequestExtend; } }); Object.defineProperty(exports, "$ResponseExtend", { enumerable: true, get: function () { return _middleware.$ResponseExtend; } }); exports.default = void 0; var _debug = _interopRequireDefault(require("debug")); var _express = require("express"); var _lodash = _interopRequireDefault(require("lodash")); var _logger = require("@verdaccio/logger"); var _middleware = require("@verdaccio/middleware"); var _tarball = require("@verdaccio/tarball"); var _utils = require("@verdaccio/utils"); var _webUtils = require("../web-utils"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } // Was required by other packages const getOrder = (order = 'asc') => { return order === 'asc'; }; const debug = (0, _debug.default)('verdaccio:web:api:package'); function addPackageWebApi(storage, auth, config) { var _ref, _config$web; const isLoginEnabled = (_ref = (config === null || config === void 0 ? void 0 : (_config$web = config.web) === null || _config$web === void 0 ? void 0 : _config$web.login) === true) !== null && _ref !== void 0 ? _ref : true; const pkgRouter = (0, _express.Router)(); /* eslint new-cap: 0 */ // eslint-disable-next-line @typescript-eslint/no-unused-vars const anonymousRemoteUser = { name: undefined, real_groups: [], groups: [] }; debug('initialized package web api'); const checkAllow = (name, remoteUser) => new Promise((resolve, reject) => { debug('is login disabled %o', isLoginEnabled); // FIXME: this logic does not work, review // const remoteUserAccess = !isLoginEnabled ? anonymousRemoteUser : remoteUser; try { auth.allow_access({ packageName: name }, remoteUser, (err, allowed) => { if (err) { resolve(false); } return resolve(allowed); }); } catch (err) { reject(err); } }); async function processPackages(packages = [], req) { const permissions = []; const packagesToProcess = packages.slice(); debug('process packages %o', packagesToProcess); for (const pkg of packagesToProcess) { const pkgCopy = { ...pkg }; pkgCopy.author = (0, _utils.formatAuthor)(pkg.author); try { if (await checkAllow(pkg.name, req.remote_user)) { if (config.web) { // @ts-ignore pkgCopy.author.avatar = (0, _utils.generateGravatarUrl)(pkgCopy.author.email, config.web.gravatar); } // convert any remote dist to a local reference // eg: if the dist points to npmjs, switch to localhost:4873/prefix/etc.tar.gz if (!_lodash.default.isNil(pkgCopy.dist) && !_lodash.default.isNull(pkgCopy.dist.tarball)) { pkgCopy.dist.tarball = (0, _tarball.getLocalRegistryTarballUri)(pkgCopy.dist.tarball, pkg.name, { protocol: req.protocol, headers: req.headers, host: req.hostname }, config === null || config === void 0 ? void 0 : config.url_prefix); } permissions.push(pkgCopy); } } catch (err) { debug('process packages error %o', err); _logger.logger.error({ name: pkg.name, error: err }, 'permission process for @{name} has failed: @{error}'); throw err; } } return permissions; } // Get list of all visible package pkgRouter.get('/packages', async function (req, res, next) { debug('hit package web api %o'); try { var _config$web2; const localPackages = await storage.getLocalDatabase(); const order = getOrder(config === null || config === void 0 ? void 0 : (_config$web2 = config.web) === null || _config$web2 === void 0 ? void 0 : _config$web2.sort_packages); debug('order %o', order); const pkgs = await processPackages(localPackages, req); next((0, _webUtils.sortByName)(pkgs, order)); } catch (error) { next(error); } }); return pkgRouter; } var _default = addPackageWebApi; exports.default = _default; //# sourceMappingURL=package.js.map