@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.
59 lines (56 loc) • 1.91 kB
JavaScript
import CacheableLookup from 'cacheable-lookup';
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(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");
export { dnsCheck as default };