Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add mirror support. fix on request error

  • Loading branch information...
commit ab4f4a1b30144dd3e2463f27a8ee84d69fed26a6 1 parent f864309
Hsiaoming Yang authored
Showing with 19 additions and 4 deletions.
  1. +19 −4 lib/sdk/yuan.js
View
23 lib/sdk/yuan.js
@@ -27,8 +27,16 @@ function Yuan(options) {
options = options || {};
var source = options.source || 'default';
+
this.server = spmrc.get('source.' + source + '.url');
+ this.mirror = spmrc.get('source.' + source + '.mirror');
+
this.server = (this.server || 'https://spmjs.org').replace(/\/$/, '');
+ this.mirror = (this.mirror || this.server).replace(/\/$/, '');
+ this.mirror = this.mirror.replace(
+ /^https\:\/\/spmjs\.org/, 'http://spmjs.org'
+ );
+
this.authKey = 'source.' + source + '.auth';
this.options = options;
}
@@ -36,12 +44,11 @@ function Yuan(options) {
Yuan.prototype.request = function(data, callback) {
data.url = util.format('%s/%s', this.server, data.urlpath);
if (data.method.toLowerCase() === 'get') {
- // http is faster
- data.url = data.url.replace(
- /^https\:\/\/spmjs\.org/, 'http://spmjs.org'
- );
+ // mirror is faster
+ data.url = util.format('%s/%s', this.mirror, data.urlpath);
}
log.debug(data.method.toLowerCase(), data.url);
+ delete data.urlpath;
data.headers = data.headers || {};
@@ -72,6 +79,14 @@ Yuan.prototype.request = function(data, callback) {
var self = this;
var req = request(data, function(err, res, body) {
+ if (err) {
+ if (err.code === 'ECONNREFUSED') {
+ log.error('exit', err);
+ process.exit(1);
+ }
+ callback(err);
+ return;
+ }
if (data.json && res.headers['content-encoding'] === 'gzip') {
zlib.gunzip(body, function(err, content) {
if (err) {
Please sign in to comment.
Something went wrong with that request. Please try again.