refactor: share non sub2api ops helpers
This commit is contained in:
@@ -72,6 +72,38 @@ export function fingerprintValues(values: Record<string, string>, keys: string[]
|
||||
return `sha256:${hash.digest("hex")}`;
|
||||
}
|
||||
|
||||
export function fingerprintEnvValues(values: Record<string, string>, keys: string[]): string {
|
||||
const material = keys
|
||||
.slice()
|
||||
.sort()
|
||||
.map((key) => `${key}=${values[key] ?? ""}`)
|
||||
.join("\n");
|
||||
return `sha256:${createHash("sha256").update(material).digest("hex")}`;
|
||||
}
|
||||
|
||||
export function parseEnvFile(text: string): Record<string, string> {
|
||||
const result: Record<string, string> = {};
|
||||
for (const rawLine of text.split(/\r?\n/u)) {
|
||||
const line = rawLine.trim();
|
||||
if (line.length === 0 || line.startsWith("#")) continue;
|
||||
const eq = line.indexOf("=");
|
||||
if (eq <= 0) continue;
|
||||
const key = line.slice(0, eq).trim();
|
||||
if (!/^[A-Za-z_][A-Za-z0-9_]*$/u.test(key)) continue;
|
||||
result[key] = unquoteEnvValue(line.slice(eq + 1).trim());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function unquoteEnvValue(value: string): string {
|
||||
if ((value.startsWith("'") && value.endsWith("'")) || (value.startsWith("\"") && value.endsWith("\""))) return value.slice(1, -1);
|
||||
return value;
|
||||
}
|
||||
|
||||
export function compactText(value: string, maxChars = 500): string {
|
||||
return value.replace(/\s+/gu, " ").trim().slice(0, maxChars);
|
||||
}
|
||||
|
||||
export function shQuote(value: string): string {
|
||||
return `'${value.replaceAll("'", "'\"'\"'")}'`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user