@tchain/tcabci-read-js-client 2.4.0 → 2.5.0
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 -96
- 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.3.0'
|
|
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,7 +110,7 @@ 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
115
|
|
|
112
116
|
if (typeof AbortSignal !== 'undefined') {
|
|
@@ -115,13 +119,13 @@ export default class TCaBCIClient {
|
|
|
115
119
|
|
|
116
120
|
req.priority = 'high'
|
|
117
121
|
|
|
118
|
-
return fetch(this.
|
|
122
|
+
return fetch(this._readNodeAddress + uri, req).then((response) =>
|
|
119
123
|
this.handleRestResponse(response),
|
|
120
124
|
)
|
|
121
125
|
}
|
|
122
126
|
|
|
123
127
|
Socket() {
|
|
124
|
-
return this.
|
|
128
|
+
return this._ws
|
|
125
129
|
}
|
|
126
130
|
|
|
127
131
|
/**
|
|
@@ -129,7 +133,7 @@ export default class TCaBCIClient {
|
|
|
129
133
|
* @constructor
|
|
130
134
|
*/
|
|
131
135
|
SetSuccessCallback(cb) {
|
|
132
|
-
this.
|
|
136
|
+
this._successCb = cb
|
|
133
137
|
|
|
134
138
|
return this
|
|
135
139
|
}
|
|
@@ -139,7 +143,7 @@ export default class TCaBCIClient {
|
|
|
139
143
|
* @constructor
|
|
140
144
|
*/
|
|
141
145
|
SetErrorCallback(cb) {
|
|
142
|
-
this.
|
|
146
|
+
this._errorCb = cb
|
|
143
147
|
|
|
144
148
|
return this
|
|
145
149
|
}
|
|
@@ -149,7 +153,7 @@ export default class TCaBCIClient {
|
|
|
149
153
|
* @constructor
|
|
150
154
|
*/
|
|
151
155
|
SetCloseCallback(cb) {
|
|
152
|
-
this.
|
|
156
|
+
this._closeCb = cb
|
|
153
157
|
|
|
154
158
|
return this
|
|
155
159
|
}
|
|
@@ -159,7 +163,7 @@ export default class TCaBCIClient {
|
|
|
159
163
|
* @constructor
|
|
160
164
|
*/
|
|
161
165
|
SetListenCallback(cb) {
|
|
162
|
-
this.
|
|
166
|
+
this._listenCb = cb
|
|
163
167
|
|
|
164
168
|
return this
|
|
165
169
|
}
|
|
@@ -169,14 +173,14 @@ export default class TCaBCIClient {
|
|
|
169
173
|
}
|
|
170
174
|
|
|
171
175
|
/**
|
|
172
|
-
* @return {TCaBCIClient}
|
|
176
|
+
* @return {Awaited<TCaBCIClient>}
|
|
173
177
|
* @throws {Error}
|
|
174
178
|
*/
|
|
175
|
-
Stop() {
|
|
179
|
+
async Stop() {
|
|
176
180
|
if (!this.getConnected()) {
|
|
177
181
|
throw new Error(NOT_CONNECTED)
|
|
178
182
|
}
|
|
179
|
-
this.Disconnect(1000)
|
|
183
|
+
await this.Disconnect(1000)
|
|
180
184
|
this.setConnected(false)
|
|
181
185
|
this.setSubscribed(false)
|
|
182
186
|
|
|
@@ -188,6 +192,7 @@ export default class TCaBCIClient {
|
|
|
188
192
|
* @param {Object} signedData
|
|
189
193
|
* @param {?Array<string>} txTypes
|
|
190
194
|
* @return {TCaBCIClient}
|
|
195
|
+
* @throws {Error}
|
|
191
196
|
*/
|
|
192
197
|
Subscribe(addresses, signedData, txTypes = null) {
|
|
193
198
|
if (!Array.isArray(addresses)) {
|
|
@@ -243,7 +248,7 @@ export default class TCaBCIClient {
|
|
|
243
248
|
txTypes,
|
|
244
249
|
)
|
|
245
250
|
|
|
246
|
-
this.
|
|
251
|
+
this._ws.send(message.ToJSON())
|
|
247
252
|
this.setSubscribeAddresses(addrs, true)
|
|
248
253
|
.setSubscribeSignedData(signedData)
|
|
249
254
|
.setSubscribed(true)
|
|
@@ -255,7 +260,7 @@ export default class TCaBCIClient {
|
|
|
255
260
|
if (!this.getSubscribed()) {
|
|
256
261
|
throw new Error(NOT_SUBSCRIBED)
|
|
257
262
|
}
|
|
258
|
-
this.
|
|
263
|
+
this._ws.send(
|
|
259
264
|
new Message(
|
|
260
265
|
true,
|
|
261
266
|
MESSAGE_TYPE.UNSUBSCRIBE,
|
|
@@ -274,7 +279,7 @@ export default class TCaBCIClient {
|
|
|
274
279
|
*/
|
|
275
280
|
LastBlock(chainName = null, chainVersion = null) {
|
|
276
281
|
return this.httpClient(
|
|
277
|
-
`/v1/blocks?chain_name=${chainName ?? this.
|
|
282
|
+
`/v1/blocks?chain_name=${chainName ?? this._chainName}&chain_version=${chainVersion ?? this._chainVersion}&limit=1&offset=0`,
|
|
278
283
|
{
|
|
279
284
|
method: 'GET',
|
|
280
285
|
},
|
|
@@ -331,8 +336,8 @@ export default class TCaBCIClient {
|
|
|
331
336
|
return this.httpClient('/v1/tx_summary', {
|
|
332
337
|
method: 'POST',
|
|
333
338
|
body: JSON.stringify({
|
|
334
|
-
chain_name: chainName ?? this.
|
|
335
|
-
chain_version: chainVersion ?? this.
|
|
339
|
+
chain_name: chainName ?? this._chainName,
|
|
340
|
+
chain_version: chainVersion ?? this._chainVersion,
|
|
336
341
|
recipient_addrs: recipientAddrs,
|
|
337
342
|
sender_addrs: senderAddrs,
|
|
338
343
|
signed_addrs: signedData,
|
|
@@ -393,8 +398,8 @@ export default class TCaBCIClient {
|
|
|
393
398
|
return this.httpClient('/v1/tx_search/p', {
|
|
394
399
|
method: 'POST',
|
|
395
400
|
body: JSON.stringify({
|
|
396
|
-
chain_name: chainName ?? this.
|
|
397
|
-
chain_version: chainVersion ?? this.
|
|
401
|
+
chain_name: chainName ?? this._chainName,
|
|
402
|
+
chain_version: chainVersion ?? this._chainVersion,
|
|
398
403
|
height: `${heightOperator} ${height}`,
|
|
399
404
|
...(maxHeight ? { max_height: maxHeight } : {}),
|
|
400
405
|
...(lastOrder ? { last_order: lastOrder } : {}),
|
|
@@ -420,10 +425,10 @@ export default class TCaBCIClient {
|
|
|
420
425
|
|
|
421
426
|
Status() {
|
|
422
427
|
return {
|
|
423
|
-
chain_name: this.
|
|
424
|
-
chain_version: this.
|
|
425
|
-
connected: this.
|
|
426
|
-
subscribed: this.
|
|
428
|
+
chain_name: this._chainName,
|
|
429
|
+
chain_version: this._chainVersion,
|
|
430
|
+
connected: this._connected,
|
|
431
|
+
subscribed: this._subscribed,
|
|
427
432
|
}
|
|
428
433
|
}
|
|
429
434
|
|
|
@@ -590,8 +595,8 @@ export default class TCaBCIClient {
|
|
|
590
595
|
return this.httpClient('/v1/bulk_tx', {
|
|
591
596
|
method: 'POST',
|
|
592
597
|
body: JSON.stringify({
|
|
593
|
-
chain_name: chainName ?? this.
|
|
594
|
-
chain_version: chainVersion ?? this.
|
|
598
|
+
chain_name: chainName ?? this._chainName,
|
|
599
|
+
chain_version: chainVersion ?? this._chainVersion,
|
|
595
600
|
addresses: addresses,
|
|
596
601
|
signed_addrs: signedData,
|
|
597
602
|
...(maxHeight ? { max_height: maxHeight } : {}),
|
|
@@ -599,109 +604,109 @@ export default class TCaBCIClient {
|
|
|
599
604
|
})
|
|
600
605
|
}
|
|
601
606
|
|
|
602
|
-
Disconnect(code = 1000) {
|
|
607
|
+
async Disconnect(code = 1000) {
|
|
603
608
|
if (this.getConnected()) {
|
|
604
|
-
this.
|
|
609
|
+
await this._ws.disconnect(code)
|
|
610
|
+
|
|
611
|
+
this.setConnected(false)
|
|
612
|
+
this.setSubscribed(false)
|
|
605
613
|
}
|
|
606
614
|
}
|
|
607
615
|
|
|
608
|
-
Reconnect(code = 1000) {
|
|
616
|
+
async Reconnect(code = 1000) {
|
|
609
617
|
if (this.getConnected()) {
|
|
610
|
-
this.
|
|
618
|
+
await this.Disconnect(code)
|
|
611
619
|
}
|
|
612
620
|
|
|
621
|
+
await this.connect()
|
|
622
|
+
|
|
613
623
|
return this
|
|
614
624
|
}
|
|
615
625
|
|
|
616
626
|
callSuccessCallback(event) {
|
|
617
|
-
if (this.
|
|
627
|
+
if (this._successCb) this._successCb(event)
|
|
618
628
|
}
|
|
619
629
|
|
|
620
630
|
callErrorCallback(event) {
|
|
621
|
-
if (this.
|
|
631
|
+
if (this._errorCb) this._errorCb(event)
|
|
622
632
|
}
|
|
623
633
|
|
|
624
634
|
callCloseCallback(event) {
|
|
625
|
-
if (this.
|
|
635
|
+
if (this._closeCb) this._closeCb(event)
|
|
626
636
|
}
|
|
627
637
|
|
|
628
638
|
callListenCallback(message) {
|
|
629
|
-
if (this.
|
|
639
|
+
if (this._listenCb) this._listenCb(message)
|
|
630
640
|
}
|
|
631
641
|
|
|
632
642
|
/**
|
|
633
|
-
* @return {Promise<
|
|
643
|
+
* @return {Promise<TWebSocket>}
|
|
634
644
|
*/
|
|
635
645
|
async connect() {
|
|
636
646
|
if (this.getConnected()) {
|
|
637
647
|
return Promise.reject(new Error(ALREADY_CONNECTED))
|
|
638
648
|
}
|
|
639
649
|
|
|
640
|
-
const options =
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
}
|
|
650
|
+
const options = new Options(this._readNodeWSAddress)
|
|
651
|
+
options.setCustomWS(this._wsLibrary)
|
|
652
|
+
|
|
653
|
+
this._ws = new TWebSocket(options)
|
|
645
654
|
|
|
646
|
-
this.
|
|
655
|
+
this._ws.addErrorListener((e) => {
|
|
656
|
+
this.setConnected(false)
|
|
657
|
+
this.setSubscribed(false)
|
|
658
|
+
this.setSubscribeAddresses([], false)
|
|
659
|
+
this.setSubscribeSignedData({})
|
|
660
|
+
this.callErrorCallback(e)
|
|
661
|
+
})
|
|
647
662
|
|
|
648
|
-
|
|
649
|
-
this.
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
this.setSubscribeAddresses([], false)
|
|
653
|
-
this.setSubscribeSignedData({})
|
|
654
|
-
this.callErrorCallback(event)
|
|
663
|
+
this._ws.addOpenListener((e) => {
|
|
664
|
+
this.setConnected(true)
|
|
665
|
+
this.callSuccessCallback(e)
|
|
666
|
+
})
|
|
655
667
|
|
|
656
|
-
|
|
668
|
+
this._ws.addMessageListener((message) => {
|
|
669
|
+
if (message.data === 'OK' && message.data.length < 10) {
|
|
670
|
+
return { status: message.data }
|
|
657
671
|
}
|
|
658
|
-
this.ws.onopen = (event) => {
|
|
659
|
-
this.setConnected(true)
|
|
660
|
-
this.callSuccessCallback(event)
|
|
661
672
|
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
this.ws.onmessage = (message) => {
|
|
665
|
-
if (message.data === 'OK' && message.data.length < 10) {
|
|
666
|
-
return { status: message.data }
|
|
667
|
-
}
|
|
673
|
+
this.callListenCallback(!isJSON(message.data) ? message.data : toJSON(message.data))
|
|
674
|
+
})
|
|
668
675
|
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
this.
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
this.callCloseCallback(event)
|
|
677
|
-
|
|
678
|
-
resolve(event)
|
|
679
|
-
}
|
|
676
|
+
this._ws.addCloseListener((e) => {
|
|
677
|
+
console.log('came gggg ', e)
|
|
678
|
+
this.setConnected(false)
|
|
679
|
+
this.setSubscribed(false)
|
|
680
|
+
this.setSubscribeAddresses([], false)
|
|
681
|
+
this.setSubscribeSignedData({})
|
|
682
|
+
this.callCloseCallback(e)
|
|
680
683
|
})
|
|
684
|
+
|
|
685
|
+
return this._ws.connect()
|
|
681
686
|
}
|
|
682
687
|
|
|
683
688
|
getConnected() {
|
|
684
|
-
return this.
|
|
689
|
+
return this._connected
|
|
685
690
|
}
|
|
686
691
|
|
|
687
692
|
setConnected(value) {
|
|
688
|
-
this.
|
|
693
|
+
this._connected = value
|
|
689
694
|
}
|
|
690
695
|
|
|
691
696
|
getSubscribed() {
|
|
692
|
-
return this.
|
|
697
|
+
return this._subscribed
|
|
693
698
|
}
|
|
694
699
|
|
|
695
700
|
setSubscribed(value) {
|
|
696
|
-
this.
|
|
701
|
+
this._subscribed = value
|
|
697
702
|
}
|
|
698
703
|
|
|
699
704
|
getSubscribeAddresses() {
|
|
700
|
-
return this.
|
|
705
|
+
return this._subscribedAddresses
|
|
701
706
|
}
|
|
702
707
|
|
|
703
708
|
getSubscribedSignedData() {
|
|
704
|
-
return this.
|
|
709
|
+
return this._subscribedSignedData
|
|
705
710
|
}
|
|
706
711
|
|
|
707
712
|
/**
|
|
@@ -711,11 +716,11 @@ export default class TCaBCIClient {
|
|
|
711
716
|
*/
|
|
712
717
|
setSubscribeAddresses(addresses, push = false) {
|
|
713
718
|
if (push) {
|
|
714
|
-
this.
|
|
719
|
+
this._subscribedAddresses.push(...addresses)
|
|
715
720
|
|
|
716
721
|
return this
|
|
717
722
|
}
|
|
718
|
-
this.
|
|
723
|
+
this._subscribedAddresses = addresses
|
|
719
724
|
|
|
720
725
|
return this
|
|
721
726
|
}
|
|
@@ -725,7 +730,7 @@ export default class TCaBCIClient {
|
|
|
725
730
|
* @return {TCaBCIClient}
|
|
726
731
|
*/
|
|
727
732
|
setSubscribeSignedData(signedData) {
|
|
728
|
-
this.
|
|
733
|
+
this._subscribedSignedData = signedData
|
|
729
734
|
|
|
730
735
|
return this
|
|
731
736
|
}
|