@nifrajs/cli 5.8.0 → 5.10.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 +191 -0
- package/dist/agent-files.d.ts +0 -22
- package/dist/agent-files.d.ts.map +1 -1
- package/dist/agent-files.js +3 -1
- package/dist/agent-files.js.map +1 -1
- package/dist/assure-hydration.d.ts.map +1 -1
- package/dist/assure-hydration.js +2 -10
- package/dist/assure-hydration.js.map +1 -1
- package/dist/assure.d.ts.map +1 -1
- package/dist/assure.js +3 -3
- package/dist/assure.js.map +1 -1
- package/dist/check-diagnostics.d.ts +1 -0
- package/dist/check-diagnostics.d.ts.map +1 -1
- package/dist/check-diagnostics.js +4 -0
- package/dist/check-diagnostics.js.map +1 -1
- package/dist/check-scan.d.ts +3 -0
- package/dist/check-scan.d.ts.map +1 -1
- package/dist/check-scan.js +62 -87
- package/dist/check-scan.js.map +1 -1
- package/dist/check.d.ts +3 -0
- package/dist/check.d.ts.map +1 -1
- package/dist/check.js +27 -25
- package/dist/check.js.map +1 -1
- package/dist/cli.d.ts +8 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +53 -20
- package/dist/cli.js.map +1 -1
- package/dist/command-catalog.d.ts +1 -5
- package/dist/command-catalog.d.ts.map +1 -1
- package/dist/command-catalog.js +20 -49
- package/dist/command-catalog.js.map +1 -1
- package/dist/config-target.js +1 -1
- package/dist/config-target.js.map +1 -1
- package/dist/db-child.js +3 -10
- package/dist/db-child.js.map +1 -1
- package/dist/db-config.d.ts +2 -0
- package/dist/db-config.d.ts.map +1 -1
- package/dist/db-config.js +2 -0
- package/dist/db-config.js.map +1 -1
- package/dist/doctor.d.ts +14 -3
- package/dist/doctor.d.ts.map +1 -1
- package/dist/doctor.js +91 -6
- package/dist/doctor.js.map +1 -1
- package/dist/frontend-guidance.d.ts +19 -0
- package/dist/frontend-guidance.d.ts.map +1 -1
- package/dist/frontend-guidance.js +267 -23
- package/dist/frontend-guidance.js.map +1 -1
- package/dist/hand-built-manifest.d.ts.map +1 -1
- package/dist/hand-built-manifest.js +1 -1
- package/dist/hand-built-manifest.js.map +1 -1
- package/dist/internal/control-character.d.ts +3 -0
- package/dist/internal/control-character.d.ts.map +1 -0
- package/dist/internal/control-character.js +10 -0
- package/dist/internal/control-character.js.map +1 -0
- package/dist/internal/escape-regexp.d.ts +3 -0
- package/dist/internal/escape-regexp.d.ts.map +1 -0
- package/dist/internal/escape-regexp.js +5 -0
- package/dist/internal/escape-regexp.js.map +1 -0
- package/dist/internal/line-at.d.ts +3 -0
- package/dist/internal/line-at.d.ts.map +1 -0
- package/dist/internal/line-at.js +5 -0
- package/dist/internal/line-at.js.map +1 -0
- package/dist/internal/nifra-project.d.ts.map +1 -1
- package/dist/internal/nifra-project.js +35 -41
- package/dist/internal/nifra-project.js.map +1 -1
- package/dist/internal/posix-path.d.ts +3 -0
- package/dist/internal/posix-path.d.ts.map +1 -0
- package/dist/internal/posix-path.js +5 -0
- package/dist/internal/posix-path.js.map +1 -0
- package/dist/internal/project-specifiers.d.ts.map +1 -1
- package/dist/internal/project-specifiers.js +3 -10
- package/dist/internal/project-specifiers.js.map +1 -1
- package/dist/internal/realpath-or-self.d.ts +3 -0
- package/dist/internal/realpath-or-self.d.ts.map +1 -0
- package/dist/internal/realpath-or-self.js +11 -0
- package/dist/internal/realpath-or-self.js.map +1 -0
- package/dist/internal/record.d.ts +3 -0
- package/dist/internal/record.d.ts.map +1 -0
- package/dist/internal/record.js +5 -0
- package/dist/internal/record.js.map +1 -0
- package/dist/internal/source-facts.d.ts +30 -0
- package/dist/internal/source-facts.d.ts.map +1 -1
- package/dist/internal/source-facts.js +155 -25
- package/dist/internal/source-facts.js.map +1 -1
- package/dist/introspect.d.ts +0 -22
- package/dist/introspect.d.ts.map +1 -1
- package/dist/introspect.js +11 -151
- package/dist/introspect.js.map +1 -1
- package/dist/levels-tool.d.ts +2 -0
- package/dist/levels-tool.d.ts.map +1 -1
- package/dist/levels-tool.js +54 -64
- package/dist/levels-tool.js.map +1 -1
- package/dist/load.d.ts +8 -1
- package/dist/load.d.ts.map +1 -1
- package/dist/load.js +5 -0
- package/dist/load.js.map +1 -1
- package/dist/local-cli.d.ts +7 -0
- package/dist/local-cli.d.ts.map +1 -1
- package/dist/local-cli.js +13 -0
- package/dist/local-cli.js.map +1 -1
- package/dist/mcp-docs-tools.d.ts +1 -1
- package/dist/mcp-docs-tools.d.ts.map +1 -1
- package/dist/mcp-docs-tools.js +46 -3
- package/dist/mcp-docs-tools.js.map +1 -1
- package/dist/mcp-http.d.ts +1 -1
- package/dist/mcp-http.js +2 -2
- package/dist/mcp-http.js.map +1 -1
- package/dist/mcp-io.d.ts +16 -0
- package/dist/mcp-io.d.ts.map +1 -1
- package/dist/mcp-io.js +63 -0
- package/dist/mcp-io.js.map +1 -1
- package/dist/mcp-isolate.d.ts.map +1 -1
- package/dist/mcp-isolate.js +2 -1
- package/dist/mcp-isolate.js.map +1 -1
- package/dist/mcp-project-child.d.ts.map +1 -1
- package/dist/mcp-project-child.js +5 -12
- package/dist/mcp-project-child.js.map +1 -1
- package/dist/mcp-render.d.ts.map +1 -1
- package/dist/mcp-render.js +9 -51
- package/dist/mcp-render.js.map +1 -1
- package/dist/mcp-root.js +1 -1
- package/dist/mcp-root.js.map +1 -1
- package/dist/mcp-run.d.ts.map +1 -1
- package/dist/mcp-run.js +3 -46
- package/dist/mcp-run.js.map +1 -1
- package/dist/mcp-tool-meta.d.ts.map +1 -1
- package/dist/mcp-tool-meta.js +0 -1
- package/dist/mcp-tool-meta.js.map +1 -1
- package/dist/mcp-ws.d.ts.map +1 -1
- package/dist/mcp-ws.js +2 -18
- package/dist/mcp-ws.js.map +1 -1
- package/dist/migrate-layout.d.ts.map +1 -1
- package/dist/migrate-layout.js +36 -57
- package/dist/migrate-layout.js.map +1 -1
- package/dist/output-schemas.d.ts.map +1 -1
- package/dist/output-schemas.js +7 -7
- package/dist/output-schemas.js.map +1 -1
- package/dist/port.d.ts.map +1 -1
- package/dist/port.js +1 -2
- package/dist/port.js.map +1 -1
- package/dist/ppr-read-fix.js +2 -2
- package/dist/ppr-read-fix.js.map +1 -1
- package/dist/recipes/5.10.0.d.ts +12 -0
- package/dist/recipes/5.10.0.d.ts.map +1 -0
- package/dist/recipes/5.10.0.js +44 -0
- package/dist/recipes/5.10.0.js.map +1 -0
- package/dist/recipes/5.9.0.d.ts +10 -0
- package/dist/recipes/5.9.0.d.ts.map +1 -0
- package/dist/recipes/5.9.0.js +41 -0
- package/dist/recipes/5.9.0.js.map +1 -0
- package/dist/recipes/index.d.ts +5 -0
- package/dist/recipes/index.d.ts.map +1 -1
- package/dist/recipes/index.js +4 -0
- package/dist/recipes/index.js.map +1 -1
- package/dist/review.d.ts.map +1 -1
- package/dist/review.js +12 -13
- package/dist/review.js.map +1 -1
- package/dist/rules/codes.d.ts +3 -0
- package/dist/rules/codes.d.ts.map +1 -1
- package/dist/rules/codes.js +3 -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 +3 -9
- package/dist/rules/contract-visibility.js.map +1 -1
- package/dist/rules/data-guard.d.ts.map +1 -1
- package/dist/rules/data-guard.js +3 -10
- package/dist/rules/data-guard.js.map +1 -1
- package/dist/rules/explanations.d.ts.map +1 -1
- package/dist/rules/explanations.js +15 -0
- package/dist/rules/explanations.js.map +1 -1
- package/dist/rules/internal-links.d.ts +5 -0
- package/dist/rules/internal-links.d.ts.map +1 -0
- package/dist/rules/internal-links.js +418 -0
- package/dist/rules/internal-links.js.map +1 -0
- package/dist/rules/legacy.d.ts.map +1 -1
- package/dist/rules/legacy.js +20 -0
- package/dist/rules/legacy.js.map +1 -1
- package/dist/rules/pages.d.ts.map +1 -1
- package/dist/rules/pages.js +41 -58
- package/dist/rules/pages.js.map +1 -1
- package/dist/rules/routes.d.ts.map +1 -1
- package/dist/rules/routes.js +20 -27
- package/dist/rules/routes.js.map +1 -1
- package/dist/rules/security.d.ts.map +1 -1
- package/dist/rules/security.js +206 -313
- package/dist/rules/security.js.map +1 -1
- package/dist/rules/server-fn-namespace.d.ts +8 -0
- package/dist/rules/server-fn-namespace.d.ts.map +1 -0
- package/dist/rules/server-fn-namespace.js +43 -0
- package/dist/rules/server-fn-namespace.js.map +1 -0
- package/dist/rules/source-scan.d.ts +29 -0
- package/dist/rules/source-scan.d.ts.map +1 -0
- package/dist/rules/source-scan.js +38 -0
- package/dist/rules/source-scan.js.map +1 -0
- package/dist/rules/text-slot-boolean.d.ts.map +1 -1
- package/dist/rules/text-slot-boolean.js +21 -43
- package/dist/rules/text-slot-boolean.js.map +1 -1
- package/dist/rules/zones.d.ts.map +1 -1
- package/dist/rules/zones.js +3 -10
- package/dist/rules/zones.js.map +1 -1
- package/dist/runtime-differences.d.ts +62 -0
- package/dist/runtime-differences.d.ts.map +1 -0
- package/dist/runtime-differences.js +222 -0
- package/dist/runtime-differences.js.map +1 -0
- package/dist/sdk.d.ts.map +1 -1
- package/dist/sdk.js +43 -43
- package/dist/sdk.js.map +1 -1
- package/dist/smoke.d.ts.map +1 -1
- package/dist/smoke.js +4 -12
- package/dist/smoke.js.map +1 -1
- package/dist/source-rewrites.d.ts +27 -0
- package/dist/source-rewrites.d.ts.map +1 -0
- package/dist/source-rewrites.js +517 -0
- package/dist/source-rewrites.js.map +1 -0
- package/dist/stylex-migrate.d.ts.map +1 -1
- package/dist/stylex-migrate.js +1 -1
- package/dist/stylex-migrate.js.map +1 -1
- package/dist/symptom-search.d.ts +5 -0
- package/dist/symptom-search.d.ts.map +1 -0
- package/dist/symptom-search.js +21 -0
- package/dist/symptom-search.js.map +1 -0
- package/dist/typecheck-coverage.d.ts +9 -0
- package/dist/typecheck-coverage.d.ts.map +1 -1
- package/dist/typecheck-coverage.js +16 -0
- package/dist/typecheck-coverage.js.map +1 -1
- package/dist/upgrade.d.ts +28 -3
- package/dist/upgrade.d.ts.map +1 -1
- package/dist/upgrade.js +153 -64
- package/dist/upgrade.js.map +1 -1
- package/dist/verification-plan.d.ts.map +1 -1
- package/dist/verification-plan.js +5 -3
- package/dist/verification-plan.js.map +1 -1
- package/docs/examples.json +51 -9
- package/docs/llms-full.txt +1296 -287
- package/docs/types.json +854 -170
- package/package.json +11 -11
- package/src/agent-files.ts +4 -1
- package/src/assure-hydration.ts +2 -9
- package/src/assure.ts +3 -4
- package/src/check-diagnostics.ts +5 -0
- package/src/check-scan.ts +74 -83
- package/src/check.ts +40 -26
- package/src/cli.ts +76 -20
- package/src/command-catalog.ts +22 -56
- package/src/config-target.ts +1 -1
- package/src/db-child.ts +3 -10
- package/src/db-config.ts +4 -0
- package/src/doctor.ts +128 -12
- package/src/frontend-guidance.ts +344 -25
- package/src/hand-built-manifest.ts +1 -2
- package/src/internal/control-character.ts +8 -0
- package/src/internal/escape-regexp.ts +4 -0
- package/src/internal/line-at.ts +4 -0
- package/src/internal/nifra-project.ts +39 -35
- package/src/internal/posix-path.ts +4 -0
- package/src/internal/project-specifiers.ts +3 -10
- package/src/internal/realpath-or-self.ts +10 -0
- package/src/internal/record.ts +4 -0
- package/src/internal/source-facts.ts +194 -29
- package/src/introspect.ts +16 -156
- package/src/levels-tool.ts +71 -78
- package/src/load.ts +15 -1
- package/src/local-cli.ts +12 -0
- package/src/mcp-docs-tools.ts +51 -3
- package/src/mcp-http.ts +2 -2
- package/src/mcp-io.ts +77 -0
- package/src/mcp-isolate.ts +2 -3
- package/src/mcp-project-child.ts +5 -14
- package/src/mcp-render.ts +11 -59
- package/src/mcp-root.ts +1 -1
- package/src/mcp-run.ts +5 -54
- package/src/mcp-tool-meta.ts +0 -1
- package/src/mcp-ws.ts +2 -19
- package/src/migrate-layout.ts +52 -72
- package/src/output-schemas.ts +7 -8
- package/src/port.ts +1 -4
- package/src/ppr-read-fix.ts +2 -2
- package/src/recipes/5.10.0.ts +45 -0
- package/src/recipes/5.9.0.ts +42 -0
- package/src/recipes/index.ts +9 -0
- package/src/review.ts +15 -15
- package/src/rules/codes.ts +3 -0
- package/src/rules/contract-visibility.ts +3 -8
- package/src/rules/data-guard.ts +3 -10
- package/src/rules/explanations.ts +15 -0
- package/src/rules/internal-links.ts +490 -0
- package/src/rules/legacy.ts +21 -0
- package/src/rules/pages.ts +47 -58
- package/src/rules/routes.ts +21 -27
- package/src/rules/security.ts +224 -255
- package/src/rules/server-fn-namespace.ts +47 -0
- package/src/rules/source-scan.ts +66 -0
- package/src/rules/text-slot-boolean.ts +26 -42
- package/src/rules/zones.ts +3 -10
- package/src/runtime-differences.ts +302 -0
- package/src/sdk.ts +55 -58
- package/src/smoke.ts +4 -13
- package/src/source-rewrites.ts +592 -0
- package/src/stylex-migrate.ts +1 -3
- package/src/symptom-search.ts +27 -0
- package/src/typecheck-coverage.ts +23 -0
- package/src/upgrade.ts +194 -74
- package/src/verification-plan.ts +10 -4
- package/dist/sync-routes.d.ts +0 -29
- package/dist/sync-routes.d.ts.map +0 -1
- package/dist/sync-routes.js +0 -56
- package/dist/sync-routes.js.map +0 -1
- package/src/sync-routes.ts +0 -70
|
@@ -36,6 +36,28 @@ export interface TextSlotBooleanCoercion {
|
|
|
36
36
|
readonly path: string
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
+
/** An `<a href>` whose value is a path of this origin, from {@link SourceFacts.internalAnchors}. */
|
|
40
|
+
export interface InternalAnchor {
|
|
41
|
+
/** The start of the `href` attribute. */
|
|
42
|
+
readonly position: number
|
|
43
|
+
/** The path as written; each `${...}` of a template literal is {@link ANCHOR_HOLE}. */
|
|
44
|
+
readonly path: string
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Stands for one `${...}` in {@link InternalAnchor.path}. */
|
|
48
|
+
export const ANCHOR_HOLE = "\u0000"
|
|
49
|
+
|
|
50
|
+
/** A `serverFunctions("namespace", module)` mount of a `*.fn.ts` module, from {@link SourceFacts.serverFunctionMounts}. */
|
|
51
|
+
export interface ServerFunctionMount {
|
|
52
|
+
/** The start of the namespace literal. */
|
|
53
|
+
readonly position: number
|
|
54
|
+
/** The start of the `serverFunctions(...)` call, where a `nifra-expect` comment is written above. */
|
|
55
|
+
readonly callPosition: number
|
|
56
|
+
readonly namespace: string
|
|
57
|
+
/** The specifier the mounted module namespace was imported from. */
|
|
58
|
+
readonly specifier: string
|
|
59
|
+
}
|
|
60
|
+
|
|
39
61
|
export interface SourceFacts {
|
|
40
62
|
parse(file: string, content: string): TSApi.SourceFile | undefined
|
|
41
63
|
isValueImportAt(
|
|
@@ -92,6 +114,17 @@ export interface SourceFacts {
|
|
|
92
114
|
* another file is not followed.
|
|
93
115
|
*/
|
|
94
116
|
textSlotBooleanCoercions(source: TSApi.SourceFile): readonly TextSlotBooleanCoercion[]
|
|
117
|
+
/**
|
|
118
|
+
* The `serverFunctions("namespace", module)` calls in `source` whose `module` is a namespace import
|
|
119
|
+
* (`import * as todos from "./todos.fn"`) of a `*.fn` file. A module reached any other way is left out.
|
|
120
|
+
*/
|
|
121
|
+
serverFunctionMounts(source: TSApi.SourceFile): readonly ServerFunctionMount[]
|
|
122
|
+
/**
|
|
123
|
+
* The `<a>` elements of `source` whose `href` is a string or template literal that starts with
|
|
124
|
+
* exactly one `/`. A value that is built (`href={url}`, `href={href({ to })}`), an external URL, a
|
|
125
|
+
* `#hash` and a `//host` are left out.
|
|
126
|
+
*/
|
|
127
|
+
internalAnchors(source: TSApi.SourceFile): readonly InternalAnchor[]
|
|
95
128
|
/** Whether the function `source` exports as `name` returns a raw `Response` on some path. */
|
|
96
129
|
exportReturnsResponse(source: TSApi.SourceFile, name: string): boolean
|
|
97
130
|
/** Whether the `import` keyword at `position` starts a runtime `import(specifier)` call (true) or a
|
|
@@ -151,6 +184,30 @@ const REGISTRATION_METHODS: ReadonlySet<string> = new Set([
|
|
|
151
184
|
"all",
|
|
152
185
|
])
|
|
153
186
|
|
|
187
|
+
/** A route registration such as `app.get("/path", handler)`: its method name, path, and the arguments after the path. */
|
|
188
|
+
function routeRegistration(
|
|
189
|
+
ts: TypeScriptApi,
|
|
190
|
+
node: TSApi.Node,
|
|
191
|
+
):
|
|
192
|
+
| {
|
|
193
|
+
readonly name: TSApi.MemberName
|
|
194
|
+
readonly method: string
|
|
195
|
+
readonly path: string
|
|
196
|
+
readonly rest: readonly TSApi.Expression[]
|
|
197
|
+
}
|
|
198
|
+
| undefined {
|
|
199
|
+
if (!ts.isCallExpression(node) || !ts.isPropertyAccessExpression(node.expression))
|
|
200
|
+
return undefined
|
|
201
|
+
const method = node.expression.name.text
|
|
202
|
+
const [first, ...rest] = node.arguments
|
|
203
|
+
if (!REGISTRATION_METHODS.has(method) || first === undefined || !ts.isStringLiteralLike(first))
|
|
204
|
+
return undefined
|
|
205
|
+
return { name: node.expression.name, method, path: first.text, rest }
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const SERVER_FN_MODULE_ID = "@nifrajs/web/fn"
|
|
209
|
+
const SERVER_FN_FILE = /\.fn(?:\.[cm]?[jt]sx?)?$/
|
|
210
|
+
|
|
154
211
|
const ZOD_MODULES: ReadonlySet<string> = new Set(["zod", "zod/v4", "zod/mini", "zod/v4/mini"])
|
|
155
212
|
|
|
156
213
|
/** The names `source` binds zod's namespace to (`z`), and those it binds zod's `coerce` to. */
|
|
@@ -181,6 +238,23 @@ function zodBindings(
|
|
|
181
238
|
return { namespaces, coerce }
|
|
182
239
|
}
|
|
183
240
|
|
|
241
|
+
/** Every name `source` declares other than by import: variables, parameters, functions and classes, any scope. */
|
|
242
|
+
function declaredNames(ts: TypeScriptApi, source: TSApi.SourceFile): Set<string> {
|
|
243
|
+
const names = new Set<string>()
|
|
244
|
+
const bind = (name: TSApi.BindingName): void => {
|
|
245
|
+
if (ts.isIdentifier(name)) names.add(name.text)
|
|
246
|
+
else for (const element of name.elements) if (ts.isBindingElement(element)) bind(element.name)
|
|
247
|
+
}
|
|
248
|
+
const visit = (node: TSApi.Node): void => {
|
|
249
|
+
if (ts.isVariableDeclaration(node) || ts.isParameter(node)) bind(node.name)
|
|
250
|
+
else if ((ts.isFunctionDeclaration(node) || ts.isClassDeclaration(node)) && node.name)
|
|
251
|
+
names.add(node.name.text)
|
|
252
|
+
ts.forEachChild(node, visit)
|
|
253
|
+
}
|
|
254
|
+
visit(source)
|
|
255
|
+
return names
|
|
256
|
+
}
|
|
257
|
+
|
|
184
258
|
/** The initializer of each variable of `source`, any scope, by name: the first one wins. */
|
|
185
259
|
function variableInitializers(
|
|
186
260
|
ts: TypeScriptApi,
|
|
@@ -707,15 +781,9 @@ export function createSourceFacts(
|
|
|
707
781
|
const routes: UndeclaredResponseRoute[] = []
|
|
708
782
|
const visit = (node: TSApi.Node): void => {
|
|
709
783
|
ts.forEachChild(node, visit)
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
const
|
|
713
|
-
if (
|
|
714
|
-
!REGISTRATION_METHODS.has(method) ||
|
|
715
|
-
first === undefined ||
|
|
716
|
-
!ts.isStringLiteralLike(first)
|
|
717
|
-
)
|
|
718
|
-
return
|
|
784
|
+
const registration = routeRegistration(ts, node)
|
|
785
|
+
if (registration === undefined) return
|
|
786
|
+
const { name, method, path, rest } = registration
|
|
719
787
|
const handlers: FunctionNode[] = []
|
|
720
788
|
for (const argument of rest.map((value) => unwrapExpression(ts, value))) {
|
|
721
789
|
const local = ts.isIdentifier(argument) ? locals.get(argument.text) : undefined
|
|
@@ -723,18 +791,49 @@ export function createSourceFacts(
|
|
|
723
791
|
else if (local !== undefined) handlers.push(...local)
|
|
724
792
|
else if (!ts.isObjectLiteralExpression(argument) || declaresResponse(ts, argument)) return
|
|
725
793
|
}
|
|
726
|
-
|
|
794
|
+
// Every registration takes a handler: `headers.get("etag")` and `map.get("/")` are reads.
|
|
795
|
+
if (handlers.length === 0 || handlers.every((fn) => raw.isUntypedAt(fn.getStart(source))))
|
|
727
796
|
return
|
|
728
|
-
routes.push({
|
|
729
|
-
position: node.expression.name.getStart(source),
|
|
730
|
-
method: method.toUpperCase(),
|
|
731
|
-
path: first.text,
|
|
732
|
-
})
|
|
797
|
+
routes.push({ position: name.getStart(source), method: method.toUpperCase(), path })
|
|
733
798
|
}
|
|
734
799
|
visit(source)
|
|
735
800
|
return routes.sort((a, b) => a.position - b.position)
|
|
736
801
|
}
|
|
737
802
|
|
|
803
|
+
const internalAnchors = (source: TSApi.SourceFile): readonly InternalAnchor[] => {
|
|
804
|
+
const found: InternalAnchor[] = []
|
|
805
|
+
const pathOf = (node: TSApi.Expression): string | undefined => {
|
|
806
|
+
if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) return node.text
|
|
807
|
+
if (!ts.isTemplateExpression(node)) return undefined
|
|
808
|
+
return (
|
|
809
|
+
node.head.text + node.templateSpans.map((span) => ANCHOR_HOLE + span.literal.text).join("")
|
|
810
|
+
)
|
|
811
|
+
}
|
|
812
|
+
const visit = (node: TSApi.Node): void => {
|
|
813
|
+
if (
|
|
814
|
+
(ts.isJsxOpeningElement(node) || ts.isJsxSelfClosingElement(node)) &&
|
|
815
|
+
ts.isIdentifier(node.tagName) &&
|
|
816
|
+
node.tagName.text === "a"
|
|
817
|
+
) {
|
|
818
|
+
for (const attribute of node.attributes.properties) {
|
|
819
|
+
if (!ts.isJsxAttribute(attribute) || attribute.name.getText(source) !== "href") continue
|
|
820
|
+
const init = attribute.initializer
|
|
821
|
+
if (init === undefined) continue
|
|
822
|
+
const path = ts.isStringLiteral(init)
|
|
823
|
+
? init.text
|
|
824
|
+
: ts.isJsxExpression(init) && init.expression !== undefined
|
|
825
|
+
? pathOf(init.expression)
|
|
826
|
+
: undefined
|
|
827
|
+
if (path?.startsWith("/") && !path.startsWith("//"))
|
|
828
|
+
found.push({ position: attribute.getStart(source), path })
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
ts.forEachChild(node, visit)
|
|
832
|
+
}
|
|
833
|
+
visit(source)
|
|
834
|
+
return found
|
|
835
|
+
}
|
|
836
|
+
|
|
738
837
|
const textSlotBooleanCoercions = (
|
|
739
838
|
source: TSApi.SourceFile,
|
|
740
839
|
): readonly TextSlotBooleanCoercion[] => {
|
|
@@ -783,19 +882,17 @@ export function createSourceFacts(
|
|
|
783
882
|
}
|
|
784
883
|
const visit = (node: TSApi.Node): void => {
|
|
785
884
|
ts.forEachChild(node, visit)
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
const
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
)
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
const named = ts.isIdentifier(options) ? consts.get(options.text) : undefined
|
|
798
|
-
if (named !== undefined) options = unwrapExpression(ts, named)
|
|
885
|
+
const registration = routeRegistration(ts, node)
|
|
886
|
+
if (registration === undefined) return
|
|
887
|
+
const { method, path, rest } = registration
|
|
888
|
+
const arguments_ = rest.map((argument) => {
|
|
889
|
+
const value = unwrapExpression(ts, argument)
|
|
890
|
+
const named = ts.isIdentifier(value) ? consts.get(value.text) : undefined
|
|
891
|
+
return named === undefined ? value : unwrapExpression(ts, named)
|
|
892
|
+
})
|
|
893
|
+
// Every registration takes a handler: `config.get("key", { query })` is not a route.
|
|
894
|
+
if (arguments_.every((value) => ts.isObjectLiteralExpression(value))) return
|
|
895
|
+
for (const options of arguments_) {
|
|
799
896
|
if (!ts.isObjectLiteralExpression(options)) continue
|
|
800
897
|
for (const property of options.properties) {
|
|
801
898
|
if (!ts.isPropertyAssignment(property) && !ts.isShorthandPropertyAssignment(property))
|
|
@@ -806,7 +903,7 @@ export function createSourceFacts(
|
|
|
806
903
|
: undefined
|
|
807
904
|
if (slot !== "query" && slot !== "headers" && slot !== "cookies") continue
|
|
808
905
|
const value = ts.isPropertyAssignment(property) ? property.initializer : property.name
|
|
809
|
-
reach(value, new Set(), { slot, method: method.toUpperCase(), path
|
|
906
|
+
reach(value, new Set(), { slot, method: method.toUpperCase(), path })
|
|
810
907
|
}
|
|
811
908
|
}
|
|
812
909
|
}
|
|
@@ -843,6 +940,72 @@ export function createSourceFacts(
|
|
|
843
940
|
}
|
|
844
941
|
}
|
|
845
942
|
|
|
943
|
+
const serverFunctionMounts = (source: TSApi.SourceFile): readonly ServerFunctionMount[] => {
|
|
944
|
+
const callees = new Set<string>()
|
|
945
|
+
const members = new Set<string>()
|
|
946
|
+
const modules = new Map<string, string>()
|
|
947
|
+
for (const statement of source.statements) {
|
|
948
|
+
if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier))
|
|
949
|
+
continue
|
|
950
|
+
const clause = statement.importClause
|
|
951
|
+
if (clause === undefined || clause.isTypeOnly) continue
|
|
952
|
+
const specifier = statement.moduleSpecifier.text
|
|
953
|
+
const bindings = clause.namedBindings
|
|
954
|
+
if (bindings === undefined) continue
|
|
955
|
+
if (ts.isNamespaceImport(bindings)) {
|
|
956
|
+
if (specifier === SERVER_FN_MODULE_ID) members.add(bindings.name.text)
|
|
957
|
+
else if (SERVER_FN_FILE.test(specifier)) modules.set(bindings.name.text, specifier)
|
|
958
|
+
} else if (specifier === SERVER_FN_MODULE_ID) {
|
|
959
|
+
for (const element of bindings.elements) {
|
|
960
|
+
if (
|
|
961
|
+
!element.isTypeOnly &&
|
|
962
|
+
(element.propertyName ?? element.name).text === "serverFunctions"
|
|
963
|
+
)
|
|
964
|
+
callees.add(element.name.text)
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
if (modules.size === 0 || (callees.size === 0 && members.size === 0)) return []
|
|
969
|
+
// A name the file declares again (a local `serverFunctions`, a parameter called like the module) is
|
|
970
|
+
// not the import, whatever scope it sits in: leave those calls out rather than guess the scope.
|
|
971
|
+
for (const name of declaredNames(ts, source)) {
|
|
972
|
+
callees.delete(name)
|
|
973
|
+
members.delete(name)
|
|
974
|
+
modules.delete(name)
|
|
975
|
+
}
|
|
976
|
+
const mounts: ServerFunctionMount[] = []
|
|
977
|
+
const visit = (node: TSApi.Node): void => {
|
|
978
|
+
if (ts.isCallExpression(node)) {
|
|
979
|
+
const callee = node.expression
|
|
980
|
+
const [namespace, module] = node.arguments
|
|
981
|
+
if (
|
|
982
|
+
(ts.isIdentifier(callee)
|
|
983
|
+
? callees.has(callee.text)
|
|
984
|
+
: ts.isPropertyAccessExpression(callee) &&
|
|
985
|
+
ts.isIdentifier(callee.expression) &&
|
|
986
|
+
members.has(callee.expression.text) &&
|
|
987
|
+
callee.name.text === "serverFunctions") &&
|
|
988
|
+
namespace !== undefined &&
|
|
989
|
+
ts.isStringLiteralLike(namespace) &&
|
|
990
|
+
module !== undefined &&
|
|
991
|
+
ts.isIdentifier(module)
|
|
992
|
+
) {
|
|
993
|
+
const specifier = modules.get(module.text)
|
|
994
|
+
if (specifier !== undefined)
|
|
995
|
+
mounts.push({
|
|
996
|
+
position: namespace.getStart(source),
|
|
997
|
+
callPosition: node.getStart(source),
|
|
998
|
+
namespace: namespace.text,
|
|
999
|
+
specifier,
|
|
1000
|
+
})
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
ts.forEachChild(node, visit)
|
|
1004
|
+
}
|
|
1005
|
+
visit(source)
|
|
1006
|
+
return mounts
|
|
1007
|
+
}
|
|
1008
|
+
|
|
846
1009
|
return {
|
|
847
1010
|
parse,
|
|
848
1011
|
isValueImportAt,
|
|
@@ -853,6 +1016,8 @@ export function createSourceFacts(
|
|
|
853
1016
|
untypedResponseRoutes,
|
|
854
1017
|
undeclaredResponseRoutes,
|
|
855
1018
|
textSlotBooleanCoercions,
|
|
1019
|
+
internalAnchors,
|
|
1020
|
+
serverFunctionMounts,
|
|
856
1021
|
exportReturnsResponse,
|
|
857
1022
|
isDynamicImportAt,
|
|
858
1023
|
}
|
package/src/introspect.ts
CHANGED
|
@@ -12,10 +12,15 @@ import {
|
|
|
12
12
|
reflectedRoutesFromEvidence,
|
|
13
13
|
snapshotProjectEvidence,
|
|
14
14
|
} from "@nifrajs/core/evidence"
|
|
15
|
-
import { compileRoutePattern, type RoutePatternSegment } from "@nifrajs/core/pattern"
|
|
16
15
|
import type { ReflectedMount, ReflectedRoute } from "@nifrajs/core/reflection"
|
|
17
16
|
import type { Manifest } from "@nifrajs/web"
|
|
18
17
|
import { discoverRoutes } from "@nifrajs/web/fs"
|
|
18
|
+
import {
|
|
19
|
+
clientCall,
|
|
20
|
+
clientChain,
|
|
21
|
+
tsTypeOf,
|
|
22
|
+
typedClientKey,
|
|
23
|
+
} from "@nifrajs/web/internal/client-call"
|
|
19
24
|
import { normalizeMountPath } from "@nifrajs/web/route-manifest"
|
|
20
25
|
import { codeUnitOrder } from "./internal/code-unit-order.ts"
|
|
21
26
|
import type { IntrospectedApp } from "./load.ts"
|
|
@@ -26,64 +31,6 @@ export function backendRoutes(backend: unknown): readonly ReflectedRoute[] {
|
|
|
26
31
|
return reflectedRoutesFromEvidence(snapshotProjectEvidence(backend))
|
|
27
32
|
}
|
|
28
33
|
|
|
29
|
-
interface JsonSchemaNode {
|
|
30
|
-
readonly type?: unknown
|
|
31
|
-
readonly properties?: Record<string, unknown>
|
|
32
|
-
readonly required?: unknown
|
|
33
|
-
readonly items?: unknown
|
|
34
|
-
readonly additionalProperties?: unknown
|
|
35
|
-
readonly anyOf?: unknown
|
|
36
|
-
readonly oneOf?: unknown
|
|
37
|
-
readonly enum?: unknown
|
|
38
|
-
readonly const?: unknown
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Render a JSON Schema as a compact TypeScript-like type (`{ id: string, name?: string }`) - the
|
|
43
|
-
* shape agents already think in, at a fraction of the raw JSON Schema's tokens (~70-80% smaller).
|
|
44
|
-
* Anything the renderer doesn't model falls back to the raw JSON for that subtree, so the output
|
|
45
|
-
* is always faithful, just not always minimal.
|
|
46
|
-
*/
|
|
47
|
-
export function tsTypeOf(schema: unknown, depth = 0): string {
|
|
48
|
-
if (typeof schema !== "object" || schema === null || depth > 6) return JSON.stringify(schema)
|
|
49
|
-
const node = schema as JsonSchemaNode
|
|
50
|
-
const union = (node.anyOf ?? node.oneOf) as unknown[] | undefined
|
|
51
|
-
if (Array.isArray(union)) return union.map((u) => tsTypeOf(u, depth + 1)).join(" | ")
|
|
52
|
-
if (Array.isArray(node.enum)) return node.enum.map((v) => JSON.stringify(v)).join(" | ")
|
|
53
|
-
if (node.const !== undefined) return JSON.stringify(node.const)
|
|
54
|
-
switch (node.type) {
|
|
55
|
-
case "string":
|
|
56
|
-
return "string"
|
|
57
|
-
case "number":
|
|
58
|
-
case "integer":
|
|
59
|
-
return "number"
|
|
60
|
-
case "boolean":
|
|
61
|
-
return "boolean"
|
|
62
|
-
case "null":
|
|
63
|
-
return "null"
|
|
64
|
-
case "array": {
|
|
65
|
-
const item = tsTypeOf(node.items, depth + 1)
|
|
66
|
-
// Parenthesize unions so `(A | B)[]` reads as the array it is.
|
|
67
|
-
return item.includes(" | ") ? `(${item})[]` : `${item}[]`
|
|
68
|
-
}
|
|
69
|
-
case "object": {
|
|
70
|
-
const props = node.properties
|
|
71
|
-
if (props === undefined) {
|
|
72
|
-
return node.additionalProperties === undefined || node.additionalProperties === false
|
|
73
|
-
? "{}"
|
|
74
|
-
: `Record<string, ${node.additionalProperties === true ? "unknown" : tsTypeOf(node.additionalProperties, depth + 1)}>`
|
|
75
|
-
}
|
|
76
|
-
const required = new Set(Array.isArray(node.required) ? (node.required as string[]) : [])
|
|
77
|
-
const fields = Object.entries(props).map(
|
|
78
|
-
([key, value]) => `${key}${required.has(key) ? "" : "?"}: ${tsTypeOf(value, depth + 1)}`,
|
|
79
|
-
)
|
|
80
|
-
return `{ ${fields.join(", ")} }`
|
|
81
|
-
}
|
|
82
|
-
default:
|
|
83
|
-
return JSON.stringify(schema)
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
34
|
/** One route's request/response field shapes, indented under its line. Reads the declared schemas off the
|
|
88
35
|
* descriptor so an agent gets the exact input + output contract - not just "this route is validated". */
|
|
89
36
|
function schemaLines(schema: ReflectedRoute["schema"]): string[] {
|
|
@@ -103,95 +50,14 @@ function schemaLines(schema: ReflectedRoute["schema"]): string[] {
|
|
|
103
50
|
return out
|
|
104
51
|
}
|
|
105
52
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
"delete",
|
|
115
|
-
"head",
|
|
116
|
-
"options",
|
|
117
|
-
])
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* The call a non-static segment adds to the chain, or `undefined` for a static one. The segment is
|
|
121
|
-
* read by the router's own grammar, so the spelling matches what the typed client accepts:
|
|
122
|
-
* - a whole param or a wildcard is called by its name - `:id` and `:id{[0-9]+}` are `({ id })`;
|
|
123
|
-
* - a segment that is part literal, part parameter is called with the segment text -
|
|
124
|
-
* `:name.json` is ``(`${name}.json`)``.
|
|
125
|
-
*/
|
|
126
|
-
function paramCall(seg: string): string | undefined {
|
|
127
|
-
let segment: RoutePatternSegment | undefined
|
|
128
|
-
try {
|
|
129
|
-
segment = compileRoutePattern(`/${seg}`).segments[0]
|
|
130
|
-
} catch {
|
|
131
|
-
return undefined
|
|
132
|
-
}
|
|
133
|
-
if (segment === undefined || segment.kind === "static") return undefined
|
|
134
|
-
if (segment.kind === "param") return `({ ${segment.name} })`
|
|
135
|
-
if (segment.kind === "wildcard") {
|
|
136
|
-
return segment.name === "*" ? '({ "*": rest })' : `({ ${segment.name} })`
|
|
137
|
-
}
|
|
138
|
-
const [only] = segment.parts
|
|
139
|
-
if (segment.parts.length === 1 && only?.t === "param") return `({ ${only.name} })`
|
|
140
|
-
const text = segment.parts
|
|
141
|
-
.map((part) => (part.t === "lit" ? part.v.replace(/[`\\]|\$\{/g, "\\$&") : `\${${part.name}}`))
|
|
142
|
-
.join("")
|
|
143
|
-
return `(\`${text}\`)`
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* The typed-client proxy chain for a path, without the terminal verb call. Shared by `clientCall`
|
|
148
|
-
* and the `nifra routes` collision annotation so both teach exactly one spelling.
|
|
149
|
-
*/
|
|
150
|
-
function clientChain(path: string): string {
|
|
151
|
-
const segs = path.split("/").filter((seg) => seg !== "")
|
|
152
|
-
if (segs.length === 0) return "api.index"
|
|
153
|
-
let chain = "api"
|
|
154
|
-
for (const seg of segs) {
|
|
155
|
-
const call = paramCall(seg)
|
|
156
|
-
if (call !== undefined) chain += call
|
|
157
|
-
else if (reservedKeyFor(seg) !== undefined) chain += `(${JSON.stringify(seg)})`
|
|
158
|
-
else chain += IDENT.test(seg) ? `.${seg}` : `[${JSON.stringify(seg)}]`
|
|
159
|
-
}
|
|
160
|
-
return chain
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* The typed-client call form for a route - the exact `client<typeof app>` proxy chain an agent should
|
|
165
|
-
* write, derived from the same convention `@nifrajs/client` implements (so it never has to read the
|
|
166
|
-
* client tests to learn it): a static segment is a property (`.users`), a path param/wildcard is a call
|
|
167
|
-
* that appends the value (`({ id })`), a segment that is part literal, part parameter is a call with
|
|
168
|
-
* the segment text (``(`${name}.json`)``), the root path is `.index`, and the HTTP verb is the terminal call.
|
|
169
|
-
* Body verbs (POST/PUT/PATCH) take the body first then call-options; other verbs take call-options first -
|
|
170
|
-
* so the `{ query }` argument lands in the right slot for each.
|
|
171
|
-
*
|
|
172
|
-
* A segment spelling a reserved proxy key (`delete`, `subscribe`, `then`, ...) is emitted as a call on
|
|
173
|
-
* the parent node instead - and NOT as bracket access, which does not help: the proxy intercepts the
|
|
174
|
-
* key however it is spelled. Emitting `.delete` here would hand an agent a chain that resolves the
|
|
175
|
-
* DELETE verb and silently requests the wrong path.
|
|
176
|
-
*/
|
|
177
|
-
export function clientCall(method: string, path: string, schema: unknown): string {
|
|
178
|
-
const s = schema as { body?: unknown; query?: unknown } | undefined
|
|
179
|
-
const verb = method.toLowerCase()
|
|
180
|
-
// The typed client has a call for the standard methods only; any other method goes out through
|
|
181
|
-
// `fetch`, against the route's own path.
|
|
182
|
-
if (!TYPED_VERBS.has(verb)) return `await fetch(url, { method: ${JSON.stringify(method)} })`
|
|
183
|
-
const chain = clientChain(path)
|
|
184
|
-
const isBodyVerb = verb === "post" || verb === "put" || verb === "patch"
|
|
185
|
-
let call: string
|
|
186
|
-
if (isBodyVerb) {
|
|
187
|
-
// body is the 1st arg; if a route has a query but no body, the body slot must still be filled.
|
|
188
|
-
const bodyArg = s?.body ? "body" : s?.query ? "undefined" : ""
|
|
189
|
-
const opts = s?.query ? (bodyArg ? ", { query }" : "{ query }") : ""
|
|
190
|
-
call = `.${verb}(${bodyArg}${opts})`
|
|
191
|
-
} else {
|
|
192
|
-
call = `.${verb}(${s?.query ? "{ query }" : ""})`
|
|
193
|
-
}
|
|
194
|
-
return `await ${chain}${call}`
|
|
53
|
+
/** A route's `call` line, with what the call returns, or why there is no typed call. */
|
|
54
|
+
function callLine(route: ReflectedRoute): string {
|
|
55
|
+
const call = ` - call: \`${clientCall(route.method, route.path, route.schema)}\``
|
|
56
|
+
if (route.method === "WS")
|
|
57
|
+
return `${call} → a WebSocket handle (\`send\`, \`messages()\`, \`close\`)`
|
|
58
|
+
return typedClientKey(route.method) !== undefined
|
|
59
|
+
? `${call} → \`{ ok, status, data, error }\``
|
|
60
|
+
: `${call} (no typed-client call for this method; \`url\` is the path above)`
|
|
195
61
|
}
|
|
196
62
|
|
|
197
63
|
/** Markdown section listing the backend's API routes with their request + response field shapes. */
|
|
@@ -201,14 +67,8 @@ export function apiRoutesSection(routes: readonly ReflectedRoute[]): string {
|
|
|
201
67
|
}
|
|
202
68
|
const lines = [...routes]
|
|
203
69
|
.sort((a, b) => codeUnitOrder(a.path, b.path) || codeUnitOrder(a.method, b.method))
|
|
204
|
-
.flatMap((r) => [
|
|
205
|
-
|
|
206
|
-
...schemaLines(r.schema),
|
|
207
|
-
TYPED_VERBS.has(r.method.toLowerCase())
|
|
208
|
-
? ` - call: \`${clientCall(r.method, r.path, r.schema)}\` → \`{ ok, status, data, error }\``
|
|
209
|
-
: ` - call: \`${clientCall(r.method, r.path, r.schema)}\` (no typed-client call for this method; \`url\` is the path above)`,
|
|
210
|
-
])
|
|
211
|
-
return `## API routes (backend/app.ts)\n\nEach route's \`body\`/\`query\`/\`response\` shape is its contract - the typed client derives request inputs and \`res.data\` from these, so a screen built on \`client<typeof app>\` stays in sync automatically. The \`call\` line is the exact \`client<typeof app>\` form: static path segments are properties, a path param is a call (\`({ id })\`), the verb is the terminal call (body first for POST/PUT/PATCH), and every call returns the never-throwing \`{ ok, status, data, error }\` Result.\n\n${lines.join("\n")}`
|
|
70
|
+
.flatMap((r) => [`- \`${r.method} ${r.path}\``, ...schemaLines(r.schema), callLine(r)])
|
|
71
|
+
return `## API routes (backend/app.ts)\n\nEach route's \`body\`/\`query\`/\`response\` shape is its contract - the typed client derives request inputs and \`res.data\` from these, so a screen built on \`client<typeof app>\` stays in sync automatically. The \`call\` line is the exact \`client<typeof app>\` form: static path segments are properties, a path param is a call (\`({ id })\`), the verb is the terminal call (body first for POST/PUT/PATCH), and every HTTP call returns the never-throwing \`{ ok, status, data, error }\` Result.\n\n${lines.join("\n")}`
|
|
212
72
|
}
|
|
213
73
|
|
|
214
74
|
/** Compact API-routes INDEX for the no-arg `nifra_context` call - `METHOD path` per route, WITHOUT the
|