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

Issue237 sync worker #253

Merged
merged 10 commits into from
Mar 7, 2014
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ jshint:
@./node_modules/.bin/jshint ./

test:
@NODE_ENV=test ./node_modules/mocha/bin/mocha \
--harmony-generators \
--reporter $(REPORTER) \
--timeout $(TIMEOUT) \
--require should \
$(MOCHA_OPTS) \
$(TESTS)


test-cov:
@NODE_ENV=test node --harmony \
node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha \
-- -u exports \
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
},
"dependencies": {
"co": "3.0.4",
"co-gather": "0.0.1",
"co-read": "0.0.1",
"co-write": "0.3.0",
"debug": "0.7.4",
Expand Down
63 changes: 30 additions & 33 deletions proxy/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,53 +17,50 @@
var thunkify = require('thunkify-wrap');
var urllib = require('urllib');
var config = require('../config');
thunkify(urllib, ['request']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里有一个疑问,如果 urllib 已经被 thunkify过了,再thunkify一次会有什么效果?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯 会出问题, 我看看能不能thunkify解决,不然就只能统一封装一个co-urllib

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我弄一个co-urllib 吧


function request(url, options, callback) {
if (typeof options === 'function') {
callback = options;
options = null;
}
function *request (url, options) {
options = options || {};
options.dataType = options.dataType || 'json';
options.timeout = options.timeout || 120000;
url = config.sourceNpmRegistry + url;
urllib.request(url, options, function (err, data, res) {
if (err) {
var statusCode = res && res.statusCode || -1;
if (err.name === 'JSONResponseFormatError' && statusCode >= 500) {
err.name = 'NPMServerError';
err.message = 'Status ' + statusCode + ', ' + (data && data.toString() || 'empty body');
}
var r;
try {
r = yield urllib.request(url, options);
} catch (err) {
var statusCode = err.res ? err.res.statusCode : 200;
var data = err.data || '[empty]';
if (err.name === 'JSONResponseFormatError' && statusCode >= 500) {
err.name = 'NPMServerError';
err.message = 'Status ' + statusCode + ', ' + data.toString();
}
callback(err, data, res);
});
throw err;
}
return r;
}

exports.get = function (name, callback) {
request('/' + name, function (err, data, res) {
if (err) {
return callback(err);
}
res = res || {};
if (res.statusCode === 404) {
data = null;
}
callback(null, data, res);
});
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 = function (startkey, callback) {
request('/-/all/since?stale=update_after&startkey=' + startkey, {
exports.getAllSince = function *(startkey) {
var r = yield request('/-/all/since?stale=update_after&startkey=' + startkey, {
dataType: 'json',
timeout: 300000
}, callback);
});
return r[0];
};

exports.getShort = function (callback) {
request('/-/short', {
exports.getShort = function *() {
var r = yield request('/-/short', {
dataType: 'json',
timeout: 300000
}, callback);
});
return r[0];
};

thunkify(exports);
Loading