Compare commits

...

16 Commits
0.2.2 ... 0.2.6

Author SHA1 Message Date
fengmk2
40301f260b Release 0.2.6 2014-01-13 17:15:32 +08:00
fengmk2
72dbde1906 Merge pull request #147 from cnpm/issue146-session-config
support custom session store
2014-01-13 01:13:56 -08:00
dead_horse
d68157faa8 support custom session store, fixed #146 2014-01-13 17:09:04 +08:00
fengmk2
c88991c028 Release 0.2.5 2014-01-13 12:00:47 +08:00
dead_horse
262abe8520 Merge pull request #144 from cnpm/download-stream
nfs download to a writeable stream.
2014-01-12 18:50:50 -08:00
fengmk2
9dd942df1a add download timeout and unit test 2014-01-13 10:44:55 +08:00
fengmk2
9ec552e08d use downloadStream() first 2014-01-13 09:48:02 +08:00
fengmk2
2420164a9d nfs download to a writeable stream.
nfs.download(key, writeStream, cb), should not create a tmp file,
and also add content-length, content-type headers.
2014-01-11 00:14:17 +08:00
fengmk2
822f2f6a4e Release 0.2.4 2014-01-10 15:59:56 +08:00
fengmk2
d423a987ae Merge pull request #143 from cnpm/issue142-module
set main script to  index.js, fixed #142
2014-01-09 23:59:07 -08:00
dead_horse
70cefd817e set main script to index.js, fixed #142 2014-01-10 15:52:32 +08:00
fengmk2
2606af24a1 Release 0.2.3 2014-01-10 15:15:59 +08:00
dead_horse
5b781cbda1 Merge pull request #140 from cnpm/package-display
dont show sync button on private package
2014-01-09 23:05:13 -08:00
fengmk2
1519b52a74 dont show sync button on private package 2014-01-10 14:38:23 +08:00
dead_horse
4b73f6e0a7 Merge pull request #139 from cnpm/sync-as-publish-with-no-deps
Sync package as publish with no deps. fixed #138
2014-01-09 21:09:33 -08:00
fengmk2
88bcb14a4f Sync package as publish with no deps. fixed #138
Let user sync their private packages from private registry to other.
Just use `$ cnpm sync --publish --no-deps private_pkg1 private_pkg2`.
2014-01-10 12:46:38 +08:00
14 changed files with 272 additions and 74 deletions

View File

@@ -1,4 +1,27 @@
0.2.6 / 2014-01-13
==================
* support custom session store, fixed #146
0.2.5 / 2014-01-13
==================
* add download timeout and unit test
* use downloadStream() first
* nfs download to a writeable stream.
0.2.4 / 2014-01-10
==================
* set main script to index.js, fixed #142
0.2.3 / 2014-01-10
==================
* Dont show sync button on private package
* Sync package as publish with no deps. fixed #138
0.2.2 / 2014-01-10
==================

View File

