@tchain/tcabci-read-js-client 2.5.3 → 2.5.5

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
@@ -4,6 +4,7 @@ import {
4
4
  BLOCK_NOT_FOUND,
5
5
  ERR_NETWORK,
6
6
  FetchError,
7
+ INVALID_ARGUMENT_WITH_CS,
7
8
  INVALID_ARGUMENTS,
8
9
  NOT_CONNECTED,
9
10
  NOT_SUBSCRIBED,
@@ -14,13 +15,12 @@ import {
14
15
  MESSAGE_TYPE,
15
16
  READ_NODE_ADDRESS,
16
17
  READ_NODE_WS_ADDRESS,
17
- TX_TYPE,
18
18
  } from './constants.js'
19
19
  import Message from './message.js'
20
20
  import { isJSON, toJSON } from './util.js'
21
21
  import { Options } from './websocketOptions.js'
22
22
  import { TWebSocket } from './websocket.js'
23
- import pkg from './package.json' with { type: 'json' }
23
+ import { TX_TYPE, TX_TYPE_LIST } from './transaction.js'
24
24
 
25
25
  /**
26
26
  * @callback successCallback
@@ -49,7 +49,7 @@ export default class TCaBCIClient {
49
49
  _connected = false
50
50
  _chainName = 'transferchain'
51
51
  _chainVersion = 'v1'
52
- _version = `v${pkg.version}`
52
+ _version = `v2.5.5`
53
53
  /**
54
54
  * @type {?successCallback}
55
55
  */
@@ -191,33 +191,29 @@ export default class TCaBCIClient {
191
191
  * @throws {Error}
192
192
  */
193
193
  Subscribe(addresses, signedData, txTypes = null) {
194
- if (!Array.isArray(addresses)) {
195
- throw new Error(INVALID_ARGUMENTS)
196
- }
194
+ if (!Array.isArray(addresses) || addresses.length === 0)
195
+ throw new Error(INVALID_ARGUMENT_WITH_CS('addresses'))
197
196
 
198
- if (typeof signedData !== 'object') {
199
- throw new Error(INVALID_ARGUMENTS)
200
- }
197
+ if (typeof signedData !== 'object') throw new Error(INVALID_ARGUMENTS)
201
198
 
202
- if (txTypes && !Array.isArray(addresses)) {
203
- throw new Error(INVALID_ARGUMENTS)
204
- }
199
+ if (txTypes && Array.isArray(txTypes)) {
200
+ if (!Array.isArray(txTypes))
201
+ throw new Error(INVALID_ARGUMENT_WITH_CS('txTypes'))
205
202
 
206
- if (txTypes && txTypes.length > Object.values(TX_TYPE).length) {
207
- throw new Error(INVALID_ARGUMENTS)
208
- }
203
+ if (txTypes.length > TX_TYPE_LIST.length)
204
+ throw new Error(INVALID_ARGUMENT_WITH_CS('txTypes'))
209
205
 
210
- if (!this.getConnected()) {
211
- throw new Error(NOT_CONNECTED)
206
+ for (let i = 0; i < txTypes.length; i++) {
207
+ if (!TX_TYPE[txTypes[i]])
208
+ throw new Error(INVALID_ARGUMENT_WITH_CS('txType ', txTypes[i]))
209
+ }
212
210
  }
213
211
 
212
+ if (!this.getConnected()) throw new Error(NOT_CONNECTED)
213
+
214
214
  let addrs = [],
215
215
  sdata = {}
216
216
 
217
- if (addresses.length === 0) {
218
- throw new Error(ADDRESSES_IS_EMPTY)
219
- }
220
-
221
217
  addrs = addresses
222
218
  sdata = signedData
223
219
 
@@ -544,7 +540,7 @@ export default class TCaBCIClient {
544
540
  sync = false,
545
541
  commit = false,
546
542
  ) {
547
- if (Object.values(TX_TYPE).indexOf(type) < 0) {
543
+ if (!TX_TYPE_LIST.includes(type)) {
548
544
  throw new Error(TRANSACTION_TYPE_NOT_VALID)
549
545
  }
550
546
 
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.3",
3
+ "version": "2.5.5",
4
4
  "description": "TransferChain Fastest Read Network WebSocket Client",
5
5
  "main": "client.js",
6
6
  "type": "module",
@@ -1,5 +1,3 @@
1
- import pkg from './package.json' with { type: 'json' }
2
-
3
1
  export const HTTPSPort = 443,
4
2
  HTTPPort = 80
5
3
 
@@ -15,7 +13,7 @@ export class Options {
15
13
  _protocols = []
16
14
  _customWS
17
15
  _endpoints = ['ws', 'longpoll']
18
- _version = `v${pkg.version}`
16
+ _version = `v2.5.5`
19
17
 
20
18
  _maxConnectionDelay = 10000
21
19
  _minReconnectionDelay = 1000 + Math.random() * 4000