@leofcoin/launch-chain 0.2.5 → 0.2.7

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/index.d.ts CHANGED
@@ -1,13 +1,20 @@
1
1
  import Chain from '@leofcoin/chain/chain';
2
- type launchMode = 'direct' | 'remote';
2
+ import WSClient from '@leofcoin/endpoint-clients/ws';
3
+ import HttpClient from '@leofcoin/endpoint-clients/http';
4
+ type launchMode = 'direct' | 'remote' | 'server';
3
5
  type endpointReturns = {
4
6
  http?: string[];
5
7
  ws?: string[];
6
8
  };
9
+ type clientReturns = {
10
+ http?: HttpClient[];
11
+ ws?: WSClient[];
12
+ };
7
13
  type launchReturn = {
8
14
  chain: Chain;
9
15
  mode: launchMode;
10
16
  endpoints: endpointReturns;
17
+ clients: clientReturns;
11
18
  };
12
19
  type endpointOptions = {
13
20
  port: number;
@@ -18,6 +25,7 @@ type launchOptions = {
18
25
  networkVersion?: string;
19
26
  stars: string[];
20
27
  forceRemote: boolean;
28
+ mode?: launchMode;
21
29
  ws?: endpointOptions[] | undefined;
22
30
  http?: endpointOptions[] | undefined;
23
31
  };
package/index.js CHANGED
@@ -1,12 +1,15 @@
1
1
  import Node from '@leofcoin/chain/node';
2
2
  import Chain from '@leofcoin/chain/chain';
3
3
  import nodeConfig from '@leofcoin/lib/node-config';
4
+ import WSClient from '@leofcoin/endpoint-clients/ws';
5
+ import HttpClient from '@leofcoin/endpoint-clients/http';
4
6
 
5
7
  const defaultOptions = {
6
8
  network: 'leofcoin:peach',
7
9
  networkVersion: 'peach',
8
10
  stars: ['wss://peach.leofcoin.org'],
9
11
  forceRemote: false,
12
+ mode: 'direct',
10
13
  ws: [{
11
14
  port: 4040
12
15
  }],
@@ -20,22 +23,21 @@ const defaultOptions = {
20
23
  * @param {string} networkVersion network/testnet-network sepperate by -
21
24
  * @returns Promise(boolean)
22
25
  */
23
- const hasHttp = async (url, networkVersion) => {
26
+ const getHttp = async (url, networkVersion) => {
24
27
  try {
25
- await fetch(url + '/network');
26
- return true;
28
+ const client = new HttpClient(url, networkVersion);
29
+ await client.network();
30
+ return client;
27
31
  }
28
32
  catch (error) {
29
- return false;
33
+ return undefined;
30
34
  }
31
35
  };
32
36
  const tryWs = (url, networkVersion) => new Promise(async (resolve, reject) => {
33
37
  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);
38
+ const socket = await new WSClient(url, networkVersion);
39
+ await socket.init();
40
+ resolve(socket);
39
41
  }
40
42
  catch (error) {
41
43
  reject(error);
@@ -47,13 +49,13 @@ const tryWs = (url, networkVersion) => new Promise(async (resolve, reject) => {
47
49
  * @param {string} networkVersion network/testnet-network sepperate by -
48
50
  * @returns Promise(boolean)
49
51
  */
50
- const hasWs = async (url, networkVersion) => {
52
+ const getWS = async (url, networkVersion) => {
51
53
  try {
52
- await tryWs(url, networkVersion);
53
- return true;
54
+ const ws = await tryWs(url, networkVersion);
55
+ return ws;
54
56
  }
55
57
  catch (error) {
56
- return false;
58
+ return undefined;
57
59
  }
58
60
  };
59
61
  // chain is undefined when mode is remote
@@ -70,65 +72,98 @@ const launch = async (options, password) => {
70
72
  options = defaultOptions;
71
73
  else
72
74
  options = { ...defaultOptions, ...options };
73
- const availableEndpoints = {
75
+ const clients = {
74
76
  http: [],
75
77
  ws: []
76
78
  };
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
79
  const endpoints = {
94
80
  http: [],
95
81
  ws: []
96
82
  };
97
83
  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';
84
+ if (options.mode === 'remote') {
85
+ if (options.http) {
86
+ for (const endpoint of options.http) {
87
+ if (endpoint.port && !endpoint.url)
88
+ endpoint.url = `http://localhost:${endpoint.port}`;
89
+ const client = await getHttp(endpoint.url, options.networkVersion);
90
+ if (client)
91
+ endpoints.http.push(endpoint.url) && clients.http.push({ url: endpoint.url, client });
92
+ }
93
+ }
94
+ if (options.ws) {
95
+ for (const endpoint of options.ws) {
96
+ if (endpoint.port && !endpoint.url)
97
+ endpoint.url = `ws://localhost:${endpoint.port}`;
98
+ const client = await getWS(endpoint.url, options.networkVersion);
99
+ if (client)
100
+ endpoints.ws.push(endpoint.url) && clients.ws.push({ url: endpoint.url, client });
101
+ }
102
+ }
103
+ if (endpoints.http.length === 0 && endpoints.ws.length === 0)
104
+ throw new Error(`no remotes connected`);
105
+ }
106
+ else if (options.mode === 'direct') {
107
+ await new Node({ network: options.network, stars: options.stars, networkVersion: options.networkVersion }, password);
108
+ await nodeConfig({ network: options.network, stars: options.stars, networkVersion: options.networkVersion });
109
+ chain = await new Chain();
110
+ if (options.ws) {
111
+ const importee = await import('@leofcoin/endpoints/ws');
112
+ const wsServer = importee.default;
113
+ for (const endpoint of options.ws) {
114
+ if (endpoint.port && !endpoint.url)
115
+ endpoint.url = `ws://localhost:${endpoint.port}`;
116
+ await wsServer(chain, endpoint.port, options.networkVersion);
117
+ endpoints.ws.push(endpoint.url);
118
+ const client = await getWS(endpoint.url, options.networkVersion);
119
+ if (client)
120
+ endpoints.ws.push(endpoint.url) && clients.ws.push({ url: endpoint.url, client });
121
+ }
122
+ }
123
+ if (options.http) {
124
+ const importee = await import('@leofcoin/endpoints/http');
125
+ const httpServer = importee.default;
126
+ for (const endpoint of options.http) {
127
+ if (endpoint.port && !endpoint.url)
128
+ endpoint.url = `http://localhost:${endpoint.port}`;
129
+ await httpServer(chain, endpoint.port, options.networkVersion);
130
+ endpoints.http.push(endpoint.url);
131
+ const client = await getHttp(endpoint.url, options.networkVersion);
132
+ if (client)
133
+ endpoints.http.push(endpoint.url) && clients.http.push({ url: endpoint.url, client });
134
+ }
135
+ }
107
136
  }
108
137
  else {
109
- if (options.forceRemote)
110
- throw new Error(`forceRemote was set but no remotes connected`);
111
138
  await new Node({ network: options.network, stars: options.stars, networkVersion: options.networkVersion }, password);
112
139
  await nodeConfig({ network: options.network, stars: options.stars, networkVersion: options.networkVersion });
113
140
  chain = await new Chain();
114
141
  if (options.ws) {
115
142
  const importee = await import('@leofcoin/endpoints/ws');
116
143
  const wsServer = importee.default;
117
- await wsServer(chain, options.ws[0].port, options.networkVersion);
118
- endpoints.ws.push(options.ws[0].url);
144
+ for (const endpoint of options.ws) {
145
+ if (endpoint.port && !endpoint.url)
146
+ endpoint.url = `ws://localhost:${endpoint.port}`;
147
+ await wsServer(chain, endpoint.port, options.networkVersion);
148
+ endpoints.ws.push(endpoint.url);
149
+ }
119
150
  }
120
151
  if (options.http) {
121
152
  const importee = await import('@leofcoin/endpoints/http');
122
153
  const httpServer = importee.default;
123
- await httpServer(chain, options.http[0].port, options.networkVersion);
124
- endpoints.http.push(options.http[0].url);
154
+ for (const endpoint of options.http) {
155
+ if (endpoint.port && !endpoint.url)
156
+ endpoint.url = `http://localhost:${endpoint.port}`;
157
+ await httpServer(chain, endpoint.port, options.networkVersion);
158
+ endpoints.http.push(endpoint.url);
159
+ }
125
160
  }
126
- mode = 'direct';
127
161
  }
128
162
  return {
129
163
  chain,
130
- mode,
131
- endpoints
164
+ mode: options.mode,
165
+ endpoints,
166
+ clients
132
167
  };
133
168
  };
134
169
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/launch-chain",
3
- "version": "0.2.5",
3
+ "version": "0.2.7",
4
4
  "description": "",
5
5
  "exports": {
6
6
  ".": "./index.js"
@@ -24,7 +24,8 @@
24
24
  "homepage": "https://github.com/leofcoin/launch-chain#readme",
25
25
  "dependencies": {
26
26
  "@leofcoin/chain": "^1.4.8",
27
- "@leofcoin/endpoints": "^0.2.0"
27
+ "@leofcoin/endpoint-clients": "^0.2.3",
28
+ "@leofcoin/endpoints": "^0.2.10"
28
29
  },
29
30
  "devDependencies": {
30
31
  "@rollup/plugin-typescript": "^11.0.0",
package/src/index.ts CHANGED
@@ -1,18 +1,26 @@
1
1
  import Node from '@leofcoin/chain/node'
2
2
  import Chain from '@leofcoin/chain/chain'
3
3
  import nodeConfig from '@leofcoin/lib/node-config'
4
+ import WSClient from '@leofcoin/endpoint-clients/ws'
5
+ import HttpClient from '@leofcoin/endpoint-clients/http'
4
6
 
5
- type launchMode = 'direct' | 'remote'
7
+ type launchMode = 'direct' | 'remote' | 'server'
6
8
 
7
9
  type endpointReturns = {
8
10
  http?: string[],
9
11
  ws?: string[],
10
12
  }
11
13
 
14
+ type clientReturns = {
15
+ http?: HttpClient[],
16
+ ws?: WSClient[],
17
+ }
18
+
12
19
  type launchReturn = {
13
20
  chain: Chain,
14
21
  mode: launchMode,
15
- endpoints: endpointReturns
22
+ endpoints: endpointReturns,
23
+ clients: clientReturns
16
24
  }
17
25
 
18
26
  type endpointOptions = {
@@ -25,15 +33,17 @@ type launchOptions = {
25
33
  networkVersion?: string,
26
34
  stars: string[],
27
35
  forceRemote: boolean,
36
+ mode?: launchMode,
28
37
  ws?: endpointOptions[] | undefined,
29
38
  http?: endpointOptions[] | undefined,
30
39
  }
31
40
 
32
- const defaultOptions:launchOptions = {
41
+ const defaultOptions: launchOptions = {
33
42
  network: 'leofcoin:peach',
34
43
  networkVersion: 'peach',
35
44
  stars: ['wss://peach.leofcoin.org'],
36
45
  forceRemote: false,
46
+ mode: 'direct',
37
47
  ws: [{
38
48
  port: 4040
39
49
  }],
@@ -48,21 +58,21 @@ const defaultOptions:launchOptions = {
48
58
  * @param {string} networkVersion network/testnet-network sepperate by -
49
59
  * @returns Promise(boolean)
50
60
  */
51
- const hasHttp = async (url: string, networkVersion: string) => {
61
+ const getHttp = async (url: string, networkVersion: string): Promise<undefined | HttpClient> => {
52
62
  try {
53
- await fetch(url + '/network')
54
- return true
63
+ const client = new HttpClient(url, networkVersion)
64
+ await client.network()
65
+ return client
55
66
  } catch (error) {
56
- return false
67
+ return undefined
57
68
  }
58
69
  }
59
70
 
60
- const tryWs = (url: string | URL, networkVersion: string | string[]): Promise<boolean> => new Promise(async (resolve, reject) => {
71
+ const tryWs = (url: string, networkVersion: string): Promise<WSClient> => new Promise(async (resolve, reject) => {
61
72
  try {
62
- const socket = await new WebSocket(url, networkVersion)
63
- socket.onerror = () => resolve(false)
64
- if (socket.readyState === 1) socket.close()
65
- resolve(true)
73
+ const socket = await new WSClient(url, networkVersion)
74
+ await socket.init()
75
+ resolve(socket)
66
76
  } catch (error) {
67
77
  reject(error)
68
78
  }
@@ -74,12 +84,12 @@ const tryWs = (url: string | URL, networkVersion: string | string[]): Promise<bo
74
84
  * @param {string} networkVersion network/testnet-network sepperate by -
75
85
  * @returns Promise(boolean)
76
86
  */
77
- const hasWs = async (url: string, networkVersion: string): Promise<boolean> => {
87
+ const getWS = async (url: string, networkVersion: string): Promise<WSClient> => {
78
88
  try {
79
- await tryWs(url, networkVersion)
80
- return true
89
+ const ws = await tryWs(url, networkVersion)
90
+ return ws
81
91
  } catch (error) {
82
- return false
92
+ return undefined
83
93
  }
84
94
  }
85
95
 
@@ -91,8 +101,8 @@ const hasWs = async (url: string, networkVersion: string): Promise<boolean> => {
91
101
  * @returns Promise({http: boolean, ws: boolean})
92
102
  */
93
103
  const hasClient = async (httpURL: string, wsURL: string, networkVersion: string) => {
94
- const ws = await hasWs(wsURL, networkVersion)
95
- const http = await hasHttp(httpURL, networkVersion)
104
+ const ws = await getWS(wsURL, networkVersion)
105
+ const http = await getHttp(httpURL, networkVersion)
96
106
  return {http, ws}
97
107
  }
98
108
 
@@ -109,46 +119,36 @@ const launch = async (options: launchOptions, password: string): Promise<launchR
109
119
  if (!options) options = defaultOptions
110
120
  else options = {...defaultOptions, ...options }
111
121
 
112
- const availableEndpoints: endpointReturns = {
122
+ const clients: clientReturns = {
113
123
  http: [],
114
124
  ws: []
115
125
  }
116
126
 
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
127
  const endpoints: endpointReturns = {
132
128
  http: [],
133
129
  ws: []
134
130
  }
135
131
 
136
132
  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
-
133
+
134
+ if (options.mode === 'remote') {
135
+ if (options.http) {
136
+ for (const endpoint of options.http) {
137
+ if (endpoint.port && !endpoint.url) endpoint.url = `http://localhost:${endpoint.port}`
138
+ const client = await getHttp(endpoint.url, options.networkVersion)
139
+ if (client) endpoints.http.push(endpoint.url) && clients.http.push({url: endpoint.url, client})
140
+ }
141
+ }
142
+
143
+ if (options.ws) {
144
+ for (const endpoint of options.ws) {
145
+ if (endpoint.port && !endpoint.url) endpoint.url = `ws://localhost:${endpoint.port}`
146
+ const client = await getWS(endpoint.url, options.networkVersion)
147
+ if (client) endpoints.ws.push(endpoint.url) && clients.ws.push({url: endpoint.url, client})
148
+ }
149
+ }
150
+ if (endpoints.http.length === 0 && endpoints.ws.length === 0) throw new Error(`no remotes connected`)
151
+ } else if (options.mode === 'direct') {
152
152
  await new Node({ network: options.network, stars: options.stars, networkVersion: options.networkVersion }, password)
153
153
  await nodeConfig({ network: options.network, stars: options.stars, networkVersion: options.networkVersion })
154
154
 
@@ -157,21 +157,66 @@ const launch = async (options: launchOptions, password: string): Promise<launchR
157
157
  if (options.ws) {
158
158
  const importee = await import('@leofcoin/endpoints/ws')
159
159
  const wsServer = importee.default
160
- await wsServer(chain, options.ws[0].port, options.networkVersion)
161
- endpoints.ws.push(options.ws[0].url)
160
+
161
+ for (const endpoint of options.ws) {
162
+ if (endpoint.port && !endpoint.url) endpoint.url = `ws://localhost:${endpoint.port}`
163
+
164
+ await wsServer(chain, endpoint.port, options.networkVersion)
165
+ endpoints.ws.push(endpoint.url)
166
+
167
+ const client = await getWS(endpoint.url, options.networkVersion)
168
+ if (client) endpoints.ws.push(endpoint.url) && clients.ws.push({url: endpoint.url, client})
169
+ }
162
170
  }
171
+
163
172
  if (options.http) {
164
173
  const importee = await import('@leofcoin/endpoints/http')
165
174
  const httpServer = importee.default
166
- await httpServer(chain, options.http[0].port, options.networkVersion)
167
- endpoints.http.push(options.http[0].url)
175
+
176
+ for (const endpoint of options.http) {
177
+ if (endpoint.port && !endpoint.url) endpoint.url = `http://localhost:${endpoint.port}`
178
+
179
+ await httpServer(chain, endpoint.port, options.networkVersion)
180
+ endpoints.http.push(endpoint.url)
181
+
182
+ const client = await getHttp(endpoint.url, options.networkVersion)
183
+ if (client) endpoints.http.push(endpoint.url) && clients.http.push({url: endpoint.url, client})
184
+ }
185
+ }
186
+ } else {
187
+ await new Node({ network: options.network, stars: options.stars, networkVersion: options.networkVersion }, password)
188
+ await nodeConfig({ network: options.network, stars: options.stars, networkVersion: options.networkVersion })
189
+
190
+ chain = await new Chain()
191
+
192
+ if (options.ws) {
193
+ const importee = await import('@leofcoin/endpoints/ws')
194
+ const wsServer = importee.default
195
+
196
+ for (const endpoint of options.ws) {
197
+ if (endpoint.port && !endpoint.url) endpoint.url = `ws://localhost:${endpoint.port}`
198
+ await wsServer(chain, endpoint.port, options.networkVersion)
199
+ endpoints.ws.push(endpoint.url)
200
+ }
201
+ }
202
+
203
+ if (options.http) {
204
+ const importee = await import('@leofcoin/endpoints/http')
205
+ const httpServer = importee.default
206
+
207
+ for (const endpoint of options.http) {
208
+ if (endpoint.port && !endpoint.url) endpoint.url = `http://localhost:${endpoint.port}`
209
+ await httpServer(chain, endpoint.port, options.networkVersion)
210
+ endpoints.http.push(endpoint.url)
211
+ }
168
212
  }
169
- mode = 'direct'
170
213
  }
214
+
171
215
  return {
172
216
  chain,
173
- mode,
174
- endpoints
217
+ mode: options.mode,
218
+ endpoints,
219
+ clients
175
220
  }
176
221
  }
177
222
 
package/test.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import launch from './index.js'
2
2
 
3
- console.log(await launch({
3
+ const {chain, endpoints, clients, mode } = await launch({
4
4
  network: 'leofcoin:peach',
5
- networkVersion: 'peach'
6
- }));
5
+ networkVersion: 'peach',
6
+ mode: 'direct'
7
+ })
8
+ console.log(await clients.ws[0].client.networkStats());
package/tsconfig.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "compilerOptions": {
3
- "target": "es2022",
4
- "module": "es2022",
3
+ "target": "ESNext",
4
+ "module": "ESNext",
5
5
  "outDir": "./",
6
- "moduleResolution":"NodeNext",
6
+ "moduleResolution":"nodenext",
7
7
  "allowJs": true,
8
8
  "declaration": true
9
9
  },