@@ -19,6 +19,8 @@ var config = require('../config');
var client = qn.create(config.qn);
exports._client = client;
/**
* Upload file
*

View File

@@ -33,7 +33,7 @@ if (config.debug) {
session = connect.session({
key: key,
secret: config.sessionSecret,
store: new RedisStore(config.redis),
store: config.sessionStore || new RedisStore(config.redis),
cookie: cookie,
});
}

View File

@@ -23,6 +23,9 @@ var utility = require('utility');
var eventproxy = require('eventproxy');
var Bagpipe = require('bagpipe');
var urlparse = require('url').parse;
var mime = require('mime');
var semver = require('semver');
var ms = require('ms');
var config = require('../../config');
var Module = require('../../proxy/module');
var Total = require('../../proxy/total');
@@ -32,7 +35,6 @@ var Log = require('../../proxy/module_log');
var DownloadTotal = require('../../proxy/download');
var SyncModuleWorker = require('../../proxy/sync_module_worker');
var logger = require('../../common/logger');
var semver = require('semver');
exports.show = function (req, res, next) {
var name = req.params.name;
@@ -167,6 +169,8 @@ exports.get = function (req, res, next) {
var _downloads = {};
var DOWNLOAD_TIMEOUT = ms('10m');
exports.download = function (req, res, next) {
var name = req.params.name;
var filename = req.params.filename;
@@ -181,9 +185,9 @@ exports.download = function (req, res, next) {
}
var dist = row.package.dist;
if (dist.key) {
return ep.emit('key', dist.key);
return ep.emit('key', dist);
} else {
return ep.emit('url', dist.tarball);
return ep.emit('url', dist);
}
ep.emit('nodist');
});
@@ -195,23 +199,46 @@ exports.download = function (req, res, next) {
ep.emit('url', nfs.url(common.getCDNKey(name, filename)));
});
ep.once('url', function (url) {
ep.once('url', function (dist) {
res.statusCode = 302;
res.setHeader('Location', url);
res.setHeader('Location', dist.tarball);
res.end();
_downloads[name] = (_downloads[name] || 0) + 1;
});
ep.once('key', function (key) {
if (!nfs.download) {
ep.once('key', function (dist) {
if (!nfs.downloadStream && !nfs.download) {
return next();
}
var tmpPath = path.join(config.uploadDir, utility.randomString() + key);
_downloads[name] = (_downloads[name] || 0) + 1;
if (typeof dist.size === 'number') {
res.setHeader('Content-Length', dist.size);
}
res.setHeader('Content-Type', mime.lookup(dist.key));
res.setHeader('Content-Disposition: attachment; filename="' + filename + '"');
res.setHeader('ETag', dist.shasum);
if (nfs.downloadStream) {
nfs.downloadStream(dist.key, res, {timeout: DOWNLOAD_TIMEOUT},
function (err) {
if (err) {
// TODO: just end or send error response?
return next(err);
}
});
return;
}
// use download file api
var tmpPath = path.join(config.uploadDir, utility.randomString() + dist.key);
function cleanup() {
fs.unlink(tmpPath, utility.noop);
}
nfs.download(key, tmpPath, function (err) {
nfs.download(dist.key, tmpPath, {timeout: DOWNLOAD_TIMEOUT},
function (err) {
if (err) {
cleanup();
return next(err);
@@ -220,7 +247,6 @@ exports.download = function (req, res, next) {
tarball.on('error', cleanup);
tarball.on('end', cleanup);
tarball.pipe(res);
_downloads[name] = (_downloads[name] || 0) + 1;
});
});
};

View File

@@ -13,13 +13,27 @@
/**
* Module dependencies.
*/
var Log = require('../proxy/module_log');
var SyncModuleWorker = require('../proxy/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) {
var publish = req.query.publish === 'true';
var noDep = req.query.nodeps === 'true';
if (publish && !req.session.isAdmin) {
return res.json(403, {
error: 'no_perms',
reason: 'Only admin can publish'
});
}
var options = {
publish: publish,
noDep: noDep,
};
SyncModuleWorker.sync(name, username, options, function (err, result) {
if (err) {
return next(err);
}

View File

@@ -24,46 +24,36 @@ var workerPath = path.join(__dirname, 'worker.js');
var childProcess = require('child_process');
var syncPath = path.join(__dirname, 'sync');
function start(customConfig) {
config.loadConfig(customConfig);
if (config.enableCluster) {
cluster.setupMaster({
exec: workerPath
});
if (config.enableCluster) {
cluster.setupMaster({
exec: workerPath
});
cluster.on('fork', function (worker) {
console.log('[%s] [worker:%d] new worker start', new Date(), worker.process.pid);
});
cluster.on('fork', function (worker) {
console.log('[%s] [worker:%d] new worker start', new Date(), worker.process.pid);
});
cluster.on('disconnect', function (worker) {
var w = cluster.fork();
console.error('[%s] [master:%s] wroker:%s disconnect! new worker:%s fork',
new Date(), process.pid, worker.process.pid, w.process.pid);
});
cluster.on('disconnect', function (worker) {
var w = cluster.fork();
console.error('[%s] [master:%s] wroker:%s disconnect! new worker:%s fork',
new Date(), process.pid, worker.process.pid, w.process.pid);
});
cluster.on('exit', function (worker, code, signal) {
var exitCode = worker.process.exitCode;
var err = new Error(util.format('worker %s died (code: %s, signal: %s)', worker.process.pid, exitCode, signal));
err.name = 'WorkerDiedError';
console.error(err);
});
cluster.on('exit', function (worker, code, signal) {
var exitCode = worker.process.exitCode;
var err = new Error(util.format('worker %s died (code: %s, signal: %s)', worker.process.pid, exitCode, signal));
err.name = 'WorkerDiedError';
console.error(err);
});
var numCPUs = require('os').cpus().length;
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
childProcess.fork(syncPath);
} else {
require(workerPath);
require(syncPath);
var numCPUs = require('os').cpus().length;
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
}
if (!module.parent) {
start();
childProcess.fork(syncPath);
} else {
module.exports = start;
require(workerPath);
require(syncPath);
}

30
index.js Normal file
View File

@@ -0,0 +1,30 @@
/*!
* cnpmjs.org - index.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');
exports.loadConfig = config.loadConfig;
exports.config = config;
exports.startWorker = function (customConfig) {
config.loadConfig(customConfig);
require('./worker');
};
exports.startSync = function (customConfig) {
config.loadConfig(customConfig);
require('./sync');
};

View File

@@ -1,8 +1,8 @@
{
"name": "cnpmjs.org",
"version": "0.2.2",
"version": "0.2.6",
"description": "Private npm registry and web",
"main": "dispatch.js",
"main": "index.js",
"scripts": {
"test": "make test-all",
"start": "./bin/nodejsctl start && cp History.md docs/web/history.md",
@@ -36,6 +36,7 @@
"logfilestream": "0.1.0",
"marked": "0.3.0",
"microtime": "0.5.1",
"mime": "1.2.11",
"mkdirp": "0.3.5",
"moment": "2.5.0",
"ms": "0.6.2",

View File

@@ -47,6 +47,8 @@ function SyncModuleWorker(options) {
this.username = options.username;
this.concurrency = options.concurrency || 1;
this._publish = options.publish; // _publish_on_cnpm
this.syncingNames = {};
this.nameMap = {};
this.names.forEach(function (name) {
@@ -82,7 +84,8 @@ SyncModuleWorker.prototype.log = function (format, arg1, arg2) {
};
SyncModuleWorker.prototype.start = function () {
this.log('user: %s, sync %s worker start, %d concurrency.', this.username, this.names[0], this.concurrency);
this.log('user: %s, sync %s worker start, %d concurrency, nodeps: %s, publish: %s',
this.username, this.names[0], this.concurrency, this.noDep, this._publish);
for (var i = 0; i < this.concurrency; i++) {
this.next(i);
}
@@ -122,11 +125,11 @@ SyncModuleWorker.prototype.next = function (concurrencyId) {
return that.next(concurrencyId);
}
that.log('[c#%d] [%s] start...', concurrencyId, pkg.name);
that._sync(pkg, function (err, versions) {
that.log('[c#%d] [%s] start...', concurrencyId, name);
that._sync(name, pkg, function (err, versions) {
delete that.syncingNames[name];
if (err) {
that.fails.push(pkg.name);
that.fails.push(name);
that.log('[error] [%s] sync error: %s', name, err.stack);
return that.next(concurrencyId);
}
@@ -139,9 +142,8 @@ SyncModuleWorker.prototype.next = function (concurrencyId) {
});
};
SyncModuleWorker.prototype._sync = function (pkg, callback) {
var username = this.name;
var name = pkg.name;
SyncModuleWorker.prototype._sync = function (name, pkg, callback) {
var username = this.username;
var that = this;
var ep = eventproxy.create();
ep.fail(callback);
@@ -159,7 +161,7 @@ SyncModuleWorker.prototype._sync = function (pkg, callback) {
if (r.package && r.package._publish_on_cnpm) {
// publish on cnpm, dont sync this version package
that.log(' [%s] publish on local cnpm, don\'t sync.', pkg.name);
that.log(' [%s] publish on local cnpm, don\'t sync', name);
ep.unbind();
callback(null, []);
return;
@@ -200,7 +202,7 @@ SyncModuleWorker.prototype._sync = function (pkg, callback) {
versionNames = Object.keys(pkg.versions);
}
if (versionNames.length === 0) {
that.log(' [%s] no times and no versions, hasModules: %s', pkg.name, hasModules);
that.log(' [%s] no times and no versions, hasModules: %s', name, hasModules);
if (!hasModules) {
// save a next module
var maintainer = pkg.maintainers && pkg.maintainers[0];
@@ -211,11 +213,11 @@ SyncModuleWorker.prototype._sync = function (pkg, callback) {
maintainer = '-';
}
var nextMod = {
name: pkg.name,
name: name,
version: 'next',
author: maintainer,
package: {
name: pkg.name,
name: name,
version: 'next',
description: pkg.description || '',
readme: pkg.readme || '',
@@ -225,7 +227,7 @@ SyncModuleWorker.prototype._sync = function (pkg, callback) {
},
};
Module.add(nextMod, function (err, result) {
that.log(' [%s] save next module, %j, error: %s', pkg.name, result, err);
that.log(' [%s] save next module, %j, error: %s', name, result, err);
});
}
}
@@ -250,7 +252,7 @@ SyncModuleWorker.prototype._sync = function (pkg, callback) {
// * shasum make sure equal
if ((version.publish_time === exists.publish_time) || (!version.publish_time && exists.publish_time)) {
debug(' [%s] %s publish_time equal: %s, %s',
pkg.name, version.version, version.publish_time, exists.publish_time);
name, version.version, version.publish_time, exists.publish_time);
// * publish_time make sure equal
if (exists.description === null && version.description) {
// * make sure description exists
@@ -274,7 +276,7 @@ SyncModuleWorker.prototype._sync = function (pkg, callback) {
}
if (versions.length === 0) {
that.log(' [%s] all versions are exists', pkg.name);
that.log(' [%s] all versions are exists', name);
return ep.emit('syncDone');
}
@@ -282,7 +284,7 @@ SyncModuleWorker.prototype._sync = function (pkg, callback) {
return a.publish_time - b.publish_time;
});
missingVersions = versions;
that.log(' [%s] %d versions', pkg.name, versions.length);
that.log(' [%s] %d versions', name, versions.length);
ep.emit('syncModule', missingVersions.shift());
});
@@ -311,7 +313,7 @@ SyncModuleWorker.prototype._sync = function (pkg, callback) {
return ep.emit('descriptionDone');
}
that.log(' [%s] saving %d descriptions', pkg.name, missingDescriptions.length);
that.log(' [%s] saving %d descriptions', name, missingDescriptions.length);
missingDescriptions.forEach(function (item) {
Module.updateDescription(item.id, item.description, function (err, result) {
if (err) {
@@ -332,10 +334,10 @@ SyncModuleWorker.prototype._sync = function (pkg, callback) {
return ep.emit('tagDone');
}
that.log(' [%s] adding %d tags', pkg.name, missingTags.length);
that.log(' [%s] adding %d tags', name, missingTags.length);
// sync tags
missingTags.forEach(function (item) {
Module.addTag(pkg.name, item[0], item[1], ep.done(function (result) {
Module.addTag(name, item[0], item[1], ep.done(function (result) {
that.log(' added tag %s:%s, module_id: %s', item[0], item[1], result && result.module_id);
ep.emit('addTag');
}));
@@ -371,8 +373,9 @@ SyncModuleWorker.prototype._syncOneVersion = function (versionIndex, sourcePacka
callback(err);
});
that.log(' [%s:%d] syncing, version: %s, dist: %j',
sourcePackage.name, versionIndex, sourcePackage.version, sourcePackage.dist);
that.log(' [%s:%d] syncing, version: %s, dist: %j, no deps: %s, publish on cnpm: %s',
sourcePackage.name, versionIndex, sourcePackage.version,
sourcePackage.dist, that.noDep, that._publish);
if (!that.noDep) {
for (var k in sourcePackage.dependencies) {
that.add(k);
@@ -442,6 +445,12 @@ SyncModuleWorker.prototype._syncOneVersion = function (versionIndex, sourcePacka
author: author,
publish_time: sourcePackage.publish_time,
};
if (that._publish) {
// sync as publish
mod.package._publish_on_cnpm = true;
}
var dist = {
shasum: shasum,
size: dataSize,
@@ -457,16 +466,23 @@ SyncModuleWorker.prototype._syncOneVersion = function (versionIndex, sourcePacka
mod.package.dist = dist;
Module.add(mod, ep.done(function (result) {
that.log(' [%s:%s] done, insertId: %s, author: %s, version: %s, size: %d, publish_time: %j',
that.log(' [%s:%s] done, insertId: %s, author: %s, version: %s, size: %d, publish_time: %j, publish on cnpm: %s',
sourcePackage.name, versionIndex,
result.id,
author, mod.version, dataSize, new Date(mod.publish_time));
author, mod.version, dataSize,
new Date(mod.publish_time),
that._publish);
callback(null, result);
}));
});
};
SyncModuleWorker.sync = function (name, username, callback) {
SyncModuleWorker.sync = function (name, username, options, callback) {
if (typeof options === 'function') {
callback = options;
options = null;
}
options = options || {};
npm.get(name, function (err, pkg, response) {
if (err) {
return callback(err);
@@ -486,6 +502,8 @@ SyncModuleWorker.sync = function (name, username, callback) {
logId: result.id,
name: name,
username: username,
noDep: options.noDep,
publish: options.publish,
});
worker.start();
callback(null, {

View File

@@ -446,6 +446,45 @@ describe('controllers/registry/module.test.js', function () {
});
});
describe('GET /:name/download/:filename', function () {
it('should download a file with 302 redirect', function (done) {
request(app)
.get('/cutter/download/cutter-0.0.2.tgz')
.expect('Location', 'http://qtestbucket.qiniudn.com/cutter/-/cutter-0.0.2.tgz')
.expect(302, done)
});
it('should download a file direct from nfs stream', function (done) {
var nfs = require('../../../common/nfs');
mm(nfs, 'downloadStream', function (key, writeStream, options, callback) {
options.timeout.should.equal(600000);
nfs._client.download(key, {writeStream: writeStream, timeout: options.timeout}, callback);
});
Module.__get__ = Module.get;
mm(Module, 'get', function (name, version, callback) {
Module.__get__(name, version, function (err, info) {
info.package.dist.key = 'cutter/-/cutter-0.0.2.tgz';
callback(err, info);
});
});
request(app)
.get('/cutter/download/cutter-0.0.2.tgz')
.expect('ETag', 'c61fde5e8c26d053574d0c722097029fd1bc963a')
.expect('Content-Type', 'application/octet-stream')
.expect('Content-Length', '3139')
// TODO supertest has a bug
// Error: expected "Content-Disposition" of "inline; filename="testputmodule-0.1.9.tgz"", got "attachment; filename="testputmodule-0.1.9.tgz": undefined"
// .expect('Content-Disposition', 'inline; filename="testputmodule-0.1.9.tgz"')
.expect(200)
.end(function (err, res) {
should.not.exist(err);
// TODO: why supertest change buffer to text?
// res.text.length.should.equal(3139);
done();
});
});
});
describe('DELETE /:name/download/:filename/-rev/:rev', function () {
var lastRev;
before(function (done) {

View File

@@ -40,6 +40,53 @@ describe('controllers/sync.test.js', function () {
describe('sync source npm package', function () {
var logIdRegistry;
var logIdWeb;
it('should sync as publish success', function (done) {
request(registryApp)
.del('/utility/-rev/123')
.set('authorization', baseauth)
// .expect(200)
// .expect({ok: true})
.end(function (err, res) {
should.not.exist(err);
mm.data(Npm, 'get', require(path.join(fixtures, 'utility.json')));
request(registryApp)
.put('/utility/sync?publish=true&nodeps=true')
.set('authorization', baseauth)
.end(function (err, res) {
should.not.exist(err);
res.body.should.have.keys('ok', 'logId');
logIdRegistry = res.body.logId;
setTimeout(function () {
request(registryApp)
.get('/utility')
.expect(200)
.end(function (err, res) {
should.not.exist(err);
Object.keys(res.body.versions).length.should.above(0);
for (var v in res.body.versions) {
var pkg = res.body.versions[v];
pkg.should.have.property('_publish_on_cnpm', true);
}
done();
});
}, 3000);
});
});
});
it('should sync as publish 403 when user not admin', function (done) {
mm.data(Npm, 'get', require(path.join(fixtures, 'utility.json')));
request(registryApp)
.put('/utility_unit_test/sync?publish=true&nodeps=true')
.expect(403)
.expect({
error: 'no_perms',
reason: 'Only admin can publish'
}, done);
});
it('should sync success', function (done) {
mm.data(Npm, 'get', require(path.join(fixtures, 'utility.json')));
done = pedding(2, done);

View File

@@ -30,6 +30,8 @@ Log.create({
name: names,
username: 'fengmk2',
concurrency: names.length,
noDep: true,
publish: true,
});
worker.start();

View File

@@ -44,7 +44,7 @@ describe('sync/sync_exist.js', function () {
should.not.exist(err);
data.successes.should.eql(['cnpmjs.org', 'cutter']);
done();
});
});
});
});
});

View File

@@ -1,7 +1,13 @@
<div id="package">
<h1>
<%= package.name %>
<small>(<a href="/sync/<%= package.name %>" target="_blank">SYNC</a> missed versions from official npm registry)</small>
<small>
<% if (package._publish_on_cnpm) { %>
(Private package)
<% } else { %>
(<a href="/sync/<%= package.name %>" target="_blank">SYNC</a> missed versions from official npm registry)
<% } %>
</small>
</h1>
<% if (package.deprecated) { %>