uptime-kuma/server/model/heartbeat.js
Dmitry 751fe1bbf5
feat: Add configurable response data storage for notifications (#6684)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Frank Elsinga <frank@elsinga.de>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2026-01-12 18:39:12 +01:00

85 lines
2.3 KiB
JavaScript

const { BeanModel } = require("redbean-node/dist/bean-model");
const zlib = require("node:zlib");
const { promisify } = require("node:util");
const gunzip = promisify(zlib.gunzip);
/**
* status:
* 0 = DOWN
* 1 = UP
* 2 = PENDING
* 3 = MAINTENANCE
*/
class Heartbeat extends BeanModel {
/**
* Return an object that ready to parse to JSON for public
* Only show necessary data to public
* @returns {object} Object ready to parse
*/
toPublicJSON() {
return {
status: this.status,
time: this.time,
msg: "", // Hide for public
ping: this.ping,
};
}
/**
* Return an object that ready to parse to JSON
* @returns {object} Object ready to parse
*/
toJSON() {
return {
monitorID: this._monitorId,
status: this._status,
time: this._time,
msg: this._msg,
ping: this._ping,
important: this._important,
duration: this._duration,
retries: this._retries,
response: this._response,
};
}
/**
* Return an object that ready to parse to JSON
* @param {{ decodeResponse?: boolean }} opts Options for JSON serialization
* @returns {Promise<object>} Object ready to parse
*/
async toJSONAsync(opts) {
return {
monitorID: this._monitorId,
status: this._status,
time: this._time,
msg: this._msg,
ping: this._ping,
important: this._important,
duration: this._duration,
retries: this._retries,
response: opts?.decodeResponse ? await Heartbeat.decodeResponseValue(this._response) : undefined,
};
}
/**
* Decode compressed response payload stored in database.
* @param {string|null} response Encoded response payload.
* @returns {string|null} Decoded response payload.
*/
static async decodeResponseValue(response) {
if (!response) {
return response;
}
try {
// Offload gzip decode from main event loop to libuv thread pool
return (await gunzip(Buffer.from(response, "base64"))).toString("utf8");
} catch (error) {
return response;
}
}
}
module.exports = Heartbeat;