@visulima/health-check
Version:
A library built to provide support for defining service health for node services. It allows you to register async health checks for your dependencies and the service itself, provides a health endpoint that exposes their status, and health metrics.
65 lines (59 loc) • 2.12 kB
JavaScript
;
const CacheableLookup = require('cacheable-lookup');
const _interopDefaultCompat = e => e && typeof e === 'object' && 'default' in e ? e.default : e;
const CacheableLookup__default = /*#__PURE__*/_interopDefaultCompat(CacheableLookup);
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
const DISPLAY_NAME = "DNS check for";
const dnsCheck = /* @__PURE__ */ __name((host, expectedAddresses, options) => async () => {
const { family = "all", hints, ...config } = options ?? {};
const cacheable = new CacheableLookup__default(config);
try {
const meta = await cacheable.lookupAsync(host.replace(/^https?:\/\//, ""), {
hints,
...family === "all" ? { all: true } : { family }
});
if (Array.isArray(expectedAddresses) && !expectedAddresses.includes(meta.address)) {
return {
displayName: `${DISPLAY_NAME} ${host}`,
health: {
healthy: false,
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
message: `${DISPLAY_NAME} ${host} returned address ${meta.address} instead of ${expectedAddresses.join(", ")}.`,
timestamp: (/* @__PURE__ */ new Date()).toISOString()
},
meta: {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
addresses: meta,
host
}
};
}
return {
displayName: `${DISPLAY_NAME} ${host}`,
health: {
healthy: true,
message: `${DISPLAY_NAME} ${host} were resolved.`,
timestamp: (/* @__PURE__ */ new Date()).toISOString()
},
meta: {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
addresses: meta,
host
}
};
} catch (error) {
return {
displayName: `${DISPLAY_NAME} ${host}`,
health: {
healthy: false,
message: error.message,
timestamp: (/* @__PURE__ */ new Date()).toISOString()
},
meta: {
host
}
};
}
}, "dnsCheck");
module.exports = dnsCheck;