UNPKG

@ethersphere/bee-factory

Version:

Orchestration CLI for spinning up local development Bee cluster with Docker

119 lines (118 loc) 5.09 kB
"use strict"; 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.waitForWorkers = exports.waitForQueen = exports.waitForBlockchain = void 0; const node_fetch_1 = __importDefault(require("node-fetch")); const index_1 = require("./index"); const error_1 = require("./error"); const bee_js_1 = require("@ethersphere/bee-js"); const AWAIT_SLEEP = 3000; const BLOCKCHAIN_BODY_REQUEST = JSON.stringify({ jsonrpc: '2.0', method: 'eth_chainId', id: 1 }); const EXPECTED_CHAIN_ID = '0xfb4'; const ALLOWED_ERRORS = ['ECONNREFUSED', 'ECONNRESET', 'UND_ERR_SOCKET']; function isAllowedError(e) { //@ts-ignore: Node 18 native fetch returns error where the underlying error is wrapped and placed in e.cause if (e.cause) { //@ts-ignore: Node 18 native fetch returns error where the underlying error is wrapped and placed in e.cause e = e.cause; } if (e.code && ALLOWED_ERRORS.includes(e.code)) { return true; } // Errors from Bee-js does not have the `FetchError` structure (eq. `code` property) // so we assert message itself. if (e.message.includes('socket hang up')) { return true; } return ALLOWED_ERRORS.some(substring => e.message.includes(substring)); } function waitForBlockchain(waitingIterations = 30) { return __awaiter(this, void 0, void 0, function* () { for (let i = 0; i < waitingIterations; i++) { try { const request = yield (0, node_fetch_1.default)('http://127.0.0.1:9545', { method: 'POST', body: BLOCKCHAIN_BODY_REQUEST, headers: { 'Content-Type': 'application/json' }, }); const response = (yield request.json()); if (response.result === EXPECTED_CHAIN_ID) { return; } } catch (e) { if (!isAllowedError(e)) { throw e; } } yield (0, index_1.sleep)(AWAIT_SLEEP); } throw new error_1.TimeoutError('Waiting for blockchain container timed-out'); }); } exports.waitForBlockchain = waitForBlockchain; function waitForQueen(verifyQueenIsUp, waitingIterations = 120) { return __awaiter(this, void 0, void 0, function* () { const beeDebug = new bee_js_1.BeeDebug('http://127.0.0.1:1635'); for (let i = 0; i < waitingIterations; i++) { try { if (!(yield verifyQueenIsUp())) { throw new Error('Queen node is not running!'); } const addresses = yield beeDebug.getNodeAddresses(); if (addresses.underlay.length > 0) { const addr = addresses.underlay.find(addr => !addr.includes('127.0.0.1')); if (addr) { return addr; } } } catch (e) { if (!isAllowedError(e)) { throw e; } } yield (0, index_1.sleep)(AWAIT_SLEEP); } throw new error_1.TimeoutError('Waiting for queen container timed-out'); }); } exports.waitForQueen = waitForQueen; function waitForWorkers(workerCount, getStatus, waitingIterations = 120) { return __awaiter(this, void 0, void 0, function* () { const beeDebug = new bee_js_1.BeeDebug('http://127.0.0.1:1635'); const status = yield getStatus(); for (let i = 1; i <= workerCount; i++) { if (status[`worker${i}`] !== 'running') { throw new Error('Some of the workers node is not running!'); } } for (let i = 0; i < waitingIterations; i++) { try { const peers = yield beeDebug.getPeers(); if (peers.length >= workerCount) { return; } } catch (e) { if (!isAllowedError(e)) { throw e; } } yield (0, index_1.sleep)(AWAIT_SLEEP); } throw new error_1.TimeoutError('Waiting for worker nodes timed-out'); }); } exports.waitForWorkers = waitForWorkers;