limitd-client
Advanced tools
Comparing version 1.1.2 to 1.1.3
24
index.js
@@ -14,2 +14,3 @@ var url = require('url'); | ||
var encoder = require('pb-stream').encoder; | ||
var cb = require('cb'); | ||
@@ -75,8 +76,19 @@ var DEFAULT_PORT = 9231; | ||
LimitdClient.prototype._request = function (request, type, done) { | ||
LimitdClient.prototype._request = function (request, type, _callback) { | ||
var callback = _callback; | ||
if (_callback && request.method !== RequestMessage.Method.WAIT) { | ||
callback = cb(function (err, result) { | ||
if (err instanceof cb.TimeoutError) { | ||
return _callback(new Error('request timeout')); | ||
} | ||
_callback(err, result); | ||
}).timeout(1000); | ||
} | ||
if (!this.stream || !this.stream.writable) { | ||
var err = new Error('The socket is closed.'); | ||
if (done) { | ||
if (callback) { | ||
return process.nextTick(function () { | ||
done(err); | ||
callback(err); | ||
}); | ||
@@ -90,3 +102,3 @@ } else { | ||
if (!done) return; | ||
if (!callback) return; | ||
@@ -96,5 +108,5 @@ this.once('response_' + request.id, function (response) { | ||
response['.limitd.ErrorResponse.response'].type === ErrorResponse.Type.UNKNOWN_BUCKET_TYPE) { | ||
return done(new Error(type + ' is not a valid bucket type')); | ||
return callback(new Error(type + ' is not a valid bucket type')); | ||
} | ||
done(null, response['.limitd.TakeResponse.response'] || response['.limitd.PutResponse.response'] || response['.limitd.StatusResponse.response']); | ||
callback(null, response['.limitd.TakeResponse.response'] || response['.limitd.PutResponse.response'] || response['.limitd.StatusResponse.response']); | ||
}); | ||
@@ -101,0 +113,0 @@ }; |
{ | ||
"name": "limitd-client", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "limitd client for node.js", | ||
@@ -12,2 +12,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"cb": "~0.1.0", | ||
"lodash": "~3.7.0", | ||
@@ -14,0 +15,0 @@ "pb-stream": "~1.0.3", |
@@ -130,2 +130,15 @@ var LimitdClient = require('../'); | ||
it('should timeout a request when there is no response from the server', function (done) { | ||
server.once('request', function () { | ||
//noop | ||
}); | ||
client.take('ip', '191.timeout.23.32', 1, function (err){ | ||
assert.ok(err); | ||
assert.equal(err.message, 'request timeout'); | ||
done(); | ||
}); | ||
}); | ||
}); |
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
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
22798
371
6