From afbd1ce0e9923114cebae080647553865430bf89 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Tue, 28 Oct 2025 00:27:29 +0800 Subject: [PATCH] [Eliminate Blocking] Real Browser Monitor + Check Apprise (#5924) --- .../monitor-types/real-browser-monitor-type.js | 18 +++++++++--------- server/notification.js | 9 ++++----- server/server.js | 2 +- server/util-server.js | 16 ++++++++++++++++ 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/server/monitor-types/real-browser-monitor-type.js b/server/monitor-types/real-browser-monitor-type.js index 2a2871d2c..07b6c8aac 100644 --- a/server/monitor-types/real-browser-monitor-type.js +++ b/server/monitor-types/real-browser-monitor-type.js @@ -2,13 +2,13 @@ const { MonitorType } = require("./monitor-type"); const { chromium } = require("playwright-core"); const { UP, log } = require("../../src/util"); const { Settings } = require("../settings"); -const commandExistsSync = require("command-exists").sync; const childProcess = require("child_process"); const path = require("path"); const Database = require("../database"); const jwt = require("jsonwebtoken"); const config = require("../config"); const { RemoteBrowser } = require("../remote-browser"); +const { commandExists } = require("../util-server"); /** * Cached instance of a browser @@ -122,7 +122,7 @@ async function prepareChromeExecutable(executablePath) { executablePath = "/usr/bin/chromium"; // Install chromium in container via apt install - if ( !commandExistsSync(executablePath)) { + if (! await commandExists(executablePath)) { await new Promise((resolve, reject) => { log.info("Chromium", "Installing Chromium..."); let child = childProcess.exec("apt update && apt --yes --no-install-recommends install chromium fonts-indic fonts-noto fonts-noto-cjk"); @@ -146,7 +146,7 @@ async function prepareChromeExecutable(executablePath) { } } else { - executablePath = findChrome(allowedList); + executablePath = await findChrome(allowedList); } } else { // User specified a path @@ -160,20 +160,20 @@ async function prepareChromeExecutable(executablePath) { /** * Find the chrome executable - * @param {any[]} executables Executables to search through - * @returns {any} Executable - * @throws Could not find executable + * @param {string[]} executables Executables to search through + * @returns {Promise} Executable + * @throws {Error} Could not find executable */ -function findChrome(executables) { +async function findChrome(executables) { // Use the last working executable, so we don't have to search for it again if (lastAutoDetectChromeExecutable) { - if (commandExistsSync(lastAutoDetectChromeExecutable)) { + if (await commandExists(lastAutoDetectChromeExecutable)) { return lastAutoDetectChromeExecutable; } } for (let executable of executables) { - if (commandExistsSync(executable)) { + if (await commandExists(executable)) { lastAutoDetectChromeExecutable = executable; return executable; } diff --git a/server/notification.js b/server/notification.js index 8ad62dc13..31028e3dd 100644 --- a/server/notification.js +++ b/server/notification.js @@ -81,6 +81,7 @@ const Brevo = require("./notification-providers/brevo"); const YZJ = require("./notification-providers/yzj"); const SMSPlanet = require("./notification-providers/sms-planet"); const SpugPush = require("./notification-providers/spugpush"); +const { commandExists } = require("./util-server"); class Notification { providerList = {}; @@ -275,12 +276,10 @@ class Notification { /** * Check if apprise exists - * @returns {boolean} Does the command apprise exist? + * @returns {Promise} Does the command apprise exist? */ - static checkApprise() { - let commandExistsSync = require("command-exists").sync; - let exists = commandExistsSync("apprise"); - return exists; + static async checkApprise() { + return await commandExists("apprise"); } } diff --git a/server/server.js b/server/server.js index 55289b55a..6dba70a3e 100644 --- a/server/server.js +++ b/server/server.js @@ -1503,7 +1503,7 @@ let needSetup = false; socket.on("checkApprise", async (callback) => { try { checkLogin(socket); - callback(Notification.checkApprise()); + callback(await Notification.checkApprise()); } catch (e) { callback(false); } diff --git a/server/util-server.js b/server/util-server.js index ecad276b3..462b80578 100644 --- a/server/util-server.js +++ b/server/util-server.js @@ -1116,3 +1116,19 @@ function fsExists(path) { }); } module.exports.fsExists = fsExists; + +/** + * By default, command-exists will throw a null error if the command does not exist, which is ugly. The function makes it better. + * Read more: https://github.com/mathisonian/command-exists/issues/22 + * @param {string} command Command to check + * @returns {Promise} True if command exists, false otherwise + */ +async function commandExists(command) { + try { + await require("command-exists")(command); + return true; + } catch (e) { + return false; + } +} +module.exports.commandExists = commandExists;