@leofcoin/launch-chain 0.2.2 → 0.2.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/README.md CHANGED
@@ -19,6 +19,7 @@ const {chain, endpoints, mode} = await launch()
19
19
  {
20
20
  network: 'leofcoin:peach',
21
21
  stars: ['wss://peach.leofcoin.org'],
22
+ forceRemote: false, // when set to true only tries to connect to an external/local exposed node
22
23
  ws: {
23
24
  port: 4040,
24
25
  url: 'ws://localhost:4040'
package/index.d.ts ADDED
@@ -0,0 +1,30 @@
1
+ import Chain from '@leofcoin/chain/chain';
2
+ type launchMode = 'direct' | 'remote';
3
+ type endpointReturns = {
4
+ http?: string[];
5
+ ws?: string[];
6
+ };
7
+ type launchReturn = {
8
+ chain: Chain;
9
+ mode: launchMode;
10
+ endpoints: endpointReturns;
11
+ };
12
+ type endpointOptions = {
13
+ port: number;
14
+ url?: string;
15
+ };
16
+ type launchOptions = {
17
+ network?: string;
18
+ networkVersion?: string;
19
+ stars: string[];
20
+ forceRemote: boolean;
21
+ ws?: endpointOptions[] | undefined;
22
+ http?: endpointOptions[] | undefined;
23
+ };
24
+ /**
25
+ *
26
+ * @param {object} options { ws: boolean || {url: string, port: number}, http: boolean || {url: string, port: number}, network}
27
+ * @returns '{ mode: string, endpoints: object, chain}'
28
+ */
29
+ declare const launch: (options: launchOptions) => Promise<launchReturn>;
30
+ export { launch as default };
package/index.js ADDED
@@ -0,0 +1,135 @@
1
+ import Node from '@leofcoin/chain/node';
2
+ import Chain from '@leofcoin/chain/chain';
3
+ import nodeConfig from '@leofcoin/lib/node-config';
4
+
5
+ const defaultOptions = {
6
+ network: 'leofcoin:peach',
7
+ networkVersion: 'peach',
8
+ stars: ['wss://peach.leofcoin.org'],
9
+ forceRemote: false,
10
+ ws: [{
11
+ port: 4040
12
+ }],
13
+ http: [{
14
+ port: 8080
15
+ }]
16
+ };
17
+ /**
18
+ *
19
+ * @param {string} url
20
+ * @param {string} networkVersion network/testnet-network sepperate by -
21
+ * @returns Promise(boolean)
22
+ */
23
+ const hasHttp = async (url, networkVersion) => {
24
+ try {
25
+ await fetch(url + '/network');
26
+ return true;
27
+ }
28
+ catch (error) {
29
+ return false;
30
+ }
31
+ };
32
+ const tryWs = (url, networkVersion) => new Promise(async (resolve, reject) => {
33
+ try {
34
+ const socket = await new WebSocket(url, networkVersion);
35
+ socket.onerror = () => resolve(false);
36
+ if (socket.readyState === 1)
37
+ socket.close();
38
+ resolve(true);
39
+ }
40
+ catch (error) {
41
+ reject(error);
42
+ }
43
+ });
44
+ /**
45
+ *
46
+ * @param {string} url
47
+ * @param {string} networkVersion network/testnet-network sepperate by -
48
+ * @returns Promise(boolean)
49
+ */
50
+ const hasWs = async (url, networkVersion) => {
51
+ try {
52
+ await tryWs(url, networkVersion);
53
+ return true;
54
+ }
55
+ catch (error) {
56
+ return false;
57
+ }
58
+ };
59
+ // chain is undefined when mode is remote
60
+ // endpoints contain urls to connect to the desired remote
61
+ // when mode is remote means an instance is already running
62
+ // when mode is direct means chain is directly available and no endpoint is needed to interact with it
63
+ /**
64
+ *
65
+ * @param {object} options { ws: boolean || {url: string, port: number}, http: boolean || {url: string, port: number}, network}
66
+ * @returns '{ mode: string, endpoints: object, chain}'
67
+ */
68
+ const launch = async (options) => {
69
+ if (!options)
70
+ options = defaultOptions;
71
+ else
72
+ options = { ...defaultOptions, ...options };
73
+ const availableEndpoints = {
74
+ http: [],
75
+ ws: []
76
+ };
77
+ if (options.http) {
78
+ for (const endpoint of options.http) {
79
+ if (endpoint.port && !endpoint.url)
80
+ endpoint.url = `http://localhost:${endpoint.port}`;
81
+ if (await hasHttp(endpoint.url, options.networkVersion))
82
+ availableEndpoints.http.push(endpoint.url);
83
+ }
84
+ }
85
+ if (options.ws) {
86
+ for (const endpoint of options.ws) {
87
+ if (endpoint.port && !endpoint.url)
88
+ endpoint.url = `ws://localhost:${endpoint.port}`;
89
+ if (await hasWs(endpoint.url, options.networkVersion))
90
+ availableEndpoints.ws.push(endpoint.url);
91
+ }
92
+ }
93
+ const endpoints = {
94
+ http: [],
95
+ ws: []
96
+ };
97
+ let chain;
98
+ let mode;
99
+ if (availableEndpoints.http.length > 0 || availableEndpoints.ws.length > 0) {
100
+ availableEndpoints.http.forEach(endpoint => {
101
+ endpoints.http.push(endpoint);
102
+ });
103
+ availableEndpoints.ws.forEach(endpoint => {
104
+ endpoints.ws.push(endpoint);
105
+ });
106
+ mode = 'remote';
107
+ }
108
+ else {
109
+ if (options.forceRemote)
110
+ throw new Error(`forceRemote was set but no remotes connected`);
111
+ await new Node({ network: options.network, stars: options.stars, networkVersion: options.networkVersion });
112
+ await nodeConfig({ network: options.network, stars: options.stars, networkVersion: options.networkVersion });
113
+ chain = await new Chain();
114
+ if (options.ws) {
115
+ const importee = await import('@leofcoin/endpoints/ws');
116
+ const wsServer = importee.default;
117
+ await wsServer(chain, options.ws[0].port, options.networkVersion);
118
+ endpoints.ws.push(options.ws[0].url);
119
+ }
120
+ if (options.http) {
121
+ const importee = await import('@leofcoin/endpoints/http');
122
+ const httpServer = importee.default;
123
+ await httpServer(chain, options.http[0].port, options.networkVersion);
124
+ endpoints.http.push(options.http[0].url);
125
+ }
126
+ mode = 'direct';
127
+ }
128
+ return {
129
+ chain,
130
+ mode,
131
+ endpoints
132
+ };
133
+ };
134
+
135
+ export { launch as default };
package/package.json CHANGED
@@ -1,12 +1,14 @@
1
1
  {
2
2
  "name": "@leofcoin/launch-chain",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "",
5
5
  "exports": {
6
- ".": "./exports/launch.js"
6
+ ".": "./index.js"
7
7
  },
8
+ "types": "./index.d.ts",
8
9
  "type": "module",
9
10
  "scripts": {
11
+ "build": "rollup -c",
10
12
  "test": "echo \"Error: no test specified\" && exit 1"
11
13
  },
12
14
  "repository": {
@@ -23,5 +25,10 @@
23
25
  "dependencies": {
24
26
  "@leofcoin/chain": "^1.4.8",
25
27
  "@leofcoin/endpoints": "^0.2.0"
28
+ },
29
+ "devDependencies": {
30
+ "@rollup/plugin-typescript": "^11.0.0",
31
+ "rollup": "^3.9.1",
32
+ "tslib": "^2.4.1"
26
33
  }
27
34
  }
@@ -0,0 +1,13 @@
1
+ import typescript from '@rollup/plugin-typescript';
2
+ import tsconfig from './tsconfig.json' assert { type: 'json'};
3
+
4
+ export default [{
5
+ input: ['./src/index.ts'],
6
+ output: {
7
+ format: 'es',
8
+ dir: './'
9
+ },
10
+ plugins: [
11
+ typescript(tsconfig)
12
+ ]
13
+ }]
package/src/index.ts ADDED
@@ -0,0 +1,178 @@
1
+ import Node from '@leofcoin/chain/node'
2
+ import Chain from '@leofcoin/chain/chain'
3
+ import nodeConfig from '@leofcoin/lib/node-config'
4
+
5
+ type launchMode = 'direct' | 'remote'
6
+
7
+ type endpointReturns = {
8
+ http?: string[],
9
+ ws?: string[],
10
+ }
11
+
12
+ type launchReturn = {
13
+ chain: Chain,
14
+ mode: launchMode,
15
+ endpoints: endpointReturns
16
+ }
17
+
18
+ type endpointOptions = {
19
+ port: number,
20
+ url?: string
21
+ }
22
+
23
+ type launchOptions = {
24
+ network?: string,
25
+ networkVersion?: string,
26
+ stars: string[],
27
+ forceRemote: boolean,
28
+ ws?: endpointOptions[] | undefined,
29
+ http?: endpointOptions[] | undefined,
30
+ }
31
+
32
+ const defaultOptions:launchOptions = {
33
+ network: 'leofcoin:peach',
34
+ networkVersion: 'peach',
35
+ stars: ['wss://peach.leofcoin.org'],
36
+ forceRemote: false,
37
+ ws: [{
38
+ port: 4040
39
+ }],
40
+ http: [{
41
+ port: 8080
42
+ }]
43
+ }
44
+
45
+ /**
46
+ *
47
+ * @param {string} url
48
+ * @param {string} networkVersion network/testnet-network sepperate by -
49
+ * @returns Promise(boolean)
50
+ */
51
+ const hasHttp = async (url: string, networkVersion: string) => {
52
+ try {
53
+ await fetch(url + '/network')
54
+ return true
55
+ } catch (error) {
56
+ return false
57
+ }
58
+ }
59
+
60
+ const tryWs = (url: string | URL, networkVersion: string | string[]): Promise<boolean> => new Promise(async (resolve, reject) => {
61
+ try {
62
+ const socket = await new WebSocket(url, networkVersion)
63
+ socket.onerror = () => resolve(false)
64
+ if (socket.readyState === 1) socket.close()
65
+ resolve(true)
66
+ } catch (error) {
67
+ reject(error)
68
+ }
69
+ })
70
+
71
+ /**
72
+ *
73
+ * @param {string} url
74
+ * @param {string} networkVersion network/testnet-network sepperate by -
75
+ * @returns Promise(boolean)
76
+ */
77
+ const hasWs = async (url: string, networkVersion: string): Promise<boolean> => {
78
+ try {
79
+ await tryWs(url, networkVersion)
80
+ return true
81
+ } catch (error) {
82
+ return false
83
+ }
84
+ }
85
+
86
+ /**
87
+ *
88
+ * @param {string} httpURL
89
+ * @param {string} wsURL
90
+ * @param {string} networkVersion
91
+ * @returns Promise({http: boolean, ws: boolean})
92
+ */
93
+ const hasClient = async (httpURL: string, wsURL: string, networkVersion: string) => {
94
+ const ws = await hasWs(wsURL, networkVersion)
95
+ const http = await hasHttp(httpURL, networkVersion)
96
+ return {http, ws}
97
+ }
98
+
99
+ // chain is undefined when mode is remote
100
+ // endpoints contain urls to connect to the desired remote
101
+ // when mode is remote means an instance is already running
102
+ // when mode is direct means chain is directly available and no endpoint is needed to interact with it
103
+ /**
104
+ *
105
+ * @param {object} options { ws: boolean || {url: string, port: number}, http: boolean || {url: string, port: number}, network}
106
+ * @returns '{ mode: string, endpoints: object, chain}'
107
+ */
108
+ const launch = async (options: launchOptions): Promise<launchReturn> => {
109
+ if (!options) options = defaultOptions
110
+ else options = {...defaultOptions, ...options }
111
+
112
+ const availableEndpoints: endpointReturns = {
113
+ http: [],
114
+ ws: []
115
+ }
116
+
117
+ if (options.http) {
118
+ for (const endpoint of options.http) {
119
+ if (endpoint.port && !endpoint.url) endpoint.url = `http://localhost:${endpoint.port}`
120
+ if (await hasHttp(endpoint.url, options.networkVersion)) availableEndpoints.http.push(endpoint.url)
121
+ }
122
+ }
123
+
124
+ if (options.ws) {
125
+ for (const endpoint of options.ws) {
126
+ if (endpoint.port && !endpoint.url) endpoint.url = `ws://localhost:${endpoint.port}`
127
+ if (await hasWs(endpoint.url, options.networkVersion)) availableEndpoints.ws.push(endpoint.url)
128
+ }
129
+ }
130
+
131
+ const endpoints: endpointReturns = {
132
+ http: [],
133
+ ws: []
134
+ }
135
+
136
+ let chain: Chain
137
+ let mode: launchMode
138
+
139
+ if (availableEndpoints.http.length > 0 || availableEndpoints.ws.length > 0) {
140
+ availableEndpoints.http.forEach(endpoint => {
141
+ endpoints.http.push(endpoint)
142
+ })
143
+
144
+ availableEndpoints.ws.forEach(endpoint => {
145
+ endpoints.ws.push(endpoint)
146
+ })
147
+
148
+ mode = 'remote'
149
+ } else {
150
+ if (options.forceRemote) throw new Error(`forceRemote was set but no remotes connected`)
151
+
152
+ await new Node({ network: options.network, stars: options.stars, networkVersion: options.networkVersion })
153
+ await nodeConfig({ network: options.network, stars: options.stars, networkVersion: options.networkVersion })
154
+
155
+ chain = await new Chain()
156
+
157
+ if (options.ws) {
158
+ const importee = await import('@leofcoin/endpoints/ws')
159
+ const wsServer = importee.default
160
+ await wsServer(chain, options.ws[0].port, options.networkVersion)
161
+ endpoints.ws.push(options.ws[0].url)
162
+ }
163
+ if (options.http) {
164
+ const importee = await import('@leofcoin/endpoints/http')
165
+ const httpServer = importee.default
166
+ await httpServer(chain, options.http[0].port, options.networkVersion)
167
+ endpoints.http.push(options.http[0].url)
168
+ }
169
+ mode = 'direct'
170
+ }
171
+ return {
172
+ chain,
173
+ mode,
174
+ endpoints
175
+ }
176
+ }
177
+
178
+ export { launch as default}
package/test.js CHANGED
@@ -1,6 +1,6 @@
1
- import launch from './exports/launch.js'
1
+ import launch from './index.js'
2
2
 
3
- launch({
3
+ console.log(await launch({
4
4
  network: 'leofcoin:peach',
5
5
  networkVersion: 'peach'
6
- })
6
+ }));
package/tsconfig.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es2022",
4
+ "module": "es2022",
5
+ "outDir": "./",
6
+ "moduleResolution":"NodeNext",
7
+ "allowJs": true,
8
+ "declaration": true
9
+ },
10
+ "include": [
11
+ "./src/**/*"
12
+ ],
13
+ "exclude": ["./node_modules"]
14
+ }
package/exports/launch.js DELETED
@@ -1,116 +0,0 @@
1
- import Node from '@leofcoin/chain/node'
2
- import Chain from '@leofcoin/chain/chain'
3
- import nodeConfig from '@leofcoin/lib/node-config'
4
-
5
- /**
6
- *
7
- * @param {string} url
8
- * @param {string} networkVersion network/testnet-network sepperate by -
9
- * @returns Promise(boolean)
10
- */
11
- const hasHttp = async (url, networkVersion) => {
12
- try {
13
- await fetch(url + '/network')
14
- return true
15
- } catch (error) {
16
- return false
17
- }
18
- }
19
-
20
- const tryWs = (url, networkVersion) => new Promise(async (resolve, reject) => {
21
- try {
22
- const socket = await new WebSocket(url, networkVersion)
23
- socket.onerror = () => resolve(false)
24
- if (socket.readyState === 1) socket.close()
25
- resolve(true)
26
- } catch (error) {
27
- reject(error)
28
- }
29
- })
30
-
31
- /**
32
- *
33
- * @param {string} url
34
- * @param {string} networkVersion network/testnet-network sepperate by -
35
- * @returns Promise(boolean)
36
- */
37
- const hasWs = async (url, networkVersion) => {
38
- try {
39
- await tryWs(url, networkVersion)
40
- return true
41
- } catch (error) {
42
- return false
43
- }
44
- }
45
-
46
- /**
47
- *
48
- * @param {string} httpURL
49
- * @param {string} wsURL
50
- * @param {string} networkVersion
51
- * @returns Promise({http: boolean, ws: boolean})
52
- */
53
- const hasClient = async (httpURL, wsURL, networkVersion) => {
54
- const ws = await hasWs(wsURL, networkVersion)
55
- const http = await hasHttp(httpURL, networkVersion)
56
- return {http, ws}
57
- }
58
-
59
- // chain is undefined when mode is remote
60
- // endpoints contain urls to connect to the desired remote
61
- // when mode is remote means an instance is already running
62
- // when mode is direct means chain is directly available and no endpoint is needed to interact with it
63
- /**
64
- *
65
- * @param {object} options { ws: boolean || {url: string, port: number}, http: boolean || {url: string, port: number}, network}
66
- * @returns '{ mode: string, endpoints: object, chain}'
67
- */
68
- const launch = async (options = {}) => {
69
- if (!options) options = {}
70
- if (!options.network) options.network = 'leofcoin:peach'
71
- if (!options.stars) options.stars = ['wss://peach.leofcoin.org']
72
- if (options.ws === undefined) options.ws = { port: 4040 }
73
- if (options.http === undefined) options.http = { port: 8080 }
74
- if (options.networkVersion === undefined) options.networkVersion = options.network.replace(':', '-')
75
-
76
- if (options.http?.port && !options.http.url) options.http.url = `http://localhost:${options.http.port}`
77
- if (options.ws?.port && !options.ws.url) options.ws.url = `ws://localhost:${options.ws.port}`
78
-
79
- const clients = await hasClient(options.http.url, options.ws.url, options.networkVersion)
80
- let endpoints = {}
81
- let chain
82
- let mode
83
-
84
- if (clients.http || clients.ws) {
85
- Object.entries(clients).forEach(([key, value]) => {
86
- if (value) endpoints[key] = options[key].url
87
- })
88
- mode = 'remote'
89
- } else {
90
- await new Node({ network: options.network, stars: options.stars })
91
- await nodeConfig({ network: options.network, stars: options.stars })
92
-
93
- chain = await new Chain()
94
-
95
- if (options.ws) {
96
- const importee = await import('@leofcoin/endpoints/ws')
97
- const wsServer = importee.default
98
- await wsServer(chain, options.ws.port, options.networkVersion)
99
- endpoints.ws = options.ws.url
100
- }
101
- if (options.http) {
102
- const importee = await import('@leofcoin/endpoints/http')
103
- const httpServer = importee.default
104
- await httpServer(chain, options.http.port, options.networkVersion)
105
- endpoints.http = options.http.url
106
- }
107
- mode = 'direct'
108
- }
109
- return {
110
- chain,
111
- mode,
112
- endpoints
113
- }
114
- }
115
-
116
- export { launch as default}