UNPKG

@verdaccio/web

Version:
80 lines (79 loc) 2.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _debug = _interopRequireDefault(require("debug")); var _express = require("express"); var _lodash = _interopRequireDefault(require("lodash")); var _url = require("url"); var _core = require("@verdaccio/core"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const debug = (0, _debug.default)('verdaccio:web:api:search'); function checkAccess(pkg, auth, remoteUser) { return new Promise((resolve, reject) => { var _pkg$package; auth.allow_access({ packageName: pkg === null || pkg === void 0 ? void 0 : (_pkg$package = pkg.package) === null || _pkg$package === void 0 ? void 0 : _pkg$package.name }, remoteUser, function (err, allowed) { if (err) { if (err.status && String(err.status).match(/^4\d\d$/)) { // auth plugin returns 4xx user error, // that's equivalent of !allowed basically allowed = false; return resolve(null); } else { reject(err); } } else { return resolve(allowed ? pkg : null); } }); }); } function addSearchWebApi(storage, auth) { const router = (0, _express.Router)(); /* eslint new-cap: 0 */ router.get('/search/:anything', async function (req, res, next) { try { var _ref; let data; const abort = new AbortController(); req.socket.on('close', function () { debug('search web aborted'); abort.abort(); }); const text = (_ref = req.params.anything) !== null && _ref !== void 0 ? _ref : ''; // These values are declared as optimal by npm cli // FUTURE: could be overwritten by ui settings. const size = 20; const from = 0; const query = { from: 0, maintenance: 0.5, popularity: 0.98, quality: 0.65, size: 20, text }; // @ts-ignore const urlParams = new _url.URLSearchParams(query); debug('search web init'); data = await (storage === null || storage === void 0 ? void 0 : storage.search({ query, url: `/-/v1/search?${urlParams.toString()}`, abort })); const checkAccessPromises = await Promise.all(data.map(pkgItem => { return checkAccess(pkgItem, auth, req.remote_user); })); const final = checkAccessPromises.filter(i => !_lodash.default.isNull(i)).slice(from, size); next(final); } catch (err) { next(_core.errorUtils.getInternalError(err.message)); } }); return router; } var _default = addSearchWebApi; exports.default = _default; //# sourceMappingURL=search.js.map