@leofcoin/launch-chain 0.1.0
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/.gitattributes +2 -0
- package/LICENSE +21 -0
- package/README.md +38 -0
- package/exports/launch.js +113 -0
- package/package.json +27 -0
package/.gitattributes
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 vandeurenglenn
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# lauch-chain
|
|
2
|
+
> launch chain & peernet (reuses exposed transports when already running)
|
|
3
|
+
|
|
4
|
+
## usage
|
|
5
|
+
```js
|
|
6
|
+
import launch from '@leofcoin/launch-chain'
|
|
7
|
+
|
|
8
|
+
const {chain, endpoints, mode} = await launch()
|
|
9
|
+
// chain is undefined when mode is remote
|
|
10
|
+
// endpoints contain urls to connect to the desired remote
|
|
11
|
+
// when mode is remote means an instance is already running
|
|
12
|
+
// when mode is direct means chain is directly available and no endpoint is needed to interact with it
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## options
|
|
16
|
+
### default
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
{
|
|
20
|
+
network: 'leofcoin:peach',
|
|
21
|
+
ws: {
|
|
22
|
+
port: 4040,
|
|
23
|
+
url: 'ws://localhost:4040'
|
|
24
|
+
},
|
|
25
|
+
http: {
|
|
26
|
+
port: 8080,
|
|
27
|
+
url: 'http://localhost:8080'
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### disabling options
|
|
33
|
+
```js
|
|
34
|
+
{
|
|
35
|
+
ws: false,
|
|
36
|
+
http: false
|
|
37
|
+
}
|
|
38
|
+
```
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import Node from '@leofcoin/chain/node'
|
|
2
|
+
import Chain from '@leofcoin/chain/chain'
|
|
3
|
+
import nodeConfig from '@leofcoin/lib/node-config.js'
|
|
4
|
+
import wsServer from '@leofcoin/endpoints/ws'
|
|
5
|
+
import httpServer from '@leofcoin/endpoints/http'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
*
|
|
9
|
+
* @param {string} url
|
|
10
|
+
* @param {string} networkVersion network/testnet-network sepperate by -
|
|
11
|
+
* @returns Promise(boolean)
|
|
12
|
+
*/
|
|
13
|
+
const hasHttp = async (url, networkVersion) => {
|
|
14
|
+
try {
|
|
15
|
+
await fetch(url + '/network')
|
|
16
|
+
return true
|
|
17
|
+
} catch (error) {
|
|
18
|
+
return false
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const tryWs = (url, networkVersion) => new Promise(async (resolve, reject) => {
|
|
23
|
+
try {
|
|
24
|
+
const socket = await new WebSocket(url, networkVersion)
|
|
25
|
+
socket.onerror = () => resolve(false)
|
|
26
|
+
if (socket.readyState === 1) socket.close()
|
|
27
|
+
resolve(true)
|
|
28
|
+
} catch (error) {
|
|
29
|
+
reject(error)
|
|
30
|
+
}
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
*
|
|
35
|
+
* @param {string} url
|
|
36
|
+
* @param {string} networkVersion network/testnet-network sepperate by -
|
|
37
|
+
* @returns Promise(boolean)
|
|
38
|
+
*/
|
|
39
|
+
const hasWs = async (url, networkVersion) => {
|
|
40
|
+
try {
|
|
41
|
+
await tryWs(url, networkVersion)
|
|
42
|
+
return true
|
|
43
|
+
} catch (error) {
|
|
44
|
+
return false
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
*
|
|
50
|
+
* @param {string} httpURL
|
|
51
|
+
* @param {string} wsURL
|
|
52
|
+
* @param {string} networkVersion
|
|
53
|
+
* @returns Promise({http: boolean, ws: boolean})
|
|
54
|
+
*/
|
|
55
|
+
const hasClient = async (httpURL, wsURL, networkVersion) => {
|
|
56
|
+
const ws = await hasWs(wsURL, networkVersion)
|
|
57
|
+
const http = await hasHttp(httpURL, networkVersion)
|
|
58
|
+
return {http, ws}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// chain is undefined when mode is remote
|
|
62
|
+
// endpoints contain urls to connect to the desired remote
|
|
63
|
+
// when mode is remote means an instance is already running
|
|
64
|
+
// when mode is direct means chain is directly available and no endpoint is needed to interact with it
|
|
65
|
+
/**
|
|
66
|
+
*
|
|
67
|
+
* @param {object} options { ws: boolean || {url: string, port: number}, http: boolean || {url: string, port: number}, network}
|
|
68
|
+
* @returns '{ mode: string, endpoints: object, chain}'
|
|
69
|
+
*/
|
|
70
|
+
const launch = async (options = {ws, http, network: 'leofcoin:peach'}) => {
|
|
71
|
+
if (!options) options = {}
|
|
72
|
+
if (!options.network) options.network = 'leofcoin:peach'
|
|
73
|
+
if (options.ws === undefined) options.ws = { port: 4040 }
|
|
74
|
+
if (options.http === undefined) options.http = { port: 8080 }
|
|
75
|
+
if (options.networkVersion === undefined) options.networkVersion = options.network.replace(':', '-')
|
|
76
|
+
|
|
77
|
+
if (options.http?.port && !options.http.url) options.http.url = `http://localhost:${options.http.port}`
|
|
78
|
+
if (options.ws?.port && !options.ws.url) options.ws.url = `ws://localhost:${options.ws.port}`
|
|
79
|
+
|
|
80
|
+
const clients = await hasClient(options.http.url, options.ws.url, options.networkVersion)
|
|
81
|
+
let endpoints
|
|
82
|
+
let chain
|
|
83
|
+
let mode
|
|
84
|
+
|
|
85
|
+
if (clients) {
|
|
86
|
+
Object.entries(clients).forEach(([key, value]) => {
|
|
87
|
+
if (value) endpoints[key] = options[key].url
|
|
88
|
+
})
|
|
89
|
+
mode = 'remote'
|
|
90
|
+
} else {
|
|
91
|
+
await new Node({ network })
|
|
92
|
+
await nodeConfig({ network })
|
|
93
|
+
|
|
94
|
+
chain = await new Chain()
|
|
95
|
+
|
|
96
|
+
if (ws) {
|
|
97
|
+
await wsServer(chain, options.ws.port, options.networkVersion)
|
|
98
|
+
endpoints.ws = options.ws.url
|
|
99
|
+
}
|
|
100
|
+
if (http) {
|
|
101
|
+
await httpServer(chain, options.http.port, options.networkVersion)
|
|
102
|
+
endpoints.http = options.http.url
|
|
103
|
+
}
|
|
104
|
+
mode = 'direct'
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
chain,
|
|
108
|
+
mode,
|
|
109
|
+
endpoints
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export { launch as default}
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@leofcoin/launch-chain",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"exports": {
|
|
6
|
+
".": "./exports/launch.js"
|
|
7
|
+
},
|
|
8
|
+
"type": "module",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/leofcoin/launch-chain.git"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [],
|
|
17
|
+
"author": "",
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/leofcoin/launch-chain/issues"
|
|
21
|
+
},
|
|
22
|
+
"homepage": "https://github.com/leofcoin/launch-chain#readme",
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@leofcoin/chain": "^1.4.5",
|
|
25
|
+
"@leofcoin/endpoints": "^0.1.0"
|
|
26
|
+
}
|
|
27
|
+
}
|