2022-01-11 19:12:55 +00:00
|
|
|
// Copyright 2018-2022 Signal Messenger, LLC
|
2021-06-18 17:04:27 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { readFileSync, writeFileSync, unlinkSync } from 'fs';
|
|
|
|
|
2022-01-11 19:12:55 +00:00
|
|
|
import { get } from 'lodash';
|
|
|
|
import { set } from 'lodash/fp';
|
|
|
|
import { strictAssert } from '../ts/util/assert';
|
2021-06-18 17:04:27 +00:00
|
|
|
|
|
|
|
const ENCODING = 'utf8';
|
|
|
|
|
2021-10-01 18:49:59 +00:00
|
|
|
type InternalConfigType = Record<string, unknown>;
|
|
|
|
|
|
|
|
export type ConfigType = {
|
|
|
|
set: (keyPath: string, value: unknown) => void;
|
|
|
|
get: (keyPath: string) => unknown;
|
|
|
|
remove: () => void;
|
2021-10-25 19:41:44 +00:00
|
|
|
|
|
|
|
// Test-only
|
|
|
|
_getCachedValue: () => InternalConfigType | undefined;
|
2021-10-01 18:49:59 +00:00
|
|
|
};
|
2021-06-18 17:04:27 +00:00
|
|
|
|
2022-01-11 19:12:55 +00:00
|
|
|
export function start({
|
|
|
|
name,
|
|
|
|
targetPath,
|
|
|
|
throwOnFilesystemErrors,
|
|
|
|
}: Readonly<{
|
|
|
|
name: string;
|
|
|
|
targetPath: string;
|
|
|
|
throwOnFilesystemErrors: boolean;
|
|
|
|
}>): ConfigType {
|
|
|
|
let cachedValue: InternalConfigType = Object.create(null);
|
2021-10-25 19:41:44 +00:00
|
|
|
let incomingJson: string | undefined;
|
2021-06-18 17:04:27 +00:00
|
|
|
|
|
|
|
try {
|
2021-10-25 19:41:44 +00:00
|
|
|
incomingJson = readFileSync(targetPath, ENCODING);
|
|
|
|
cachedValue = incomingJson ? JSON.parse(incomingJson) : undefined;
|
2021-06-18 17:04:27 +00:00
|
|
|
console.log(`config/get: Successfully read ${name} config file`);
|
|
|
|
|
|
|
|
if (!cachedValue) {
|
|
|
|
console.log(
|
2022-01-11 19:12:55 +00:00
|
|
|
`config/start: ${name} config value was falsy, cache is now empty object`
|
2021-06-18 17:04:27 +00:00
|
|
|
);
|
|
|
|
cachedValue = Object.create(null);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2022-01-11 19:12:55 +00:00
|
|
|
if (throwOnFilesystemErrors && error.code !== 'ENOENT') {
|
2021-06-18 17:04:27 +00:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2021-10-25 19:41:44 +00:00
|
|
|
if (incomingJson) {
|
|
|
|
console.log(
|
2022-01-11 19:12:55 +00:00
|
|
|
`config/start: ${name} config file was malformed, starting afresh`
|
2021-10-25 19:41:44 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
console.log(
|
2022-01-11 19:12:55 +00:00
|
|
|
`config/start: Did not find ${name} config file (or it was empty), cache is now empty object`
|
2021-10-25 19:41:44 +00:00
|
|
|
);
|
|
|
|
}
|
2021-06-18 17:04:27 +00:00
|
|
|
cachedValue = Object.create(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
function ourGet(keyPath: string): unknown {
|
|
|
|
return get(cachedValue, keyPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
function ourSet(keyPath: string, value: unknown): void {
|
2022-01-11 19:12:55 +00:00
|
|
|
const newCachedValue = set(keyPath, value, cachedValue);
|
2021-06-18 17:04:27 +00:00
|
|
|
|
|
|
|
console.log(`config/set: Saving ${name} config to disk`);
|
2022-01-11 19:12:55 +00:00
|
|
|
|
|
|
|
if (!throwOnFilesystemErrors) {
|
|
|
|
cachedValue = newCachedValue;
|
|
|
|
}
|
|
|
|
const outgoingJson = JSON.stringify(newCachedValue, null, ' ');
|
|
|
|
try {
|
|
|
|
writeFileSync(targetPath, outgoingJson, ENCODING);
|
|
|
|
console.log(`config/set: Saved ${name} config to disk`);
|
|
|
|
cachedValue = newCachedValue;
|
|
|
|
} catch (err: unknown) {
|
|
|
|
if (throwOnFilesystemErrors) {
|
|
|
|
throw err;
|
|
|
|
} else {
|
|
|
|
console.warn(
|
|
|
|
`config/set: Failed to save ${name} config to disk; only updating in-memory data`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-06-18 17:04:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function remove(): void {
|
|
|
|
console.log(`config/remove: Deleting ${name} config from disk`);
|
2022-01-11 19:12:55 +00:00
|
|
|
try {
|
|
|
|
unlinkSync(targetPath);
|
|
|
|
console.log(`config/remove: Deleted ${name} config from disk`);
|
|
|
|
} catch (err: unknown) {
|
|
|
|
const errCode: unknown = get(err, 'code');
|
|
|
|
if (throwOnFilesystemErrors) {
|
|
|
|
strictAssert(errCode === 'ENOENT', 'Expected deletion of no file');
|
|
|
|
console.log(`config/remove: No ${name} config on disk, did nothing`);
|
|
|
|
} else {
|
|
|
|
console.warn(
|
|
|
|
`config/remove: Got ${String(
|
|
|
|
errCode
|
|
|
|
)} when removing ${name} config from disk`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-06-18 17:04:27 +00:00
|
|
|
cachedValue = Object.create(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
set: ourSet,
|
|
|
|
get: ourGet,
|
|
|
|
remove,
|
2021-10-25 19:41:44 +00:00
|
|
|
_getCachedValue: () => cachedValue,
|
2021-06-18 17:04:27 +00:00
|
|
|
};
|
|
|
|
}
|