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

Commit

Permalink
fix download url
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 9, 2013
1 parent a7dd6fd commit 4be41be
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 35 deletions.
6 changes: 6 additions & 0 deletions controllers/registry/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ exports.getTarballFilepath = function (filename) {
exports.getCDNKey = function (name, filename) {
return '/' + name + '/-/' + filename;
};

exports.downloadURL = function (pkg, req) {
if (pkg.dist && pkg.dist.tarball) {
pkg.dist.tarball = 'http://' + req.headers.host + '/' + pkg.name + '/download/' + path.basename(pkg.dist.tarball);
}
};
58 changes: 30 additions & 28 deletions controllers/registry/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ 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,
//if module not exist in this registry,
//sync the module backend and return package info from official registry
if (rows.length === 0) {
if (!req.session.allowSync) {
Expand Down Expand Up @@ -83,9 +83,7 @@ exports.show = function (req, res, next) {
for (var i = startIndex; i < rows.length; i++) {
var row = rows[i];
var pkg = row.package;
if (pkg.dist && pkg.dist.tarball) {
pkg.dist.tarball = 'http://' + req.headers.host + '/' + pkg.name + '/download/' + path.basename(pkg.dist.tarball);
}
common.downloadURL(pkg, req);
versions[pkg.version] = pkg;
times[pkg.version] = row.gmt_modified;
if ((!distTags.latest && !latestMod) || distTags.latest === row.version) {
Expand Down Expand Up @@ -124,13 +122,13 @@ exports.show = function (req, res, next) {
exports.get = function (req, res, next) {
var name = req.params.name;
var version = req.params.version;

var ep = eventproxy.create();
ep.fail(next);

//frist get by tag
Module.getByTag(name, version, ep.done(function (mod) {
if (mod) {
common.downloadURL(mod.package, req);
return res.json(mod.package);
}
ep.emit('notFoundByTag');
Expand All @@ -142,26 +140,29 @@ exports.get = function (req, res, next) {
});

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);
});
if (mod) {
common.downloadURL(mod.package, req);
return res.json(mod.package);
}

if (!req.session.allowSync) {
return next();
}
res.json(mod.package);

var username = (req.session && req.session.username) || 'anonymous';
_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
});
}
res.json(pkg);
});
});
};

Expand Down Expand Up @@ -626,7 +627,7 @@ exports.getSyncLog = function (req, res, next) {
});
};

function parseModsForList(mods) {
function parseModsForList(mods, req) {
var results = {
_updated: Date.now()
};
Expand All @@ -643,6 +644,7 @@ function parseModsForList(mods) {
pkg['dist-tags'] = {
latest: pkg.version
};
common.downloadURL(pkg, req);
results[mod.name] = pkg;
}
return results;
Expand All @@ -653,7 +655,7 @@ exports.listAllModules = function (req, res, next) {
if (err) {
return next(err);
}
return res.json(parseModsForList(mods));
return res.json(parseModsForList(mods, req));
});
};

Expand All @@ -666,12 +668,12 @@ exports.listAllModulesSince = function (req, res, next) {
});
}
debug('list all modules from %s', req.startkey);
var startkey = parseInt(query.startkey, 10) || 0;
var startkey = Number(query.startkey) || 0;
Module.listSince(new Date(startkey), function (err, mods) {
if (err) {
return next(err);
}
res.json(parseModsForList(mods));
res.json(parseModsForList(mods, req));
});
};

Expand Down
19 changes: 12 additions & 7 deletions test/controllers/registry/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ describe('controllers/registry/module.test.js', function () {
// "url": "http://fengmk2.github.com"
// });
res.body.name.should.equal('cnpmjs.org');
res.body.versions[Object.keys(res.body.versions)[0]].dist.tarball.should.include('/cnpmjs.org/download');
done();
});
});
Expand All @@ -95,6 +96,7 @@ describe('controllers/registry/module.test.js', function () {
body.version.should.equal('0.0.4');
body._id.should.equal('cnpmjs.org@0.0.4');
body.dist.should.have.keys('tarball', 'shasum', 'size');
body.dist.tarball.should.include('/cnpmjs.org/download/cnpmjs.org-0.0.4.tgz');
done();
});
});
Expand All @@ -109,6 +111,7 @@ describe('controllers/registry/module.test.js', function () {
body.version.should.equal('0.0.4');
body._id.should.equal('cnpmjs.org@0.0.4');
body.dist.should.have.keys('tarball', 'shasum', 'size');
body.dist.tarball.should.include('/cnpmjs.org/download/cnpmjs.org-0.0.4.tgz');
done();
});
});
Expand Down Expand Up @@ -316,7 +319,7 @@ describe('controllers/registry/module.test.js', function () {
should.not.exist(err);
res.body.name.should.equal('testputmodule');
res.body.version.should.equal('0.1.9');
res.body.dist.tarball.should.include('/testputmodule/-/testputmodule-0.1.9.tgz');
res.body.dist.tarball.should.include('/testputmodule/download/testputmodule-0.1.9.tgz');
done();
});
});
Expand All @@ -342,20 +345,22 @@ describe('controllers/registry/module.test.js', function () {
.expect(200, function (err, res) {
res.body.should.be.an.Object;
res.body._updated.should.be.a.Number;
Object.keys(res.body).length.should.be.above(1);
var keys = Object.keys(res.body);
keys.length.should.be.above(1);
res.body[keys[1]].dist.tarball.should.include('/download/');
done();
});
});

it('should get 200 but response empty', function (done) {
request(app)
.get('/-/all/since?stale=update_after&startkey=' + Date.now())
.get('/-/all/since?stale=update_after&startkey=' + (Date.now() * 2))
.expect(200, function (err, res) {
res.body.should.be.an.Object;
res.body._updated.should.be.a.Number;
// some erro happened at travis-ci
// res.body.should.eql({
// _updated: res.body._updated
// });
res.body.should.eql({
_updated: res.body._updated
});
done();
});
});
Expand Down

0 comments on commit 4be41be

Please sign in to comment.