@fdm-monster/server
Version:
FDM Monster is a bulk OctoPrint, Klipper, PrusaLink and BambuLab manager to set up, configure and monitor 3D printers. Our aim is to provide neat overview over your farm.
213 lines (212 loc) • 8.91 kB
JavaScript
import { InternalServerException } from "../exceptions/runtime.exceptions.js";
import { AppConstants } from "../server.constants.js";
import { credentialSettingsKey, frontendSettingKey, serverSettingsKey, timeoutSettingKey, wizardSettingKey } from "../constants/server-settings.constants.js";
import { isTestEnvironment } from "../utils/env.utils.js";
import { v4 } from "uuid";
import { getClient } from "@sentry/node";
//#region src/state/settings.store.ts
var SettingsStore = class SettingsStore {
logger;
settings = null;
constructor(loggerFactory, settingsService) {
this.settingsService = settingsService;
this.logger = loggerFactory(SettingsStore.name);
}
getSettings() {
this.throwIfSettingsUnset();
const settings = this.settings;
return Object.freeze({
[serverSettingsKey]: {
loginRequired: settings[serverSettingsKey].loginRequired,
registration: settings[serverSettingsKey].registration,
sentryDiagnosticsEnabled: settings[serverSettingsKey].sentryDiagnosticsEnabled,
experimentalMoonrakerSupport: settings[serverSettingsKey].experimentalMoonrakerSupport,
experimentalPrusaLinkSupport: settings[serverSettingsKey].experimentalPrusaLinkSupport,
experimentalBambuSupport: settings[serverSettingsKey].experimentalBambuSupport
},
[wizardSettingKey]: settings[wizardSettingKey],
[frontendSettingKey]: settings[frontendSettingKey],
[timeoutSettingKey]: settings[timeoutSettingKey]
});
}
getSettingsSensitive() {
this.throwIfSettingsUnset();
const settings = this.settings;
return Object.freeze({
[credentialSettingsKey]: {
jwtExpiresIn: settings[credentialSettingsKey].jwtExpiresIn,
refreshTokenAttempts: settings[credentialSettingsKey].refreshTokenAttempts,
refreshTokenExpiry: settings[credentialSettingsKey].refreshTokenExpiry,
slicerApiKey: settings[credentialSettingsKey].slicerApiKey
},
[serverSettingsKey]: {
loginRequired: settings[serverSettingsKey].loginRequired,
registration: settings[serverSettingsKey].registration,
sentryDiagnosticsEnabled: settings[serverSettingsKey].sentryDiagnosticsEnabled,
experimentalMoonrakerSupport: settings[serverSettingsKey].experimentalMoonrakerSupport,
experimentalPrusaLinkSupport: settings[serverSettingsKey].experimentalPrusaLinkSupport,
experimentalBambuSupport: settings[serverSettingsKey].experimentalBambuSupport
}
});
}
async loadSettings() {
this.settings = await this.settingsService.getOrCreate();
await this.processSentryEnabled();
}
async getCredentialSettings() {
this.throwIfSettingsUnset();
return this.settings[credentialSettingsKey];
}
async getAnonymousDiagnosticsEnabled() {
this.throwIfSettingsUnset();
return this.settings[serverSettingsKey].sentryDiagnosticsEnabled;
}
async persistOptionalCredentialSettings(overrideJwtSecret, overrideJwtExpiresIn) {
this.throwIfSettingsUnset();
const credentialSettings = await this.getCredentialSettings();
if (overrideJwtSecret?.length) await this.settingsService.updateJwtSecretCredentialSetting({ jwtSecret: overrideJwtSecret });
if (overrideJwtExpiresIn?.length) await this.updateCoreCredentialSettings({
refreshTokenExpiry: credentialSettings.refreshTokenExpiry,
refreshTokenAttempts: credentialSettings.refreshTokenAttempts,
jwtExpiresIn: Number.parseInt(overrideJwtExpiresIn)
});
this.settings[credentialSettingsKey] = await this.getCredentialSettings();
}
getWizardState() {
this.throwIfSettingsUnset();
const settings = this.settings;
return {
wizardCompleted: settings[wizardSettingKey].wizardCompleted,
wizardVersion: settings[wizardSettingKey].wizardVersion,
latestWizardVersion: AppConstants.currentWizardVersion
};
}
isWizardCompleted() {
this.throwIfSettingsUnset();
const settings = this.settings;
return settings["wizard"].wizardCompleted && settings["wizard"].wizardVersion === AppConstants.currentWizardVersion;
}
getWizardSettings() {
this.throwIfSettingsUnset();
return this.settings[wizardSettingKey];
}
isRegistrationEnabled() {
this.throwIfSettingsUnset();
return this.settings[serverSettingsKey].registration;
}
getServerSettings() {
return this.getSettings()[serverSettingsKey];
}
getTimeoutSettings() {
return this.getSettings()[timeoutSettingKey];
}
async setWizardCompleted(version) {
this.settings = await this.settingsService.updateWizardSettings({
wizardCompleted: true,
wizardCompletedAt: /* @__PURE__ */ new Date(),
wizardVersion: version
});
return this.getSettings();
}
async getLoginRequired() {
return this.getServerSettings().loginRequired;
}
async setLoginRequired(loginRequired = true) {
this.throwIfSettingsUnset();
this.settings[serverSettingsKey].loginRequired = loginRequired;
this.settings = await this.settingsService.updateServerSettings(this.settings[serverSettingsKey]);
return this.getSettings();
}
async setRegistrationEnabled(registration = true) {
this.throwIfSettingsUnset();
this.settings[serverSettingsKey].registration = registration;
this.settings = await this.settingsService.updateServerSettings(this.settings[serverSettingsKey]);
return this.getSettings();
}
async updateServerSettings(serverSettings) {
this.settings = await this.settingsService.updateServerSettings(serverSettings);
return this.getSettings();
}
async updateTimeoutSettings(timeoutSettings) {
this.settings = await this.settingsService.updateTimeoutSettings(timeoutSettings);
return this.getSettings();
}
async updateCoreCredentialSettings(credentialSettings) {
this.settings = await this.settingsService.updateCoreCredentialSettings(credentialSettings);
}
async setRefreshTokenSettings({ refreshTokenAttempts, refreshTokenExpiry }) {
this.throwIfSettingsUnset();
this.settings[credentialSettingsKey].refreshTokenAttempts = refreshTokenAttempts;
this.settings[credentialSettingsKey].refreshTokenExpiry = refreshTokenExpiry;
await this.updateCoreCredentialSettings(this.settings[credentialSettingsKey]);
}
async setSentryDiagnosticsEnabled(sentryDiagnosticsEnabled) {
this.throwIfSettingsUnset();
this.settings[serverSettingsKey].sentryDiagnosticsEnabled = sentryDiagnosticsEnabled;
this.settings = await this.settingsService.updateServerSettings(this.settings[serverSettingsKey]);
await this.processSentryEnabled();
return this.getSettings();
}
async setExperimentalMoonrakerSupport(experimentalMoonrakerSupport) {
this.throwIfSettingsUnset();
this.settings[serverSettingsKey].experimentalMoonrakerSupport = experimentalMoonrakerSupport;
this.settings = await this.settingsService.updateServerSettings(this.settings[serverSettingsKey]);
return this.getSettings();
}
async setExperimentalPrusaLinkSupport(experimentalPrusaLinkSupport) {
this.throwIfSettingsUnset();
this.settings[serverSettingsKey].experimentalPrusaLinkSupport = experimentalPrusaLinkSupport;
this.settings = await this.settingsService.updateServerSettings(this.settings[serverSettingsKey]);
return this.getSettings();
}
async setExperimentalBambuSupport(experimentalBambuSupport) {
this.throwIfSettingsUnset();
this.settings[serverSettingsKey].experimentalBambuSupport = experimentalBambuSupport;
this.settings = await this.settingsService.updateServerSettings(this.settings[serverSettingsKey]);
return this.getSettings();
}
async updateFrontendSettings(frontendSettings) {
this.settings = await this.settingsService.updateFrontendSettings(frontendSettings);
return this.getSettings();
}
getSlicerApiKey() {
this.throwIfSettingsUnset();
return this.settings[credentialSettingsKey].slicerApiKey;
}
async generateSlicerApiKey() {
const newApiKey = v4();
return this.setSlicerApiKey(newApiKey);
}
async setSlicerApiKey(apiKey) {
this.throwIfSettingsUnset();
this.settings = await this.settingsService.updateSlicerApiKey({ slicerApiKey: apiKey });
return apiKey;
}
async deleteSlicerApiKey() {
this.throwIfSettingsUnset();
this.settings = await this.settingsService.updateSlicerApiKey({ slicerApiKey: null });
}
validateSlicerApiKey(apiKey) {
const storedKey = this.getSlicerApiKey();
if (!storedKey) return false;
return storedKey === apiKey;
}
throwIfSettingsUnset() {
if (!this.settings) throw new InternalServerException("Could not check server settings (server settings not loaded)");
}
async processSentryEnabled() {
const sentryEnabled = await this.getAnonymousDiagnosticsEnabled();
if (sentryEnabled) this.logger.log("Enabling Sentry for remote diagnostics");
else this.logger.log("Disabling Sentry for remote diagnostics");
if (isTestEnvironment()) return;
const client = getClient();
if (!client) {
this.logger.warn("Could not apply Sentry. Was the SDK initialized?");
return;
}
client.getOptions().enabled = sentryEnabled;
}
};
//#endregion
export { SettingsStore };
//# sourceMappingURL=settings.store.js.map