Code cleanup
-Replace tabs with double-spaces -Betting spacing and lining up of code functions -Add missing semi-colons -Remove extra characters and spaces where applicable -Remove commented-out code fragments -Add missing 2021 date to LICENSE -Small touchups and other misc nitpickings
This commit is contained in:
+34
-18
@@ -13,6 +13,7 @@ Client.prototype.call = function(method, params, callback, errback, path) {
|
||||
if (Array.isArray(method)) {
|
||||
// multiple rpc batch call
|
||||
requestJSON = [];
|
||||
|
||||
method.forEach(function(batchCall, i) {
|
||||
requestJSON.push({
|
||||
id: time + '-' + i,
|
||||
@@ -29,7 +30,7 @@ Client.prototype.call = function(method, params, callback, errback, path) {
|
||||
};
|
||||
}
|
||||
|
||||
// First we encode the request into JSON
|
||||
// first we encode the request into JSON
|
||||
var requestJSON = JSON.stringify(requestJSON);
|
||||
|
||||
// prepare request options
|
||||
@@ -51,36 +52,45 @@ Client.prototype.call = function(method, params, callback, errback, path) {
|
||||
}
|
||||
|
||||
// use HTTP auth if user and password set
|
||||
if (this.opts.username && this.opts.password) {
|
||||
if (this.opts.username && this.opts.password)
|
||||
requestOptions.auth = this.opts.username + ':' + this.opts.password;
|
||||
}
|
||||
|
||||
// Now we'll make a request to the server
|
||||
// now make a request to the server
|
||||
var cbCalled = false
|
||||
var request = this.http.request(requestOptions);
|
||||
|
||||
// start request timeout timer
|
||||
var reqTimeout = setTimeout(function() {
|
||||
if (cbCalled) return;
|
||||
if (cbCalled)
|
||||
return;
|
||||
|
||||
cbCalled = true;
|
||||
request.abort();
|
||||
|
||||
var err = new Error('ETIMEDOUT');
|
||||
|
||||
err.code = 'ETIMEDOUT';
|
||||
errback(err);
|
||||
}, this.opts.timeout || 30000);
|
||||
|
||||
// set additional timeout on socket in case of remote freeze after sending headers
|
||||
request.setTimeout(this.opts.timeout || 30000, function() {
|
||||
if (cbCalled) return;
|
||||
if (cbCalled)
|
||||
return;
|
||||
|
||||
cbCalled = true;
|
||||
request.abort();
|
||||
|
||||
var err = new Error('ESOCKETTIMEDOUT');
|
||||
|
||||
err.code = 'ESOCKETTIMEDOUT';
|
||||
errback(err);
|
||||
});
|
||||
|
||||
request.on('error', function(err) {
|
||||
if (cbCalled) return;
|
||||
if (cbCalled)
|
||||
return;
|
||||
|
||||
cbCalled = true;
|
||||
clearTimeout(reqTimeout);
|
||||
errback(err);
|
||||
@@ -89,18 +99,22 @@ Client.prototype.call = function(method, params, callback, errback, path) {
|
||||
request.on('response', function(response) {
|
||||
clearTimeout(reqTimeout);
|
||||
|
||||
// We need to buffer the response chunks in a nonblocking way.
|
||||
// we need to buffer the response chunks in a nonblocking way
|
||||
var buffer = '';
|
||||
|
||||
response.on('data', function(chunk) {
|
||||
buffer = buffer + chunk;
|
||||
});
|
||||
// When all the responses are finished, we decode the JSON and
|
||||
|
||||
// when all the responses are finished, we decode the JSON and
|
||||
// depending on whether it's got a result or an error, we call
|
||||
// emitSuccess or emitError on the promise.
|
||||
response.on('end', function() {
|
||||
var err;
|
||||
|
||||
if (cbCalled) return;
|
||||
if (cbCalled)
|
||||
return;
|
||||
|
||||
cbCalled = true;
|
||||
|
||||
try {
|
||||
@@ -115,12 +129,12 @@ Client.prototype.call = function(method, params, callback, errback, path) {
|
||||
err.code = -32603;
|
||||
errback(err);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Array.isArray(decoded)) {
|
||||
if (!Array.isArray(decoded))
|
||||
decoded = [decoded];
|
||||
}
|
||||
|
||||
// iterate over each response, normally there will be just one
|
||||
// unless a batch rpc call response is being processed
|
||||
@@ -128,27 +142,29 @@ Client.prototype.call = function(method, params, callback, errback, path) {
|
||||
if (decodedResponse.hasOwnProperty('error') && decodedResponse.error != null) {
|
||||
if (errback) {
|
||||
err = new Error(decodedResponse.error.message || '');
|
||||
if (decodedResponse.error.code) {
|
||||
|
||||
if (decodedResponse.error.code)
|
||||
err.code = decodedResponse.error.code;
|
||||
}
|
||||
|
||||
errback(err);
|
||||
}
|
||||
} else if (decodedResponse.hasOwnProperty('result')) {
|
||||
if (callback) {
|
||||
if (callback)
|
||||
callback(decodedResponse.result, response.headers);
|
||||
}
|
||||
} else {
|
||||
if (errback) {
|
||||
err = new Error(decodedResponse.error.message || '');
|
||||
if (decodedResponse.error.code) {
|
||||
|
||||
if (decodedResponse.error.code)
|
||||
err.code = decodedResponse.error.code;
|
||||
}
|
||||
|
||||
errback(err);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
request.on('error', errback);
|
||||
request.end(requestJSON);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user