// ONE LINE OF THE SOURCE MIRROR'S SCRUB FORMAT, split — the pure half of // parseScrubRules (common/publish/source.ts), moved here unchanged so the // display rules (lib/privacy/compile.ts, settings.privacy.aliases) read the // same format with the same parser, in any runtime: no node imports. // // A line is `lhs==>rhs` (split at the LAST `==>`, as git-filter-repo splits // it), `literal:lhs==>rhs`, `regex:…==>…` or `glob:…==>…`; a line with no // `==>` is replaced by filter-repo's `***REMOVED***`. Lines whose first // non-blank character is `#` are comments. An empty left side is an error. export const SCRUB_REMOVED = "***REMOVED***"; export type ScrubLineKind = "plain" | "literal" | "regex" | "glob"; export type ScrubLine = { kind: ScrubLineKind; // The left side with its prefix removed (plain: the whole left side). pattern: string; // The right side, or null when the line has no `==>`. replacement: string | null; }; export type ScrubLineResult = { ok: true; line: ScrubLine } | { ok: false; error: string }; /** * An operator file's text as lines: a UTF-8 byte-order mark at the start is * dropped and CRLF endings become LF. An editor that writes either must not * turn the first rule (and the denial it implies) into one that never matches. */ export function operatorLines(text: string): string[] { return text.replace(/^\uFEFF/, "").split("\n").map((l) => l.replace(/\r$/, "")); } /** A blank line or a comment: no rule. */ export function isScrubSkip(line: string): boolean { const t = line.trim(); return t === "" || t.startsWith("#"); } /** One (non-skip) line, split. `n` is its 1-based line number, for the error. */ export function splitScrubLine(line: string, n: number): ScrubLineResult { const i = line.lastIndexOf("==>"); const lhs = i === -1 ? line : line.slice(0, i); const replacement = i === -1 ? null : line.slice(i + 3); for (const prefix of ["regex:", "glob:", "literal:"] as const) { if (lhs.startsWith(prefix)) { if (lhs.length === prefix.length) { return { ok: false, error: `scrub line ${n} has an empty left side — it would match nothing and deny nothing` }; } return { ok: true, line: { kind: prefix.slice(0, -1) as ScrubLineKind, pattern: lhs.slice(prefix.length), replacement } }; } } if (i === 0) { return { ok: false, error: `scrub line ${n} has an empty left side — it would match nothing and deny nothing` }; } return { ok: true, line: { kind: "plain", pattern: lhs, replacement } }; } /** The literal a line denies by implication ("" for regex and glob lines). */ export function scrubLineDenied(line: ScrubLine): string { return line.kind === "plain" || line.kind === "literal" ? line.pattern : ""; }