mirror of
https://github.com/verdaccio/verdaccio.git
synced 2024-12-30 22:34:10 -05:00
added utils.is_object function for convenience
This commit is contained in:
parent
4c2c4b87c2
commit
61658cfbdc
8 changed files with 29 additions and 27 deletions
|
@ -1,6 +1,7 @@
|
||||||
var assert = require('assert');
|
var assert = require('assert')
|
||||||
var crypto = require('crypto');
|
, crypto = require('crypto')
|
||||||
var minimatch = require('minimatch');
|
, minimatch = require('minimatch')
|
||||||
|
, utils = require('./utils')
|
||||||
|
|
||||||
// [[a, [b, c]], d] -> [a, b, c, d]
|
// [[a, [b, c]], d] -> [a, b, c, d]
|
||||||
function flatten(array) {
|
function flatten(array) {
|
||||||
|
@ -37,10 +38,7 @@ function Config(config) {
|
||||||
|
|
||||||
['users', 'uplinks', 'packages'].forEach(function(x) {
|
['users', 'uplinks', 'packages'].forEach(function(x) {
|
||||||
if (this[x] == null) this[x] = {};
|
if (this[x] == null) this[x] = {};
|
||||||
assert(
|
assert(utils.is_object(this[x]), 'CONFIG: bad "'+x+'" value (object expected)');
|
||||||
typeof(this[x]) === 'object' &&
|
|
||||||
!Array.isArray(this[x])
|
|
||||||
, 'CONFIG: bad "'+x+'" value (object expected)');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
for (var i in this.users) check_user_or_uplink(i);
|
for (var i in this.users) check_user_or_uplink(i);
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
var util = require('util');
|
var util = require('util')
|
||||||
|
, utils = require('./utils')
|
||||||
|
|
||||||
function parse_error_params(params, status, msg) {
|
function parse_error_params(params, status, msg) {
|
||||||
if (typeof(params) === 'string') {
|
if (typeof(params) === 'string') {
|
||||||
|
@ -11,7 +12,7 @@ function parse_error_params(params, status, msg) {
|
||||||
msg: msg,
|
msg: msg,
|
||||||
status: params,
|
status: params,
|
||||||
};
|
};
|
||||||
} else if (typeof(params) === 'object' && params != null) {
|
} else if (utils.is_object(params)) {
|
||||||
if (params.msg == null) params.msg = msg;
|
if (params.msg == null) params.msg = msg;
|
||||||
if (params.status == null) params.status = status;
|
if (params.status == null) params.status = status;
|
||||||
return params;
|
return params;
|
||||||
|
|
|
@ -174,7 +174,7 @@ module.exports = function(config_hash) {
|
||||||
if (req.params._rev != null && req.params._rev != '-rev') return next('route');
|
if (req.params._rev != null && req.params._rev != '-rev') return next('route');
|
||||||
var name = req.params.package;
|
var name = req.params.package;
|
||||||
|
|
||||||
if (Object.keys(req.body).length == 1 && typeof(req.body.users) === 'object') {
|
if (Object.keys(req.body).length == 1 && utils.is_object(req.body.users)) {
|
||||||
return next(new UError({
|
return next(new UError({
|
||||||
// 501 status is more meaningful, but npm doesn't show error message for 5xx
|
// 501 status is more meaningful, but npm doesn't show error message for 5xx
|
||||||
status: 404,
|
status: 404,
|
||||||
|
|
|
@ -153,6 +153,7 @@ Storage.prototype.add_version = function(name, version, metadata, tag, callback)
|
||||||
msg: 'this version already present'
|
msg: 'this version already present'
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
data.versions[version] = metadata
|
data.versions[version] = metadata
|
||||||
data['dist-tags'][tag] = version
|
data['dist-tags'][tag] = version
|
||||||
cb()
|
cb()
|
||||||
|
@ -345,7 +346,7 @@ Storage.prototype.update_package = function(name, updateFn, _callback) {
|
||||||
|
|
||||||
Storage.prototype._normalize_package = function(pkg) {
|
Storage.prototype._normalize_package = function(pkg) {
|
||||||
['versions', 'dist-tags', '_distfiles', '_attachments'].forEach(function(key) {
|
['versions', 'dist-tags', '_distfiles', '_attachments'].forEach(function(key) {
|
||||||
if (typeof(pkg[key]) != 'object' || pkg[key] == null) pkg[key] = {}
|
if (!utils.is_object(pkg[key])) pkg[key] = {}
|
||||||
});
|
});
|
||||||
if (typeof(pkg._rev) !== 'string') pkg._rev = '0-0000000000000000'
|
if (typeof(pkg._rev) !== 'string') pkg._rev = '0-0000000000000000'
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
var Logger = require('bunyan');
|
var Logger = require('bunyan')
|
||||||
var Stream = require('stream');
|
, Stream = require('stream')
|
||||||
|
, utils = require('./utils')
|
||||||
|
|
||||||
function getlvl(x) {
|
function getlvl(x) {
|
||||||
if (x < 15) {
|
if (x < 15) {
|
||||||
|
@ -123,7 +124,7 @@ function print(type, msg, obj, colors) {
|
||||||
var _ref = name.split('.');
|
var _ref = name.split('.');
|
||||||
for (var _i = 0; _i < _ref.length; _i++) {
|
for (var _i = 0; _i < _ref.length; _i++) {
|
||||||
var id = _ref[_i];
|
var id = _ref[_i];
|
||||||
if (typeof str === 'object') {
|
if (utils.is_object(str) || Array.isArray(str)) {
|
||||||
str = str[id];
|
str = str[id];
|
||||||
} else {
|
} else {
|
||||||
str = void 0;
|
str = void 0;
|
||||||
|
|
|
@ -29,7 +29,7 @@ module.exports.media = function media(expect) {
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.expect_json = function expect_json(req, res, next) {
|
module.exports.expect_json = function expect_json(req, res, next) {
|
||||||
if (typeof(req.body) !== 'object' || req.body === null) {
|
if (!utils.is_object(req.body)) {
|
||||||
return next({
|
return next({
|
||||||
status: 400,
|
status: 400,
|
||||||
msg: 'can\'t parse incoming json',
|
msg: 'can\'t parse incoming json',
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
var URL = require('url');
|
var URL = require('url')
|
||||||
var request = require('request');
|
, request = require('request')
|
||||||
var UError = require('./error').UserError;
|
, UError = require('./error').UserError
|
||||||
var mystreams = require('./streams');
|
, mystreams = require('./streams')
|
||||||
var Logger = require('./logger');
|
, Logger = require('./logger')
|
||||||
|
, utils = require('./utils')
|
||||||
|
|
||||||
//
|
//
|
||||||
// Implements Storage interface
|
// Implements Storage interface
|
||||||
|
@ -47,7 +48,7 @@ Storage.prototype.request = function(options, cb) {
|
||||||
uri: uri,
|
uri: uri,
|
||||||
}, "making request: '@{method} @{uri}'");
|
}, "making request: '@{method} @{uri}'");
|
||||||
|
|
||||||
if (typeof(options.json) === 'object' && options.json != null) {
|
if (utils.is_object(options.json)) {
|
||||||
var json = JSON.stringify(options.json);
|
var json = JSON.stringify(options.json);
|
||||||
headers['content-type'] = headers['content-type'] || 'application/json';
|
headers['content-type'] = headers['content-type'] || 'application/json';
|
||||||
}
|
}
|
||||||
|
@ -70,7 +71,7 @@ Storage.prototype.request = function(options, cb) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof(body) === 'object' && body !== null) {
|
if (utils.is_object(body)) {
|
||||||
if (body.error) {
|
if (body.error) {
|
||||||
var error = body.error;
|
var error = body.error;
|
||||||
}
|
}
|
||||||
|
|
10
lib/utils.js
10
lib/utils.js
|
@ -18,19 +18,19 @@ module.exports.validate_name = function(name) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function is_object(obj) {
|
module.exports.is_object = function(obj) {
|
||||||
return typeof(obj) === 'object' && !Array.isArray(obj);
|
return typeof(obj) === 'object' && obj !== null && !Array.isArray(obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.validate_metadata = function(object, name) {
|
module.exports.validate_metadata = function(object, name) {
|
||||||
assert(is_object(object));
|
assert(module.exports.is_object(object));
|
||||||
assert.equal(object.name, name);
|
assert.equal(object.name, name);
|
||||||
|
|
||||||
if (!is_object(object['dist-tags'])) {
|
if (!module.exports.is_object(object['dist-tags'])) {
|
||||||
object['dist-tags'] = {};
|
object['dist-tags'] = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!is_object(object['versions'])) {
|
if (!module.exports.is_object(object['versions'])) {
|
||||||
object['versions'] = {};
|
object['versions'] = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue