@tchain/tcabci-read-js-client 1.3.3 → 1.3.4
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 +17 -3
- package/client_test.js +12 -1
- package/errors.js +23 -12
- package/message.js +20 -3
- package/package.json +1 -1
package/client.js
CHANGED
|
@@ -18,7 +18,7 @@ class TCAbciClient {
|
|
|
18
18
|
subscribed = false
|
|
19
19
|
subscribedAddresses = []
|
|
20
20
|
connected = false
|
|
21
|
-
version = "v1.3.
|
|
21
|
+
version = "v1.3.4"
|
|
22
22
|
errorCb = null
|
|
23
23
|
listenCb = null
|
|
24
24
|
ws = null
|
|
@@ -57,18 +57,31 @@ class TCAbciClient {
|
|
|
57
57
|
this.setConnected(false)
|
|
58
58
|
this.setSubscribed(false)
|
|
59
59
|
}
|
|
60
|
-
Subscribe(addresses) {
|
|
60
|
+
Subscribe(addresses = [], txTypes = []) {
|
|
61
61
|
if (!Array.isArray(addresses)) {
|
|
62
62
|
throw INVALID_ARGUMENTS
|
|
63
63
|
}
|
|
64
|
+
|
|
65
|
+
if (txTypes && !Array.isArray(addresses)) {
|
|
66
|
+
throw INVALID_ARGUMENTS
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (txTypes.length && txTypes.length > Object.values(TX_TYPE).length) {
|
|
70
|
+
throw INVALID_ARGUMENTS
|
|
71
|
+
}
|
|
72
|
+
|
|
64
73
|
if (!this.getConnected()) {
|
|
65
74
|
throw NOT_CONNECTED
|
|
66
75
|
}
|
|
76
|
+
|
|
67
77
|
let addrs = []
|
|
78
|
+
|
|
68
79
|
if (addresses.length === 0) {
|
|
69
80
|
throw ADDRESSES_IS_EMPTY
|
|
70
81
|
}
|
|
82
|
+
|
|
71
83
|
addrs = addresses
|
|
84
|
+
|
|
72
85
|
if (this.getSubscribeAddresses().length > 0) {
|
|
73
86
|
let newAddress = []
|
|
74
87
|
for (let i = 0; i < addresses.length; i++) {
|
|
@@ -78,7 +91,8 @@ class TCAbciClient {
|
|
|
78
91
|
}
|
|
79
92
|
addrs = newAddress
|
|
80
93
|
}
|
|
81
|
-
|
|
94
|
+
|
|
95
|
+
const message = new Message(true, MESSAGE_TYPE.SUBSCRIBE, addrs, txTypes)
|
|
82
96
|
this.ws.send(message.ToJSONString())
|
|
83
97
|
this.setSubscribeAddresses(addrs, true)
|
|
84
98
|
this.setSubscribed(true)
|
package/client_test.js
CHANGED
|
@@ -63,7 +63,7 @@ describe("TCAbciClient TESTS", () => {
|
|
|
63
63
|
const client = new tcAbciClient()
|
|
64
64
|
client.Start()
|
|
65
65
|
.then(() => {
|
|
66
|
-
client.Subscribe(["2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGN"])
|
|
66
|
+
client.Subscribe(["2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGN"], [TX_TYPE.TX_TYPE_STORAGE])
|
|
67
67
|
const { connected, subscribed } = client.Status()
|
|
68
68
|
unitJS.assert.equal(connected, true)
|
|
69
69
|
unitJS.assert.equal(subscribed, true)
|
|
@@ -74,6 +74,17 @@ describe("TCAbciClient TESTS", () => {
|
|
|
74
74
|
})
|
|
75
75
|
})
|
|
76
76
|
|
|
77
|
+
it('should error subscribe with invalid tx type parameter',(done) => {
|
|
78
|
+
const client = new tcAbciClient()
|
|
79
|
+
client.Start()
|
|
80
|
+
.then(() => {
|
|
81
|
+
client.Subscribe(["2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGN"], ['invalid'])
|
|
82
|
+
})
|
|
83
|
+
.catch(() => {
|
|
84
|
+
done()
|
|
85
|
+
})
|
|
86
|
+
})
|
|
87
|
+
|
|
77
88
|
it('should unsubscribe with valid parameters',(done) => {
|
|
78
89
|
const client = new tcAbciClient()
|
|
79
90
|
client.Start()
|
package/errors.js
CHANGED
|
@@ -1,14 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
return new Error(this.INVALID_ARGUMENTS + " " + val)
|
|
1
|
+
const ALREADY_CONNECTED = new Error("Already connected"),
|
|
2
|
+
NOT_CONNECTED = new Error("Not connected"),
|
|
3
|
+
INVALID_ARGUMENTS = new Error("Check your arguments"),
|
|
4
|
+
INVALID_ARGUMENT_WITH_CS = (val) => {
|
|
5
|
+
return new Error(INVALID_ARGUMENTS + " " + val)
|
|
7
6
|
},
|
|
8
|
-
NOT_SUBSCRIBED
|
|
9
|
-
ALREADY_SUBSCRIBED
|
|
10
|
-
ADDRESSES_IS_EMPTY
|
|
11
|
-
BLOCK_NOT_FOUND
|
|
12
|
-
TRANSACTION_TYPE_NOT_VALID
|
|
13
|
-
TRANSACTION_NOT_BROADCAST
|
|
7
|
+
NOT_SUBSCRIBED = new Error("Not subscribed"),
|
|
8
|
+
ALREADY_SUBSCRIBED = new Error("Already subscribed"),
|
|
9
|
+
ADDRESSES_IS_EMPTY = new Error("Addresses is empty"),
|
|
10
|
+
BLOCK_NOT_FOUND = new Error("Block does not exist"),
|
|
11
|
+
TRANSACTION_TYPE_NOT_VALID = new Error("Transaction type is not valid"),
|
|
12
|
+
TRANSACTION_NOT_BROADCAST = new Error("Transaction can not be broadcast")
|
|
13
|
+
|
|
14
|
+
module.exports = {
|
|
15
|
+
ALREADY_CONNECTED,
|
|
16
|
+
NOT_CONNECTED,
|
|
17
|
+
INVALID_ARGUMENTS,
|
|
18
|
+
INVALID_ARGUMENT_WITH_CS,
|
|
19
|
+
NOT_SUBSCRIBED,
|
|
20
|
+
ALREADY_SUBSCRIBED,
|
|
21
|
+
ADDRESSES_IS_EMPTY,
|
|
22
|
+
BLOCK_NOT_FOUND,
|
|
23
|
+
TRANSACTION_TYPE_NOT_VALID,
|
|
24
|
+
TRANSACTION_NOT_BROADCAST,
|
|
14
25
|
}
|
package/message.js
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
const { INVALID_ARGUMENTS, INVALID_ARGUMENT_WITH_CS } = require("./errors")
|
|
2
|
-
const { MESSAGE_TYPE } = require("./constants")
|
|
2
|
+
const { MESSAGE_TYPE, TX_TYPE} = require("./constants")
|
|
3
3
|
class Message {
|
|
4
4
|
#is_web = false
|
|
5
5
|
#type = ""
|
|
6
6
|
#addrs = []
|
|
7
|
-
|
|
7
|
+
#txTypes = []
|
|
8
|
+
constructor(isWeb, type, addrs, txTypes = []) {
|
|
8
9
|
this.#is_web = isWeb
|
|
9
10
|
this.#type = type
|
|
10
11
|
this.#addrs = addrs
|
|
12
|
+
this.#txTypes = txTypes
|
|
11
13
|
}
|
|
12
14
|
|
|
13
15
|
ToJSONString() {
|
|
@@ -15,7 +17,8 @@ class Message {
|
|
|
15
17
|
return JSON.stringify({
|
|
16
18
|
"is_web": this.#is_web,
|
|
17
19
|
"type": this.#type,
|
|
18
|
-
"addrs": this.#addrs
|
|
20
|
+
"addrs": this.#addrs,
|
|
21
|
+
...(this.#txTypes.length > 0 ? { "tx_types": this.#txTypes } : {})
|
|
19
22
|
})
|
|
20
23
|
}
|
|
21
24
|
|
|
@@ -23,15 +26,29 @@ class Message {
|
|
|
23
26
|
if (typeof this.#is_web !== "boolean") {
|
|
24
27
|
throw INVALID_ARGUMENT_WITH_CS("is_web")
|
|
25
28
|
}
|
|
29
|
+
|
|
26
30
|
if (typeof this.#type !== "string") {
|
|
27
31
|
throw INVALID_ARGUMENT_WITH_CS("type")
|
|
28
32
|
}
|
|
33
|
+
|
|
29
34
|
if (!Array.isArray(this.#addrs)) {
|
|
30
35
|
throw INVALID_ARGUMENT_WITH_CS("addrs")
|
|
31
36
|
}
|
|
37
|
+
|
|
32
38
|
if ([MESSAGE_TYPE.SUBSCRIBE, MESSAGE_TYPE.UNSUBSCRIBE].indexOf(this.#type) === -1) {
|
|
33
39
|
throw INVALID_ARGUMENTS
|
|
34
40
|
}
|
|
41
|
+
|
|
42
|
+
if (this.#txTypes.length) {
|
|
43
|
+
let vals = Object.values(TX_TYPE)
|
|
44
|
+
for (let i = 0; i < this.#txTypes.length; i++) {
|
|
45
|
+
if (vals.indexOf(this.#txTypes[i]) === -1) {
|
|
46
|
+
throw INVALID_ARGUMENT_WITH_CS('tx_types')
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
vals = []
|
|
51
|
+
}
|
|
35
52
|
}
|
|
36
53
|
}
|
|
37
54
|
|