@nifrajs/cli 5.1.0 → 5.2.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/CHANGELOG.md +3211 -0
- package/README.md +3 -0
- package/dist/agent-files.d.ts +19 -11
- package/dist/agent-files.d.ts.map +1 -1
- package/dist/agent-files.js +26 -18
- package/dist/agent-files.js.map +1 -1
- package/dist/assure-hydration.d.ts.map +1 -1
- package/dist/assure-hydration.js +1 -0
- package/dist/assure-hydration.js.map +1 -1
- package/dist/assure.d.ts.map +1 -1
- package/dist/assure.js +14 -1
- package/dist/assure.js.map +1 -1
- package/dist/capabilities-tool.d.ts.map +1 -1
- package/dist/capabilities-tool.js +131 -45
- package/dist/capabilities-tool.js.map +1 -1
- package/dist/check-diagnostics.d.ts +5 -0
- package/dist/check-diagnostics.d.ts.map +1 -1
- package/dist/check-diagnostics.js +3 -0
- package/dist/check-diagnostics.js.map +1 -1
- package/dist/check-scan.d.ts.map +1 -1
- package/dist/check-scan.js +6 -3
- package/dist/check-scan.js.map +1 -1
- package/dist/check.d.ts.map +1 -1
- package/dist/check.js +52 -12
- package/dist/check.js.map +1 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +23 -12
- package/dist/cli.js.map +1 -1
- package/dist/command-catalog.d.ts.map +1 -1
- package/dist/command-catalog.js +9 -5
- package/dist/command-catalog.js.map +1 -1
- package/dist/dev-bun-config.d.ts +6 -3
- package/dist/dev-bun-config.d.ts.map +1 -1
- package/dist/dev-bun-config.js +13 -10
- package/dist/dev-bun-config.js.map +1 -1
- package/dist/dev-feed-tool.d.ts +3 -2
- package/dist/dev-feed-tool.d.ts.map +1 -1
- package/dist/dev-feed-tool.js +26 -5
- package/dist/dev-feed-tool.js.map +1 -1
- package/dist/doctor.d.ts +15 -1
- package/dist/doctor.d.ts.map +1 -1
- package/dist/doctor.js +79 -61
- package/dist/doctor.js.map +1 -1
- package/dist/i18n-check.d.ts +11 -1
- package/dist/i18n-check.d.ts.map +1 -1
- package/dist/i18n-check.js +170 -17
- package/dist/i18n-check.js.map +1 -1
- package/dist/init-agents.d.ts +46 -6
- package/dist/init-agents.d.ts.map +1 -1
- package/dist/init-agents.js +193 -31
- package/dist/init-agents.js.map +1 -1
- package/dist/internal/project-specifiers.d.ts.map +1 -1
- package/dist/internal/project-specifiers.js +9 -1
- package/dist/internal/project-specifiers.js.map +1 -1
- package/dist/internal/source-facts.d.ts +6 -0
- package/dist/internal/source-facts.d.ts.map +1 -1
- package/dist/internal/source-facts.js +106 -0
- package/dist/internal/source-facts.js.map +1 -1
- package/dist/internal/workspace-packages.d.ts +22 -0
- package/dist/internal/workspace-packages.d.ts.map +1 -0
- package/dist/internal/workspace-packages.js +215 -0
- package/dist/internal/workspace-packages.js.map +1 -0
- package/dist/load.d.ts +7 -2
- package/dist/load.d.ts.map +1 -1
- package/dist/load.js +11 -2
- package/dist/load.js.map +1 -1
- package/dist/lock-links.d.ts +42 -0
- package/dist/lock-links.d.ts.map +1 -0
- package/dist/lock-links.js +144 -0
- package/dist/lock-links.js.map +1 -0
- package/dist/mcp-exec.d.ts.map +1 -1
- package/dist/mcp-exec.js +5 -2
- package/dist/mcp-exec.js.map +1 -1
- package/dist/mcp-http.js +1 -1
- package/dist/mcp-root.d.ts +6 -4
- package/dist/mcp-root.d.ts.map +1 -1
- package/dist/mcp-root.js +12 -8
- package/dist/mcp-root.js.map +1 -1
- package/dist/migrate-layout.d.ts +2 -1
- package/dist/migrate-layout.d.ts.map +1 -1
- package/dist/migrate-layout.js +434 -29
- package/dist/migrate-layout.js.map +1 -1
- package/dist/project-site.d.ts +7 -0
- package/dist/project-site.d.ts.map +1 -0
- package/dist/project-site.js +19 -0
- package/dist/project-site.js.map +1 -0
- package/dist/recipes/4.0.0.d.ts.map +1 -1
- package/dist/recipes/4.0.0.js +2 -0
- package/dist/recipes/4.0.0.js.map +1 -1
- package/dist/recipes/5.1.0.d.ts +9 -0
- package/dist/recipes/5.1.0.d.ts.map +1 -0
- package/dist/recipes/5.1.0.js +22 -0
- package/dist/recipes/5.1.0.js.map +1 -0
- package/dist/recipes/5.2.0.d.ts +10 -0
- package/dist/recipes/5.2.0.d.ts.map +1 -0
- package/dist/recipes/5.2.0.js +32 -0
- package/dist/recipes/5.2.0.js.map +1 -0
- package/dist/recipes/index.d.ts +3 -0
- package/dist/recipes/index.d.ts.map +1 -1
- package/dist/recipes/index.js +7 -0
- package/dist/recipes/index.js.map +1 -1
- package/dist/review.d.ts.map +1 -1
- package/dist/review.js +2 -0
- package/dist/review.js.map +1 -1
- package/dist/rule-explain.d.ts +26 -0
- package/dist/rule-explain.d.ts.map +1 -0
- package/dist/rule-explain.js +49 -0
- package/dist/rule-explain.js.map +1 -0
- package/dist/rules/codes.d.ts +2 -0
- package/dist/rules/codes.d.ts.map +1 -1
- package/dist/rules/codes.js +2 -0
- package/dist/rules/codes.js.map +1 -1
- package/dist/rules/contract-visibility.d.ts.map +1 -1
- package/dist/rules/contract-visibility.js +93 -21
- package/dist/rules/contract-visibility.js.map +1 -1
- package/dist/rules/explanations.d.ts +14 -0
- package/dist/rules/explanations.d.ts.map +1 -0
- package/dist/rules/explanations.js +316 -0
- package/dist/rules/explanations.js.map +1 -0
- package/dist/rules/legacy.d.ts.map +1 -1
- package/dist/rules/legacy.js +24 -1
- package/dist/rules/legacy.js.map +1 -1
- package/dist/rules/merge-auth.d.ts +12 -0
- package/dist/rules/merge-auth.d.ts.map +1 -0
- package/dist/rules/merge-auth.js +52 -0
- package/dist/rules/merge-auth.js.map +1 -0
- package/dist/rules/pages.d.ts +27 -0
- package/dist/rules/pages.d.ts.map +1 -1
- package/dist/rules/pages.js +177 -2
- package/dist/rules/pages.js.map +1 -1
- package/dist/rules/routes.d.ts +3 -2
- package/dist/rules/routes.d.ts.map +1 -1
- package/dist/rules/routes.js +4 -3
- package/dist/rules/routes.js.map +1 -1
- package/dist/rules/zones.d.ts.map +1 -1
- package/dist/rules/zones.js +3 -2
- package/dist/rules/zones.js.map +1 -1
- package/dist/typecheck-coverage.d.ts +11 -0
- package/dist/typecheck-coverage.d.ts.map +1 -1
- package/dist/typecheck-coverage.js +50 -7
- package/dist/typecheck-coverage.js.map +1 -1
- package/dist/upgrade.d.ts +37 -1
- package/dist/upgrade.d.ts.map +1 -1
- package/dist/upgrade.js +171 -20
- package/dist/upgrade.js.map +1 -1
- package/dist/verification-plan.d.ts.map +1 -1
- package/dist/verification-plan.js +2 -0
- package/dist/verification-plan.js.map +1 -1
- package/dist/version-drift.d.ts.map +1 -1
- package/dist/version-drift.js +12 -1
- package/dist/version-drift.js.map +1 -1
- package/dist/web-app-options.d.ts +1 -1
- package/dist/web-app-options.d.ts.map +1 -1
- package/dist/web-app-options.js.map +1 -1
- package/docs/examples.json +19 -1
- package/docs/llms-full.txt +625 -130
- package/docs/types.json +396 -50
- package/package.json +13 -12
- package/src/agent-files.ts +27 -17
- package/src/assure-hydration.ts +1 -0
- package/src/assure.ts +11 -1
- package/src/capabilities-tool.ts +159 -50
- package/src/check-diagnostics.ts +8 -0
- package/src/check-scan.ts +9 -3
- package/src/check.ts +67 -12
- package/src/cli.ts +24 -11
- package/src/command-catalog.ts +16 -8
- package/src/dev-bun-config.ts +13 -9
- package/src/dev-feed-tool.ts +40 -6
- package/src/doctor.ts +102 -66
- package/src/i18n-check.ts +254 -18
- package/src/init-agents.ts +221 -41
- package/src/internal/project-specifiers.ts +9 -1
- package/src/internal/source-facts.ts +124 -0
- package/src/internal/workspace-packages.ts +218 -0
- package/src/load.ts +19 -3
- package/src/lock-links.ts +173 -0
- package/src/mcp-exec.ts +5 -2
- package/src/mcp-http.ts +1 -1
- package/src/mcp-root.ts +14 -8
- package/src/migrate-layout.ts +495 -31
- package/src/project-site.ts +18 -0
- package/src/recipes/4.0.0.ts +2 -0
- package/src/recipes/5.1.0.ts +23 -0
- package/src/recipes/5.2.0.ts +33 -0
- package/src/recipes/index.ts +7 -0
- package/src/review.ts +2 -0
- package/src/rule-explain.ts +75 -0
- package/src/rules/codes.ts +2 -0
- package/src/rules/contract-visibility.ts +118 -20
- package/src/rules/explanations.ts +329 -0
- package/src/rules/legacy.ts +24 -1
- package/src/rules/merge-auth.ts +55 -0
- package/src/rules/pages.ts +221 -2
- package/src/rules/routes.ts +4 -3
- package/src/rules/zones.ts +3 -2
- package/src/typecheck-coverage.ts +51 -6
- package/src/upgrade.ts +227 -22
- package/src/verification-plan.ts +6 -0
- package/src/version-drift.ts +15 -0
- package/src/web-app-options.ts +9 -1
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { isAbsolute, relative, sep } from "node:path"
|
|
2
|
+
import { fileURLToPath } from "node:url"
|
|
3
|
+
|
|
4
|
+
/** A recorded `file:line:col` as a project-relative POSIX path and line, when it is inside the project
|
|
5
|
+
* and not under a `node_modules`. */
|
|
6
|
+
export function projectSite(
|
|
7
|
+
root: string,
|
|
8
|
+
site: string | undefined,
|
|
9
|
+
): { readonly file: string; readonly line: number } | undefined {
|
|
10
|
+
const [, raw, line] = site?.match(/^(.*):(\d+):\d+$/) ?? []
|
|
11
|
+
if (raw === undefined || line === undefined) return undefined
|
|
12
|
+
const path = raw.startsWith("file://") ? fileURLToPath(raw) : raw
|
|
13
|
+
if (!isAbsolute(path)) return undefined
|
|
14
|
+
const rel = relative(root, path)
|
|
15
|
+
if (rel === "" || rel.startsWith("..") || isAbsolute(rel)) return undefined
|
|
16
|
+
if (rel.split(sep).includes("node_modules")) return undefined
|
|
17
|
+
return { file: rel.split(sep).join("/"), line: Number(line) }
|
|
18
|
+
}
|
package/src/recipes/4.0.0.ts
CHANGED
|
@@ -20,6 +20,8 @@ export const recipe: UpgradeRecipe = {
|
|
|
20
20
|
"A loader or action that returns data needs `loaderOutput` / `actionOutput` in its backend half, or the request fails; the data is projected through the schema, so undeclared keys never reach the page. A deferred value is declared with `t.deferred(schema)`. `nifra check` reports a loader without one (NF-C030).",
|
|
21
21
|
"Builds fail on what looks like a credential in a client bundle, a `public/` file or a prerendered page; exempt a reviewed false positive with `secretExemptions` in `nifra.config.ts`.",
|
|
22
22
|
"The Cloudflare Pages target is `cloudflare` (`cf-pages` is refused), and `export const target` in `nifra.config.ts` sets what `nifra build` emits.",
|
|
23
|
+
'Page data reaches the browser in one inert `<script type="application/json" id="__nifra-handover">` instead of a script that assigned `window.__NIFRA_DATA__` and its siblings; the client entry assigns the same globals before anything reads them. A test or tool that read `window.__NIFRA_DATA__=` out of the HTML parses that element\'s JSON (`HANDOVER_ID` from `@nifrajs/web`), and an inline script that read the globals before the client entry must run after it.',
|
|
24
|
+
"`x-nifra-isr-revalidate` and `x-nifra-isr-tags`, which carry a route's `revalidate` and `revalidateTags`, are set only once a cache wrapper such as `withISR` attaches, and `withISR` removes them from every response it returns. A test or proxy that read a page's TTL from those headers on an unwrapped app reads nothing; assert on the wrapper's cache behavior, or on the route's `revalidate` export.",
|
|
23
25
|
'`withISR` no longer caches a page per query string by default: a request with any query parameter renders fresh and is not stored. Pass `query: ["page", ...]` to cache those parameters, or `query: "all"` to both `withISR` and `revalidateEndpoint` to keep the old keys.',
|
|
24
26
|
"A page file under the backend's API prefix or any other mount (`routes/api/*.tsx` with a backend) now fails at startup, at build and in `nifra check` (NF-C027), instead of answering 404. Move the page, change `apiPrefix` in `backend/framework.ts`, or serve that path from the backend.",
|
|
25
27
|
'Two page routes of one shape that share a path, such as `users/[id].tsx` beside `users/[slug].tsx`, fail dev, build and `nifra check` with "overlapping routes", because one could never be served. A static page beside a dynamic one (`users/me.tsx` beside `users/[id].tsx`) is fine: the more specific route serves the path they share. In a backend file, `nifra check` reports two same-method registrations that accept a shared path (NF-C024); make them disjoint (`/users/:id{[0-9]+}`) or mark the intended pair with a `// nifra-expect route-overlap` comment.',
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { UpgradeRecipe } from "./index.ts"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Upgrade recipe for Nifra 5.1.
|
|
5
|
+
*
|
|
6
|
+
* No import or dependency moves. Three changes can alter what a 5.0 app serves, so each note says what
|
|
7
|
+
* changes and the one edit that keeps or restores the old answer.
|
|
8
|
+
*/
|
|
9
|
+
export const recipe: UpgradeRecipe = {
|
|
10
|
+
version: "5.1.0",
|
|
11
|
+
pins: [
|
|
12
|
+
{ match: "@nifrajs/", to: "5.1.0" },
|
|
13
|
+
{ match: "create-nifra", to: "5.1.0" },
|
|
14
|
+
{ match: "nifra", to: "5.1.0" },
|
|
15
|
+
],
|
|
16
|
+
importMoves: [],
|
|
17
|
+
notes: [
|
|
18
|
+
'`merge()` now recompiles a merged route under the `responseContract()` and `effectLedger()` the server it is merged into installed before the merge, when the route declares a response (or capabilities) and its own server installed neither, as a `group()` route already was. Under `responseContract("enforce")`, a merged route whose payload carries a field its closed `t.object` does not declare now answers 500; declare the field, or install `responseContract("enforce", { extra: "strip" })` to drop it. After installing, `nifra routes --hooks --since .nifra/hooks-before-upgrade.json` lists each route that gained the contract.',
|
|
19
|
+
"A route with a `body` schema refuses a urlencoded, multipart or `text/plain` body whose `Origin` names another site with `403 forbidden_origin`, before reading it. A frontend on another origin that posts forms or `FormData` to the API, or an identity provider's `form_post` callback, needs that origin in the `formOrigins` server option (`@nifrajs/edge` takes it too). JSON bodies are unchanged.",
|
|
20
|
+
"`clientIp: { trustedHops: n }` reads the same `X-Forwarded-For` entry whether or not the adapter saw a socket peer. With no peer (Workers, edge handlers, a unix socket) it used to take the entry one place further left, which the client wrote; with no peer, `trustedHops: 0` is now `undefined`.",
|
|
21
|
+
'New and optional: a route assurance `baseline` rule `{ name: "response-contract", match: { hasResponse: true }, require: [NIFRA_ASSURANCE.RESPONSE_CONTRACT] }` makes `nifra assure` fail a route that declares a response no contract enforces.',
|
|
22
|
+
],
|
|
23
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { UpgradeRecipe } from "./index.ts"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Upgrade recipe for Nifra 5.2.
|
|
5
|
+
*
|
|
6
|
+
* No import or dependency moves. Six changes alter what a 5.1 app serves or builds, and the development audit,
|
|
7
|
+
* `nifra check`, `nifra i18n check` and the upgrade itself report more; each note says what changes and
|
|
8
|
+
* what keeps the old answer.
|
|
9
|
+
*/
|
|
10
|
+
export const recipe: UpgradeRecipe = {
|
|
11
|
+
version: "5.2.0",
|
|
12
|
+
pins: [
|
|
13
|
+
{ match: "@nifrajs/", to: "5.2.0" },
|
|
14
|
+
{ match: "create-nifra", to: "5.2.0" },
|
|
15
|
+
{ match: "nifra", to: "5.2.0" },
|
|
16
|
+
],
|
|
17
|
+
importMoves: [],
|
|
18
|
+
notes: [
|
|
19
|
+
"A closed request-header schema (`headers: t.object({...})`) validates only the header names it declares, read case-insensitively. It used to see every header a client sends (`host`, `user-agent`), so such a route answered real requests with 422; those requests are now served. Use `t.headers({...})` for an open header slot.",
|
|
20
|
+
'`responseContract("enforce", { extra: "strip" })` projects a union whose object branches all require one key holding literals no two branches share (`ok: true` / `ok: false`). When those branches are closed `t.object`s, a payload that carried undeclared fields used to answer 500; it is now sent without them.',
|
|
21
|
+
"An `authenticate()` stage that throws, or returns a malformed decision, still answers 503 `auth_unavailable`, but its cause now goes to `onError` (a response it returns replaces the 503) and, when no hook answers, to the logger as `unhandled request error`. A stage that throws a `Response` or `status(...)`, such as `requirePrincipal`'s 401 or redirect, now has it sent as thrown instead of a 503, and `authed()` from `@nifrajs/better-auth` no longer catches provider failures itself. An `onError` hook that answers every error now answers these too; return nothing for them to keep the 503.",
|
|
22
|
+
'The development `unusedScopedHooks` audit also reports a hook only `merge()`d routes follow (a merged route keeps its own server\'s chain), an `authenticate()` after the last route, and a `responseContract()` or `effectLedger()` installed after routes it would have covered. With `unusedScopedHooks: "error"` each now throws at startup outside production: move the call before the routes, or wire the hook on the merged server.',
|
|
23
|
+
"`nifra check` adds NF-C043 (warning): a parent's `authenticate()` or authenticated scope precedes a `merge()` of a server that authenticates none of its routes. Wire the auth on that server, or mark the merge `// nifra-expect NF-C043: <reason>`.",
|
|
24
|
+
"`nifra check`'s capability walk also starts from the module that registered each route, recorded while `nifra.assurance.ts` loads the app, so a route it could not see before (a computed path, a helper-built server) is now checked and may report an undeclared capability.",
|
|
25
|
+
"`nifra check`'s NF-C035 also reads app modules that call `createWebApp` themselves: a call that mounts `api` with no `apiPrefix`, or with one other than the framework config's, is reported. Import the `apiPrefix` from `backend/framework.ts` so both read one value.",
|
|
26
|
+
'`nifra i18n check` and `checkCatalogs()` report two new errors: `latin-share`, a message in a locale that does not write in Latin whose words are mostly Latin script, and `volume`, a long default message translated to under a third of its length. List kept brand names in `latinWords`, raise `maxLatinShare` or lower `minVolumeRatio`, or list the keys under `ignore` (`"*"` for every key) to keep the old answer.',
|
|
27
|
+
"`alternates()` from `@nifrajs/i18n/routing` given `locales` of a single locale returns no links, where it returned one self-reference.",
|
|
28
|
+
'`nifra check` typechecks every project `tsconfig.json` references, each as its own program, where it ran `tsc -p tsconfig.json` alone: a solution-style tsconfig (`"files": []` plus `references`) used to pass having read nothing, and its projects\' errors now fail the check. Name other configs to typecheck in `nifra.check.json` as `"tsconfigs": [...]`.',
|
|
29
|
+
"The Bun build (`nifra build` without `--vite`, `buildClient`, `buildServer`, `nifra dev --bun`) expands `import.meta.glob` as Vite does. A glob it used to bundle as a call that loads nothing now ships its files, and one it cannot read (a computed pattern, an alias, the `query` option) fails the build naming the file and line: write it as a literal, or build with `--vite`.",
|
|
30
|
+
"With both `nifra.config.ts` and `backend/framework.ts`, `nifra dev` and `nifra build` take `publicEnvPrefix` from `backend/framework.ts` when `nifra.config.ts` leaves it unset, so browser code reads the variables under that prefix there too, and `nifra check` (NF-C029) reads `nifra.config.ts` first. Set the prefix in one file to keep a single value.",
|
|
31
|
+
"In a project that declares `@nifrajs/cli`, `nifra upgrade --write` drops the version pin from the MCP launch (`bunx @nifrajs/cli mcp`) so it runs the installed CLI from then on; restart the agent afterwards. `nifra upgrade --write` now refuses while a `link:` or `file:` dependency installs another nifra version, naming the commands for that repository; `--allow-linked-skew` proceeds anyway.",
|
|
32
|
+
],
|
|
33
|
+
}
|
package/src/recipes/index.ts
CHANGED
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
* Per-release upgrade recipes, as data. Each recipe declares the mechanical edits a target version
|
|
3
3
|
* needs - dependency pins and exact import-specifier moves - so `nifra upgrade <version>` can apply
|
|
4
4
|
* them deterministically. Recipes are static imports (they bundle) and keyed by target version.
|
|
5
|
+
*
|
|
6
|
+
* Every minor and major release has one, even with no edits: its notes are where a change an app can
|
|
7
|
+
* observe is announced to `nifra upgrade` and `--list`. `check:publish` fails a release without it.
|
|
5
8
|
*/
|
|
6
9
|
|
|
7
10
|
import { recipe as v1_8_0 } from "./1.8.0.ts"
|
|
@@ -9,6 +12,8 @@ import { recipe as v2_0_0 } from "./2.0.0.ts"
|
|
|
9
12
|
import { recipe as v3_0_0 } from "./3.0.0.ts"
|
|
10
13
|
import { recipe as v4_0_0 } from "./4.0.0.ts"
|
|
11
14
|
import { recipe as v5_0_0 } from "./5.0.0.ts"
|
|
15
|
+
import { recipe as v5_1_0 } from "./5.1.0.ts"
|
|
16
|
+
import { recipe as v5_2_0 } from "./5.2.0.ts"
|
|
12
17
|
|
|
13
18
|
/** A pin rule: every dependency whose name starts with `match` is set to version `to`. */
|
|
14
19
|
export interface PinRule {
|
|
@@ -54,6 +59,8 @@ const RECIPES: Record<string, UpgradeRecipe> = {
|
|
|
54
59
|
[v3_0_0.version]: v3_0_0,
|
|
55
60
|
[v4_0_0.version]: v4_0_0,
|
|
56
61
|
[v5_0_0.version]: v5_0_0,
|
|
62
|
+
[v5_1_0.version]: v5_1_0,
|
|
63
|
+
[v5_2_0.version]: v5_2_0,
|
|
57
64
|
}
|
|
58
65
|
|
|
59
66
|
export function getRecipe(version: string): UpgradeRecipe | undefined {
|
package/src/review.ts
CHANGED
|
@@ -154,8 +154,10 @@ export const CHECK_CODES: Readonly<Record<string, ReviewCheckId>> = {
|
|
|
154
154
|
"NF-C040": "route-boundary",
|
|
155
155
|
"NF-C041": "route-boundary",
|
|
156
156
|
"NF-C042": "route-boundary",
|
|
157
|
+
"NF-C043": "security",
|
|
157
158
|
"NF-D001": "dependency",
|
|
158
159
|
"NF-D002": "dependency",
|
|
160
|
+
"NF-D003": "dependency",
|
|
159
161
|
"NF-A001": "route-assurance",
|
|
160
162
|
"NF-H001": "hydration",
|
|
161
163
|
"NF-H002": "hydration",
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `nifra explain NF-xxxx`: a rule code answered from the catalog, with no project read. The catalog
|
|
3
|
+
* loads only when a code is asked for, so the explain command's other paths never pay for it.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/** The shape of a built-in rule code. A positional that matches it is a code, not an error message. */
|
|
7
|
+
export const RULE_CODE_SHAPE = /^NF-[A-Z]\d{3}$/
|
|
8
|
+
|
|
9
|
+
const DOCS_ORIGIN = "https://nifra.dev"
|
|
10
|
+
|
|
11
|
+
export interface RuleExplainOutput {
|
|
12
|
+
readonly kind: "rule"
|
|
13
|
+
readonly code: string
|
|
14
|
+
readonly title: string
|
|
15
|
+
/** The `nifra review` check it reports under. */
|
|
16
|
+
readonly category: string
|
|
17
|
+
/** What the rule reports and why it matters. */
|
|
18
|
+
readonly message: string
|
|
19
|
+
readonly fix: string
|
|
20
|
+
readonly command?: string
|
|
21
|
+
readonly docs: string
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface UnknownRuleOutput {
|
|
25
|
+
readonly code: "NIFRA_UNKNOWN_RULE"
|
|
26
|
+
readonly message: string
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function explainRule(code: string): Promise<RuleExplainOutput | UnknownRuleOutput> {
|
|
30
|
+
const [{ isBuiltInCode, RETIRED_CODES, RULE_CODES }, { RULE_EXPLANATIONS }, { CHECK_CODES }] =
|
|
31
|
+
await Promise.all([
|
|
32
|
+
import("./rules/codes.ts"),
|
|
33
|
+
import("./rules/explanations.ts"),
|
|
34
|
+
import("./review.ts"),
|
|
35
|
+
])
|
|
36
|
+
if (RETIRED_CODES.has(code))
|
|
37
|
+
return {
|
|
38
|
+
code: "NIFRA_UNKNOWN_RULE",
|
|
39
|
+
message: `${code} is retired: no rule reports it any more, and the code is never reused.`,
|
|
40
|
+
}
|
|
41
|
+
if (!isBuiltInCode(code) || !Object.hasOwn(RULE_CODES, code))
|
|
42
|
+
return {
|
|
43
|
+
code: "NIFRA_UNKNOWN_RULE",
|
|
44
|
+
message: `${code} is not a nifra rule code. \`nifra check --json\` lists each finding's code; \`nifra explain "<message>"\` explains an error instead.`,
|
|
45
|
+
}
|
|
46
|
+
const explanation = RULE_EXPLANATIONS[code]
|
|
47
|
+
const out: RuleExplainOutput = {
|
|
48
|
+
kind: "rule",
|
|
49
|
+
code,
|
|
50
|
+
title: RULE_CODES[code],
|
|
51
|
+
category: CHECK_CODES[code] ?? "configuration",
|
|
52
|
+
message: explanation.why,
|
|
53
|
+
fix: explanation.fix,
|
|
54
|
+
docs: `${DOCS_ORIGIN}${explanation.docs}`,
|
|
55
|
+
}
|
|
56
|
+
return explanation.command === undefined ? out : { ...out, command: explanation.command }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export const isRuleExplanation = (value: unknown): value is RuleExplainOutput =>
|
|
60
|
+
typeof value === "object" &&
|
|
61
|
+
value !== null &&
|
|
62
|
+
Reflect.get(value, "kind") === "rule" &&
|
|
63
|
+
["code", "title", "category", "message", "fix", "docs"].every(
|
|
64
|
+
(key) => typeof Reflect.get(value, key) === "string",
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
export function renderRuleExplanation(out: RuleExplainOutput): string[] {
|
|
68
|
+
return [
|
|
69
|
+
`${out.code} ${out.title} (${out.category})`,
|
|
70
|
+
` ${out.message}`,
|
|
71
|
+
` fix: ${out.fix}`,
|
|
72
|
+
...(out.command === undefined ? [] : [` run: ${out.command}`]),
|
|
73
|
+
` docs: ${out.docs}`,
|
|
74
|
+
]
|
|
75
|
+
}
|
package/src/rules/codes.ts
CHANGED
|
@@ -39,12 +39,14 @@ export const RULE_CODES = Object.freeze({
|
|
|
39
39
|
"NF-C040": "Layout meta reads data its layout never loads",
|
|
40
40
|
"NF-C041": "Server-only export in a route frontend file",
|
|
41
41
|
"NF-C042": "Layout loader refuses access without a gate",
|
|
42
|
+
"NF-C043": "Merged routes skip the parent's authentication",
|
|
42
43
|
"NF-C020": "Island enhancer cleanup check",
|
|
43
44
|
"NF-C021": "nano binding cleanup check",
|
|
44
45
|
"NF-C022": "nano bindList key check",
|
|
45
46
|
"NF-C023": "nano computed deps check",
|
|
46
47
|
"NF-D001": "Doctor check",
|
|
47
48
|
"NF-D002": "Mixed nifra versions",
|
|
49
|
+
"NF-D003": "Stale file: dependencies in bun.lock",
|
|
48
50
|
"NF-A001": "Assurance bundle check",
|
|
49
51
|
"NF-H001": "Hydration mismatch",
|
|
50
52
|
"NF-H002": "Duplicate framework runtime",
|
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
*
|
|
6
6
|
* The served routes come from the backend the contract-lock check already loaded; this rule runs no
|
|
7
7
|
* project code of its own. Without a lock the backend is not loaded and the comparison is skipped.
|
|
8
|
+
* A contract imported from a workspace package is read through that package's `exports`.
|
|
9
|
+
* Re-exports on the way to a contract, and `...operations` spreads inside one, are followed.
|
|
8
10
|
*/
|
|
9
11
|
import { existsSync, readFileSync, realpathSync } from "node:fs"
|
|
10
12
|
import { join, relative } from "node:path"
|
|
@@ -13,6 +15,7 @@ import { BACKEND_APP_FILE } from "../app-files.ts"
|
|
|
13
15
|
import { type Diagnostic, diagnostic } from "../diagnostics.ts"
|
|
14
16
|
import { createSpecifierResolver, type SpecifierResolver } from "../internal/project-specifiers.ts"
|
|
15
17
|
import { scriptKindOf } from "../internal/script-kind.ts"
|
|
18
|
+
import { createExportedFileResolver } from "../internal/workspace-packages.ts"
|
|
16
19
|
import type { CheckRule } from "./index.ts"
|
|
17
20
|
import { loadRuleTypeScript } from "./typescript.ts"
|
|
18
21
|
|
|
@@ -126,14 +129,31 @@ function calls(
|
|
|
126
129
|
)
|
|
127
130
|
}
|
|
128
131
|
|
|
129
|
-
|
|
132
|
+
interface OperationsRead {
|
|
133
|
+
readonly operations: readonly Operation[]
|
|
134
|
+
readonly unreadable: readonly string[]
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** The operations an object literal of a contract declares, and what could not be read. `spreadOf`
|
|
138
|
+
* reads a `...name` spread of another operations object. */
|
|
130
139
|
function operationsOf(
|
|
131
140
|
ts: typeof TSApi,
|
|
132
141
|
literal: TSApi.ObjectLiteralExpression,
|
|
133
|
-
|
|
134
|
-
|
|
142
|
+
spreadOf: (name: string) => OperationsRead | undefined,
|
|
143
|
+
): OperationsRead {
|
|
144
|
+
// A later key replaces an earlier one, as in the object the page builds at runtime.
|
|
145
|
+
const operations = new Map<string, Operation>()
|
|
135
146
|
const unreadable: string[] = []
|
|
136
147
|
for (const property of literal.properties) {
|
|
148
|
+
if (ts.isSpreadAssignment(property)) {
|
|
149
|
+
const spread = unwrap(ts, property.expression)
|
|
150
|
+
const read = ts.isIdentifier(spread) ? spreadOf(spread.text) : undefined
|
|
151
|
+
if (read !== undefined) {
|
|
152
|
+
for (const op of read.operations) operations.set(op.name, op)
|
|
153
|
+
unreadable.push(...read.unreadable)
|
|
154
|
+
continue
|
|
155
|
+
}
|
|
156
|
+
}
|
|
137
157
|
if (!ts.isPropertyAssignment(property)) {
|
|
138
158
|
unreadable.push("an operation spread or written as a method")
|
|
139
159
|
continue
|
|
@@ -156,9 +176,9 @@ function operationsOf(
|
|
|
156
176
|
const path = field("path")
|
|
157
177
|
if (method === undefined || path === undefined)
|
|
158
178
|
unreadable.push(`operation "${name}" has a method or path that is not a string literal`)
|
|
159
|
-
else operations.
|
|
179
|
+
else operations.set(name, { name, method: method.toUpperCase(), path })
|
|
160
180
|
}
|
|
161
|
-
return { operations, unreadable }
|
|
181
|
+
return { operations: [...operations.values()], unreadable }
|
|
162
182
|
}
|
|
163
183
|
|
|
164
184
|
interface Parsed {
|
|
@@ -166,6 +186,13 @@ interface Parsed {
|
|
|
166
186
|
readonly bound: Bindings
|
|
167
187
|
}
|
|
168
188
|
|
|
189
|
+
/** An operations object literal and the file it is written in, which its spreads resolve against. */
|
|
190
|
+
interface Located {
|
|
191
|
+
readonly literal: TSApi.ObjectLiteralExpression
|
|
192
|
+
readonly file: string
|
|
193
|
+
readonly parsed: Parsed
|
|
194
|
+
}
|
|
195
|
+
|
|
169
196
|
export const contractVisibilityRule: CheckRule = {
|
|
170
197
|
code: "NF-C039",
|
|
171
198
|
title: "Implemented contract operation the backend does not serve",
|
|
@@ -192,7 +219,19 @@ export const contractVisibilityRule: CheckRule = {
|
|
|
192
219
|
const loaded = await loadRuleTypeScript(ctx)
|
|
193
220
|
const ts = loaded.compiler
|
|
194
221
|
if (ts === undefined) return []
|
|
195
|
-
const
|
|
222
|
+
const projectResolve = createSpecifierResolver()
|
|
223
|
+
const packageEntry = await createExportedFileResolver(ctx.root)
|
|
224
|
+
/** Why a package specifier could not be read, by importing file and specifier. */
|
|
225
|
+
const packageReasons = new Map<string, string>()
|
|
226
|
+
const resolve: SpecifierResolver = (from, specifier) => {
|
|
227
|
+
const local = projectResolve(from, specifier)
|
|
228
|
+
if (local !== undefined) return local
|
|
229
|
+
const entry = packageEntry(from, specifier)
|
|
230
|
+
if (entry === undefined) return undefined
|
|
231
|
+
if ("path" in entry) return entry.path
|
|
232
|
+
packageReasons.set(`${from}\0${specifier}`, entry.reason)
|
|
233
|
+
return undefined
|
|
234
|
+
}
|
|
196
235
|
// The resolver answers with real paths, so the root is read as one to name files project-relative.
|
|
197
236
|
const realRoot = (() => {
|
|
198
237
|
try {
|
|
@@ -217,20 +256,21 @@ export const contractVisibilityRule: CheckRule = {
|
|
|
217
256
|
return parsed
|
|
218
257
|
}
|
|
219
258
|
/** The object literal `defineContract({...})` gives the const `name` in `file`, followed one hop
|
|
220
|
-
* into a project module it is imported from. */
|
|
259
|
+
* into a project module it is imported from, and through any re-exports on the way. */
|
|
221
260
|
const contractLiteral = (
|
|
222
261
|
file: string,
|
|
223
262
|
parsed: Parsed,
|
|
224
263
|
name: string,
|
|
225
264
|
hops = 0,
|
|
226
|
-
|
|
265
|
+
seen = new Set<string>(),
|
|
266
|
+
): Located | undefined => {
|
|
227
267
|
for (const statement of parsed.tree.statements) {
|
|
228
268
|
if (!ts.isVariableStatement(statement)) continue
|
|
229
269
|
for (const declaration of statement.declarationList.declarations) {
|
|
230
270
|
if (!ts.isIdentifier(declaration.name) || declaration.name.text !== name) continue
|
|
231
271
|
if (declaration.initializer === undefined) return undefined
|
|
232
272
|
const init = unwrap(ts, declaration.initializer)
|
|
233
|
-
if (ts.isObjectLiteralExpression(init)) return init
|
|
273
|
+
if (ts.isObjectLiteralExpression(init)) return { literal: init, file, parsed }
|
|
234
274
|
if (
|
|
235
275
|
!ts.isCallExpression(init) ||
|
|
236
276
|
!calls(ts, init.expression, parsed.bound, "defineContract")
|
|
@@ -239,19 +279,66 @@ export const contractVisibilityRule: CheckRule = {
|
|
|
239
279
|
const arg = init.arguments[0]
|
|
240
280
|
const literal = arg === undefined ? undefined : unwrap(ts, arg)
|
|
241
281
|
return literal !== undefined && ts.isObjectLiteralExpression(literal)
|
|
242
|
-
? literal
|
|
282
|
+
? { literal, file, parsed }
|
|
243
283
|
: undefined
|
|
244
284
|
}
|
|
245
285
|
}
|
|
246
286
|
const from = parsed.bound.imported.get(name)
|
|
247
|
-
if (from
|
|
248
|
-
|
|
287
|
+
if (from !== undefined)
|
|
288
|
+
return hops > 0 ? undefined : follow(file, from.specifier, from.name, hops + 1, seen)
|
|
289
|
+
// A re-export declares nothing, so passing through one does not spend the hop.
|
|
290
|
+
for (const statement of parsed.tree.statements) {
|
|
291
|
+
if (
|
|
292
|
+
!ts.isExportDeclaration(statement) ||
|
|
293
|
+
statement.isTypeOnly ||
|
|
294
|
+
statement.moduleSpecifier === undefined ||
|
|
295
|
+
!ts.isStringLiteral(statement.moduleSpecifier)
|
|
296
|
+
)
|
|
297
|
+
continue
|
|
298
|
+
const clause = statement.exportClause
|
|
299
|
+
if (clause === undefined) {
|
|
300
|
+
const found = follow(file, statement.moduleSpecifier.text, name, hops, seen)
|
|
301
|
+
if (found !== undefined) return found
|
|
302
|
+
continue
|
|
303
|
+
}
|
|
304
|
+
if (!ts.isNamedExports(clause)) continue
|
|
305
|
+
const element = clause.elements.find((e) => !e.isTypeOnly && e.name.text === name)
|
|
306
|
+
if (element !== undefined)
|
|
307
|
+
return follow(
|
|
308
|
+
file,
|
|
309
|
+
statement.moduleSpecifier.text,
|
|
310
|
+
(element.propertyName ?? element.name).text,
|
|
311
|
+
hops,
|
|
312
|
+
seen,
|
|
313
|
+
)
|
|
314
|
+
}
|
|
315
|
+
return undefined
|
|
316
|
+
}
|
|
317
|
+
const follow = (
|
|
318
|
+
file: string,
|
|
319
|
+
specifier: string,
|
|
320
|
+
name: string,
|
|
321
|
+
hops: number,
|
|
322
|
+
seen: Set<string>,
|
|
323
|
+
): Located | undefined => {
|
|
324
|
+
const target = resolve(join(ctx.root, file), specifier)
|
|
249
325
|
if (target === undefined) return undefined
|
|
250
326
|
const rel = relative(realRoot, target).replaceAll("\\", "/")
|
|
327
|
+
const key = `${rel}\0${name}`
|
|
328
|
+
if (seen.has(key)) return undefined
|
|
329
|
+
seen.add(key)
|
|
251
330
|
const next = parse(rel, readText(target))
|
|
252
|
-
return next === undefined ? undefined : contractLiteral(rel, next,
|
|
331
|
+
return next === undefined ? undefined : contractLiteral(rel, next, name, hops, seen)
|
|
253
332
|
}
|
|
254
333
|
|
|
334
|
+
// Each spread is looked up afresh from the file it is written in; the depth only stops a cycle.
|
|
335
|
+
const readOperations = (found: Located, depth = 0): OperationsRead =>
|
|
336
|
+
operationsOf(ts, found.literal, (name) => {
|
|
337
|
+
if (depth >= 8) return undefined
|
|
338
|
+
const inner = contractLiteral(found.file, found.parsed, name)
|
|
339
|
+
return inner === undefined ? undefined : readOperations(inner, depth + 1)
|
|
340
|
+
})
|
|
341
|
+
|
|
255
342
|
const implementations: Implementation[] = []
|
|
256
343
|
for (const file of candidates) {
|
|
257
344
|
const parsed = parse(file, ctx.sources.read(file))
|
|
@@ -264,12 +351,12 @@ export const contractVisibilityRule: CheckRule = {
|
|
|
264
351
|
if (ts.isCallExpression(node) && calls(ts, node.expression, parsed.bound, "implement")) {
|
|
265
352
|
const line = parsed.tree.getLineAndCharacterOfPosition(node.getStart()).line + 1
|
|
266
353
|
const arg = node.arguments[0] === undefined ? undefined : unwrap(ts, node.arguments[0])
|
|
267
|
-
let
|
|
354
|
+
let located: Located | undefined
|
|
268
355
|
let contract = arg?.getText() ?? "(none)"
|
|
269
356
|
if (arg !== undefined && ts.isIdentifier(arg))
|
|
270
|
-
|
|
357
|
+
located = contractLiteral(file, parsed, arg.text)
|
|
271
358
|
else if (arg !== undefined && ts.isObjectLiteralExpression(arg)) {
|
|
272
|
-
|
|
359
|
+
located = { literal: arg, file, parsed }
|
|
273
360
|
contract = "an inline contract"
|
|
274
361
|
} else if (
|
|
275
362
|
arg !== undefined &&
|
|
@@ -277,20 +364,31 @@ export const contractVisibilityRule: CheckRule = {
|
|
|
277
364
|
calls(ts, arg.expression, parsed.bound, "defineContract")
|
|
278
365
|
) {
|
|
279
366
|
const inner = arg.arguments[0] === undefined ? undefined : unwrap(ts, arg.arguments[0])
|
|
280
|
-
if (inner !== undefined && ts.isObjectLiteralExpression(inner))
|
|
367
|
+
if (inner !== undefined && ts.isObjectLiteralExpression(inner))
|
|
368
|
+
located = { literal: inner, file, parsed }
|
|
281
369
|
contract = "an inline contract"
|
|
282
370
|
}
|
|
283
|
-
if (
|
|
371
|
+
if (located === undefined) {
|
|
372
|
+
const from =
|
|
373
|
+
arg !== undefined && ts.isIdentifier(arg)
|
|
374
|
+
? parsed.bound.imported.get(arg.text)?.specifier
|
|
375
|
+
: undefined
|
|
376
|
+
const reason =
|
|
377
|
+
from === undefined
|
|
378
|
+
? undefined
|
|
379
|
+
: packageReasons.get(`${join(ctx.root, file)}\0${from}`)
|
|
284
380
|
implementations.push({
|
|
285
381
|
file,
|
|
286
382
|
line,
|
|
287
383
|
contract,
|
|
288
384
|
operations: [],
|
|
289
385
|
unreadable: [
|
|
290
|
-
|
|
386
|
+
reason === undefined
|
|
387
|
+
? `the contract ${contract} is not a defineContract({...}) the scan can read`
|
|
388
|
+
: `the contract ${contract} is imported from "${from}", which the scan cannot read: ${reason}`,
|
|
291
389
|
],
|
|
292
390
|
})
|
|
293
|
-
} else implementations.push({ file, line, contract, ...
|
|
391
|
+
} else implementations.push({ file, line, contract, ...readOperations(located) })
|
|
294
392
|
}
|
|
295
393
|
ts.forEachChild(node, visit)
|
|
296
394
|
}
|