From 755ae33f369fa2672493924de4726242d137ac6a Mon Sep 17 00:00:00 2001 From: Mic Neale Date: Fri, 10 Feb 2023 15:03:55 +1100 Subject: [PATCH 1/3] pick a server from a list --- package-lock.json | 2 +- src/did/did-loader.js | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index c0c7938..3ab569d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "web5-cli", + "name": "cli2", "lockfileVersion": 2, "requires": true, "packages": { diff --git a/src/did/did-loader.js b/src/did/did-loader.js index 16bdd19..a6a0ebf 100644 --- a/src/did/did-loader.js +++ b/src/did/did-loader.js @@ -10,7 +10,13 @@ if (fs.existsSync(config.did.storagePath)) { didState = JSON.parse(didStateJson); } else { if (config.did.method === 'ion') { - didState = await DIDIon.generate({ serviceEndpoint: 'https://dwn-aggregator.faktj7f1fndve.ap-southeast-2.cs.amazonlightsail.com/' }); + const endpoints = [ + 'https://dwn-usa-1.ue8cktdq71va0.us-east-2.cs.amazonlightsail.com/', + 'https://dwn-aggregator.faktj7f1fndve.ap-southeast-2.cs.amazonlightsail.com/', + 'https://dwn-india.vtv94qck5sjvq.ap-south-1.cs.amazonlightsail.com/' + ]; + const serviceEndpoint = endpoints[Math.floor(Math.random() * endpoints.length)]; + didState = await DIDIon.generate({ serviceEndpoint: serviceEndpoint }); fs.writeFileSync(config.did.storagePath, JSON.stringify(didState, null, 2)); } else { throw new Error(`DID Method ${config.did.method} not supported`); From 18924672b222ce9b9eda462f4b9cd15a9f846897 Mon Sep 17 00:00:00 2001 From: Mic Neale Date: Mon, 13 Feb 2023 19:15:09 +1100 Subject: [PATCH 2/3] choose the nearest available DWN --- src/did/did-loader.js | 52 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/src/did/did-loader.js b/src/did/did-loader.js index a6a0ebf..aa998e8 100644 --- a/src/did/did-loader.js +++ b/src/did/did-loader.js @@ -10,12 +10,58 @@ if (fs.existsSync(config.did.storagePath)) { didState = JSON.parse(didStateJson); } else { if (config.did.method === 'ion') { - const endpoints = [ + +const endpoints = [ 'https://dwn-usa-1.ue8cktdq71va0.us-east-2.cs.amazonlightsail.com/', 'https://dwn-aggregator.faktj7f1fndve.ap-southeast-2.cs.amazonlightsail.com/', 'https://dwn-india.vtv94qck5sjvq.ap-south-1.cs.amazonlightsail.com/' - ]; - const serviceEndpoint = endpoints[Math.floor(Math.random() * endpoints.length)]; + ]; + +const selectedEndpoint = async () => { + let fastestEndpoint = ''; + let fastestResponseTime = Number.POSITIVE_INFINITY; + let responseTimes = []; + + // Pick 3 random endpoints + let randomEndpoints = []; + while (randomEndpoints.length < 3) { + let randomIndex = Math.floor(Math.random() * endpoints.length); + if (!randomEndpoints.includes(endpoints[randomIndex])) { + randomEndpoints.push(endpoints[randomIndex]); + } + } + + console.log(randomEndpoints); + + // Check the response time of each endpoint and make sure it returns 200 OK + for (let endpoint of randomEndpoints) { + let start = performance.now(); + let response = await fetch(endpoint + "health"); + let end = performance.now(); + if (response.status === 200) { + responseTimes.push({ + endpoint, + responseTime: end - start + }); + } + } + + // Pick the endpoint with the fastest response time + for (let responseTime of responseTimes) { + if (responseTime.responseTime < fastestResponseTime) { + fastestResponseTime = responseTime.responseTime; + fastestEndpoint = responseTime.endpoint; + } + } + + return fastestEndpoint; +} + +const serviceEndpoint = await selectedEndpoint(); + + + + didState = await DIDIon.generate({ serviceEndpoint: serviceEndpoint }); fs.writeFileSync(config.did.storagePath, JSON.stringify(didState, null, 2)); } else { From 614692400065114cbdfb0999d9338f7c267171be Mon Sep 17 00:00:00 2001 From: Mic Neale Date: Mon, 13 Feb 2023 19:19:24 +1100 Subject: [PATCH 3/3] can show a list of DWNs --- src/did/did-loader.js | 85 +++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 47 deletions(-) diff --git a/src/did/did-loader.js b/src/did/did-loader.js index aa998e8..70710de 100644 --- a/src/did/did-loader.js +++ b/src/did/did-loader.js @@ -11,56 +11,47 @@ if (fs.existsSync(config.did.storagePath)) { } else { if (config.did.method === 'ion') { -const endpoints = [ - 'https://dwn-usa-1.ue8cktdq71va0.us-east-2.cs.amazonlightsail.com/', - 'https://dwn-aggregator.faktj7f1fndve.ap-southeast-2.cs.amazonlightsail.com/', - 'https://dwn-india.vtv94qck5sjvq.ap-south-1.cs.amazonlightsail.com/' - ]; - -const selectedEndpoint = async () => { - let fastestEndpoint = ''; - let fastestResponseTime = Number.POSITIVE_INFINITY; - let responseTimes = []; - - // Pick 3 random endpoints - let randomEndpoints = []; - while (randomEndpoints.length < 3) { - let randomIndex = Math.floor(Math.random() * endpoints.length); - if (!randomEndpoints.includes(endpoints[randomIndex])) { - randomEndpoints.push(endpoints[randomIndex]); - } - } - - console.log(randomEndpoints); - - // Check the response time of each endpoint and make sure it returns 200 OK - for (let endpoint of randomEndpoints) { - let start = performance.now(); - let response = await fetch(endpoint + "health"); - let end = performance.now(); - if (response.status === 200) { - responseTimes.push({ - endpoint, - responseTime: end - start - }); - } - } - - // Pick the endpoint with the fastest response time - for (let responseTime of responseTimes) { - if (responseTime.responseTime < fastestResponseTime) { - fastestResponseTime = responseTime.responseTime; - fastestEndpoint = responseTime.endpoint; - } - } - - return fastestEndpoint; -} - -const serviceEndpoint = await selectedEndpoint(); + const endpoints = [ + 'https://dwn-usa-1.ue8cktdq71va0.us-east-2.cs.amazonlightsail.com/', + 'https://dwn-aggregator.faktj7f1fndve.ap-southeast-2.cs.amazonlightsail.com/', + 'https://dwn-india.vtv94qck5sjvq.ap-south-1.cs.amazonlightsail.com/' + ]; + + const selectedEndpoints = async () => { + let responseTimes = []; + + // Pick 3 random endpoints + let randomEndpoints = []; + while (randomEndpoints.length < 3) { + let randomIndex = Math.floor(Math.random() * endpoints.length); + if (!randomEndpoints.includes(endpoints[randomIndex])) { + randomEndpoints.push(endpoints[randomIndex]); + } + } + + // Check the response time of each endpoint and make sure it returns 200 OK + for (let endpoint of randomEndpoints) { + let start = performance.now(); + let response = await fetch(endpoint + "health"); + let end = performance.now(); + if (response.status === 200) { + responseTimes.push({ + endpoint, + responseTime: end - start + }); + } + } + + // Sort the endpoints based on response time, fastest first + responseTimes.sort((a, b) => a.responseTime - b.responseTime); + + return responseTimes.map(rt => rt.endpoint); + } + const serviceEndpoints = await selectedEndpoints(); + const serviceEndpoint = serviceEndpoints[0]; didState = await DIDIon.generate({ serviceEndpoint: serviceEndpoint }); fs.writeFileSync(config.did.storagePath, JSON.stringify(didState, null, 2));