UNPKG

everything-dev

Version:

A consolidated product package for building Module Federation apps with oRPC APIs.

103 lines (101 loc) 4.78 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); //#region src/fastkv.ts const FASTKV_TIMEOUT_MS = 1e4; function getNetworkIdForAccount(accountId) { return accountId.endsWith(".testnet") ? "testnet" : "mainnet"; } function getFastKvBaseUrlForNetwork(network) { return network === "testnet" ? process.env.REGISTRY_FASTKV_TESTNET_URL || "https://kv.test.fastnear.com" : process.env.REGISTRY_FASTKV_MAINNET_URL || "https://kv.main.fastnear.com"; } function getFastKvBaseUrlForAccount(accountId) { return getNetworkIdForAccount(accountId) === "testnet" ? getFastKvBaseUrlForNetwork("testnet") : getFastKvBaseUrlForNetwork("mainnet"); } function buildRegistryConfigUrl(accountId, gatewayId) { const baseUrl = getFastKvBaseUrlForAccount(accountId); const namespace = getRegistryNamespaceForAccount(accountId); const key = encodeURIComponent(getRegistryConfigKey(accountId, gatewayId)); return `${baseUrl}/v0/latest/${encodeURIComponent(namespace)}/${encodeURIComponent(accountId)}/${key}`; } function buildRegistryConfigUrlForNetwork(network, accountId, gatewayId) { const baseUrl = getFastKvBaseUrlForNetwork(network); const namespace = getRegistryNamespaceForNetwork(network); const key = encodeURIComponent(getRegistryConfigKey(accountId, gatewayId)); return `${baseUrl}/v0/latest/${encodeURIComponent(namespace)}/${encodeURIComponent(accountId)}/${key}`; } function getRegistryNamespaceForAccount(accountId) { return accountId.endsWith(".testnet") ? process.env.REGISTRY_FASTKV_TESTNET_NAMESPACE || "dev.everything.near" : process.env.REGISTRY_FASTKV_MAINNET_NAMESPACE || "dev.everything.near"; } function getRegistryNamespaceForNetwork(network) { return network === "testnet" ? process.env.REGISTRY_FASTKV_TESTNET_NAMESPACE || "dev.everything.near" : process.env.REGISTRY_FASTKV_MAINNET_NAMESPACE || "dev.everything.near"; } function getRegistryConfigKey(accountId, gatewayId, pathSegments = []) { return `apps/${accountId}/${gatewayId}${pathSegments.length > 0 ? `/${pathSegments.map((segment) => encodeURIComponent(segment)).join("/")}` : ""}/bos.config.json`; } function parseBosUrl(bosUrl) { const match = bosUrl.match(/^bos:\/\/([^/]+)\/(.+)$/); if (!match?.[1] || !match[2]) throw new Error(`Invalid BOS URL: ${bosUrl}`); const pathSegments = match[2].split("/").filter(Boolean).map((segment) => decodeURIComponent(segment)); if (pathSegments.length === 0) throw new Error(`Invalid BOS URL: ${bosUrl}`); const [gatewayId, ...pathSegmentsTail] = pathSegments; if (!gatewayId) throw new Error(`Invalid BOS URL: ${bosUrl}`); return { accountId: match[1], gatewayId, pathSegments: pathSegmentsTail }; } async function fetchBosConfigFromFastKv(bosUrl) { const { accountId, gatewayId, pathSegments } = parseBosUrl(bosUrl); const value = (await fetchJson(`${getFastKvBaseUrlForAccount(accountId)}/v0/latest/${encodeURIComponent(getRegistryNamespaceForAccount(accountId))}/${encodeURIComponent(accountId)}`, { method: "POST", body: JSON.stringify({ key: getRegistryConfigKey(accountId, gatewayId, pathSegments), limit: 1 }) }))?.entries?.find(Boolean)?.value; if (!value) throw new Error(`No config found for ${bosUrl}`); if (typeof value === "string") return JSON.parse(value); if (typeof value !== "object") throw new Error(`Invalid config value for ${bosUrl}`); return value; } async function fetchRemotePluginManifest(cdnUrl) { try { const controller = new AbortController(); const timeout = setTimeout(() => controller.abort(), FASTKV_TIMEOUT_MS); const baseUrl = cdnUrl.replace(/\/$/, ""); const response = await fetch(`${baseUrl}/plugin.manifest.json`, { signal: controller.signal }); clearTimeout(timeout); if (!response.ok) return null; return await response.json(); } catch { return null; } } async function fetchJson(url, init) { const controller = new AbortController(); const timeout = setTimeout(() => controller.abort(), FASTKV_TIMEOUT_MS); try { const response = await fetch(url, { ...init, headers: { accept: "application/json", "content-type": "application/json", ...init?.headers ?? {} }, signal: controller.signal }); if (!response.ok) return null; return await response.json(); } finally { clearTimeout(timeout); } } //#endregion exports.buildRegistryConfigUrl = buildRegistryConfigUrl; exports.buildRegistryConfigUrlForNetwork = buildRegistryConfigUrlForNetwork; exports.fetchBosConfigFromFastKv = fetchBosConfigFromFastKv; exports.fetchRemotePluginManifest = fetchRemotePluginManifest; exports.getFastKvBaseUrlForNetwork = getFastKvBaseUrlForNetwork; exports.getRegistryNamespaceForAccount = getRegistryNamespaceForAccount; exports.getRegistryNamespaceForNetwork = getRegistryNamespaceForNetwork; //# sourceMappingURL=fastkv.cjs.map