Some fixes to config setting

This commit is contained in:
Cooldude2606 committed 2026-03-11 00:28:36 +00:00
1 parent 9d807fc6d6
commit 414ef0bc8b
6 files changed
+28 -19

No files matched your search

+6 -5
View File
@@ -315,23 +315,24 @@ export default class ControlConnection extends BaseConnection {
}
async handleInstanceConfigSetRequest(request: lib.InstanceConfigSetRequest) {
const instance = this._controller.instances.getForRequest(request.instanceId);
try {
for (const [fieldName, fieldValue] of Object.entries(request.fields)) {
ControlConnection.validateInstanceConfigSetField(fieldName);
if (typeof fieldValue === "object") {
for (const [propName, propValue] of Object.entries(fieldValue)) {
this._controller.config.stageProp(
fieldName as keyof lib.ControllerConfigFields, propName, propValue, "control"
instance.config.stageProp(
fieldName as keyof lib.InstanceConfigFields, propName, propValue, "control"
);
}
} else {
this._controller.config.stage(fieldName as keyof lib.ControllerConfigFields, fieldValue, "control");
instance.config.stage(fieldName as keyof lib.InstanceConfigFields, fieldValue, "control");
}
}
this._controller.config.commitStaging();
instance.config.commitStaging();
} finally {
this._controller.config.revertStaging();
instance.config.revertStaging();
}
}
+7 -7
View File
@@ -9,7 +9,6 @@ import * as libSchema from "../schema";
import { StringEnum } from "../data/composites";
import { safeOutputFile } from "../file_ops";
import * as validators from "./validators";
import { string } from "yargs";
const ConfigLocation = StringEnum(["controller", "host", "control"]);
export type ConfigLocation = Static<typeof ConfigLocation>;
@@ -208,7 +207,7 @@ export class Config<
* Note that mutating this object will lead to unexpected behaviour.
*/
declare static fieldDefinitions: ConfigDefs<any>;
declare ["constructor"]: typeof Config;
declare ["constructor"]: typeof Config<Fields>;
/** A set of common validators and validator factories */
static validators = validators;
@@ -272,14 +271,14 @@ export class Config<
}
/** A staging version of the config with changes tracked, invalided by revertStaging */
private _staging?: { config: Config<Fields>, changes: Set<keyof Fields & string> };
private _staging?: { config: Config<Fields>, changes: Set<string>};
/** Get the latest version of staging */
get staging(): Config<Fields> {
if (this._staging) { return this._staging.config; }
const config = new this.constructor(this.location, this.fields) as this;
const changes = new Set<keyof Fields & string>();
config.on("fieldChanged", name => changes.add(name as keyof Fields & string));
const config = new this.constructor(this.location, this.fields);
const changes = new Set<string>();
config.on("fieldChanged", name => changes.add(name));
this._staging = { config, changes };
return config;
}
@@ -832,7 +831,8 @@ export class Config<
// Validation success, set all values which have changed
for (const fieldName of staging.changes) {
this._set(fieldName, config.fields[fieldName]);
const field = fieldName as keyof Fields & string;
this._set(field, config.fields[field]);
}
}
+2 -2
View File
@@ -45,12 +45,12 @@ export class ControllerConfigSetRequest {
static permission = "core.controller.update_config" as const;
constructor(
public fields: Record<string, string | Record<string, string>>,
public fields: Record<string, string | Record<string, unknown>>,
) { }
static jsonSchema = Type.Object({
"fields": Type.Record(Type.String(), Type.Union([
Type.String(), Type.Record(Type.String(), Type.String()),
Type.String(), Type.Record(Type.String(), Type.Unknown()),
])),
});
+2 -2
View File
@@ -45,12 +45,12 @@ export class HostConfigSetRequest {
static permission = "core.host.update_config" as const;
constructor(
public fields: Record<string, string | Record<string, string>>,
public fields: Record<string, string | Record<string, unknown>>,
) { }
static jsonSchema = Type.Object({
"fields": Type.Record(Type.String(), Type.Union([
Type.String(), Type.Record(Type.String(), Type.String()),
Type.String(), Type.Record(Type.String(), Type.Unknown()),
])),
});
+2 -2
View File
@@ -111,13 +111,13 @@ export class InstanceConfigSetRequest {
constructor(
public instanceId: number,
public fields: Record<string, string | Record<string, string>>,
public fields: Record<string, string | Record<string, unknown>>,
) { }
static jsonSchema = Type.Object({
"instanceId": Type.Integer(),
"fields": Type.Record(Type.String(), Type.Union([
Type.String(), Type.Record(Type.String(), Type.String()),
Type.String(), Type.Record(Type.String(), Type.Unknown()),
])),
});
@@ -164,7 +164,15 @@ export default function BaseConfigTree(props: BaseConfigTreeProps) {
const [fieldName, prop] = propsMap.get(field)!;
const fieldValue = (fields[fieldName] ?? {}) as Record<string, string>;
fields[fieldName] = fieldValue;
fieldValue[prop] = value ? JSON.parse(value) : undefined;
try {
fieldValue[prop] = value ? JSON.parse(value) : undefined;
} catch (err: any) {
setErrorFields(errors => {
const newErrors = new Map(errors);
newErrors.set(fieldName, err.message);
return newErrors;
});
}
} else {
fields[field] = value === null ? "" : String(value);