Repository URL to install this package:
Version:
0.6.0 ▾
|
"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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchBeeIdentity = exports.getHashedIdentity = exports.HASHED_IDENTITY_HEADER = void 0;
const crypto_1 = require("crypto");
const logger_1 = require("./logger");
let interval;
let identity = 'unknown';
exports.HASHED_IDENTITY_HEADER = 'X-Bee-Node';
/**
* Identity is not available during Bee's booting,
* so we retry the call until we receive proper identity and then stop the retries.
*/
function getHashedIdentity() {
return identity;
}
exports.getHashedIdentity = getHashedIdentity;
function fetchBeeIdentity(beeDebug, frequencyMs = 15000) {
return __awaiter(this, void 0, void 0, function* () {
logger_1.logger.info(`fetching bee identity with frequency ${frequencyMs}ms`);
if (!(yield attemptFetchingBeeIdentity(beeDebug))) {
interval = setInterval(() => __awaiter(this, void 0, void 0, function* () { return attemptFetchingBeeIdentity(beeDebug); }), frequencyMs);
}
});
}
exports.fetchBeeIdentity = fetchBeeIdentity;
function attemptFetchingBeeIdentity(beeDebug) {
return __awaiter(this, void 0, void 0, function* () {
try {
const { overlay } = yield beeDebug.getNodeAddresses();
identity = mapAddress(overlay);
logger_1.logger.info('bee debug overlay', { overlay, identity });
clearInterval(interval);
return true;
}
catch (e) {
logger_1.logger.error('failed to fetch identity', e);
}
});
}
function mapAddress(overlay) {
const hash = (0, crypto_1.createHash)('sha1');
hash.update(overlay);
return hash.digest('hex');
}