mirror of
https://github.com/verdaccio/verdaccio.git
synced 2025-03-25 02:32:52 -05:00
refactor: encourage usage of lodash
This commit is contained in:
parent
2df4f7b628
commit
a6321a0961
5 changed files with 34 additions and 20 deletions
|
@ -2,6 +2,7 @@
|
|||
|
||||
const Middleware = require('../../web/middleware');
|
||||
const mime = require('mime');
|
||||
const _ = require('lodash');
|
||||
|
||||
const media = Middleware.media;
|
||||
const expect_json = Middleware.expect_json;
|
||||
|
@ -9,14 +10,16 @@ const expect_json = Middleware.expect_json;
|
|||
module.exports = function(route, auth, storage) {
|
||||
const can = Middleware.allow(auth);
|
||||
const tag_package_version = function(req, res, next) {
|
||||
if (typeof(req.body) !== 'string') {
|
||||
if (_.isString(req.body) === false) {
|
||||
return next('route');
|
||||
}
|
||||
|
||||
let tags = {};
|
||||
tags[req.params.tag] = req.body;
|
||||
storage.merge_tags(req.params.package, tags, function(err) {
|
||||
if (err) return next(err);
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
res.status(201);
|
||||
return next({ok: 'package tagged'});
|
||||
});
|
||||
|
@ -33,7 +36,7 @@ module.exports = function(route, auth, storage) {
|
|||
can('publish'), media(mime.lookup('json')), tag_package_version);
|
||||
|
||||
route.delete('/-/package/:package/dist-tags/:tag', can('publish'), function(req, res, next) {
|
||||
let tags = {};
|
||||
const tags = {};
|
||||
tags[req.params.tag] = null;
|
||||
storage.merge_tags(req.params.package, tags, function(err) {
|
||||
if (err) {
|
||||
|
@ -48,7 +51,9 @@ module.exports = function(route, auth, storage) {
|
|||
|
||||
route.get('/-/package/:package/dist-tags', can('access'), function(req, res, next) {
|
||||
storage.get_package(req.params.package, {req: req}, function(err, info) {
|
||||
if (err) return next(err);
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
next(info['dist-tags']);
|
||||
});
|
||||
|
@ -57,7 +62,9 @@ module.exports = function(route, auth, storage) {
|
|||
route.post('/-/package/:package/dist-tags', can('publish'), media(mime.lookup('json')), expect_json,
|
||||
function(req, res, next) {
|
||||
storage.merge_tags(req.params.package, req.body, function(err) {
|
||||
if (err) return next(err);
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
res.status(201);
|
||||
return next({ok: 'tags updated'});
|
||||
});
|
||||
|
@ -66,7 +73,9 @@ module.exports = function(route, auth, storage) {
|
|||
route.put('/-/package/:package/dist-tags', can('publish'), media(mime.lookup('json')), expect_json,
|
||||
function(req, res, next) {
|
||||
storage.replace_tags(req.params.package, req.body, function(err) {
|
||||
if (err) return next(err);
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
res.status(201);
|
||||
return next({ok: 'tags updated'});
|
||||
});
|
||||
|
@ -75,7 +84,9 @@ module.exports = function(route, auth, storage) {
|
|||
route.delete('/-/package/:package/dist-tags', can('publish'), media(mime.lookup('json')),
|
||||
function(req, res, next) {
|
||||
storage.replace_tags(req.params.package, {}, function(err) {
|
||||
if (err) return next(err);
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
res.status(201);
|
||||
return next({ok: 'tags removed'});
|
||||
});
|
||||
|
|
|
@ -16,20 +16,20 @@ module.exports = function(route, auth, storage, config) {
|
|||
}
|
||||
info = Utils.filter_tarball_urls(info, req, config);
|
||||
|
||||
let version = req.params.version;
|
||||
if (_.isNil(version)) {
|
||||
let queryVersion = req.params.version;
|
||||
if (_.isNil(queryVersion)) {
|
||||
return next(info);
|
||||
}
|
||||
|
||||
let t = Utils.get_version(info, version);
|
||||
let t = Utils.get_version(info, queryVersion);
|
||||
if (_.isNil(t) === false) {
|
||||
return next(t);
|
||||
}
|
||||
|
||||
if (_.isNil(info['dist-tags']) === false) {
|
||||
if (_.isNil(info['dist-tags'][version]) === false) {
|
||||
version = info['dist-tags'][version];
|
||||
t = Utils.get_version(info, version);
|
||||
if (_.isNil(info['dist-tags'][queryVersion]) === false) {
|
||||
queryVersion = info['dist-tags'][queryVersion];
|
||||
t = Utils.get_version(info, queryVersion);
|
||||
if (_.isNil(t)) {
|
||||
return next(t);
|
||||
}
|
||||
|
@ -42,8 +42,8 @@ module.exports = function(route, auth, storage, config) {
|
|||
|
||||
route.get('/:package/-/:filename', can('access'), function(req, res) {
|
||||
const stream = storage.get_tarball(req.params.package, req.params.filename);
|
||||
stream.on('content-length', function(v) {
|
||||
res.header('Content-Length', v);
|
||||
stream.on('content-length', function(content) {
|
||||
res.header('Content-Length', content);
|
||||
});
|
||||
stream.on('error', function(err) {
|
||||
return res.report_error(err);
|
||||
|
|
|
@ -28,7 +28,7 @@ module.exports = function(config_hash) {
|
|||
const error_reporting_middleware = function(req, res, next) {
|
||||
res.report_error = res.report_error || function(err) {
|
||||
if (err.status && err.status >= 400 && err.status < 600) {
|
||||
if (!res.headersSent) {
|
||||
if (_.isNil(res.headersSent) === false) {
|
||||
res.status(err.status);
|
||||
next({error: err.message || 'unknown error'});
|
||||
}
|
||||
|
@ -67,7 +67,7 @@ module.exports = function(config_hash) {
|
|||
// Hook for tests only
|
||||
if (config._debug) {
|
||||
app.get('/-/_debug', function(req, res, next) {
|
||||
let do_gc = typeof(global.gc) !== 'undefined';
|
||||
const do_gc = _.isNil(global.gc) === false;
|
||||
if (do_gc) {
|
||||
global.gc();
|
||||
}
|
||||
|
|
|
@ -35,7 +35,10 @@ module.exports = function(config, auth, storage) {
|
|||
// Get list of all visible package
|
||||
route.get('/packages', function(req, res, next) {
|
||||
storage.get_local(function(err, packages) {
|
||||
if (err) throw err; // that function shouldn't produce any
|
||||
if (err) {
|
||||
// that function shouldn't produce any
|
||||
throw err;
|
||||
}
|
||||
|
||||
async.filterSeries(
|
||||
packages,
|
||||
|
|
|
@ -32,12 +32,12 @@ module.exports = function(config, auth, storage) {
|
|||
});
|
||||
});
|
||||
|
||||
router.get('/-/logo', function(req, res, next) {
|
||||
router.get('/-/logo', function(req, res) {
|
||||
res.sendFile(_.get(config, 'web.logo') || `${env.APP_ROOT}/static/logo-sm.png`
|
||||
);
|
||||
});
|
||||
|
||||
router.get('/', function(req, res, next) {
|
||||
router.get('/', function(req, res) {
|
||||
const proto = req.get('X-Forwarded-Proto') || req.protocol;
|
||||
const base = Utils.combineBaseUrl(proto, req.get('host'), config.url_prefix);
|
||||
res.setHeader('Content-Type', 'text/html');
|
||||
|
|
Loading…
Add table
Reference in a new issue