@nifrajs/cli 5.8.0 → 5.9.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.
Files changed (248) hide show
  1. package/CHANGELOG.md +134 -0
  2. package/dist/agent-files.d.ts +0 -22
  3. package/dist/agent-files.d.ts.map +1 -1
  4. package/dist/agent-files.js +3 -1
  5. package/dist/agent-files.js.map +1 -1
  6. package/dist/assure-hydration.d.ts.map +1 -1
  7. package/dist/assure-hydration.js +2 -10
  8. package/dist/assure-hydration.js.map +1 -1
  9. package/dist/assure.d.ts.map +1 -1
  10. package/dist/assure.js +3 -3
  11. package/dist/assure.js.map +1 -1
  12. package/dist/check-scan.d.ts +2 -0
  13. package/dist/check-scan.d.ts.map +1 -1
  14. package/dist/check-scan.js +54 -86
  15. package/dist/check-scan.js.map +1 -1
  16. package/dist/check.d.ts +3 -0
  17. package/dist/check.d.ts.map +1 -1
  18. package/dist/check.js +20 -23
  19. package/dist/check.js.map +1 -1
  20. package/dist/cli.d.ts +8 -0
  21. package/dist/cli.d.ts.map +1 -1
  22. package/dist/cli.js +42 -13
  23. package/dist/cli.js.map +1 -1
  24. package/dist/command-catalog.d.ts.map +1 -1
  25. package/dist/command-catalog.js +20 -30
  26. package/dist/command-catalog.js.map +1 -1
  27. package/dist/config-target.js +1 -1
  28. package/dist/config-target.js.map +1 -1
  29. package/dist/db-child.js +3 -10
  30. package/dist/db-child.js.map +1 -1
  31. package/dist/db-config.d.ts +2 -0
  32. package/dist/db-config.d.ts.map +1 -1
  33. package/dist/db-config.js +2 -0
  34. package/dist/db-config.js.map +1 -1
  35. package/dist/doctor.d.ts +14 -3
  36. package/dist/doctor.d.ts.map +1 -1
  37. package/dist/doctor.js +91 -6
  38. package/dist/doctor.js.map +1 -1
  39. package/dist/frontend-guidance.d.ts +19 -0
  40. package/dist/frontend-guidance.d.ts.map +1 -1
  41. package/dist/frontend-guidance.js +267 -23
  42. package/dist/frontend-guidance.js.map +1 -1
  43. package/dist/hand-built-manifest.d.ts.map +1 -1
  44. package/dist/hand-built-manifest.js +1 -1
  45. package/dist/hand-built-manifest.js.map +1 -1
  46. package/dist/internal/control-character.d.ts +3 -0
  47. package/dist/internal/control-character.d.ts.map +1 -0
  48. package/dist/internal/control-character.js +10 -0
  49. package/dist/internal/control-character.js.map +1 -0
  50. package/dist/internal/escape-regexp.d.ts +3 -0
  51. package/dist/internal/escape-regexp.d.ts.map +1 -0
  52. package/dist/internal/escape-regexp.js +5 -0
  53. package/dist/internal/escape-regexp.js.map +1 -0
  54. package/dist/internal/line-at.d.ts +3 -0
  55. package/dist/internal/line-at.d.ts.map +1 -0
  56. package/dist/internal/line-at.js +5 -0
  57. package/dist/internal/line-at.js.map +1 -0
  58. package/dist/internal/nifra-project.d.ts.map +1 -1
  59. package/dist/internal/nifra-project.js +35 -41
  60. package/dist/internal/nifra-project.js.map +1 -1
  61. package/dist/internal/posix-path.d.ts +3 -0
  62. package/dist/internal/posix-path.d.ts.map +1 -0
  63. package/dist/internal/posix-path.js +5 -0
  64. package/dist/internal/posix-path.js.map +1 -0
  65. package/dist/internal/project-specifiers.d.ts.map +1 -1
  66. package/dist/internal/project-specifiers.js +3 -10
  67. package/dist/internal/project-specifiers.js.map +1 -1
  68. package/dist/internal/realpath-or-self.d.ts +3 -0
  69. package/dist/internal/realpath-or-self.d.ts.map +1 -0
  70. package/dist/internal/realpath-or-self.js +11 -0
  71. package/dist/internal/realpath-or-self.js.map +1 -0
  72. package/dist/internal/record.d.ts +3 -0
  73. package/dist/internal/record.d.ts.map +1 -0
  74. package/dist/internal/record.js +5 -0
  75. package/dist/internal/record.js.map +1 -0
  76. package/dist/internal/source-facts.d.ts.map +1 -1
  77. package/dist/internal/source-facts.js +28 -25
  78. package/dist/internal/source-facts.js.map +1 -1
  79. package/dist/introspect.d.ts +0 -22
  80. package/dist/introspect.d.ts.map +1 -1
  81. package/dist/introspect.js +11 -151
  82. package/dist/introspect.js.map +1 -1
  83. package/dist/levels-tool.d.ts +2 -0
  84. package/dist/levels-tool.d.ts.map +1 -1
  85. package/dist/levels-tool.js +54 -64
  86. package/dist/levels-tool.js.map +1 -1
  87. package/dist/load.d.ts +6 -1
  88. package/dist/load.d.ts.map +1 -1
  89. package/dist/load.js.map +1 -1
  90. package/dist/local-cli.d.ts +7 -0
  91. package/dist/local-cli.d.ts.map +1 -1
  92. package/dist/local-cli.js +13 -0
  93. package/dist/local-cli.js.map +1 -1
  94. package/dist/mcp-docs-tools.d.ts +1 -1
  95. package/dist/mcp-docs-tools.d.ts.map +1 -1
  96. package/dist/mcp-docs-tools.js +46 -3
  97. package/dist/mcp-docs-tools.js.map +1 -1
  98. package/dist/mcp-http.d.ts +1 -1
  99. package/dist/mcp-http.js +2 -2
  100. package/dist/mcp-io.d.ts +16 -0
  101. package/dist/mcp-io.d.ts.map +1 -1
  102. package/dist/mcp-io.js +63 -0
  103. package/dist/mcp-io.js.map +1 -1
  104. package/dist/mcp-isolate.d.ts.map +1 -1
  105. package/dist/mcp-isolate.js +2 -1
  106. package/dist/mcp-isolate.js.map +1 -1
  107. package/dist/mcp-project-child.d.ts.map +1 -1
  108. package/dist/mcp-project-child.js +5 -12
  109. package/dist/mcp-project-child.js.map +1 -1
  110. package/dist/mcp-render.d.ts.map +1 -1
  111. package/dist/mcp-render.js +9 -51
  112. package/dist/mcp-render.js.map +1 -1
  113. package/dist/mcp-root.js +1 -1
  114. package/dist/mcp-root.js.map +1 -1
  115. package/dist/mcp-run.d.ts.map +1 -1
  116. package/dist/mcp-run.js +3 -46
  117. package/dist/mcp-run.js.map +1 -1
  118. package/dist/mcp-ws.d.ts.map +1 -1
  119. package/dist/mcp-ws.js +2 -18
  120. package/dist/mcp-ws.js.map +1 -1
  121. package/dist/migrate-layout.d.ts.map +1 -1
  122. package/dist/migrate-layout.js +36 -57
  123. package/dist/migrate-layout.js.map +1 -1
  124. package/dist/output-schemas.d.ts.map +1 -1
  125. package/dist/output-schemas.js +7 -7
  126. package/dist/output-schemas.js.map +1 -1
  127. package/dist/port.d.ts.map +1 -1
  128. package/dist/port.js +1 -2
  129. package/dist/port.js.map +1 -1
  130. package/dist/ppr-read-fix.js +2 -2
  131. package/dist/ppr-read-fix.js.map +1 -1
  132. package/dist/recipes/5.9.0.d.ts +10 -0
  133. package/dist/recipes/5.9.0.d.ts.map +1 -0
  134. package/dist/recipes/5.9.0.js +41 -0
  135. package/dist/recipes/5.9.0.js.map +1 -0
  136. package/dist/recipes/index.d.ts +5 -0
  137. package/dist/recipes/index.d.ts.map +1 -1
  138. package/dist/recipes/index.js +2 -0
  139. package/dist/recipes/index.js.map +1 -1
  140. package/dist/review.d.ts.map +1 -1
  141. package/dist/review.js +9 -13
  142. package/dist/review.js.map +1 -1
  143. package/dist/rules/contract-visibility.d.ts.map +1 -1
  144. package/dist/rules/contract-visibility.js +3 -9
  145. package/dist/rules/contract-visibility.js.map +1 -1
  146. package/dist/rules/data-guard.d.ts.map +1 -1
  147. package/dist/rules/data-guard.js +3 -10
  148. package/dist/rules/data-guard.js.map +1 -1
  149. package/dist/rules/pages.d.ts.map +1 -1
  150. package/dist/rules/pages.js +41 -58
  151. package/dist/rules/pages.js.map +1 -1
  152. package/dist/rules/routes.d.ts.map +1 -1
  153. package/dist/rules/routes.js +20 -27
  154. package/dist/rules/routes.js.map +1 -1
  155. package/dist/rules/security.d.ts.map +1 -1
  156. package/dist/rules/security.js +206 -313
  157. package/dist/rules/security.js.map +1 -1
  158. package/dist/rules/zones.d.ts.map +1 -1
  159. package/dist/rules/zones.js +3 -10
  160. package/dist/rules/zones.js.map +1 -1
  161. package/dist/runtime-differences.d.ts +62 -0
  162. package/dist/runtime-differences.d.ts.map +1 -0
  163. package/dist/runtime-differences.js +222 -0
  164. package/dist/runtime-differences.js.map +1 -0
  165. package/dist/sdk.d.ts.map +1 -1
  166. package/dist/sdk.js +43 -43
  167. package/dist/sdk.js.map +1 -1
  168. package/dist/smoke.d.ts.map +1 -1
  169. package/dist/smoke.js +4 -12
  170. package/dist/smoke.js.map +1 -1
  171. package/dist/source-rewrites.d.ts +27 -0
  172. package/dist/source-rewrites.d.ts.map +1 -0
  173. package/dist/source-rewrites.js +402 -0
  174. package/dist/source-rewrites.js.map +1 -0
  175. package/dist/stylex-migrate.d.ts.map +1 -1
  176. package/dist/stylex-migrate.js +1 -1
  177. package/dist/stylex-migrate.js.map +1 -1
  178. package/dist/symptom-search.d.ts +5 -0
  179. package/dist/symptom-search.d.ts.map +1 -0
  180. package/dist/symptom-search.js +21 -0
  181. package/dist/symptom-search.js.map +1 -0
  182. package/dist/upgrade.d.ts +28 -3
  183. package/dist/upgrade.d.ts.map +1 -1
  184. package/dist/upgrade.js +153 -64
  185. package/dist/upgrade.js.map +1 -1
  186. package/dist/verification-plan.d.ts.map +1 -1
  187. package/dist/verification-plan.js +5 -3
  188. package/dist/verification-plan.js.map +1 -1
  189. package/docs/examples.json +32 -2
  190. package/docs/llms-full.txt +749 -95
  191. package/docs/types.json +300 -61
  192. package/package.json +11 -11
  193. package/src/agent-files.ts +4 -1
  194. package/src/assure-hydration.ts +2 -9
  195. package/src/assure.ts +3 -4
  196. package/src/check-scan.ts +65 -82
  197. package/src/check.ts +25 -23
  198. package/src/cli.ts +61 -13
  199. package/src/command-catalog.ts +22 -32
  200. package/src/config-target.ts +1 -1
  201. package/src/db-child.ts +3 -10
  202. package/src/db-config.ts +4 -0
  203. package/src/doctor.ts +128 -12
  204. package/src/frontend-guidance.ts +344 -25
  205. package/src/hand-built-manifest.ts +1 -2
  206. package/src/internal/control-character.ts +8 -0
  207. package/src/internal/escape-regexp.ts +4 -0
  208. package/src/internal/line-at.ts +4 -0
  209. package/src/internal/nifra-project.ts +39 -35
  210. package/src/internal/posix-path.ts +4 -0
  211. package/src/internal/project-specifiers.ts +3 -10
  212. package/src/internal/realpath-or-self.ts +10 -0
  213. package/src/internal/record.ts +4 -0
  214. package/src/internal/source-facts.ts +39 -29
  215. package/src/introspect.ts +16 -156
  216. package/src/levels-tool.ts +71 -78
  217. package/src/load.ts +6 -1
  218. package/src/local-cli.ts +12 -0
  219. package/src/mcp-docs-tools.ts +51 -3
  220. package/src/mcp-http.ts +2 -2
  221. package/src/mcp-io.ts +77 -0
  222. package/src/mcp-isolate.ts +2 -3
  223. package/src/mcp-project-child.ts +5 -14
  224. package/src/mcp-render.ts +11 -59
  225. package/src/mcp-root.ts +1 -1
  226. package/src/mcp-run.ts +5 -54
  227. package/src/mcp-ws.ts +2 -19
  228. package/src/migrate-layout.ts +52 -72
  229. package/src/output-schemas.ts +7 -8
  230. package/src/port.ts +1 -4
  231. package/src/ppr-read-fix.ts +2 -2
  232. package/src/recipes/5.9.0.ts +42 -0
  233. package/src/recipes/index.ts +7 -0
  234. package/src/review.ts +12 -15
  235. package/src/rules/contract-visibility.ts +3 -8
  236. package/src/rules/data-guard.ts +3 -10
  237. package/src/rules/pages.ts +47 -58
  238. package/src/rules/routes.ts +21 -27
  239. package/src/rules/security.ts +224 -255
  240. package/src/rules/zones.ts +3 -10
  241. package/src/runtime-differences.ts +302 -0
  242. package/src/sdk.ts +55 -58
  243. package/src/smoke.ts +4 -13
  244. package/src/source-rewrites.ts +463 -0
  245. package/src/stylex-migrate.ts +1 -3
  246. package/src/symptom-search.ts +27 -0
  247. package/src/upgrade.ts +194 -74
  248. package/src/verification-plan.ts +10 -4
