-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
58 lines (53 loc) · 1.92 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* @file index.js
* @fileOverview Index file for the eris-db javascript API. This file contains a factory method
* for creating a new <tt>ErisDB</tt> instance.
* @author Andreas Olofsson (andreas@erisindustries.com)
* @module index
*/
'use strict';
var erisdb = require('./lib/erisdb');
var validation = require('./lib/validation');
var WebSocketClient = require('./lib/rpc/websocket');
var HTTPClient = require('./lib/rpc/http');
var clients = require('./lib/rpc/clients');
var url = require('url');
/**
* ErisDB allows you to do remote calls to a running erisdb-tendermint client.
*
* NOTE: optional 'websocket' second param is deprecated.
*
* @param {string} URL The RPC endpoint URL.
* @returns {module:erisdb-ErisDB}
*/
exports.createInstance = function(URL){
var client;
if(!URL || typeof(URL) !== "string" || URL === ""){
URL = 'http://localhost:1337/rpc';
}
var parsed = url.parse(URL);
parsed.protocol = parsed.protocol.slice(0,-1);
if(parsed.protocol === 'ws' || parsed.protocol === 'wss'){
client = new WebSocketClient(URL);
} else if (parsed.protocol === 'http' || parsed.protocol === 'https'){
client = new HTTPClient(URL);
} else {
throw new Error("Protocol not supported: " + parsed.protocol);
}
var validator = new validation.SinglePolicyValidator(true);
return erisdb.createInstance(client, validator);
};
exports.clients = clients;
/**
* ErisDB allows you to do remote calls to a running erisdb-tendermint client.
*
* @param {module:rpc/client~Client} client - A client object.
* @param {module:validation~CallValidator} [validator] - a validator for determining if unsafe operations can be done.
* @returns {module:erisdb-ErisDB}
*/
exports.createInstanceFromClient = function(client, validator){
if(!validator){
validator = new validation.SinglePolicyValidator(true);
}
return erisdb.createInstance(client, validator);
};