@tchain/tcabci-read-js-client 2.0.3 → 2.1.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/client.js +35 -28
- 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.0
|
|
27
|
+
version = 'v2.1.0'
|
|
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
|
|
|
@@ -54,7 +58,6 @@ export default class TCAbciClient {
|
|
|
54
58
|
req.cache = 'no-cache'
|
|
55
59
|
req.headers = {
|
|
56
60
|
Client: `tcabaci-read-js-client${this.version}`,
|
|
57
|
-
'X-Client': `tcabaci-read-js-client${this.version}`,
|
|
58
61
|
}
|
|
59
62
|
|
|
60
63
|
if (typeof AbortSignal !== 'undefined') {
|
|
@@ -63,13 +66,7 @@ export default class TCAbciClient {
|
|
|
63
66
|
|
|
64
67
|
req.priority = 'high'
|
|
65
68
|
|
|
66
|
-
return fetch(this.readNodeAddress + uri, req).then((response) =>
|
|
67
|
-
if (response.ok) return response.json()
|
|
68
|
-
|
|
69
|
-
return Promise.reject(
|
|
70
|
-
new FetchError(response.statusText).setCode(response.status),
|
|
71
|
-
)
|
|
72
|
-
})
|
|
69
|
+
return fetch(this.readNodeAddress + uri, req).then((response) => this.handleResponse(response))
|
|
73
70
|
}
|
|
74
71
|
|
|
75
72
|
Socket() {
|
|
@@ -296,7 +293,7 @@ export default class TCAbciClient {
|
|
|
296
293
|
}),
|
|
297
294
|
})
|
|
298
295
|
.then((res) => {
|
|
299
|
-
return { data: res.data
|
|
296
|
+
return { data: res.data }
|
|
300
297
|
})
|
|
301
298
|
.catch((e) => this.handleRestError(e, { 400: TRANSACTION_NOT_BROADCAST }))
|
|
302
299
|
}
|
|
@@ -321,15 +318,13 @@ export default class TCAbciClient {
|
|
|
321
318
|
}
|
|
322
319
|
|
|
323
320
|
async connect() {
|
|
324
|
-
const wsClient = await this.wsClient()
|
|
325
|
-
|
|
326
321
|
return new Promise((resolve, reject) => {
|
|
327
322
|
if (this.getConnected()) {
|
|
328
323
|
reject(ALREADY_CONNECTED)
|
|
329
324
|
}
|
|
330
325
|
|
|
331
326
|
const options = {
|
|
332
|
-
WebSocket:
|
|
327
|
+
WebSocket: this.wsLibrary,
|
|
333
328
|
connectionTimeout: 1000,
|
|
334
329
|
maxRetries: 10,
|
|
335
330
|
}
|
|
@@ -364,18 +359,6 @@ export default class TCAbciClient {
|
|
|
364
359
|
})
|
|
365
360
|
}
|
|
366
361
|
|
|
367
|
-
async wsClient() {
|
|
368
|
-
if (
|
|
369
|
-
typeof window !== 'undefined' &&
|
|
370
|
-
typeof window.WebSocket !== 'undefined'
|
|
371
|
-
) {
|
|
372
|
-
return window.WebSocket
|
|
373
|
-
}
|
|
374
|
-
const { WebSocket } = await import('ws')
|
|
375
|
-
|
|
376
|
-
return WebSocket
|
|
377
|
-
}
|
|
378
|
-
|
|
379
362
|
getConnected() {
|
|
380
363
|
return this.connected
|
|
381
364
|
}
|
|
@@ -405,6 +388,30 @@ export default class TCAbciClient {
|
|
|
405
388
|
this.subscribedAddresses = addresses
|
|
406
389
|
}
|
|
407
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
|
+
|
|
408
415
|
/**
|
|
409
416
|
* @param {Error} err
|
|
410
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.0
|
|
3
|
+
"version": "2.1.0",
|
|
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
|
}
|