From cc41b0acaf904153f9a213c624b004d71dca0f24 Mon Sep 17 00:00:00 2001 From: yairhaimo Date: Fri, 7 Aug 2015 12:56:21 +0300 Subject: [PATCH 1/2] add the ability to get a json list of all local packages --- lib/index-web.js | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/lib/index-web.js b/lib/index-web.js index caef8f99..53f025e3 100644 --- a/lib/index-web.js +++ b/lib/index-web.js @@ -40,31 +40,46 @@ module.exports = function(config, auth, storage) { Handlebars.registerPartial('entry', fs.readFileSync(require.resolve('./GUI/entry.hbs'), 'utf8')) var template = Handlebars.compile(fs.readFileSync(require.resolve('./GUI/index.hbs'), 'utf8')) } - app.get('/', function(req, res, next) { - var base = config.url_prefix - ? config.url_prefix.replace(/\/$/, '') - : req.protocol + '://' + req.get('host') - res.setHeader('Content-Type', 'text/html') + // returns an object of local packages + function get_library(user, cb) { storage.get_local(function(err, packages) { if (err) throw err // that function shouldn't produce any async.filterSeries(packages, function(package, cb) { - auth.allow_access(package.name, req.remote_user, function(err, allowed) { + auth.allow_access(package.name, user, function(err, allowed) { setImmediate(function () { cb(!err && allowed) }) }) - }, function(packages) { - next(template({ - name: config.web && config.web.title ? config.web.title : 'Sinopia', - packages: packages, - baseUrl: base, - username: req.remote_user.name, - })) - }) + }, cb); }) + } + + app.get('/-/library', function(req, res, next) { + get_library(req.remote_user, function(packages) { + next(packages); + }); + }); + + app.get('/', function(req, res, next) { + var base = config.url_prefix + ? config.url_prefix.replace(/\/$/, '') + : req.protocol + '://' + req.get('host') + + res.setHeader('Content-Type', 'text/html') + + get_library(req.remote_user, function(packages) { + next(template({ + name: config.web && config.web.title ? config.web.title : 'Sinopia', + packages: packages, + baseUrl: base, + username: req.remote_user.name, + })); + }); + }) + // Static app.get('/-/static/:filename', function(req, res, next) { var file = __dirname + '/static/' + req.params.filename @@ -143,4 +158,3 @@ module.exports = function(config, auth, storage) { }) return app } - From b842695b0cfe4d9f9580fa3618f653e8e1cd9f37 Mon Sep 17 00:00:00 2001 From: yairhaimo Date: Fri, 7 Aug 2015 17:26:02 +0300 Subject: [PATCH 2/2] /-/library/all gets all versions --- lib/index-web.js | 15 +++++++++------ lib/storage.js | 27 ++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/lib/index-web.js b/lib/index-web.js index 53f025e3..b25233d9 100644 --- a/lib/index-web.js +++ b/lib/index-web.js @@ -42,8 +42,10 @@ module.exports = function(config, auth, storage) { } // returns an object of local packages - function get_library(user, cb) { - storage.get_local(function(err, packages) { + function get_library(user, cb, getAllVersions) { + getAllVersions = getAllVersions || false; + var getFn = getAllVersions ? storage.get_local_all_versions.bind(storage) : storage.get_local.bind(storage); + getFn(function(err, packages) { if (err) throw err // that function shouldn't produce any async.filterSeries(packages, function(package, cb) { auth.allow_access(package.name, user, function(err, allowed) { @@ -55,10 +57,11 @@ module.exports = function(config, auth, storage) { }) } - app.get('/-/library', function(req, res, next) { - get_library(req.remote_user, function(packages) { - next(packages); - }); + app.get('/-/library/:all?', function(req, res, next) { + var all = req.params.all || false; + get_library(req.remote_user, function(packages) { + next(packages); + }, all); }); app.get('/', function(req, res, next) { diff --git a/lib/storage.js b/lib/storage.js index 3daace97..8c164f62 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -407,6 +407,32 @@ Storage.prototype.get_local = function(callback) { callback(null, []) } } +//TODO: DRY +Storage.prototype.get_local_all_versions = function(callback) { + var self = this + var locals = this.config.localList.get() + var packages = [] + + var getPackage = function(i) { + self.local.get_package(locals[i], function(err, info) { + if (!err) { + packages.push(info); + } + + if (i >= locals.length - 1) { + callback(null, packages) + } else { + getPackage(i + 1) + } + }) + } + + if (locals.length) { + getPackage(0) + } else { + callback(null, []) + } +} // function fetches package information from uplinks and synchronizes it with local data // if package is available locally, it MUST be provided in pkginfo @@ -528,4 +554,3 @@ Storage._merge_versions = function(local, up, config) { } } } -