0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-16 21:56:25 -05:00

Fix more eslint warnings, add doc

This commit is contained in:
Juan Picado 2017-05-04 21:05:47 +02:00
parent 7c6c2d2932
commit 2a1f1a7dfa
No known key found for this signature in database
GPG key ID: 18AC54485952D158
3 changed files with 64 additions and 32 deletions

View file

@ -47,7 +47,8 @@ if (commander.args.length != 0) {
commander.help();
}
let config, config_path;
let config;
let config_path;
try {
if (commander.config) {
config_path = Path.resolve(commander.config);
@ -63,10 +64,17 @@ try {
afterConfigLoad();
/**
* Retrieve all addresses defined in the config file.
* Verdaccio is able to listen multiple ports
* eg:
* listen:
- localhost:5555
- localhost:5557
*/
function get_listen_addresses() {
// command line || config file || default
let addresses;
if (commander.listen) {
addresses = [commander.listen];
} else if (Array.isArray(config.listen)) {
@ -76,7 +84,6 @@ function get_listen_addresses() {
} else {
addresses = ['4873'];
}
addresses = addresses.map(function(addr) {
let parsed_addr = Utils.parse_address(addr);
@ -93,12 +100,17 @@ function get_listen_addresses() {
return addresses;
}
/**
* Trigger the server after configuration has been loaded.
*/
function afterConfigLoad() {
if (!config.self_path) config.self_path = Path.resolve(config_path);
if (!config.https) config.https = {enable: false};
let app = server(config);
if (!config.self_path) {
config.self_path = Path.resolve(config_path);
}
if (!config.https) {
config.https = {enable: false};
}
const app = server(config);
get_listen_addresses().forEach(function(addr) {
let webServer;
@ -173,7 +185,7 @@ function afterConfigLoad() {
}
process.on('uncaughtException', function(err) {
logger.logger.fatal( {err: err}
, 'uncaught exception, please report this\n@{err.stack}' );
logger.logger.fatal( {err: err},
'uncaught exception, please report this\n@{err.stack}' );
process.exit(255);
});

View file

@ -8,8 +8,9 @@ const Utils = require('./utils');
const pkgJSON = require('../package.json');
/**
*
* @param {*} x
* Match the level based on buyan severity scale
* @param {*} x severity level
* @return {String} security level
*/
function getlvl(x) {
switch(true) {
@ -23,11 +24,18 @@ function getlvl(x) {
}
}
module.exports.setup = function(logs) {
/**
* Setup the Buyan logger
* @param {*} logs list of log configuration
*/
function setup(logs) {
let streams = [];
if (logs == null) logs = [{type: 'stdout', format: 'pretty', level: 'http'}];
if (logs == null) {
logs = [{type: 'stdout', format: 'pretty', level: 'http'}];
}
logs.forEach(function(target) {
// create a stream for each log configuration
const stream = new Stream();
stream.writable = true;
@ -74,7 +82,8 @@ module.exports.setup = function(logs) {
});
});
let logger = new Logger({
// buyan default configuration
const logger = new Logger({
name: pkgJSON.name,
streams: streams,
serializers: {
@ -85,14 +94,14 @@ module.exports.setup = function(logs) {
});
module.exports.logger = logger;
};
}
// adopted from socket.io
// this part was converted to coffee-script and back again over the years,
// so it might look weird
// level to color
let levels = {
const levels = {
fatal: chalk.red,
error: chalk.red,
warn: chalk.yellow,
@ -104,27 +113,35 @@ let levels = {
let max = 0;
for (let l in levels) {
max = Math.max(max, l.length);
if (Object.prototype.hasOwnProperty.call(levels, l)) {
max = Math.max(max, l.length);
}
}
/**
*
* @param {*} str
* Apply whitespaces based on the length
* @param {*} str the log message
* @return {String}
*/
function pad(str) {
if (str.length < max) return str + ' '.repeat(max - str.length);
if (str.length < max) {
return str + ' '.repeat(max - str.length);
}
return str;
}
/**
* Build a string
* Apply colors to a string based on level parameters.
* @param {*} type
* @param {*} msg
* @param {*} obj
* @param {*} colors
* @return {String}
*/
function print(type, msg, obj, colors) {
if (typeof type === 'number') type = getlvl(type);
if (typeof type === 'number') {
type = getlvl(type);
}
let finalmsg = msg.replace(/@{(!?[$A-Za-z_][$0-9A-Za-z\._]*)}/g, function(_, name) {
let str = obj;
let is_error;
@ -168,11 +185,12 @@ function print(type, msg, obj, colors) {
default: '---',
}];
let sub = subsystems[colors ? 0 : 1][obj.sub] || subsystems[+!colors].default;
const sub = subsystems[colors ? 0 : 1][obj.sub] || subsystems[+!colors].default;
if (colors) {
// return ' \033[' + levels[type] + 'm' + (pad(type)) + '\033[39m ' + sub + ' ' + finalmsg
return ` ${levels[type]((pad(type)))}${chalk.white(`${sub} ${finalmsg}`)}`;
} else {
return ` ${(pad(type))}${sub} ${finalmsg}`;
}
}
module.exports.setup = setup;

View file

@ -26,9 +26,11 @@ class Storage {
// Proxy and Local classes should have similar API interfaces
this.uplinks = {};
for (let p in config.uplinks) {
// instance for each up-link definition
this.uplinks[p] = new Proxy(config.uplinks[p], config);
this.uplinks[p].upname = p;
if (Object.prototype.hasOwnProperty.call(config.uplinks, p)) {
// instance for each up-link definition
this.uplinks[p] = new Proxy(config.uplinks[p], config);
this.uplinks[p].upname = p;
}
}
// an instance for local storage
this.local = new Local(config);
@ -254,14 +256,14 @@ class Storage {
}
}
if (uplink == null) {
uplink = Proxy({
uplink = new Proxy({
url: file.url,
_autogenerated: true,
}, self.config);
}
let savestream = self.local.add_tarball(name, filename);
var on_open = function() {
let on_open = function() {
on_open = function() {}; // prevent it from being called twice
let rstream2 = uplink.get_url(file.url);
rstream2.on('error', function(err) {
@ -533,14 +535,14 @@ class Storage {
static _merge_versions(local, up, config) {
// copy new versions to a cache
// NOTE: if a certain version was updated, we can't refresh it reliably
for (var i in up.versions) {
for (let i in up.versions) {
if (local.versions[i] == null) {
local.versions[i] = up.versions[i];
}
}
// refresh dist-tags
for (var i in up['dist-tags']) {
for (let i in up['dist-tags']) {
if (local['dist-tags'][i] !== up['dist-tags'][i]) {
local['dist-tags'][i] = up['dist-tags'][i];
if (i === 'latest') {