FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
aepp-sdk-js/es/node-pool/index.js at develop · javascriptit/aepp-sdk-js · GitHub
javascriptit
aepp-sdk-js
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
aepp-sdk-js
/
es
/
node-pool
/
index.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
148 lines (142 loc) · 4.43 KB
Breadcrumbs
aepp-sdk-js
/
es
/
node-pool
/
index.js
Copy path
File metadata and controls
148 lines (142 loc) · 4.43 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* NodePool module
*
@module
@aeternity
/aepp-sdk/es/node-pool
*
@export
NodePool
*
@example
import NodePool from '@aeternity/aepp-sdk/es/node-pool'
*/
import
{
getterForCurrentNode
,
prepareNodeObject
}
from
'./helpers'
import
AsyncInit
from
'../utils/async-init'
import
{
getNetworkId
}
from
'../node'
/**
* Node Pool Stamp
* This stamp allow you to make basic manipulation(add, remove, select) on list of nodes
*
@function
*
@alias
module:
@aeternity
/aepp-sdk/es/node-pool
*
@rtype
Stamp
*
@param
{
Object
} [options={}] - Initializer object
*
@param
{
Array
} [options.nodes] - Array with Node instances
*
@return
{
Object
} NodePool instance
*/
export
const
NodePool
=
AsyncInit
.
compose
(
{
async
init
(
{
nodes
=
[
]
}
=
{
}
)
{
this
.
pool
=
new
Map
(
)
this
.
validateNodes
(
nodes
)
nodes
.
forEach
(
(
node
,
i
)
=>
{
const
{
name
,
instance
}
=
node
this
.
addNode
(
name
,
instance
,
i
===
0
)
}
)
if
(
nodes
.
length
)
this
.
selectNode
(
nodes
[
0
]
.
name
)
}
,
propertyDescriptors
:
{
api
:
{
enumerable
:
true
,
configurable
:
false
,
get
(
)
{
return
getterForCurrentNode
(
this
.
selectedNode
)
}
}
}
,
methods
:
{
/**
* Add Node
*
@function
*
@alias
module:
@aeternity
/aepp-sdk/es/node-pool
*
@rtype
(name: String, nodeInstance: Object, select: Boolean) => Void
*
@param
{
String
} name - Node name
*
@param
{
Object
} nodeInstance - Node instance
*
@param
{
Boolean
} select - Select this node as current
*
@return
{
Void
}
*
@example
* nodePool.addNode('testNode', awaitNode({ url, internalUrl }), true) // add and select new node with name 'testNode'
*/
addNode
(
name
,
nodeInstance
,
select
=
false
)
{
if
(
this
.
pool
.
has
(
name
)
)
throw
new
Error
(
`Node with name
${
name
}
already exist`
)
this
.
validateNodes
(
[
{
name
,
instance
:
nodeInstance
}
]
)
this
.
pool
.
set
(
name
,
prepareNodeObject
(
name
,
nodeInstance
)
)
if
(
select
||
!
this
.
selectedNode
)
{
this
.
selectNode
(
name
)
}
}
,
/**
* Select Node
*
@function
*
@alias
module:
@aeternity
/aepp-sdk/es/node-pool
*
@rtype
(name: String) => Void
*
@param
{
String
} name - Node name
*
@return
{
Void
}
*
@example
* nodePool.selectNode('testNode')
*/
selectNode
(
name
)
{
if
(
!
this
.
pool
.
has
(
name
)
)
throw
new
Error
(
`Node with name
${
name
}
not in pool`
)
this
.
selectedNode
=
this
.
pool
.
get
(
name
)
}
,
/**
* Get NetworkId of current Node
*
@function
*
@alias
module:
@aeternity
/aepp-sdk/es/node-pool
*
@rtype
() => String
*
@return
{
String
}
*
@example
* nodePool.getNetworkId()
*/
getNetworkId
,
/**
* Check if you have selected node
*
@function
*
@alias
module:
@aeternity
/aepp-sdk/es/node-pool
*
@rtype
() => Boolean
*
@return
{
Boolean
}
*
@example
* nodePool.isNodeConnected()
*/
isNodeConnected
(
)
{
return
!
!
this
.
selectedNode
.
instance
}
,
/**
* Get information about node
*
@function
*
@alias
module:
@aeternity
/aepp-sdk/es/node-pool
*
@rtype
() => Object
*
@return
{
Object
}
*
@example
* nodePool.getNodeInfo() // { name, version, networkId, protocol, ... }
*/
getNodeInfo
(
)
{
if
(
!
this
.
isNodeConnected
(
)
)
throw
new
Error
(
'Can not get node info. Node is not connected'
)
return
{
name
:
this
.
selectedNode
.
name
,
...
this
.
selectedNode
.
instance
.
getNodeInfo
(
)
}
}
,
/**
* Get array of available nodes
*
@function
*
@alias
module:
@aeternity
/aepp-sdk/es/node-pool
*
@rtype
() => Object[]
*
@return
{
Object[]
}
*
@example
* nodePool.getNodesInPool()
*/
getNodesInPool
(
)
{
return
Array
.
from
(
this
.
pool
.
entries
(
)
)
.
map
(
(
[
name
,
node
]
)
=>
(
{
name
,
...
node
.
instance
.
getNodeInfo
(
)
}
)
)
}
,
validateNodes
(
nodes
)
{
const
nodeProps
=
[
'api'
,
'consensusProtocolVersion'
,
'genesisHash'
]
nodes
.
forEach
(
(
node
,
index
)
=>
{
if
(
typeof
node
!==
'object'
)
throw
new
Error
(
'Node must be an object with "name" and "instance" props'
)
if
(
[
'name'
,
'instance'
]
.
find
(
k
=>
!
node
[
k
]
)
)
throw
new
Error
(
`Node object on index
${
index
}
must contain node "name" and "ins"`
)
if
(
!
node
.
instance
||
typeof
node
.
instance
!==
'object'
||
nodeProps
.
find
(
prop
=>
!
(
prop
in
node
.
instance
)
)
)
{
throw
new
Error
(
'Invalid node instance object'
)
}
}
)
}
}
,
props
:
{
selectedNode
:
{
}
}
}
)
export
default
NodePool
Back
|
FazBrowse Home
|
New Git URL