diff --git a/defaultmodules/defaultmodules.js b/defaultmodules/defaultmodules.mjs similarity index 69% rename from defaultmodules/defaultmodules.js rename to defaultmodules/defaultmodules.mjs index d80b72f346..ec341e462d 100644 --- a/defaultmodules/defaultmodules.js +++ b/defaultmodules/defaultmodules.mjs @@ -6,7 +6,4 @@ const defaultModules = ["alert", "calendar", "clock", "compliments", "helloworld globalThis.defaultModules = defaultModules; -/*************** DO NOT EDIT THE LINE BELOW ***************/ -if (typeof module !== "undefined") { - module.exports = defaultModules; -} +export { defaultModules as default }; diff --git a/defaultmodules/updatenotification/node_helper.js b/defaultmodules/updatenotification/node_helper.js index 4728e884f4..b013104f16 100644 --- a/defaultmodules/updatenotification/node_helper.js +++ b/defaultmodules/updatenotification/node_helper.js @@ -1,7 +1,7 @@ import fs from "node:fs"; import path from "node:path"; import NodeHelper from "node_helper"; -import defaultModules from "../defaultmodules.js"; +import defaultModules from "../defaultmodules.mjs"; import GitHelper from "./git_helper.js"; import UpdateHelper from "./update_helper.js"; diff --git a/eslint.config.mjs b/eslint.config.mjs index 3d9a50db48..08d1227919 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -32,7 +32,7 @@ export default defineConfig([ } }, { - files: ["**/*.js", "js/app.mjs", "js/deprecated.mjs", "js/electron_helper.mjs", "js/ip_access_control.mjs", "js/releasenotes.mjs", "js/server.mjs", "js/vendor.mjs", "tests/**/*.mjs"], + files: ["**/*.js", "defaultmodules/defaultmodules.mjs", "js/app.mjs", "js/deprecated.mjs", "js/electron_helper.mjs", "js/http_fetcher.mjs", "js/ip_access_control.mjs", "js/releasenotes.mjs", "js/server.mjs", "js/server_functions.mjs", "js/vendor.mjs", "tests/**/*.mjs"], languageOptions: { ecmaVersion: "latest", globals: { @@ -104,7 +104,7 @@ export default defineConfig([ } }, { - files: ["**/*.js", "js/app.mjs", "js/deprecated.mjs", "js/electron_helper.mjs", "js/ip_access_control.mjs", "js/releasenotes.mjs", "js/server.mjs", "js/vendor.mjs", "tests/**/*.mjs"], + files: ["**/*.js", "defaultmodules/defaultmodules.mjs", "js/app.mjs", "js/deprecated.mjs", "js/electron_helper.mjs", "js/http_fetcher.mjs", "js/ip_access_control.mjs", "js/releasenotes.mjs", "js/server.mjs", "js/server_functions.mjs", "js/vendor.mjs", "tests/**/*.mjs"], ignores: [ "clientonly/index.js", "js/logger.js", @@ -119,7 +119,7 @@ export default defineConfig([ }, { files: ["**/*.mjs"], - ignores: ["js/app.mjs", "js/deprecated.mjs", "js/electron_helper.mjs", "js/ip_access_control.mjs", "js/releasenotes.mjs", "js/server.mjs", "js/vendor.mjs", "tests/**/*.mjs"], + ignores: ["defaultmodules/defaultmodules.mjs", "js/app.mjs", "js/deprecated.mjs", "js/electron_helper.mjs", "js/http_fetcher.mjs", "js/ip_access_control.mjs", "js/releasenotes.mjs", "js/server.mjs", "js/server_functions.mjs", "js/vendor.mjs", "tests/**/*.mjs"], languageOptions: { ecmaVersion: "latest", globals: { diff --git a/js/app.mjs b/js/app.mjs index 730a8771a3..f6d05bbfd8 100644 --- a/js/app.mjs +++ b/js/app.mjs @@ -1,6 +1,5 @@ import "./alias-resolver.js"; -import { createRequire } from "node:module"; -import { fileURLToPath } from "node:url"; +import { fileURLToPath, pathToFileURL } from "node:url"; import fs from "node:fs"; import path from "node:path"; import { spawn } from "node:child_process"; @@ -9,12 +8,9 @@ import packageJson from "../package.json" with { type: "json" }; import Log from "./logger.js"; import Server from "./server.mjs"; import Utils from "./utils.js"; -import serverFunctions from "#server_functions"; +import { getEnvVarsAsObj } from "#server_functions"; -const require = createRequire(import.meta.url); const { ConfigError } = Utils; -const { getEnvVarsAsObj } = serverFunctions; - // global absolute root path global.root_path = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); @@ -244,7 +240,12 @@ class App { Utils.checkConfigFile(configObj); global.defaultModulesDir = config.defaultModulesDir; - this.#defaultModules = require(`${global.root_path}/${global.defaultModulesDir}/defaultmodules`); + const defaultModulesPath = path.resolve(`${global.root_path}/${global.defaultModulesDir}/defaultmodules`); + const cjsDefaultModulesPath = `${defaultModulesPath}.js`; + const defaultModulesUrl = fs.existsSync(cjsDefaultModulesPath) + ? pathToFileURL(cjsDefaultModulesPath) + : pathToFileURL(`${defaultModulesPath}.mjs`); + this.#defaultModules = (await import(defaultModulesUrl.href)).default; Log.setLogLevel(config.logLevel); diff --git a/js/http_fetcher.js b/js/http_fetcher.mjs similarity index 98% rename from js/http_fetcher.js rename to js/http_fetcher.mjs index 7d8c13169a..bc2538d1e6 100644 --- a/js/http_fetcher.js +++ b/js/http_fetcher.mjs @@ -1,7 +1,9 @@ -const { EventEmitter } = require("node:events"); -const { fetch: undiciFetch, Agent } = require("undici"); -const Log = require("logger"); -const { getUserAgent } = require("#server_functions"); +import { EventEmitter } from "node:events"; +import undici from "undici"; +import Log from "./logger.js"; +import { getUserAgent } from "#server_functions"; + +const { fetch: undiciFetch, Agent } = undici; const FIFTEEN_MINUTES = 15 * 60 * 1000; const THIRTY_MINUTES = 30 * 60 * 1000; @@ -392,4 +394,4 @@ class HTTPFetcher extends EventEmitter { } } -module.exports = HTTPFetcher; +export { HTTPFetcher as default, HTTPFetcher as "module.exports" }; diff --git a/js/loader.js b/js/loader.js index 2d46a68a9a..14cd744c8b 100644 --- a/js/loader.js +++ b/js/loader.js @@ -1,4 +1,4 @@ -import "../defaultmodules/defaultmodules.js"; +import "../defaultmodules/defaultmodules.mjs"; import "./vendor.mjs"; /* Module state */ @@ -21,7 +21,7 @@ const getEnvVarsFromConfig = () => { /** * Retrieve object of env variables. - * @returns {object} with key: values as assembled in js/server_functions.js + * @returns {object} with key: values as assembled in js/server_functions.mjs */ const getEnvVars = async () => { // In test mode, skip server fetch and use config values directly diff --git a/js/server.mjs b/js/server.mjs index b5b4a47f27..6464d44bce 100644 --- a/js/server.mjs +++ b/js/server.mjs @@ -8,9 +8,7 @@ import { Server as SocketServer } from "socket.io"; import Log from "./logger.js"; import { ipAccessControl, socketIpAccessControl } from "./ip_access_control.mjs"; import vendor from "./vendor.mjs"; -import serverFunctions from "#server_functions"; - -const { getHtml, getVersion, getEnvVars, getServerPort, cors } = serverFunctions; +import { getHtml, getVersion, getEnvVars, getServerPort, cors } from "#server_functions"; /** * Server diff --git a/js/server_functions.js b/js/server_functions.mjs similarity index 93% rename from js/server_functions.js rename to js/server_functions.mjs index d79ced0fbc..99e1a9ea13 100644 --- a/js/server_functions.js +++ b/js/server_functions.mjs @@ -1,9 +1,9 @@ -const dns = require("node:dns"); -const fs = require("node:fs"); -const path = require("node:path"); -const ipaddr = require("ipaddr.js"); -const undici = require("undici"); -const Log = require("logger"); +import dns from "node:dns"; +import fs from "node:fs"; +import path from "node:path"; +import ipaddr from "ipaddr.js"; +import undici from "undici"; +import Log from "./logger.js"; const startUp = new Date(); @@ -264,7 +264,7 @@ const getServerPort = (config = global.config) => { const getConfigFilePath = () => { // Ensure root_path is set (for standalone contexts like watcher) if (!global.root_path) { - global.root_path = path.resolve(`${__dirname}/../`); + global.root_path = path.resolve(import.meta.dirname, "../"); } // Check environment variable if global not set @@ -275,4 +275,18 @@ const getConfigFilePath = () => { return path.resolve(global.configuration_file || `${global.root_path}/config/config.js`); }; -module.exports = { cors, getHtml, getVersion, getStartup, getEnvVars, getEnvVarsAsObj, getUserAgent, getServerPort, getConfigFilePath, replaceSecretPlaceholder }; +const serverFunctions = { cors, getHtml, getVersion, getStartup, getEnvVars, getEnvVarsAsObj, getUserAgent, getServerPort, getConfigFilePath, replaceSecretPlaceholder }; + +export { + serverFunctions as "module.exports", + cors, + getHtml, + getVersion, + getStartup, + getEnvVars, + getEnvVarsAsObj, + getUserAgent, + getServerPort, + getConfigFilePath, + replaceSecretPlaceholder +}; diff --git a/package.json b/package.json index da710f5130..7d905f23da 100755 --- a/package.json +++ b/package.json @@ -28,10 +28,10 @@ "type": "commonjs", "imports": { "#server_functions": { - "default": "./js/server_functions.js" + "default": "./js/server_functions.mjs" }, "#http_fetcher": { - "default": "./js/http_fetcher.js" + "default": "./js/http_fetcher.mjs" } }, "main": "js/electron.js", diff --git a/tests/unit/functions/http_fetcher_spec.js b/tests/unit/functions/http_fetcher_spec.mjs similarity index 99% rename from tests/unit/functions/http_fetcher_spec.js rename to tests/unit/functions/http_fetcher_spec.mjs index ae36fb0948..e86e128357 100644 --- a/tests/unit/functions/http_fetcher_spec.js +++ b/tests/unit/functions/http_fetcher_spec.mjs @@ -1,6 +1,6 @@ -const { http, HttpResponse } = require("msw"); -const { setupServer } = require("msw/node"); -const HTTPFetcher = require("#http_fetcher"); +import { http, HttpResponse } from "msw"; +import { setupServer } from "msw/node"; +import HTTPFetcher from "#http_fetcher"; const TEST_URL = "http://test.example.com/data"; let server; diff --git a/tests/unit/functions/server_functions_spec.js b/tests/unit/functions/server_functions_spec.mjs similarity index 94% rename from tests/unit/functions/server_functions_spec.js rename to tests/unit/functions/server_functions_spec.mjs index cd43eaf2bd..b6903ff020 100644 --- a/tests/unit/functions/server_functions_spec.js +++ b/tests/unit/functions/server_functions_spec.mjs @@ -1,13 +1,19 @@ -// Tests use vi.spyOn on shared module objects (dns, undici). -// vi.spyOn modifies the object property directly on the cached module instance, so it -// is intercepted by server_functions.js regardless of the Module.prototype.require override -// in vitest-setup.js. restoreAllMocks:true auto-restores spies, but may reuse the same -// spy instance — mockClear() is called explicitly in beforeEach to reset call history. -const dns = require("node:dns"); -const undici = require("undici"); -const { cors, getUserAgent, replaceSecretPlaceholder } = require("#server_functions"); +import { createRequire } from "node:module"; +import dns from "node:dns"; +import undici from "undici"; +import { cors, getUserAgent, replaceSecretPlaceholder } from "#server_functions"; + +const require = createRequire(import.meta.url); +const commonJSExports = require("#server_functions"); describe("server_functions tests", () => { + it("preserves the CommonJS API", () => { + expect(Object.getPrototypeOf(commonJSExports)).toBe(Object.prototype); + expect(commonJSExports).toHaveProperty("cors", expect.any(Function)); + expect(commonJSExports).toHaveProperty("getUserAgent", expect.any(Function)); + expect(commonJSExports).toHaveProperty("replaceSecretPlaceholder", expect.any(Function)); + }); + describe("The replaceSecretPlaceholder method with cors=allowWhitelist", () => { beforeEach(() => { global.config = { cors: "allowWhitelist" }; diff --git a/tests/unit/global_vars/defaults_modules_spec.js b/tests/unit/global_vars/defaults_modules_spec.js deleted file mode 100644 index 993dcb63ee..0000000000 --- a/tests/unit/global_vars/defaults_modules_spec.js +++ /dev/null @@ -1,15 +0,0 @@ -const fs = require("node:fs"); -const path = require("node:path"); -const defaults = require("../../../js/defaults"); - -const root_path = path.join(__dirname, "../../.."); - -describe("Default modules set in defaultmodules/defaultmodules.js", () => { - const expectedDefaultModules = require(`${root_path}/${defaults.defaultModulesDir}/defaultmodules`); - - for (const defaultModule of expectedDefaultModules) { - it(`contains a folder for defaultmodules/${defaultModule}"`, () => { - expect(fs.existsSync(path.join(root_path, "defaultmodules", defaultModule))).toBe(true); - }); - } -}); diff --git a/tests/unit/global_vars/defaults_modules_spec.mjs b/tests/unit/global_vars/defaults_modules_spec.mjs new file mode 100644 index 0000000000..5998f48cbe --- /dev/null +++ b/tests/unit/global_vars/defaults_modules_spec.mjs @@ -0,0 +1,17 @@ +import fs from "node:fs"; +import path from "node:path"; +import defaultModules from "../../../defaultmodules/defaultmodules.mjs"; + +const rootPath = path.resolve(import.meta.dirname, "../../.."); + +describe("Default modules set in defaultmodules/defaultmodules.mjs", () => { + it("publishes the default module list globally", () => { + expect(globalThis.defaultModules).toEqual(defaultModules); + }); + + for (const defaultModule of defaultModules) { + it(`contains a folder for defaultmodules/${defaultModule}"`, () => { + expect(fs.existsSync(path.join(rootPath, "defaultmodules", defaultModule))).toBe(true); + }); + } +});