@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.
50 lines (44 loc) • 1.53 kB
JavaScript
;
const ping = require('pingman');
const _interopDefaultCompat = e => e && typeof e === 'object' && 'default' in e ? e.default : e;
const ping__default = /*#__PURE__*/_interopDefaultCompat(ping);
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
const DISPLAY_NAME = "Ping check for";
const pingCheck = /* @__PURE__ */ __name((host, options) => async () => {
try {
const response = await ping__default(host.replace(/^https?:\/\//, ""), options);
if (!response.alive) {
return {
displayName: `${DISPLAY_NAME} ${host}`,
health: {
healthy: false,
message: `Ping failed for ${host}.`,
timestamp: (/* @__PURE__ */ new Date()).toISOString()
},
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
meta: response
};
}
return {
displayName: `${DISPLAY_NAME} ${host}`,
health: {
healthy: true,
message: `${DISPLAY_NAME} ${host} was successful.`,
timestamp: (/* @__PURE__ */ new Date()).toISOString()
},
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
meta: response
};
} catch (error) {
return {
displayName: `${DISPLAY_NAME} ${host}`,
health: {
healthy: false,
message: error.message,
timestamp: (/* @__PURE__ */ new Date()).toISOString()
}
};
}
}, "pingCheck");
module.exports = pingCheck;