From 288cab6dd775de94eb21db1579b8264afd47ea45 Mon Sep 17 00:00:00 2001 From: Nelson Chan <3271800+chakflying@users.noreply.github.com> Date: Thu, 25 Jan 2024 07:59:42 +0800 Subject: [PATCH] Fix: Make sure browser is connected before returning (#4417) --- .../monitor-types/real-browser-monitor-type.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/server/monitor-types/real-browser-monitor-type.js b/server/monitor-types/real-browser-monitor-type.js index 5e5a56a0..ae814fa2 100644 --- a/server/monitor-types/real-browser-monitor-type.js +++ b/server/monitor-types/real-browser-monitor-type.js @@ -9,6 +9,10 @@ const Database = require("../database"); const jwt = require("jsonwebtoken"); const config = require("../config"); +/** + * Cached instance of a browser + * @type {import ("playwright-core").Browser} + */ let browser = null; let allowedList = []; @@ -62,8 +66,15 @@ async function isAllowedChromeExecutable(executablePath) { return allowedList.includes(executablePath); } +/** + * Get the current instance of the browser. If there isn't one, create + * it. + * @returns {Promise} The browser + */ async function getBrowser() { - if (!browser) { + if (browser && browser.isConnected()) { + return browser; + } else { let executablePath = await Settings.get("chromeExecutable"); executablePath = await prepareChromeExecutable(executablePath); @@ -72,8 +83,9 @@ async function getBrowser() { //headless: false, executablePath, }); + + return browser; } - return browser; } async function prepareChromeExecutable(executablePath) {