@@ -0,0 +1,463 @@
1
+ /**
2
+ * Source rewrites an upgrade recipe names: the edits an import move cannot express, made on the
3
+ * syntax tree with the project's own TypeScript. A rewrite changes only a shape it recognizes whole
4
+ * and lists every other site for the user to edit by hand; it never guesses.
5
+ */
6
+ import type * as TS from "typescript"
7
+ import { codeUnitOrder } from "./internal/code-unit-order.ts"
8
+ import { scriptKindOf } from "./internal/script-kind.ts"
9
+
10
+ /** A site a rewrite found and left alone, with what to write instead. */
11
+ export interface ManualSite {
12
+ readonly line: number
13
+ readonly reason: string
14
+ }
15
+
16
+ export interface RewriteResult {
17
+ readonly text: string
18
+ /** Sites rewritten. */
19
+ readonly count: number
20
+ readonly manual: readonly ManualSite[]
21
+ }
22
+
23
+ export interface SourceRewrite {
24
+ readonly id: string
25
+ /** What it rewrites, for the plan. */
26
+ readonly description: string
27
+ /** Whether `text` may hold a site: a file it refuses is never parsed. */
28
+ readonly mentions: (text: string) => boolean
29
+ readonly rewrite: (ts: typeof TS, file: string, text: string) => RewriteResult
30
+ }
31
+
32
+ interface Edit {
33
+ readonly start: number
34
+ readonly end: number
35
+ readonly text: string
36
+ }
37
+
38
+ /** Apply `edits` (disjoint ranges) to `text`, from the end so earlier offsets stay valid. */
39
+ function applyEdits(text: string, edits: readonly Edit[]): string {
40
+ let out = text
41
+ for (const edit of edits.toSorted((a, b) => b.start - a.start)) {
42
+ out = `${out.slice(0, edit.start)}${edit.text}${out.slice(edit.end)}`
43
+ }
44
+ return out
45
+ }
46
+
47
+ const WEB = "@nifrajs/web"
48
+ const NODE = "@nifrajs/node"
49
+ const DENO = "@nifrajs/deno"
50
+ const CORE_STATIC = "@nifrajs/core/serve-static"
51
+ const DAY = '"public, max-age=86400"'
52
+ const YEAR = "public, max-age=31536000, immutable"
53
+ const PUBLIC_DIR_KEYS = new Set(["dir", "hashedPrefix", "hashedFiles", "cache", "files"])
54
+ const PUBLIC_DIR_FORM =
55
+ 'serveStatic({ dir, prefix: "/", fallthrough: true, hashedFiles: (path) => path.startsWith("/assets/"), cacheControl: ONE_DAY })'
56
+
57
+ type Runtime = "core" | "node" | "deno"
58
+
59
+ /** The named imports of one module: the declaration, and each imported name's local binding. */
60
+ interface ModuleImport {
61
+ readonly declaration: TS.ImportDeclaration
62
+ readonly named: TS.NamedImports | undefined
63
+ readonly locals: Map<string, string>
64
+ }
65
+
66
+ function propertyName(ts: typeof TS, name: TS.PropertyName): string | undefined {
67
+ return ts.isIdentifier(name) || ts.isStringLiteral(name) ? name.text : undefined
68
+ }
69
+
70
+ /**
71
+ * 5.9: `servePublicDir` (removed from `@nifrajs/web`) becomes the runtime's `serveStatic` with the
72
+ * options that keep its answers; `@nifrajs/node`'s `static` mount objects are wrapped in
73
+ * `serveStatic()`; and `signals` on `@nifrajs/node`'s and `@nifrajs/deno`'s `serve()` becomes
74
+ * `gracefulSignals`.
75
+ */
76
+ function rewriteServeStatic(ts: typeof TS, file: string, text: string): RewriteResult {
77
+ const tree = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true, scriptKindOf(ts, file))
78
+ const edits: Edit[] = []
79
+ const manual: ManualSite[] = []
80
+ let count = 0
81
+ const lineOf = (node: TS.Node): number =>
82
+ tree.getLineAndCharacterOfPosition(node.getStart(tree)).line + 1
83
+ const source = (node: TS.Node): string => node.getText(tree)
84
+ const imports = new Map<string, ModuleImport>()
85
+ // Every import of @nifrajs/web, with the specifiers of names 5.9 moved out of it.
86
+ const webImports: {
87
+ readonly declaration: TS.ImportDeclaration
88
+ readonly named: TS.NamedImports
89
+ }[] = []
90
+ const publicDirSpecifiers = new Set<TS.ImportSpecifier>()
91
+ const hashedFilesSpecifiers = new Set<TS.ImportSpecifier>()
92
+ // A servePublicDir site left for the user keeps its import.
93
+ let publicDirLeft = false
94
+ let publicDir: string | undefined
95
+ let hashedFilesLocal: string | undefined
96
+
97
+ for (const statement of tree.statements) {
98
+ if (ts.isExportDeclaration(statement)) {
99
+ const from = statement.moduleSpecifier
100
+ if (from !== undefined && ts.isStringLiteral(from) && from.text === WEB) {
101
+ const names = source(statement)
102
+ if (/\b(?:servePublicDir|ServePublicDirOptions|PublicDirCache|HashedFiles)\b/.test(names)) {
103
+ manual.push({
104
+ line: lineOf(statement),
105
+ reason: `re-exports from ${WEB} a name 5.9 removed: re-export serveStatic, ServeStaticOptions or HashedFiles from ${CORE_STATIC}`,
106
+ })
107
+ }
108
+ }
109
+ continue
110
+ }
111
+ if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier))
112
+ continue
113
+ const from = statement.moduleSpecifier.text
114
+ const clause = statement.importClause
115
+ const bindings = clause?.namedBindings
116
+ const named = bindings !== undefined && ts.isNamedImports(bindings) ? bindings : undefined
117
+ const locals = new Map<string, string>()
118
+ for (const element of named?.elements ?? []) {
119
+ locals.set((element.propertyName ?? element.name).text, element.name.text)
120
+ }
121
+ if (!imports.has(from)) imports.set(from, { declaration: statement, named, locals })
122
+ if (from !== WEB) continue
123
+ if (named !== undefined) webImports.push({ declaration: statement, named })
124
+ if (
125
+ bindings !== undefined &&
126
+ ts.isNamespaceImport(bindings) &&
127
+ text.includes("servePublicDir")
128
+ ) {
129
+ publicDirLeft = true
130
+ manual.push({
131
+ line: lineOf(statement),
132
+ reason: `servePublicDir through a namespace import of ${WEB}: call ${PUBLIC_DIR_FORM} from ${CORE_STATIC}`,
133
+ })
134
+ }
135
+ for (const element of named?.elements ?? []) {
136
+ const imported = (element.propertyName ?? element.name).text
137
+ if (imported === "servePublicDir") {
138
+ publicDir = element.name.text
139
+ publicDirSpecifiers.add(element)
140
+ } else if (imported === "HashedFiles") {
141
+ hashedFilesLocal = element.name.text
142
+ hashedFilesSpecifiers.add(element)
143
+ } else if (imported === "ServePublicDirOptions" || imported === "PublicDirCache") {
144
+ manual.push({
145
+ line: lineOf(element),
146
+ reason: `${imported} is removed: type the options as ServeStaticOptions from ${CORE_STATIC}, whose fields differ (prefix, fallthrough, hashedFiles, cacheControl)`,
147
+ })
148
+ }
149
+ }
150
+ }
151
+
152
+ const nodeServe = new Set(
153
+ [...(imports.get(NODE)?.locals ?? [])].filter(([name]) => name === "serve").map(([, l]) => l),
154
+ )
155
+ const denoServe = new Set(
156
+ [...(imports.get(DENO)?.locals ?? [])].filter(([name]) => name === "serve").map(([, l]) => l),
157
+ )
158
+ // The file's runtime: a module that serves on Node or Deno gets that adapter's reader.
159
+ const runtime: Runtime = imports.has(NODE) ? "node" : imports.has(DENO) ? "deno" : "core"
160
+ const runtimeModule = runtime === "node" ? NODE : runtime === "deno" ? DENO : CORE_STATIC
161
+ const bound = new Set<string>()
162
+ const collect = (node: TS.Node): void => {
163
+ if (ts.isIdentifier(node)) bound.add(node.text)
164
+ ts.forEachChild(node, collect)
165
+ }
166
+ collect(tree)
167
+ // The local name of the runtime's serveStatic, or `undefined` when the name is taken by another binding.
168
+ const existingStatic = imports.get(runtimeModule)?.locals.get("serveStatic")
169
+ const staticName =
170
+ existingStatic ?? (bound.has("serveStatic") ? undefined : ("serveStatic" as const))
171
+ const coreDay = runtime === "core" && !bound.has("ONE_DAY")
172
+ let staticUsed = false
173
+ let dayUsed = false
174
+
175
+ const publicDirCall = (call: TS.CallExpression): void => {
176
+ const [options, ...rest] = call.arguments
177
+ const refuse = (why: string): void => {
178
+ publicDirLeft = true
179
+ manual.push({ line: lineOf(call), reason: `${why}: call ${PUBLIC_DIR_FORM}` })
180
+ }
181
+ if (staticName === undefined) {
182
+ refuse("`serveStatic` already names something else here")
183
+ return
184
+ }
185
+ if (options === undefined || rest.length > 0 || !ts.isObjectLiteralExpression(options)) {
186
+ refuse("servePublicDir with options built elsewhere")
187
+ return
188
+ }
189
+ const values = new Map<string, TS.Expression | undefined>()
190
+ for (const property of options.properties) {
191
+ const name =
192
+ ts.isPropertyAssignment(property) || ts.isShorthandPropertyAssignment(property)
193
+ ? propertyName(ts, property.name)
194
+ : undefined
195
+ if (name === undefined || !PUBLIC_DIR_KEYS.has(name)) {
196
+ refuse(`servePublicDir option \`${source(property)}\` has no direct counterpart`)
197
+ return
198
+ }
199
+ values.set(name, ts.isPropertyAssignment(property) ? property.initializer : undefined)
200
+ }
201
+ if (!values.has("dir")) {
202
+ refuse("servePublicDir without a `dir`")
203
+ return
204
+ }
205
+ const field = (name: string, as = name): string => {
206
+ const value = values.get(name)
207
+ return value === undefined
208
+ ? as === name
209
+ ? name
210
+ : `${as}: ${name}`
211
+ : `${as}: ${source(value)}`
212
+ }
213
+ let cacheControl: string | undefined
214
+ if (values.has("cache")) {
215
+ const cache = values.get("cache")
216
+ if (cache === undefined || !ts.isObjectLiteralExpression(cache)) {
217
+ refuse("servePublicDir `cache` built elsewhere")
218
+ return
219
+ }
220
+ for (const property of cache.properties) {
221
+ const name = ts.isPropertyAssignment(property) ? propertyName(ts, property.name) : undefined
222
+ if (name === "assets" && ts.isPropertyAssignment(property)) {
223
+ cacheControl = source(property.initializer)
224
+ } else if (
225
+ name === "hashed" &&
226
+ ts.isPropertyAssignment(property) &&
227
+ ts.isStringLiteralLike(property.initializer) &&
228
+ property.initializer.text === YEAR
229
+ ) {
230
+ // serveStatic's own cache-control for a content-hashed file.
231
+ } else {
232
+ refuse(
233
+ `servePublicDir \`cache.${name ?? "?"}\` has no counterpart: a content-hashed file is always sent \`${YEAR}\``,
234
+ )
235
+ return
236
+ }
237
+ }
238
+ }
239
+ if (cacheControl === undefined) {
240
+ if (coreDay) dayUsed = true
241
+ cacheControl = coreDay ? "ONE_DAY" : DAY
242
+ }
243
+ const prefixValue = values.get("hashedPrefix")
244
+ // A shorthand `hashedPrefix` is a binding of that name.
245
+ const hashedPrefix = !values.has("hashedPrefix")
246
+ ? '"/assets/"'
247
+ : prefixValue === undefined
248
+ ? "hashedPrefix"
249
+ : source(prefixValue)
250
+ const hashed = values.has("hashedFiles")
251
+ ? field("hashedFiles")
252
+ : `hashedFiles: (path) => path.startsWith(${hashedPrefix})`
253
+ const fields = [
254
+ field("dir"),
255
+ 'prefix: "/"',
256
+ // A miss is the app's: servePublicDir answered `undefined` for every path it could not serve.
257
+ "fallthrough: true",
258
+ hashed,
259
+ `cacheControl: ${cacheControl}`,
260
+ ...(values.has("files") ? [field("files")] : []),
261
+ ]
262
+ const literal = source(options)
263
+ let body: string
264
+ if (literal.includes("\n")) {
265
+ const first = options.properties[0]
266
+ const lineStart = (at: number): number => text.lastIndexOf("\n", at - 1) + 1
267
+ const indent =
268
+ first === undefined
269
+ ? " "
270
+ : text.slice(lineStart(first.getStart(tree)), first.getStart(tree))
271
+ const close =
272
+ text.slice(lineStart(options.getStart(tree)), options.getStart(tree)).match(/^\s*/)?.[0] ??
273
+ ""
274
+ body = `{\n${fields.map((f) => `${indent}${f},`).join("\n")}\n${close}}`
275
+ } else body = `{ ${fields.join(", ")} }`
276
+ edits.push({ start: call.getStart(tree), end: call.end, text: `${staticName}(${body})` })
277
+ staticUsed = true
278
+ count++
279
+ }
280
+
281
+ const serveCall = (call: TS.CallExpression, node: boolean): void => {
282
+ const options = call.arguments[1]
283
+ if (options === undefined) return
284
+ if (!ts.isObjectLiteralExpression(options)) {
285
+ if (/\bsignals\b/.test(text) || (node && /\bstatic\s*:/.test(text))) {
286
+ manual.push({
287
+ line: lineOf(options),
288
+ reason: `serve() options built elsewhere: rename \`signals\` to \`gracefulSignals\`${node ? ", and pass `static` a function, serveStatic(mounts) for mount objects" : ""}`,
289
+ })
290
+ }
291
+ return
292
+ }
293
+ for (const property of options.properties) {
294
+ if (ts.isSpreadAssignment(property)) {
295
+ if (/\bsignals\b/.test(source(property.expression)) || /\bsignals\b/.test(text)) {
296
+ manual.push({
297
+ line: lineOf(property),
298
+ reason:
299
+ "serve() options spread in: rename `signals` to `gracefulSignals` where they are built",
300
+ })
301
+ }
302
+ continue
303
+ }
304
+ const name = propertyName(ts, property.name)
305
+ if (name === "signals") {
306
+ if (ts.isShorthandPropertyAssignment(property)) {
307
+ edits.push({
308
+ start: property.getStart(tree),
309
+ end: property.end,
310
+ text: "gracefulSignals: signals",
311
+ })
312
+ } else {
313
+ edits.push({
314
+ start: property.name.getStart(tree),
315
+ end: property.name.end,
316
+ text: "gracefulSignals",
317
+ })
318
+ }
319
+ count++
320
+ } else if (node && name === "static" && ts.isPropertyAssignment(property)) {
321
+ const value = property.initializer
322
+ if (ts.isObjectLiteralExpression(value) || ts.isArrayLiteralExpression(value)) {
323
+ if (staticName === undefined) {
324
+ manual.push({
325
+ line: lineOf(value),
326
+ reason:
327
+ "`static` takes a function now: wrap the mounts in serveStatic(), which another binding here is named",
328
+ })
329
+ continue
330
+ }
331
+ edits.push({
332
+ start: value.getStart(tree),
333
+ end: value.end,
334
+ text: `${staticName}(${source(value)})`,
335
+ })
336
+ staticUsed = true
337
+ count++
338
+ } else if (
339
+ !ts.isCallExpression(value) &&
340
+ !ts.isArrowFunction(value) &&
341
+ !ts.isFunctionExpression(value)
342
+ ) {
343
+ manual.push({
344
+ line: lineOf(value),
345
+ reason:
346
+ "`static` takes a function now: pass serveStatic(mounts) if this holds mount objects",
347
+ })
348
+ }
349
+ }
350
+ }
351
+ }
352
+
353
+ const visit = (node: TS.Node): void => {
354
+ if (ts.isCallExpression(node) && ts.isIdentifier(node.expression)) {
355
+ const callee = node.expression.text
356
+ if (callee === publicDir) {
357
+ publicDirCall(node)
358
+ return
359
+ }
360
+ if (nodeServe.has(callee)) serveCall(node, true)
361
+ else if (denoServe.has(callee)) serveCall(node, false)
362
+ } else if (ts.isIdentifier(node) && node.text === "servePublicDir" && publicDir === undefined) {
363
+ // Reached some way other than a static import (a dynamic one, a require): never left unsaid.
364
+ if (!manual.some(({ line }) => line === lineOf(node))) {
365
+ manual.push({
366
+ line: lineOf(node),
367
+ reason: `servePublicDir is removed from ${WEB}: use ${PUBLIC_DIR_FORM} from ${CORE_STATIC}`,
368
+ })
369
+ }
370
+ } else if (
371
+ ts.isIdentifier(node) &&
372
+ node.text === publicDir &&
373
+ !ts.isImportSpecifier(node.parent) &&
374
+ !(ts.isCallExpression(node.parent) && node.parent.expression === node)
375
+ ) {
376
+ publicDirLeft = true
377
+ manual.push({
378
+ line: lineOf(node),
379
+ reason: `servePublicDir used as a value: use ${PUBLIC_DIR_FORM}`,
380
+ })
381
+ }
382
+ ts.forEachChild(node, visit)
383
+ }
384
+ visit(tree)
385
+ const removed = new Set([...hashedFilesSpecifiers, ...(publicDirLeft ? [] : publicDirSpecifiers)])
386
+ if (count === 0 && removed.size === 0) return { text, count: 0, manual }
387
+
388
+ // The web import loses the removed names; the runtime's serveStatic (and ONE_DAY) are imported.
389
+ const names: string[] = []
390
+ if (staticUsed && existingStatic === undefined) names.push("serveStatic")
391
+ if (dayUsed) names.push("ONE_DAY")
392
+ if (hashedFilesLocal !== undefined) {
393
+ names.push(
394
+ hashedFilesLocal === "HashedFiles"
395
+ ? "type HashedFiles"
396
+ : `type HashedFiles as ${hashedFilesLocal}`,
397
+ )
398
+ }
399
+ const quote = /from\s*'/.test(text) ? "'" : '"'
400
+ const specifierOrder = (a: string, b: string): number =>
401
+ codeUnitOrder(a.replace(/^type /, ""), b.replace(/^type /, ""))
402
+ const target = imports.get(runtimeModule)
403
+ let added: string | undefined
404
+ if (names.length > 0) {
405
+ if (target?.named !== undefined) {
406
+ const all = [...target.named.elements.map(source), ...names].toSorted(specifierOrder)
407
+ edits.push({
408
+ start: target.named.getStart(tree),
409
+ end: target.named.end,
410
+ text: `{ ${all.join(", ")} }`,
411
+ })
412
+ } else {
413
+ added = `import { ${names.toSorted(specifierOrder).join(", ")} } from ${quote}${runtimeModule}${quote}`
414
+ }
415
+ }
416
+ // The new import goes where the first web import that loses a name was, or after the imports.
417
+ let placed = added === undefined
418
+ for (const { declaration, named } of webImports) {
419
+ if (!named.elements.some((element) => removed.has(element))) continue
420
+ const kept = named.elements.filter((element) => !removed.has(element)).map(source)
421
+ const place = placed ? undefined : added
422
+ placed = true
423
+ if (kept.length === 0 && declaration.importClause?.name === undefined) {
424
+ // The declaration goes, with its line ending, unless the new import takes its place.
425
+ const eol = text.startsWith("\r\n", declaration.end)
426
+ ? 2
427
+ : text[declaration.end] === "\n"
428
+ ? 1
429
+ : 0
430
+ edits.push({
431
+ start: declaration.getStart(tree),
432
+ end: place === undefined ? declaration.end + eol : declaration.end,
433
+ text: place ?? "",
434
+ })
435
+ } else {
436
+ edits.push({ start: named.getStart(tree), end: named.end, text: `{ ${kept.join(", ")} }` })
437
+ if (place !== undefined)
438
+ edits.push({ start: declaration.end, end: declaration.end, text: `\n${place}` })
439
+ }
440
+ }
441
+ if (!placed && added !== undefined) {
442
+ const last = tree.statements.filter(ts.isImportDeclaration).at(-1)
443
+ const at = last?.end ?? 0
444
+ edits.push({ start: at, end: at, text: last === undefined ? `${added}\n` : `\n${added}` })
445
+ }
446
+ return { text: applyEdits(text, edits), count, manual }
447
+ }
448
+
449
+ const SERVE_STATIC: SourceRewrite = {
450
+ id: "serve-static",
451
+ description:
452
+ "servePublicDir → serveStatic, @nifrajs/node `static` mounts → serveStatic(), `signals` → `gracefulSignals`",
453
+ mentions: (text) =>
454
+ text.includes("servePublicDir") ||
455
+ (/@nifrajs\/(?:node|deno)["']/.test(text) && /\bsignals\b|\bstatic\s*:/.test(text)),
456
+ rewrite: rewriteServeStatic,
457
+ }
458
+
459
+ const REWRITES: Readonly<Record<string, SourceRewrite>> = { [SERVE_STATIC.id]: SERVE_STATIC }
460
+
461
+ export function getSourceRewrite(id: string): SourceRewrite | undefined {
462
+ return REWRITES[id]
463
+ }
@@ -7,6 +7,7 @@
7
7
  */
8
8
  import { readdir, readFile, writeFile } from "node:fs/promises"
9
9
  import { relative, resolve } from "node:path"
10
+ import { isRecord } from "./internal/record.ts"
10
11
 
11
12
  export interface MigrationIssue {
12
13
  readonly file: string
@@ -596,9 +597,6 @@ const PSEUDO_VARIANTS: Readonly<Record<string, string>> = {
596
597
  const CLASS_ATTRIBUTE =
597
598
  /\bclassName\s*=\s*(?:"([^"]*)"|'([^']*)'|\{\s*(?:"([^"]*)"|'([^']*)')\s*\}|\{([\s\S]*?)\})/g
598
599
 
599
- const isRecord = (value: unknown): value is Record<string, unknown> =>
600
- typeof value === "object" && value !== null
601
-
602
600
  const cloneStyle = (style: StyleObject): StyleObject => {
603
601
  const result: Record<string, string | number | StyleObject> = {}
604
602
  for (const [key, value] of Object.entries(style))
@@ -0,0 +1,27 @@
1
+ /** Longest symptom text scored; the MCP schemas advertise the same cap, this enforces it. */
2
+ const MAX_SYMPTOM_LENGTH = 256
3
+
4
+ /** The trimmed, capped query, or undefined when there is nothing to rank by. */
5
+ export function symptomQuery(symptom: string | undefined): string | undefined {
6
+ const query = symptom?.trim().slice(0, MAX_SYMPTOM_LENGTH)
7
+ return query === undefined || query.length === 0 ? undefined : query
8
+ }
9
+
10
+ /** The best-scoring entries for a query, highest first, at most `limit` (1 to 5, default 3). */
11
+ export function rankBySymptom<T>(
12
+ entries: readonly T[],
13
+ query: string,
14
+ limit: number | undefined,
15
+ score: (entry: T, words: readonly string[]) => number,
16
+ ): T[] {
17
+ const words = query
18
+ .toLowerCase()
19
+ .split(/\s+/)
20
+ .filter((w) => w.length > 1)
21
+ return entries
22
+ .map((entry) => ({ entry, s: score(entry, words) }))
23
+ .filter((r) => r.s > 0)
24
+ .sort((a, b) => b.s - a.s)
25
+ .slice(0, Math.min(Math.max(limit ?? 3, 1), 5))
26
+ .map((r) => r.entry)
27
+ }