limitd-client
Advanced tools
Comparing version
98
index.js
@@ -50,2 +50,7 @@ var url = require('url'); | ||
client.emit('ready'); | ||
stream.on('error', function (err) { | ||
client.emit('error', err); | ||
}); | ||
}).once('connect', function () { | ||
@@ -70,25 +75,3 @@ process.nextTick(function () { | ||
LimitdClient.prototype._request = function (method, type, key, count, done) { | ||
if (typeof count === 'function') { | ||
done = count; | ||
count = method === 'PUT' ? 'all' : 1; | ||
} | ||
if (typeof count === 'undefined') { | ||
count = method === 'PUT' ? 'all' : 1; | ||
} | ||
var request = new RequestMessage({ | ||
'id': randomstring.generate(7), | ||
'type': type, | ||
'key': key, | ||
'method': RequestMessage.Method[method], | ||
}); | ||
if (count === 'all') { | ||
request.set('all', true); | ||
} else { | ||
request.set('count', count); | ||
} | ||
LimitdClient.prototype._request = function (request, type, done) { | ||
if (!this.stream || !this.stream.writable) { | ||
@@ -105,3 +88,3 @@ var err = new Error('The socket is closed.'); | ||
this.writer.write(request); | ||
this.stream.write(request.encodeDelimited().toBuffer()); | ||
@@ -115,19 +98,76 @@ if (!done) return; | ||
} | ||
done(null, response['.limitd.TakeResponse.response'] || response['.limitd.PutResponse.response']); | ||
done(null, response['.limitd.TakeResponse.response'] || response['.limitd.PutResponse.response'] || response['.limitd.StatusResponse.response']); | ||
}); | ||
}; | ||
LimitdClient.prototype._takeOrWait = function (method, type, key, count, done) { | ||
if (typeof count === 'undefined' || typeof done === 'undefined') { | ||
done = null; | ||
count = 1; | ||
} else if (typeof count === 'function') { | ||
done = count; | ||
count = 1; | ||
} | ||
var request = new RequestMessage({ | ||
'id': randomstring.generate(7), | ||
'type': type, | ||
'key': key, | ||
'method': RequestMessage.Method[method], | ||
}); | ||
if (count === 'all') { | ||
request.set('all', true); | ||
} else { | ||
request.set('count', count); | ||
} | ||
return this._request(request, type, done); | ||
}; | ||
LimitdClient.prototype.take = function (type, key, count, done) { | ||
return this._request('TAKE', type, key, count, done); | ||
return this._takeOrWait('TAKE', type, key, count, done); | ||
}; | ||
LimitdClient.prototype.wait = function (type, key, count, done) { | ||
return this._takeOrWait('WAIT', type, key, count, done); | ||
}; | ||
LimitdClient.prototype.reset = | ||
LimitdClient.prototype.put = function (type, key, count, done) { | ||
return this._request('PUT', type, key, count, done); | ||
if (typeof count === 'undefined' || typeof done === 'undefined') { | ||
done = null; | ||
count = 'all'; | ||
} else if (typeof count === 'function') { | ||
done = count; | ||
count = 'all'; | ||
} | ||
var request = new RequestMessage({ | ||
'id': randomstring.generate(7), | ||
'type': type, | ||
'key': key, | ||
'method': RequestMessage.Method.PUT, | ||
}); | ||
if (count === 'all') { | ||
request.set('all', true); | ||
} else { | ||
request.set('count', count); | ||
} | ||
return this._request(request, type, done); | ||
}; | ||
LimitdClient.prototype.wait = function (type, key, count, done) { | ||
return this._request('WAIT', type, key, count, done); | ||
LimitdClient.prototype.status = function (type, key, done) { | ||
var request = new RequestMessage({ | ||
'id': randomstring.generate(7), | ||
'type': type, | ||
'key': key, | ||
'method': RequestMessage.Method.STATUS, | ||
}); | ||
return this._request(request, type, done); | ||
}; | ||
module.exports = LimitdClient; |
{ | ||
"name": "limitd-client", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "limitd client for node.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
{ | ||
"name": "limitd-protocol", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "The ", | ||
@@ -24,2 +24,2 @@ "scripts": { | ||
} | ||
} | ||
} |
@@ -47,2 +47,29 @@ var LimitdClient = require('../'); | ||
it('should be able to send PUT requests', function (done) { | ||
server.once('request', function (request) { | ||
assert.isString(request.id); | ||
assert.equal(request.method, protocol.Request.Method.PUT); | ||
assert.equal(request.type, 'ip'); | ||
assert.equal(request.all, true); | ||
done(); | ||
}); | ||
client.put('ip', '191.12.23.32'); | ||
}); | ||
it('should be able to send status requests', function (done) { | ||
server.once('request', function (request) { | ||
assert.isString(request.id); | ||
assert.equal(request.method, protocol.Request.Method.STATUS); | ||
assert.equal(request.type, 'ip'); | ||
done(); | ||
}); | ||
client.status('ip', '191.12.23.32', function () {}); | ||
}); | ||
it('should be able to parse the response of TAKE', function (done) { | ||
@@ -49,0 +76,0 @@ |
@@ -48,2 +48,4 @@ var LimitdClient = require('../'); | ||
}); | ||
}).once('error', function (err) { | ||
console.log('ignore error', err.code); | ||
}); | ||
@@ -50,0 +52,0 @@ }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
22053
11.07%22
4.76%351
18.18%