@yedoma-labs/kulyn-flags 0.0.0-stage → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","names":[],"sources":["../src/errors.ts","../src/dates.ts","../src/lifetimes.ts","../src/hygiene.ts","../src/registry.ts","../src/scan.ts"],"sourcesContent":["import type { HygieneCode, HygieneFinding } from \"./types\";\n\nexport type FlagRegistryErrorCode =\n\t| \"INVALID_DEFINITION\"\n\t| \"INVALID_KEY\"\n\t| \"DUPLICATE_KEY\"\n\t| \"EMPTY_OWNER\"\n\t| \"INVALID_KIND\"\n\t| \"INVALID_DESCRIPTION\"\n\t| \"INVALID_DATE\"\n\t| \"EXPIRY_NOT_AFTER_CREATION\"\n\t| \"MISSING_EXPIRY\"\n\t| \"EXCEEDS_MAX_LIFETIME\"\n\t| \"INVALID_OPTIONS\";\n\n/**\n * Thrown when a flag register or its options are invalid.\n */\nexport class FlagRegistryError extends Error {\n\treadonly code: FlagRegistryErrorCode;\n\t/** The offending flag key, when it is known. */\n\treadonly key: string | undefined;\n\n\tconstructor(code: FlagRegistryErrorCode, message: string, key?: string) {\n\t\tsuper(message);\n\t\tthis.name = \"FlagRegistryError\";\n\t\tthis.code = code;\n\t\tthis.key = key;\n\t}\n}\n\n/**\n * Thrown by `assertFlagHygiene` when findings match the `failOn` codes.\n */\nexport class FlagHygieneError extends Error {\n\treadonly code: HygieneCode;\n\treadonly findings: readonly HygieneFinding[];\n\n\tconstructor(findings: readonly HygieneFinding[]) {\n\t\tconst lines = findings.map((finding) => ` - [${finding.code}] ${finding.message}`);\n\t\tsuper(`Flag hygiene check failed with ${findings.length} finding(s):\\n${lines.join(\"\\n\")}`);\n\t\tthis.name = \"FlagHygieneError\";\n\t\tthis.code = findings[0]?.code ?? \"UNUSED\";\n\t\tthis.findings = findings;\n\t}\n}\n","export const DAY_MS = 86_400_000;\n\n/**\n * `YYYY-MM-DD`, optionally followed by `T HH:MM[:SS[.fraction]]` and a mandatory timezone.\n * Only fixed quantifiers and a single optional group, so matching is linear.\n */\nconst ISO_PATTERN =\n\t/^(\\d{4})-(\\d{2})-(\\d{2})(?:T(\\d{2}):(\\d{2})(?::(\\d{2})(?:\\.(\\d{1,9}))?)?(Z|[+-]\\d{2}:\\d{2}))?$/;\n\nfunction isLeapYear(year: number): boolean {\n\treturn (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;\n}\n\nfunction daysInMonth(year: number, month: number): number {\n\tconst days = [31, isLeapYear(year) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];\n\treturn days[month - 1] ?? 0;\n}\n\n/**\n * Parses a strict ISO 8601 date or date-time and returns epoch milliseconds.\n * Returns `undefined` for anything that is not a real calendar date and time.\n */\nexport function parseIsoDate(value: unknown): number | undefined {\n\tif (typeof value !== \"string\") {\n\t\treturn undefined;\n\t}\n\tconst match = ISO_PATTERN.exec(value);\n\tif (match === null) {\n\t\treturn undefined;\n\t}\n\n\tconst year = Number(match[1]);\n\tconst month = Number(match[2]);\n\tconst day = Number(match[3]);\n\tif (month < 1 || month > 12 || day < 1 || day > daysInMonth(year, month)) {\n\t\treturn undefined;\n\t}\n\n\tif (match[4] !== undefined) {\n\t\tconst hours = Number(match[4]);\n\t\tconst minutes = Number(match[5]);\n\t\tconst seconds = match[6] === undefined ? 0 : Number(match[6]);\n\t\tif (hours > 23 || minutes > 59 || seconds > 59) {\n\t\t\treturn undefined;\n\t\t}\n\t\tconst offset = match[8];\n\t\tif (offset !== undefined && offset !== \"Z\") {\n\t\t\tconst offsetHours = Number(offset.slice(1, 3));\n\t\t\tconst offsetMinutes = Number(offset.slice(4, 6));\n\t\t\tif (offsetHours > 23 || offsetMinutes > 59) {\n\t\t\t\treturn undefined;\n\t\t\t}\n\t\t}\n\t}\n\n\tconst time = Date.parse(value);\n\treturn Number.isNaN(time) ? undefined : time;\n}\n","import { FlagRegistryError } from \"./errors\";\nimport type { FlagKind, LifetimeOverrides, MaxLifetimeDays } from \"./types\";\n\nexport const FLAG_KINDS: readonly FlagKind[] = [\"release\", \"experiment\", \"ops\", \"permission\"];\n\n/**\n * Default maximum lifetimes. `permission` flags may live without limit.\n */\nexport const DEFAULT_MAX_LIFETIME_DAYS: MaxLifetimeDays = Object.freeze({\n\trelease: 90,\n\texperiment: 30,\n\tops: 365,\n\tpermission: null,\n});\n\nexport function isFlagKind(value: unknown): value is FlagKind {\n\treturn typeof value === \"string\" && (FLAG_KINDS as readonly string[]).includes(value);\n}\n\n/**\n * Merges user overrides into the defaults and validates them.\n */\nexport function resolveMaxLifetimeDays(overrides: LifetimeOverrides | undefined): MaxLifetimeDays {\n\tconst resolved: Record<FlagKind, number | null> = { ...DEFAULT_MAX_LIFETIME_DAYS };\n\tif (overrides === undefined) {\n\t\treturn resolved;\n\t}\n\tif (typeof overrides !== \"object\" || overrides === null) {\n\t\tthrow new FlagRegistryError(\"INVALID_OPTIONS\", \"maxLifetimeDays must be an object\");\n\t}\n\n\tfor (const kind of Object.keys(overrides)) {\n\t\tif (!isFlagKind(kind)) {\n\t\t\tthrow new FlagRegistryError(\n\t\t\t\t\"INVALID_OPTIONS\",\n\t\t\t\t`Unknown flag kind in maxLifetimeDays: \"${kind}\"`,\n\t\t\t);\n\t\t}\n\t\tconst value = overrides[kind];\n\t\tif (value === undefined) {\n\t\t\tcontinue;\n\t\t}\n\t\tif (value !== null && !(Number.isInteger(value) && value > 0)) {\n\t\t\tthrow new FlagRegistryError(\n\t\t\t\t\"INVALID_OPTIONS\",\n\t\t\t\t`maxLifetimeDays.${kind} must be a positive integer or null, got ${String(value)}`,\n\t\t\t);\n\t\t}\n\t\tresolved[kind] = value;\n\t}\n\treturn resolved;\n}\n","import { DAY_MS, parseIsoDate } from \"./dates\";\nimport { FlagHygieneError, FlagRegistryError } from \"./errors\";\nimport { isFlagKind, resolveMaxLifetimeDays } from \"./lifetimes\";\nimport type {\n\tAssertFlagHygieneOptions,\n\tCheckFlagHygieneOptions,\n\tFlagDefinition,\n\tHygieneCode,\n\tHygieneFinding,\n} from \"./types\";\n\n/** Order of codes in the result when two findings share a key. */\nconst CODE_ORDER: readonly HygieneCode[] = [\n\t\"EXPIRED\",\n\t\"EXCEEDS_MAX_LIFETIME\",\n\t\"UNUSED\",\n\t\"UNREGISTERED_USAGE\",\n];\n\n/**\n * Checks a flag register for flags that should be cleaned up.\n *\n * - `EXPIRED`: `now` is at or after `expiresAt`.\n * - `EXCEEDS_MAX_LIFETIME`: the flag lives longer than the configured maximum for its kind.\n * This catches flags registered before a limit was lowered.\n * - `UNUSED`: only when `usedKeys` is given. The key is registered but not used.\n * - `UNREGISTERED_USAGE`: only when `usedKeys` is given. The key is used but not registered.\n *\n * Findings are sorted by key, then by code.\n */\nexport function checkFlagHygiene(options: CheckFlagHygieneOptions): readonly HygieneFinding[] {\n\tconst nowMs = resolveNow(options.now);\n\tconst lifetimes = resolveMaxLifetimeDays(options.maxLifetimeDays);\n\n\tconst registered = new Set<string>();\n\tconst findings: HygieneFinding[] = [];\n\n\tfor (const flag of options.flags) {\n\t\tif (registered.has(flag.key)) {\n\t\t\tthrow new FlagRegistryError(\"DUPLICATE_KEY\", `Duplicate flag key \"${flag.key}\"`, flag.key);\n\t\t}\n\t\tregistered.add(flag.key);\n\t\tcollectLifecycleFindings(flag, nowMs, lifetimes[flag.kind], findings);\n\t}\n\n\tif (options.usedKeys !== undefined) {\n\t\tconst used = new Set(options.usedKeys);\n\t\tfor (const key of registered) {\n\t\t\tif (!used.has(key)) {\n\t\t\t\tfindings.push({\n\t\t\t\t\tkey,\n\t\t\t\t\tcode: \"UNUSED\",\n\t\t\t\t\tmessage: `Flag \"${key}\" is registered but not used`,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t\tfor (const key of used) {\n\t\t\tif (!registered.has(key)) {\n\t\t\t\tfindings.push({\n\t\t\t\t\tkey,\n\t\t\t\t\tcode: \"UNREGISTERED_USAGE\",\n\t\t\t\t\tmessage: `Flag \"${key}\" is used but not registered`,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t}\n\n\treturn findings.sort(\n\t\t(a, b) =>\n\t\t\tcompareStrings(a.key, b.key) || CODE_ORDER.indexOf(a.code) - CODE_ORDER.indexOf(b.code),\n\t);\n}\n\n/**\n * Throws `FlagHygieneError` if any finding has a code listed in `failOn`.\n * Defaults to all codes. Use it in tests or CI.\n */\nexport function assertFlagHygiene(\n\tfindings: readonly HygieneFinding[],\n\toptions?: AssertFlagHygieneOptions,\n): void {\n\tconst failOn = new Set<HygieneCode>(options?.failOn ?? CODE_ORDER);\n\tconst failing = findings.filter((finding) => failOn.has(finding.code));\n\tif (failing.length > 0) {\n\t\tthrow new FlagHygieneError(failing);\n\t}\n}\n\nfunction collectLifecycleFindings(\n\tflag: FlagDefinition,\n\tnowMs: number,\n\tmaxDays: number | null | undefined,\n\tfindings: HygieneFinding[],\n): void {\n\tif (!isFlagKind(flag.kind)) {\n\t\tthrow new FlagRegistryError(\"INVALID_KIND\", `Flag \"${flag.key}\" has invalid kind`, flag.key);\n\t}\n\tconst createdMs = parseIsoDate(flag.createdAt);\n\tif (createdMs === undefined) {\n\t\tthrow new FlagRegistryError(\n\t\t\t\"INVALID_DATE\",\n\t\t\t`Flag \"${flag.key}\" has invalid createdAt`,\n\t\t\tflag.key,\n\t\t);\n\t}\n\tif (flag.expiresAt === undefined) {\n\t\treturn;\n\t}\n\tconst expiresMs = parseIsoDate(flag.expiresAt);\n\tif (expiresMs === undefined) {\n\t\tthrow new FlagRegistryError(\n\t\t\t\"INVALID_DATE\",\n\t\t\t`Flag \"${flag.key}\" has invalid expiresAt`,\n\t\t\tflag.key,\n\t\t);\n\t}\n\n\tif (nowMs >= expiresMs) {\n\t\tfindings.push({\n\t\t\tkey: flag.key,\n\t\t\tcode: \"EXPIRED\",\n\t\t\tmessage: `Flag \"${flag.key}\" expired at ${flag.expiresAt}`,\n\t\t});\n\t}\n\n\tif (maxDays !== null && maxDays !== undefined) {\n\t\tconst lifetimeMs = expiresMs - createdMs;\n\t\tif (lifetimeMs > maxDays * DAY_MS) {\n\t\t\tfindings.push({\n\t\t\t\tkey: flag.key,\n\t\t\t\tcode: \"EXCEEDS_MAX_LIFETIME\",\n\t\t\t\tmessage: `Flag \"${flag.key}\" (${flag.kind}) lives ${lifetimeMs / DAY_MS} days, the maximum is ${maxDays}`,\n\t\t\t});\n\t\t}\n\t}\n}\n\nfunction resolveNow(now: Date | undefined): number {\n\tif (now === undefined) {\n\t\treturn Date.now();\n\t}\n\tconst time = now.getTime();\n\tif (Number.isNaN(time)) {\n\t\tthrow new FlagRegistryError(\"INVALID_OPTIONS\", \"now is an invalid Date\");\n\t}\n\treturn time;\n}\n\nfunction compareStrings(a: string, b: string): number {\n\tif (a === b) {\n\t\treturn 0;\n\t}\n\treturn a < b ? -1 : 1;\n}\n","import { DAY_MS, parseIsoDate } from \"./dates\";\nimport { FlagRegistryError } from \"./errors\";\nimport { isFlagKind, resolveMaxLifetimeDays } from \"./lifetimes\";\nimport type { DefineFlagsOptions, FlagDefinition, MaxLifetimeDays } from \"./types\";\n\nconst KEY_PATTERN = /^[a-z][a-z0-9-.]{1,79}$/;\n\n/**\n * Validates a flag register and returns a frozen, normalized copy.\n *\n * Throws `FlagRegistryError` with a `code` for the first invalid entry.\n * A flag kind with a finite maximum lifetime requires `expiresAt`, so that a flag\n * cannot escape the limit by omitting the date.\n */\nexport function defineFlags(\n\tdefinitions: readonly FlagDefinition[],\n\toptions?: DefineFlagsOptions,\n): readonly FlagDefinition[] {\n\tif (!Array.isArray(definitions)) {\n\t\tthrow new FlagRegistryError(\"INVALID_DEFINITION\", \"defineFlags expects an array of flags\");\n\t}\n\tconst maxLifetimeDays = resolveMaxLifetimeDays(options?.maxLifetimeDays);\n\n\tconst seen = new Set<string>();\n\tconst result: FlagDefinition[] = [];\n\tfor (const [index, raw] of definitions.entries()) {\n\t\tconst flag = validateDefinition(raw, index, maxLifetimeDays);\n\t\tif (seen.has(flag.key)) {\n\t\t\tthrow new FlagRegistryError(\"DUPLICATE_KEY\", `Duplicate flag key \"${flag.key}\"`, flag.key);\n\t\t}\n\t\tseen.add(flag.key);\n\t\tresult.push(flag);\n\t}\n\treturn Object.freeze(result);\n}\n\nfunction validateDefinition(\n\traw: unknown,\n\tindex: number,\n\tmaxLifetimeDays: MaxLifetimeDays,\n): FlagDefinition {\n\tif (typeof raw !== \"object\" || raw === null) {\n\t\tthrow new FlagRegistryError(\"INVALID_DEFINITION\", `Flag at index ${index} is not an object`);\n\t}\n\tconst record = raw as Record<string, unknown>;\n\n\tconst key = record.key;\n\tif (typeof key !== \"string\" || !KEY_PATTERN.test(key)) {\n\t\tthrow new FlagRegistryError(\n\t\t\t\"INVALID_KEY\",\n\t\t\t`Invalid flag key at index ${index}: ${JSON.stringify(key)} (must match ${KEY_PATTERN.source})`,\n\t\t\ttypeof key === \"string\" ? key : undefined,\n\t\t);\n\t}\n\n\tconst owner = record.owner;\n\tif (typeof owner !== \"string\" || owner.trim() === \"\") {\n\t\tthrow new FlagRegistryError(\"EMPTY_OWNER\", `Flag \"${key}\" has no owner`, key);\n\t}\n\n\tconst kind = record.kind;\n\tif (!isFlagKind(kind)) {\n\t\tthrow new FlagRegistryError(\n\t\t\t\"INVALID_KIND\",\n\t\t\t`Flag \"${key}\" has invalid kind ${JSON.stringify(kind)}`,\n\t\t\tkey,\n\t\t);\n\t}\n\n\tconst createdAt = record.createdAt;\n\tconst createdMs = parseIsoDate(createdAt);\n\tif (createdMs === undefined) {\n\t\tthrow new FlagRegistryError(\n\t\t\t\"INVALID_DATE\",\n\t\t\t`Flag \"${key}\" has invalid createdAt ${JSON.stringify(createdAt)}`,\n\t\t\tkey,\n\t\t);\n\t}\n\n\tconst expiresAt = record.expiresAt;\n\tlet expiresMs: number | undefined;\n\tif (expiresAt !== undefined) {\n\t\texpiresMs = parseIsoDate(expiresAt);\n\t\tif (expiresMs === undefined) {\n\t\t\tthrow new FlagRegistryError(\n\t\t\t\t\"INVALID_DATE\",\n\t\t\t\t`Flag \"${key}\" has invalid expiresAt ${JSON.stringify(expiresAt)}`,\n\t\t\t\tkey,\n\t\t\t);\n\t\t}\n\t\tif (expiresMs <= createdMs) {\n\t\t\tthrow new FlagRegistryError(\n\t\t\t\t\"EXPIRY_NOT_AFTER_CREATION\",\n\t\t\t\t`Flag \"${key}\" expires at ${String(expiresAt)}, which is not after createdAt`,\n\t\t\t\tkey,\n\t\t\t);\n\t\t}\n\t}\n\n\tconst maxDays = maxLifetimeDays[kind];\n\tif (maxDays !== null) {\n\t\tif (expiresMs === undefined) {\n\t\t\tthrow new FlagRegistryError(\n\t\t\t\t\"MISSING_EXPIRY\",\n\t\t\t\t`Flag \"${key}\" (${kind}) needs an expiresAt, the maximum lifetime is ${maxDays} days`,\n\t\t\t\tkey,\n\t\t\t);\n\t\t}\n\t\tif (expiresMs - createdMs > maxDays * DAY_MS) {\n\t\t\tthrow new FlagRegistryError(\n\t\t\t\t\"EXCEEDS_MAX_LIFETIME\",\n\t\t\t\t`Flag \"${key}\" (${kind}) lives longer than the maximum of ${maxDays} days`,\n\t\t\t\tkey,\n\t\t\t);\n\t\t}\n\t}\n\n\tconst description = record.description;\n\tif (description !== undefined && typeof description !== \"string\") {\n\t\tthrow new FlagRegistryError(\n\t\t\t\"INVALID_DESCRIPTION\",\n\t\t\t`Flag \"${key}\" has a non-string description`,\n\t\t\tkey,\n\t\t);\n\t}\n\n\tconst flag: FlagDefinition = { key, owner, kind, createdAt: createdAt as string };\n\tif (expiresAt !== undefined) {\n\t\tflag.expiresAt = expiresAt as string;\n\t}\n\tif (description !== undefined) {\n\t\tflag.description = description;\n\t}\n\treturn flag;\n}\n","import type { FindFlagKeysOptions } from \"./types\";\n\nconst DEFAULT_FUNCTIONS: readonly string[] = [\"isEnabled\", \"getFlag\", \"flag\", \"evaluate\"];\n\n/** Upper bound for a key literal. Longer literals are ignored. */\nconst MAX_LITERAL_LENGTH = 100;\n\n/**\n * Finds flag keys in source text.\n *\n * Recognized forms, with single or double quotes:\n * - `isEnabled(\"key\")`, `getFlag('key')` and the other configured function names\n * - `flags.key` (identifier-style keys only)\n * - `flags[\"key\"]`\n *\n * Text in comments, and template literals (backticks), are ignored, including `${...}`\n * placeholders in template literals that are not code. Calls inside `${...}` are still scanned.\n * Calls and property access written inside a quoted string (`\"isEnabled('x')\"`) are ignored,\n * because they are text, not code.\n * This function does not read files. The caller passes the text.\n *\n * Limitations: regex literals containing quotes are not understood. A quote inside a\n * regex literal can hide code on the same line.\n */\nexport function findFlagKeysInSource(\n\tsourceText: string,\n\toptions?: FindFlagKeysOptions,\n): Set<string> {\n\tconst functions = options?.functions ?? DEFAULT_FUNCTIONS;\n\tconst { code, stringRanges } = maskWithStrings(sourceText);\n\tconst keys = new Set<string>();\n\n\tif (functions.length > 0) {\n\t\tconst alternation = functions.map(escapeRegExp).join(\"|\");\n\t\t// The literal must be closed by the same quote and be a whole argument, so that\n\t\t// `isEnabled(\"a\" + b)` is not read as the key \"a\".\n\t\tconst callPattern = new RegExp(\n\t\t\tString.raw`\\b(?:${alternation})\\s*\\(\\s*([\"'])([^\"'\\\\\\r\\n]{1,${MAX_LITERAL_LENGTH}})\\1(?=\\s*[,)])`,\n\t\t\t\"g\",\n\t\t);\n\t\tfor (const match of code.matchAll(callPattern)) {\n\t\t\tif (isInRanges(stringRanges, match.index)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tconst key = match[2];\n\t\t\tif (key !== undefined) {\n\t\t\t\tkeys.add(key);\n\t\t\t}\n\t\t}\n\t}\n\n\t// `flags.key` is only matched for identifier-like keys, and not for method calls like\n\t// `flags.get(` or chained access like `flags.a.b`.\n\tfor (const match of code.matchAll(/\\bflags\\.([a-z][a-z0-9]*)(?![\\w.-]|\\s*\\()/g)) {\n\t\tif (isInRanges(stringRanges, match.index)) {\n\t\t\tcontinue;\n\t\t}\n\t\tconst key = match[1];\n\t\tif (key !== undefined) {\n\t\t\tkeys.add(key);\n\t\t}\n\t}\n\n\tconst bracketPattern = new RegExp(\n\t\tString.raw`\\bflags\\s*\\[\\s*([\"'])([^\"'\\\\\\r\\n]{1,${MAX_LITERAL_LENGTH}})\\1\\s*\\]`,\n\t\t\"g\",\n\t);\n\tfor (const match of code.matchAll(bracketPattern)) {\n\t\tif (isInRanges(stringRanges, match.index)) {\n\t\t\tcontinue;\n\t\t}\n\t\tconst key = match[2];\n\t\tif (key !== undefined) {\n\t\t\tkeys.add(key);\n\t\t}\n\t}\n\n\treturn keys;\n}\n\ntype Frame = { kind: \"template\" } | { kind: \"code\"; braces: number; interpolation: boolean };\n\n/**\n * Returns `source` with comments and template-literal text replaced by spaces.\n * Newlines are kept, so the output has the same length and line structure.\n * Single- and double-quoted strings are kept, because keys are read from them.\n * Runs in linear time: each character is visited a constant number of times.\n */\nexport function maskNonCode(source: string): string {\n\treturn maskWithStrings(source).code;\n}\n\n/**\n * Like `maskNonCode`, and also returns the ranges of quoted string literals as flat\n * `[start, end, start, end, ...]` pairs (end is exclusive), in source order.\n */\nfunction maskWithStrings(source: string): { code: string; stringRanges: number[] } {\n\tconst out = source.split(\"\");\n\tconst n = source.length;\n\tconst stringRanges: number[] = [];\n\tconst frames: Frame[] = [{ kind: \"code\", braces: 0, interpolation: false }];\n\n\tconst mask = (from: number, to: number): void => {\n\t\tfor (let k = from; k < to; k++) {\n\t\t\tif (out[k] !== \"\\n\" && out[k] !== \"\\r\") {\n\t\t\t\tout[k] = \" \";\n\t\t\t}\n\t\t}\n\t};\n\n\tlet i = 0;\n\twhile (i < n) {\n\t\tconst top = frames[frames.length - 1];\n\t\tif (top === undefined) {\n\t\t\tbreak;\n\t\t}\n\t\tconst ch = source[i];\n\t\tconst next = source[i + 1];\n\n\t\tif (top.kind === \"template\") {\n\t\t\tif (ch === \"\\\\\") {\n\t\t\t\tmask(i, i + 2);\n\t\t\t\ti += 2;\n\t\t\t} else if (ch === \"`\") {\n\t\t\t\tmask(i, i + 1);\n\t\t\t\tframes.pop();\n\t\t\t\ti++;\n\t\t\t} else if (ch === \"$\" && next === \"{\") {\n\t\t\t\tmask(i, i + 2);\n\t\t\t\tframes.push({ kind: \"code\", braces: 0, interpolation: true });\n\t\t\t\ti += 2;\n\t\t\t} else {\n\t\t\t\tmask(i, i + 1);\n\t\t\t\ti++;\n\t\t\t}\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (ch === \"/\" && next === \"/\") {\n\t\t\tconst newline = source.indexOf(\"\\n\", i);\n\t\t\tconst end = newline === -1 ? n : newline;\n\t\t\tmask(i, end);\n\t\t\ti = end;\n\t\t} else if (ch === \"/\" && next === \"*\") {\n\t\t\tconst close = source.indexOf(\"*/\", i + 2);\n\t\t\tconst end = close === -1 ? n : close + 2;\n\t\t\tmask(i, end);\n\t\t\ti = end;\n\t\t} else if (ch === '\"' || ch === \"'\") {\n\t\t\t// Strings end at the matching quote or at a line break (unterminated string).\n\t\t\tlet j = i + 1;\n\t\t\twhile (j < n && source[j] !== ch && source[j] !== \"\\n\") {\n\t\t\t\tj += source[j] === \"\\\\\" ? 2 : 1;\n\t\t\t}\n\t\t\tconst end = source[j] === ch ? j + 1 : j;\n\t\t\tstringRanges.push(i, end);\n\t\t\ti = end;\n\t\t} else if (ch === \"`\") {\n\t\t\tmask(i, i + 1);\n\t\t\tframes.push({ kind: \"template\" });\n\t\t\ti++;\n\t\t} else if (ch === \"{\") {\n\t\t\ttop.braces++;\n\t\t\ti++;\n\t\t} else if (ch === \"}\") {\n\t\t\tif (top.interpolation && top.braces === 0) {\n\t\t\t\tmask(i, i + 1);\n\t\t\t\tframes.pop();\n\t\t\t} else if (top.braces > 0) {\n\t\t\t\ttop.braces--;\n\t\t\t}\n\t\t\ti++;\n\t\t} else {\n\t\t\ti++;\n\t\t}\n\t}\n\n\treturn { code: out.join(\"\"), stringRanges };\n}\n\n/** True when `index` lies in one of the sorted `[start, end)` pairs of `ranges`. */\nfunction isInRanges(ranges: readonly number[], index: number | undefined): boolean {\n\tif (index === undefined) {\n\t\treturn false;\n\t}\n\tlet lo = 0;\n\tlet hi = ranges.length / 2 - 1;\n\twhile (lo <= hi) {\n\t\tconst mid = (lo + hi) >> 1;\n\t\tconst start = ranges[mid * 2] ?? 0;\n\t\tconst end = ranges[mid * 2 + 1] ?? 0;\n\t\tif (index < start) {\n\t\t\thi = mid - 1;\n\t\t} else if (index >= end) {\n\t\t\tlo = mid + 1;\n\t\t} else {\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n}\n\nfunction escapeRegExp(value: string): string {\n\treturn value.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\n}\n"],"mappings":";;;;;AAkBA,IAAa,oBAAb,cAAuC,MAAM;CAC5C;;CAEA;CAEA,YAAY,MAA6B,SAAiB,KAAc;EACvE,MAAM,OAAO;EACb,KAAK,OAAO;EACZ,KAAK,OAAO;EACZ,KAAK,MAAM;CACZ;AACD;;;;AAKA,IAAa,mBAAb,cAAsC,MAAM;CAC3C;CACA;CAEA,YAAY,UAAqC;EAChD,MAAM,QAAQ,SAAS,KAAK,YAAY,QAAQ,QAAQ,KAAK,IAAI,QAAQ,SAAS;EAClF,MAAM,kCAAkC,SAAS,OAAO,gBAAgB,MAAM,KAAK,IAAI,GAAG;EAC1F,KAAK,OAAO;EACZ,KAAK,OAAO,SAAS,EAAE,EAAE,QAAQ;EACjC,KAAK,WAAW;CACjB;AACD;;AC7CA,MAAa,SAAS;;;;;AAMtB,MAAM,cACL;AAED,SAAS,WAAW,MAAuB;CAC1C,OAAQ,OAAO,MAAM,KAAK,OAAO,QAAQ,KAAM,OAAO,QAAQ;AAC/D;AAEA,SAAS,YAAY,MAAc,OAAuB;CAEzD,OAAO;EADO;EAAI,WAAW,IAAI,IAAI,KAAK;EAAI;EAAI;EAAI;EAAI;EAAI;EAAI;EAAI;EAAI;EAAI;EAAI;CACxE,CAAC,CAAC,QAAQ,MAAM;AAC3B;;;;;AAMA,SAAgB,aAAa,OAAoC;CAChE,IAAI,OAAO,UAAU,UACp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GACtB,IAAI;GACJ;EACD,OAAO,IAAI,OAAO,KAAK;GACtB,IAAI,IAAI,iBAAiB,IAAI,WAAW,GAAG;IAC1C,KAAK,GAAG,IAAI,CAAC;IACb,OAAO,IAAI;GACZ,OAAO,IAAI,IAAI,SAAS,GACvB,IAAI;GAEL;EACD,OACC;CAEF;CAEA,OAAO;EAAE,MAAM,IAAI,KAAK,EAAE;EAAG;CAAa;AAC3C;;AAGA,SAAS,WAAW,QAA2B,OAAoC;CAClF,IAAI,UAAU,KAAA,GACb,OAAO;CAER,IAAI,KAAK;CACT,IAAI,KAAK,OAAO,SAAS,IAAI;CAC7B,OAAO,MAAM,IAAI;EAChB,MAAM,MAAO,KAAK,MAAO;EACzB,MAAM,QAAQ,OAAO,MAAM,MAAM;EACjC,MAAM,MAAM,OAAO,MAAM,IAAI,MAAM;EACnC,IAAI,QAAQ,OACX,KAAK,MAAM;OACL,IAAI,SAAS,KACnB,KAAK,MAAM;OAEX,OAAO;CAET;CACA,OAAO;AACR;AAEA,SAAS,aAAa,OAAuB;CAC5C,OAAO,MAAM,QAAQ,uBAAuB,MAAM;AACnD"}
@@ -0,0 +1,127 @@
1
+ /**
2
+ * Why a flag exists. Each kind has its own default maximum lifetime.
3
+ */
4
+ type FlagKind = "release" | "experiment" | "ops" | "permission";
5
+ /**
6
+ * A single entry in the flag register.
7
+ *
8
+ * Dates are ISO 8601 strings. `createdAt` and `expiresAt` accept `YYYY-MM-DD` or a
9
+ * date-time with an explicit timezone (`Z` or `+HH:MM`).
10
+ */
11
+ interface FlagDefinition {
12
+ /** Flag key, e.g. `checkout.new-ui`. Must match `/^[a-z][a-z0-9-.]{1,79}$/`. */
13
+ key: string;
14
+ /** Team or person responsible for removing the flag. Must not be blank. */
15
+ owner: string;
16
+ kind: FlagKind;
17
+ createdAt: string;
18
+ /** Date after which the flag is considered expired. */
19
+ expiresAt?: string;
20
+ description?: string;
21
+ }
22
+ /**
23
+ * Per-kind maximum lifetime in days. `null` disables the limit for that kind.
24
+ */
25
+ type MaxLifetimeDays = Readonly<Record<FlagKind, number | null>>;
26
+ /**
27
+ * Overrides for the default maximum lifetimes. Keys that are omitted keep their default.
28
+ */
29
+ type LifetimeOverrides = Partial<Record<FlagKind, number | null>>;
30
+ interface DefineFlagsOptions {
31
+ maxLifetimeDays?: LifetimeOverrides;
32
+ }
33
+ type HygieneCode = "EXPIRED" | "EXCEEDS_MAX_LIFETIME" | "UNUSED" | "UNREGISTERED_USAGE";
34
+ interface HygieneFinding {
35
+ key: string;
36
+ code: HygieneCode;
37
+ message: string;
38
+ }
39
+ interface CheckFlagHygieneOptions {
40
+ flags: readonly FlagDefinition[];
41
+ /** Reference time for expiry checks. Defaults to the current time. */
42
+ now?: Date;
43
+ /**
44
+ * Flag keys found in the code base. When omitted, no usage findings are produced.
45
+ */
46
+ usedKeys?: Iterable<string>;
47
+ /** Maximum lifetimes used for `EXCEEDS_MAX_LIFETIME`. Defaults match `defineFlags`. */
48
+ maxLifetimeDays?: LifetimeOverrides;
49
+ }
50
+ interface AssertFlagHygieneOptions {
51
+ /** Finding codes that cause a throw. Defaults to all codes. */
52
+ failOn?: readonly HygieneCode[];
53
+ }
54
+ interface FindFlagKeysOptions {
55
+ /**
56
+ * Function names whose first string argument is a flag key.
57
+ * Defaults to `isEnabled`, `getFlag`, `flag` and `evaluate`.
58
+ */
59
+ functions?: readonly string[];
60
+ }
61
+ type FlagRegistryErrorCode = "INVALID_DEFINITION" | "INVALID_KEY" | "DUPLICATE_KEY" | "EMPTY_OWNER" | "INVALID_KIND" | "INVALID_DESCRIPTION" | "INVALID_DATE" | "EXPIRY_NOT_AFTER_CREATION" | "MISSING_EXPIRY" | "EXCEEDS_MAX_LIFETIME" | "INVALID_OPTIONS";
62
+ /**
63
+ * Thrown when a flag register or its options are invalid.
64
+ */
65
+ export declare class FlagRegistryError extends Error {
66
+ readonly code: FlagRegistryErrorCode;
67
+ /** The offending flag key, when it is known. */
68
+ readonly key: string | undefined;
69
+ constructor(code: FlagRegistryErrorCode, message: string, key?: string);
70
+ }
71
+ /**
72
+ * Thrown by `assertFlagHygiene` when findings match the `failOn` codes.
73
+ */
74
+ export declare class FlagHygieneError extends Error {
75
+ readonly code: HygieneCode;
76
+ readonly findings: readonly HygieneFinding[];
77
+ constructor(findings: readonly HygieneFinding[]);
78
+ }
79
+ /**
80
+ * Checks a flag register for flags that should be cleaned up.
81
+ *
82
+ * - `EXPIRED`: `now` is at or after `expiresAt`.
83
+ * - `EXCEEDS_MAX_LIFETIME`: the flag lives longer than the configured maximum for its kind.
84
+ * This catches flags registered before a limit was lowered.
85
+ * - `UNUSED`: only when `usedKeys` is given. The key is registered but not used.
86
+ * - `UNREGISTERED_USAGE`: only when `usedKeys` is given. The key is used but not registered.
87
+ *
88
+ * Findings are sorted by key, then by code.
89
+ */
90
+ export declare function checkFlagHygiene(options: CheckFlagHygieneOptions): readonly HygieneFinding[];
91
+ /**
92
+ * Throws `FlagHygieneError` if any finding has a code listed in `failOn`.
93
+ * Defaults to all codes. Use it in tests or CI.
94
+ */
95
+ export declare function assertFlagHygiene(findings: readonly HygieneFinding[], options?: AssertFlagHygieneOptions): void;
96
+ /**
97
+ * Default maximum lifetimes. `permission` flags may live without limit.
98
+ */
99
+ export declare const DEFAULT_MAX_LIFETIME_DAYS: MaxLifetimeDays;
100
+ /**
101
+ * Validates a flag register and returns a frozen, normalized copy.
102
+ *
103
+ * Throws `FlagRegistryError` with a `code` for the first invalid entry.
104
+ * A flag kind with a finite maximum lifetime requires `expiresAt`, so that a flag
105
+ * cannot escape the limit by omitting the date.
106
+ */
107
+ export declare function defineFlags(definitions: readonly FlagDefinition[], options?: DefineFlagsOptions): readonly FlagDefinition[];
108
+ /**
109
+ * Finds flag keys in source text.
110
+ *
111
+ * Recognized forms, with single or double quotes:
112
+ * - `isEnabled("key")`, `getFlag('key')` and the other configured function names
113
+ * - `flags.key` (identifier-style keys only)
114
+ * - `flags["key"]`
115
+ *
116
+ * Text in comments, and template literals (backticks), are ignored, including `${...}`
117
+ * placeholders in template literals that are not code. Calls inside `${...}` are still scanned.
118
+ * Calls and property access written inside a quoted string (`"isEnabled('x')"`) are ignored,
119
+ * because they are text, not code.
120
+ * This function does not read files. The caller passes the text.
121
+ *
122
+ * Limitations: regex literals containing quotes are not understood. A quote inside a
123
+ * regex literal can hide code on the same line.
124
+ */
125
+ export declare function findFlagKeysInSource(sourceText: string, options?: FindFlagKeysOptions): Set<string>;
126
+ export type { AssertFlagHygieneOptions, CheckFlagHygieneOptions, DefineFlagsOptions, FindFlagKeysOptions, FlagDefinition, FlagKind, FlagRegistryErrorCode, HygieneCode, HygieneFinding, LifetimeOverrides, MaxLifetimeDays };
127
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1,127 @@
1
+ /**
2
+ * Why a flag exists. Each kind has its own default maximum lifetime.
3
+ */
4
+ type FlagKind = "release" | "experiment" | "ops" | "permission";
5
+ /**
6
+ * A single entry in the flag register.
7
+ *
8
+ * Dates are ISO 8601 strings. `createdAt` and `expiresAt` accept `YYYY-MM-DD` or a
9
+ * date-time with an explicit timezone (`Z` or `+HH:MM`).
10
+ */
11
+ interface FlagDefinition {
12
+ /** Flag key, e.g. `checkout.new-ui`. Must match `/^[a-z][a-z0-9-.]{1,79}$/`. */
13
+ key: string;
14
+ /** Team or person responsible for removing the flag. Must not be blank. */
15
+ owner: string;
16
+ kind: FlagKind;
17
+ createdAt: string;
18
+ /** Date after which the flag is considered expired. */
19
+ expiresAt?: string;
20
+ description?: string;
21
+ }
22
+ /**
23
+ * Per-kind maximum lifetime in days. `null` disables the limit for that kind.
24
+ */
25
+ type MaxLifetimeDays = Readonly<Record<FlagKind, number | null>>;
26
+ /**
27
+ * Overrides for the default maximum lifetimes. Keys that are omitted keep their default.
28
+ */
29
+ type LifetimeOverrides = Partial<Record<FlagKind, number | null>>;
30
+ interface DefineFlagsOptions {
31
+ maxLifetimeDays?: LifetimeOverrides;
32
+ }
33
+ type HygieneCode = "EXPIRED" | "EXCEEDS_MAX_LIFETIME" | "UNUSED" | "UNREGISTERED_USAGE";
34
+ interface HygieneFinding {
35
+ key: string;
36
+ code: HygieneCode;
37
+ message: string;
38
+ }
39
+ interface CheckFlagHygieneOptions {
40
+ flags: readonly FlagDefinition[];
41
+ /** Reference time for expiry checks. Defaults to the current time. */
42
+ now?: Date;
43
+ /**
44
+ * Flag keys found in the code base. When omitted, no usage findings are produced.
45
+ */
46
+ usedKeys?: Iterable<string>;
47
+ /** Maximum lifetimes used for `EXCEEDS_MAX_LIFETIME`. Defaults match `defineFlags`. */
48
+ maxLifetimeDays?: LifetimeOverrides;
49
+ }
50
+ interface AssertFlagHygieneOptions {
51
+ /** Finding codes that cause a throw. Defaults to all codes. */
52
+ failOn?: readonly HygieneCode[];
53
+ }
54
+ interface FindFlagKeysOptions {
55
+ /**
56
+ * Function names whose first string argument is a flag key.
57
+ * Defaults to `isEnabled`, `getFlag`, `flag` and `evaluate`.
58
+ */
59
+ functions?: readonly string[];
60
+ }
61
+ type FlagRegistryErrorCode = "INVALID_DEFINITION" | "INVALID_KEY" | "DUPLICATE_KEY" | "EMPTY_OWNER" | "INVALID_KIND" | "INVALID_DESCRIPTION" | "INVALID_DATE" | "EXPIRY_NOT_AFTER_CREATION" | "MISSING_EXPIRY" | "EXCEEDS_MAX_LIFETIME" | "INVALID_OPTIONS";
62
+ /**
63
+ * Thrown when a flag register or its options are invalid.
64
+ */
65
+ export declare class FlagRegistryError extends Error {
66
+ readonly code: FlagRegistryErrorCode;
67
+ /** The offending flag key, when it is known. */
68
+ readonly key: string | undefined;
69
+ constructor(code: FlagRegistryErrorCode, message: string, key?: string);
70
+ }
71
+ /**
72
+ * Thrown by `assertFlagHygiene` when findings match the `failOn` codes.
73
+ */
74
+ export declare class FlagHygieneError extends Error {
75
+ readonly code: HygieneCode;
76
+ readonly findings: readonly HygieneFinding[];
77
+ constructor(findings: readonly HygieneFinding[]);
78
+ }
79
+ /**
80
+ * Checks a flag register for flags that should be cleaned up.
81
+ *
82
+ * - `EXPIRED`: `now` is at or after `expiresAt`.
83
+ * - `EXCEEDS_MAX_LIFETIME`: the flag lives longer than the configured maximum for its kind.
84
+ * This catches flags registered before a limit was lowered.
85
+ * - `UNUSED`: only when `usedKeys` is given. The key is registered but not used.
86
+ * - `UNREGISTERED_USAGE`: only when `usedKeys` is given. The key is used but not registered.
87
+ *
88
+ * Findings are sorted by key, then by code.
89
+ */
90
+ export declare function checkFlagHygiene(options: CheckFlagHygieneOptions): readonly HygieneFinding[];
91
+ /**
92
+ * Throws `FlagHygieneError` if any finding has a code listed in `failOn`.
93
+ * Defaults to all codes. Use it in tests or CI.
94
+ */
95
+ export declare function assertFlagHygiene(findings: readonly HygieneFinding[], options?: AssertFlagHygieneOptions): void;
96
+ /**
97
+ * Default maximum lifetimes. `permission` flags may live without limit.
98
+ */
99
+ export declare const DEFAULT_MAX_LIFETIME_DAYS: MaxLifetimeDays;
100
+ /**
101
+ * Validates a flag register and returns a frozen, normalized copy.
102
+ *
103
+ * Throws `FlagRegistryError` with a `code` for the first invalid entry.
104
+ * A flag kind with a finite maximum lifetime requires `expiresAt`, so that a flag
105
+ * cannot escape the limit by omitting the date.
106
+ */
107
+ export declare function defineFlags(definitions: readonly FlagDefinition[], options?: DefineFlagsOptions): readonly FlagDefinition[];
108
+ /**
109
+ * Finds flag keys in source text.
110
+ *
111
+ * Recognized forms, with single or double quotes:
112
+ * - `isEnabled("key")`, `getFlag('key')` and the other configured function names
113
+ * - `flags.key` (identifier-style keys only)
114
+ * - `flags["key"]`
115
+ *
116
+ * Text in comments, and template literals (backticks), are ignored, including `${...}`
117
+ * placeholders in template literals that are not code. Calls inside `${...}` are still scanned.
118
+ * Calls and property access written inside a quoted string (`"isEnabled('x')"`) are ignored,
119
+ * because they are text, not code.
120
+ * This function does not read files. The caller passes the text.
121
+ *
122
+ * Limitations: regex literals containing quotes are not understood. A quote inside a
123
+ * regex literal can hide code on the same line.
124
+ */
125
+ export declare function findFlagKeysInSource(sourceText: string, options?: FindFlagKeysOptions): Set<string>;
126
+ export type { AssertFlagHygieneOptions, CheckFlagHygieneOptions, DefineFlagsOptions, FindFlagKeysOptions, FlagDefinition, FlagKind, FlagRegistryErrorCode, HygieneCode, HygieneFinding, LifetimeOverrides, MaxLifetimeDays };
127
+ //# sourceMappingURL=index.d.ts.map