@zgeoff/chex 0.3.7 → 0.3.9

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.
package/README.md CHANGED
@@ -52,15 +52,15 @@ The docs checks read the repo's READMEs, its `docs/` tree and its skills:
52
52
 
53
53
  The repo checks read the branch, the pushed commits and the repo's config:
54
54
 
55
- | Check | Fails on | Arguments |
56
- | ------------------ | ------------------------------------------------------------------------------------------------------------------------------ | --------------- |
57
- | `commits` | a pushed commit whose message fails `@zgeoff/commitlint-config` | `--pushed-refs` |
58
- | `local-paths` | a line a pushed commit adds that holds the home folder or the host name | `--pushed-refs` |
59
- | `test-timeouts` | a `bun test` timeout above its lane's limit, in a script or a `bunfig.toml` | none |
60
- | `test-layout` | a test folder, file name or import outside [the test layout](../../docs/guides/test-layout.md) | none |
61
- | `workflows` | nothing: it warns on each fault of the shared workflow lint, and passes | the paths |
62
- | `dependency-gates` | a stale age-gate exclude, an override or an audit ignore that nothing needs, or a first-party package with no age-gate exclude | none |
63
- | `uncommitted` | a file the branch changes that holds a change the push does not send | none |
55
+ | Check | Fails on | Arguments |
56
+ | ------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------- |
57
+ | `commits` | a pushed commit whose message fails `@zgeoff/commitlint-config` | `--pushed-refs` |
58
+ | `local-paths` | a line a pushed commit adds that holds the home folder or the host name | `--pushed-refs` |
59
+ | `test-timeouts` | a `bun test` timeout above its lane's limit, in a script or a `bunfig.toml` | none |
60
+ | `test-layout` | a test folder, file name or import outside [the test layout](../../docs/guides/test-layout.md) | none |
61
+ | `workflows` | nothing: it warns on each fault of the shared workflow lint, and passes | the paths |
62
+ | `dependency-gates` | a stale age-gate exclude, an override or an audit ignore that nothing needs, a first-party package with no age-gate exclude, or a pinned release younger than the age gate ([details](../../docs/architecture/dependency-updates.md#the-dependency-gates-check)) | none |
63
+ | `uncommitted` | a file the branch changes that holds a change the push does not send | none |
64
64
 
65
65
  With no path, `prose` and `links` read every README outside `.claude/` and `.agents/`, every file
66
66
  under `docs/`, each spike's `results.md`, and the Markdown under `.agents/skills/`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zgeoff/chex",
3
- "version": "0.3.7",
3
+ "version": "0.3.9",
4
4
  "description": "Run each shared docs and repo check as one subcommand, with the lefthook pre-push baseline that calls them",
5
5
  "keywords": [
6
6
  "checks",
@@ -44,6 +44,8 @@
44
44
  "@zgeoff/rig": "0.2.5",
45
45
  "@zgeoff/std": "0.4.3",
46
46
  "@zgeoff/testing": "0.3.0",
47
+ "@zgeoff/tools-scripts": "0.0.0",
48
+ "msw": "2.15.0",
47
49
  "tiny-invariant": "1.3.3",
48
50
  "tsrs": "0.18.0-ts7.1.0-dev.20260929",
49
51
  "typescript": "7.0.2"
@@ -41,9 +41,9 @@ export function collectStaleAgeGateExcludes(
41
41
  }
42
42
 
43
43
  // The shared audit-fix workflow writes this comment for each exclude it adds, and removes the
44
- // exclude once the date passes.
44
+ // exclude once the date passes. oxfmt indents the comment inside the array, so it may follow spaces or tabs.
45
45
  const removalCommentPattern =
46
- /^# (?<name>\S+) \S+ \(published [^)]*\) ages past the gate ~(?<date>\d{4}-\d{2}-\d{2}) — remove then/gmu;
46
+ /^[ \t]*# (?<name>\S+) \S+ \(published [^)]*\) ages past the gate ~(?<date>\d{4}-\d{2}-\d{2}) — remove then/gmu;
47
47
 
48
48
  function collectRemovalDates(bunfig: string): Map<string, string> {
49
49
  const removalDates = new Map<string, string>();
@@ -2,6 +2,9 @@ import { isRecord } from './is-record.ts';
2
2
 
3
3
  export interface AgeGate {
4
4
  readonly isOn: boolean;
5
+
6
+ // The gate in seconds; 0 when the gate is off.
7
+ readonly seconds: number;
5
8
  readonly excludes: readonly string[];
6
9
  }
7
10
 
@@ -15,8 +18,11 @@ export function parseAgeGate(bunfig: string): AgeGate {
15
18
  throw new TypeError('bunfig.toml sets a minimumReleaseAgeExcludes that is not a list of names');
16
19
  }
17
20
 
21
+ const seconds = parseGateSeconds(minimumReleaseAge);
22
+
18
23
  return {
19
- isOn: minimumReleaseAge !== undefined && minimumReleaseAge !== 0 && minimumReleaseAge !== '0',
24
+ isOn: seconds > 0,
25
+ seconds,
20
26
  excludes: excludes ?? [],
21
27
  };
22
28
  }
@@ -24,3 +30,30 @@ export function parseAgeGate(bunfig: string): AgeGate {
24
30
  function isStringList(value: unknown): value is string[] {
25
31
  return Array.isArray(value) && value.every((item: unknown) => typeof item === 'string');
26
32
  }
33
+
34
+ const unitSeconds = new Map([
35
+ ['s', 1],
36
+ ['m', 60],
37
+ ['h', 3600],
38
+ ['d', 86_400],
39
+ ['w', 604_800],
40
+ ]);
41
+
42
+ function parseGateSeconds(value: unknown): number {
43
+ if (value === undefined) {
44
+ return 0;
45
+ }
46
+
47
+ if (typeof value === 'number') {
48
+ return value;
49
+ }
50
+
51
+ const match = typeof value === 'string' ? /^(?<count>\d+)(?<unit>[smhdw]?)$/u.exec(value) : null;
52
+ const count = match?.groups?.['count'];
53
+
54
+ if (count === undefined) {
55
+ throw new TypeError('bunfig.toml sets a minimumReleaseAge that is not a number of seconds');
56
+ }
57
+
58
+ return Number(count) * (unitSeconds.get(match?.groups?.['unit'] ?? '') ?? 1);
59
+ }
@@ -2,7 +2,18 @@ import { isRecord } from './is-record.ts';
2
2
 
3
3
  // An entry's id is `<name>@<version>`. A workspace, link, file or git entry carries a protocol in
4
4
  // its version, such as `workspace:packages/std`, and bun never puts it through the age gate.
5
+ export interface LockfileRegistryPackage {
6
+ readonly name: string;
7
+ readonly version: string;
8
+ }
9
+
5
10
  export function parseLockfileRegistryNames(lockfile: string): string[] {
11
+ return [
12
+ ...new Set(parseLockfileRegistryPackages(lockfile).map((entry) => entry.name)),
13
+ ].toSorted();
14
+ }
15
+
16
+ export function parseLockfileRegistryPackages(lockfile: string): LockfileRegistryPackage[] {
6
17
  const parsed: unknown = Bun.JSONC.parse(lockfile);
7
18
  const packages = isRecord(parsed) ? parsed['packages'] : undefined;
8
19
 
@@ -10,7 +21,7 @@ export function parseLockfileRegistryNames(lockfile: string): string[] {
10
21
  throw new TypeError('bun.lock has no packages object');
11
22
  }
12
23
 
13
- const names = Object.values(packages).flatMap((entry) => {
24
+ const found = Object.values(packages).flatMap((entry) => {
14
25
  const id: unknown = Array.isArray(entry) ? entry[0] : undefined;
15
26
 
16
27
  if (typeof id !== 'string') {
@@ -20,8 +31,13 @@ export function parseLockfileRegistryNames(lockfile: string): string[] {
20
31
  const versionStart = id.indexOf('@', 1);
21
32
  const version = id.slice(versionStart + 1);
22
33
 
23
- return versionStart > 0 && !version.includes(':') ? [id.slice(0, versionStart)] : [];
34
+ return versionStart > 0 && !version.includes(':')
35
+ ? [{ name: id.slice(0, versionStart), version }]
36
+ : [];
24
37
  });
25
38
 
26
- return [...new Set(names)].toSorted();
39
+ return found.toSorted(
40
+ (left, right) =>
41
+ left.name.localeCompare(right.name) || left.version.localeCompare(right.version),
42
+ );
27
43
  }
@@ -8,9 +8,13 @@ import { parseAuditAdvisories } from './parse-audit-advisories.ts';
8
8
  import { parseAuditIgnoreIDs } from './parse-audit-ignore-ids.ts';
9
9
  import { parseFixablePackages } from './parse-fixable-packages.ts';
10
10
  import { parseLockfileDependents } from './parse-lockfile-dependents.ts';
11
- import { parseLockfileRegistryNames } from './parse-lockfile-registry-names.ts';
11
+ import {
12
+ parseLockfileRegistryNames,
13
+ parseLockfileRegistryPackages,
14
+ } from './parse-lockfile-registry-names.ts';
12
15
  import type { RootManifest } from './parse-root-manifest.ts';
13
16
  import { parseRootManifest } from './parse-root-manifest.ts';
17
+ import { readYoungLockedReleases } from './read-young-locked-releases.ts';
14
18
  import type { DependencyGateFinding } from './types.ts';
15
19
 
16
20
  export interface DependencyGateSources {
@@ -20,17 +24,20 @@ export interface DependencyGateSources {
20
24
  // An ISO date, such as 2026-03-08.
21
25
  readonly today: string;
22
26
 
27
+ // The time now, as epoch milliseconds.
28
+ readonly nowMs: number;
29
+
23
30
  readonly runProgram: RunProgram;
24
31
 
25
32
  // The path is relative to the repo root.
26
33
  readonly readRepoFile: (path: string) => string | null;
27
34
  }
28
35
 
29
- // The audit runs only for a repo that ignores an advisory, so a repo with no ignore needs no
30
- // network.
31
- export function readDependencyGateFindings(
36
+ // The audit runs only for a repo that ignores an advisory, and the registry only for a repo with an
37
+ // age gate, so a repo with neither needs no network.
38
+ export async function readDependencyGateFindings(
32
39
  sources: Readonly<DependencyGateSources>,
33
- ): DependencyGateFinding[] {
40
+ ): Promise<DependencyGateFinding[]> {
34
41
  const manifestText = sources.readRepoFile('package.json');
35
42
 
36
43
  if (manifestText === null) {
@@ -50,6 +57,13 @@ export function readDependencyGateFindings(
50
57
  parseAgeGate(bunfig),
51
58
  parseLockfileRegistryNames(lockfile),
52
59
  )),
60
+ ...(lockfile === null
61
+ ? []
62
+ : await readYoungLockedReleases({
63
+ ageGate: parseAgeGate(bunfig),
64
+ packages: parseLockfileRegistryPackages(lockfile),
65
+ nowMs: sources.nowMs,
66
+ })),
53
67
  ...collectOverrideFindings(manifest, lockfile),
54
68
  ...(ignoreIDs.length === 0 ? [] : readIgnoreFindings(sources, ignoreIDs)),
55
69
  ];
@@ -0,0 +1,61 @@
1
+ import { isRecord } from './is-record.ts';
2
+
3
+ const concurrency = 8;
4
+
5
+ // One request per package name: the packument holds the publish time of every version. The map
6
+ // goes from a package name to its versions' publish times, as epoch milliseconds.
7
+ export async function readPublishTimes(
8
+ names: readonly string[],
9
+ ): Promise<Map<string, Map<string, number>>> {
10
+ const times = new Map<string, Map<string, number>>();
11
+
12
+ const queue = [...names];
13
+
14
+ const work = async (): Promise<void> => {
15
+ for (let name = queue.shift(); name !== undefined; name = queue.shift()) {
16
+ const packageTimes = await readPackageTimes(name);
17
+
18
+ times.set(name, packageTimes);
19
+ }
20
+ };
21
+
22
+ await Promise.all(Array.from({ length: Math.min(concurrency, names.length) }, work));
23
+
24
+ return times;
25
+ }
26
+
27
+ const registryURL = 'https://registry.npmjs.org/';
28
+
29
+ async function readPackageTimes(name: string): Promise<Map<string, number>> {
30
+ const url = `${registryURL}${name.replace('/', '%2f')}`;
31
+ let body: unknown;
32
+
33
+ try {
34
+ const response = await fetch(url);
35
+
36
+ if (!response.ok) {
37
+ throw new Error(`answered ${response.status}`);
38
+ }
39
+
40
+ body = await response.json();
41
+ } catch (error) {
42
+ throw new Error(
43
+ `could not read the publish times of ${name} from ${url}: ${error instanceof Error ? error.message : String(error)}`,
44
+ { cause: error },
45
+ );
46
+ }
47
+
48
+ const time = isRecord(body) ? body['time'] : undefined;
49
+
50
+ if (!isRecord(time)) {
51
+ throw new TypeError(`the registry document for ${name} has no time map`);
52
+ }
53
+
54
+ return new Map(
55
+ Object.entries(time).flatMap(([version, stamp]) =>
56
+ typeof stamp === 'string' && !Number.isNaN(Date.parse(stamp))
57
+ ? [[version, Date.parse(stamp)] as const]
58
+ : [],
59
+ ),
60
+ );
61
+ }
@@ -0,0 +1,52 @@
1
+ import { firstPartyScopes } from './first-party-scopes.ts';
2
+ import type { AgeGate } from './parse-age-gate.ts';
3
+ import type { LockfileRegistryPackage } from './parse-lockfile-registry-names.ts';
4
+ import { readPublishTimes } from './read-publish-times.ts';
5
+ import type { DependencyGateFinding } from './types.ts';
6
+
7
+ // Bun applies the gate when it resolves a version, not to one the lockfile already pins, so a
8
+ // lockfile written under an exclude keeps a young release after the exclude goes.
9
+ export interface YoungLockedReleaseSources {
10
+ readonly ageGate: AgeGate;
11
+ readonly packages: readonly LockfileRegistryPackage[];
12
+ readonly nowMs: number;
13
+ }
14
+
15
+ export async function readYoungLockedReleases(
16
+ options: Readonly<YoungLockedReleaseSources>,
17
+ ): Promise<DependencyGateFinding[]> {
18
+ if (!options.ageGate.isOn) {
19
+ return [];
20
+ }
21
+
22
+ const checked = options.packages.filter(
23
+ (entry) =>
24
+ !firstPartyScopes.some((scope) => entry.name.startsWith(scope)) &&
25
+ !options.ageGate.excludes.includes(entry.name) &&
26
+ !options.ageGate.excludes.includes(`${entry.name}@${entry.version}`),
27
+ );
28
+
29
+ const times = await readPublishTimes([...new Set(checked.map((entry) => entry.name))]);
30
+
31
+ return checked.flatMap((entry) => {
32
+ const name = entry.name;
33
+ const version = entry.version;
34
+ const publishedMs = times.get(name)?.get(version);
35
+
36
+ if (publishedMs === undefined) {
37
+ throw new Error(`the registry lists no publish time for ${name}@${version}`);
38
+ }
39
+
40
+ if (options.nowMs - publishedMs >= options.ageGate.seconds * 1000) {
41
+ return [];
42
+ }
43
+
44
+ return [
45
+ {
46
+ path: 'bun.lock',
47
+ entry: `${name}@${version}`,
48
+ problem: `bun.lock pins a release published ${new Date(publishedMs).toISOString()}, younger than the ${options.ageGate.seconds} second minimumReleaseAge, and no age-gate exclude covers it`,
49
+ },
50
+ ];
51
+ });
52
+ }
@@ -9,9 +9,11 @@ export interface DependencyGatesCheck extends DependencyGateSources {
9
9
  readonly writeError: (text: string) => void;
10
10
  }
11
11
 
12
- export function runDependencyGatesCheck(check: Readonly<DependencyGatesCheck>): number {
12
+ export async function runDependencyGatesCheck(
13
+ check: Readonly<DependencyGatesCheck>,
14
+ ): Promise<number> {
13
15
  try {
14
- const findings = readDependencyGateFindings(check);
16
+ const findings = await readDependencyGateFindings(check);
15
17
 
16
18
  for (const finding of findings) {
17
19
  check.writeOutput(`${finding.path}: ${finding.entry}: ${finding.problem}\n`);
@@ -27,13 +29,14 @@ export function runDependencyGatesCheck(check: Readonly<DependencyGatesCheck>):
27
29
  }
28
30
  }
29
31
 
30
- export function runDependencyGatesCommand(): number {
32
+ export function runDependencyGatesCommand(): Promise<number> {
31
33
  const cwd = process.cwd();
32
34
 
33
35
  return runDependencyGatesCheck({
34
36
  cwd,
35
37
  env: process.env,
36
38
  today: new Date().toISOString().slice(0, 10),
39
+ nowMs: Date.now(),
37
40
  runProgram,
38
41
  readRepoFile: (path) => {
39
42
  const fullPath = join(cwd, path);
@@ -50,5 +53,5 @@ export function runDependencyGatesCommand(): number {
50
53
  }
51
54
 
52
55
  if (import.meta.main) {
53
- process.exitCode = runDependencyGatesCommand();
56
+ process.exitCode = await runDependencyGatesCommand();
54
57
  }