@a.incantalupo/web-tools 0.4.0 → 0.6.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/README.md CHANGED
@@ -14,8 +14,11 @@ stack-tools images <url...> [--budget 600] [--brief | --json] # which image fi
14
14
  stack-tools lighthouse <url...> [--budget] [--min-perf <n>] [--runs <n>] [--out dir] [--json] [--header 'Name: value'] # Lighthouse on phone and desktop; use it instead of the Lighthouse CLI (on WSL that one leaves Chrome's profile in your folder)
15
15
  stack-tools audit <url> [--profile file.ts] [--render] [--teaser] [--reviews a.json...] [--preview] [--header 'Name: value'] [--keys .env] [--out dir | --out-root dir]
16
16
  stack-tools audit-rerender <dir> [--profile file.ts] [--teaser] # rebuild a report from its findings.json, no new crawl
17
+ stack-tools vercel-headers [dir] # after the build: security headers on every answer of a prebuilt Vercel deploy
17
18
  ```
18
19
 
20
+ `vercel-headers` is a build step, not a check: `"build": "vite build && stack-tools vercel-headers"`. It puts one route first in `.vercel/output/config.json` (a prebuilt deploy never reads `vercel.json`) with `x-content-type-options: nosniff`, `x-frame-options: DENY`, `referrer-policy: strict-origin-when-cross-origin`, a `permissions-policy` and `cross-origin-opener-policy: same-origin`. The Content-Security-Policy stays the framework's, and HSTS Vercel's. Running it twice changes nothing.
21
+
19
22
  Outputs go where you run the command (`./design-refs`, `./site-eval/<host>/<date>`) unless `--out` or `--out-root` says otherwise. A project keeps its own outputs; nothing is written inside this package.
20
23
 
21
24
  ## Short output, for agents and scripts
@@ -47,6 +50,7 @@ FAIL /contact reading ease 61 · 1 fail · 2 warn (small-text, target-44)
47
50
  | Largest contentful paint | 2.5 s |
48
51
  | Layout shift (CLS) | 0.1 |
49
52
  | The server's first byte | 800 ms |
53
+ | Total blocking time (main-thread work while loading; the lab's stand-in for INP) | 200 ms |
50
54
  | Scripts, as sent | 100 kB |
51
55
 
52
56
  - A `FAIL` line names the number and its limit; for the largest paint, also the element that was timed.
@@ -47,7 +47,8 @@ await mkdir(outDir, { recursive: true });
47
47
  const runs = await runLighthouse(positionals, outDir, times);
48
48
  // The limits of a public page. Lab numbers on a simulated mid-range phone and on a
49
49
  // desktop: limits, not goals.
50
- const LIMITS = { lcpMs: 2500, cls: 0.1, ttfbMs: 800, scriptBytes: 100 * 1024 };
50
+ // TBT: the lab's stand-in for INP (main-thread work while the page loads); INP itself is field-only.
51
+ const LIMITS = { lcpMs: 2500, cls: 0.1, ttfbMs: 800, tbtMs: 200, scriptBytes: 100 * 1024 };
51
52
  const judging = values.budget || minPerf != null;
52
53
  const over = [];
53
54
  for (const r of runs) {
@@ -66,6 +67,8 @@ for (const r of runs) {
66
67
  add("cls", `CLS ${m.cls.toFixed(3)} over ${LIMITS.cls}`);
67
68
  if (m.ttfbMs != null && m.ttfbMs > LIMITS.ttfbMs)
68
69
  add("ttfb", `first byte ${Math.round(m.ttfbMs)} ms over ${LIMITS.ttfbMs} ms`);
70
+ if (m.tbtMs != null && m.tbtMs > LIMITS.tbtMs)
71
+ add("tbt", `blocking time ${Math.round(m.tbtMs)} ms over ${LIMITS.tbtMs} ms`);
69
72
  if (r.scriptBytes != null && r.scriptBytes > LIMITS.scriptBytes)
70
73
  add("js", `scripts ${Math.round(r.scriptBytes / 1024)} kB over ${LIMITS.scriptBytes / 1024} kB`);
71
74
  }
package/dist/cli.js CHANGED
@@ -19,7 +19,8 @@ const commands = {
19
19
  images: { file: "./refs/images.js", about: "which image file each slot downloads, weights, lazy LCP: <url...> [--brief|--json]" },
20
20
  lighthouse: { file: "./audit/lighthouse-cli.js", about: "Lighthouse on phone and desktop, judged against the budgets with --budget: <url...> [--budget] [--min-perf n] [--runs n] [--out dir] [--json]" },
21
21
  audit: { file: "./audit/cli.js", about: "audit a site (SEO, AI visibility, speed, contacts): <url> [--profile file.ts] [--render] [--teaser]" },
22
- "audit-rerender": { file: "./audit/rerender.js", about: "rebuild an audit report from its findings.json: <dir> [--profile file.ts] [--teaser]" }
22
+ "audit-rerender": { file: "./audit/rerender.js", about: "rebuild an audit report from its findings.json: <dir> [--profile file.ts] [--teaser]" },
23
+ "vercel-headers": { file: "./vercel/headers-cli.js", about: "add the security headers to a Vercel build output, after the build: [dir]" }
23
24
  };
24
25
  const name = process.argv[2];
25
26
  const command = name ? commands[name] : undefined;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,18 @@
1
+ // stack-tools vercel-headers [dir]: adds the security headers to the Vercel build output of
2
+ // the project in `dir` (default: where the command runs). Run it after the build:
3
+ // "build": "vite build && stack-tools vercel-headers"
4
+ import { readFileSync, writeFileSync } from "node:fs";
5
+ import { resolve } from "node:path";
6
+ import { SECURITY_HEADERS, withSecurityHeaders } from "./headers.js";
7
+ const dir = process.argv[2] && !process.argv[2].startsWith("-") ? process.argv[2] : ".";
8
+ const file = resolve(dir, ".vercel/output/config.json");
9
+ let config;
10
+ try {
11
+ config = JSON.parse(readFileSync(file, "utf8"));
12
+ }
13
+ catch {
14
+ console.error(`vercel-headers: no ${file}. Run it after the build (the Vercel adapter writes that file).`);
15
+ process.exit(1);
16
+ }
17
+ writeFileSync(file, JSON.stringify(withSecurityHeaders(config), null, "\t") + "\n");
18
+ console.log(`vercel-headers: ${Object.keys(SECURITY_HEADERS).length} security headers on every answer`);
@@ -0,0 +1,16 @@
1
+ export declare const SECURITY_HEADERS: Record<string, string>;
2
+ export type Route = {
3
+ src?: string;
4
+ headers?: Record<string, string>;
5
+ continue?: boolean;
6
+ [key: string]: unknown;
7
+ };
8
+ export type OutputConfig = {
9
+ routes?: Route[];
10
+ [key: string]: unknown;
11
+ };
12
+ /**
13
+ * The same config with the headers route first: before `handle: filesystem`, so static files
14
+ * get the headers too. A second call replaces the route instead of adding another.
15
+ */
16
+ export declare function withSecurityHeaders(config: OutputConfig): OutputConfig;
@@ -0,0 +1,29 @@
1
+ // The security headers a site sends on every answer, and how they get into a Vercel build
2
+ // output (.vercel/output/config.json).
3
+ //
4
+ // Why the build output: prerendered pages are files served by the CDN, so no server hook runs
5
+ // for them, and a prebuilt deploy (`vercel deploy --prebuilt`) reads only that config.json,
6
+ // never vercel.json.
7
+ //
8
+ // No Content-Security-Policy here: it depends on the site's own scripts, so the framework
9
+ // writes it (SvelteKit's `csp`). On a prerendered page that CSP is a <meta> tag, where browsers
10
+ // ignore frame-ancestors: x-frame-options below is what forbids framing there.
11
+ // No HSTS either: Vercel sends it on every domain.
12
+ export const SECURITY_HEADERS = {
13
+ "x-content-type-options": "nosniff",
14
+ "x-frame-options": "DENY",
15
+ "referrer-policy": "strict-origin-when-cross-origin",
16
+ "permissions-policy": "camera=(), microphone=(), geolocation=(), payment=(), usb=(), browsing-topics=()",
17
+ "cross-origin-opener-policy": "same-origin"
18
+ };
19
+ /** The route that adds the headers: every path, then on to the routes after it. */
20
+ const headersRoute = () => ({ src: "/.*", headers: { ...SECURITY_HEADERS }, continue: true });
21
+ const isOurs = (r) => r.src === "/.*" && r.continue === true && r.headers?.["x-frame-options"] !== undefined;
22
+ /**
23
+ * The same config with the headers route first: before `handle: filesystem`, so static files
24
+ * get the headers too. A second call replaces the route instead of adding another.
25
+ */
26
+ export function withSecurityHeaders(config) {
27
+ const rest = (config.routes ?? []).filter((r) => !isOurs(r));
28
+ return { ...config, routes: [headersRoute(), ...rest] };
29
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@a.incantalupo/web-tools",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "Checks and captures for any website: readability and contrast, screenshots, style snapshots and diffs, glyph and image checks, a site audit with niche profiles, your own signed-in browser.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -30,6 +30,7 @@
30
30
  "scripts": {
31
31
  "build": "rm -rf dist && tsc -p tsconfig.json && chmod +x dist/cli.js",
32
32
  "check": "tsc -p tsconfig.json --noEmit",
33
+ "test": "node --test 'test/*.test.ts'",
33
34
  "prepublishOnly": "pnpm build"
34
35
  },
35
36
  "dependencies": {