Migrate to ECMAScript Modules

Migrate the usage of CommonJS specific code and imports to ESM.  Roughly
summarised this:
- Changes all require() calls to import statement or expressions.
- Changes usage of module.exports to export statements.
- Replaces require.resolve with import.meta.resolve.
- Replaces conditional imports with conditional subpath imports.
- Replaces module === require.main with import.meta.main.
- Adds the full filename with extention to all relative imports.
- Changes all usage of __dirname and __filename to their import.meta
  equivalents.
- Sets "type": "module" in package.json files.
- Removes usage of "use strict".
- Adds .ts an an alias of .js for the webpack build.

Due to differences in how require and import resolves files the plugin
entrypoints will have to include the filename and extension after this
migration in order to work.
This commit is contained in:
Hornwitser committed 2026-09-11 22:43:57 +02:00
1 parent b69111dda3
commit 5dcaca4d0b
261 files changed
+1115 -1330

No files matched your search

+4 -4
View File
@@ -2,10 +2,10 @@ import fs from "node:fs/promises";
import path from "path";
import events from "events";
import { Static } from "@sinclair/typebox";
import { logger } from "./logging";
import { downloadFile, safeOutputFile } from "./file_ops";
import { ModPortalReleaseSchema, ModPortalDetailsSchema, ModNameVersionPair } from "./data/messages_mod";
import { ModInfo, ModPack, MajorMinorVersion, ModVersionEquality, SourceVersion } from "./data";
import { logger } from "./logging.js";
import { downloadFile, safeOutputFile } from "./file_ops.js";
import { ModPortalReleaseSchema, ModPortalDetailsSchema, ModNameVersionPair } from "./data/messages_mod.js";
import { ModInfo, ModPack, MajorMinorVersion, ModVersionEquality, SourceVersion } from "./data/index.js";
export interface ModStoreEvents {
/** A stored mod was created, updated or deleted */
+1 -1
View File
@@ -1,4 +1,4 @@
import { InstallationError } from "./errors";
import { InstallationError } from "./errors.js";
declare global {
// eslint-disable-next-line vars-on-top
var _clusterioLibImportedFrom: string;
+3 -3
View File
@@ -6,9 +6,9 @@
* @author Hornwitser
* @module lib/command
*/
import * as libData from "./data";
import * as libErrors from "./errors";
import type { Link } from "./link";
import * as libData from "./data/index.js";
import * as libErrors from "./errors.js";
import type { Link } from "./link/index.js";
import type { Argv } from "yargs";
export type CommandHandler = (args: any, control: any) => Promise<void>;
+7 -7
View File
@@ -3,13 +3,13 @@ import { Type, Static } from "@sinclair/typebox";
import * as fs from "node:fs/promises";
import events from "events";
import isDeepStrictEqual from "../is_deep_strict_equal";
import { basicType } from "../helpers";
import * as libSchema from "../schema";
import { StringEnum } from "../data/composites";
import { safeOutputFile } from "../file_ops";
import * as validators from "./validators";
import { RequestError } from "../errors";
import isDeepStrictEqual from "#is_deep_strict_equal";
import { basicType } from "../helpers.js";
import * as libSchema from "../schema.js";
import { StringEnum } from "../data/composites.js";
import { safeOutputFile } from "../file_ops.js";
import * as validators from "./validators.js";
import { RequestError } from "../errors.js";
const ConfigLocation = StringEnum(["controller", "host", "control"]);
export type ConfigLocation = Static<typeof ConfigLocation>;
+4 -4
View File
@@ -1,9 +1,9 @@
// Core definitions for the configuration system
import * as classes from "./classes";
import * as validators from "./validators";
import type { PluginNodeEnvInfo, PluginWebpackEnvInfo } from "../plugin";
import * as classes from "./classes.js";
import * as validators from "./validators.js";
import type { PluginNodeEnvInfo, PluginWebpackEnvInfo } from "../plugin.js";
import { Static } from "@sinclair/typebox";
import { isTargetVersion } from "../data/version";
import { isTargetVersion } from "../data/version.js";
type configFromJSON<T> = (...args: Parameters<typeof classes.Config.fromJSON>) => T;
type configFromFile<T> = (...args: Parameters<typeof classes.Config.fromFile>) => Promise<T>;
+2 -2
View File
@@ -3,5 +3,5 @@
* @author Hornwitser
* @module lib/config
*/
export * from "./classes";
export * from "./definitions";
export * from "./classes.js";
export * from "./definitions.js";
+2 -2
View File
@@ -1,6 +1,6 @@
import { Static, Type } from "@sinclair/typebox";
import { StringEnum } from "./composites";
import { TargetVersion, TargetVersionSchema } from "./version";
import { StringEnum } from "./composites.js";
import { TargetVersion, TargetVersionSchema } from "./version.js";
export const InstanceStatus = StringEnum([
"unknown",
+5 -5
View File
@@ -2,16 +2,16 @@ import fs from "node:fs/promises";
import JSZip from "jszip";
import { Type, Static } from "@sinclair/typebox";
import * as libHash from "../hash";
import * as libSchema from "../schema";
import { findRoot } from "../zip_ops";
import { ModRecord } from "./ModPack";
import * as libHash from "../hash.js";
import * as libSchema from "../schema.js";
import { findRoot } from "../zip_ops.js";
import { ModRecord } from "./ModPack.js";
import {
MajorMinorVersion, MajorMinorVersionSchema, normaliseMajorMinorVersion, integerMajorMinorVersion,
SourceVersion, SourceVersionSchema, isSourceVersion, normaliseSourceVersion, integerSourceVersion,
ModVersionEquality,
} from "./version";
} from "./version.js";
type ModDependencyType = "incompatible" | "optional" | "hidden" | "unordered" | "required" | "recommended";
+7 -7
View File
@@ -1,19 +1,19 @@
import zlib from "zlib";
import { Type, Static } from "@sinclair/typebox";
import * as libSchema from "../schema";
import * as libLuaTools from "../lua_tools";
import * as libSchema from "../schema.js";
import * as libLuaTools from "../lua_tools.js";
import ExportManifest from "./ExportManifest";
import type { ExportSettings } from "../export";
import type { Logger } from "../logging";
import ExportManifest from "./ExportManifest.js";
import type { ExportSettings } from "../export.js";
import type { Logger } from "../logging.js";
import ModInfo, { ModDependencyUnsatisfiedReason } from "./ModInfo";
import ModInfo, { ModDependencyUnsatisfiedReason } from "./ModInfo.js";
import {
PartialVersion, PartialVersionSchema, integerPartialVersion,
SourceVersion, SourceVersionSchema, normaliseFullVersion, integerSourceVersion,
} from "./version";
} from "./version.js";
export const ModSettingColor = Type.Object({
+1 -1
View File
@@ -1,5 +1,5 @@
import { Type, Static } from "@sinclair/typebox";
import { compile } from "../schema";
import { compile } from "../schema.js";
/**
* Information about a module stored in its module.json file
+1 -1
View File
@@ -1,5 +1,5 @@
import { Static, Type } from "@sinclair/typebox";
import { permissions as globalPermissions } from "../permissions";
import { permissions as globalPermissions } from "../permissions.js";
interface RoleCollection {
get(id: number): Role;
+1 -1
View File
@@ -1,6 +1,6 @@
import { Static, Type } from "@sinclair/typebox";
import PlayerStats from "./PlayerStats";
import PlayerStats from "./PlayerStats.js";
/** Represents a user in the cluster */
export default class UserDetails {
+23 -23
View File
@@ -3,26 +3,26 @@
* @module lib/data
* @author Hornwitser
*/
export { default as ExportManifest } from "./ExportManifest";
export { default as ModInfo, ModDependency } from "./ModInfo";
export { ModSettingColor, getInstalledModUpdates, applyModRecordAdvisories } from "./ModPack";
export type { ModSetting, ModRecord, ModRecordAdvisory } from "./ModPack";
export { default as ModPack } from "./ModPack";
export { default as ModuleInfo } from "./ModuleInfo";
export { default as Permission } from "./Permission";
export { default as PlayerStats } from "./PlayerStats";
export { default as Role } from "./Role";
export { default as InstanceDetails, InstanceStatus } from "./InstanceDetails";
export { default as HostDetails } from "./HostDetails";
export { default as UserDetails } from "./UserDetails";
export type { IUser } from "./UserDetails";
export * from "./composites";
export * from "./messages_core";
export * from "./messages_controller";
export * from "./messages_host";
export * from "./messages_instance";
export * from "./messages_mod";
export * from "./messages_user";
export * from "./messages_role";
export * from "./messages_plugin";
export * from "./version";
export { default as ExportManifest } from "./ExportManifest.js";
export { default as ModInfo, ModDependency } from "./ModInfo.js";
export { ModSettingColor, getInstalledModUpdates, applyModRecordAdvisories } from "./ModPack.js";
export type { ModSetting, ModRecord, ModRecordAdvisory } from "./ModPack.js";
export { default as ModPack } from "./ModPack.js";
export { default as ModuleInfo } from "./ModuleInfo.js";
export { default as Permission } from "./Permission.js";
export { default as PlayerStats } from "./PlayerStats.js";
export { default as Role } from "./Role.js";
export { default as InstanceDetails, InstanceStatus } from "./InstanceDetails.js";
export { default as HostDetails } from "./HostDetails.js";
export { default as UserDetails } from "./UserDetails.js";
export type { IUser } from "./UserDetails.js";
export * from "./composites.js";
export * from "./messages_core.js";
export * from "./messages_controller.js";
export * from "./messages_host.js";
export * from "./messages_instance.js";
export * from "./messages_mod.js";
export * from "./messages_user.js";
export * from "./messages_role.js";
export * from "./messages_plugin.js";
export * from "./version.js";
+1 -1
View File
@@ -1,4 +1,4 @@
import type { ValidateFunction } from "ajv";
import type { Message } from "./messages_core";
import type { Message } from "./messages_core.js";
const message_validate: ValidateFunction<Message>;
export default message_validate;
+4 -4
View File
@@ -1,8 +1,8 @@
import { Type, Static } from "@sinclair/typebox";
import { JsonString, StringEnum, StringKey, plainJson } from "./composites";
import { levels } from "../logging";
import { ControllerConfig, HostConfig } from "../config";
import { ExternalFactorioVersionSchema, LatestReleasesSchema } from "../external";
import { JsonString, StringEnum, StringKey, plainJson } from "./composites.js";
import { levels } from "../logging.js";
import { ControllerConfig, HostConfig } from "../config/index.js";
import { ExternalFactorioVersionSchema, LatestReleasesSchema } from "../external/index.js";
export class ControllerStopRequest {
declare ["constructor"]: typeof ControllerStopRequest;
+2 -2
View File
@@ -1,8 +1,8 @@
// Note: Run compile_validator script after making changes to this file.
import { TSchema, Type, Static } from "@sinclair/typebox";
import messageValidate from "./message_validate"; // generated file
import { StringEnum } from "./composites";
import messageValidate from "./message_validate.js"; // generated file
import { StringEnum } from "./composites.js";
export type AddressType = "controller" | "host" | "instance" | "control" | "broadcast";
export type AddressShorthand =
+4 -4
View File
@@ -1,8 +1,8 @@
import { Type, Static } from "@sinclair/typebox";
import { jsonArray, plainJson, StringEnum } from "./composites";
import { CollectorResultSerialized } from "../prometheus";
import { HostConfig } from "../config/definitions";
import HostDetails from "./HostDetails";
import { jsonArray, plainJson, StringEnum } from "./composites.js";
import { CollectorResultSerialized } from "../prometheus.js";
import { HostConfig } from "../config/definitions.js";
import HostDetails from "./HostDetails.js";
export class HostStopRequest {
declare ["constructor"]: typeof HostStopRequest;
+8 -8
View File
@@ -1,12 +1,12 @@
import { Type, Static } from "@sinclair/typebox";
import PlayerStats from "./PlayerStats";
import { JsonString, StringEnum, jsonArray, plainJson } from "./composites";
import { InstanceConfig } from "../config";
import type { IUser } from "./UserDetails";
import type { MessageRequest } from "./messages_core";
import { CollectorResultSerialized } from "../prometheus";
import { TargetVersion, TargetVersionSchema, PartialVersion, PartialVersionSchema } from "./version";
import InstanceDetails, { InstanceStatus } from "./InstanceDetails";
import PlayerStats from "./PlayerStats.js";
import { JsonString, StringEnum, jsonArray, plainJson } from "./composites.js";
import { InstanceConfig } from "../config/index.js";
import type { IUser } from "./UserDetails.js";
import type { MessageRequest } from "./messages_core.js";
import { CollectorResultSerialized } from "../prometheus.js";
import { TargetVersion, TargetVersionSchema, PartialVersion, PartialVersionSchema } from "./version.js";
import InstanceDetails, { InstanceStatus } from "./InstanceDetails.js";
export class InstanceDetailsGetRequest {
declare ["constructor"]: typeof InstanceDetailsGetRequest;
+4 -4
View File
@@ -1,13 +1,13 @@
import { Type, Static } from "@sinclair/typebox";
import ModInfo, { ModDependency } from "./ModInfo";
import ModPack from "./ModPack";
import { JsonString, jsonArray } from "./composites";
import ModInfo, { ModDependency } from "./ModInfo.js";
import ModPack from "./ModPack.js";
import { JsonString, jsonArray } from "./composites.js";
import {
MajorMinorVersion, MajorMinorVersionSchema, normaliseMajorMinorVersion,
SourceVersion, SourceVersionSchema,
ModVersionEquality,
} from "./version";
} from "./version.js";
export class ModPackGetRequest {
+2 -2
View File
@@ -1,6 +1,6 @@
import { Type, Static } from "@sinclair/typebox";
import { jsonArray } from "./composites";
import { PluginNodeEnvInfo } from "../plugin";
import { jsonArray } from "./composites.js";
import { PluginNodeEnvInfo } from "../plugin.js";
/* This is similar to other plugin definitions but with lots removed */
export class PluginDetails {
+3 -3
View File
@@ -1,7 +1,7 @@
import { Type, Static } from "@sinclair/typebox";
import { JsonNumber, jsonArray } from "./composites";
import Permission from "./Permission";
import Role from "./Role";
import { JsonNumber, jsonArray } from "./composites.js";
import Permission from "./Permission.js";
import Role from "./Role.js";
export class PermissionListRequest {
declare ["constructor"]: typeof PermissionListRequest;
+3 -3
View File
@@ -1,7 +1,7 @@
import { Type, Static } from "@sinclair/typebox";
import UserDetails, { IUser } from "./UserDetails";
import { StringEnum, jsonArray, plainJson } from "./composites";
import { MessageRequest } from "./messages_core";
import UserDetails, { IUser } from "./UserDetails.js";
import { StringEnum, jsonArray, plainJson } from "./composites.js";
import { MessageRequest } from "./messages_core.js";
export class UserGetRequest {
declare ["constructor"]: typeof UserGetRequest;
+2 -2
View File
@@ -6,8 +6,8 @@
*/
import fs from "node:fs/promises";
import * as libFileOps from "./file_ops";
import { basicType } from "./helpers";
import * as libFileOps from "./file_ops.js";
import { basicType } from "./helpers.js";
export type ItemCountWithQuality = { [quality: string]: number };
+4 -4
View File
@@ -2,10 +2,10 @@ import path from "path";
import fs from "node:fs/promises";
import { EventEmitter } from "events";
import { safeOutputFile } from "./file_ops";
import { SubscribableValue } from "./subscriptions";
import { logger } from "./logging";
import { ControllerConfig } from "./config";
import { safeOutputFile } from "./file_ops.js";
import { SubscribableValue } from "./subscriptions.js";
import { logger } from "./logging.js";
import { ControllerConfig } from "./config/index.js";
type DatastoreKey = string | number;
type DatastoreValue = string | number | boolean | object;
+1 -1
View File
@@ -1,4 +1,4 @@
import { FactorioColor } from "./lua_tools";
import { FactorioColor } from "./lua_tools.js";
/**
* A localised string.
+1 -1
View File
@@ -1,5 +1,5 @@
import { Static, Type } from "@sinclair/typebox";
import { FullVersionSchema, integerFullVersion, isFullVersion } from "../data/version";
import { FullVersionSchema, integerFullVersion, isFullVersion } from "../data/version.js";
const ARCHIVE_URL = "https://factorio.com/download/archive/";
+1 -1
View File
@@ -1,5 +1,5 @@
import { Static, Type } from "@sinclair/typebox";
import { FullVersion, FullVersionSchema } from "../data/version";
import { FullVersion, FullVersionSchema } from "../data/version.js";
const LATEST_RELEASES_URL = "https://factorio.com/api/latest-releases";
+2 -2
View File
@@ -2,5 +2,5 @@
* Library for interfacing with external apis
* @module lib/external
*/
export * from "./FactorioVersions";
export * from "./LatestReleases";
export * from "./FactorioVersions.js";
export * from "./LatestReleases.js";
+1 -1
View File
@@ -3,4 +3,4 @@
* @module lib/factorio
* @author Hornwitser
*/
export * from "./exchange_string";
export * from "./exchange_string.js";
+1 -1
View File
@@ -7,7 +7,7 @@ import fs from "node:fs/promises";
import path from "path";
import stream from "stream";
import { redactUrl } from "./helpers";
import { redactUrl } from "./helpers.js";
/**
+1 -1
View File
@@ -3,7 +3,7 @@
* @module lib/helpers
*/
import type { Logger } from "./logging";
import type { Logger } from "./logging.js";
import setBlocking from "set-blocking";
/**
@@ -0,0 +1 @@
export { default } from "fast-deep-equal/es6/index.js";
-1
View File
@@ -1 +0,0 @@
export default function isDeepStrictEqual(a: any, b: any): boolean;
-9
View File
@@ -1,9 +0,0 @@
"use strict";
if (process.env.APP_ENV === "browser") {
module.exports = require("fast-deep-equal/es6");
} else {
module.exports = require("util").isDeepStrictEqual;
}
@@ -0,0 +1 @@
export { isDeepStrictEqual as default } from "util";
@@ -0,0 +1,2 @@
const NativeWebSocket = WebSocket;
export { NativeWebSocket as WebSocket };
-2
View File
@@ -1,2 +0,0 @@
import ws = require("ws");
export = ws;
-3
View File
@@ -1,3 +0,0 @@
"use strict";
module.exports = process.env.APP_ENV === "browser" ? window.WebSocket : require("ws");
+1
View File
@@ -0,0 +1 @@
export { WebSocket } from "ws";
+13 -10
View File
@@ -2,12 +2,12 @@
import { strict as assert } from "assert";
import events from "events";
import WebSocket from "./WebSocket";
import * as libData from "../data";
import * as libErrors from "../errors";
import { logger } from "../logging";
import ExponentialBackoff from "../ExponentialBackoff";
import type { Request, Event } from "./link";
import { WebSocket } from "#WebSocket";
import * as libData from "../data/index.js";
import * as libErrors from "../errors.js";
import { logger } from "../logging.js";
import ExponentialBackoff from "../ExponentialBackoff.js";
import type { Request, Event } from "./link.js";
/**
* Numbered codes describing why a connection was closed
@@ -151,7 +151,7 @@ type WebSocketBaseConnectorEvents = {
"drop": [],
};
export type WebSocketClusterio = WebSocket.WebSocket & {
export type WebSocketClusterio = WebSocket & {
clusterio_ignore_dump?: boolean;
};
@@ -275,7 +275,9 @@ export abstract class WebSocketBaseConnector<
if (process.env.APP_ENV === "browser") {
this._socket!.close(ConnectionClosed.Timeout, "Heartbeat timeout");
} else {
this._socket!.terminate();
// This is only available in node, but the typechecking doesn't
// detect that this code is unreachable in the browser build.
(this._socket! as any).terminate();
}
} else {
@@ -635,7 +637,7 @@ export abstract class WebSocketClientConnector extends WebSocketBaseConnector<We
}
};
this._socket!.onerror = (event) => {
this._socket!.onerror = ((event: { error?: any, message?: string, }) => {
// It's assumed that close is always called by ws
let code = !event.error ? "" : `, code: ${event.error.code}`;
let message = `Connector | Socket error: ${event.message || "unknown error"}${code}`;
@@ -657,7 +659,8 @@ export abstract class WebSocketClientConnector extends WebSocketBaseConnector<We
this._closing = true;
}
}
};
// The event paramater have conflicting types in the node and browser versions, ignore it.
}) as any;
this._socket!.onopen = () => {
logger.verbose("Connector | Open");
+2 -2
View File
@@ -3,5 +3,5 @@
* @module lib/link
* @author Hornwitser
*/
export * from "./link";
export * from "./connectors";
export * from "./link.js";
export * from "./connectors.js";
+8 -8
View File
@@ -1,15 +1,15 @@
// Implementation of Link class
import * as libData from "../data";
import * as libErrors from "../errors";
import { logger } from "../logging";
import * as libSchema from "../schema";
import { dataClasses } from "./messages";
import { BaseConnector, WebSocketBaseConnector } from "./connectors";
import * as libData from "../data/index.js";
import * as libErrors from "../errors.js";
import { logger } from "../logging.js";
import * as libSchema from "../schema.js";
import { dataClasses } from "./messages.js";
import { BaseConnector, WebSocketBaseConnector } from "./connectors.js";
import { strict as assert } from "assert";
import type { PluginNodeEnvInfo, PluginWebpackEnvInfo } from "../plugin";
import type { PluginNodeEnvInfo, PluginWebpackEnvInfo } from "../plugin.js";
import type {
AddressType, JSONDeserialisable, MessageRoutable, MessageRequest, MessageEvent, IUser,
} from "../data";
} from "../data/index.js";
export interface Request<Req, Res> {
constructor: Partial<JSONDeserialisable<Req & Request<Req, Res>>> & {
+10 -10
View File
@@ -1,13 +1,13 @@
import * as core from "../data/messages_core";
import * as controller from "../data/messages_controller";
import * as host from "../data/messages_host";
import * as instance from "../data/messages_instance";
import * as mod from "../data/messages_mod";
import * as user from "../data/messages_user";
import * as role from "../data/messages_role";
import * as plugin from "../data/messages_plugin";
import * as subscriptions from "../subscriptions";
import type { RequestClass, EventClass } from "./link";
import * as core from "../data/messages_core.js";
import * as controller from "../data/messages_controller.js";
import * as host from "../data/messages_host.js";
import * as instance from "../data/messages_instance.js";
import * as mod from "../data/messages_mod.js";
import * as user from "../data/messages_user.js";
import * as role from "../data/messages_role.js";
import * as plugin from "../data/messages_plugin.js";
import * as subscriptions from "../subscriptions.js";
import type { RequestClass, EventClass } from "./link.js";
/**
* Event and Request payloads registered with the Link class. Used to decode
+3 -2
View File
@@ -1,8 +1,8 @@
import fs from "node:fs/promises";
import { createRequire } from "node:module";
import path from "path";
import { logger } from "./logging";
import * as libFileOps from "./file_ops";
import { logger } from "./logging.js";
import * as libFileOps from "./file_ops.js";
import vm from "vm";
/**
@@ -67,6 +67,7 @@ async function findLocalPlugins(pluginList: Map<string, string>): Promise<boolea
return hasChanged > 0;
}
const require = createRequire(import.meta.url);
function getPluginName(requireSpec: string) {
const context = vm.createContext({ require: require, pluginInfo: null });
const code = `pluginInfo = require(${JSON.stringify(requireSpec)}).plugin;`;
+6 -6
View File
@@ -14,12 +14,12 @@ import Transport from "winston-transport";
import { LEVEL, MESSAGE } from "triple-beam";
import chalk from "chalk";
import * as libData from "./data";
import * as libErrors from "./errors";
import * as libFileOps from "./file_ops";
import { type LogFilter, levels, logFilter, logger } from "./logging";
import * as libStream from "./stream";
import type { Link } from "./link";
import * as libData from "./data/index.js";
import * as libErrors from "./errors.js";
import * as libFileOps from "./file_ops.js";
import { type LogFilter, levels, logFilter, logger } from "./logging.js";
import * as libStream from "./stream.js";
import type { Link } from "./link/index.js";
const finished = util.promisify(stream.finished);
+1 -1
View File
@@ -6,7 +6,7 @@
* @author Hornwitser
* @module lib/users
*/
import { Permission } from "./data";
import { Permission } from "./data/index.js";
export const permissions = new Map<string, Permission>();
+4 -4
View File
@@ -2,10 +2,10 @@
* Plugin interfaces and utilities.
* @module lib/plugin
*/
import * as libHelpers from "./helpers";
import type { Logger } from "./logging";
import type { FieldDefinition } from "./config";
import type { PlayerStats } from "./data";
import * as libHelpers from "./helpers.js";
import type { Logger } from "./logging.js";
import type { FieldDefinition } from "./config/index.js";
import type { PlayerStats } from "./data/index.js";
export const PluginFeatureFlags = [
+12 -7
View File
@@ -5,9 +5,10 @@
*/
import path from "path";
import fs from "node:fs/promises";
import * as libErrors from "./errors";
import * as libPlugin from "./plugin";
import { logger } from "./logging";
import { fileURLToPath, pathToFileURL } from "node:url";
import * as libErrors from "./errors.js";
import * as libPlugin from "./plugin.js";
import { logger } from "./logging.js";
/**
@@ -32,7 +33,9 @@ export async function loadPluginInfos(pluginList: Map<string, string>) {
try {
const absolute = path.isAbsolute(pluginPath);
const packageImport = path.posix.join(pluginPath, "package.json");
packagePath = require.resolve(packageImport);
packagePath = fileURLToPath(import.meta.resolve(
absolute ? pathToFileURL(packageImport).href : packageImport
));
await fs.access(packagePath, fs.constants.F_OK);
} catch (err) {
let errMsg = `Plugin path ${pluginPath} does not exist`;
@@ -46,8 +49,10 @@ export async function loadPluginInfos(pluginList: Map<string, string>) {
}
try {
pluginInfo = require(pluginPath).plugin;
pluginPackage = require(packagePath);
pluginPackage = (await import(pathToFileURL(packagePath).href, { with: { type: "json" }})).default;
pluginInfo = (await import(
pathToFileURL(path.posix.join(pluginPath, pluginPackage.main ?? "index.js")).href
)).plugin;
} catch (err: any) {
if (err.code === "InstallationError") {
throw err;
@@ -89,7 +94,7 @@ export async function loadPluginClass<Class extends { new (...args: any): any }>
className: string,
pluginClass: Class,
): Promise<Class> {
let entrypoint = require(requirePath);
let entrypoint = await import(pathToFileURL(requirePath).href);
if (!entrypoint[className]) {
throw new libErrors.PluginError(pluginName,
new Error(`Expected ${requirePath} to export a class named ${className}`)
+6 -4
View File
@@ -12,7 +12,7 @@
* the listed interfaces for more information.
*
* @example
* const { Counter, exposition } = require("@clusterio/lib");
* import { Counter, exposition } from "@clusterio/lib";
*
* // Collectors are by default registered to the default collector regitry.
* const totalRequests = new Counter(
@@ -27,7 +27,8 @@
*
* // Code handling the /metrics HTTP request, here shown for express
* // but any http framework may be used
* const app = require("express")();
* import express from "express";
* const app = express();
* async function getMetrics(req, res) {
* // By default exposition uses the default collector registry
* let text = await exposition();
@@ -39,7 +40,7 @@
* @module lib/prometheus
*/
import { Type, Static } from "@sinclair/typebox";
import { StringEnum } from "./data/composites";
import { StringEnum } from "./data/composites.js";
/**
* Result from collecting a {@link Collector}
@@ -1669,7 +1670,8 @@ async function* expositionLines(
* express.js.
*
* @example
* const app = require("express")();
* import express from "express";
* const app = express();
* async function getMetrics(req, res) {
* // By default exposition uses the default collector registry
* let text = await exposition();
+4 -4
View File
@@ -1,15 +1,15 @@
import util from "util";
import path from "path";
import { exec } from "child_process";
import { logger } from "./logging";
import { RequestError } from "./errors";
import { PluginNodeEnvInfo } from "./plugin";
import { logger } from "./logging.js";
import { RequestError } from "./errors.js";
import { PluginNodeEnvInfo } from "./plugin.js";
const execAsync = util.promisify(exec);
function isDev() {
// dev: <devRoot>/packages/lib/dist/src/rce_ops.js
// prod: <prodRoot>/node_modules/@clusterio/lib/dist/src/rce_ops.js
return __dirname.split(path.sep).at(-5) === "packages"; // opposed to "@clusterio"
return import.meta.dirname.split(path.sep).at(-5) === "packages"; // opposed to "@clusterio"
}
async function logExec(cmd: string) {
+1 -1
View File
@@ -2,7 +2,7 @@
* JSON schemas used for validating link messages
* @module lib/schema
*/
import Ajv, { JSONSchemaType, ValidateFunction } from "ajv";
import { Ajv, JSONSchemaType, ValidateFunction } from "ajv";
const ajv = new Ajv({
// These are duplicated in scripts/compile_validator
allowUnionTypes: true,
+13 -7
View File
@@ -4,11 +4,12 @@
*/
import path from "path";
import * as libConfig from "./config";
import * as libFileOps from "./file_ops";
import { logger } from "./logging";
import * as libHelpers from "./helpers";
import { LockFile } from "./LockFile";
import * as libConfig from "./config/index.js";
import * as libFileOps from "./file_ops.js";
import { logger } from "./logging.js";
import * as libHelpers from "./helpers.js";
import { LockFile } from "./LockFile.js";
import { pathToFileURL } from "url";
function print(...content: any[]) {
@@ -59,8 +60,13 @@ export async function handlePluginCommand(
let pluginInfo: { name: string };
try {
pluginInfo = require(pluginPath).plugin;
const pluginPackage = (await import(
pathToFileURL(path.posix.join(pluginPath, "package.json")).href,
{ with: { type: "json" }},
)).default;
pluginInfo = (await import(
pathToFileURL(path.posix.join(pluginPath, pluginPackage.main ?? "index.js")).href
)).plugin;
} catch (err: any) {
logger.error(`Unable to import plugin info from ${args.path}:\n${err.stack}`);
process.exitCode = 1;
+5 -5
View File
@@ -1,9 +1,9 @@
import { Type, Static } from "@sinclair/typebox";
import { Link, Event, EventClass, RequestHandler, WebSocketBaseConnector } from "./link";
import { Address, MessageRequest, IUser, JsonBoolean, StringEnum, AccountDetails } from "./data";
import isDeepStrictEqual from "./is_deep_strict_equal";
import { RequestError } from "./errors";
import { logger } from "./logging";
import { Link, Event, EventClass, RequestHandler, WebSocketBaseConnector } from "./link/index.js";
import { Address, MessageRequest, IUser, JsonBoolean, StringEnum } from "./data/index.js";
import isDeepStrictEqual from "#is_deep_strict_equal";
import { RequestError } from "./errors.js";
import { logger } from "./logging.js";
export type SubscriptionRequestHandler<T> = RequestHandler<SubscriptionRequest, Event<T> | null>;
export type EventSubscriberCallback<T> = (event: T | null, synced: boolean) => void
+2 -2
View File
@@ -1,8 +1,8 @@
import fs from "node:fs/promises";
import os from "os";
import util from "util";
import { Gauge } from "./prometheus";
import { SystemInfo } from "./data";
import { Gauge } from "./prometheus.js";
import { SystemInfo } from "./data/index.js";
function filterCpuModel() {
const model = os.cpus()[0].model;