Skip to content

Commit

Permalink
Incorporate suggested PR changes
Browse files Browse the repository at this point in the history
Co-authored-by: Frank Hinek <frankhinek@users.noreply.github.com>
Co-authored-by: Moe Jangda <moe@tbd.email>
frankhinek and mistermoe committed May 28, 2023
1 parent 33adc1a commit c1dce93
Showing 2 changed files with 37 additions and 19 deletions.
21 changes: 12 additions & 9 deletions packages/web5/src/web5.ts
Original file line number Diff line number Diff line change
@@ -116,34 +116,37 @@ export class Web5 {
* by default during the Tech Preview period.
*/
static async getTechPreviewDwnEndpoints(): Promise<string[]> {
const response = await fetch('https://dwn.tbddev.org/.well-known/did.json');

// Return an empty array if dwn.tbddev.org is not responding.
if (!response.ok) { return []; }
let response: Response;
try {
response = await fetch('https://dwn.tbddev.org/.well-known/did.json');
if (!response.ok) {
throw new Error(`HTTP Error: ${response.status} ${response.statusText}`);
}
} catch(e) {
console.warn('failed to get tech preview dwn endpoints:', e.message);
return [];
}

const didDoc = await response.json();
const [ service ] = didUtils.getServices(didDoc, { id: '#dwn', type: 'DecentralizedWebNode' });
const { nodes } = <DwnServiceEndpoint>service.serviceEndpoint;

// allocate up to 2 nodes for a user.
const dwnUrls = new Set<string>();
let attempts = 0;
const numNodesToAllocate = Math.min(nodes.length, 2);

while(dwnUrls.size < numNodesToAllocate && attempts < nodes.length) {
for (let attempts = 0; attempts < nodes.length && dwnUrls.size < numNodesToAllocate; attempts += 1) {
const nodeIdx = getRandomInt(0, nodes.length);
const dwnUrl = nodes[nodeIdx];

try {
const healthCheck = await fetch(`${dwnUrl}/health`);
if (healthCheck.status === 200) {
if (healthCheck.ok) {
dwnUrls.add(dwnUrl);
}
} catch(e: unknown) {
// Ignore healthcheck failures and try the next node.
}

attempts++;
}

return Array.from(dwnUrls);
35 changes: 25 additions & 10 deletions packages/web5/tests/tech-preview.spec.ts
Original file line number Diff line number Diff line change
@@ -15,9 +15,10 @@ describe('Tech Preview', () => {
let mockDwnEndpoints: Array<string>;

let tbdWellKnownOkResponse = {
status : 200,
ok : true,
json : async () => Promise.resolve({
status : 200,
statusText : 'OK',
ok : true,
json : async () => Promise.resolve({
id : 'did:web:dwn.tbddev.org',
service : [
{
@@ -32,19 +33,22 @@ describe('Tech Preview', () => {
};

let tbdWellKnownBadResponse = {
status : 400,
ok : false
status : 400,
statusText : 'Bad Request',
ok : false
};

let dwnServerHealthOkResponse = {
status : 200,
ok : true,
json : async () => Promise.resolve({ok: true})
status : 200,
statusText : 'OK',
ok : true,
json : async () => Promise.resolve({ok: true})
};

let dwnServerHealthBadResponse = {
status : 400,
ok : false
status : 400,
statusText : 'Bad Request',
ok : false
};

beforeEach(() => {
@@ -146,5 +150,16 @@ describe('Tech Preview', () => {

expect(dwnEndpoints).to.be.an('array').that.has.lengthOf(0);
});

it('returns 0 DWN endpoints if fetching dwn.tbddev.org throws an exception', async function() {
// Stub fetch to simulate fetching dwn.tbddev.org throwing an exception.
fetchStub.restore();
fetchStub = sinon.stub(globalThis as any, 'fetch');
fetchStub.withArgs('https://dwn.tbddev.org/.well-known/did.json').rejects(new Error('Network error'));

const dwnEndpoints = await Web5.getTechPreviewDwnEndpoints();

expect(dwnEndpoints).to.be.an('array').that.has.lengthOf(0);
});
});
});

0 comments on commit c1dce93

Please sign in to comment.