@tchain/tcabci-read-js-client 2.5.4 → 2.5.6

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 CHANGED
@@ -1,9 +1,9 @@
1
1
  import {
2
- ADDRESSES_IS_EMPTY,
2
+ FetchError,
3
3
  ALREADY_CONNECTED,
4
4
  BLOCK_NOT_FOUND,
5
5
  ERR_NETWORK,
6
- FetchError,
6
+ INVALID_ARGUMENT_WITH_CS,
7
7
  INVALID_ARGUMENTS,
8
8
  NOT_CONNECTED,
9
9
  NOT_SUBSCRIBED,
@@ -19,7 +19,8 @@ import Message from './message.js'
19
19
  import { isJSON, toJSON } from './util.js'
20
20
  import { Options } from './websocketOptions.js'
21
21
  import { TWebSocket } from './websocket.js'
22
- import { TX_TYPE_LIST } from './transaction.js'
22
+ import { TX_TYPE, TX_TYPE_LIST } from './transaction.js'
23
+ let https
23
24
 
24
25
  /**
25
26
  * @callback successCallback
@@ -48,7 +49,7 @@ export default class TCaBCIClient {
48
49
  _connected = false
49
50
  _chainName = 'transferchain'
50
51
  _chainVersion = 'v1'
51
- _version = `v2.5.4`
52
+ _version = `v2.5.6`
52
53
  /**
53
54
  * @type {?successCallback}
54
55
  */
@@ -66,6 +67,11 @@ export default class TCaBCIClient {
66
67
  */
67
68
  _listenCb = null
68
69
  _wsLibrary = null
70
+ /**
71
+ * @type {Options}
72
+ * @private
73
+ */
74
+ _options
69
75
  /**
70
76
  * @type {TWebSocket}
71
77
  */
@@ -89,15 +95,18 @@ export default class TCaBCIClient {
89
95
  this._wsLibrary = wsLibrary
90
96
 
91
97
  if (Array.isArray(readNodeAddresses) && readNodeAddresses.length === 2) {
92
- this._readNodeAddress = readNodeAddresses[0]
93
- this._readNodeWSAddress = readNodeAddresses[1]
94
-
95
98
  if (!readNodeAddresses[0].startsWith('http'))
96
99
  throw new Error(INVALID_ARGUMENTS)
97
100
  if (!readNodeAddresses[1].startsWith('ws'))
98
101
  throw new Error(INVALID_ARGUMENTS)
102
+
103
+ this._readNodeAddress = readNodeAddresses[0]
104
+ this._readNodeWSAddress = readNodeAddresses[1]
99
105
  }
100
106
 
107
+ this._options = new Options(this._readNodeWSAddress)
108
+ this._options.setCustomWS(this._wsLibrary)
109
+
101
110
  if (chainName) this._chainName = chainName
102
111
  if (chainVersion) this._chainVersion = chainVersion
103
112
  }
@@ -119,6 +128,10 @@ export default class TCaBCIClient {
119
128
  )
120
129
  }
121
130
 
131
+ SetDebug(debug) {
132
+ this._options.setDebug(debug)
133
+ }
134
+
122
135
  Socket() {
123
136
  return this._ws
124
137
  }
@@ -190,33 +203,29 @@ export default class TCaBCIClient {
190
203
  * @throws {Error}
191
204
  */
192
205
  Subscribe(addresses, signedData, txTypes = null) {
193
- if (!Array.isArray(addresses)) {
194
- throw new Error(INVALID_ARGUMENTS)
195
- }
206
+ if (!Array.isArray(addresses) || addresses.length === 0)
207
+ throw new Error(INVALID_ARGUMENT_WITH_CS('addresses'))
196
208
 
197
- if (typeof signedData !== 'object') {
198
- throw new Error(INVALID_ARGUMENTS)
199
- }
209
+ if (typeof signedData !== 'object') throw new Error(INVALID_ARGUMENTS)
200
210
 
201
- if (txTypes && !Array.isArray(addresses)) {
202
- throw new Error(INVALID_ARGUMENTS)
203
- }
211
+ if (txTypes && Array.isArray(txTypes)) {
212
+ if (!Array.isArray(txTypes))
213
+ throw new Error(INVALID_ARGUMENT_WITH_CS('txTypes'))
204
214
 
205
- if (txTypes && txTypes.length > TX_TYPE_LIST.length) {
206
- throw new Error(INVALID_ARGUMENTS)
207
- }
215
+ if (txTypes.length > TX_TYPE_LIST.length)
216
+ throw new Error(INVALID_ARGUMENT_WITH_CS('txTypes'))
208
217
 
209
- if (!this.getConnected()) {
210
- throw new Error(NOT_CONNECTED)
218
+ for (let i = 0; i < txTypes.length; i++) {
219
+ if (!TX_TYPE[txTypes[i]])
220
+ throw new Error(INVALID_ARGUMENT_WITH_CS('txType ', txTypes[i]))
221
+ }
211
222
  }
212
223
 
224
+ if (!this.getConnected()) throw new Error(NOT_CONNECTED)
225
+
213
226
  let addrs = [],
214
227
  sdata = {}
215
228
 
216
- if (addresses.length === 0) {
217
- throw new Error(ADDRESSES_IS_EMPTY)
218
- }
219
-
220
229
  addrs = addresses
221
230
  sdata = signedData
222
231
 
@@ -543,7 +552,7 @@ export default class TCaBCIClient {
543
552
  sync = false,
544
553
  commit = false,
545
554
  ) {
546
- if (Object.values(TX_TYPE).indexOf(type) < 0) {
555
+ if (!TX_TYPE_LIST.includes(type)) {
547
556
  throw new Error(TRANSACTION_TYPE_NOT_VALID)
548
557
  }
549
558
 
@@ -642,10 +651,7 @@ export default class TCaBCIClient {
642
651
  return Promise.reject(new Error(ALREADY_CONNECTED))
643
652
  }
644
653
 
645
- const options = new Options(this._readNodeWSAddress)
646
- options.setCustomWS(this._wsLibrary)
647
-
648
- this._ws = new TWebSocket(options)
654
+ this._ws = new TWebSocket(this._options)
649
655
 
650
656
  this._ws.addErrorListener((e) => {
651
657
  this.setConnected(false)
package/client_test.js CHANGED
@@ -36,6 +36,9 @@ describe('TCaBCIClient', () => {
36
36
  .catch((err) => {
37
37
  done(err)
38
38
  })
39
+ .finally(() => {
40
+ client.Stop()
41
+ })
39
42
  })
40
43
 
41
44
  it('start with valid parameters and read node addresses', (done) => {
@@ -66,6 +69,9 @@ describe('TCaBCIClient', () => {
66
69
  .catch((err) => {
67
70
  done(err)
68
71
  })
72
+ .finally(() => {
73
+ client.Stop()
74
+ })
69
75
  })
70
76
 
71
77
  it('reconnect with start and valid parameters', (done) => {
@@ -116,6 +122,9 @@ describe('TCaBCIClient', () => {
116
122
  .catch((err) => {
117
123
  done(err)
118
124
  })
125
+ .finally(() => {
126
+ client.Stop()
127
+ })
119
128
  })
120
129
 
121
130
  it('subscribe with valid parameters', (done) => {
@@ -150,6 +159,9 @@ describe('TCaBCIClient', () => {
150
159
  .catch((err) => {
151
160
  done(err)
152
161
  })
162
+ .finally(() => {
163
+ client.Stop()
164
+ })
153
165
  })
154
166
 
155
167
  it('should error subscribe with invalid tx type parameter', (done) => {
@@ -166,6 +178,7 @@ describe('TCaBCIClient', () => {
166
178
  '2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGN',
167
179
  '2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGU',
168
180
  ],
181
+ {},
169
182
  ['invalid'],
170
183
  )
171
184
 
@@ -177,6 +190,9 @@ describe('TCaBCIClient', () => {
177
190
  .catch(() => {
178
191
  done()
179
192
  })
193
+ .finally(() => {
194
+ client.Stop()
195
+ })
180
196
  })
181
197
 
182
198
  it('unsubscribe with valid parameters', (done) => {
@@ -219,6 +235,9 @@ describe('TCaBCIClient', () => {
219
235
  .catch((err) => {
220
236
  done(err)
221
237
  })
238
+ .finally(() => {
239
+ client.Stop()
240
+ })
222
241
  })
223
242
 
224
243
  it('last block', (done) => {
@@ -226,14 +245,17 @@ describe('TCaBCIClient', () => {
226
245
 
227
246
  client
228
247
  .LastBlock()
229
- .then((data) => {
230
- unitJS.value(data.blocks).hasLength(1)
231
- unitJS.value(data.total_count).isGreaterThan(1)
248
+ .then(({ blocks, total_count }) => {
249
+ unitJS.array(blocks).hasLength(1)
250
+ unitJS.assert.equal(total_count, 1)
232
251
  done()
233
252
  })
234
253
  .catch((err) => {
235
254
  done(err)
236
255
  })
256
+ .finally(() => {
257
+ client.Stop()
258
+ })
237
259
  })
238
260
 
239
261
  it('transaction search result', (done) => {
@@ -258,6 +280,9 @@ describe('TCaBCIClient', () => {
258
280
  .catch((err) => {
259
281
  done(err)
260
282
  })
283
+ .finally(() => {
284
+ client.Stop()
285
+ })
261
286
  })
262
287
  //
263
288
  it('transaction summary result', (done) => {
@@ -281,6 +306,9 @@ describe('TCaBCIClient', () => {
281
306
  .catch((err) => {
282
307
  done(err)
283
308
  })
309
+ .finally(() => {
310
+ client.Stop()
311
+ })
284
312
  })
285
313
 
286
314
  it('should error not broadcast transaction if type is incorrect', (done) => {
@@ -299,6 +327,8 @@ describe('TCaBCIClient', () => {
299
327
  unitJS.assert.equal(TRANSACTION_TYPE_NOT_VALID, err.message)
300
328
  done()
301
329
  }
330
+
331
+ client.Stop()
302
332
  })
303
333
 
304
334
  it('should error not broadcast transaction', (done) => {
@@ -325,6 +355,9 @@ describe('TCaBCIClient', () => {
325
355
  unitJS.assert.equal(TRANSACTION_NOT_BROADCAST, err.message)
326
356
  done()
327
357
  })
358
+ .finally(() => {
359
+ client.Stop()
360
+ })
328
361
  })
329
362
 
330
363
  it('should error in bulk tx if addresses count is zero', (done) => {
@@ -339,6 +372,9 @@ describe('TCaBCIClient', () => {
339
372
  unitJS.assert.equal(400, err.response.status)
340
373
  done()
341
374
  })
375
+ .finally(() => {
376
+ client.Stop()
377
+ })
342
378
  })
343
379
 
344
380
  it('should error in bulk tx if addresses count is greater than 251', (done) => {
@@ -361,5 +397,8 @@ describe('TCaBCIClient', () => {
361
397
  unitJS.assert.equal(400, err.response.status)
362
398
  done()
363
399
  })
400
+ .finally(() => {
401
+ client.Stop()
402
+ })
364
403
  })
365
404
  })
package/message.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { INVALID_ARGUMENTS, INVALID_ARGUMENT_WITH_CS } from './errors.js'
2
- import { MESSAGE_TYPE, TX_TYPE } from './constants.js'
2
+ import { MESSAGE_TYPE } from './constants.js'
3
+ import { TX_TYPE_LIST } from './transaction.js'
3
4
 
4
5
  export default class Message {
5
6
  _is_web = false
@@ -60,15 +61,11 @@ export default class Message {
60
61
  }
61
62
 
62
63
  if (this._txTypes.length) {
63
- let vals = Object.values(TX_TYPE)
64
-
65
64
  for (let i = 0; i < this._txTypes.length; i++) {
66
- if (vals.indexOf(this._txTypes[i]) === -1) {
65
+ if (!TX_TYPE_LIST.includes(this._txTypes[i])) {
67
66
  throw new Error(INVALID_ARGUMENT_WITH_CS('tx_types'))
68
67
  }
69
68
  }
70
-
71
- vals = []
72
69
  }
73
70
  }
74
71
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tchain/tcabci-read-js-client",
3
- "version": "2.5.4",
3
+ "version": "2.5.6",
4
4
  "description": "TransferChain Fastest Read Network WebSocket Client",
5
5
  "main": "client.js",
6
6
  "type": "module",
@@ -20,13 +20,14 @@
20
20
  "url": "https://github.com/TransferChain/tcabci-read-js-client/issues"
21
21
  },
22
22
  "homepage": "https://github.com/TransferChain/tcabci-read-js-client#readme",
23
+ "packageManager": "pnpm@10.18.2",
23
24
  "devDependencies": {
24
- "@eslint/js": "^9.34.0",
25
- "chai": "^6.0.1",
26
- "esm": "*",
25
+ "@eslint/js": "^9.37.0",
26
+ "chai": "^6.2.0",
27
+ "esm": "^3.2.25",
27
28
  "express": "^5.1.0",
28
- "globals": "^16.3.0",
29
- "mocha": "^11.7.1",
29
+ "globals": "^16.4.0",
30
+ "mocha": "^11.7.4",
30
31
  "nyc": "^17.1.0",
31
32
  "prettier": "^3.6.2",
32
33
  "unit.js": "^2.1.1",
@@ -13,7 +13,7 @@ export class Options {
13
13
  _protocols = []
14
14
  _customWS
15
15
  _endpoints = ['ws', 'longpoll']
16
- _version = `v2.5.4`
16
+ _version = `v2.5.6`
17
17
 
18
18
  _maxConnectionDelay = 10000
19
19
  _minReconnectionDelay = 1000 + Math.random() * 4000