# runtime/entrypoints/leak.entrypoint.ts

> 67 lines of code and 11 definitions.

Tree: Build tree
Language: typescript
Layer: runtime
Canonical: https://banes-lab.com/anatomy/build#file-build-runtime-entrypoints-leak-entrypoint-ts
Source text: https://banes-lab.com/assets/sources/source.2e3db1a851b3664ae3d70a5c4e464a8c5e9e783751a80e31afbcc8e90d10ca7d.generated.txt

## Definitions

- `derive` (lexical_declaration, line 32)
- `validate` (lexical_declaration, line 49)
- `argv` (lexical_declaration, line 26)
- `inventory` (lexical_declaration, line 33)
- `seeds` (lexical_declaration, line 37)
- `target` (lexical_declaration, line 42)
- `leaks` (lexical_declaration, line 50)
- `graphs` (lexical_declaration, line 54)
- `roots` (lexical_declaration, line 58)
- `findings` (lexical_declaration, line 59)
- `payload` (lexical_declaration, line 61)

## Uses

- [core/loaders/inventory.loader.ts](https://banes-lab.com/source/build/core/loaders/inventory.loader.ts.md)
- [core/loaders/leak.loader.ts](https://banes-lab.com/source/build/core/loaders/leak.loader.ts.md)
- [core/loaders/seed.loader.ts](https://banes-lab.com/source/build/core/loaders/seed.loader.ts.md)
- [core/persistence/report.persistence.ts](https://banes-lab.com/source/build/core/persistence/report.persistence.ts.md)
- [core/reporters/derivation.reporter.ts](https://banes-lab.com/source/build/core/reporters/derivation.reporter.ts.md)
- [core/validators/leak.validator.ts](https://banes-lab.com/source/build/core/validators/leak.validator.ts.md)

## Source

```typescript
import {
    CLEAN_LEAKS,
    NO_GRAPHS,
    NO_INVENTORY,
    NO_LEAK_SET,
    NO_SEEDS,
    SOURCES_SUFFIX,
    TOKENS_SUFFIX,
    WROTE,
} from "#configuration/strings/derivation.strings";
import { DERIVE_DESCRIBE, LEAK_SUMMARY } from "#configuration/strings/contract.strings";
import { conclude, refuse, say } from "#core/reporters/derivation.reporter";
import { deriveLeakSet, leakTarget, readLeakSet, workspaceRoots } from "#core/loaders/leak.loader";
import { hasFlag, resolveArgv } from "@govlab/argv";
import { inventoryTarget, readInventory } from "#core/loaders/inventory.loader";
import { readSeeds, seedsTarget } from "#core/loaders/seed.loader";
import { DERIVE_FLAG } from "#configuration/constants/leak.constants";
import type { Finding } from "#types/derivation.types";
import { LEAK_COMMAND } from "#configuration/constants/contract.constants";
import { PAYLOAD_MISSING } from "#configuration/strings/coverage.strings";
import { loadContentGraphs } from "#core/loaders/coverage.loader";
import { persistJson } from "#core/persistence/report.persistence";
import { readPagePayload } from "#core/loaders/payload.loader";
import { validatePayload } from "#core/validators/leak.validator";

const argv = resolveArgv({
    command: LEAK_COMMAND,
    flags: [{ describe: DERIVE_DESCRIBE, name: DERIVE_FLAG, takesValue: false }],
    summary: LEAK_SUMMARY,
});

const derive = async function derive(): Promise<void> {
    const inventory = readInventory();
    if (inventory === null) {
        refuse(`${NO_INVENTORY}${inventoryTarget()}`);
    }
    const seeds = readSeeds();
    if (seeds === null) {
        refuse(`${NO_SEEDS}${seedsTarget()}`);
    }
    const leaks = deriveLeakSet(inventory, seeds);
    const target = leakTarget();
    await persistJson(target, leaks);
    say(
        `${WROTE}${target}: ${String(leaks.tokens.length)}${TOKENS_SUFFIX}${String(leaks.sources.length)}${SOURCES_SUFFIX}`,
    );
};

const validate = async function validate(): Promise<void> {
    const leaks = readLeakSet();
    if (leaks === null) {
        refuse(`${NO_LEAK_SET}${leakTarget()}`);
    }
    const graphs = await loadContentGraphs();
    if (graphs.length === 0) {
        say(NO_GRAPHS);
    }
    const roots = workspaceRoots();
    const findings: Finding[] = [];
    for (const graph of graphs) {
        const payload = readPagePayload(graph.page);
        if (payload === null) {
            findings.push({ file: graph.page, message: `${PAYLOAD_MISSING}${graph.page}` });
            continue;
        }
        findings.push(...validatePayload(payload, leaks, roots));
    }
    conclude(LEAK_COMMAND, findings, CLEAN_LEAKS);
};

await (hasFlag(argv, DERIVE_FLAG) ? derive() : validate());
```
