@tchain/tcabci-read-js-client 2.4.0 → 2.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -2
- package/client.js +101 -101
- package/client_test.js +97 -40
- package/package.json +6 -5
- package/websocket.js +344 -0
- package/websocketOptions.js +277 -0
- package/websocketOptions_test.js +129 -0
- package/websocket_test.js +539 -0
package/README.md
CHANGED
package/client.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import ReconnectingWebSocket from 'reconnecting-websocket'
|
|
2
1
|
import {
|
|
3
2
|
ADDRESSES_IS_EMPTY,
|
|
4
3
|
ALREADY_CONNECTED,
|
|
@@ -19,6 +18,8 @@ import {
|
|
|
19
18
|
} from './constants.js'
|
|
20
19
|
import Message from './message.js'
|
|
21
20
|
import { isJSON, toJSON } from './util.js'
|
|
21
|
+
import { DefaultTimeout, Options } from './websocketOptions.js'
|
|
22
|
+
import { TWebSocket } from './websocket.js'
|
|
22
23
|
|
|
23
24
|
/**
|
|
24
25
|
* @callback successCallback
|
|
@@ -41,33 +42,36 @@ import { isJSON, toJSON } from './util.js'
|
|
|
41
42
|
* @return void
|
|
42
43
|
*/
|
|
43
44
|
export default class TCaBCIClient {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
45
|
+
_subscribed = false
|
|
46
|
+
_subscribedAddresses = []
|
|
47
|
+
_subscribedSignedData = {}
|
|
48
|
+
_connected = false
|
|
49
|
+
_chainName = 'transferchain'
|
|
50
|
+
_chainVersion = 'v1'
|
|
51
|
+
_version = 'v2.5.1'
|
|
51
52
|
/**
|
|
52
53
|
* @type {?successCallback}
|
|
53
54
|
*/
|
|
54
|
-
|
|
55
|
+
_successCb = null
|
|
55
56
|
/**
|
|
56
57
|
* @type {?errorCallback}
|
|
57
58
|
*/
|
|
58
|
-
|
|
59
|
+
_errorCb = null
|
|
59
60
|
/**
|
|
60
61
|
* @type {?closeCallback}
|
|
61
62
|
*/
|
|
62
|
-
|
|
63
|
+
_closeCb = null
|
|
63
64
|
/**
|
|
64
65
|
* @type {?listenCallback}
|
|
65
66
|
*/
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
67
|
+
_listenCb = null
|
|
68
|
+
_wsLibrary = null
|
|
69
|
+
/**
|
|
70
|
+
* @type {TWebSocket}
|
|
71
|
+
*/
|
|
72
|
+
_ws
|
|
73
|
+
_readNodeAddress = READ_NODE_ADDRESS
|
|
74
|
+
_readNodeWSAddress = READ_NODE_WS_ADDRESS
|
|
71
75
|
|
|
72
76
|
/**
|
|
73
77
|
* @param {Array<String>} readNodeAddresses
|
|
@@ -82,11 +86,11 @@ export default class TCaBCIClient {
|
|
|
82
86
|
chainVersion = null,
|
|
83
87
|
) {
|
|
84
88
|
if (!wsLibrary) throw new Error(INVALID_ARGUMENTS)
|
|
85
|
-
this.
|
|
89
|
+
this._wsLibrary = wsLibrary
|
|
86
90
|
|
|
87
91
|
if (Array.isArray(readNodeAddresses) && readNodeAddresses.length === 2) {
|
|
88
|
-
this.
|
|
89
|
-
this.
|
|
92
|
+
this._readNodeAddress = readNodeAddresses[0]
|
|
93
|
+
this._readNodeWSAddress = readNodeAddresses[1]
|
|
90
94
|
|
|
91
95
|
if (!readNodeAddresses[0].startsWith('http'))
|
|
92
96
|
throw new Error(INVALID_ARGUMENTS)
|
|
@@ -94,8 +98,8 @@ export default class TCaBCIClient {
|
|
|
94
98
|
throw new Error(INVALID_ARGUMENTS)
|
|
95
99
|
}
|
|
96
100
|
|
|
97
|
-
if (chainName) this.
|
|
98
|
-
if (chainVersion) this.
|
|
101
|
+
if (chainName) this._chainName = chainName
|
|
102
|
+
if (chainVersion) this._chainVersion = chainVersion
|
|
99
103
|
}
|
|
100
104
|
|
|
101
105
|
/**
|
|
@@ -106,22 +110,17 @@ export default class TCaBCIClient {
|
|
|
106
110
|
httpClient(uri, req) {
|
|
107
111
|
req.cache = 'no-cache'
|
|
108
112
|
req.headers = {
|
|
109
|
-
Client: `tcabaci-read-js-client${this.
|
|
113
|
+
Client: `tcabaci-read-js-client${this._version}`,
|
|
110
114
|
}
|
|
111
|
-
|
|
112
|
-
if (typeof AbortSignal !== 'undefined') {
|
|
113
|
-
req.signal = AbortSignal.timeout(10000)
|
|
114
|
-
}
|
|
115
|
-
|
|
116
115
|
req.priority = 'high'
|
|
117
116
|
|
|
118
|
-
return fetch(this.
|
|
117
|
+
return fetch(this._readNodeAddress + uri, req).then((response) =>
|
|
119
118
|
this.handleRestResponse(response),
|
|
120
119
|
)
|
|
121
120
|
}
|
|
122
121
|
|
|
123
122
|
Socket() {
|
|
124
|
-
return this.
|
|
123
|
+
return this._ws
|
|
125
124
|
}
|
|
126
125
|
|
|
127
126
|
/**
|
|
@@ -129,7 +128,7 @@ export default class TCaBCIClient {
|
|
|
129
128
|
* @constructor
|
|
130
129
|
*/
|
|
131
130
|
SetSuccessCallback(cb) {
|
|
132
|
-
this.
|
|
131
|
+
this._successCb = cb
|
|
133
132
|
|
|
134
133
|
return this
|
|
135
134
|
}
|
|
@@ -139,7 +138,7 @@ export default class TCaBCIClient {
|
|
|
139
138
|
* @constructor
|
|
140
139
|
*/
|
|
141
140
|
SetErrorCallback(cb) {
|
|
142
|
-
this.
|
|
141
|
+
this._errorCb = cb
|
|
143
142
|
|
|
144
143
|
return this
|
|
145
144
|
}
|
|
@@ -149,7 +148,7 @@ export default class TCaBCIClient {
|
|
|
149
148
|
* @constructor
|
|
150
149
|
*/
|
|
151
150
|
SetCloseCallback(cb) {
|
|
152
|
-
this.
|
|
151
|
+
this._closeCb = cb
|
|
153
152
|
|
|
154
153
|
return this
|
|
155
154
|
}
|
|
@@ -159,7 +158,7 @@ export default class TCaBCIClient {
|
|
|
159
158
|
* @constructor
|
|
160
159
|
*/
|
|
161
160
|
SetListenCallback(cb) {
|
|
162
|
-
this.
|
|
161
|
+
this._listenCb = cb
|
|
163
162
|
|
|
164
163
|
return this
|
|
165
164
|
}
|
|
@@ -169,14 +168,14 @@ export default class TCaBCIClient {
|
|
|
169
168
|
}
|
|
170
169
|
|
|
171
170
|
/**
|
|
172
|
-
* @return {TCaBCIClient}
|
|
171
|
+
* @return {Awaited<TCaBCIClient>}
|
|
173
172
|
* @throws {Error}
|
|
174
173
|
*/
|
|
175
|
-
Stop() {
|
|
174
|
+
async Stop() {
|
|
176
175
|
if (!this.getConnected()) {
|
|
177
176
|
throw new Error(NOT_CONNECTED)
|
|
178
177
|
}
|
|
179
|
-
this.Disconnect(1000)
|
|
178
|
+
await this.Disconnect(1000)
|
|
180
179
|
this.setConnected(false)
|
|
181
180
|
this.setSubscribed(false)
|
|
182
181
|
|
|
@@ -188,6 +187,7 @@ export default class TCaBCIClient {
|
|
|
188
187
|
* @param {Object} signedData
|
|
189
188
|
* @param {?Array<string>} txTypes
|
|
190
189
|
* @return {TCaBCIClient}
|
|
190
|
+
* @throws {Error}
|
|
191
191
|
*/
|
|
192
192
|
Subscribe(addresses, signedData, txTypes = null) {
|
|
193
193
|
if (!Array.isArray(addresses)) {
|
|
@@ -243,7 +243,7 @@ export default class TCaBCIClient {
|
|
|
243
243
|
txTypes,
|
|
244
244
|
)
|
|
245
245
|
|
|
246
|
-
this.
|
|
246
|
+
this._ws.send(message.ToJSON())
|
|
247
247
|
this.setSubscribeAddresses(addrs, true)
|
|
248
248
|
.setSubscribeSignedData(signedData)
|
|
249
249
|
.setSubscribed(true)
|
|
@@ -255,7 +255,7 @@ export default class TCaBCIClient {
|
|
|
255
255
|
if (!this.getSubscribed()) {
|
|
256
256
|
throw new Error(NOT_SUBSCRIBED)
|
|
257
257
|
}
|
|
258
|
-
this.
|
|
258
|
+
this._ws.send(
|
|
259
259
|
new Message(
|
|
260
260
|
true,
|
|
261
261
|
MESSAGE_TYPE.UNSUBSCRIBE,
|
|
@@ -274,7 +274,7 @@ export default class TCaBCIClient {
|
|
|
274
274
|
*/
|
|
275
275
|
LastBlock(chainName = null, chainVersion = null) {
|
|
276
276
|
return this.httpClient(
|
|
277
|
-
`/v1/blocks?chain_name=${chainName ?? this.
|
|
277
|
+
`/v1/blocks?chain_name=${chainName ?? this._chainName}&chain_version=${chainVersion ?? this._chainVersion}&limit=1&offset=0`,
|
|
278
278
|
{
|
|
279
279
|
method: 'GET',
|
|
280
280
|
},
|
|
@@ -331,8 +331,8 @@ export default class TCaBCIClient {
|
|
|
331
331
|
return this.httpClient('/v1/tx_summary', {
|
|
332
332
|
method: 'POST',
|
|
333
333
|
body: JSON.stringify({
|
|
334
|
-
chain_name: chainName ?? this.
|
|
335
|
-
chain_version: chainVersion ?? this.
|
|
334
|
+
chain_name: chainName ?? this._chainName,
|
|
335
|
+
chain_version: chainVersion ?? this._chainVersion,
|
|
336
336
|
recipient_addrs: recipientAddrs,
|
|
337
337
|
sender_addrs: senderAddrs,
|
|
338
338
|
signed_addrs: signedData,
|
|
@@ -393,8 +393,8 @@ export default class TCaBCIClient {
|
|
|
393
393
|
return this.httpClient('/v1/tx_search/p', {
|
|
394
394
|
method: 'POST',
|
|
395
395
|
body: JSON.stringify({
|
|
396
|
-
chain_name: chainName ?? this.
|
|
397
|
-
chain_version: chainVersion ?? this.
|
|
396
|
+
chain_name: chainName ?? this._chainName,
|
|
397
|
+
chain_version: chainVersion ?? this._chainVersion,
|
|
398
398
|
height: `${heightOperator} ${height}`,
|
|
399
399
|
...(maxHeight ? { max_height: maxHeight } : {}),
|
|
400
400
|
...(lastOrder ? { last_order: lastOrder } : {}),
|
|
@@ -420,10 +420,10 @@ export default class TCaBCIClient {
|
|
|
420
420
|
|
|
421
421
|
Status() {
|
|
422
422
|
return {
|
|
423
|
-
chain_name: this.
|
|
424
|
-
chain_version: this.
|
|
425
|
-
connected: this.
|
|
426
|
-
subscribed: this.
|
|
423
|
+
chain_name: this._chainName,
|
|
424
|
+
chain_version: this._chainVersion,
|
|
425
|
+
connected: this._connected,
|
|
426
|
+
subscribed: this._subscribed,
|
|
427
427
|
}
|
|
428
428
|
}
|
|
429
429
|
|
|
@@ -590,8 +590,8 @@ export default class TCaBCIClient {
|
|
|
590
590
|
return this.httpClient('/v1/bulk_tx', {
|
|
591
591
|
method: 'POST',
|
|
592
592
|
body: JSON.stringify({
|
|
593
|
-
chain_name: chainName ?? this.
|
|
594
|
-
chain_version: chainVersion ?? this.
|
|
593
|
+
chain_name: chainName ?? this._chainName,
|
|
594
|
+
chain_version: chainVersion ?? this._chainVersion,
|
|
595
595
|
addresses: addresses,
|
|
596
596
|
signed_addrs: signedData,
|
|
597
597
|
...(maxHeight ? { max_height: maxHeight } : {}),
|
|
@@ -599,109 +599,109 @@ export default class TCaBCIClient {
|
|
|
599
599
|
})
|
|
600
600
|
}
|
|
601
601
|
|
|
602
|
-
Disconnect(code = 1000) {
|
|
602
|
+
async Disconnect(code = 1000) {
|
|
603
603
|
if (this.getConnected()) {
|
|
604
|
-
this.
|
|
604
|
+
await this._ws.disconnect(code)
|
|
605
|
+
|
|
606
|
+
this.setConnected(false)
|
|
607
|
+
this.setSubscribed(false)
|
|
605
608
|
}
|
|
606
609
|
}
|
|
607
610
|
|
|
608
|
-
Reconnect(code = 1000) {
|
|
611
|
+
async Reconnect(code = 1000) {
|
|
609
612
|
if (this.getConnected()) {
|
|
610
|
-
this.
|
|
613
|
+
await this.Disconnect(code)
|
|
611
614
|
}
|
|
612
615
|
|
|
616
|
+
await this.connect()
|
|
617
|
+
|
|
613
618
|
return this
|
|
614
619
|
}
|
|
615
620
|
|
|
616
621
|
callSuccessCallback(event) {
|
|
617
|
-
if (this.
|
|
622
|
+
if (this._successCb) this._successCb(event)
|
|
618
623
|
}
|
|
619
624
|
|
|
620
625
|
callErrorCallback(event) {
|
|
621
|
-
if (this.
|
|
626
|
+
if (this._errorCb) this._errorCb(event)
|
|
622
627
|
}
|
|
623
628
|
|
|
624
629
|
callCloseCallback(event) {
|
|
625
|
-
if (this.
|
|
630
|
+
if (this._closeCb) this._closeCb(event)
|
|
626
631
|
}
|
|
627
632
|
|
|
628
633
|
callListenCallback(message) {
|
|
629
|
-
if (this.
|
|
634
|
+
if (this._listenCb) this._listenCb(message)
|
|
630
635
|
}
|
|
631
636
|
|
|
632
637
|
/**
|
|
633
|
-
* @return {Promise<
|
|
638
|
+
* @return {Promise<TWebSocket>}
|
|
634
639
|
*/
|
|
635
640
|
async connect() {
|
|
636
641
|
if (this.getConnected()) {
|
|
637
642
|
return Promise.reject(new Error(ALREADY_CONNECTED))
|
|
638
643
|
}
|
|
639
644
|
|
|
640
|
-
const options =
|
|
641
|
-
|
|
642
|
-
connectionTimeout: 1000,
|
|
643
|
-
maxRetries: 10,
|
|
644
|
-
}
|
|
645
|
+
const options = new Options(this._readNodeWSAddress)
|
|
646
|
+
options.setCustomWS(this._wsLibrary)
|
|
645
647
|
|
|
646
|
-
this.
|
|
648
|
+
this._ws = new TWebSocket(options)
|
|
647
649
|
|
|
648
|
-
|
|
649
|
-
this.
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
650
|
+
this._ws.addErrorListener((e) => {
|
|
651
|
+
this.setConnected(false)
|
|
652
|
+
this.setSubscribed(false)
|
|
653
|
+
this.setSubscribeAddresses([], false)
|
|
654
|
+
this.setSubscribeSignedData({})
|
|
655
|
+
this.callErrorCallback(e)
|
|
656
|
+
})
|
|
655
657
|
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
this.
|
|
659
|
-
|
|
660
|
-
this.callSuccessCallback(event)
|
|
658
|
+
this._ws.addOpenListener((e) => {
|
|
659
|
+
this.setConnected(true)
|
|
660
|
+
this.callSuccessCallback(e)
|
|
661
|
+
})
|
|
661
662
|
|
|
662
|
-
|
|
663
|
+
this._ws.addMessageListener((message) => {
|
|
664
|
+
if (message.data === 'OK' && message.data.length < 10) {
|
|
665
|
+
return { status: message.data }
|
|
663
666
|
}
|
|
664
|
-
this.ws.onmessage = (message) => {
|
|
665
|
-
if (message.data === 'OK' && message.data.length < 10) {
|
|
666
|
-
return { status: message.data }
|
|
667
|
-
}
|
|
668
667
|
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
}
|
|
668
|
+
this.callListenCallback(!isJSON(message.data) ? message.data : toJSON(message.data))
|
|
669
|
+
})
|
|
670
|
+
|
|
671
|
+
this._ws.addCloseListener((e) => {
|
|
672
|
+
console.log('came gggg ', e)
|
|
673
|
+
this.setConnected(false)
|
|
674
|
+
this.setSubscribed(false)
|
|
675
|
+
this.setSubscribeAddresses([], false)
|
|
676
|
+
this.setSubscribeSignedData({})
|
|
677
|
+
this.callCloseCallback(e)
|
|
680
678
|
})
|
|
679
|
+
|
|
680
|
+
return this._ws.connect()
|
|
681
681
|
}
|
|
682
682
|
|
|
683
683
|
getConnected() {
|
|
684
|
-
return this.
|
|
684
|
+
return this._connected
|
|
685
685
|
}
|
|
686
686
|
|
|
687
687
|
setConnected(value) {
|
|
688
|
-
this.
|
|
688
|
+
this._connected = value
|
|
689
689
|
}
|
|
690
690
|
|
|
691
691
|
getSubscribed() {
|
|
692
|
-
return this.
|
|
692
|
+
return this._subscribed
|
|
693
693
|
}
|
|
694
694
|
|
|
695
695
|
setSubscribed(value) {
|
|
696
|
-
this.
|
|
696
|
+
this._subscribed = value
|
|
697
697
|
}
|
|
698
698
|
|
|
699
699
|
getSubscribeAddresses() {
|
|
700
|
-
return this.
|
|
700
|
+
return this._subscribedAddresses
|
|
701
701
|
}
|
|
702
702
|
|
|
703
703
|
getSubscribedSignedData() {
|
|
704
|
-
return this.
|
|
704
|
+
return this._subscribedSignedData
|
|
705
705
|
}
|
|
706
706
|
|
|
707
707
|
/**
|
|
@@ -711,11 +711,11 @@ export default class TCaBCIClient {
|
|
|
711
711
|
*/
|
|
712
712
|
setSubscribeAddresses(addresses, push = false) {
|
|
713
713
|
if (push) {
|
|
714
|
-
this.
|
|
714
|
+
this._subscribedAddresses.push(...addresses)
|
|
715
715
|
|
|
716
716
|
return this
|
|
717
717
|
}
|
|
718
|
-
this.
|
|
718
|
+
this._subscribedAddresses = addresses
|
|
719
719
|
|
|
720
720
|
return this
|
|
721
721
|
}
|
|
@@ -725,7 +725,7 @@ export default class TCaBCIClient {
|
|
|
725
725
|
* @return {TCaBCIClient}
|
|
726
726
|
*/
|
|
727
727
|
setSubscribeSignedData(signedData) {
|
|
728
|
-
this.
|
|
728
|
+
this._subscribedSignedData = signedData
|
|
729
729
|
|
|
730
730
|
return this
|
|
731
731
|
}
|