@leofcoin/launch-chain 0.2.5 → 0.2.6

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
+ import WSClient from '@leofcoin/endpoint-clients/ws';
3
+ import HttpClient from '@leofcoin/endpoint-clients/http';
2
4
  type launchMode = 'direct' | 'remote';
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
@@ -74,20 +76,26 @@ const launch = async (options, password) => {
74
76
  http: [],
75
77
  ws: []
76
78
  };
79
+ const availableClients = {
80
+ http: [],
81
+ ws: []
82
+ };
77
83
  if (options.http) {
78
84
  for (const endpoint of options.http) {
79
85
  if (endpoint.port && !endpoint.url)
80
86
  endpoint.url = `http://localhost:${endpoint.port}`;
81
- if (await hasHttp(endpoint.url, options.networkVersion))
82
- availableEndpoints.http.push(endpoint.url);
87
+ const client = await getHttp(endpoint.url, options.networkVersion);
88
+ if (client)
89
+ availableEndpoints.http.push(endpoint.url) && availableClients.http.push({ url: endpoint.url, client });
83
90
  }
84
91
  }
85
92
  if (options.ws) {
86
93
  for (const endpoint of options.ws) {
87
94
  if (endpoint.port && !endpoint.url)
88
95
  endpoint.url = `ws://localhost:${endpoint.port}`;
89
- if (await hasWs(endpoint.url, options.networkVersion))
90
- availableEndpoints.ws.push(endpoint.url);
96
+ const client = await getWS(endpoint.url, options.networkVersion);
97
+ if (client)
98
+ availableEndpoints.ws.push(endpoint.url) && availableClients.ws.push({ url: endpoint.url, client });
91
99
  }
92
100
  }
93
101
  const endpoints = {
@@ -128,7 +136,8 @@ const launch = async (options, password) => {
128
136
  return {
129
137
  chain,
130
138
  mode,
131
- endpoints
139
+ endpoints,
140
+ clients: availableClients
132
141
  };
133
142
  };
134
143
 
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.6",
4
4
  "description": "",
5
5
  "exports": {
6
6
  ".": "./index.js"
@@ -24,6 +24,7 @@
24
24
  "homepage": "https://github.com/leofcoin/launch-chain#readme",
25
25
  "dependencies": {
26
26
  "@leofcoin/chain": "^1.4.8",
27
+ "@leofcoin/endpoint-clients": "^0.2.3",
27
28
  "@leofcoin/endpoints": "^0.2.0"
28
29
  },
29
30
  "devDependencies": {
package/src/index.ts CHANGED
@@ -1,6 +1,8 @@
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
  type launchMode = 'direct' | 'remote'
6
8
 
@@ -9,10 +11,16 @@ type endpointReturns = {
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,6 +33,7 @@ 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
  }
@@ -34,6 +43,7 @@ const defaultOptions:launchOptions = {
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
 
@@ -114,17 +124,24 @@ const launch = async (options: launchOptions, password: string): Promise<launchR
114
124
  ws: []
115
125
  }
116
126
 
127
+ const availableClients: clientReturns = {
128
+ http: [],
129
+ ws: []
130
+ }
131
+
117
132
  if (options.http) {
118
133
  for (const endpoint of options.http) {
119
134
  if (endpoint.port && !endpoint.url) endpoint.url = `http://localhost:${endpoint.port}`
120
- if (await hasHttp(endpoint.url, options.networkVersion)) availableEndpoints.http.push(endpoint.url)
135
+ const client = await getHttp(endpoint.url, options.networkVersion)
136
+ if (client) availableEndpoints.http.push(endpoint.url) && availableClients.http.push({url: endpoint.url, client})
121
137
  }
122
138
  }
123
139
 
124
140
  if (options.ws) {
125
141
  for (const endpoint of options.ws) {
126
142
  if (endpoint.port && !endpoint.url) endpoint.url = `ws://localhost:${endpoint.port}`
127
- if (await hasWs(endpoint.url, options.networkVersion)) availableEndpoints.ws.push(endpoint.url)
143
+ const client = await getWS(endpoint.url, options.networkVersion)
144
+ if (client) availableEndpoints.ws.push(endpoint.url) && availableClients.ws.push({url: endpoint.url, client})
128
145
  }
129
146
  }
130
147
 
@@ -171,7 +188,8 @@ const launch = async (options: launchOptions, password: string): Promise<launchR
171
188
  return {
172
189
  chain,
173
190
  mode,
174
- endpoints
191
+ endpoints,
192
+ clients: availableClients
175
193
  }
176
194
  }
177
195
 
package/test.js CHANGED
@@ -1,6 +1,10 @@
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
+ forceRemote: true,
7
+ ws: [{ url:'wss://ws-remote.leofcoin.org'}],
8
+ http: [{ url: 'https://remote.leofcoin.org' }]
9
+ })
10
+ 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
  },