Comparing version 1.4.0 to 1.5.0
@@ -17,9 +17,27 @@ var util = require('util'); | ||
this.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.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({ | ||
'id': '' + (new Date()).getTime(), | ||
'method': method, | ||
'params': params | ||
}); | ||
var requestJSON = JSON.stringify(requestJSON); | ||
@@ -75,28 +93,35 @@ // prepare request options | ||
if (decoded.hasOwnProperty('error') && decoded.error != null) { | ||
if (errback) { | ||
err = new Error(decoded.error.message || ''); | ||
if (decoded.error.code) { | ||
err.code = decoded.error.code; | ||
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 | ||
decoded.forEach(function(decodedResponse, i) { | ||
if (decodedResponse.hasOwnProperty('error') && decodedResponse.error != null) { | ||
if (errback) { | ||
err = new Error(decodedResponse.error.message || ''); | ||
if (decodedResponse.error.code) { | ||
err.code = decodedResponse.error.code; | ||
} | ||
errback(err); | ||
} | ||
errback(err); | ||
} | ||
} else if (decoded.hasOwnProperty('result')) { | ||
if (callback) { | ||
callback(decoded.result); | ||
} | ||
} else { | ||
if (errback) { | ||
err = new Error(decoded.error.message || ''); | ||
if (decoded.error.code) { | ||
err.code = decoded.error.code; | ||
} else if (decodedResponse.hasOwnProperty('result')) { | ||
if (callback) { | ||
callback(decodedResponse.result); | ||
} | ||
errback(err); | ||
} else { | ||
if (errback) { | ||
err = new Error(decodedResponse.error.message || ''); | ||
if (decodedResponse.error.code) { | ||
err.code = decodedResponse.error.code; | ||
} | ||
errback(err); | ||
} | ||
} | ||
} | ||
}); | ||
}); | ||
}); | ||
request.write(requestJSON); | ||
request.end(); | ||
request.end(requestJSON); | ||
}; | ||
@@ -103,0 +128,0 @@ } |
{ | ||
"name": "bitcoin", | ||
"description": "Communicate with bitcoind via JSON-RPC", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"main": "./lib/bitcoin", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
@@ -67,1 +67,17 @@ # node-bitcoin | ||
``` | ||
### Batch multiple RPC calls into single HTTP request | ||
```js | ||
var batch = []; | ||
for (var i = 0; i < 10; ++i) { | ||
batch.push({ | ||
method: 'getnewaddress', | ||
params: ['myaccount'] | ||
}); | ||
} | ||
client.cmd(batch, function(err, address) { | ||
if (err) return console.log(err); | ||
console.log('Address:', address); | ||
}); | ||
``` |
@@ -28,2 +28,4 @@ var path = require('path'); | ||
var batchCallbackCount = 0; | ||
vows.describe('api').addBatch({ | ||
@@ -64,3 +66,9 @@ '': { | ||
topic: function(client){ | ||
client.getAddressesByAccount(test.account, this.callback); | ||
var self = this; | ||
client.getNewAddress(test.account, function(err, address) { | ||
if (err) { | ||
return self.callback(err); | ||
} | ||
client.getAddressesByAccount(test.account, self.callback); | ||
}); | ||
}, | ||
@@ -122,24 +130,2 @@ 'is not empty': function(addresses) { | ||
}, | ||
'getblocknumber is deprecated': { | ||
topic: function(client) { | ||
client.cmd('getblocknumber', this.callback); | ||
}, | ||
'and has been replaced by getblockcount': { | ||
topic: function(number, client) { | ||
client.cmd('getblockcount', this.callback); | ||
}, | ||
'getBlockNumber uses getblockcount': { | ||
topic: function(count, number, client) { | ||
var self = this; | ||
client.getBlockNumber(function(err, number2) { | ||
self.callback(err, number2, count, number); | ||
}); | ||
}, | ||
'and should match both': function(err, number2, count, number) { | ||
assert.equal(number2, count); | ||
assert.equal(number2, number); | ||
} | ||
} | ||
} | ||
}, | ||
'creating a bitcoin related error': { | ||
@@ -156,2 +142,25 @@ topic: function(client) { | ||
}, | ||
'running batch of rpc calls': { | ||
topic: function(client) { | ||
// create batch of calls to get 10 new addresses | ||
var batch = []; | ||
for (var i = 0; i < 10; ++i) { | ||
batch.push({ | ||
method: 'getnewaddress', | ||
params: [test.account] | ||
}); | ||
} | ||
var self = this; | ||
client.cmd(batch, function(err, address) { | ||
assert.isTrue(++batchCallbackCount <= 10); | ||
if (batchCallbackCount === 10) { | ||
self.callback(err, address); | ||
} | ||
}); | ||
}, | ||
'should receive new address': function(err, address){ | ||
assert.equal(err, null); | ||
assert.ok(address); | ||
} | ||
} | ||
}, | ||
@@ -158,0 +167,0 @@ 'invalid credentials': { |
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
Sorry, the diff of this file is not supported yet
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 6 instances in 1 package
2174426
560
83
1
22