tendermint
Advanced tools
Comparing version 1.1.1 to 1.2.0
@@ -7,2 +7,8 @@ 'use strict'; | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
var EventEmitter = require('events'); | ||
var _require = require('request'), | ||
@@ -14,2 +20,5 @@ get = _require.get; | ||
var camel = require('camelcase'); | ||
var websocket = require('websocket-stream'); | ||
var ndjson = require('ndjson'); | ||
var pumpify = require('pumpify').obj; | ||
var tendermintMethods = require('./methods.js'); | ||
@@ -29,3 +38,5 @@ | ||
var Client = function () { | ||
var Client = function (_EventEmitter) { | ||
_inherits(Client, _EventEmitter); | ||
function Client() { | ||
@@ -36,12 +47,38 @@ var uriString = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'localhost:46657'; | ||
var _this = _possibleConstructorReturn(this, (Client.__proto__ || Object.getPrototypeOf(Client)).call(this)); | ||
var uri = url.parse(uriString); | ||
if (uri.protocol !== 'http' && uri.protocol !== 'tcp') { | ||
if (uri.protocol !== 'http:' && uri.protocol !== 'ws:') { | ||
uri = url.parse('http://' + uriString); | ||
} | ||
this.uri = 'http://' + uri.hostname + ':' + uri.port + '/'; | ||
if (uri.protocol === 'ws:') { | ||
_this.websocket = true; | ||
_this.uri = 'ws://' + uri.hostname + ':' + uri.port + '/websocket'; | ||
_this.call = _this.callWs; | ||
_this.connectWs(); | ||
} else if (uri.protocol === 'http:') { | ||
_this.uri = 'http://' + uri.hostname + ':' + uri.port + '/'; | ||
_this.call = _this.callHttp; | ||
} | ||
return _this; | ||
} | ||
_createClass(Client, [{ | ||
key: 'call', | ||
value: function call(method, args, cb) { | ||
key: 'connectWs', | ||
value: function connectWs() { | ||
var _this2 = this; | ||
this.ws = pumpify(ndjson.stringify(), websocket(this.uri)); | ||
this.ws.on('error', function (err) { | ||
return _this2.emit('error', err); | ||
}); | ||
this.ws.on('data', function (data) { | ||
data = JSON.parse(data); | ||
if (!data.id) return; | ||
_this2.emit(data.id, data.error || null, data.result); | ||
}); | ||
} | ||
}, { | ||
key: 'callHttp', | ||
value: function callHttp(method, args, cb) { | ||
get({ | ||
@@ -61,6 +98,20 @@ uri: this.uri + method, | ||
} | ||
}, { | ||
key: 'callWs', | ||
value: function callWs(method, args, cb) { | ||
var params = []; | ||
for (var k in args) { | ||
params.push(args[k]); | ||
}var id = Math.random().toString(36); | ||
if (method === 'subscribe') { | ||
this.on(id + '#event', cb); | ||
} else { | ||
this.once(id, cb); | ||
} | ||
this.ws.write({ jsonrpc: '2.0', id: id, method: method, params: params }); | ||
} | ||
}]); | ||
return Client; | ||
}(); | ||
}(EventEmitter); | ||
@@ -67,0 +118,0 @@ // add methods to Client class based on methods defined in './methods.js' |
{ | ||
"name": "tendermint", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "A light client which talks to your Tendermint node over RPC", | ||
@@ -16,4 +16,7 @@ "main": "index.js", | ||
"camelcase": "^4.0.0", | ||
"ndjson": "^1.5.0", | ||
"old": "^0.1.3", | ||
"request": "^2.79.0" | ||
"pumpify": "^1.3.5", | ||
"request": "^2.79.0", | ||
"websocket-stream": "^3.3.3" | ||
}, | ||
@@ -20,0 +23,0 @@ "devDependencies": { |
'use strict' | ||
const EventEmitter = require('events') | ||
const { get } = require('request') | ||
@@ -7,2 +8,5 @@ const url = require('url') | ||
const camel = require('camelcase') | ||
const websocket = require('websocket-stream') | ||
const ndjson = require('ndjson') | ||
const pumpify = require('pumpify').obj | ||
const tendermintMethods = require('./methods.js') | ||
@@ -22,12 +26,34 @@ | ||
class Client { | ||
class Client extends EventEmitter { | ||
constructor (uriString = 'localhost:46657') { | ||
super() | ||
let uri = url.parse(uriString) | ||
if (uri.protocol !== 'http' && uri.protocol !== 'tcp') { | ||
if (uri.protocol !== 'http:' && uri.protocol !== 'ws:') { | ||
uri = url.parse(`http://${uriString}`) | ||
} | ||
this.uri = `http://${uri.hostname}:${uri.port}/` | ||
if (uri.protocol === 'ws:') { | ||
this.websocket = true | ||
this.uri = `ws://${uri.hostname}:${uri.port}/websocket` | ||
this.call = this.callWs | ||
this.connectWs() | ||
} else if (uri.protocol === 'http:') { | ||
this.uri = `http://${uri.hostname}:${uri.port}/` | ||
this.call = this.callHttp | ||
} | ||
} | ||
call (method, args, cb) { | ||
connectWs () { | ||
this.ws = pumpify( | ||
ndjson.stringify(), | ||
websocket(this.uri) | ||
) | ||
this.ws.on('error', (err) => this.emit('error', err)) | ||
this.ws.on('data', (data) => { | ||
data = JSON.parse(data) | ||
if (!data.id) return | ||
this.emit(data.id, data.error || null, data.result) | ||
}) | ||
} | ||
callHttp (method, args, cb) { | ||
get({ | ||
@@ -47,2 +73,15 @@ uri: this.uri + method, | ||
} | ||
callWs (method, args, cb) { | ||
let params = [] | ||
for (let k in args) params.push(args[k]) | ||
let id = Math.random().toString(36) | ||
if (method === 'subscribe') { | ||
this.on(id + '#event', cb) | ||
} else { | ||
this.once(id, cb) | ||
} | ||
this.ws.write({ jsonrpc: '2.0', id, method, params }) | ||
} | ||
} | ||
@@ -49,0 +88,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10625928
249
7
+ Addedndjson@^1.5.0
+ Addedpumpify@^1.3.5
+ Addedwebsocket-stream@^3.3.3
+ Addedcore-util-is@1.0.3(transitive)
+ Addedduplexify@3.7.1(transitive)
+ Addedend-of-stream@1.4.4(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedndjson@1.5.0(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedoptions@0.0.6(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedpump@2.0.1(transitive)
+ Addedpumpify@1.5.1(transitive)
+ Addedreadable-stream@2.3.8(transitive)
+ Addedsafe-buffer@5.1.2(transitive)
+ Addedsplit2@2.2.0(transitive)
+ Addedstream-shift@1.0.3(transitive)
+ Addedstring_decoder@1.1.1(transitive)
+ Addedthrough2@2.0.5(transitive)
+ Addedultron@1.0.2(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedwebsocket-stream@3.3.3(transitive)
+ Addedwrappy@1.0.2(transitive)
+ Addedws@1.1.5(transitive)
+ Addedxtend@4.0.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)