Browse Source

Updated onMessage method in client.js.

Removed onEnd method in client.js.
Cleaned up batch methods in electrum_client.js.
Removed timeout & end listeners in initSocket method in init_socket.js.
Removed timeout, end, secureconnect & onerror listeners in connect method in TlsSocketWrapper.js.
get-transaction-merkle
Corey Phillips 4 years ago
parent
commit
3e64e965da
No known key found for this signature in database GPG Key ID: 80C0975F55D3A07B
  1. 16
      lib/TlsSocketWrapper.js
  2. 51
      lib/client.js
  3. 20
      lib/electrum_client.js
  4. 12
      lib/init_socket.js

16
lib/TlsSocketWrapper.js

@ -1,7 +1,7 @@
/**
* Simple wrapper to mimick Socket class from NET package, since TLS package havs slightly different API.
* Simple wrapper to mimick Socket class from NET package, since TLS package has slightly different API.
* We implement several methods that TCP sockets are expected to have. We will proxy call them as soon as
* realt TLS socket will be created (TLS socket created after connection).
* real TLS socket will be created (TLS socket created after connection).
*/
class TlsSocketWrapper {
constructor({ tls, verbose = false } = {}) {
@ -87,15 +87,6 @@ class TlsSocketWrapper {
this._socket.on('data', data => {
this._passOnEvent('data', data);
});
this._socket.on('end', data => {
this._passOnEvent('end', data);
});
this._socket.on('timeout', () => {
this._passOnEvent('timeout');
});
this._socket.on('onerror', data => {
this._passOnEvent('onerror', data);
});
this._socket.on('error', data => {
this._passOnEvent('error', data);
});
@ -105,9 +96,6 @@ class TlsSocketWrapper {
this._socket.on('connect', data => {
this._passOnEvent('connect', data);
});
this._socket.on('secureConnect', data => {
this._passOnEvent('secureConnect', data);
});
this._socket.on('connection', data => {
this._passOnEvent('connection', data);
});

51
lib/client.js

@ -19,15 +19,15 @@ class Client {
}
async connect() {
if(this.status) {
return Promise.resolve({ error: false, data: "" });
}
const connectionResponse = await connectSocket(this.conn, this.port, this.host);
this.status = connectionResponse.error === true ? 0 : 1;
return Promise.resolve(connectionResponse);
if (this.status) {
return Promise.resolve({error: false, data: ""});
}
const connectionResponse = await connectSocket(this.conn, this.port, this.host);
this.status = connectionResponse.error === true ? 0 : 1;
return Promise.resolve(connectionResponse);
}
close(){
close() {
if (!this.status) {
return;
}
@ -38,7 +38,7 @@ class Client {
request(method, params) {
if (!this.status) {
return Promise.reject(new Error('ESOCKET'));
return Promise.reject(new Error('Connection to server lost, please retry'));
}
return new Promise((resolve, reject) => {
const id = ++this.id;
@ -47,10 +47,10 @@ class Client {
this.conn.write(content + '\n');
});
}
requestBatch(method, params, secondParam) {
if (!this.status) {
return Promise.reject(new Error('ESOCKET'));
return Promise.reject(new Error('Connection to server lost, please retry'));
}
return new Promise((resolve, reject) => {
let arguments_far_calls = {};
@ -70,7 +70,7 @@ class Client {
this.conn.write(content + '\n');
});
}
response(msg) {
let callback;
if (!msg.id && msg[0] && msg[0].id) {
@ -84,7 +84,7 @@ class Client {
} else {
callback = this.callback_message_queue[msg.id];
}
if (callback) {
delete this.callback_message_queue[msg.id];
if (msg.error) {
@ -97,15 +97,29 @@ class Client {
}
}
onMessage(body, n){
onMessage(body, n) {
const msg = JSON.parse(body);
if (msg instanceof Array) {
this.response(msg);
} else {
if (msg.id !== void 0) {
this.response(msg)
} else {
this.subscribe.emit(msg.method, msg.params);
try {
const msg = JSON.parse(body);
if (msg instanceof Array) {
this.response(msg);
} else {
this.subscribe.emit(msg.method, msg.params);
if (msg.id !== void 0) {
this.response(msg);
} else {
this.subscribe.emit(msg.method, msg.params);
}
}
} catch (error) {
this.conn.end();
this.conn.destroy();
this.onClose(error);
}
}
}
}
@ -124,12 +138,9 @@ class Client {
this.mp.run(chunk)
}
onEnd(){
}
onError(e){
//console.log('OnError:' + e);
}
}
module.exports = Client;

20
lib/electrum_client.js

@ -347,21 +347,19 @@ class ElectrumClient extends Client{
requestBatch(method, params, secondParam) {
const parentPromise = super.requestBatch(method, params, secondParam);
return parentPromise.then(response => {
return response;
});
return parentPromise.then(response => response);
}
blockchainScripthash_getBalanceBatch(scripthash) {
return this.requestBatch('blockchain.scripthash.get_balance', scripthash);
blockchainScripthash_getBalanceBatch(scripthashes) {
return this.requestBatch('blockchain.scripthash.get_balance', scripthashes);
}
blockchainScripthash_listunspentBatch(scripthash) {
return this.requestBatch('blockchain.scripthash.listunspent', scripthash);
blockchainScripthash_listunspentBatch(scripthashes) {
return this.requestBatch('blockchain.scripthash.listunspent', scripthashes);
}
blockchainScripthash_getHistoryBatch(scripthash) {
return this.requestBatch('blockchain.scripthash.get_history', scripthash);
blockchainScripthash_getHistoryBatch(scripthashes) {
return this.requestBatch('blockchain.scripthash.get_history', scripthashes);
}
blockchainTransaction_getBatch(tx_hash, verbose) {
return this.requestBatch('blockchain.transaction.get', tx_hash, verbose);
blockchainTransaction_getBatch(tx_hashes, verbose) {
return this.requestBatch('blockchain.transaction.get', tx_hashes, verbose);
}
}

12
lib/init_socket.js

@ -29,22 +29,10 @@ const initSocket = (self, protocol, options) => {
conn.on('close', (e) => {
self.onClose(e)
})
conn.on('timeout', () => {
//self.onClose(e);
/*const e = new Error('ETIMEDOUT')
e.errorno = 'ETIMEDOUT'
e.code = 'ETIMEDOUT'
e.connect = false
conn.emit('error', e)*/
})
conn.on('data', (chunk) => {
conn.setTimeout(0)
self.onRecv(chunk)
})
conn.on('end', (e) => {
conn.setTimeout(0)
self.onEnd(e)
})
conn.on('error', (e) => {
self.onError(e)
})

Loading…
Cancel
Save