Spaces:
Sleeping
Sleeping
File size: 7,296 Bytes
fb4d8fe | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | /**
* Config includes: $include directive for modular configs
*
* @example
* ```json5
* {
* "$include": "./base.json5", // single file
* "$include": ["./a.json5", "./b.json5"] // merge multiple
* }
* ```
*/
import JSON5 from "json5";
import fs from "node:fs";
import path from "node:path";
export const INCLUDE_KEY = "$include";
export const MAX_INCLUDE_DEPTH = 10;
// ============================================================================
// Types
// ============================================================================
export type IncludeResolver = {
readFile: (path: string) => string;
parseJson: (raw: string) => unknown;
};
// ============================================================================
// Errors
// ============================================================================
export class ConfigIncludeError extends Error {
constructor(
message: string,
public readonly includePath: string,
public readonly cause?: Error,
) {
super(message);
this.name = "ConfigIncludeError";
}
}
export class CircularIncludeError extends ConfigIncludeError {
constructor(public readonly chain: string[]) {
super(`Circular include detected: ${chain.join(" -> ")}`, chain[chain.length - 1]);
this.name = "CircularIncludeError";
}
}
// ============================================================================
// Utilities
// ============================================================================
function isPlainObject(value: unknown): value is Record<string, unknown> {
return (
typeof value === "object" &&
value !== null &&
!Array.isArray(value) &&
Object.prototype.toString.call(value) === "[object Object]"
);
}
/** Deep merge: arrays concatenate, objects merge recursively, primitives: source wins */
export function deepMerge(target: unknown, source: unknown): unknown {
if (Array.isArray(target) && Array.isArray(source)) {
return [...target, ...source];
}
if (isPlainObject(target) && isPlainObject(source)) {
const result: Record<string, unknown> = { ...target };
for (const key of Object.keys(source)) {
result[key] = key in result ? deepMerge(result[key], source[key]) : source[key];
}
return result;
}
return source;
}
// ============================================================================
// Include Resolver Class
// ============================================================================
class IncludeProcessor {
private visited = new Set<string>();
private depth = 0;
constructor(
private basePath: string,
private resolver: IncludeResolver,
) {
this.visited.add(path.normalize(basePath));
}
process(obj: unknown): unknown {
if (Array.isArray(obj)) {
return obj.map((item) => this.process(item));
}
if (!isPlainObject(obj)) {
return obj;
}
if (!(INCLUDE_KEY in obj)) {
return this.processObject(obj);
}
return this.processInclude(obj);
}
private processObject(obj: Record<string, unknown>): Record<string, unknown> {
const result: Record<string, unknown> = {};
for (const [key, value] of Object.entries(obj)) {
result[key] = this.process(value);
}
return result;
}
private processInclude(obj: Record<string, unknown>): unknown {
const includeValue = obj[INCLUDE_KEY];
const otherKeys = Object.keys(obj).filter((k) => k !== INCLUDE_KEY);
const included = this.resolveInclude(includeValue);
if (otherKeys.length === 0) {
return included;
}
if (!isPlainObject(included)) {
throw new ConfigIncludeError(
"Sibling keys require included content to be an object",
typeof includeValue === "string" ? includeValue : INCLUDE_KEY,
);
}
// Merge included content with sibling keys
const rest: Record<string, unknown> = {};
for (const key of otherKeys) {
rest[key] = this.process(obj[key]);
}
return deepMerge(included, rest);
}
private resolveInclude(value: unknown): unknown {
if (typeof value === "string") {
return this.loadFile(value);
}
if (Array.isArray(value)) {
return value.reduce<unknown>((merged, item) => {
if (typeof item !== "string") {
throw new ConfigIncludeError(
`Invalid $include array item: expected string, got ${typeof item}`,
String(item),
);
}
return deepMerge(merged, this.loadFile(item));
}, {});
}
throw new ConfigIncludeError(
`Invalid $include value: expected string or array of strings, got ${typeof value}`,
String(value),
);
}
private loadFile(includePath: string): unknown {
const resolvedPath = this.resolvePath(includePath);
this.checkCircular(resolvedPath);
this.checkDepth(includePath);
const raw = this.readFile(includePath, resolvedPath);
const parsed = this.parseFile(includePath, resolvedPath, raw);
return this.processNested(resolvedPath, parsed);
}
private resolvePath(includePath: string): string {
const resolved = path.isAbsolute(includePath)
? includePath
: path.resolve(path.dirname(this.basePath), includePath);
return path.normalize(resolved);
}
private checkCircular(resolvedPath: string): void {
if (this.visited.has(resolvedPath)) {
throw new CircularIncludeError([...this.visited, resolvedPath]);
}
}
private checkDepth(includePath: string): void {
if (this.depth >= MAX_INCLUDE_DEPTH) {
throw new ConfigIncludeError(
`Maximum include depth (${MAX_INCLUDE_DEPTH}) exceeded at: ${includePath}`,
includePath,
);
}
}
private readFile(includePath: string, resolvedPath: string): string {
try {
return this.resolver.readFile(resolvedPath);
} catch (err) {
throw new ConfigIncludeError(
`Failed to read include file: ${includePath} (resolved: ${resolvedPath})`,
includePath,
err instanceof Error ? err : undefined,
);
}
}
private parseFile(includePath: string, resolvedPath: string, raw: string): unknown {
try {
return this.resolver.parseJson(raw);
} catch (err) {
throw new ConfigIncludeError(
`Failed to parse include file: ${includePath} (resolved: ${resolvedPath})`,
includePath,
err instanceof Error ? err : undefined,
);
}
}
private processNested(resolvedPath: string, parsed: unknown): unknown {
const nested = new IncludeProcessor(resolvedPath, this.resolver);
nested.visited = new Set([...this.visited, resolvedPath]);
nested.depth = this.depth + 1;
return nested.process(parsed);
}
}
// ============================================================================
// Public API
// ============================================================================
const defaultResolver: IncludeResolver = {
readFile: (p) => fs.readFileSync(p, "utf-8"),
parseJson: (raw) => JSON5.parse(raw),
};
/**
* Resolves all $include directives in a parsed config object.
*/
export function resolveConfigIncludes(
obj: unknown,
configPath: string,
resolver: IncludeResolver = defaultResolver,
): unknown {
return new IncludeProcessor(configPath, resolver).process(obj);
}
|