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

Code before:
                previousBeat = await R.findOne("heartbeat", " monitor_id = ? ORDER BY time DESC", [
                    this.id
                ])
            }

            let bean = R.dispense("heartbeat")
            bean.monitor_id = this.id;
            bean.time = R.isoDateTime(dayjs.utc());
            bean.status = 0;

            // Duration
            if (previousBeat) {
                bean.duration = dayjs(bean.time).diff(dayjs(previousBeat.time), 'second');
            } else {
                bean.duration = 0;
            }

            try {
                if (this.type === "http" || this.type === "keyword") {
                    let startTime = dayjs().valueOf();
                    let res = await axios.get(this.url, {
                        headers: { 'User-Agent':'Uptime-Kuma' }
                    })
                    bean.msg = `${res.status} - ${res.statusText}`
                    bean.ping = dayjs().valueOf() - startTime;
                    if(this.url.startsWith("https")) {
                        Monitor.sendCertInfo(checkCertificate(res), io, this.id, this.user_id);
                    }

                    if (this.type === "http") {
                        bean.status = 1;
                    } else {

                        let data = res.data;

                        // Convert to string for object/array
                        if (typeof data !== "string") {
                            data = JSON.stringify(data)
                        }

                        if (data.includes(this.keyword)) {
                            bean.msg += ", keyword is found"
                            bean.status = 1;
                        } else {
                            throw new Error(bean.msg + ", but keyword is not found")
                        }

                    }


                } else if (this.type === "port") {

Review comment (suggestion):
```suggestion
                    if (this.url.startsWith("https")) {
```

Code after:
        };
    }

    start(io) {
        let previousBeat = null;
        let retries = 0;

        const beat = async () => {

            if (! previousBeat) {
                previousBeat = await R.findOne("heartbeat", " monitor_id = ? ORDER BY time DESC", [
                    this.id
                ])
            }

            const isFirstBeat = !previousBeat;

            let bean = R.dispense("heartbeat")
            bean.monitor_id = this.id;
            bean.time = R.isoDateTime(dayjs.utc());
            bean.status = DOWN;

            // Duration
            if (! isFirstBeat) {
                bean.duration = dayjs(bean.time).diff(dayjs(previousBeat.time), 'second');
            } else {
                bean.duration = 0;
            }

            try {
                if (this.type === "http" || this.type === "keyword") {
                    let startTime = dayjs().valueOf();
                    let res = await axios.get(this.url, {
                        headers: { "User-Agent": "Uptime-Kuma" },
                        httpsAgent: customAgent,
                    });
                    bean.msg = `${res.status} - ${res.statusText}`
                    bean.ping = dayjs().valueOf() - startTime;

                    // Check certificate if https is used

                    let certInfoStartTime = dayjs().valueOf();
                    if (this.getUrl()?.protocol === "https:") {
                        try {
                            await this.updateTlsInfo(checkCertificate(res));
                        } catch (e) {
                            console.error(e.message)
                        }
                    }

                    debug("Cert Info Query Time: " + (dayjs().valueOf() - certInfoStartTime) + "ms")