@tchain/tcabci-read-js-client 2.5.8 → 2.5.10

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_test.js DELETED
@@ -1,404 +0,0 @@
1
- import TCaBCIClient from './client.js'
2
- import { WebSocket } from 'ws'
3
- import unitJS from 'unit.js'
4
- import {
5
- TRANSACTION_NOT_BROADCAST,
6
- TRANSACTION_TYPE_NOT_VALID,
7
- } from './errors.js'
8
- import { READ_NODE_ADDRESS, READ_NODE_WS_ADDRESS } from './constants.js'
9
- import { TX_TYPE_ADDRESS } from './transaction.js'
10
-
11
- const waiter = async (timeout = 1000) => {
12
- return new Promise((resolve) => setTimeout(resolve, timeout))
13
- }
14
-
15
- describe('TCaBCIClient', () => {
16
- it('start with valid parameters', (done) => {
17
- const client = new TCaBCIClient([], WebSocket, 'medusa', 'v2')
18
-
19
- client
20
- .Start()
21
- .then(() => {
22
- return waiter(500)
23
- })
24
- .then(() => {
25
- const { connected, subscribed } = client.Status()
26
-
27
- unitJS.assert.equal(connected, true)
28
- unitJS.assert.equal(subscribed, false)
29
- })
30
- .then(() => {
31
- return client.Disconnect()
32
- })
33
- .then(() => {
34
- done()
35
- })
36
- .catch((err) => {
37
- done(err)
38
- })
39
- .finally(() => {
40
- client.Stop()
41
- })
42
- })
43
-
44
- it('start with valid parameters and read node addresses', (done) => {
45
- const client = new TCaBCIClient(
46
- [READ_NODE_ADDRESS, READ_NODE_WS_ADDRESS],
47
- WebSocket,
48
- 'medusa',
49
- 'v2',
50
- )
51
-
52
- client
53
- .Start()
54
- .then(() => {
55
- return waiter(500)
56
- })
57
- .then(() => {
58
- const { connected, subscribed } = client.Status()
59
-
60
- unitJS.assert.equal(connected, true)
61
- unitJS.assert.equal(subscribed, false)
62
- })
63
- .then(() => {
64
- return client.Disconnect()
65
- })
66
- .then(() => {
67
- done()
68
- })
69
- .catch((err) => {
70
- done(err)
71
- })
72
- .finally(() => {
73
- client.Stop()
74
- })
75
- })
76
-
77
- it('reconnect with start and valid parameters', (done) => {
78
- const client = new TCaBCIClient([], WebSocket, 'medusa', 'v2')
79
-
80
- client
81
- .Start()
82
- .then(() => {
83
- return waiter(500)
84
- })
85
- .then(() => {
86
- const { connected, subscribed } = client.Status()
87
-
88
- unitJS.assert.equal(connected, true)
89
- unitJS.assert.equal(subscribed, false)
90
-
91
- return client.Reconnect(1007)
92
- })
93
- .then(() => {
94
- const { connected: c2, subscribed: b2 } = client.Status()
95
- unitJS.assert.equal(c2, false)
96
- unitJS.assert.equal(b2, false)
97
-
98
- return waiter(500)
99
- })
100
- .then(() => {
101
- return new Promise((resolve) => {
102
- setTimeout(
103
- (client, res) => {
104
- const { connected, subscribed } = client.Status()
105
-
106
- unitJS.assert.equal(connected, true)
107
- unitJS.assert.equal(subscribed, false)
108
- res()
109
- },
110
- 3100,
111
- client,
112
- resolve,
113
- )
114
- })
115
- })
116
- .then(() => {
117
- return client.Disconnect()
118
- })
119
- .then(() => {
120
- done()
121
- })
122
- .catch((err) => {
123
- done(err)
124
- })
125
- .finally(() => {
126
- client.Stop()
127
- })
128
- })
129
-
130
- it('subscribe with valid parameters', (done) => {
131
- const client = new TCaBCIClient([], WebSocket, 'medusa', 'v2')
132
-
133
- client
134
- .Start()
135
- .then(() => {
136
- return waiter(750)
137
- })
138
- .then(() => {
139
- client.Subscribe(
140
- [
141
- '2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGN',
142
- ],
143
- {
144
- '2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGN':
145
- '2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGN',
146
- },
147
- )
148
-
149
- const { connected, subscribed } = client.Status()
150
-
151
- unitJS.assert.equal(connected, true)
152
- unitJS.assert.equal(subscribed, true)
153
-
154
- return client.Disconnect()
155
- })
156
- .then(() => {
157
- done()
158
- })
159
- .catch((err) => {
160
- done(err)
161
- })
162
- .finally(() => {
163
- client.Stop()
164
- })
165
- })
166
-
167
- it('should error subscribe with invalid tx type parameter', (done) => {
168
- const client = new TCaBCIClient([], WebSocket, 'medusa', 'v2')
169
-
170
- client
171
- .Start()
172
- .then(() => {
173
- return waiter(500)
174
- })
175
- .then(() => {
176
- client.Subscribe(
177
- [
178
- '2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGN',
179
- '2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGU',
180
- ],
181
- {},
182
- ['invalid'],
183
- )
184
-
185
- return client.Disconnect()
186
- })
187
- .then(() => {
188
- done(new Error('invalid'))
189
- })
190
- .catch(() => {
191
- done()
192
- })
193
- .finally(() => {
194
- client.Stop()
195
- })
196
- })
197
-
198
- it('unsubscribe with valid parameters', (done) => {
199
- const client = new TCaBCIClient([], WebSocket, 'medusa', 'v2')
200
-
201
- client
202
- .Start()
203
- .then(() => {
204
- return waiter(500)
205
- })
206
- .then(() => {
207
- client.Subscribe(
208
- [
209
- '2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGN',
210
- ],
211
- {
212
- '2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGN':
213
- '2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGN',
214
- },
215
- )
216
- const { connected, subscribed } = client.Status()
217
-
218
- unitJS.assert.equal(connected, true)
219
- unitJS.assert.equal(subscribed, true)
220
-
221
- return Promise.resolve()
222
- })
223
- .then(() => {
224
- client.Unsubscribe()
225
- const { connected, subscribed } = client.Status()
226
-
227
- unitJS.assert.equal(connected, true)
228
- unitJS.assert.equal(subscribed, false)
229
-
230
- return client.Disconnect()
231
- })
232
- .then(() => {
233
- done()
234
- })
235
- .catch((err) => {
236
- done(err)
237
- })
238
- .finally(() => {
239
- client.Stop()
240
- })
241
- })
242
-
243
- it('last block', (done) => {
244
- const client = new TCaBCIClient([], WebSocket, 'medusa', 'v2')
245
-
246
- client
247
- .LastBlock()
248
- .then(({ blocks, total_count }) => {
249
- unitJS.array(blocks).hasLength(1)
250
- unitJS.assert.equal(total_count, 1)
251
- done()
252
- })
253
- .catch((err) => {
254
- done(err)
255
- })
256
- .finally(() => {
257
- client.Stop()
258
- })
259
- })
260
-
261
- it('transaction search result', (done) => {
262
- const client = new TCaBCIClient([], WebSocket, 'medusa', 'v2')
263
-
264
- client
265
- .TxSearch({
266
- heightOperator: '>=',
267
- height: 0,
268
- recipientAddrs: [
269
- '2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGN',
270
- ],
271
- limit: 1,
272
- offset: 0,
273
- orderBy: 'ASC',
274
- })
275
- .then((data) => {
276
- unitJS.value(data.txs.length).isGreaterThan(0)
277
- unitJS.value(data.total_count).isGreaterThan(0)
278
- done()
279
- })
280
- .catch((err) => {
281
- done(err)
282
- })
283
- .finally(() => {
284
- client.Stop()
285
- })
286
- })
287
- //
288
- it('transaction summary result', (done) => {
289
- const client = new TCaBCIClient([], WebSocket, 'medusa', 'v2')
290
-
291
- client
292
- .TxSummary({
293
- recipientAddrs: [
294
- '2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGN',
295
- ],
296
- })
297
- .then((data) => {
298
- unitJS.value(data.first_block_height).isGreaterThan(0)
299
- unitJS.value(data.first_transaction).isNotEmpty()
300
- unitJS.value(data.last_block_height).isGreaterThan(0)
301
- unitJS.value(data.total_count).isGreaterThan(0)
302
- unitJS.value(data.last_transaction).isNotEmpty()
303
-
304
- done()
305
- })
306
- .catch((err) => {
307
- done(err)
308
- })
309
- .finally(() => {
310
- client.Stop()
311
- })
312
- })
313
-
314
- it('should error not broadcast transaction if type is incorrect', (done) => {
315
- const client = new TCaBCIClient([], WebSocket, 'medusa', 'v2')
316
-
317
- try {
318
- client.Broadcast({
319
- type: 'dummy type',
320
- data: '',
321
- sender_addr: '',
322
- recipient_addr: '',
323
- })
324
-
325
- done(new Error('invalid'))
326
- } catch (err) {
327
- unitJS.assert.equal(TRANSACTION_TYPE_NOT_VALID, err.message)
328
- done()
329
- }
330
-
331
- client.Stop()
332
- })
333
-
334
- it('should error not broadcast transaction', (done) => {
335
- const client = new TCaBCIClient([], WebSocket, 'medusa', 'v2')
336
-
337
- client
338
- .Broadcast({
339
- id: 'dummy id',
340
- version: 0,
341
- type: TX_TYPE_ADDRESS,
342
- data: btoa(JSON.stringify({ data: '' })),
343
- sender_addr:
344
- '2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGN',
345
- recipient_addr:
346
- '2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGN',
347
- sign: btoa('dummy sign'),
348
- fee: 0,
349
- })
350
- .then((data) => {
351
- unitJS.assert.notEqual(data.data.hash, '')
352
- done()
353
- })
354
- .catch((err) => {
355
- unitJS.assert.equal(TRANSACTION_NOT_BROADCAST, err.message)
356
- done()
357
- })
358
- .finally(() => {
359
- client.Stop()
360
- })
361
- })
362
-
363
- it('should error in bulk tx if addresses count is zero', (done) => {
364
- const client = new TCaBCIClient([], WebSocket, 'medusa', 'v2')
365
-
366
- client
367
- .Bulk([])
368
- .then(() => {
369
- done(new Error('invalid return'))
370
- })
371
- .catch((err) => {
372
- unitJS.assert.equal(400, err.response.status)
373
- done()
374
- })
375
- .finally(() => {
376
- client.Stop()
377
- })
378
- })
379
-
380
- it('should error in bulk tx if addresses count is greater than 251', (done) => {
381
- const addresses = []
382
-
383
- for (let i = 0; i < 51; i++) {
384
- addresses.push(
385
- '2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGN',
386
- )
387
- }
388
-
389
- const client = new TCaBCIClient(null, WebSocket, 'medusa', 'v2')
390
-
391
- client
392
- .Bulk(addresses)
393
- .then(() => {
394
- done(new Error('invalid return'))
395
- })
396
- .catch((err) => {
397
- unitJS.assert.equal(400, err.response.status)
398
- done()
399
- })
400
- .finally(() => {
401
- client.Stop()
402
- })
403
- })
404
- })
package/errors_test.js DELETED
@@ -1,11 +0,0 @@
1
- import { FetchError } from './errors.js'
2
- import unitJS from 'unit.js'
3
-
4
- describe('Errors', () => {
5
- it('FetchError', () => {
6
- const err = new FetchError('err1').setStatus(1)
7
-
8
- unitJS.assert.equal(err.message, 'err1')
9
- unitJS.assert.equal(err.code, 1)
10
- })
11
- })
package/eslint.config.js DELETED
@@ -1,45 +0,0 @@
1
- import globals from 'globals'
2
- import pluginJs from '@eslint/js'
3
- import pluginPrettier from 'eslint-plugin-prettier/recommended'
4
-
5
- export default [
6
- {
7
- languageOptions: {
8
- globals: {
9
- ...globals.browser,
10
- process: 'readonly',
11
- global: 'readonly',
12
- },
13
- },
14
- },
15
- pluginJs.configs.recommended,
16
- {
17
- ignores: ['.nyc*', 'node_modules/*', 'eslint*', '*_test.js'],
18
- },
19
- {
20
- rules: {
21
- 'no-console': 'error',
22
- 'no-debugger': 'error',
23
- 'no-extra-boolean-cast': 'off',
24
- 'no-unused-vars': [
25
- 'error',
26
- {
27
- argsIgnorePattern: '^_',
28
- varsIgnorePattern: '^_',
29
- caughtErrorsIgnorePattern: '^_',
30
- },
31
- ],
32
- indent: [
33
- 'error',
34
- 2,
35
- {
36
- SwitchCase: 1,
37
- MemberExpression: 1,
38
- ImportDeclaration: 1,
39
- // VariableDeclarator: { var: 2, let: 2, const: 3 },
40
- },
41
- ],
42
- },
43
- },
44
- pluginPrettier,
45
- ]
@@ -1,134 +0,0 @@
1
- import { describe, it } from 'mocha'
2
- import { expect } from 'chai'
3
- import {
4
- DefaultTimeout,
5
- HTTPPort,
6
- HTTPSPort,
7
- Options,
8
- } from './websocketOptions.js'
9
- import WebSocket from 'ws'
10
-
11
- describe('Options', () => {
12
- it('HTTPSPort', () => {
13
- expect(HTTPSPort).to.equal(443)
14
- })
15
-
16
- it('HTTPPort', () => {
17
- expect(HTTPPort).to.equal(80)
18
- })
19
-
20
- it('constructor/1', () => {
21
- const options = new Options('localhost')
22
-
23
- expect(options).to.be.an.instanceOf(Options)
24
- })
25
-
26
- it('check/0 with valid params', () => {
27
- const options = new Options('localhost')
28
-
29
- expect(options.check()).to.be.equal(true)
30
- })
31
-
32
- it('should error check/0 with invalid params', () => {
33
- const options = new Options(1)
34
-
35
- expect(options.check).to.throw(Error)
36
- })
37
-
38
- it('should error check/0 if exceeded timeout', () => {
39
- const options = new Options('localhost', 20000)
40
-
41
- expect(options.check).to.throw(Error)
42
- })
43
-
44
- it('isLongPoll/0', () => {
45
- const options = new Options('localhost')
46
-
47
- expect(options.isLongPool).to.be.equal(false)
48
- })
49
-
50
- it('setEndpoints/2 with valid params', () => {
51
- const options = new Options('localhost')
52
-
53
- expect(() => {
54
- options.setEndpoints('e1', 'e2')
55
- }).to.not.throw(Error)
56
- })
57
-
58
- it('should error setEndpoints/2 with invalid params', () => {
59
- const options = new Options('localhost')
60
-
61
- expect(() => {
62
- options.setEndpoints(1, false)
63
- }).to.throw(Error)
64
- })
65
-
66
- it('setProtocols/1 with valid params', () => {
67
- const options = new Options('localhost')
68
-
69
- expect(options.setProtocols(['soap'])).to.be.eq(options)
70
- })
71
-
72
- it('should error setProtocols/1 with invalid params', () => {
73
- const options = new Options('localhost')
74
-
75
- expect(() => {
76
- options.setProtocols('invalid')
77
- }).to.throw(Error)
78
- })
79
-
80
- it('protocols/0', () => {
81
- const options = new Options('localhost')
82
-
83
- options.setProtocols(['wamp', 'soap'])
84
- expect(options.protocols).to.have.deep.members(['wamp', 'soap'])
85
- })
86
-
87
- it('setCustomWS/1 with valid params', () => {
88
- const options = new Options('localhost')
89
-
90
- expect(options.setCustomWS(WebSocket)).to.be.eq(options)
91
- })
92
-
93
- it('customWS/0', () => {
94
- const options = new Options('localhost')
95
-
96
- options.setCustomWS(WebSocket)
97
- expect(options.customWS).to.be.eq(WebSocket)
98
- })
99
-
100
- it('url/0', () => {
101
- const options = new Options('localhost.localdomain')
102
-
103
- expect(options.url).to.equal('wss://localhost.localdomain/ws')
104
- })
105
-
106
- it('url/0 with protocol', () => {
107
- const options = new Options('ws://localhost.localdomain')
108
-
109
- expect(options.url).to.equal('ws://localhost.localdomain')
110
- })
111
-
112
- it('url/0 with protocol case two', () => {
113
- const options = new Options('wss://localhost.localdomain/ws')
114
-
115
- expect(options.url).to.equal('wss://localhost.localdomain/ws')
116
- })
117
-
118
- it('make/0', () => {
119
- const options = new Options('localhost.io')
120
- options.setDebug(true)
121
-
122
- const opts = options.make()
123
-
124
- expect(opts.maxReconnectionDelay).to.be.eq(10000)
125
- expect(opts.minReconnectionDelay).to.be.at.least(1000)
126
- expect(opts.reconnectionDelayGrowFactor).to.be.eq(1.3)
127
- expect(opts.minUptime).to.be.eq(5000)
128
- expect(opts.connectionTimeout).to.be.eq(DefaultTimeout)
129
- expect(opts.maxRetries).to.be.eq(10)
130
- expect(opts.maxEnqueuedMessages).to.be.eq(Infinity)
131
- expect(opts.startClosed).to.be.eq(false)
132
- expect(opts.debug).to.be.eq(true)
133
- })
134
- })