Separate YAML reading from configuration resolution #80
No files matched your search
@@ -0,0 +1,6 @@
|
||||
---
|
||||
'@hledger-reports/web': patch
|
||||
'@hledger-reports/worker': patch
|
||||
---
|
||||
|
||||
Separate reading and parsing the configuration file from resolving web and worker settings. Configuration, validation, and error behavior are unchanged.
|
||||
@@ -11,6 +11,9 @@ never overwrite an existing journal selection or secrets.
|
||||
Environment references support `${NAME}` and `${NAME:-fallback}`. Account names
|
||||
and labels are not expanded. Restart services after changes.
|
||||
|
||||
Both services validate the authored web settings. Only the web service expands
|
||||
environment references in `url.app` and `auth`.
|
||||
|
||||
Data lives in the sibling `data/` directory beside `config/`; web and worker must
|
||||
select the same deployment. Do not move or replace existing data to change labels.
|
||||
|
||||
|
||||
@@ -68,6 +68,7 @@ it('preserves absolute journals and supports relative config filenames', async (
|
||||
|
||||
it.each([
|
||||
'journal: [private-value',
|
||||
configYaml('%YAML 1.1\n---\njournal: book\n'),
|
||||
configYaml('journal: book\n') + '---\njournal: other\n',
|
||||
configYaml('journal: book\n').replace(
|
||||
'username: shared-user',
|
||||
@@ -78,7 +79,7 @@ it.each([
|
||||
'password: !!str private-value'
|
||||
)
|
||||
])(
|
||||
'requires safe single-document YAML without duplicate keys or explicit tags %#',
|
||||
'requires safe single-document YAML 1.2 without duplicate keys or explicit tags %#',
|
||||
async (source) => {
|
||||
const { path } = await fixture(source);
|
||||
try {
|
||||
@@ -116,6 +117,16 @@ it('uses YAML 1.2 core scalars and rejects unsupported document shapes', async (
|
||||
await expect(loadConfig(path)).rejects.toMatchObject({ code: 'CONFIG_INVALID' });
|
||||
});
|
||||
|
||||
it('does not let YAML merge keys supply credential fields', async () => {
|
||||
const { path } = await fixture(`journal: book
|
||||
url: { app: 'http://localhost:3000' }
|
||||
auth:
|
||||
credentials:
|
||||
<<: { username: shared-user, password: synthetic-password }
|
||||
`);
|
||||
await expect(loadConfig(path)).rejects.toMatchObject({ code: 'CONFIG_INVALID' });
|
||||
});
|
||||
|
||||
it('bounds YAML alias expansion', async () => {
|
||||
const { path } = await fixture(
|
||||
'a: &a [book, book]\nb: &b [*a, *a, *a, *a, *a, *a, *a, *a, *a, *a]\n' +
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import { dirname, resolve } from 'node:path';
|
||||
import { parseAllDocuments, visit } from 'yaml';
|
||||
import { ConfigError, invalidInput } from './errors.js';
|
||||
import { checkSchema, readInput } from './input.js';
|
||||
import { invalidInput } from './errors.js';
|
||||
import { checkSchema, validateAuthoredInput } from './input.js';
|
||||
import { interpolateString } from './interpolate.js';
|
||||
import {
|
||||
configSchema,
|
||||
@@ -13,6 +11,7 @@ import {
|
||||
type WorkerConfig
|
||||
} from './schema.js';
|
||||
import { normalizeAppUrl, validateIssuer } from './urls.js';
|
||||
import { readYamlFile } from './yaml.js';
|
||||
|
||||
export { ConfigError, type ConfigErrorCode, type ConfigIssue } from './errors.js';
|
||||
export {
|
||||
@@ -83,7 +82,7 @@ function resolveAuth(
|
||||
|
||||
/** Resolve the complete private web configuration (also used by config-only validation). */
|
||||
export function validateConfig(input: unknown, options: ConfigOptions = {}): Config {
|
||||
const raw = readInput(input);
|
||||
const raw = validateAuthoredInput(input);
|
||||
const env = environment(options);
|
||||
const journal = resolveJournal(raw, env);
|
||||
const app = interpolateString(raw.url.app, env, '/url/app');
|
||||
@@ -97,7 +96,7 @@ export function validateConfig(input: unknown, options: ConfigOptions = {}): Con
|
||||
|
||||
/** Validate the FULL shared raw contract, but never read or interpolate web environment fields. */
|
||||
export function validateWorkerConfig(input: unknown, options: ConfigOptions = {}): WorkerConfig {
|
||||
const raw = readInput(input);
|
||||
const raw = validateAuthoredInput(input);
|
||||
return {
|
||||
version: 1,
|
||||
journal: resolveJournal(raw, environment(options)),
|
||||
@@ -109,8 +108,8 @@ export function validateWorkerConfig(input: unknown, options: ConfigOptions = {}
|
||||
|
||||
/** Read only the selected YAML; resolving its journal path does not open the journal. */
|
||||
export async function loadConfig(path: string, options: ConfigOptions = {}): Promise<Config> {
|
||||
const { input, configPath } = await readConfigFile(path);
|
||||
const config = validateConfig(input, options);
|
||||
const { authoredInput, configPath } = await readYamlFile(path);
|
||||
const config = validateConfig(authoredInput, options);
|
||||
return { ...config, journal: resolve(dirname(configPath), config.journal) };
|
||||
}
|
||||
|
||||
@@ -118,49 +117,7 @@ export async function loadWorkerConfig(
|
||||
path: string,
|
||||
options: ConfigOptions = {}
|
||||
): Promise<WorkerConfig> {
|
||||
const { input, configPath } = await readConfigFile(path);
|
||||
const config = validateWorkerConfig(input, options);
|
||||
const { authoredInput, configPath } = await readYamlFile(path);
|
||||
const config = validateWorkerConfig(authoredInput, options);
|
||||
return { ...config, journal: resolve(dirname(configPath), config.journal) };
|
||||
}
|
||||
|
||||
async function readConfigFile(path: string): Promise<{ input: unknown; configPath: string }> {
|
||||
let configPath: string;
|
||||
let source: string;
|
||||
try {
|
||||
configPath = resolve(path);
|
||||
source = await readFile(configPath, 'utf8');
|
||||
} catch {
|
||||
throw new ConfigError('CONFIG_READ_FAILED');
|
||||
}
|
||||
let input: unknown;
|
||||
try {
|
||||
const documents = parseAllDocuments(source, {
|
||||
version: '1.2',
|
||||
schema: 'core',
|
||||
stringKeys: true,
|
||||
uniqueKeys: true,
|
||||
merge: false,
|
||||
prettyErrors: false,
|
||||
logLevel: 'silent'
|
||||
});
|
||||
const document = documents[0];
|
||||
if (
|
||||
documents.length !== 1 ||
|
||||
!document ||
|
||||
document.errors.length > 0 ||
|
||||
document.warnings.length > 0 ||
|
||||
document.directives?.yaml.version !== '1.2'
|
||||
) {
|
||||
throw new Error();
|
||||
}
|
||||
visit(document, {
|
||||
Node(_key, node) {
|
||||
if (node.tag !== undefined) throw new Error();
|
||||
}
|
||||
});
|
||||
input = document.toJS({ maxAliasCount: 100 });
|
||||
} catch {
|
||||
throw new ConfigError('CONFIG_PARSE_FAILED');
|
||||
}
|
||||
return { input, configPath };
|
||||
}
|
||||
@@ -58,7 +58,7 @@ export function checkSchema(schema: TSchema, value: unknown, basePath = ''): voi
|
||||
if (issues.length > 0) throw new ConfigError('CONFIG_INVALID', issues);
|
||||
}
|
||||
|
||||
export function readInput(input: unknown): ConfigInput {
|
||||
export function validateAuthoredInput(input: unknown): ConfigInput {
|
||||
let copy: unknown;
|
||||
try {
|
||||
copy = copyNode(input, new Set());
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import { resolve } from 'node:path';
|
||||
import { parseAllDocuments, visit, type Document } from 'yaml';
|
||||
import { ConfigError } from './errors.js';
|
||||
|
||||
/** Read the selected YAML file without validating or resolving its settings. */
|
||||
export async function readYamlFile(
|
||||
path: string
|
||||
): Promise<{ authoredInput: unknown; configPath: string }> {
|
||||
let configPath: string;
|
||||
let source: string;
|
||||
try {
|
||||
configPath = resolve(path);
|
||||
source = await readFile(configPath, 'utf8');
|
||||
} catch {
|
||||
throw new ConfigError('CONFIG_READ_FAILED');
|
||||
}
|
||||
try {
|
||||
const documents = parseAllDocuments(source, {
|
||||
version: '1.2',
|
||||
schema: 'core',
|
||||
stringKeys: true,
|
||||
uniqueKeys: true,
|
||||
merge: false,
|
||||
prettyErrors: false,
|
||||
logLevel: 'silent'
|
||||
});
|
||||
const document = requireSupportedDocument(documents);
|
||||
const authoredInput: unknown = document.toJS({ maxAliasCount: 100 });
|
||||
return { authoredInput, configPath };
|
||||
} catch {
|
||||
throw new ConfigError('CONFIG_PARSE_FAILED');
|
||||
}
|
||||
}
|
||||
|
||||
function requireSupportedDocument(documents: readonly Document.Parsed[]): Document.Parsed {
|
||||
const document = documents[0];
|
||||
if (
|
||||
documents.length !== 1 ||
|
||||
!document ||
|
||||
document.errors.length > 0 ||
|
||||
document.warnings.length > 0 ||
|
||||
document.directives?.yaml.version !== '1.2'
|
||||
) {
|
||||
throw new Error();
|
||||
}
|
||||
visit(document, {
|
||||
Node(_key, node) {
|
||||
if (node.tag !== undefined) throw new Error();
|
||||
}
|
||||
});
|
||||
return document;
|
||||
}
|
||||
Reference in new issue
Block a user