mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-25 08:23:13 +09:00
Merge pull request #764 from Cooldude2606/refactor/config-file-path
Refactor config file paths into the config class
This commit is contained in:
15 files changed
+311
-119
No files matched your search
@@ -210,7 +210,6 @@ interface InitializeParameters {
|
|||||||
shouldRun: boolean;
|
shouldRun: boolean;
|
||||||
clusterLogger: winston.Logger;
|
clusterLogger: winston.Logger;
|
||||||
pluginInfos: lib.PluginNodeEnvInfo[] | null;
|
pluginInfos: lib.PluginNodeEnvInfo[] | null;
|
||||||
controllerConfigPath: string | null;
|
|
||||||
controllerConfig: lib.ControllerConfig | null;
|
controllerConfig: lib.ControllerConfig | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -334,7 +333,6 @@ async function initialize(): Promise<InitializeParameters> {
|
|||||||
shouldRun,
|
shouldRun,
|
||||||
clusterLogger,
|
clusterLogger,
|
||||||
pluginInfos: null,
|
pluginInfos: null,
|
||||||
controllerConfigPath: null,
|
|
||||||
controllerConfig: null,
|
controllerConfig: null,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -344,20 +342,19 @@ async function initialize(): Promise<InitializeParameters> {
|
|||||||
lib.registerPluginMessages(pluginInfos);
|
lib.registerPluginMessages(pluginInfos);
|
||||||
lib.addPluginConfigFields(pluginInfos);
|
lib.addPluginConfigFields(pluginInfos);
|
||||||
|
|
||||||
|
let controllerConfig;
|
||||||
const controllerConfigPath = args.config;
|
const controllerConfigPath = args.config;
|
||||||
logger.info(`Loading config from ${controllerConfigPath}`);
|
logger.info(`Loading config from ${controllerConfigPath}`);
|
||||||
let controllerConfig = new lib.ControllerConfig("controller");
|
|
||||||
try {
|
try {
|
||||||
let jsonConfig = JSON.parse(await fs.readFile(controllerConfigPath, { encoding: "utf8" }));
|
controllerConfig = await lib.ControllerConfig.fromFile("controller", controllerConfigPath);
|
||||||
controllerConfig = lib.ControllerConfig.fromJSON(jsonConfig, "controller");
|
|
||||||
|
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
if (err.code === "ENOENT") {
|
if (err.code === "ENOENT") {
|
||||||
logger.info("Config not found, initializing new config");
|
logger.info("Config not found, initializing new config");
|
||||||
controllerConfig = new lib.ControllerConfig("controller");
|
controllerConfig = new lib.ControllerConfig("controller", undefined, controllerConfigPath);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
throw new lib.StartupError(`Failed to load ${controllerConfigPath}: ${err.message}`);
|
throw new lib.StartupError(`Failed to load ${controllerConfigPath}: ${err.stack ?? err.message ?? err}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -368,15 +365,11 @@ async function initialize(): Promise<InitializeParameters> {
|
|||||||
let asyncRandomBytes = util.promisify(crypto.randomBytes);
|
let asyncRandomBytes = util.promisify(crypto.randomBytes);
|
||||||
let bytes = await asyncRandomBytes(256);
|
let bytes = await asyncRandomBytes(256);
|
||||||
controllerConfig.set("controller.auth_secret", bytes.toString("base64"));
|
controllerConfig.set("controller.auth_secret", bytes.toString("base64"));
|
||||||
await lib.safeOutputFile(
|
await controllerConfig.save();
|
||||||
controllerConfigPath, JSON.stringify(controllerConfig, null, "\t")
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (command === "config") {
|
if (command === "config") {
|
||||||
await lib.handleConfigCommand(
|
await lib.handleConfigCommand(args, controllerConfig);
|
||||||
args, controllerConfig, controllerConfigPath
|
|
||||||
);
|
|
||||||
|
|
||||||
} else if (command === "bootstrap") {
|
} else if (command === "bootstrap") {
|
||||||
await handleBootstrapCommand(args, controllerConfig);
|
await handleBootstrapCommand(args, controllerConfig);
|
||||||
@@ -386,7 +379,6 @@ async function initialize(): Promise<InitializeParameters> {
|
|||||||
args,
|
args,
|
||||||
clusterLogger,
|
clusterLogger,
|
||||||
pluginInfos,
|
pluginInfos,
|
||||||
controllerConfigPath,
|
|
||||||
controllerConfig,
|
controllerConfig,
|
||||||
shouldRun,
|
shouldRun,
|
||||||
};
|
};
|
||||||
@@ -402,17 +394,15 @@ async function startup() {
|
|||||||
shouldRun,
|
shouldRun,
|
||||||
clusterLogger,
|
clusterLogger,
|
||||||
pluginInfos,
|
pluginInfos,
|
||||||
controllerConfigPath,
|
|
||||||
controllerConfig,
|
controllerConfig,
|
||||||
} = await initialize();
|
} = await initialize();
|
||||||
if (!shouldRun || !pluginInfos || !controllerConfigPath || !controllerConfig) {
|
if (!shouldRun || !pluginInfos || !controllerConfig) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
controller = new Controller(
|
controller = new Controller(
|
||||||
clusterLogger,
|
clusterLogger,
|
||||||
pluginInfos,
|
pluginInfos,
|
||||||
controllerConfigPath,
|
|
||||||
controllerConfig,
|
controllerConfig,
|
||||||
Boolean(args.canRestart),
|
Boolean(args.canRestart),
|
||||||
Boolean(args.recovery),
|
Boolean(args.recovery),
|
||||||
|
|||||||
@@ -50,7 +50,6 @@ export default class Controller {
|
|||||||
clusterLogger: winston.Logger;
|
clusterLogger: winston.Logger;
|
||||||
/** Array of plugin info objects for known plugins */
|
/** Array of plugin info objects for known plugins */
|
||||||
pluginInfos: lib.PluginNodeEnvInfo[];
|
pluginInfos: lib.PluginNodeEnvInfo[];
|
||||||
configPath: string;
|
|
||||||
/** Controller config. */
|
/** Controller config. */
|
||||||
config: lib.ControllerConfig;
|
config: lib.ControllerConfig;
|
||||||
app: Application;
|
app: Application;
|
||||||
@@ -155,7 +154,6 @@ export default class Controller {
|
|||||||
constructor(
|
constructor(
|
||||||
clusterLogger: winston.Logger,
|
clusterLogger: winston.Logger,
|
||||||
pluginInfos: lib.PluginNodeEnvInfo[],
|
pluginInfos: lib.PluginNodeEnvInfo[],
|
||||||
configPath: string,
|
|
||||||
config: lib.ControllerConfig,
|
config: lib.ControllerConfig,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -184,7 +182,6 @@ export default class Controller {
|
|||||||
) {
|
) {
|
||||||
this.clusterLogger = clusterLogger;
|
this.clusterLogger = clusterLogger;
|
||||||
this.pluginInfos = pluginInfos;
|
this.pluginInfos = pluginInfos;
|
||||||
this.configPath = configPath;
|
|
||||||
this.config = config;
|
this.config = config;
|
||||||
|
|
||||||
this.app = express();
|
this.app = express();
|
||||||
@@ -584,12 +581,8 @@ export default class Controller {
|
|||||||
});
|
});
|
||||||
|
|
||||||
private async _saveDataInternal() {
|
private async _saveDataInternal() {
|
||||||
if (this.config.dirty) {
|
|
||||||
this.config.dirty = false;
|
|
||||||
await lib.safeOutputFile(this.configPath, JSON.stringify(this.config, null, "\t"));
|
|
||||||
}
|
|
||||||
|
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
|
this.config.save(),
|
||||||
this.systems.save(),
|
this.systems.save(),
|
||||||
this.hosts.save(),
|
this.hosts.save(),
|
||||||
this.instances.save(),
|
this.instances.save(),
|
||||||
|
|||||||
+6
-6
@@ -209,19 +209,19 @@ async function startControl() {
|
|||||||
.parse() as CtlArguments
|
.parse() as CtlArguments
|
||||||
;
|
;
|
||||||
|
|
||||||
logger.verbose(`Loading config from ${args.config}`);
|
|
||||||
let controlConfig;
|
let controlConfig;
|
||||||
|
const controlConfigPath = args.config;
|
||||||
|
logger.verbose(`Loading config from ${controlConfigPath}`);
|
||||||
try {
|
try {
|
||||||
const jsonConfig = JSON.parse(await fs.readFile(args.config, "utf8"));
|
controlConfig = await lib.ControlConfig.fromFile("control", controlConfigPath);
|
||||||
controlConfig = lib.ControlConfig.fromJSON(jsonConfig, "control");
|
|
||||||
|
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
if (err.code === "ENOENT") {
|
if (err.code === "ENOENT") {
|
||||||
logger.verbose("Config not found, initializing new config");
|
logger.verbose("Config not found, initializing new config");
|
||||||
controlConfig = new lib.ControlConfig("control");
|
controlConfig = new lib.ControlConfig("control", undefined, controlConfigPath);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
throw new lib.StartupError(`Failed to load ${args.config}: ${err.message}`);
|
throw new lib.StartupError(`Failed to load ${args.config}: ${err.stack ?? err.message ?? err}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -232,7 +232,7 @@ async function startControl() {
|
|||||||
|
|
||||||
// Handle the control-config command before trying to connect.
|
// Handle the control-config command before trying to connect.
|
||||||
if (args._[0] === "control-config") {
|
if (args._[0] === "control-config") {
|
||||||
await lib.handleConfigCommand(args, controlConfig, args.config);
|
await lib.handleConfigCommand(args, controlConfig);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -167,26 +167,26 @@ async function startHost() {
|
|||||||
lib.registerPluginMessages(pluginInfos);
|
lib.registerPluginMessages(pluginInfos);
|
||||||
lib.addPluginConfigFields(pluginInfos);
|
lib.addPluginConfigFields(pluginInfos);
|
||||||
|
|
||||||
logger.info(`Loading config from ${args.config}`);
|
|
||||||
let hostConfig;
|
let hostConfig;
|
||||||
|
const hostConfigPath = args.config;
|
||||||
|
logger.info(`Loading config from ${hostConfigPath}`);
|
||||||
try {
|
try {
|
||||||
const jsonConfig = JSON.parse(await fs.readFile(args.config, "utf8"));
|
hostConfig = await lib.HostConfig.fromFile("host", hostConfigPath);
|
||||||
hostConfig = lib.HostConfig.fromJSON(jsonConfig, "host");
|
|
||||||
|
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
if (err.code === "ENOENT") {
|
if (err.code === "ENOENT") {
|
||||||
logger.info("Config not found, initializing new config");
|
logger.info("Config not found, initializing new config");
|
||||||
hostConfig = new lib.HostConfig("host");
|
hostConfig = new lib.HostConfig("host", undefined, hostConfigPath);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
throw new lib.StartupError(`Failed to load ${args.config}: ${err.message}`);
|
throw new lib.StartupError(`Failed to load ${args.config}: ${err.stack ?? err.message ?? err}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
hostConfig.set("host.version", version); // Allows tracking last loaded version
|
hostConfig.set("host.version", version); // Allows tracking last loaded version
|
||||||
|
|
||||||
if (command === "config") {
|
if (command === "config") {
|
||||||
await lib.handleConfigCommand(args, hostConfig, args.config);
|
await lib.handleConfigCommand(args, hostConfig);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -229,7 +229,6 @@ async function startHost() {
|
|||||||
let hostConnector = new HostConnector(hostConfig, tlsCa, pluginInfos);
|
let hostConnector = new HostConnector(hostConfig, tlsCa, pluginInfos);
|
||||||
host = new Host(
|
host = new Host(
|
||||||
hostConnector,
|
hostConnector,
|
||||||
args.config,
|
|
||||||
hostConfig,
|
hostConfig,
|
||||||
tlsCa,
|
tlsCa,
|
||||||
pluginInfos,
|
pluginInfos,
|
||||||
|
|||||||
+18
-27
@@ -43,19 +43,20 @@ async function discoverInstances(instancesDir: string) {
|
|||||||
let instanceInfos = new Map<number, { path: string, config: lib.InstanceConfig }>();
|
let instanceInfos = new Map<number, { path: string, config: lib.InstanceConfig }>();
|
||||||
for (let entry of await fs.readdir(instancesDir, { withFileTypes: true })) {
|
for (let entry of await fs.readdir(instancesDir, { withFileTypes: true })) {
|
||||||
if (entry.isDirectory()) {
|
if (entry.isDirectory()) {
|
||||||
let instanceConfig = new lib.InstanceConfig("host");
|
let instanceConfig = new lib.InstanceConfig("host", Host.instanceConfigWarning);
|
||||||
let configPath = path.join(instancesDir, entry.name, "instance.json");
|
const instancePath = path.join(instancesDir, entry.name);
|
||||||
|
const configPath = path.join(instancePath, "instance.json");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const jsonConfig = JSON.parse(await fs.readFile(configPath, "utf8"));
|
instanceConfig = await lib.InstanceConfig.fromFile("host", configPath);
|
||||||
instanceConfig = lib.InstanceConfig.fromJSON(jsonConfig, "host");
|
instanceConfig.update(Host.instanceConfigWarning, false); // File might be missing the warning
|
||||||
|
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
if (err.code === "ENOENT") {
|
if (err.code === "ENOENT") {
|
||||||
continue; // Ignore folders without config.json
|
continue; // Ignore folders without config.json
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.error(`Error occured while parsing ${configPath}: ${err.message}`);
|
logger.error(`Error occurred while parsing ${configPath}: ${err.message}`);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,7 +65,6 @@ async function discoverInstances(instancesDir: string) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let instancePath = path.join(instancesDir, entry.name);
|
|
||||||
logger.verbose(`found instance ${instanceConfig.get("instance.name")} in ${instancePath}`);
|
logger.verbose(`found instance ${instanceConfig.get("instance.name")} in ${instancePath}`);
|
||||||
instanceInfos.set(instanceConfig.get("instance.id"), {
|
instanceInfos.set(instanceConfig.get("instance.id"), {
|
||||||
path: instancePath,
|
path: instancePath,
|
||||||
@@ -258,7 +258,6 @@ export default class Host extends lib.Link {
|
|||||||
*/
|
*/
|
||||||
tlsCa?: string;
|
tlsCa?: string;
|
||||||
pluginInfos: lib.PluginNodeEnvInfo[];
|
pluginInfos: lib.PluginNodeEnvInfo[];
|
||||||
configPath: string;
|
|
||||||
config: lib.HostConfig;
|
config: lib.HostConfig;
|
||||||
|
|
||||||
/** Mapping of plugin name to loaded plugin */
|
/** Mapping of plugin name to loaded plugin */
|
||||||
@@ -279,6 +278,10 @@ export default class Host extends lib.Link {
|
|||||||
_disconnecting = false;
|
_disconnecting = false;
|
||||||
_shuttingDown = false;
|
_shuttingDown = false;
|
||||||
|
|
||||||
|
static instanceConfigWarning = {
|
||||||
|
"_warning": "Changes to this file will be overwritten by the controller's copy.",
|
||||||
|
};
|
||||||
|
|
||||||
static async bootstrap(hostConfig: lib.HostConfig) {
|
static async bootstrap(hostConfig: lib.HostConfig) {
|
||||||
const modsDirectory = hostConfig.get("host.mods_directory");
|
const modsDirectory = hostConfig.get("host.mods_directory");
|
||||||
await fs.ensureDir(modsDirectory);
|
await fs.ensureDir(modsDirectory);
|
||||||
@@ -289,7 +292,6 @@ export default class Host extends lib.Link {
|
|||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
connector: HostConnector,
|
connector: HostConnector,
|
||||||
hostConfigPath: string,
|
|
||||||
hostConfig: lib.HostConfig,
|
hostConfig: lib.HostConfig,
|
||||||
tlsCa: string | undefined,
|
tlsCa: string | undefined,
|
||||||
pluginInfos: lib.PluginNodeEnvInfo[],
|
pluginInfos: lib.PluginNodeEnvInfo[],
|
||||||
@@ -309,7 +311,6 @@ export default class Host extends lib.Link {
|
|||||||
this.tlsCa = tlsCa;
|
this.tlsCa = tlsCa;
|
||||||
|
|
||||||
this.pluginInfos = pluginInfos;
|
this.pluginInfos = pluginInfos;
|
||||||
this.configPath = hostConfigPath;
|
|
||||||
this.config = hostConfig;
|
this.config = hostConfig;
|
||||||
|
|
||||||
this.config.on("fieldChanged", (name, curr, prev) => {
|
this.config.on("fieldChanged", (name, curr, prev) => {
|
||||||
@@ -661,10 +662,11 @@ export default class Host extends lib.Link {
|
|||||||
instanceInfo.config.update(config, true, "controller");
|
instanceInfo.config.update(config, true, "controller");
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
let instanceConfig = new lib.InstanceConfig("host");
|
let instanceConfig = new lib.InstanceConfig("host", Host.instanceConfigWarning);
|
||||||
instanceConfig.update(config, false, "controller");
|
instanceConfig.update(config, false, "controller");
|
||||||
|
|
||||||
let instanceDir = await this._createNewInstanceDir(instanceConfig.get("instance.name"));
|
let instanceDir = await this._createNewInstanceDir(instanceConfig.get("instance.name"));
|
||||||
|
instanceConfig.filepath = path.join(instanceDir, "instance.json");
|
||||||
|
|
||||||
logger.info(`Creating ${instanceDir}`);
|
logger.info(`Creating ${instanceDir}`);
|
||||||
await Instance.populate_folders(instanceDir);
|
await Instance.populate_folders(instanceDir);
|
||||||
@@ -699,18 +701,7 @@ export default class Host extends lib.Link {
|
|||||||
await this.sendSaveListUpdate(instanceId, path.join(instanceInfo.path, "saves"));
|
await this.sendSaveListUpdate(instanceId, path.join(instanceInfo.path, "saves"));
|
||||||
|
|
||||||
// save a copy of the instance config
|
// save a copy of the instance config
|
||||||
await this.saveInstanceConfig(instanceInfo);
|
await instanceInfo.config.save();
|
||||||
}
|
|
||||||
|
|
||||||
async saveInstanceConfig(instanceInfo: { path: string, config: lib.InstanceConfig }) {
|
|
||||||
let warnedOutput = {
|
|
||||||
_warning: "Changes to this file will be overwritten by the controller's copy.",
|
|
||||||
...instanceInfo.config.toJSON(),
|
|
||||||
};
|
|
||||||
await lib.safeOutputFile(
|
|
||||||
path.join(instanceInfo.path, "instance.json"),
|
|
||||||
JSON.stringify(warnedOutput, null, "\t")
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async handleInstanceUnassignInternalRequest(request: lib.InstanceUnassignInternalRequest) {
|
async handleInstanceUnassignInternalRequest(request: lib.InstanceUnassignInternalRequest) {
|
||||||
@@ -1192,11 +1183,11 @@ export default class Host extends lib.Link {
|
|||||||
}
|
}
|
||||||
|
|
||||||
logger.info("Saving config");
|
logger.info("Saving config");
|
||||||
await lib.safeOutputFile(this.configPath, JSON.stringify(this.config, null, "\t"));
|
await this.config.save();
|
||||||
for (const instanceInfo of this.instanceInfos.values()) {
|
// Save host side in case host_assigned_game_port changed in the config.
|
||||||
// Save in case host_assigned_game_port changed in the config.
|
await Promise.all(
|
||||||
await this.saveInstanceConfig(instanceInfo);
|
[...this.instanceInfos.values()].map(instanceInfo => instanceInfo.config.save())
|
||||||
}
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Clear silly interval in pidfile library.
|
// Clear silly interval in pidfile library.
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
// Configuration classes
|
// Configuration classes
|
||||||
import { Type, Static } from "@sinclair/typebox";
|
import { Type, Static } from "@sinclair/typebox";
|
||||||
import TypedEventEmitter from "../TypedEventEmitter";
|
import * as fs from "fs-extra";
|
||||||
|
|
||||||
|
import TypedEventEmitter from "../TypedEventEmitter";
|
||||||
import isDeepStrictEqual from "../is_deep_strict_equal";
|
import isDeepStrictEqual from "../is_deep_strict_equal";
|
||||||
import { basicType } from "../helpers";
|
import { basicType } from "../helpers";
|
||||||
import * as libSchema from "../schema";
|
import * as libSchema from "../schema";
|
||||||
import { StringEnum } from "../data/composites";
|
import { StringEnum } from "../data/composites";
|
||||||
|
import { safeOutputFile } from "../file_ops";
|
||||||
|
|
||||||
const ConfigLocation = StringEnum(["controller", "host", "control"]);
|
const ConfigLocation = StringEnum(["controller", "host", "control"]);
|
||||||
export type ConfigLocation = Static<typeof ConfigLocation>;
|
export type ConfigLocation = Static<typeof ConfigLocation>;
|
||||||
@@ -198,11 +200,15 @@ export class Config<
|
|||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
fields: Fields;
|
/** Defines the default access locations for the config class */
|
||||||
_unknownFields: Record<string, FieldValue> = {};
|
static defaultAccess: ConfigLocation[] = ["controller", "host", "control"];
|
||||||
|
|
||||||
|
/** Contains the fields and their values */
|
||||||
|
private fields: Fields;
|
||||||
|
private _unknownFields: Record<string, FieldValue> = {};
|
||||||
|
|
||||||
/** Set to true when a field in the config is changed. */
|
/** Set to true when a field in the config is changed. */
|
||||||
dirty = false;
|
private dirty = false;
|
||||||
/** Set to true when a 'restart required' field in the config is changed. */
|
/** Set to true when a 'restart required' field in the config is changed. */
|
||||||
restartRequired = false;
|
restartRequired = false;
|
||||||
|
|
||||||
@@ -210,14 +216,16 @@ export class Config<
|
|||||||
* Create a new instance of the given config
|
* Create a new instance of the given config
|
||||||
*
|
*
|
||||||
* @param location -
|
* @param location -
|
||||||
* Location to evaluate access for this instance from
|
* Location to evaluate access for this instance from.
|
||||||
* @param fields -
|
* @param fields -
|
||||||
* Serialized representation of fields to load.
|
* Serialized representation of fields to load.
|
||||||
|
* @param filepath -
|
||||||
|
* Filepath for the config to save to.
|
||||||
*/
|
*/
|
||||||
constructor(
|
constructor(
|
||||||
public location: ConfigLocation,
|
public location: ConfigLocation,
|
||||||
fields?: Static<typeof Config.jsonSchema>,
|
fields?: Static<typeof Config.jsonSchema>,
|
||||||
public defaultAccess: ConfigLocation[] = ["controller", "host", "control"],
|
public filepath?: string,
|
||||||
) {
|
) {
|
||||||
if (typeof location !== "string") {
|
if (typeof location !== "string") {
|
||||||
throw new Error("location must be a string");
|
throw new Error("location must be a string");
|
||||||
@@ -262,7 +270,7 @@ export class Config<
|
|||||||
|| (
|
|| (
|
||||||
(!def.credential || mode & ConfigAccess.write)
|
(!def.credential || mode & ConfigAccess.write)
|
||||||
&& (!def.readonly || mode & ConfigAccess.read)
|
&& (!def.readonly || mode & ConfigAccess.read)
|
||||||
&& !(def.access ?? this.defaultAccess).includes(location)
|
&& !(def.access ?? this.constructor.defaultAccess).includes(location)
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
if (error) {
|
if (error) {
|
||||||
@@ -295,8 +303,31 @@ export class Config<
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static jsonSchema = ConfigSchema;
|
/**
|
||||||
|
* Create config from file
|
||||||
|
*
|
||||||
|
* @param location - Location used for access control.
|
||||||
|
* @param filepath - Filepath for the config to save to.
|
||||||
|
* @returns Instance of this config
|
||||||
|
*/
|
||||||
|
static async fromFile(location: ConfigLocation, filepath: string) {
|
||||||
|
const fields = await fs.readJson(filepath, { encoding: "utf8" });
|
||||||
|
return this.fromJSON(fields, location, filepath);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Saves this config to file if there are unsaved changes */
|
||||||
|
async save() {
|
||||||
|
if (!this.dirty) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!this.filepath) {
|
||||||
|
throw new Error("Cannot save config which has no filepath");
|
||||||
|
}
|
||||||
|
await safeOutputFile(this.filepath, JSON.stringify(this, null, "\t"));
|
||||||
|
this.dirty = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static jsonSchema = ConfigSchema;
|
||||||
static validate = libSchema.compile(this.jsonSchema as any);
|
static validate = libSchema.compile(this.jsonSchema as any);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -304,11 +335,13 @@ export class Config<
|
|||||||
*
|
*
|
||||||
* @param json - Serialized config to load.
|
* @param json - Serialized config to load.
|
||||||
* @param location - Location used for access control.
|
* @param location - Location used for access control.
|
||||||
|
* @param filepath - Filepath for the config to save to.
|
||||||
* @returns Instance of this config
|
* @returns Instance of this config
|
||||||
*/
|
*/
|
||||||
static fromJSON(
|
static fromJSON(
|
||||||
json: Static<typeof this.jsonSchema>,
|
json: Static<typeof this.jsonSchema>,
|
||||||
location: ConfigLocation,
|
location: ConfigLocation,
|
||||||
|
filepath?: string,
|
||||||
) {
|
) {
|
||||||
// migrate: Pre alpha 14 config format
|
// migrate: Pre alpha 14 config format
|
||||||
if (typeof json === "object" && json !== null && json.groups instanceof Array) {
|
if (typeof json === "object" && json !== null && json.groups instanceof Array) {
|
||||||
@@ -324,7 +357,7 @@ export class Config<
|
|||||||
if (!valid) {
|
if (!valid) {
|
||||||
throw new Error("Invalid config");
|
throw new Error("Invalid config");
|
||||||
}
|
}
|
||||||
return new this(location, json);
|
return new this(location, json, filepath);
|
||||||
}
|
}
|
||||||
|
|
||||||
toJSON() {
|
toJSON() {
|
||||||
|
|||||||
@@ -3,6 +3,9 @@ import * as classes from "./classes";
|
|||||||
import type { PluginNodeEnvInfo, PluginWebpackEnvInfo } from "../plugin";
|
import type { PluginNodeEnvInfo, PluginWebpackEnvInfo } from "../plugin";
|
||||||
import { Static } from "@sinclair/typebox";
|
import { Static } from "@sinclair/typebox";
|
||||||
|
|
||||||
|
type configFromJSON<T> = (...args: Parameters<typeof classes.Config.fromJSON>) => T;
|
||||||
|
type configFromFile<T> = (...args: Parameters<typeof classes.Config.fromFile>) => Promise<T>;
|
||||||
|
|
||||||
export interface ControllerConfigFields {
|
export interface ControllerConfigFields {
|
||||||
"controller.version": string;
|
"controller.version": string;
|
||||||
"controller.name": string;
|
"controller.name": string;
|
||||||
@@ -39,7 +42,8 @@ export interface ControllerConfigFields {
|
|||||||
* @extends classes.Config
|
* @extends classes.Config
|
||||||
*/
|
*/
|
||||||
export class ControllerConfig extends classes.Config<ControllerConfigFields> {
|
export class ControllerConfig extends classes.Config<ControllerConfigFields> {
|
||||||
declare static fromJSON: (json: classes.ConfigSchema, location: classes.ConfigLocation) => ControllerConfig;
|
declare static fromJSON: configFromJSON<ControllerConfig>;
|
||||||
|
declare static fromFile: configFromFile<ControllerConfig>;
|
||||||
static migrations(config: Static<typeof this.jsonSchema>) {
|
static migrations(config: Static<typeof this.jsonSchema>) {
|
||||||
if (config.hasOwnProperty("controller.external_address")) {
|
if (config.hasOwnProperty("controller.external_address")) {
|
||||||
config["controller.public_url"] = config["controller.external_address"];
|
config["controller.public_url"] = config["controller.external_address"];
|
||||||
@@ -270,7 +274,8 @@ export interface HostConfigFields {
|
|||||||
* @extends classes.Config
|
* @extends classes.Config
|
||||||
*/
|
*/
|
||||||
export class HostConfig extends classes.Config<HostConfigFields> {
|
export class HostConfig extends classes.Config<HostConfigFields> {
|
||||||
declare static fromJSON: (json: classes.ConfigSchema, location: classes.ConfigLocation) => HostConfig;
|
declare static fromJSON: configFromJSON<HostConfig>;
|
||||||
|
declare static fromFile: configFromFile<HostConfig>;
|
||||||
static fieldDefinitions: classes.ConfigDefs<HostConfigFields> = {
|
static fieldDefinitions: classes.ConfigDefs<HostConfigFields> = {
|
||||||
"host.version": {
|
"host.version": {
|
||||||
type: "string",
|
type: "string",
|
||||||
@@ -423,7 +428,8 @@ export interface InstanceConfigFields {
|
|||||||
* @extends classes.Config
|
* @extends classes.Config
|
||||||
*/
|
*/
|
||||||
export class InstanceConfig extends classes.Config<InstanceConfigFields> {
|
export class InstanceConfig extends classes.Config<InstanceConfigFields> {
|
||||||
declare static fromJSON: (json: classes.ConfigSchema, location: classes.ConfigLocation) => InstanceConfig;
|
declare static fromJSON: configFromJSON<InstanceConfig>;
|
||||||
|
declare static fromFile: configFromFile<InstanceConfig>;
|
||||||
static migrations(config: Static<typeof this.jsonSchema>) {
|
static migrations(config: Static<typeof this.jsonSchema>) {
|
||||||
function boolToEnableDisable(name: string) {
|
function boolToEnableDisable(name: string) {
|
||||||
if (config.hasOwnProperty(name) && typeof config[name] === "boolean") {
|
if (config.hasOwnProperty(name) && typeof config[name] === "boolean") {
|
||||||
@@ -617,7 +623,8 @@ export interface ControlConfigFields {
|
|||||||
* @extends classes.Config
|
* @extends classes.Config
|
||||||
*/
|
*/
|
||||||
export class ControlConfig extends classes.Config<ControlConfigFields> {
|
export class ControlConfig extends classes.Config<ControlConfigFields> {
|
||||||
declare static fromJSON: (json: classes.ConfigSchema, location: classes.ConfigLocation) => ControlConfig;
|
declare static fromJSON: configFromJSON<ControlConfig>;
|
||||||
|
declare static fromFile: configFromFile<ControlConfig>;
|
||||||
static fieldDefinitions: classes.ConfigDefs<ControlConfigFields> = {
|
static fieldDefinitions: classes.ConfigDefs<ControlConfigFields> = {
|
||||||
"control.controller_url": {
|
"control.controller_url": {
|
||||||
description: "URL to connect to the controller at",
|
description: "URL to connect to the controller at",
|
||||||
|
|||||||
@@ -135,7 +135,6 @@ export function configCommand(yargs: any) {
|
|||||||
export async function handleConfigCommand(
|
export async function handleConfigCommand(
|
||||||
args: Record<string, unknown>,
|
args: Record<string, unknown>,
|
||||||
instance: libConfig.Config<any>,
|
instance: libConfig.Config<any>,
|
||||||
configPath: string
|
|
||||||
) {
|
) {
|
||||||
let command = (args._ as string[])[1];
|
let command = (args._ as string[])[1];
|
||||||
|
|
||||||
@@ -165,7 +164,7 @@ export async function handleConfigCommand(
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
instance.set(args.field as string, args.value);
|
instance.set(args.field as string, args.value);
|
||||||
await libFileOps.safeOutputFile(configPath, JSON.stringify(instance, null, "\t"));
|
await instance.save();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof libConfig.InvalidField || err instanceof libConfig.InvalidValue) {
|
if (err instanceof libConfig.InvalidField || err instanceof libConfig.InvalidValue) {
|
||||||
logger.error(err.message);
|
logger.error(err.message);
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
"./src/schema.ts",
|
"./src/schema.ts",
|
||||||
"./src/subscriptions.ts",
|
"./src/subscriptions.ts",
|
||||||
"./src/zip_ops.ts",
|
"./src/zip_ops.ts",
|
||||||
|
"./src/file_ops.ts",
|
||||||
"./src/ExponentialBackoff.ts",
|
"./src/ExponentialBackoff.ts",
|
||||||
"./src/RateLimiter.ts",
|
"./src/RateLimiter.ts",
|
||||||
"./src/TypedEventEmitter.ts",
|
"./src/TypedEventEmitter.ts",
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ describe("controller/src/Controller", function() {
|
|||||||
/** @type {Controller} */
|
/** @type {Controller} */
|
||||||
let controller, mockInstanceConfig;
|
let controller, mockInstanceConfig;
|
||||||
before(async function() {
|
before(async function() {
|
||||||
controller = new Controller({}, [], "", new ControllerConfig("controller"));
|
controller = new Controller({}, [], new ControllerConfig("controller"));
|
||||||
mockInstanceConfig = new MockInstanceConfig(new Map([
|
mockInstanceConfig = new MockInstanceConfig(new Map([
|
||||||
["instance.id", 100], ["instance.name", "test"], ["factorio.settings", []],
|
["instance.id", 100], ["instance.name", "test"], ["factorio.settings", []],
|
||||||
]));
|
]));
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ describe("controller/src/WsServer", function() {
|
|||||||
before(function() {
|
before(function() {
|
||||||
let config = new ControllerConfig("controller");
|
let config = new ControllerConfig("controller");
|
||||||
config.set("controller.trusted_proxies", "127.0.0.0/8, ::1");
|
config.set("controller.trusted_proxies", "127.0.0.0/8, ::1");
|
||||||
let controller = new Controller({}, [], "", config);
|
let controller = new Controller({}, [], config);
|
||||||
wsServer = controller.wsServer;
|
wsServer = controller.wsServer;
|
||||||
});
|
});
|
||||||
describe(".remoteAddr()", function() {
|
describe(".remoteAddr()", function() {
|
||||||
|
|||||||
+8
-6
@@ -10,13 +10,15 @@ const { _discoverInstances } = require("@clusterio/host/dist/node/src/Host");
|
|||||||
describe("Host testing", function() {
|
describe("Host testing", function() {
|
||||||
describe("discoverInstances()", function() {
|
describe("discoverInstances()", function() {
|
||||||
it("should discover test instance", async function() {
|
it("should discover test instance", async function() {
|
||||||
let instancePath = path.join("test", "file", "instances");
|
const instancePath = path.join("test", "file", "instances");
|
||||||
let instanceInfos = await _discoverInstances(instancePath);
|
const instanceInfos = await _discoverInstances(instancePath);
|
||||||
|
|
||||||
let referenceConfig = new lib.InstanceConfig("host");
|
const configPath = path.join(instancePath, "test", "instance.json");
|
||||||
referenceConfig.set("instance.id", 1);
|
const referenceConfig = new lib.InstanceConfig("host", {
|
||||||
referenceConfig.set("instance.name", "test");
|
...Host.instanceConfigWarning,
|
||||||
referenceConfig.dirty = false;
|
"instance.id": 1,
|
||||||
|
"instance.name": "test",
|
||||||
|
}, configPath);
|
||||||
|
|
||||||
assert.deepEqual(instanceInfos, new Map([
|
assert.deepEqual(instanceInfos, new Map([
|
||||||
[1, {
|
[1, {
|
||||||
|
|||||||
+37
-20
@@ -175,6 +175,16 @@ async function exec(command, options = {}) {
|
|||||||
return await util.promisify(child_process.exec)(command, options);
|
return await util.promisify(child_process.exec)(command, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function execController(...args) {
|
||||||
|
args[0] = `node --enable-source-maps ../../packages/controller ${args[0]}`;
|
||||||
|
return await exec(...args);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function execHost(...args) {
|
||||||
|
args[0] = `node --enable-source-maps ../../packages/host ${args[0]}`;
|
||||||
|
return await exec(...args);
|
||||||
|
}
|
||||||
|
|
||||||
async function execCtl(...args) {
|
async function execCtl(...args) {
|
||||||
args[0] = `node --enable-source-maps ../../packages/ctl ${args[0]}`;
|
args[0] = `node --enable-source-maps ../../packages/ctl ${args[0]}`;
|
||||||
return await exec(...args);
|
return await exec(...args);
|
||||||
@@ -191,8 +201,15 @@ function getControl() {
|
|||||||
function spawn(name, cmd, waitFor) {
|
function spawn(name, cmd, waitFor) {
|
||||||
// eslint-disable-next-line node/no-process-env
|
// eslint-disable-next-line node/no-process-env
|
||||||
const silent = process.env.SILENT_TEST;
|
const silent = process.env.SILENT_TEST;
|
||||||
|
const bootstrap = !controllerProcess || !hostProcess;
|
||||||
|
function log(...args) {
|
||||||
|
if (!silent || bootstrap) {
|
||||||
|
console.log(...args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
console.log(cmd);
|
log(cmd);
|
||||||
let parts = cmd.split(" ");
|
let parts = cmd.split(" ");
|
||||||
let process = child_process.spawn(parts[0], parts.slice(1), { cwd: path.join("temp", "test") });
|
let process = child_process.spawn(parts[0], parts.slice(1), { cwd: path.join("temp", "test") });
|
||||||
let stdout = new LineSplitter({ readableObjectMode: true });
|
let stdout = new LineSplitter({ readableObjectMode: true });
|
||||||
@@ -206,10 +223,10 @@ function spawn(name, cmd, waitFor) {
|
|||||||
}
|
}
|
||||||
resolve(process);
|
resolve(process);
|
||||||
}
|
}
|
||||||
console.log(name, line);
|
log(name, line);
|
||||||
};
|
};
|
||||||
let onDataErr = line => {
|
let onDataErr = line => {
|
||||||
console.log(name, line.toString("utf8"));
|
log(name, line.toString("utf8"));
|
||||||
};
|
};
|
||||||
stdout.on("data", onDataOut);
|
stdout.on("data", onDataOut);
|
||||||
stderr.on("data", onDataErr);
|
stderr.on("data", onDataErr);
|
||||||
@@ -265,33 +282,33 @@ before(async function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
console.log("Setting Controller Config");
|
console.log("Setting Controller Config");
|
||||||
await exec("node ../../packages/controller config set controller.auth_secret TestSecretDoNotUse");
|
await execController("config set controller.auth_secret TestSecretDoNotUse");
|
||||||
await exec("node ../../packages/controller config set controller.http_port 8880");
|
await execController("config set controller.http_port 8880");
|
||||||
await exec("node ../../packages/controller config set controller.https_port 4443");
|
await execController("config set controller.https_port 4443");
|
||||||
await exec("node ../../packages/controller config set controller.heartbeat_interval 0.25");
|
await execController("config set controller.heartbeat_interval 0.25");
|
||||||
await exec("node ../../packages/controller config set controller.session_timeout 2");
|
await execController("config set controller.session_timeout 2");
|
||||||
await exec("node ../../packages/controller config set controller.tls_certificate ../../test/file/tls/cert.pem");
|
await execController("config set controller.tls_certificate ../../test/file/tls/cert.pem");
|
||||||
await exec("node ../../packages/controller config set controller.tls_private_key ../../test/file/tls/key.pem");
|
await execController("config set controller.tls_private_key ../../test/file/tls/key.pem");
|
||||||
|
|
||||||
console.log("Setting Controller Plugins");
|
console.log("Setting Controller Plugins");
|
||||||
await exec("node ../../packages/ctl plugin add ../../plugins/global_chat");
|
await execCtl("plugin add ../../plugins/global_chat");
|
||||||
await exec("node ../../packages/ctl plugin add ../../plugins/research_sync");
|
await execCtl("plugin add ../../plugins/research_sync");
|
||||||
await exec("node ../../packages/ctl plugin add ../../plugins/statistics_exporter");
|
await execCtl("plugin add ../../plugins/statistics_exporter");
|
||||||
await exec("node ../../packages/ctl plugin add ../../plugins/subspace_storage");
|
await execCtl("plugin add ../../plugins/subspace_storage");
|
||||||
await exec("node ../../packages/ctl plugin add ../../plugins/player_auth");
|
await execCtl("plugin add ../../plugins/player_auth");
|
||||||
|
|
||||||
console.log("Bootstrapping");
|
console.log("Bootstrapping");
|
||||||
await exec("node ../../packages/controller bootstrap create-admin test");
|
await execController("bootstrap create-admin test");
|
||||||
await exec("node ../../packages/controller bootstrap create-ctl-config test");
|
await execController("bootstrap create-ctl-config test");
|
||||||
await exec("node ../../packages/ctl control-config set control.tls_ca ../../test/file/tls/cert.pem");
|
await execCtl("control-config set control.tls_ca ../../test/file/tls/cert.pem");
|
||||||
|
|
||||||
controllerProcess = await spawnNode("controller:", "../../packages/controller run", /Started controller/);
|
controllerProcess = await spawnNode("controller:", "../../packages/controller run", /Started controller/);
|
||||||
|
|
||||||
await execCtl("host create-config --id 4 --name host --generate-token");
|
await execCtl("host create-config --id 4 --name host --generate-token");
|
||||||
|
|
||||||
const relativeFactorioDir = path.isAbsolute(factorioDir) ? factorioDir : path.join("..", "..", factorioDir);
|
const relativeFactorioDir = path.isAbsolute(factorioDir) ? factorioDir : path.join("..", "..", factorioDir);
|
||||||
await exec(`node ../../packages/host config set host.factorio_directory ${relativeFactorioDir}`);
|
await execHost(`config set host.factorio_directory ${relativeFactorioDir}`);
|
||||||
await exec("node ../../packages/host config set host.tls_ca ../../test/file/tls/cert.pem");
|
await execHost("config set host.tls_ca ../../test/file/tls/cert.pem");
|
||||||
|
|
||||||
hostProcess = await spawnNode("host:", "../../packages/host run", /Started host/);
|
hostProcess = await spawnNode("host:", "../../packages/host run", /Started host/);
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ function connectHost(controller, hostId, plugins) {
|
|||||||
controller.wsServer.hostConnections.set(hostId, hostConnection);
|
controller.wsServer.hostConnections.set(hostId, hostConnection);
|
||||||
const hostConfig = new lib.HostConfig("host");
|
const hostConfig = new lib.HostConfig("host");
|
||||||
hostConfig.set("host.id", hostId);
|
hostConfig.set("host.id", hostId);
|
||||||
return new Host(hostSide, "invalid", hostConfig, undefined, []);
|
return new Host(hostSide, hostConfig, undefined, []);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -107,12 +107,7 @@ describe("Integration of link routing", function() {
|
|||||||
let controlB;
|
let controlB;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
controller = new Controller(
|
controller = new Controller({}, [], new lib.ControllerConfig("controller"));
|
||||||
{},
|
|
||||||
[],
|
|
||||||
"invalid",
|
|
||||||
new lib.ControllerConfig("controller")
|
|
||||||
);
|
|
||||||
hostA = connectHost(controller, 10, []);
|
hostA = connectHost(controller, 10, []);
|
||||||
hostB = connectHost(controller, 20, []);
|
hostB = connectHost(controller, 20, []);
|
||||||
instanceA1 = connectInstance(controller, hostA, 11);
|
instanceA1 = connectInstance(controller, hostA, 11);
|
||||||
|
|||||||
+169
-4
@@ -1,5 +1,7 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
const lib = require("@clusterio/lib");
|
const lib = require("@clusterio/lib");
|
||||||
|
const fs = require("fs-extra");
|
||||||
|
const path = require("path");
|
||||||
const assert = require("assert").strict;
|
const assert = require("assert").strict;
|
||||||
const CA = lib.ConfigAccess;
|
const CA = lib.ConfigAccess;
|
||||||
|
|
||||||
@@ -7,6 +9,8 @@ const CA = lib.ConfigAccess;
|
|||||||
describe("lib/config/classes", function() {
|
describe("lib/config/classes", function() {
|
||||||
describe("Config", function() {
|
describe("Config", function() {
|
||||||
class TestConfig extends lib.Config {
|
class TestConfig extends lib.Config {
|
||||||
|
static defaultAccess = ["local", "remote"];
|
||||||
|
|
||||||
static migrations(config) {
|
static migrations(config) {
|
||||||
if (config.hasOwnProperty("test.migration")) {
|
if (config.hasOwnProperty("test.migration")) {
|
||||||
config["alpha.foo"] = config["test.migration"];
|
config["alpha.foo"] = config["test.migration"];
|
||||||
@@ -38,10 +42,6 @@ describe("lib/config/classes", function() {
|
|||||||
"test.cred": { credential: ["local"] },
|
"test.cred": { credential: ["local"] },
|
||||||
"test.rdo": { readonly: ["local"] },
|
"test.rdo": { readonly: ["local"] },
|
||||||
};
|
};
|
||||||
|
|
||||||
constructor(location, fields) {
|
|
||||||
super(location, fields, ["local", "remote"]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("constructor", function() {
|
describe("constructor", function() {
|
||||||
@@ -76,6 +76,10 @@ describe("lib/config/classes", function() {
|
|||||||
assert.equal(config.get("alpha.foo"), "foo");
|
assert.equal(config.get("alpha.foo"), "foo");
|
||||||
assert.equal(config.fields["test.migration"], undefined);
|
assert.equal(config.fields["test.migration"], undefined);
|
||||||
});
|
});
|
||||||
|
it("should accept a filepath", function() {
|
||||||
|
const testInstance = new TestConfig("local", undefined, "filepath");
|
||||||
|
assert.equal(testInstance.filepath, "filepath");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe(".toJSON()", function() {
|
describe(".toJSON()", function() {
|
||||||
@@ -187,6 +191,167 @@ describe("lib/config/classes", function() {
|
|||||||
assert.equal(testInstance.fields["test.priv"], undefined);
|
assert.equal(testInstance.fields["test.priv"], undefined);
|
||||||
assert.equal(testInstance.fields["test.func"], 42);
|
assert.equal(testInstance.fields["test.func"], 42);
|
||||||
});
|
});
|
||||||
|
it("should accept a filepath", function() {
|
||||||
|
const testInstance = TestConfig.fromJSON({}, "local", "filepath");
|
||||||
|
assert.equal(testInstance.filepath, "filepath");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("static .fromFile", function() {
|
||||||
|
const filepath = path.join("temp", "test", "config_test.json");
|
||||||
|
it("should throw for file and json errors", async function() {
|
||||||
|
await fs.writeFile(filepath, "abc");
|
||||||
|
assert.rejects(
|
||||||
|
TestConfig.fromFile("local", filepath),
|
||||||
|
new Error(
|
||||||
|
`${filepath}: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data`
|
||||||
|
)
|
||||||
|
);
|
||||||
|
assert.rejects(
|
||||||
|
TestConfig.fromFile("local", `${filepath}.notExist`),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("should throw on incorrect input passed", async function() {
|
||||||
|
await fs.writeFile(filepath, "null");
|
||||||
|
await assert.rejects(
|
||||||
|
TestConfig.fromFile("local", filepath),
|
||||||
|
new Error("Invalid config")
|
||||||
|
);
|
||||||
|
await fs.writeFile(filepath, "undefined");
|
||||||
|
assert.rejects(
|
||||||
|
TestConfig.fromFile("local", filepath),
|
||||||
|
new Error("Invalid config")
|
||||||
|
);
|
||||||
|
await fs.writeFile(filepath, "[]");
|
||||||
|
assert.rejects(
|
||||||
|
TestConfig.fromFile("local", filepath),
|
||||||
|
new Error("Invalid config")
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("should load defaults for missing fields", async function() {
|
||||||
|
await fs.writeJSON(filepath, {
|
||||||
|
"alpha.foo": "a",
|
||||||
|
"test.enum": "a",
|
||||||
|
});
|
||||||
|
const testInstance = await TestConfig.fromFile("local", filepath);
|
||||||
|
assert.equal(testInstance.get("alpha.foo"), "a");
|
||||||
|
assert.deepEqual(testInstance.get("beta.bar"), {});
|
||||||
|
assert.equal(testInstance.get("test.enum"), "a");
|
||||||
|
assert.equal(testInstance.get("test.test"), null);
|
||||||
|
assert.equal(testInstance.get("test.func"), 42);
|
||||||
|
assert.equal(testInstance.filepath, filepath);
|
||||||
|
});
|
||||||
|
it("should load fields", async function() {
|
||||||
|
await fs.writeJSON(filepath, {
|
||||||
|
"test.enum": "c",
|
||||||
|
"test.test": "blah",
|
||||||
|
"test.func": 22,
|
||||||
|
"test.bool": null,
|
||||||
|
"test.json": { valid: true },
|
||||||
|
"test.priv": "bar",
|
||||||
|
});
|
||||||
|
const testInstance = await TestConfig.fromFile("local", filepath);
|
||||||
|
assert.equal(testInstance.get("test.enum"), "c");
|
||||||
|
assert.equal(testInstance.get("test.test"), "blah");
|
||||||
|
assert.equal(testInstance.get("test.func"), 22);
|
||||||
|
assert.equal(testInstance.get("test.bool"), null);
|
||||||
|
assert.equal(testInstance.get("test.priv"), "bar");
|
||||||
|
assert.deepEqual(testInstance.get("test.json"), { valid: true });
|
||||||
|
assert.equal(testInstance.filepath, filepath);
|
||||||
|
});
|
||||||
|
it("should preserve unknown fields", async function() {
|
||||||
|
const testFields = {
|
||||||
|
"extra.blah": true,
|
||||||
|
"extra.spam": "foobar",
|
||||||
|
"alpha.foo": "true",
|
||||||
|
"beta.bar": { value: 20 },
|
||||||
|
"test.enum": "a",
|
||||||
|
"test.test": "blah",
|
||||||
|
"test.func": 50,
|
||||||
|
"test.bool": false,
|
||||||
|
"test.json": {},
|
||||||
|
"test.rest": {},
|
||||||
|
"test.priv": null,
|
||||||
|
"test.cred": null,
|
||||||
|
"test.rdo": null,
|
||||||
|
"test.alpha": null,
|
||||||
|
"test.beta": "decay",
|
||||||
|
"test.gamma": 99,
|
||||||
|
};
|
||||||
|
await fs.writeJson(filepath, testFields);
|
||||||
|
const testInstance = await TestConfig.fromFile("local", filepath);
|
||||||
|
assert.deepEqual(testInstance.toJSON(), testFields);
|
||||||
|
assert.equal(testInstance.filepath, filepath);
|
||||||
|
});
|
||||||
|
it("should ignore inaccessible fields", async function() {
|
||||||
|
await fs.writeJSON(filepath, { "test.priv": "bad" });
|
||||||
|
const testInstance = await TestConfig.fromFile("remote", filepath);
|
||||||
|
assert.equal(testInstance.fields["test.priv"], undefined);
|
||||||
|
assert.equal(testInstance.fields["test.func"], 42);
|
||||||
|
assert.equal(testInstance.filepath, filepath);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe(".save()", function() {
|
||||||
|
const filepath = path.join("temp", "test", "config_test.json");
|
||||||
|
beforeEach(async function() {
|
||||||
|
await fs.remove(filepath);
|
||||||
|
assert(!await fs.exists(filepath), "File was not removed");
|
||||||
|
});
|
||||||
|
it("should throw if there is no filepath", async function() {
|
||||||
|
const testInstance = new TestConfig("local", { "alpha.foo": "a" });
|
||||||
|
testInstance.set("alpha.foo", "b"); // Sets the dirty flag
|
||||||
|
await assert.rejects(
|
||||||
|
testInstance.save(),
|
||||||
|
new Error("Cannot save config which has no filepath")
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("should do nothing when not dirty", async function() {
|
||||||
|
const testInstance = new TestConfig("local", { "alpha.foo": "a" }, filepath);
|
||||||
|
await testInstance.save();
|
||||||
|
assert(!await fs.exists(filepath), "File was created");
|
||||||
|
});
|
||||||
|
it("should save data to file", async function() {
|
||||||
|
const testInstance = new TestConfig("local", { "alpha.foo": "a" }, filepath);
|
||||||
|
testInstance.set("alpha.foo", "b"); // Sets the dirty flag
|
||||||
|
await testInstance.save();
|
||||||
|
const json = await fs.readJSON(filepath);
|
||||||
|
assert.deepEqual(json, testInstance.toJSON());
|
||||||
|
});
|
||||||
|
it("should clear the dirty flag after saving", async function() {
|
||||||
|
const testInstance = new TestConfig("local", { "alpha.foo": "a" }, filepath);
|
||||||
|
testInstance.set("alpha.foo", "b"); // Sets the dirty flag
|
||||||
|
await testInstance.save();
|
||||||
|
assert(await fs.exists(filepath), "File was not created");
|
||||||
|
await fs.remove(filepath);
|
||||||
|
assert(!await fs.exists(filepath), "File was not removed");
|
||||||
|
await testInstance.save();
|
||||||
|
assert(!await fs.exists(filepath), "File was created");
|
||||||
|
});
|
||||||
|
it("should be round trip savable", async function() {
|
||||||
|
const testInstance = new TestConfig("local", {
|
||||||
|
"extra.blah": true,
|
||||||
|
"extra.spam": "foobar",
|
||||||
|
"alpha.foo": "true",
|
||||||
|
"beta.bar": { value: 20 },
|
||||||
|
"test.enum": "a",
|
||||||
|
"test.test": "blah",
|
||||||
|
"test.func": 50,
|
||||||
|
"test.bool": false,
|
||||||
|
"test.json": {},
|
||||||
|
"test.rest": {},
|
||||||
|
"test.priv": null,
|
||||||
|
"test.cred": null,
|
||||||
|
"test.rdo": null,
|
||||||
|
"test.alpha": null,
|
||||||
|
"test.beta": "decay",
|
||||||
|
"test.gamma": 99,
|
||||||
|
}, filepath);
|
||||||
|
testInstance.set("alpha.foo", "b"); // Sets the dirty flag
|
||||||
|
await testInstance.save();
|
||||||
|
const loadedInstance = await TestConfig.fromFile("local", filepath);
|
||||||
|
assert.deepEqual(loadedInstance, testInstance);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe(".update()", function() {
|
describe(".update()", function() {
|
||||||
|
|||||||
Reference in new issue
Block a user