npm-registry-client
Advanced tools
Comparing version 0.0.7 to 0.0.8
@@ -28,2 +28,7 @@ | ||
this.retries = options.retries || 2 | ||
this.retryFactor = options.retryFactor || 10 | ||
this.retryMinTimeout = options.retryMinTimeout || 10000 | ||
this.retryMaxTimeout = options.retryMaxTimeout || 60000 | ||
this.cache = options.cache | ||
@@ -30,0 +35,0 @@ if (!this.cache) throw new Error("Cache dir is required") |
@@ -79,3 +79,3 @@ | ||
var headers = res.headers | ||
, updated = Date.parse(headers.date) | ||
, updated = data._updated || Date.parse(headers.date) | ||
Object.keys(updates).forEach(function (p) { | ||
@@ -82,0 +82,0 @@ data[p] = updates[p] |
@@ -9,2 +9,3 @@ module.exports = regRequest | ||
, request = require("request") | ||
, retry = require("retry") | ||
@@ -78,6 +79,36 @@ function regRequest (method, where, what, etag, nofollow, cb_) { | ||
makeRequest.call(this, method, remote, where, what, etag, nofollow, cb) | ||
// Tuned to spread 3 attempts over about a minute. | ||
// See formula at <https://github.com/tim-kos/node-retry>. | ||
var operation = retry.operation({ | ||
retries: this.retries, | ||
factor: this.retryFactor, | ||
minTimeout: this.retryMinTimeout, | ||
maxTimeout: this.retryMaxTimeout | ||
}) | ||
var self = this | ||
operation.attempt(function (currentAttempt) { | ||
self.log.info("retry", "registry request attempt " + currentAttempt | ||
+ " at " + (new Date()).toLocaleTimeString()) | ||
makeRequest.call(self, method, remote, where, what, etag, nofollow | ||
, function (er, parsed, raw, response) { | ||
// Only retry on 408, 5xx or no `response`. | ||
var statusCode = response && response.statusCode | ||
var statusRetry = !statusCode || (statusCode === 408 || statusCode >= 500) | ||
if (er && statusRetry && operation.retry(er)) { | ||
self.log.info("retry", "will retry, error on last attempt: " + er) | ||
return | ||
} | ||
cb.apply(null, arguments) | ||
}) | ||
}) | ||
} | ||
function makeRequest (method, remote, where, what, etag, nofollow, cb) { | ||
function makeRequest (method, remote, where, what, etag, nofollow, cb_) { | ||
var cbCalled = false | ||
function cb () { | ||
if (cbCalled) return | ||
cbCalled = true | ||
cb_.apply(null, arguments) | ||
} | ||
var opts = { url: remote | ||
@@ -125,2 +156,5 @@ , method: method | ||
req.on("error", cb) | ||
req.on("socket", function (s) { | ||
s.on("error", cb) | ||
}) | ||
@@ -127,0 +161,0 @@ if (what && (what instanceof Stream)) { |
@@ -5,3 +5,3 @@ { | ||
"description": "Client for the npm registry", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"repository": { | ||
@@ -22,3 +22,4 @@ "url": "git://github.com/isaacs/npm-registry-client" | ||
"mkdirp": "~0.3.3", | ||
"rimraf": "~2.0.1" | ||
"rimraf": "~2.0.1", | ||
"retry": "0.6.0" | ||
}, | ||
@@ -31,5 +32,3 @@ "devDependencies": { | ||
}, | ||
"engines": { | ||
"node": "*" | ||
} | ||
"license": "BSD" | ||
} |
@@ -37,2 +37,9 @@ # npm-registry-client | ||
that works, otherwise logs are disabled. | ||
* `retries` {Number} Number of times to retry on GET failures. | ||
Default=2 | ||
* `retryFactor` {Number} `factor` setting for `node-retry`. Default=10 | ||
* `retryMinTimeout` {Number} `minTimeout` setting for `node-retry`. | ||
Default=10000 (10 seconds) | ||
* `retryMaxTimeout` {Number} `maxTimeout` setting for `node-retry`. | ||
Default=60000 (60 seconds) | ||
@@ -39,0 +46,0 @@ # client.request(method, where, [what], [etag], [nofollow], cb) |
@@ -14,15 +14,28 @@ // a fake registry server. | ||
function handler (req, res) { | ||
var u = "* " + req.url | ||
, mu = req.method + " " + req.url | ||
, k = expect[mu] ? mu : expect[u] ? u : null | ||
req.connection.setTimeout(1000) | ||
if (!k) throw Error("unexpected request", req.method, req.url) | ||
expect[k] -- | ||
var u = '* ' + req.url | ||
, mu = req.method + ' ' + req.url | ||
if (Object.keys(expect).reduce(function (s, k) { | ||
return s + expect[k] | ||
}, 0) === 0) server.close() | ||
var k = server._expect[mu] ? mu : server._expect[u] ? u : null | ||
if (!k) throw Error('unexpected request', req.method, req.url) | ||
var fn = server._expect[k].shift() | ||
if (!fn) throw Error('unexpected request', req.method, req.url) | ||
var remain = (Object.keys(server._expect).reduce(function (s, k) { | ||
return s + server._expect[k].length | ||
}, 0)) | ||
if (remain === 0) server.close() | ||
else console.error("TEST SERVER: %d reqs remain", remain) | ||
console.error(Object.keys(server._expect).map(function(k) { | ||
return [k, server._expect[k].length] | ||
}).reduce(function (acc, kv) { | ||
acc[kv[0]] = kv[1] | ||
return acc | ||
}, {})) | ||
res.json = json | ||
server._expect[k](req, res) | ||
fn(req, res) | ||
} | ||
@@ -36,11 +49,10 @@ | ||
server.expect = function (method, u, fn) { | ||
if (typeof u === "function") { | ||
if (typeof u === 'function') { | ||
fn = u | ||
u = method | ||
method = "*" | ||
method = '*' | ||
} | ||
u = method + " " + u | ||
server._expect[u] = fn | ||
expect[u] = expect[u] || 0 | ||
expect[u] ++ | ||
u = method + ' ' + u | ||
server._expect[u] = server._expect[u] || [] | ||
server._expect[u].push(fn) | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Misc. License Issues
License(Experimental) A package's licensing information has fine-grained problems.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
63766
22
0
963
137
10
1
+ Addedretry@0.6.0
+ Addedretry@0.6.0(transitive)