Skip to content
This repository has been archived by the owner on Dec 4, 2018. It is now read-only.

Commit

Permalink
coding style: standard
Browse files Browse the repository at this point in the history
  • Loading branch information
freewil committed Oct 26, 2015
1 parent 33d4ff9 commit fe1ab3f
Show file tree
Hide file tree
Showing 8 changed files with 476 additions and 465 deletions.
18 changes: 17 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
# node-bitcoin [![Build Status](https://travis-ci.org/freewil/node-bitcoin.svg?branch=master)](https://travis-ci.org/freewil/node-bitcoin)
# node-bitcoin
[![travis][travis-image]][travis-url]
[![npm][npm-image]][npm-url]
[![downloads][downloads-image]][downloads-url]
[![js-standard-style][standard-image]][standard-url]

[travis-image]: https://travis-ci.org/freewil/node-bitcoin.svg?branch=master
[travis-url]: https://travis-ci.org/freewil/node-bitcoin

[npm-image]: https://img.shields.io/npm/v/bitcoin.svg?style=flat
[npm-url]: https://npmjs.org/package/bitcoin

[downloads-image]: https://img.shields.io/npm/dm/bitcoin.svg?style=flat
[downloads-url]: https://npmjs.org/package/bitcoin

[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat
[standard-url]: http://standardjs.com

node-bitcoin is a simple wrapper for the Bitcoin client's JSON-RPC API.

Expand Down
4 changes: 2 additions & 2 deletions lib/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = {
getBalance: 'getbalance',
getBestBlockHash: 'getbestblockhash', // bitcoind v0.9.0+
getBlock: 'getblock',
getBlockchainInfo : 'getblockchaininfo', // bitcoind v0.9.2+
getBlockchainInfo: 'getblockchaininfo', // bitcoind v0.9.2+
getBlockCount: 'getblockcount',
getBlockHash: 'getblockhash',
getBlockTemplate: 'getblocktemplate', // bitcoind v0.7.0+
Expand Down Expand Up @@ -82,4 +82,4 @@ module.exports = {
walletLock: 'walletlock',
walletPassphrase: 'walletpassphrase',
walletPassphraseChange: 'walletpassphrasechange'
};
}
76 changes: 36 additions & 40 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,58 @@
var commands = require('./commands'),
rpc = require('./jsonrpc');
var commands = require('./commands')
var rpc = require('./jsonrpc')

//===----------------------------------------------------------------------===//
// ===----------------------------------------------------------------------===//
// Client
//===----------------------------------------------------------------------===//
function Client(opts) {
this.rpc = new rpc.Client(opts);
// ===----------------------------------------------------------------------===//
function Client (opts) {
this.rpc = new rpc.Client(opts)
}


//===----------------------------------------------------------------------===//
// ===----------------------------------------------------------------------===//
// cmd
//===----------------------------------------------------------------------===//
Client.prototype.cmd = function() {
var args = [].slice.call(arguments);
var cmd = args.shift();
// ===----------------------------------------------------------------------===//
Client.prototype.cmd = function () {
var args = [].slice.call(arguments)
var cmd = args.shift()

callRpc(cmd, args, this.rpc);
callRpc(cmd, args, this.rpc)
}


//===----------------------------------------------------------------------===//
// ===----------------------------------------------------------------------===//
// callRpc
//===----------------------------------------------------------------------===//
function callRpc(cmd, args, rpc) {
var fn = args[args.length-1];
// ===----------------------------------------------------------------------===//
function callRpc (cmd, args, rpc) {
var fn = args[args.length - 1]

// If the last argument is a callback, pop it from the args list
if (typeof fn === 'function') {
args.pop();
args.pop()
} else {
fn = function() {};
fn = function () {}
}

rpc.call(cmd, args, function(){
var args = [].slice.call(arguments);
args.unshift(null);
fn.apply(this, args);
}, function(err){
fn(err);
});
rpc.call(cmd, args, function () {
var args = [].slice.call(arguments)
args.unshift(null)
fn.apply(this, args)
}, function (err) {
fn(err)
})
}

//===----------------------------------------------------------------------===//
// ===----------------------------------------------------------------------===//
// Initialize wrappers
//===----------------------------------------------------------------------===//
(function() {

// ===----------------------------------------------------------------------===//
(function () {
for (var protoFn in commands) {
(function(protoFn) {
Client.prototype[protoFn] = function() {
var args = [].slice.call(arguments);
callRpc(commands[protoFn], args, this.rpc);
};
})(protoFn);
(function (protoFn) {
Client.prototype[protoFn] = function () {
var args = [].slice.call(arguments)
callRpc(commands[protoFn], args, this.rpc)
}
})(protoFn)
}

})();
})()

// Export!
module.exports.Client = Client;
module.exports.Client = Client
147 changes: 73 additions & 74 deletions lib/jsonrpc.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
var http = require('http'),
https = require('https');
var http = require('http')
var https = require('https')

var Client = function(opts) {
this.opts = opts || {};
this.http = this.opts.ssl ? https : http;
};
var Client = function (opts) {
this.opts = opts || {}
this.http = this.opts.ssl ? https : http
}

Client.prototype.call = function(method, params, callback, errback, path) {
var time = Date.now();
var requestJSON;
Client.prototype.call = function (method, params, callback, errback, path) {
var time = Date.now()
var requestJSON

if (Array.isArray(method)) {
// multiple rpc batch call
requestJSON = [];
method.forEach(function(batchCall, i) {
requestJSON = []
method.forEach(function (batchCall, i) {
requestJSON.push({
id: time + '-' + i,
method: batchCall.method,
params: batchCall.params
});
});
})
})
} else {
// single rpc call
requestJSON = {
id: time,
method: method,
params: params
};
}
}

// First we encode the request into JSON
var requestJSON = JSON.stringify(requestJSON);
requestJSON = JSON.stringify(requestJSON)

// prepare request options
var requestOptions = {
Expand All @@ -44,113 +44,112 @@ Client.prototype.call = function(method, params, callback, errback, path) {
},
agent: false,
rejectUnauthorized: this.opts.ssl && this.opts.sslStrict !== false
};
}

if (this.opts.ssl && this.opts.sslCa) {
requestOptions.ca = this.opts.sslCa;
requestOptions.ca = this.opts.sslCa
}

// use HTTP auth if user and password set
if (this.opts.user && this.opts.pass) {
requestOptions.auth = this.opts.user + ':' + this.opts.pass;
requestOptions.auth = this.opts.user + ':' + this.opts.pass
}

// Now we'll make a request to the server
var cbCalled = false;
var request = this.http.request(requestOptions);
var cbCalled = false
var request = this.http.request(requestOptions)

// start request timeout timer
var reqTimeout = setTimeout(function() {
if (cbCalled) return;
cbCalled = true;
request.abort();
var err = new Error('ETIMEDOUT');
err.code = 'ETIMEDOUT';
errback(err);
}, this.opts.timeout || 30000);
var reqTimeout = setTimeout(function () {
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;
cbCalled = true;
request.abort();
request.setTimeout(this.opts.timeout || 30000, function () {
if (cbCalled) return
cbCalled = true
request.abort()
var err = new Error('ESOCKETTIMEDOUT')
err.code = 'ESOCKETTIMEDOUT'
errback(err);
});
errback(err)
})

request.on('error', function(err) {
if (cbCalled) return;
cbCalled = true;
clearTimeout(reqTimeout);
errback(err);
});
request.on('error', function (err) {
if (cbCalled) return
cbCalled = true
clearTimeout(reqTimeout)
errback(err)
})

request.on('response', function(response) {
clearTimeout(reqTimeout);
request.on('response', function (response) {
clearTimeout(reqTimeout)

// We need to buffer the response chunks in a nonblocking way.
var buffer = '';
response.on('data', function(chunk) {
buffer = buffer + chunk;
});
var buffer = ''
response.on('data', function (chunk) {
buffer = buffer + chunk
})
// 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;
response.on('end', function () {
var err

if (cbCalled) return;
cbCalled = true;
if (cbCalled) return
cbCalled = true

try {
var decoded = JSON.parse(buffer);
var decoded = JSON.parse(buffer)
} catch (e) {
if (response.statusCode !== 200) {
err = new Error('Invalid params, response status code: ' + response.statusCode);
err.code = -32602;
errback(err);
err = new Error('Invalid params, response status code: ' + response.statusCode)
err.code = -32602
errback(err)
} else {
err = new Error('Problem parsing JSON response from server');
err.code = -32603;
errback(err);
err = new Error('Problem parsing JSON response from server')
err.code = -32603
errback(err)
}
return;
return
}

if (!Array.isArray(decoded)) {
decoded = [decoded];
decoded = [decoded]
}

// iterate over each response, normally there will be just one
// unless a batch rpc call response is being processed
decoded.forEach(function(decodedResponse, i) {
decoded.forEach(function (decodedResponse, i) {
if (decodedResponse.hasOwnProperty('error') && decodedResponse.error != null) {
if (errback) {
err = new Error(decodedResponse.error.message || '');
err = new Error(decodedResponse.error.message || '')
if (decodedResponse.error.code) {
err.code = decodedResponse.error.code;
err.code = decodedResponse.error.code
}
errback(err);
errback(err)
}
} else if (decodedResponse.hasOwnProperty('result')) {
if (callback) {
callback(decodedResponse.result, response.headers);
callback(decodedResponse.result, response.headers)
}
} else {
if (errback) {
err = new Error(decodedResponse.error.message || '');
err = new Error(decodedResponse.error.message || '')
if (decodedResponse.error.code) {
err.code = decodedResponse.error.code;
err.code = decodedResponse.error.code
}
errback(err);
errback(err)
}
}
});

});
});
request.end(requestJSON);
};
})
})
})
request.end(requestJSON)
}

module.exports.Client = Client;
module.exports.Client = Client
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"dependencies": {},
"devDependencies": {
"clone": "^1.0.2",
"mocha": "^2.3.3"
"mocha": "^2.3.3",
"standard": "^5.3.1"
},
"optionalDependencies": {},
"repository": {
Expand All @@ -25,6 +26,7 @@
"node": ">= 0.10.0"
},
"scripts": {
"pretest": "standard --verbose",
"test": "make test"
},
"bugs": {
Expand Down
Loading

0 comments on commit fe1ab3f

Please sign in to comment.