diff --git a/lib/config.js b/lib/config.js index 56b78822e..8d55b3ca2 100644 --- a/lib/config.js +++ b/lib/config.js @@ -7,8 +7,6 @@ const assert = require('assert'); const Crypto = require('crypto'); const Error = require('http-errors'); const minimatch = require('minimatch'); -const Path = require('path'); -const LocalData = require('./local-data'); const Utils = require('./utils'); const pkginfo = require('pkginfo')(module); // eslint-disable-line no-unused-vars const pkgVersion = module.exports.version; @@ -93,24 +91,6 @@ class Config { assert.equal(typeof(config), 'object', 'CONFIG: it doesn\'t look like a valid config file'); assert(self.storage, 'CONFIG: storage path not defined'); - // local data handler is linked with the configuration handler - self.localList = new LocalData( - Path.join( - Path.resolve(Path.dirname(self.self_path || ''), self.storage), - // FUTURE: the database might be parameterizable from config.yaml - '.sinopia-db.json' - ) - ); - // it generates a secret key - // FUTURE: this might be an external secret key, perhaps whitin config file? - if (!self.secret) { - self.secret = self.localList.data.secret; - if (!self.secret) { - self.secret = Crypto.pseudoRandomBytes(32).toString('hex'); - self.localList.data.secret = self.secret; - self.localList.sync(); - } - } const users = { 'all': true, diff --git a/lib/local-storage.js b/lib/local-storage.js index 3c5ca0d63..80660d5d2 100644 --- a/lib/local-storage.js +++ b/lib/local-storage.js @@ -39,9 +39,11 @@ class Storage { /** * Constructor * @param {Object} config config list of properties + * @param {LocalData} localList reference */ - constructor(config) { + constructor(config, localList) { this.config = config; + this.localList = localList; this.logger = Logger.logger.child({sub: 'fs'}); } @@ -135,7 +137,7 @@ class Storage { }); Search.remove(name); - this.config.localList.remove(name); + this.localList.remove(name); } /** @@ -285,7 +287,7 @@ class Storage { data.versions[version] = metadata; Utils.tag_version(data, version, tag); - this.config.localList.add(name); + this.localList.add(name); cb(); }, callback); } diff --git a/lib/search.js b/lib/search.js index 51cba12b1..6fc442b43 100644 --- a/lib/search.js +++ b/lib/search.js @@ -30,7 +30,7 @@ class Search { */ query(q) { return q === '*' - ? this.storage.config.localList.get().map( function( pkg ) { + ? this.storage.localList.get().map( function( pkg ) { return {ref: pkg, score: 1}; }) : this.index.search(q); } diff --git a/lib/storage.js b/lib/storage.js index e4a9066f4..7f636d764 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -2,10 +2,13 @@ const assert = require('assert'); const async = require('async'); +const path = require('path'); const Error = require('http-errors'); const semver = require('semver'); +const Crypto = require('crypto'); const Stream = require('stream'); const Local = require('./local-storage'); +const LocalData = require('./local-data'); const Logger = require('./logger'); const MyStreams = require('./streams'); const Proxy = require('./up-storage'); @@ -33,8 +36,24 @@ class Storage { this.uplinks[p].upname = p; } } + // local data handler is linked with the configuration handler + this.localList = new LocalData(path.join(path.resolve(path.dirname(config.self_path || ''), config.storage), + // FUTURE: the database might be parameterizable from config.yaml + '.sinopia-db.json' + ) + ); + // it generates a secret key + // FUTURE: this might be an external secret key, perhaps whitin config file? + if (!config.secret) { + config.secret = this.localList.data.secret; + if (!config.secret) { + config.secret = Crypto.pseudoRandomBytes(32).toString('hex'); + this.localList.data.secret = config.secret; + this.localList.sync(); + } + } // an instance for local storage - this.local = new Local(config); + this.local = new Local(config, this.localList); this.logger = Logger.logger.child(); } @@ -438,7 +457,7 @@ class Storage { */ get_local(callback) { let self = this; - let locals = this.config.localList.get(); + let locals = this.localList.get(); let packages = []; const getPackage = function(i) {