UNPKG

@ethersphere/bee-js

Version:
140 lines (139 loc) 6.57 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getVersions = exports.isSupportedApiVersion = exports.isSupportedExactVersion = exports.getNodeInfo = exports.getReadiness = exports.getHealth = exports.getDebugStatus = exports.SUPPORTED_API_VERSION = exports.SUPPORTED_BEE_VERSION = exports.SUPPORTED_BEE_VERSION_EXACT = void 0; const cafe_utility_1 = require("cafe-utility"); const major_js_1 = __importDefault(require("semver/functions/major.js")); const debug_1 = require("../../types/debug"); const http_1 = require("../../utils/http"); exports.SUPPORTED_BEE_VERSION_EXACT = '2.6.0-d0aa8b93'; exports.SUPPORTED_BEE_VERSION = exports.SUPPORTED_BEE_VERSION_EXACT.split('-')[0]; exports.SUPPORTED_API_VERSION = '7.3.0'; const NODE_INFO_URL = 'node'; const STATUS_URL = 'status'; const HEALTH_URL = 'health'; const READINESS_URL = 'readiness'; async function getDebugStatus(requestOptions) { const response = await (0, http_1.http)(requestOptions, { method: 'get', url: STATUS_URL, responseType: 'json', }); const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' }); return { overlay: cafe_utility_1.Types.asString(body.overlay, { name: 'overlay' }), proximity: cafe_utility_1.Types.asNumber(body.proximity, { name: 'proximity' }), beeMode: (0, debug_1.toBeeMode)(cafe_utility_1.Types.asString(body.beeMode, { name: 'beeMode' })), reserveSize: cafe_utility_1.Types.asNumber(body.reserveSize, { name: 'reserveSize' }), reserveSizeWithinRadius: cafe_utility_1.Types.asNumber(body.reserveSizeWithinRadius, { name: 'reserveSizeWithinRadius' }), pullsyncRate: cafe_utility_1.Types.asNumber(body.pullsyncRate, { name: 'pullsyncRate' }), storageRadius: cafe_utility_1.Types.asNumber(body.storageRadius, { name: 'storageRadius' }), connectedPeers: cafe_utility_1.Types.asNumber(body.connectedPeers, { name: 'connectedPeers' }), neighborhoodSize: cafe_utility_1.Types.asNumber(body.neighborhoodSize, { name: 'neighborhoodSize' }), batchCommitment: cafe_utility_1.Types.asNumber(body.batchCommitment, { name: 'batchCommitment' }), isReachable: cafe_utility_1.Types.asBoolean(body.isReachable, { name: 'isReachable' }), lastSyncedBlock: cafe_utility_1.Types.asNumber(body.lastSyncedBlock, { name: 'lastSyncedBlock' }), committedDepth: cafe_utility_1.Types.asNumber(body.committedDepth, { name: 'committedDepth' }), isWarmingUp: cafe_utility_1.Types.asBoolean(body.isWarmingUp, { name: 'isWarmingUp' }), }; } exports.getDebugStatus = getDebugStatus; /** * Get health of node * * @param requestOptions Options for making requests */ async function getHealth(requestOptions) { const response = await (0, http_1.http)(requestOptions, { method: 'get', url: HEALTH_URL, responseType: 'json', }); const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' }); return { apiVersion: cafe_utility_1.Types.asString(body.apiVersion, { name: 'apiVersion' }), version: cafe_utility_1.Types.asString(body.version, { name: 'version' }), status: cafe_utility_1.Types.asString(body.status, { name: 'status' }), }; } exports.getHealth = getHealth; /** * Get readiness of node * * @param requestOptions Options for making requests */ async function getReadiness(requestOptions) { const response = await (0, http_1.http)(requestOptions, { method: 'get', url: READINESS_URL, }); const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' }); return { apiVersion: cafe_utility_1.Types.asString(body.apiVersion, { name: 'apiVersion' }), version: cafe_utility_1.Types.asString(body.version, { name: 'version' }), status: cafe_utility_1.Types.asString(body.status, { name: 'status' }), }; } exports.getReadiness = getReadiness; /** * Get information about Bee node * * @param requestOptions Options for making requests */ async function getNodeInfo(requestOptions) { const response = await (0, http_1.http)(requestOptions, { method: 'get', url: NODE_INFO_URL, responseType: 'json', }); const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' }); return { beeMode: (0, debug_1.toBeeMode)(cafe_utility_1.Types.asString(body.beeMode, { name: 'beeMode' })), chequebookEnabled: cafe_utility_1.Types.asBoolean(body.chequebookEnabled, { name: 'chequebookEnabled' }), swapEnabled: cafe_utility_1.Types.asBoolean(body.swapEnabled, { name: 'swapEnabled' }), }; } exports.getNodeInfo = getNodeInfo; /** * Connects to a node and checks if its version matches with the one that bee-js supports. * * This is the most strict version check and most probably you will * want to use the relaxed API-versions check `isSupportedApiVersion`. * * @param requestOptions Options for making requests */ async function isSupportedExactVersion(requestOptions) { const { version } = await getHealth(requestOptions); return version === exports.SUPPORTED_BEE_VERSION_EXACT; } exports.isSupportedExactVersion = isSupportedExactVersion; /** * Connects to a node and checks if its Main API versions matches with the one that bee-js supports. * * This should be the main way how to check compatibility for your app and Bee node. * * @param requestOptions Options for making requests */ async function isSupportedApiVersion(requestOptions) { const { apiVersion } = await getHealth(requestOptions); return (0, major_js_1.default)(apiVersion) === (0, major_js_1.default)(exports.SUPPORTED_API_VERSION); } exports.isSupportedApiVersion = isSupportedApiVersion; /** * Returns object with all versions specified by the connected Bee node (properties prefixed with `bee*`) * and versions that bee-js supports (properties prefixed with `supported*`). * * @param requestOptions Options for making requests */ async function getVersions(requestOptions) { const { version, apiVersion } = await getHealth(requestOptions); return { supportedBeeVersion: exports.SUPPORTED_BEE_VERSION_EXACT, supportedBeeApiVersion: exports.SUPPORTED_API_VERSION, beeVersion: version, beeApiVersion: apiVersion, }; } exports.getVersions = getVersions;