From bc9fb9c0b0c07714d26ab0261b49ad97c84756f0 Mon Sep 17 00:00:00 2001 From: lovelycs <419579547@qq.com> Date: Mon, 4 Mar 2019 14:48:13 +0800 Subject: [PATCH] bug fix --- src/client/index.ts | 12 +++++++++++- src/provider/Communication/ipc_ws.js | 14 +++++++++++++- src/provider/IPC.js | 18 ++++++++--------- src/provider/WS.js | 29 ++++++++++++++-------------- src/utils/abi/coder/dynamic.ts | 2 +- test/utils/abi.js | 5 +++++ yarn.lock | 8 -------- 7 files changed, 53 insertions(+), 35 deletions(-) diff --git a/src/client/index.ts b/src/client/index.ts index 608edf89..9516b6c2 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -159,8 +159,18 @@ export default class client { return reps; } - async subscribe() { + async subscribe(...args) { + if (!this.isConnected) { + return this._onReq('request', 'subscribe_subscribe', ...args); + } + + const rep: RPCresponse = await this._provider.request('subscribe_subscribe', ...args); + if (rep.error) { + throw rep.error + }; + // const subscription = rep.result; + // this._provider.on() } async unSubscribe() { diff --git a/src/provider/Communication/ipc_ws.js b/src/provider/Communication/ipc_ws.js index 82267015..8db06b30 100644 --- a/src/provider/Communication/ipc_ws.js +++ b/src/provider/Communication/ipc_ws.js @@ -2,10 +2,11 @@ import Communication from './index.js'; class IPC_WS extends Communication { constructor({ - onEventTypes, sendFuncName + onEventTypes, sendFuncName, path }) { super(); + this.path = path; this._onEventTypes = onEventTypes || []; this._sendFuncName = sendFuncName; @@ -42,6 +43,7 @@ class IPC_WS extends Communication { try { let res = JSON.parse(ele); + // console.log('ele', ele); if ( !(res instanceof Array) && res.result ) { // Compatible: somtimes data.result is a json string, sometimes not. try { @@ -49,6 +51,8 @@ class IPC_WS extends Communication { } catch (e) { // console.log(e); } + } else { + // console.log('res', res); } results.push(res); @@ -145,6 +149,14 @@ class IPC_WS extends Communication { }); } + _send(payloads) { + if (!this.connectStatus) { + return Promise.reject( this.ERRORS.CONNECT(this.path) ); + } + this.socket[this._sendFuncName]( JSON.stringify(payloads) ); + return this._onSend(payloads); + } + on (type, cb) { let eventType = this._checkOnType(type); if ( eventType < 0 ) { diff --git a/src/provider/IPC.js b/src/provider/IPC.js index 66c62743..ab6c7c5d 100644 --- a/src/provider/IPC.js +++ b/src/provider/IPC.js @@ -9,7 +9,8 @@ class IPC_RPC extends IPC_WS { }) { super({ onEventTypes: ['error', 'end', 'timeout', 'data', 'close', 'connect'], - sendFuncName: 'write' + sendFuncName: 'write', + path }); if (!path) { @@ -18,7 +19,6 @@ class IPC_RPC extends IPC_WS { } this.type = 'ipc'; - this.path = path; this.timeout = timeout; this.delimiter = options.delimiter; @@ -66,13 +66,13 @@ class IPC_RPC extends IPC_WS { }); } - _send(payloads) { - if (!this.connectStatus) { - return Promise.reject( this.ERRORS.CONNECT(this.path) ); - } - this.socket.write( JSON.stringify(payloads) ); - return this._onSend(payloads); - } + // _send(payloads) { + // if (!this.connectStatus) { + // return Promise.reject( this.ERRORS.CONNECT(this.path) ); + // } + // this.socket.write( JSON.stringify(payloads) ); + // return this._onSend(payloads); + // } reconnect() { this.socket.connect({ path: this.path }); diff --git a/src/provider/WS.js b/src/provider/WS.js index 805e9cca..f3254f89 100644 --- a/src/provider/WS.js +++ b/src/provider/WS.js @@ -2,7 +2,7 @@ import IPC_WS from './Communication/ipc_ws'; const websocket = require('websocket').w3cwebsocket; class WS_RPC extends IPC_WS { - constructor(url = 'ws://localhost:31420', timeout = 60000, options = { + constructor(path = 'ws://localhost:31420', timeout = 60000, options = { protocol: '', headers: '', clientConfig: '', @@ -11,16 +11,15 @@ class WS_RPC extends IPC_WS { }) { super({ onEventTypes: ['error', 'close', 'connect'], - sendFuncName: 'send' + sendFuncName: 'send', + path }); - if (!url) { - console.error( this.ERRORS.CONNECT(url) ); - return this.ERRORS.CONNECT(url); + if (!path) { + console.error( this.ERRORS.CONNECT(path) ); + return this.ERRORS.CONNECT(path); } - this.type = 'ws'; - this.url = url; this.timeout = timeout; this.protocol = options.protocol; this.headers = options.headers; @@ -45,7 +44,7 @@ class WS_RPC extends IPC_WS { } reconnect() { - this.socket = new websocket(this.url, this.protocol, undefined, this.headers, undefined, this.clientConfig); + this.socket = new websocket(this.path, this.protocol, undefined, this.headers, undefined, this.clientConfig); this.socket.onopen = () => { (this.socket.readyState === this.socket.OPEN) && this._connected(); }; @@ -61,13 +60,13 @@ class WS_RPC extends IPC_WS { }; } - _send(payloads) { - if (!this.connectStatus) { - return Promise.reject( this.ERRORS.CONNECT(this.url) ); - } - this.socket.send( JSON.stringify(payloads) ); - return this._onSend(payloads); - } + // _send(payloads) { + // if (!this.connectStatus) { + // return Promise.reject( this.ERRORS.CONNECT(this.path) ); + // } + // this.socket.send( JSON.stringify(payloads) ); + // return this._onSend(payloads); + // } disconnect() { this.socket && this.socket.close && this.socket.close(); diff --git a/src/utils/abi/coder/dynamic.ts b/src/utils/abi/coder/dynamic.ts index 3136a588..8d6134d5 100644 --- a/src/utils/abi/coder/dynamic.ts +++ b/src/utils/abi/coder/dynamic.ts @@ -53,7 +53,7 @@ function getBytesData(type, params) { } let isHex = /^0x[0-9a-fA-F]+$/.test(params) && params.length % 2 === 0; - let isCommonHex = /^[0-9a-fA-F]+$/.test(params) && params.length % 2 === 0; + let isCommonHex = /^[0-9a-fA-F]+$/.test(params) && params.length % 2 === 0 && type === 'bytes'; if (type === 'bytes' && !isCommonHex && !isHex) { throw '[Error] Illegal params. Should be hex-string.'; diff --git a/test/utils/abi.js b/test/utils/abi.js index b2b7d1f2..590e2f06 100644 --- a/test/utils/abi.js +++ b/test/utils/abi.js @@ -588,6 +588,11 @@ describe('utils/tools', function () { assert.equal(data, _data); }); + + it('encode2decode', function() { + let result = abi.encodeParameter('string', '15'); + assert.equal('15', abi.decodeParameter('string', result)); + }); }); diff --git a/yarn.lock b/yarn.lock index 4b183e58..8adcb6dc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4504,7 +4504,6 @@ pure-uuid@^1.5.3: qs@^6.6.0: version "6.6.0" resolved "http://registry.npm.taobao.org/qs/download/qs-6.6.0.tgz#a99c0f69a8d26bf7ef012f871cdabb0aee4424c2" - integrity sha1-qZwPaajSa/fvAS+HHNq7Cu5EJMI= qs@~6.5.2: version "6.5.2" @@ -4954,13 +4953,6 @@ source-map-resolve@^0.5.0: source-map-url "^0.4.0" urix "^0.1.0" -source-map-support@^0.5.10: - version "0.5.10" - resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.10.tgz#2214080bc9d51832511ee2bab96e3c2f9353120c" - dependencies: - buffer-from "^1.0.0" - source-map "^0.6.0" - source-map-support@^0.5.6, source-map-support@^0.5.9, source-map-support@~0.5.6: version "0.5.9" resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.9.tgz#41bc953b2534267ea2d605bccfa7bfa3111ced5f"