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 #36 from fengmk2/issue31-install
Browse files Browse the repository at this point in the history
Issue31 install
  • Loading branch information
fengmk2 committed Dec 9, 2013
2 parents 2a9716a + b056fa5 commit a7dd6fd
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
TESTS = $(shell ls -S `find test -type f -name "*.test.js" -print`)
REPORTER = spec
REPORTER = tap
TIMEOUT = 20000
MOCHA_OPTS =

Expand Down
1 change: 1 addition & 0 deletions config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ var config = {
dead_horse: true,
cnpmjstest10: true,
},
syncByInstall: true
};

// load config/config.js, everything in config.js will cover the same key in index.js
Expand Down
106 changes: 80 additions & 26 deletions controllers/registry/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,22 @@ exports.show = function (req, res, next) {
Module.listByName(name, ep.done('rows'));

ep.all('tags', 'rows', function (tags, rows) {
//if module not exist in this registry,
//sync the module backend and return package info from official registry
if (rows.length === 0) {
return next();
if (!req.session.allowSync) {
return next();
}
var username = (req.session && req.session.username) || 'anonymous';
return _sync(name, username, function (err, result) {
if (err) {
return next(err);
}
if (!result.ok) {
return res.json(result.statusCode, result.pkg);
}
res.json(200, result.pkg);
});
}

var nextMod = rows[0];
Expand Down Expand Up @@ -107,25 +121,45 @@ exports.show = function (req, res, next) {
});
};

var VERSION_RE = /^\d+\.\d+\.\d+/;

exports.get = function (req, res, next) {
var name = req.params.name;
var version = req.params.version;
if (VERSION_RE.test(version)) {
Module.get(name, version, function (err, mod) {
if (err || !mod) {
return next(err);
}
res.json(mod.package);
});
return;
}

var tag = version;
Module.getByTag(name, tag, function (err, mod) {
if (err || !mod) {
return next(err);
var ep = eventproxy.create();
ep.fail(next);

//frist get by tag
Module.getByTag(name, version, ep.done(function (mod) {
if (mod) {
return res.json(mod.package);
}
ep.emit('notFoundByTag');
}));

ep.once('notFoundByTag', function () {
//then get by name
Module.get(name, version, ep.done('getByName'));
});

ep.once('getByName', function (mod) {
if (!mod) {
if (!req.session.allowSync) {
return next();
}
var username = (req.session && req.session.username) || 'anonymous';
return _sync(name, username, function (err, result) {
if (err) {
return next(err);
}
var pkg = result.pkg.versions[version];
if (!pkg) {
return res.json(404, {
error: 'not exist',
reason: 'version not found: ' + version
});
}
return res.json(pkg);
});
}
res.json(mod.package);
});
Expand Down Expand Up @@ -528,30 +562,50 @@ exports.removeAll = function (req, res, next) {
});
};

exports.sync = function (req, res, next) {
var username = req.session.name;
var name = req.params.name;

function _sync(name, username, callback) {
npm.get(name, function (err, pkg, response) {
if (err) {
return next(err);
return callback(err);
}

if (!pkg || !pkg._rev) {
return res.json(response.statusCode, pkg);
return callback(null, {
ok: false,
statusCode: response.statusCode,
pkg: pkg
});
}

Log.create({name: name, username: username}, function (err, result) {
if (err) {
return next(err);
return callback(err);
}
var worker = new SyncModuleWorker({
logId: result.id,
name: name,
username: username,
});
worker.start();
res.json(201, {ok: true, logId: result.id});
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
});
});
};
Expand Down
32 changes: 32 additions & 0 deletions middleware/sync_by_install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*!
* cnpmjs.org - middleware/sync_by_install.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
*/

'use strict';

/**
* Module dependencies.
*/

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

/**
* req.session.allowSync - allow sync triggle by cnpm install
*/
module.exports = function (req, res, next) {
if (!config.syncByInstall) {
return next();
}
// request not by node, consider it request from web
if (req.headers['user-agent'] && req.headers['user-agent'].indexOf('node') !== 0) {
return next();
}
req.session.allowSync = true;
next();
};
2 changes: 0 additions & 2 deletions proxy/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,3 @@ exports.get = function (name, callback) {
callback(null, data, res);
});
};


5 changes: 3 additions & 2 deletions routes/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

var login = require('../middleware/login');
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 pkg = require('../controllers/registry/package');
Expand All @@ -34,8 +35,8 @@ function routes(app) {
app.get('/-/short', mod.listAllModuleNames);

// module
app.get('/:name', mod.show);
app.get('/:name/:version', mod.get);
app.get('/:name', [syncByInstall], mod.show);
app.get('/:name/:version', [syncByInstall], mod.get);
// try to add module
app.put('/:name', [login, publishable], mod.add);

Expand Down

0 comments on commit a7dd6fd

Please sign in to comment.