UNPKG

everything-dev

Version:

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

80 lines (78 loc) 3.99 kB
import { fetchJsonOrNull } from "./http-client.mjs"; //#region src/fastkv.ts 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) { return fetchJsonOrNull(`${cdnUrl.replace(/\/$/, "")}/plugin.manifest.json`, { retries: 0 }); } async function fetchJson(url, init) { const headers = { accept: "application/json", "content-type": "application/json", ...init?.headers }; return fetchJsonOrNull(url, { method: init?.method, headers, body: init?.body ?? void 0, retries: 3 }); } //#endregion export { buildRegistryConfigUrl, buildRegistryConfigUrlForNetwork, fetchBosConfigFromFastKv, fetchRemotePluginManifest, getFastKvBaseUrlForNetwork, getRegistryNamespaceForAccount, getRegistryNamespaceForNetwork, parseBosUrl }; //# sourceMappingURL=fastkv.mjs.map