Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #67 from fengmk2/issue58-sync-button
Browse files Browse the repository at this point in the history
Issue58 sync button
  • Loading branch information
fengmk2 committed Dec 11, 2013
2 parents c26e2a5 + a53de7d commit 24e7de4
Show file tree
Hide file tree
Showing 17 changed files with 313 additions and 114 deletions.
2 changes: 1 addition & 1 deletion controllers/registry/common.js → controllers/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

var crypto = require('crypto');
var path = require('path');
var config = require('../../config');
var config = require('../config');

exports.getTarballFilepath = function (filename) {
// ensure download file path unique
Expand Down
73 changes: 4 additions & 69 deletions controllers/registry/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ var config = require('../../config');
var Module = require('../../proxy/module');
var Total = require('../../proxy/total');
var nfs = require('../../common/nfs');
var npm = require('../../proxy/npm');
var common = require('./common');
var common = require('../common');
var Log = require('../../proxy/module_log');
var DownloadTotal = require('../../proxy/download');
var SyncModuleWorker = require('./sync_module_worker');
var SyncModuleWorker = require('../sync_module_worker');
var logger = require('../../common/logger');
var semver = require('semver');

Expand All @@ -51,7 +50,7 @@ exports.show = function (req, res, next) {
return next();
}
var username = (req.session && req.session.username) || 'anonymous';
return _sync(name, username, function (err, result) {
return SyncModuleWorker.sync(name, username, function (err, result) {
if (err) {
return next(err);
}
Expand Down Expand Up @@ -144,7 +143,7 @@ exports.get = function (req, res, next) {
}

var username = (req.session && req.session.username) || 'anonymous';
_sync(name, username, function (err, result) {
SyncModuleWorker.sync(name, username, function (err, result) {
if (err) {
return next(err);
}
Expand Down Expand Up @@ -563,70 +562,6 @@ exports.removeAll = function (req, res, next) {
});
};

function _sync(name, username, callback) {
npm.get(name, function (err, pkg, response) {
if (err) {
return callback(err);
}
if (!pkg || !pkg._rev) {
return callback(null, {
ok: false,
statusCode: response.statusCode,
pkg: pkg
});
}
Log.create({name: name, username: username}, function (err, result) {
if (err) {
return callback(err);
}
var worker = new SyncModuleWorker({
logId: result.id,
name: name,
username: username,
});
worker.start();
callback(null, {
ok: true,
logId: result.id,
pkg: pkg
});
});
});
}

exports.sync = function (req, res, next) {
var username = req.session.name;
var name = req.params.name;
_sync(name, username, function (err, result) {
if (err) {
return next(err);
}
if (!result.ok) {
return res.json(result.statusCode, result.pkg);
}
res.json(201, {
ok: true,
logId: result.logId
});
});
};

exports.getSyncLog = function (req, res, next) {
var logId = req.params.id;
var name = req.params.name;
var offset = Number(req.query.offset) || 0;
Log.get(logId, function (err, row) {
if (err || !row) {
return next(err);
}
var log = row.log.trim();
if (offset > 0) {
log = log.split('\n').slice(offset).join('\n');
}
res.json(200, {ok: true, log: log});
});
};

function parseModsForList(mods, req) {
var results = {
_updated: Date.now()
Expand Down
21 changes: 0 additions & 21 deletions controllers/registry/tag.js

This file was deleted.

50 changes: 50 additions & 0 deletions controllers/sync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**!
* cnpmjs.org - controllers/download.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <[email protected]> (http://deadhorse.me)
*/

'use strict';

/**
* Module dependencies.
*/
var Log = require('../proxy/module_log');
var SyncModuleWorker = require('./sync_module_worker');

exports.sync = function (req, res, next) {
var username = req.session.name || 'anonymous';
var name = req.params.name;
SyncModuleWorker.sync(name, username, function (err, result) {
if (err) {
return next(err);
}
if (!result.ok) {
return res.json(result.statusCode, result.pkg);
}
res.json(201, {
ok: true,
logId: result.logId
});
});
};

exports.getSyncLog = function (req, res, next) {
var logId = req.params.id;
var name = req.params.name;
var offset = Number(req.query.offset) || 0;
Log.get(logId, function (err, row) {
if (err || !row) {
return next(err);
}
var log = row.log.trim();
if (offset > 0) {
log = log.split('\n').slice(offset).join('\n');
}
res.json(200, {ok: true, log: log});
});
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**!
* cnpmjs.org - controllers/registry/sync_module_worker.js
* cnpmjs.org - controllers/sync_module_worker.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
Expand All @@ -23,11 +23,11 @@ var crypto = require('crypto');
var eventproxy = require('eventproxy');
var urllib = require('urllib');
var utility = require('utility');
var nfs = require('../../common/nfs');
var npm = require('../../proxy/npm');
var nfs = require('../common/nfs');
var npm = require('../proxy/npm');
var common = require('./common');
var Module = require('../../proxy/module');
var Log = require('../../proxy/module_log');
var Module = require('../proxy/module');
var Log = require('../proxy/module_log');
var ms = require('ms');

function SyncModuleWorker(options) {
Expand Down Expand Up @@ -369,3 +369,34 @@ SyncModuleWorker.prototype._syncOneVersion = function (versionIndex, sourcePacka
}));
});
};

SyncModuleWorker.sync = function (name, username, callback) {
npm.get(name, function (err, pkg, response) {
if (err) {
return callback(err);
}
if (!pkg || !pkg._rev) {
return callback(null, {
ok: false,
statusCode: response.statusCode,
pkg: pkg
});
}
Log.create({name: name, username: username}, function (err, result) {
if (err) {
return callback(err);
}
var worker = new SyncModuleWorker({
logId: result.id,
name: name,
username: username,
});
worker.start();
callback(null, {
ok: true,
logId: result.id,
pkg: pkg
});
});
});
};
8 changes: 8 additions & 0 deletions controllers/web/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ var gravatar = require('gravatar');
var humanize = require('humanize-number');
var Module = require('../../proxy/module');
var down = require('../download');
var sync = require('../sync');
var Log = require('../../proxy/module_log');

exports.display = function (req, res, next) {
var params = req.params;
Expand Down Expand Up @@ -90,6 +92,12 @@ exports.search = function (req, res, next) {
});
};

exports.displaySync = function (req, res, next) {
res.render('sync', {
name: req.params.name
});
};

function setLicense(pkg) {
var license;
license = pkg.license || pkg.licenses || pkg.licence || pkg.licences;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
"coveralls": "*",
"mocha-lcov-reporter": "*",
"mocha": "*",
"mm": "*"
"mm": "*",
"pedding": "*"
},
"homepage": "https://github.com/fengmk2/cnpmjs.org",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion proxy/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function request(url, callback) {
url = config.sourceNpmRegistry + url;
var options = {
dataType: 'json',
timeout: 10000,
timeout: 10000
};
urllib.request(url, options, callback);
}
Expand Down
6 changes: 3 additions & 3 deletions routes/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ var publishable = require('../middleware/publishable');
var syncByInstall = require('../middleware/sync_by_install');
var home = require('../controllers/registry/home');
var mod = require('../controllers/registry/module');
var tag = require('../controllers/registry/tag');
var user = require('../controllers/registry/user');
var sync = require('../controllers/sync');

function routes(app) {
app.get('/', home.show);
Expand All @@ -40,8 +40,8 @@ function routes(app) {
app.put('/:name', [login, publishable], mod.add);

// sync from source npm
app.put('/:name/sync', [login], mod.sync);
app.get('/:name/sync/log/:id', mod.getSyncLog);
app.put('/:name/sync', sync.sync);
app.get('/:name/sync/log/:id', sync.getSyncLog);

app.get('/:name/download/:filename', mod.download);

Expand Down
5 changes: 5 additions & 0 deletions routes/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@

var pkg = require('../controllers/web/package');
var user = require('../controllers/web/user');
var sync = require('../controllers/sync');

function routes(app) {
app.get('/package/:name', pkg.display);
app.get('/package/:name/:version', pkg.display);
app.get('/browse/keyword/:word', pkg.search);

app.get('/~:name', user.display);

app.get('/sync/:name', pkg.displaySync);
app.put('/sync/:name', sync.sync);
app.get('/sync/:name/log/:id', sync.getSyncLog);
}

module.exports = routes;
2 changes: 1 addition & 1 deletion test/controllers/registry/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ describe('controllers/registry/module.test.js', function () {
res.body.time.should.eql({});
res.body['dist-tags'].should.eql({});
lastRev = res.body._rev;
console.log('lastRev: %s', lastRev);
// console.log('lastRev: %s', lastRev);
done();
});
});
Expand Down
Loading

0 comments on commit 24e7de4

Please sign in to comment.