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

Commit

Permalink
change npm.js to generator
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse committed Mar 6, 2014
1 parent 1a0b9ed commit 611321e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"qn": "0.2.0",
"ready": "0.1.1",
"semver": "2.2.1",
"thunkify-wrap": "0.0.4",
"thunkify-wrap": "0.0.5",
"urllib": "0.5.6",
"utility": "0.1.10"
},
Expand Down
15 changes: 7 additions & 8 deletions proxy/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
var thunkify = require('thunkify-wrap');
var urllib = require('urllib');
var config = require('../config');
var co = require('co');
thunkify(urllib, ['request']);

function *request(url, options) {
function *request (url, options) {
options = options || {};
options.dataType = options.dataType || 'json';
options.timeout = options.timeout || 120000;
Expand All @@ -40,28 +39,28 @@ function *request(url, options) {
return r;
}

exports.get = co(function *(name) {
exports.get = function *(name) {
var r = yield request('/' + name);
var data = r[0];
var res = r[1];
if (res && res.statusCode === 404) {
data = null;
}
return data;
});
};

exports.getAllSince = co(function *(startkey) {
exports.getAllSince = function *(startkey) {
var r = yield request('/-/all/since?stale=update_after&startkey=' + startkey, {
dataType: 'json',
timeout: 300000
});
return r[0];
});
};

exports.getShort = co(function *() {
exports.getShort = function *() {
var r = yield request('/-/short', {
dataType: 'json',
timeout: 300000
});
return r[0];
});
};
8 changes: 4 additions & 4 deletions test/proxy/npm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ describe('proxy/npm.test.js', function () {

it('should return a module info from source npm', function (done) {
co(function *() {
var data = yield npm.get.bind(null, 'pedding');
var data = yield npm.get('pedding');
data.name.should.equal('pedding');
done();
})();
});

it('should return null when module not exist', function (done) {
co(function *() {
var data = yield npm.get.bind(null, 'pedding-not-exists');
var data = yield npm.get('pedding-not-exists');
should.not.exist(data);
done();
})();
Expand All @@ -46,7 +46,7 @@ describe('proxy/npm.test.js', function () {
mm.http.request(/\//, '{');
co(function *() {
try {
var data = yield npm.get.bind(null, 'pedding-not-exists');
var data = yield npm.get('pedding-not-exists');
} catch (err) {
err.name.should.equal('JSONResponseFormatError');
done();
Expand All @@ -60,7 +60,7 @@ describe('proxy/npm.test.js', function () {
// http://registry.npmjs.org/octopie
co(function *() {
try {
var data = yield npm.get.bind(null, 'octopie');
var data = yield npm.get('octopie');
} catch (err) {
err.name.should.equal('NPMServerError');
err.message.should.equal('Status 500, ' + content);
Expand Down

0 comments on commit 611321e

Please sign in to comment.