limitd-client
Advanced tools
Comparing version 2.1.2 to 2.1.3
@@ -58,3 +58,4 @@ const url = require('url'); | ||
this.pending_requests = {}; | ||
this.pending_requests = new Map(); | ||
this.connect(done); | ||
@@ -75,3 +76,2 @@ | ||
this.resetCircuitBreaker = () => this._request.reset(); | ||
@@ -159,3 +159,3 @@ } | ||
.on('data', function (response) { | ||
var response_handler = self.pending_requests[response.request_id]; | ||
var response_handler = self.pending_requests.get(response.request_id); | ||
if (response_handler) { | ||
@@ -184,24 +184,11 @@ response_handler(response); | ||
LimitdClient.prototype._request = function (request, type, callback) { | ||
const client = this; | ||
if (!this.stream || !this.stream.writable) { | ||
const err = new Error('The socket is closed.'); | ||
if (callback) { | ||
return setImmediate(callback, err); | ||
} else { | ||
throw err; | ||
} | ||
} | ||
lpm.write(this.stream, Protocol.Request.encode(request)); | ||
LimitdClient.prototype._responseHandler = function(requestID, callback) { | ||
const start = Date.now(); | ||
client.pending_requests[request.id] = (response) => { | ||
delete client.pending_requests[request.id]; | ||
return (response) => { | ||
this.pending_requests.delete(requestID); | ||
if (response.error && | ||
response.error.type === 'UNKNOWN_BUCKET_TYPE') { | ||
return callback(new Error(type + ' is not a valid bucket type')); | ||
return callback(new Error('Invalid bucket type')); | ||
} | ||
@@ -221,2 +208,17 @@ | ||
LimitdClient.prototype._request = function (request, type, callback) { | ||
if (!this.stream || !this.stream.writable) { | ||
const err = new Error('The socket is closed.'); | ||
if (callback) { | ||
return setImmediate(callback, err); | ||
} else { | ||
throw err; | ||
} | ||
} | ||
lpm.write(this.stream, Protocol.Request.encode(request)); | ||
this.pending_requests.set(request.id, this._responseHandler(request.id, callback)); | ||
}; | ||
LimitdClient.prototype._takeOrWait = function (method, type, key, count, done) { | ||
@@ -237,2 +239,4 @@ if (typeof count === 'undefined' && typeof done === 'undefined') { | ||
const takeAll = count === 'all'; | ||
const request = { | ||
@@ -243,4 +247,4 @@ 'id': uuid(), | ||
'method': method, | ||
'all': count === 'all' || null, | ||
'count': count !== 'all' ? count : null | ||
'all': takeAll || null, | ||
'count': !takeAll ? count : null | ||
}; | ||
@@ -247,0 +251,0 @@ |
{ | ||
"name": "limitd-client", | ||
"version": "2.1.2", | ||
"version": "2.1.3", | ||
"description": "limitd client for node.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -126,3 +126,3 @@ const LimitdClient = require('../'); | ||
assert.ok(err); | ||
assert.equal(err.message, 'ip is not a valid bucket type'); | ||
assert.equal(err.message, 'Invalid bucket type'); | ||
done(); | ||
@@ -129,0 +129,0 @@ }); |
@@ -7,3 +7,3 @@ const MockServer = require('./MockServer'); | ||
describe.only('circuit breaker', function () { | ||
describe('circuit breaker', function () { | ||
const server = new MockServer({ port }); | ||
@@ -10,0 +10,0 @@ var client; |
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
34114
855