Comparing version 0.1.1 to 0.1.2
@@ -0,1 +1,6 @@ | ||
0.1.2 | ||
===== | ||
* add `strictSSL` option. When `false` it disables SSL certificate check. See more info in README of [request](https://github.com/request/request) library we use internally. | ||
0.1.1 | ||
@@ -2,0 +7,0 @@ ===== |
@@ -24,4 +24,4 @@ var url = require('url'); | ||
function Client(options) { | ||
this.commands = []; | ||
this.config = new Config(options); | ||
this.commands = []; | ||
this.config = new Config(options); | ||
} | ||
@@ -38,114 +38,114 @@ | ||
*/ | ||
Client.prototype.send = function(callback) { | ||
var commands = this.commands; | ||
this.reset(); | ||
requests.send( | ||
this.config, JSON.stringify(commands), callback | ||
); | ||
Client.prototype.send = function (callback) { | ||
var commands = this.commands; | ||
this.reset(); | ||
requests.send( | ||
this.config, JSON.stringify(commands), callback | ||
); | ||
}; | ||
Client.prototype._sendOne = function(callback) { | ||
Client.prototype._sendOne = function (callback) { | ||
var callbackWrapper = function(error, res) { | ||
if (typeof callback !== "function") { | ||
return; | ||
} | ||
if (error !== null) { | ||
callback(error, null); | ||
} else { | ||
callback(null, res[0]); | ||
} | ||
}; | ||
var callbackWrapper = function (error, res) { | ||
if (typeof callback !== "function") { | ||
return; | ||
} | ||
if (error !== null) { | ||
callback(error, null); | ||
} else { | ||
callback(null, res[0]); | ||
} | ||
}; | ||
this.send(callbackWrapper); | ||
this.send(callbackWrapper); | ||
}; | ||
Client.prototype.add = function(method, params) { | ||
var command = { | ||
"method": method, | ||
"params": params | ||
}; | ||
this.commands.push(command); | ||
Client.prototype.add = function (method, params) { | ||
var command = { | ||
"method": method, | ||
"params": params | ||
}; | ||
this.commands.push(command); | ||
}; | ||
Client.prototype.reset = function() { | ||
this.commands = []; | ||
Client.prototype.reset = function () { | ||
this.commands = []; | ||
}; | ||
Client.prototype._check = function() { | ||
if (this.commands.length > 0) { | ||
throw "command buffer not empty"; | ||
} | ||
Client.prototype._check = function () { | ||
if (this.commands.length > 0) { | ||
throw "command buffer not empty"; | ||
} | ||
}; | ||
Client.prototype.publish = function(channel, data, callback) { | ||
this._check(); | ||
var params = { | ||
"channel": channel, | ||
"data": data | ||
}; | ||
this.add("publish", params); | ||
this._sendOne(callback); | ||
Client.prototype.publish = function (channel, data, callback) { | ||
this._check(); | ||
var params = { | ||
"channel": channel, | ||
"data": data | ||
}; | ||
this.add("publish", params); | ||
this._sendOne(callback); | ||
}; | ||
Client.prototype.publishClient = function(channel, data, client, callback) { | ||
this._check(); | ||
var params = { | ||
"channel": channel, | ||
"data": data, | ||
"client": client | ||
}; | ||
this.add("publish", params); | ||
this._sendOne(callback); | ||
Client.prototype.publishClient = function (channel, data, client, callback) { | ||
this._check(); | ||
var params = { | ||
"channel": channel, | ||
"data": data, | ||
"client": client | ||
}; | ||
this.add("publish", params); | ||
this._sendOne(callback); | ||
}; | ||
Client.prototype.disconnect = function(user, callback) { | ||
this._check(); | ||
var params = { | ||
"user": user | ||
}; | ||
this.add("disconnect", params); | ||
this._sendOne(callback); | ||
Client.prototype.disconnect = function (user, callback) { | ||
this._check(); | ||
var params = { | ||
"user": user | ||
}; | ||
this.add("disconnect", params); | ||
this._sendOne(callback); | ||
}; | ||
Client.prototype.unsubscribe = function(user, channel, callback) { | ||
this._check(); | ||
var params = { | ||
"channel": channel, | ||
"user": user | ||
}; | ||
this.add("unsubscribe", params); | ||
this._sendOne(callback); | ||
Client.prototype.unsubscribe = function (user, channel, callback) { | ||
this._check(); | ||
var params = { | ||
"channel": channel, | ||
"user": user | ||
}; | ||
this.add("unsubscribe", params); | ||
this._sendOne(callback); | ||
}; | ||
Client.prototype.presence = function(channel, callback) { | ||
this._check(); | ||
var params = { | ||
"channel": channel | ||
}; | ||
this.add("presence", params); | ||
this._sendOne(callback); | ||
Client.prototype.presence = function (channel, callback) { | ||
this._check(); | ||
var params = { | ||
"channel": channel | ||
}; | ||
this.add("presence", params); | ||
this._sendOne(callback); | ||
}; | ||
Client.prototype.history = function(channel, callback) { | ||
this._check(); | ||
var params = { | ||
"channel": channel | ||
}; | ||
this.add("history", params); | ||
this._sendOne(callback); | ||
Client.prototype.history = function (channel, callback) { | ||
this._check(); | ||
var params = { | ||
"channel": channel | ||
}; | ||
this.add("history", params); | ||
this._sendOne(callback); | ||
}; | ||
Client.prototype.channels = function(callback) { | ||
this._check(); | ||
var params = {}; | ||
this.add("channels", params); | ||
this._sendOne(callback); | ||
Client.prototype.channels = function (callback) { | ||
this._check(); | ||
var params = {}; | ||
this.add("channels", params); | ||
this._sendOne(callback); | ||
}; | ||
Client.prototype.stats = function(callback) { | ||
this._check(); | ||
var params = {}; | ||
this.add("stats", params); | ||
this._sendOne(callback); | ||
Client.prototype.stats = function (callback) { | ||
this._check(); | ||
var params = {}; | ||
this.add("stats", params); | ||
this._sendOne(callback); | ||
}; | ||
@@ -152,0 +152,0 @@ |
var Token = require('./token'); | ||
function Config(options) { | ||
options = options || {}; | ||
var url = options.url || ""; | ||
options = options || {}; | ||
var url = options.url || ""; | ||
if (url.length > 0) { | ||
var path = "/api"; | ||
if (url[url.length - 1] === "/") { | ||
url = url.slice(0, url.length - 1); | ||
if (url.length > 0) { | ||
var path = "/api"; | ||
if (url[url.length - 1] === "/") { | ||
url = url.slice(0, url.length - 1); | ||
} | ||
if (url.indexOf(path, url.length - path.length) == -1) { | ||
url = url + path + "/"; | ||
} else { | ||
url = url + "/"; | ||
} | ||
} | ||
if (url.indexOf(path, url.length - path.length) == -1) { | ||
url = url + path + "/"; | ||
} else { | ||
url = url + "/"; | ||
} | ||
} | ||
this.url = url; | ||
this.token = new Token(options.secret); | ||
this.timeout = options.timeout; | ||
this.url = url; | ||
this.token = new Token(options.secret); | ||
this.timeout = options.timeout; | ||
} | ||
module.exports = Config; |
@@ -6,47 +6,50 @@ var request = require('request'); | ||
function send(config, body, callback) { | ||
var params = { | ||
url: config.url, | ||
timeout: config.timeout | ||
}; | ||
if(body) { | ||
var sign = config.token.apiSign(body); | ||
params.headers = {'content-type': 'application/json', 'X-API-Sign': sign}; | ||
params.body = body; | ||
} | ||
request["post"](params, function(err, res) { | ||
handleResponse(err, config.url, res, callback); | ||
}); | ||
var params = { | ||
url: config.url, | ||
timeout: config.timeout | ||
}; | ||
if (config.strictSSL === false) { | ||
params["strictSSL"] = false; | ||
} | ||
if (body) { | ||
var sign = config.token.apiSign(body); | ||
params.headers = {'content-type': 'application/json', 'X-API-Sign': sign}; | ||
params.body = body; | ||
} | ||
request["post"](params, function (err, res) { | ||
handleResponse(err, config.url, res, callback); | ||
}); | ||
} | ||
function handleResponse(err, url, res, callback) { | ||
if (typeof callback !== "function") { | ||
return; | ||
} | ||
if (typeof callback !== "function") { | ||
return; | ||
} | ||
var error = null; | ||
if (err) { | ||
error = new errors.RequestError( | ||
"Request failed with an error", | ||
url, | ||
err, | ||
res ? res.statusCode : null, | ||
res ? res.body : null | ||
); | ||
} else if (res.statusCode >= 400) { | ||
error = new errors.RequestError( | ||
"Unexpected status code " + res.statusCode, | ||
url, | ||
err, | ||
res ? res.statusCode : null, | ||
res ? res.body : null | ||
); | ||
} | ||
if (error === null) { | ||
var data = JSON.parse(res.body); | ||
callback(null, data); | ||
} else { | ||
callback(error, null); | ||
} | ||
var error = null; | ||
if (err) { | ||
error = new errors.RequestError( | ||
"Request failed with an error", | ||
url, | ||
err, | ||
res ? res.statusCode : null, | ||
res ? res.body : null | ||
); | ||
} else if (res.statusCode >= 400) { | ||
error = new errors.RequestError( | ||
"Unexpected status code " + res.statusCode, | ||
url, | ||
err, | ||
res ? res.statusCode : null, | ||
res ? res.body : null | ||
); | ||
} | ||
if (error === null) { | ||
var data = JSON.parse(res.body); | ||
callback(null, data); | ||
} else { | ||
callback(error, null); | ||
} | ||
} | ||
exports.send = send; |
@@ -9,3 +9,3 @@ var crypto = require('crypto'); | ||
function Token(secret) { | ||
this.secret = secret; | ||
this.secret = secret; | ||
} | ||
@@ -18,6 +18,6 @@ | ||
*/ | ||
Token.prototype.apiSign = function(string) { | ||
return crypto.createHmac('sha256', this.secret) | ||
.update(new Buffer(string, 'utf-8')) | ||
.digest('hex'); | ||
Token.prototype.apiSign = function (string) { | ||
return crypto.createHmac('sha256', this.secret) | ||
.update(new Buffer(string, 'utf-8')) | ||
.digest('hex'); | ||
}; | ||
@@ -32,9 +32,9 @@ | ||
*/ | ||
Token.prototype.channelSign = function(client, channel, info) { | ||
info = info || ""; | ||
return crypto.createHmac('sha256', this.secret) | ||
.update(new Buffer(client, 'utf-8')) | ||
.update(new Buffer(channel, 'utf-8')) | ||
.update(new Buffer(info, 'utf-8')) | ||
.digest('hex'); | ||
Token.prototype.channelSign = function (client, channel, info) { | ||
info = info || ""; | ||
return crypto.createHmac('sha256', this.secret) | ||
.update(new Buffer(client, 'utf-8')) | ||
.update(new Buffer(channel, 'utf-8')) | ||
.update(new Buffer(info, 'utf-8')) | ||
.digest('hex'); | ||
}; | ||
@@ -50,11 +50,11 @@ | ||
*/ | ||
Token.prototype.clientToken = function(user, timestamp, info) { | ||
info = info || ""; | ||
return crypto.createHmac('sha256', this.secret) | ||
.update(new Buffer(user, 'utf-8')) | ||
.update(new Buffer(timestamp, 'utf-8')) | ||
.update(new Buffer(info, 'utf-8')) | ||
.digest('hex'); | ||
Token.prototype.clientToken = function (user, timestamp, info) { | ||
info = info || ""; | ||
return crypto.createHmac('sha256', this.secret) | ||
.update(new Buffer(user, 'utf-8')) | ||
.update(new Buffer(timestamp, 'utf-8')) | ||
.update(new Buffer(info, 'utf-8')) | ||
.digest('hex'); | ||
}; | ||
module.exports = Token; |
{ | ||
"name": "jscent", | ||
"description": "Node.js client to interact with the Centrifugo HTTP API", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"author": "Alexandr Emelin <frvzmb@gmail.com>", | ||
@@ -6,0 +6,0 @@ "contributors": [], |
10694
281