@actyx/sdk
Version:
Actyx SDK
99 lines • 3.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.invalidNodeInfo = exports.NodeInfo = exports.getInfo = void 0;
const cross_fetch_1 = require("cross-fetch");
const internal_common_1 = require("./internal_common");
const SV = require("semver");
const E = require("fp-ts/lib/Either");
const log_1 = require("./internal_common/log");
const function_1 = require("fp-ts/lib/function");
const utils_1 = require("./v2/utils");
let lastInfoTime = 0;
let lastInfo = null;
const getInfo = (config) => {
const uri = `http://${(0, utils_1.getApiLocation)(config.actyxHost, config.actyxPort)}/node/info`;
return async (token, maxAgeMillis) => {
if (Date.now() - maxAgeMillis <= lastInfoTime) {
return lastInfo || exports.invalidNodeInfo;
}
const resp = await (0, cross_fetch_1.default)(uri, { method: 'get', headers: (0, utils_1.mkHeaders)(token) });
if (resp.status === 404) {
log_1.default.actyx.warn('The targeted node seems not to support the `/api/v2/node/info` endpoint. Consider updating to the latest version.');
return exports.invalidNodeInfo;
}
const json = await resp.json();
return (0, function_1.pipe)(internal_common_1.NodeInfo.decode(json), E.map((io) => new NodeInfo(io)), E.fold((errors) => {
log_1.default.actyx.error('errors while parsing NodeInfo response:', ...errors);
return exports.invalidNodeInfo;
}, (x) => {
lastInfoTime = Date.now();
lastInfo = x;
return x;
}));
};
};
exports.getInfo = getInfo;
/**
* Information about the Actyx node this SDK is connected to.
*
* Instances are returned by `Pond.nodeInfo()`.
* @public
*/
class NodeInfo {
constructor(io) {
this.io = io;
this.semver = SV.coerce(this.io.version) || new SV.SemVer('0.0.0');
}
/**
*
* @returns the full version string including git hash and CPU architecture
*/
longVersion() {
return this.io.version;
}
/**
*
* @returns the semantic version part of the full version string, e.g. `2.6.1`
*/
semVer() {
return this.semver.format();
}
/**
*
* @param version - The version to compare with, in semantic version format `<major>.<minor>.<patch>`
* @returns `true` if the reported Actyx version is greater than or equal to the supplied version
*/
isAtLeastVersion(version) {
return SV.gte(this.semver, version);
}
/**
*
* @returns the uptime of the Actyx node in milliseconds
*/
uptimeMillis() {
return this.io.uptime.secs * 1000 + Math.floor(this.io.uptime.nanos / 1000000);
}
/**
*
* @returns the number of other nodes the Actyx node is currently connected to
*/
connectedNodes() {
return this.io.connectedNodes;
}
/**
*
* @returns information about the perceived connectivity of other nodes
*/
peersStatus() {
var _a;
return (_a = this.io.swarmState) === null || _a === void 0 ? void 0 : _a.peersStatus;
}
}
exports.NodeInfo = NodeInfo;
exports.invalidNodeInfo = new NodeInfo({
connectedNodes: -1,
uptime: { secs: 0, nanos: 0 },
version: '1.0.0-unknown',
swarmState: undefined,
});
//# sourceMappingURL=node-info.js.map