Repository: louislam/uptime-kuma
File: server/model/status_page.js (JavaScript)

Code before:
const { UptimeKumaServer } = require("../uptime-kuma-server");
const jsesc = require("jsesc");
const googleAnalytics = require("../google-analytics");
const { marked } = require("marked");
const { Feed } = require("feed");
const config = require("../config");

class StatusPage extends BeanModel {

    /**
     * Like this: { "test-uptime.kuma.pet": "default" }
     * @type {{}}
     */
    static domainMappingList = { };

    /**
     * Handle responses to RSS pages
     * @param {Response} response Response object
     * @param {string} slug Status page slug
     * @returns {Promise<void>}
     */
    static async handleStatusPageRSSResponse(response, slug) {
        let statusPage = await R.findOne("status_page", " slug = ? ", [
            slug
        ]);
        response.send(await StatusPage.renderRSS(statusPage, slug));
    }

    /**
     * Handle responses to status page
     * @param {Response} response Response object
     * @param {string} indexHTML HTML to render
     * @param {string} slug Status page slug
     * @returns {Promise<void>}
     */
    static async handleStatusPageResponse(response, indexHTML, slug) {
        // Handle url with trailing slash (http://localhost:3001/status/)
        // The slug comes from the route "/status/:slug". If the slug is empty, express converts it to "index.html"
        if (slug === "index.html") {
            slug = "default";
        }

        let statusPage = await R.findOne("status_page", " slug = ? ", [
            slug
        ]);

        if (statusPage) {
            response.send(await StatusPage.renderHTML(indexHTML, statusPage));
        } else {
            response.status(404).send(UptimeKumaServer.getInstance().indexHTML);
        }

Review comment (bug):
this function can likely Fail in some way.
Please cover this with a try-catch and send an error if one is catched.

Code after:
const { UptimeKumaServer } = require("../uptime-kuma-server");
const jsesc = require("jsesc");
const googleAnalytics = require("../google-analytics");
const { marked } = require("marked");
const { Feed } = require("feed");
const config = require("../config");

const { STATUS_PAGE_ALL_DOWN, STATUS_PAGE_ALL_UP, STATUS_PAGE_MAINTENANCE, STATUS_PAGE_PARTIAL_DOWN, UP, MAINTENANCE, DOWN } = require("../../src/util");

class StatusPage extends BeanModel {

    /**
     * Like this: { "test-uptime.kuma.pet": "default" }
     * @type {{}}
     */
    static domainMappingList = { };

    /**
     * Handle responses to RSS pages
     * @param {Response} response Response object
     * @param {string} slug Status page slug
     * @returns {Promise<void>}
     */
    static async handleStatusPageRSSResponse(response, slug) {
        let statusPage = await R.findOne("status_page", " slug = ? ", [
            slug
        ]);

        if (statusPage) {
            response.send(await StatusPage.renderRSS(statusPage, slug));
        } else {
            response.status(404).send(UptimeKumaServer.getInstance().indexHTML);
        }
    }

    /**
     * Handle responses to status page
     * @param {Response} response Response object
     * @param {string} indexHTML HTML to render
     * @param {string} slug Status page slug
     * @returns {Promise<void>}
     */
    static async handleStatusPageResponse(response, indexHTML, slug) {
        // Handle url with trailing slash (http://localhost:3001/status/)
        // The slug comes from the route "/status/:slug". If the slug is empty, express converts it to "index.html"
        if (slug === "index.html") {
            slug = "default";
        }

        let statusPage = await R.findOne("status_page", " slug = ? ", [
            slug