Skip to content

Commit

Permalink
Timeout and and server response configutations added
Browse files Browse the repository at this point in the history
  • Loading branch information
sambua committed Jan 14, 2017
1 parent fcdd3ef commit 76ac5e7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
node_modules
.idea/

npm-debug.log
*.swp
.DS_Store
33 changes: 30 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ module.exports = ZiggeoSdk;

ZiggeoSdk.Config = {
local: false,
server_api_url: "srvapi.ziggeo.com"
server_api_url: "srvapi.ziggeo.com",
requestTimeout: 30 * 1000 // Set 30 seconds server response timeout
};

ZiggeoSdk.Connect = {
Expand All @@ -22,6 +23,7 @@ ZiggeoSdk.Connect = {
var obj = {
host: meta.host ? meta.host : ZiggeoSdk.Config.server_api_url,
ssl: "ssl" in meta ? meta.ssl : !ZiggeoSdk.Config.local,
timeout: ZiggeoSdk.Config.requestTimeout,
path: path,
method: method,
headers: {}
Expand All @@ -47,6 +49,8 @@ ZiggeoSdk.Connect = {
requestChunks: function (method, path, callbacks, data, file, meta, post_process_data) {
var options = this.__options(method, path, meta);
var post_data = null;
var timeout = ZiggeoSdk.Config.timeout;

if (data) {
if (method == "GET") {
options.path = options.path + "?" + this.__querystring.stringify(data);
Expand All @@ -59,6 +63,11 @@ ZiggeoSdk.Connect = {
}
}
var provider = options.ssl ? this.__https : this.__http;

// Ignore invalid self-signed ssl certificate for testing purposes
if( ZiggeoSdk.Config.local )
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

var request = provider.request(options, function (result) {
var data = [];
result.on("data", function (chunk) {
Expand All @@ -74,11 +83,29 @@ ZiggeoSdk.Connect = {
callbacks(data);
}
} else {
if (callbacks.failure)
callbacks.failure(data);
if (callbacks.failure)
callbacks.failure(data);
else
callbacks(new Error("Error with status code: " + result.statusCode + '/' + result.statusMessage + '. Please be sure correct arguments provided.'));
}
});
});

request.setTimeout( timeout, function() {});

request
.on('socket', function(socket) {
socket.removeAllListeners('timeout'); // remove node's default listener
socket.setTimeout( timeout, function(){ }); // there shouldn't be of 'inactivity'
socket.on('timeout', function() {
callbacks(new Error('Connection timed out. Socket couldn\'t connect to server in ' + timeout/1000 + ' seconds.'));
});
}).on('timeout', function() {
callbacks(new Error('Connection timed out. Couldn\'t connect to server in ' + timeout/1000 + ' seconds.'));
}).on('error', function (e) {
console.log('Got error: ' + e.message ) ;
});

if (file) {
var boundaryKey = Math.random().toString(16);
request.setHeader('Content-Type', 'multipart/form-data; boundary="'+boundaryKey+'"');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"version": "0.0.15",
"author": "Ziggeo",
"repository": "https://github.com/Ziggeo/ZiggeoNodeSdk.git",
"license": "Apache 2.0",
"license": "Apache-2.0",
"engines": {
"node": ""
}
Expand Down

0 comments on commit 76ac5e7

Please sign in to comment.