@tchain/tcabci-read-js-client 2.0.4 → 2.1.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/client.js +35 -27
- package/client_test.js +14 -13
- package/package.json +4 -4
package/client.js
CHANGED
|
@@ -24,19 +24,23 @@ export default class TCAbciClient {
|
|
|
24
24
|
subscribed = false
|
|
25
25
|
subscribedAddresses = []
|
|
26
26
|
connected = false
|
|
27
|
-
version = 'v2.
|
|
27
|
+
version = 'v2.1.1'
|
|
28
28
|
errorCb = null
|
|
29
29
|
listenCb = null
|
|
30
|
+
wsLibrary = null
|
|
30
31
|
ws = null
|
|
31
32
|
readNodeAddress = READ_NODE_ADDRESS
|
|
32
33
|
readNodeWSAddress = READ_NODE_WS_ADDRESS
|
|
33
34
|
|
|
34
35
|
/**
|
|
35
|
-
*
|
|
36
36
|
* @param {Array<String>} readNodeAddresses
|
|
37
|
+
* @param {WebSocket} wsLibrary
|
|
37
38
|
*/
|
|
38
|
-
constructor(readNodeAddresses = []) {
|
|
39
|
-
if (
|
|
39
|
+
constructor(readNodeAddresses = [], wsLibrary) {
|
|
40
|
+
if (!wsLibrary) throw INVALID_ARGUMENTS
|
|
41
|
+
this.wsLibrary = wsLibrary
|
|
42
|
+
|
|
43
|
+
if (Array.isArray(readNodeAddresses) && readNodeAddresses.length === 2) {
|
|
40
44
|
this.readNodeAddress = readNodeAddresses[0]
|
|
41
45
|
this.readNodeWSAddress = readNodeAddresses[1]
|
|
42
46
|
|
|
@@ -62,13 +66,7 @@ export default class TCAbciClient {
|
|
|
62
66
|
|
|
63
67
|
req.priority = 'high'
|
|
64
68
|
|
|
65
|
-
return fetch(this.readNodeAddress + uri, req).then((response) =>
|
|
66
|
-
if (response.ok) return response.json()
|
|
67
|
-
|
|
68
|
-
return Promise.reject(
|
|
69
|
-
new FetchError(response.statusText).setCode(response.status),
|
|
70
|
-
)
|
|
71
|
-
})
|
|
69
|
+
return fetch(this.readNodeAddress + uri, req).then((response) => this.handleResponse(response))
|
|
72
70
|
}
|
|
73
71
|
|
|
74
72
|
Socket() {
|
|
@@ -295,7 +293,7 @@ export default class TCAbciClient {
|
|
|
295
293
|
}),
|
|
296
294
|
})
|
|
297
295
|
.then((res) => {
|
|
298
|
-
return { data: res.data
|
|
296
|
+
return { data: res.data }
|
|
299
297
|
})
|
|
300
298
|
.catch((e) => this.handleRestError(e, { 400: TRANSACTION_NOT_BROADCAST }))
|
|
301
299
|
}
|
|
@@ -320,15 +318,13 @@ export default class TCAbciClient {
|
|
|
320
318
|
}
|
|
321
319
|
|
|
322
320
|
async connect() {
|
|
323
|
-
const wsClient = await this.wsClient()
|
|
324
|
-
|
|
325
321
|
return new Promise((resolve, reject) => {
|
|
326
322
|
if (this.getConnected()) {
|
|
327
323
|
reject(ALREADY_CONNECTED)
|
|
328
324
|
}
|
|
329
325
|
|
|
330
326
|
const options = {
|
|
331
|
-
WebSocket:
|
|
327
|
+
WebSocket: this.wsLibrary,
|
|
332
328
|
connectionTimeout: 1000,
|
|
333
329
|
maxRetries: 10,
|
|
334
330
|
}
|
|
@@ -363,18 +359,6 @@ export default class TCAbciClient {
|
|
|
363
359
|
})
|
|
364
360
|
}
|
|
365
361
|
|
|
366
|
-
async wsClient() {
|
|
367
|
-
if (
|
|
368
|
-
typeof window !== 'undefined' &&
|
|
369
|
-
typeof window.WebSocket !== 'undefined'
|
|
370
|
-
) {
|
|
371
|
-
return window.WebSocket
|
|
372
|
-
}
|
|
373
|
-
const { WebSocket } = await import('ws')
|
|
374
|
-
|
|
375
|
-
return WebSocket
|
|
376
|
-
}
|
|
377
|
-
|
|
378
362
|
getConnected() {
|
|
379
363
|
return this.connected
|
|
380
364
|
}
|
|
@@ -404,6 +388,30 @@ export default class TCAbciClient {
|
|
|
404
388
|
this.subscribedAddresses = addresses
|
|
405
389
|
}
|
|
406
390
|
|
|
391
|
+
/**
|
|
392
|
+
* @param {Response} response
|
|
393
|
+
* @return {Promise<*>}
|
|
394
|
+
*/
|
|
395
|
+
async handleResponse(response) {
|
|
396
|
+
if (response.status >= 200 && response.status < 400) {
|
|
397
|
+
return response.json()
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
let data = await response.text()
|
|
401
|
+
|
|
402
|
+
try {
|
|
403
|
+
data = JSON.parse(data)
|
|
404
|
+
|
|
405
|
+
return Promise.reject(
|
|
406
|
+
new FetchError(data.message).setCode(response.status).setResponse(data)
|
|
407
|
+
)
|
|
408
|
+
} catch (e) {
|
|
409
|
+
return Promise.reject(
|
|
410
|
+
new FetchError(response.statusText)
|
|
411
|
+
)
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
407
415
|
/**
|
|
408
416
|
* @param {Error} err
|
|
409
417
|
* @param {?Object|Error} custom
|
package/client_test.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import TCAbciClient from './client.js'
|
|
2
|
+
import { WebSocket } from 'ws'
|
|
2
3
|
import unitJS from 'unit.js'
|
|
3
4
|
import {
|
|
4
5
|
TRANSACTION_NOT_BROADCAST,
|
|
@@ -20,7 +21,7 @@ import {
|
|
|
20
21
|
|
|
21
22
|
describe('TCAbciClient TESTS', () => {
|
|
22
23
|
it('should start with valid parameters', (done) => {
|
|
23
|
-
const client = new TCAbciClient()
|
|
24
|
+
const client = new TCAbciClient([], WebSocket)
|
|
24
25
|
|
|
25
26
|
client
|
|
26
27
|
.Start()
|
|
@@ -37,7 +38,7 @@ describe('TCAbciClient TESTS', () => {
|
|
|
37
38
|
})
|
|
38
39
|
|
|
39
40
|
it('should start with valid parameters and read node addresses', (done) => {
|
|
40
|
-
const client = new TCAbciClient([READ_NODE_ADDRESS, READ_NODE_WS_ADDRESS])
|
|
41
|
+
const client = new TCAbciClient([READ_NODE_ADDRESS, READ_NODE_WS_ADDRESS], WebSocket)
|
|
41
42
|
|
|
42
43
|
client
|
|
43
44
|
.Start()
|
|
@@ -54,7 +55,7 @@ describe('TCAbciClient TESTS', () => {
|
|
|
54
55
|
})
|
|
55
56
|
|
|
56
57
|
it('should reconnect with start and valid parameters', (done) => {
|
|
57
|
-
const client = new TCAbciClient()
|
|
58
|
+
const client = new TCAbciClient([], WebSocket)
|
|
58
59
|
|
|
59
60
|
client
|
|
60
61
|
.Start()
|
|
@@ -84,7 +85,7 @@ describe('TCAbciClient TESTS', () => {
|
|
|
84
85
|
})
|
|
85
86
|
|
|
86
87
|
it('should subscribe with valid parameters', (done) => {
|
|
87
|
-
const client = new TCAbciClient()
|
|
88
|
+
const client = new TCAbciClient([], WebSocket)
|
|
88
89
|
|
|
89
90
|
client
|
|
90
91
|
.Start()
|
|
@@ -107,7 +108,7 @@ describe('TCAbciClient TESTS', () => {
|
|
|
107
108
|
})
|
|
108
109
|
|
|
109
110
|
it('should error subscribe with invalid tx type parameter', (done) => {
|
|
110
|
-
const client = new TCAbciClient()
|
|
111
|
+
const client = new TCAbciClient([], WebSocket)
|
|
111
112
|
|
|
112
113
|
client
|
|
113
114
|
.Start()
|
|
@@ -126,7 +127,7 @@ describe('TCAbciClient TESTS', () => {
|
|
|
126
127
|
})
|
|
127
128
|
|
|
128
129
|
it('should unsubscribe with valid parameters', (done) => {
|
|
129
|
-
const client = new TCAbciClient()
|
|
130
|
+
const client = new TCAbciClient([], WebSocket)
|
|
130
131
|
|
|
131
132
|
client
|
|
132
133
|
.Start()
|
|
@@ -155,7 +156,7 @@ describe('TCAbciClient TESTS', () => {
|
|
|
155
156
|
})
|
|
156
157
|
|
|
157
158
|
it('should return last block', (done) => {
|
|
158
|
-
const client = new TCAbciClient()
|
|
159
|
+
const client = new TCAbciClient([], WebSocket)
|
|
159
160
|
|
|
160
161
|
client
|
|
161
162
|
.LastBlock()
|
|
@@ -170,7 +171,7 @@ describe('TCAbciClient TESTS', () => {
|
|
|
170
171
|
})
|
|
171
172
|
|
|
172
173
|
it('should return transaction search result', (done) => {
|
|
173
|
-
const client = new TCAbciClient()
|
|
174
|
+
const client = new TCAbciClient([], WebSocket)
|
|
174
175
|
|
|
175
176
|
client
|
|
176
177
|
.TxSearch({
|
|
@@ -194,7 +195,7 @@ describe('TCAbciClient TESTS', () => {
|
|
|
194
195
|
})
|
|
195
196
|
//
|
|
196
197
|
it('should return transaction summary result', (done) => {
|
|
197
|
-
const client = new TCAbciClient()
|
|
198
|
+
const client = new TCAbciClient([], WebSocket)
|
|
198
199
|
|
|
199
200
|
client
|
|
200
201
|
.TxSummary({
|
|
@@ -216,7 +217,7 @@ describe('TCAbciClient TESTS', () => {
|
|
|
216
217
|
})
|
|
217
218
|
|
|
218
219
|
it('should not broadcast transaction if type is incorrect', (done) => {
|
|
219
|
-
const client = new TCAbciClient()
|
|
220
|
+
const client = new TCAbciClient([], WebSocket)
|
|
220
221
|
|
|
221
222
|
try {
|
|
222
223
|
client.Broadcast({
|
|
@@ -234,7 +235,7 @@ describe('TCAbciClient TESTS', () => {
|
|
|
234
235
|
})
|
|
235
236
|
|
|
236
237
|
it('should not broadcast transaction', (done) => {
|
|
237
|
-
const client = new TCAbciClient()
|
|
238
|
+
const client = new TCAbciClient([], WebSocket)
|
|
238
239
|
|
|
239
240
|
client
|
|
240
241
|
.Broadcast({
|
|
@@ -260,7 +261,7 @@ describe('TCAbciClient TESTS', () => {
|
|
|
260
261
|
})
|
|
261
262
|
|
|
262
263
|
it('should error in bulk tx if addresses count is zero', (done) => {
|
|
263
|
-
const client = new TCAbciClient()
|
|
264
|
+
const client = new TCAbciClient([], WebSocket)
|
|
264
265
|
|
|
265
266
|
client
|
|
266
267
|
.Bulk([])
|
|
@@ -282,7 +283,7 @@ describe('TCAbciClient TESTS', () => {
|
|
|
282
283
|
)
|
|
283
284
|
}
|
|
284
285
|
|
|
285
|
-
const client = new TCAbciClient()
|
|
286
|
+
const client = new TCAbciClient(null, WebSocket)
|
|
286
287
|
|
|
287
288
|
client
|
|
288
289
|
.Bulk(addresses)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tchain/tcabci-read-js-client",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.1",
|
|
4
4
|
"description": "TransferChain Fastest Read Network WebSocket Client",
|
|
5
5
|
"main": "client.js",
|
|
6
6
|
"type": "module",
|
|
@@ -31,10 +31,10 @@
|
|
|
31
31
|
"eslint-plugin-import": "^2.29.1",
|
|
32
32
|
"eslint-plugin-node": "^11.0.0",
|
|
33
33
|
"eslint-plugin-promise": "^6.1.1",
|
|
34
|
-
"prettier": "^3.2.4"
|
|
34
|
+
"prettier": "^3.2.4",
|
|
35
|
+
"ws": "^8.18.0"
|
|
35
36
|
},
|
|
36
37
|
"dependencies": {
|
|
37
|
-
"reconnecting-websocket": "^4.4.0"
|
|
38
|
-
"ws": "^8.18.0"
|
|
38
|
+
"reconnecting-websocket": "^4.4.0"
|
|
39
39
|
}
|
|
40
40
|
}
|