Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
2 changes: 1 addition & 1 deletion defaultmodules/updatenotification/node_helper.js
Original file line number Diff line number Diff line change
@@ -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";

Expand Down
6 changes: 3 additions & 3 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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",
Expand All @@ -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: {
Expand Down
15 changes: 8 additions & 7 deletions js/app.mjs
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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)), "..");

Expand Down Expand Up @@ -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);

Expand Down
12 changes: 7 additions & 5 deletions js/http_fetcher.js → js/http_fetcher.mjs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -392,4 +394,4 @@ class HTTPFetcher extends EventEmitter {
}
}

module.exports = HTTPFetcher;
export { HTTPFetcher as default, HTTPFetcher as "module.exports" };
4 changes: 2 additions & 2 deletions js/loader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "../defaultmodules/defaultmodules.js";
import "../defaultmodules/defaultmodules.mjs";
import "./vendor.mjs";

/* Module state */
Expand All @@ -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
Expand Down
4 changes: 1 addition & 3 deletions js/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 22 additions & 8 deletions js/server_functions.js → js/server_functions.mjs
Original file line number Diff line number Diff line change
@@ -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();

Expand Down Expand Up @@ -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
Expand All @@ -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
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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" };
Expand Down
15 changes: 0 additions & 15 deletions tests/unit/global_vars/defaults_modules_spec.js

This file was deleted.

17 changes: 17 additions & 0 deletions tests/unit/global_vars/defaults_modules_spec.mjs
Original file line number Diff line number Diff line change
@@ -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);
});
}
});
Loading