# JavaScript API

whatfrees is an ES module for Node.js 22.20 or later on Linux or macOS. It has four entries.

| Import | Exports |
| --- | --- |
| `whatfrees` | `scan`, `check`, `ScanOptions` and every report type |
| `whatfrees/measure` | `measure`, `diskUsage` and their types |
| `whatfrees/sources` | `sources`, `defineSource`, `runSources`, `quote`, `isAllowed`, `ALLOWED_COMMANDS` and the source types |
| `whatfrees/schema` | `REPORT_SCHEMA`, `CHECK_SCHEMA` and the report types |

Nothing here deletes, moves or changes a file. `scan` writes only the size cache.

## scan

```ts
import { scan } from "whatfrees";

const report = await scan({ min: 10 * 1024 ** 2, budget: 2 });
console.log(report.totals, report.partial);
```

`scan(options?)` returns a `Report`. It rejects on Windows. See [the JSON report](https://whatfrees.loadsheet.dev/reference/report) for its shape.

| Option | Type | Default | Meaning |
| --- | --- | --- | --- |
| `only` | `string[]` | all sources | Source ids to run. An unknown id throws |
| `roots` | `string[]` | none | Extra folders for the `worktrees` source |
| `budget` | number | `5` | Seconds for the whole scan. With `0`, owners answer and nothing is walked |
| `exact` | boolean | `false` | Walk every tree to the end, with no cache |
| `cache` | boolean | `true` | Read and write the size cache |
| `min` | number | `0` | Leave out entries that free fewer bytes. The totals still count them. The command line passes 50 MB |
| `all` | boolean | `false` | Keep the entries `min` and `live` would leave out |
| `sources` | `Source[]` | the built-in ones | Sources to run instead |

The library's `min` defaults to `0`. The command line's `--min` defaults to 50 MB.

## check

```ts
import { check } from "whatfrees";

const answer = await check("/home/you/.codex/worktrees/9f2c/app");
if (answer.verdict === "safe") console.log("go on");
```

`check(path)` returns a `CheckResult`: `{ schema, path, exists, verdict, deletable, evidence }`. `deletable` is `true` only for `safe` and `orphan` on a path that exists. Check the `verdict` itself before automatic cleanup: an `orphan` still needs manual review. See [whatfrees check](https://whatfrees.loadsheet.dev/guide/check).

## measure

```ts
import { measure } from "whatfrees/measure";

const [tree] = await measure(["/home/you/app/node_modules"], { deadlineMs: 2000 });
console.log(tree.frees, tree.apparent, tree.shared, tree.precision);
```

`measure(paths, options?)` walks trees in parallel and returns one `Measured` per path. It doesn't follow symlinks or cross into another file system.

| Option | Default | Meaning |
| --- | --- | --- |
| `deadlineMs` | none | Time budget. A tree still being read comes back as a `lower-bound` |
| `workers` | `4` | Walker threads. `0` walks in the calling thread |
| `signal` | none | An `AbortSignal`. Aborting rejects the call |

A `Measured` has `path`, `frees`, `apparent`, `shared`, `precision`, `files` and, when the path couldn't be read, `error` with the code, such as `ENOENT`. Here `precision` is `exact`, `lower-bound` or `unmeasured`. `files` counts everything that isn't a folder.

`diskUsage(path)` returns `{ free, total }` in bytes for the file system that holds `path`.

## Defining a source

A source is one place that holds space. Build it with `defineSource`, then pass it to `scan`.

```ts
import { scan } from "whatfrees";
import { defineSource } from "whatfrees/sources";

const scratch = defineSource({
  id: "scratch",
  description: "Folders named scratch in the project.",
  platforms: ["linux", "darwin"],
  detect: () => true,
  entries: () => [
    {
      what: "scratch folder",
      path: "/home/you/app/scratch",
      verdict: "ask",
      evidence: ["Nothing says what it is for."],
      command: ["rm", "-rf", "--", "/home/you/app/scratch"],
    },
  ],
});

const report = await scan({ sources: [scratch], min: 0 });
```

| Field | Meaning |
| --- | --- |
| `id` | Lowercase words joined by `-`. Anything else throws |
| `description` | What it covers. `whatfrees sources` prints it |
| `platforms` | `"linux"`, `"darwin"` or both |
| `detect(ctx)` | Whether there is anything to look at. Keep it cheap: a file check or a `PATH` lookup |
| `entries(ctx)` | What it found, as `Found` items. May be async |

`ctx` holds `platform`, `home`, `env`, `roots`, a `signal` that aborts when the budget runs out, and a `deadline`. Stop work when the signal aborts.

A `Found` has `what`, `verdict` and `command`. It may also have `key`, `path`, `bytes`, `evidence`, `then` and `lastUsed`.

- Leave out `bytes` and whatfrees measures `path`. Pass `bytes` when the owner already knows the size.
- `command` is the words of a command, verb first. whatfrees quotes them. A command whose verb isn't in `ALLOWED_COMMANDS` throws, and the source is skipped with `reason: "error"`.
- `key` is unique within the source. It defaults to `path`. The entry's id is `<source id>:<key>`.

Ask the owner first. Run subprocesses with a timeout and pass them `ctx.signal`. Never delete in a source.

### runSources

`runSources(list, options)` runs sources under one budget and returns `{ entries, skipped, partial }`. `options` needs `budgetMs`, `platform`, `home` and `env`. `scan` builds on it and adds the totals, the disk and the filters.

## Types

`whatfrees` and `whatfrees/schema` export `Report`, `Entry`, `Bytes`, `Disk`, `Skipped`, `Next`, `CheckResult`, `Verdict` and `Precision`. `whatfrees/schema` also exports the two schema ids as constants.

```ts
import { CHECK_SCHEMA, REPORT_SCHEMA, type Report } from "whatfrees/schema";

function isReport(value: { schema?: string }): value is Report {
  return value.schema === REPORT_SCHEMA;
}
```

<!-- vitehub-docs:section-links -->
## In this section

- [Command line](https://whatfrees.loadsheet.dev/raw/reference/cli.md)
- [JSON report](https://whatfrees.loadsheet.dev/raw/reference/report.md)
- [JavaScript API](https://whatfrees.loadsheet.dev/raw/reference/api.md) (this page)

All pages: [llms.txt](https://whatfrees.loadsheet.dev/llms.txt)

Something on this page does not work as written? Tell the user, and draft an issue for them to post: [issue tracker](https://github.com/words-with-agents/whatfrees/issues). Report a security vulnerability privately, never in a public issue.
