cursedbelt 5.12.0 → 5.13.0
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/dist/react/components/ErrorBoundary.d.ts +10 -1
- package/dist/react/components/ErrorBoundary.d.ts.map +1 -1
- package/dist/react/components/ErrorBoundary.js +16 -22
- package/dist/react/components/ErrorBoundary.js.map +1 -1
- package/dist/react/lazy/staleChunk.d.ts +18 -1
- package/dist/react/lazy/staleChunk.d.ts.map +1 -1
- package/dist/react/lazy/staleChunk.js +44 -19
- package/dist/react/lazy/staleChunk.js.map +1 -1
- package/dist/scripts/guardrailsEnforce.d.ts +7 -0
- package/dist/scripts/guardrailsEnforce.d.ts.map +1 -1
- package/package.json +1 -1
- package/scripts/checkAreaStyles.spec.ts +47 -34
- package/scripts/guardrailsEnforce.spec.ts +101 -55
- package/scripts/guardrailsEnforce.ts +21 -4
- package/scripts/paths.ts +1 -1
- package/src/barrelsReachNoOptionalPeer.spec.ts +17 -12
- package/src/lockfileIsTracked.spec.ts +9 -5
- package/src/react/components/ErrorBoundary.tsx +16 -24
- package/src/react/components/StaleDeployHealer.spec.tsx +32 -1
- package/src/react/lazy/staleChunk.spec.tsx +23 -4
- package/src/react/lazy/staleChunk.ts +42 -17
- package/src/shippedFilesAreTracked.spec.ts +8 -8
- package/src/typecheckCachesAreSeparate.spec.ts +13 -7
- package/src/unpeeredStaysOffTheBarrel.spec.ts +14 -8
|
@@ -17,7 +17,15 @@
|
|
|
17
17
|
* boundary, rather than reloading in a loop.
|
|
18
18
|
*
|
|
19
19
|
* The memory is `sessionStorage`, which is per tab AND per origin, so one generic
|
|
20
|
-
* key serves every app without two of them ever reading each other's.
|
|
20
|
+
* key serves every app without two of them ever reading each other's. It is a TIME,
|
|
21
|
+
* not a flag: a tab left open across two deploys (a film playing for hours) heals
|
|
22
|
+
* both times. `ErrorBoundary`'s `StaleDeployHealer` reads and writes the same
|
|
23
|
+
* memory, so the two never add up to more than one reload a minute.
|
|
24
|
+
*
|
|
25
|
+
* 🔴 It fails CLOSED. Storage this tab cannot read or write (site data blocked)
|
|
26
|
+
* means no reload at all — the error goes to the boundary, which explains it —
|
|
27
|
+
* because a chunk genuinely missing from the deployed build would otherwise reload
|
|
28
|
+
* forever with nothing able to remember trying.
|
|
21
29
|
*
|
|
22
30
|
* `guardrailsEnforce`'s `no-raw-lazy` is the WHETHER: a bare `React.lazy` in an app
|
|
23
31
|
* is refused, so the next lazy view cannot quietly reopen this.
|
|
@@ -35,16 +43,44 @@ export interface StaleChunkEnv {
|
|
|
35
43
|
now: () => number;
|
|
36
44
|
}
|
|
37
45
|
|
|
38
|
-
function browserEnv(): StaleChunkEnv {
|
|
46
|
+
export function browserEnv(): StaleChunkEnv {
|
|
39
47
|
let storage: StaleChunkEnv['storage'] = null;
|
|
40
48
|
try {
|
|
41
49
|
storage = window.sessionStorage;
|
|
42
50
|
} catch {
|
|
43
|
-
// Storage refused (
|
|
51
|
+
// Storage refused (blocked site data) — no memory, so no reload: the guard fails closed.
|
|
44
52
|
}
|
|
45
53
|
return { storage, reload: () => window.location.reload(), now: Date.now };
|
|
46
54
|
}
|
|
47
55
|
|
|
56
|
+
/**
|
|
57
|
+
* Claim this tab's one stale-chunk reload for the window: true when a reload may
|
|
58
|
+
* happen now, and the claim is already recorded. False when one happened inside the
|
|
59
|
+
* window, or when the memory cannot be read or written — fail closed, see the header.
|
|
60
|
+
*/
|
|
61
|
+
export function claimStaleChunkReload(env: StaleChunkEnv = browserEnv()): boolean {
|
|
62
|
+
if (staleChunkReloadedRecently(env)) return false;
|
|
63
|
+
const stamp = String(env.now());
|
|
64
|
+
try {
|
|
65
|
+
env.storage?.setItem(STALE_CHUNK_RELOADED_AT_KEY, stamp);
|
|
66
|
+
return env.storage?.getItem(STALE_CHUNK_RELOADED_AT_KEY) === stamp;
|
|
67
|
+
} catch {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Whether this tab reloaded for a stale chunk inside the window — true, too, when it cannot tell. */
|
|
73
|
+
export function staleChunkReloadedRecently(env: StaleChunkEnv = browserEnv()): boolean {
|
|
74
|
+
const { storage, now } = env;
|
|
75
|
+
if (!storage) return true;
|
|
76
|
+
try {
|
|
77
|
+
const last = Number(storage.getItem(STALE_CHUNK_RELOADED_AT_KEY) ?? 0) || 0;
|
|
78
|
+
return last > 0 && now() - last < STALE_CHUNK_RELOAD_WINDOW_MS;
|
|
79
|
+
} catch {
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
48
84
|
/** Wrap a dynamic `import()` so a chunk a deploy removed reloads the page instead of failing. */
|
|
49
85
|
export function reloadOnStaleChunk<T>(
|
|
50
86
|
load: () => Promise<T>,
|
|
@@ -54,20 +90,9 @@ export function reloadOnStaleChunk<T>(
|
|
|
54
90
|
try {
|
|
55
91
|
return await load();
|
|
56
92
|
} catch (error) {
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
last = Number(storage?.getItem(STALE_CHUNK_RELOADED_AT_KEY) ?? 0) || 0;
|
|
61
|
-
} catch {
|
|
62
|
-
// Unreadable memory is treated as no reload yet.
|
|
63
|
-
}
|
|
64
|
-
if (last > 0 && now() - last < STALE_CHUNK_RELOAD_WINDOW_MS) throw error;
|
|
65
|
-
try {
|
|
66
|
-
storage?.setItem(STALE_CHUNK_RELOADED_AT_KEY, String(now()));
|
|
67
|
-
} catch {
|
|
68
|
-
// Unwritable: the reload still happens; the guard just cannot remember it.
|
|
69
|
-
}
|
|
70
|
-
reload();
|
|
93
|
+
const current = env();
|
|
94
|
+
if (!claimStaleChunkReload(current)) throw error;
|
|
95
|
+
current.reload();
|
|
71
96
|
return new Promise<T>(() => undefined);
|
|
72
97
|
}
|
|
73
98
|
};
|
|
@@ -25,7 +25,6 @@
|
|
|
25
25
|
* when `git` cannot answer — a check that silently degrades to green is worse than none.
|
|
26
26
|
*/
|
|
27
27
|
import { describe, expect, test } from 'bun:test';
|
|
28
|
-
import { spawnSync } from 'node:child_process';
|
|
29
28
|
import { existsSync } from 'node:fs';
|
|
30
29
|
import { fileURLToPath } from 'node:url';
|
|
31
30
|
import pkg from '../package.json';
|
|
@@ -36,14 +35,15 @@ const repoRoot = fileURLToPath(new URL('..', import.meta.url));
|
|
|
36
35
|
const shippedDirs = (): string[] =>
|
|
37
36
|
((pkg as { files?: string[] }).files ?? []).filter((f) => f !== 'dist' && existsSync(`${repoRoot}/${f}`));
|
|
38
37
|
|
|
39
|
-
const git = (...args: string[]): { ok: boolean; out: string } => {
|
|
40
|
-
const
|
|
41
|
-
|
|
38
|
+
const git = async (...args: string[]): Promise<{ ok: boolean; out: string }> => {
|
|
39
|
+
const child = Bun.spawn(['git', '-C', repoRoot, ...args], { stdin: 'ignore', stdout: 'pipe', stderr: 'pipe' });
|
|
40
|
+
const [out, , code] = await Promise.all([new Response(child.stdout).text(), new Response(child.stderr).text(), child.exited]);
|
|
41
|
+
return { ok: code === 0, out: out.trim() };
|
|
42
42
|
};
|
|
43
43
|
|
|
44
44
|
describe('every file inside the published surface is tracked', () => {
|
|
45
|
-
test('git can answer at all — a degraded check must refuse, not pass', () => {
|
|
46
|
-
expect(git('rev-parse', '--is-inside-work-tree').ok, 'not a git checkout, so this check proves nothing').toBe(true);
|
|
45
|
+
test('git can answer at all — a degraded check must refuse, not pass', async () => {
|
|
46
|
+
expect((await git('rev-parse', '--is-inside-work-tree')).ok, 'not a git checkout, so this check proves nothing').toBe(true);
|
|
47
47
|
});
|
|
48
48
|
|
|
49
49
|
test('package.json names a shippable directory to scan', () => {
|
|
@@ -52,10 +52,10 @@ describe('every file inside the published surface is tracked', () => {
|
|
|
52
52
|
expect(shippedDirs().length, '`files` names no committed directory — nothing would be scanned').toBeGreaterThan(0);
|
|
53
53
|
});
|
|
54
54
|
|
|
55
|
-
test('no untracked file sits inside a directory `files` ships', () => {
|
|
55
|
+
test('no untracked file sits inside a directory `files` ships', async () => {
|
|
56
56
|
// --others = untracked; --exclude-standard honours .gitignore, so deliberately
|
|
57
57
|
// ignored paths (.agent.noindex, node_modules) are not offences.
|
|
58
|
-
const { ok, out } = git('ls-files', '--others', '--exclude-standard', '--', ...shippedDirs());
|
|
58
|
+
const { ok, out } = await git('ls-files', '--others', '--exclude-standard', '--', ...shippedDirs());
|
|
59
59
|
expect(ok, 'git ls-files failed').toBe(true);
|
|
60
60
|
const untracked = out === '' ? [] : out.split('\n');
|
|
61
61
|
expect(
|
|
@@ -28,7 +28,6 @@
|
|
|
28
28
|
* ~90ms per project because `--showConfig` does no type checking at all.
|
|
29
29
|
*/
|
|
30
30
|
import { describe, expect, it } from 'bun:test';
|
|
31
|
-
import { spawnSync } from 'node:child_process';
|
|
32
31
|
import { type Dirent, readdirSync } from 'node:fs';
|
|
33
32
|
import { dirname, join, relative, resolve } from 'node:path';
|
|
34
33
|
|
|
@@ -78,13 +77,20 @@ interface Resolved {
|
|
|
78
77
|
emits: boolean;
|
|
79
78
|
}
|
|
80
79
|
|
|
81
|
-
function resolveProject(path: string): Resolved {
|
|
82
|
-
const
|
|
80
|
+
async function resolveProject(path: string): Promise<Resolved> {
|
|
81
|
+
const child = Bun.spawn([process.execPath, 'x', 'tsc', '--showConfig', '-p', path], {
|
|
83
82
|
cwd: ROOT,
|
|
84
|
-
|
|
83
|
+
stdin: 'ignore',
|
|
84
|
+
stdout: 'pipe',
|
|
85
|
+
stderr: 'pipe',
|
|
85
86
|
});
|
|
86
|
-
|
|
87
|
-
|
|
87
|
+
const [stdout, stderr, code] = await Promise.all([
|
|
88
|
+
new Response(child.stdout).text(),
|
|
89
|
+
new Response(child.stderr).text(),
|
|
90
|
+
child.exited,
|
|
91
|
+
]);
|
|
92
|
+
expect(code, `tsc --showConfig failed for ${path}:\n${stderr}`).toBe(0);
|
|
93
|
+
const options = (JSON.parse(stdout) as { compilerOptions?: Record<string, unknown> }).compilerOptions ?? {};
|
|
88
94
|
const info = options.tsBuildInfoFile;
|
|
89
95
|
return {
|
|
90
96
|
name: relative(ROOT, path),
|
|
@@ -94,7 +100,7 @@ function resolveProject(path: string): Resolved {
|
|
|
94
100
|
};
|
|
95
101
|
}
|
|
96
102
|
|
|
97
|
-
const PROJECTS = projectPaths().map(resolveProject);
|
|
103
|
+
const PROJECTS = await Promise.all(projectPaths().map(resolveProject));
|
|
98
104
|
|
|
99
105
|
describe('the typecheck caches', () => {
|
|
100
106
|
it('finds every tsconfig in the repo', () => {
|
|
@@ -36,18 +36,24 @@ const packageOf = (specifier: string): string => {
|
|
|
36
36
|
};
|
|
37
37
|
|
|
38
38
|
/** Every package an entry's bundle names, static or dynamic, with all packages left external. */
|
|
39
|
-
function packagesReachedBy(entry: string, label: string): Set<string
|
|
39
|
+
async function packagesReachedBy(entry: string, label: string): Promise<Set<string>> {
|
|
40
40
|
const outdir = `${process.env.TMPDIR ?? '/tmp'}/cursedbelt-unpeered-scan-${process.pid}/${label}`;
|
|
41
41
|
rmSync(outdir, { recursive: true, force: true });
|
|
42
|
-
const
|
|
42
|
+
const child = Bun.spawn(['bun', 'build', entry, '--target=browser', '--packages=external', '--outdir', outdir], {
|
|
43
43
|
cwd: REPO,
|
|
44
|
+
stdin: 'ignore',
|
|
44
45
|
stdout: 'pipe',
|
|
45
46
|
stderr: 'pipe',
|
|
46
47
|
});
|
|
48
|
+
const [, stderr, exitCode] = await Promise.all([
|
|
49
|
+
new Response(child.stdout).text(),
|
|
50
|
+
new Response(child.stderr).text(),
|
|
51
|
+
child.exited,
|
|
52
|
+
]);
|
|
47
53
|
const emitted = [...new Bun.Glob('**/*.js').scanSync({ cwd: outdir, onlyFiles: true })];
|
|
48
54
|
const bundle = emitted.map((f) => readFileSync(`${outdir}/${f}`, 'utf8')).join('\n');
|
|
49
|
-
if (
|
|
50
|
-
throw new Error(`could not bundle ${entry} (exit ${
|
|
55
|
+
if (exitCode !== 0 || bundle.trim() === '') {
|
|
56
|
+
throw new Error(`could not bundle ${entry} (exit ${exitCode}) — a check that cannot measure is not a passing check:\n${stderr}`);
|
|
51
57
|
}
|
|
52
58
|
const found = new Set<string>();
|
|
53
59
|
for (const m of bundle.matchAll(ANY_SPECIFIER)) {
|
|
@@ -58,10 +64,10 @@ function packagesReachedBy(entry: string, label: string): Set<string> {
|
|
|
58
64
|
}
|
|
59
65
|
|
|
60
66
|
describe('unpeered packages stay off the cursedbelt/react barrel', () => {
|
|
61
|
-
it('🔴 the barrel names no unpeered package, even lazily', () => {
|
|
67
|
+
it('🔴 the barrel names no unpeered package, even lazily', async () => {
|
|
62
68
|
const source = (pkg.exports as Record<string, { source?: string }>)['./react']?.source;
|
|
63
69
|
expect(source, 'package.json has no ./react source entry').toBeString();
|
|
64
|
-
const reached = packagesReachedBy(`${REPO}${(source as string).slice(2)}`, 'barrel');
|
|
70
|
+
const reached = await packagesReachedBy(`${REPO}${(source as string).slice(2)}`, 'barrel');
|
|
65
71
|
expect(reached.size, 'the scan found no packages at all — it is not measuring').toBeGreaterThan(0);
|
|
66
72
|
expect(
|
|
67
73
|
UNPEERED.filter((name) => reached.has(name)),
|
|
@@ -84,13 +90,13 @@ describe('unpeered packages stay off the cursedbelt/react barrel', () => {
|
|
|
84
90
|
expect(UNPEERED.filter((n) => !named.has(n))).toEqual([]);
|
|
85
91
|
});
|
|
86
92
|
|
|
87
|
-
it('the scan sees a lazy import — 5.0.0 reproduced, so this check can go red', () => {
|
|
93
|
+
it('the scan sees a lazy import — 5.0.0 reproduced, so this check can go red', async () => {
|
|
88
94
|
const dir = `${process.env.TMPDIR ?? '/tmp'}/cursedbelt-unpeered-fixture-${process.pid}`;
|
|
89
95
|
mkdirSync(dir, { recursive: true });
|
|
90
96
|
const entry = `${dir}/lazy.ts`;
|
|
91
97
|
writeFileSync(entry, "export const Picker = () => import('@emoji-mart/react');\nexport const zod = () => import('@hookform/resolvers/zod');\n");
|
|
92
98
|
try {
|
|
93
|
-
const reached = packagesReachedBy(entry, 'fixture');
|
|
99
|
+
const reached = await packagesReachedBy(entry, 'fixture');
|
|
94
100
|
expect([...reached].sort()).toEqual(['@emoji-mart/react', '@hookform/resolvers']);
|
|
95
101
|
} finally {
|
|
96
102
|
rmSync(dir, { recursive: true, force: true });
|