mirror of
https://github.com/bigchaindb/js-bigchaindb-driver.git
synced 2024-11-22 01:36:56 +01:00
remove boolean comparison for numbers & add docs
This commit is contained in:
parent
f5debab03a
commit
a99ccd57f1
@ -45,11 +45,36 @@ To do so, you need to pass the **app_id and app_key**.
|
||||
|
||||
.. code-block:: js
|
||||
|
||||
let conn = new driver.Connection('https://test.bigchaindb.com/api/v1/', {
|
||||
const conn = new driver.Connection('https://test.bigchaindb.com/api/v1/', {
|
||||
app_id: 'Get one from testnet.bigchaindb.com',
|
||||
app_key: 'Get one from testnet.bigchaindb.com'
|
||||
})
|
||||
|
||||
A more complex connection can be created if the intention is to connect to
|
||||
different nodes of a BigchainDB network.
|
||||
The connection strategy will be the one specified in the BEP-14_
|
||||
|
||||
.. _BEP-14: https://github.com/bigchaindb/BEPs/tree/master/14#connection-strategy
|
||||
|
||||
.. code-block:: js
|
||||
|
||||
const conn = new driver.Connection([
|
||||
'https://test.bigchaindb.com', // the first node does not use custom headers, only common headers
|
||||
{endpoint: 'https://test.bigchaindb.com/api/v1/',
|
||||
headers: {app_id: 'your_app_id',
|
||||
app_key: 'your_app_key'}},
|
||||
{endpoint: 'https://test2.bigchaindb.com/api/v1/',
|
||||
headers: {app_id: 'your_app_id',
|
||||
app_key: 'your_app_key',
|
||||
extra_header: 'extra value'}},
|
||||
{endpoint: 'https://test3.bigchaindb.com/api/v1/',
|
||||
headers: {app_id: 'your_app_id',
|
||||
app_key: 'your_app_key',
|
||||
other_header: 'other value'}},
|
||||
{endpoint: 'https://test4.bigchaindb.com/api/v1/',
|
||||
headers: {custom_auth: 'custom token'}],
|
||||
{'Content-Type': 'application/json'}, // this header is used by all nodes)
|
||||
|
||||
Cryptographic Identities Generation
|
||||
-----------------------------------
|
||||
Alice and Bob are represented by public/private key pairs. The private key is
|
||||
|
@ -19,7 +19,7 @@ const DEFAULT_TIMEOUT = 20000 // The default value is 20 seconds
|
||||
*/
|
||||
|
||||
export default class Connection {
|
||||
// 20 seconds is the default value for a timeout if not specified
|
||||
// This driver implements the BEP-14 https://github.com/bigchaindb/BEPs/tree/master/14
|
||||
constructor(nodes, headers = {}, timeout = DEFAULT_TIMEOUT) {
|
||||
// Copy object
|
||||
this.headers = Object.assign({}, headers)
|
||||
@ -42,7 +42,7 @@ export default class Connection {
|
||||
this.normalizedNodes.push(Connection.normalizeNode(nodes, this.headers))
|
||||
}
|
||||
|
||||
this.transport = new Transport(this.normalizedNodes, timeout) // TODO
|
||||
this.transport = new Transport(this.normalizedNodes, timeout)
|
||||
}
|
||||
|
||||
static normalizeNode(node, headers) {
|
||||
|
@ -42,7 +42,7 @@ export default class Transport {
|
||||
let response
|
||||
let connection
|
||||
// A new request will be executed until there is a valid response or timeout < 0
|
||||
while (!this.timeout || this.timeout > 0) {
|
||||
while (this.timeout >= 0) {
|
||||
connection = this.pickConnection()
|
||||
// Date in milliseconds
|
||||
const startTime = Date.now()
|
||||
@ -55,7 +55,7 @@ export default class Transport {
|
||||
this.maxBackoffTime
|
||||
)
|
||||
const elapsed = Date.now() - startTime
|
||||
if (connection.backoffTime && this.timeout) {
|
||||
if (connection.backoffTime > 0 && this.timeout > 0) {
|
||||
this.timeout -= elapsed
|
||||
} else {
|
||||
// No connection error, the response is valid
|
||||
|
Loading…
Reference in New Issue
Block a user