mirror of
https://github.com/morluto/rea.git
synced 2026-11-30 02:17:09 +00:00
* refactor(config): extract ClientConfigurationToml from ClientConfigurationDocument * refactor(setup): decouple the TOML serializer from the document reader
41 lines
1.6 KiB
TypeScript
41 lines
1.6 KiB
TypeScript
import { isDeepStrictEqual } from "node:util";
|
|
import { z } from "zod";
|
|
import { compareUnicodeCodePoints } from "../domain/unicodeCodePointOrder.js";
|
|
|
|
/** Admit source mappings without Zod's record clone dropping caller keys. */
|
|
export const clientConfigurationRecordSchema = z
|
|
.custom<Record<string, unknown>>((value) => {
|
|
if (typeof value !== "object" || value === null) return false;
|
|
const prototype: unknown = Object.getPrototypeOf(value);
|
|
return prototype === null || prototype === Object.prototype;
|
|
})
|
|
.transform((value) => Object.fromEntries(Object.entries(value)));
|
|
|
|
/** Compare parsed configuration values without depending on parser prototypes. */
|
|
export const clientConfigurationValuesEqual = (
|
|
left: unknown,
|
|
right: unknown,
|
|
): boolean =>
|
|
isDeepStrictEqual(
|
|
normalizeConfigurationValue(left),
|
|
normalizeConfigurationValue(right),
|
|
);
|
|
|
|
const normalizeConfigurationValue = (value: unknown): unknown => {
|
|
if (Array.isArray(value)) return value.map(normalizeConfigurationValue);
|
|
if (typeof value !== "object" || value === null || value instanceof Date)
|
|
return value;
|
|
return Object.fromEntries(
|
|
Object.entries(value)
|
|
.sort(([left], [right]) => compareUnicodeCodePoints(left, right))
|
|
.map(([key, nested]) => [key, normalizeConfigurationValue(nested)]),
|
|
);
|
|
};
|
|
|
|
/**
|
|
* Whether raw configuration text holds no settings to preserve. Some clients
|
|
* create a zero-byte config file before any server is added.
|
|
*/
|
|
export const isEmptyClientConfigurationText = (text: string): boolean =>
|
|
text.replace(/^\uFEFF/, "").trim() === "";
|