var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { utils as didUtils } from '@web5/dids'; /** * Dynamically selects up to 2 DWN endpoints that are provided * by default during the Tech Preview period. * * @beta */ export function getTechPreviewDwnEndpoints() { return __awaiter(this, void 0, void 0, function* () { let response; try { response = yield fetch('https://dwn.tbddev.org/.well-known/did.json'); if (!response.ok) { throw new Error(`HTTP Error: ${response.status} ${response.statusText}`); } } catch (error) { console.warn('failed to get tech preview dwn endpoints:', error.message); return []; } const didDocument = yield response.json(); const [dwnService] = didUtils.getServices({ didDocument, id: '#dwn', type: 'DecentralizedWebNode' }); // allocate up to 2 nodes for a user. const techPreviewEndpoints = new Set(); if ('serviceEndpoint' in dwnService && !Array.isArray(dwnService.serviceEndpoint) && typeof dwnService.serviceEndpoint !== 'string' && Array.isArray(dwnService.serviceEndpoint.nodes)) { const dwnUrls = dwnService.serviceEndpoint.nodes; const numNodesToAllocate = Math.min(dwnUrls.length, 2); for (let attempts = 0; attempts < dwnUrls.length && techPreviewEndpoints.size < numNodesToAllocate; attempts += 1) { const nodeIdx = getRandomInt(0, dwnUrls.length); const dwnUrl = dwnUrls[nodeIdx]; try { const healthCheck = yield fetch(`${dwnUrl}/health`); if (healthCheck.ok) { techPreviewEndpoints.add(dwnUrl); } } catch (error) { // Ignore healthcheck failures and try the next node. } } } return Array.from(techPreviewEndpoints); }); } function getRandomInt(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min)) + min; } //# sourceMappingURL=tech-preview.js.map