@nifrajs/cli 5.5.1 → 5.7.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 +314 -0
- package/dist/cdn-check.d.ts +7 -6
- package/dist/cdn-check.d.ts.map +1 -1
- package/dist/cdn-check.js +13 -1
- package/dist/cdn-check.js.map +1 -1
- package/dist/check-diagnostics.d.ts.map +1 -1
- package/dist/check-diagnostics.js +9 -3
- package/dist/check-diagnostics.js.map +1 -1
- package/dist/check-scan.d.ts +6 -0
- package/dist/check-scan.d.ts.map +1 -1
- package/dist/check-scan.js +30 -32
- package/dist/check-scan.js.map +1 -1
- package/dist/check.d.ts.map +1 -1
- package/dist/check.js +35 -12
- package/dist/check.js.map +1 -1
- package/dist/cli.d.ts +4 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +66 -9
- package/dist/cli.js.map +1 -1
- package/dist/command-catalog.d.ts +19 -1
- package/dist/command-catalog.d.ts.map +1 -1
- package/dist/command-catalog.js +111 -6
- package/dist/command-catalog.js.map +1 -1
- package/dist/contracts.d.ts +42 -4
- package/dist/contracts.d.ts.map +1 -1
- package/dist/contracts.js +288 -9
- package/dist/contracts.js.map +1 -1
- package/dist/doctor.d.ts +12 -2
- package/dist/doctor.d.ts.map +1 -1
- package/dist/doctor.js +67 -1
- package/dist/doctor.js.map +1 -1
- package/dist/fix-recipes.d.ts.map +1 -1
- package/dist/fix-recipes.js +27 -0
- package/dist/fix-recipes.js.map +1 -1
- package/dist/frontend-guidance.d.ts +1 -1
- package/dist/frontend-guidance.d.ts.map +1 -1
- package/dist/frontend-guidance.js +17 -8
- package/dist/frontend-guidance.js.map +1 -1
- package/dist/internal/git-ignored.d.ts +12 -0
- package/dist/internal/git-ignored.d.ts.map +1 -0
- package/dist/internal/git-ignored.js +49 -0
- package/dist/internal/git-ignored.js.map +1 -0
- package/dist/internal/source-facts.d.ts +29 -0
- package/dist/internal/source-facts.d.ts.map +1 -1
- package/dist/internal/source-facts.js +160 -0
- package/dist/internal/source-facts.js.map +1 -1
- package/dist/introspect.js +1 -1
- package/dist/introspect.js.map +1 -1
- package/dist/load.d.ts +7 -1
- package/dist/load.d.ts.map +1 -1
- package/dist/load.js +18 -0
- package/dist/load.js.map +1 -1
- package/dist/local-cli.d.ts +15 -0
- package/dist/local-cli.d.ts.map +1 -0
- package/dist/local-cli.js +74 -0
- package/dist/local-cli.js.map +1 -0
- package/dist/lock-links.d.ts +0 -7
- package/dist/lock-links.d.ts.map +1 -1
- package/dist/lock-links.js +5 -44
- package/dist/lock-links.js.map +1 -1
- package/dist/mcp-delegate.d.ts +13 -5
- package/dist/mcp-delegate.d.ts.map +1 -1
- package/dist/mcp-delegate.js +24 -8
- package/dist/mcp-delegate.js.map +1 -1
- package/dist/mcp-exec.d.ts.map +1 -1
- package/dist/mcp-exec.js +57 -3
- package/dist/mcp-exec.js.map +1 -1
- package/dist/mcp-http.d.ts +2 -1
- package/dist/mcp-http.d.ts.map +1 -1
- package/dist/mcp-http.js +3 -2
- package/dist/mcp-http.js.map +1 -1
- package/dist/mcp-tool-meta.d.ts +21 -0
- package/dist/mcp-tool-meta.d.ts.map +1 -0
- package/dist/mcp-tool-meta.js +246 -0
- package/dist/mcp-tool-meta.js.map +1 -0
- package/dist/mcp.d.ts +3 -2
- package/dist/mcp.d.ts.map +1 -1
- package/dist/mcp.js +36 -98
- package/dist/mcp.js.map +1 -1
- package/dist/openapi-types.d.ts.map +1 -1
- package/dist/openapi-types.js +45 -2
- package/dist/openapi-types.js.map +1 -1
- package/dist/output-schemas.d.ts +8 -0
- package/dist/output-schemas.d.ts.map +1 -1
- package/dist/output-schemas.js +14 -0
- package/dist/output-schemas.js.map +1 -1
- package/dist/ppr-read-fix.d.ts +8 -0
- package/dist/ppr-read-fix.d.ts.map +1 -0
- package/dist/ppr-read-fix.js +356 -0
- package/dist/ppr-read-fix.js.map +1 -0
- package/dist/recipes/5.6.0.d.ts +10 -0
- package/dist/recipes/5.6.0.d.ts.map +1 -0
- package/dist/recipes/5.6.0.js +68 -0
- package/dist/recipes/5.6.0.js.map +1 -0
- package/dist/recipes/5.7.0.d.ts +12 -0
- package/dist/recipes/5.7.0.d.ts.map +1 -0
- package/dist/recipes/5.7.0.js +70 -0
- package/dist/recipes/5.7.0.js.map +1 -0
- package/dist/recipes/index.d.ts +13 -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 +9 -2
- package/dist/review.js.map +1 -1
- package/dist/rules/codes.d.ts +10 -3
- package/dist/rules/codes.d.ts.map +1 -1
- package/dist/rules/codes.js +11 -4
- package/dist/rules/codes.js.map +1 -1
- package/dist/rules/explanations.d.ts.map +1 -1
- package/dist/rules/explanations.js +52 -16
- package/dist/rules/explanations.js.map +1 -1
- package/dist/rules/islands.d.ts +91 -0
- package/dist/rules/islands.d.ts.map +1 -1
- package/dist/rules/islands.js +757 -3
- package/dist/rules/islands.js.map +1 -1
- package/dist/rules/legacy.js +1 -1
- package/dist/rules/legacy.js.map +1 -1
- package/dist/rules/ppr.d.ts +16 -0
- package/dist/rules/ppr.d.ts.map +1 -0
- package/dist/rules/ppr.js +114 -0
- package/dist/rules/ppr.js.map +1 -0
- package/dist/rules/swap.d.ts +7 -0
- package/dist/rules/swap.d.ts.map +1 -0
- package/dist/rules/swap.js +338 -0
- package/dist/rules/swap.js.map +1 -0
- package/dist/rules/text-slot-boolean.d.ts +8 -0
- package/dist/rules/text-slot-boolean.d.ts.map +1 -0
- package/dist/rules/text-slot-boolean.js +57 -0
- package/dist/rules/text-slot-boolean.js.map +1 -0
- package/dist/rules/undeclared-response.d.ts +9 -0
- package/dist/rules/undeclared-response.d.ts.map +1 -0
- package/dist/rules/undeclared-response.js +93 -0
- package/dist/rules/undeclared-response.js.map +1 -0
- package/dist/scaffold-hole.d.ts +105 -0
- package/dist/scaffold-hole.d.ts.map +1 -0
- package/dist/scaffold-hole.js +688 -0
- package/dist/scaffold-hole.js.map +1 -0
- package/dist/scaffold.d.ts +5 -3
- package/dist/scaffold.d.ts.map +1 -1
- package/dist/scaffold.js +56 -46
- package/dist/scaffold.js.map +1 -1
- package/dist/upgrade.d.ts +34 -8
- package/dist/upgrade.d.ts.map +1 -1
- package/dist/upgrade.js +197 -25
- package/dist/upgrade.js.map +1 -1
- package/dist/verification-plan.d.ts.map +1 -1
- package/dist/verification-plan.js +12 -1
- package/dist/verification-plan.js.map +1 -1
- package/dist/version-drift.js +1 -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 +70 -10
- package/docs/llms-full.txt +3162 -1196
- package/docs/types.json +1473 -322
- package/package.json +11 -11
- package/src/cdn-check.ts +21 -7
- package/src/check-diagnostics.ts +9 -3
- package/src/check-scan.ts +35 -32
- package/src/check.ts +32 -10
- package/src/cli.ts +71 -9
- package/src/command-catalog.ts +149 -6
- package/src/contracts.ts +348 -14
- package/src/doctor.ts +62 -3
- package/src/fix-recipes.ts +28 -0
- package/src/frontend-guidance.ts +20 -8
- package/src/internal/git-ignored.ts +47 -0
- package/src/internal/source-facts.ts +198 -0
- package/src/introspect.ts +1 -1
- package/src/load.ts +29 -0
- package/src/local-cli.ts +83 -0
- package/src/lock-links.ts +5 -43
- package/src/mcp-delegate.ts +30 -9
- package/src/mcp-exec.ts +74 -3
- package/src/mcp-http.ts +3 -2
- package/src/mcp-tool-meta.ts +268 -0
- package/src/mcp.ts +35 -99
- package/src/openapi-types.ts +43 -2
- package/src/output-schemas.ts +28 -0
- package/src/ppr-read-fix.ts +426 -0
- package/src/recipes/5.6.0.ts +69 -0
- package/src/recipes/5.7.0.ts +72 -0
- package/src/recipes/index.ts +18 -0
- package/src/review.ts +9 -2
- package/src/rules/codes.ts +11 -4
- package/src/rules/explanations.ts +52 -16
- package/src/rules/islands.ts +875 -3
- package/src/rules/legacy.ts +1 -1
- package/src/rules/ppr.ts +143 -0
- package/src/rules/swap.ts +402 -0
- package/src/rules/text-slot-boolean.ts +56 -0
- package/src/rules/undeclared-response.ts +92 -0
- package/src/scaffold-hole.ts +880 -0
- package/src/scaffold.ts +60 -49
- package/src/upgrade.ts +229 -33
- package/src/verification-plan.ts +20 -1
- package/src/version-drift.ts +1 -1
- package/src/web-app-options.ts +2 -0
- package/dist/rules/nano.d.ts +0 -6
- package/dist/rules/nano.d.ts.map +0 -1
- package/dist/rules/nano.js +0 -273
- package/dist/rules/nano.js.map +0 -1
- package/src/rules/nano.ts +0 -307
package/src/mcp.ts
CHANGED
|
@@ -40,8 +40,9 @@
|
|
|
40
40
|
* handed to that CLI; when that is impossible the version-sensitive tools refuse rather than answer
|
|
41
41
|
* for the wrong release (see `./mcp-delegate.ts`). The protocol is hand-rolled (newline-delimited JSON-RPC 2.0 over stdio), including
|
|
42
42
|
* standard MCP progress notifications and request cancellation - no SDK dependency, the same
|
|
43
|
-
* minimal-surface choice as the rest of nifra. The pure dispatch lives in `./mcp-protocol.ts
|
|
44
|
-
* module is the
|
|
43
|
+
* minimal-surface choice as the rest of nifra. The pure dispatch lives in `./mcp-protocol.ts` and the
|
|
44
|
+
* stdio framing in `@nifrajs/mcp/stdio`; this module is the dispatch shell (roots, tool wiring, the run
|
|
45
|
+
* subprocess).
|
|
45
46
|
*
|
|
46
47
|
* This process runs no project code and holds none of the project's `.env`: the config, the backend
|
|
47
48
|
* and everything they import run in a fresh subprocess per call (`./mcp-isolate.ts`), and a session
|
|
@@ -50,6 +51,7 @@
|
|
|
50
51
|
|
|
51
52
|
import { stat } from "node:fs/promises"
|
|
52
53
|
import { resolve } from "node:path"
|
|
54
|
+
import { runStdioTransport, type StdioConnection } from "@nifrajs/mcp/stdio"
|
|
53
55
|
import { loadDocsCorpus } from "./docs-search.ts"
|
|
54
56
|
import { loadExamplesCorpus } from "./examples.ts"
|
|
55
57
|
import type { AppLoader } from "./load.ts"
|
|
@@ -59,9 +61,6 @@ import { docsTools } from "./mcp-docs-tools.ts"
|
|
|
59
61
|
import {
|
|
60
62
|
createMcpProtocolState,
|
|
61
63
|
handleRpc,
|
|
62
|
-
isJsonRpcRequest,
|
|
63
|
-
isJsonRpcResponse,
|
|
64
|
-
type JsonRpcNotification,
|
|
65
64
|
type JsonRpcRequest,
|
|
66
65
|
type JsonRpcResponse,
|
|
67
66
|
type McpPrompt,
|
|
@@ -188,8 +187,6 @@ const ANSWERED_WITHOUT_THE_APP: ReadonlySet<string> = new Set([
|
|
|
188
187
|
"notifications/initialized",
|
|
189
188
|
"notifications/roots/list_changed",
|
|
190
189
|
])
|
|
191
|
-
const MAX_STDIO_MESSAGE_BYTES = 8 * 1024 * 1024
|
|
192
|
-
const STDIO_ENCODER = new TextEncoder()
|
|
193
190
|
|
|
194
191
|
interface ProjectApp {
|
|
195
192
|
readonly name: string
|
|
@@ -268,29 +265,12 @@ export async function runMcpServer(
|
|
|
268
265
|
let drift: ToolingDrift | undefined = await detectToolingDrift(rootState.root, version)
|
|
269
266
|
const serverInfo = { name: "nifra", version }
|
|
270
267
|
const state = createMcpProtocolState()
|
|
271
|
-
const send = (message: JsonRpcResponse | JsonRpcNotification): void => {
|
|
272
|
-
let encoded: string
|
|
273
|
-
try {
|
|
274
|
-
encoded = JSON.stringify(message)
|
|
275
|
-
} catch {
|
|
276
|
-
encoded = JSON.stringify(
|
|
277
|
-
rpcError(responseIdOf(message), -32603, "MCP response is not serializable"),
|
|
278
|
-
)
|
|
279
|
-
}
|
|
280
|
-
if (STDIO_ENCODER.encode(encoded).byteLength > MAX_STDIO_MESSAGE_BYTES)
|
|
281
|
-
encoded = JSON.stringify(
|
|
282
|
-
rpcError(responseIdOf(message), -32603, "MCP response exceeded the size limit"),
|
|
283
|
-
)
|
|
284
|
-
process.stdout.write(`${encoded}\n`)
|
|
285
|
-
}
|
|
286
268
|
let rootsSupported = false
|
|
287
269
|
let rootsRequestPending = false
|
|
288
|
-
const requestRoots = (): void => {
|
|
270
|
+
const requestRoots = (connection: StdioConnection): void => {
|
|
289
271
|
if (!rootsSupported || rootsRequestPending) return
|
|
290
272
|
rootsRequestPending = true
|
|
291
|
-
|
|
292
|
-
`${JSON.stringify({ jsonrpc: "2.0", id: ROOTS_REQUEST_ID, method: "roots/list" })}\n`,
|
|
293
|
-
)
|
|
273
|
+
connection.send({ jsonrpc: "2.0", id: ROOTS_REQUEST_ID, method: "roots/list" })
|
|
294
274
|
}
|
|
295
275
|
const onRootsAnswer = async (message: JsonRpcResponse): Promise<void> => {
|
|
296
276
|
const result = "result" in message ? message.result : undefined
|
|
@@ -310,15 +290,20 @@ export async function runMcpServer(
|
|
|
310
290
|
// must see the adopted root, even though dispatches themselves run concurrently.
|
|
311
291
|
let rootsUpdate: Promise<void> = Promise.resolve()
|
|
312
292
|
|
|
313
|
-
const dispatchRootsAnswer = (message: JsonRpcResponse):
|
|
293
|
+
const dispatchRootsAnswer = (message: JsonRpcResponse): boolean => {
|
|
314
294
|
// The roots answer is a response to a request initiated by this server, not a client RPC request.
|
|
315
295
|
// Serialize adoption with normal dispatch so a following request cannot observe a half-updated root.
|
|
316
|
-
if (
|
|
296
|
+
if (message.id !== ROOTS_REQUEST_ID) return false
|
|
297
|
+
if (!rootsRequestPending) return true
|
|
317
298
|
rootsRequestPending = false
|
|
318
299
|
rootsUpdate = onRootsAnswer(message).catch(() => {})
|
|
300
|
+
return true
|
|
319
301
|
}
|
|
320
302
|
|
|
321
|
-
const dispatch = async (
|
|
303
|
+
const dispatch = async (
|
|
304
|
+
message: JsonRpcRequest,
|
|
305
|
+
connection: StdioConnection,
|
|
306
|
+
): Promise<JsonRpcResponse | null> => {
|
|
322
307
|
await rootsUpdate
|
|
323
308
|
if (message.method === "initialize") {
|
|
324
309
|
rootsSupported = clientSupportsRoots(message.params)
|
|
@@ -338,13 +323,12 @@ export async function runMcpServer(
|
|
|
338
323
|
prompts: projectPrompts(),
|
|
339
324
|
instructions: rootInstructions(rootState, drift),
|
|
340
325
|
},
|
|
341
|
-
{ state, sendNotification: send, exposeToolErrors: true },
|
|
326
|
+
{ state, sendNotification: connection.send, exposeToolErrors: true },
|
|
342
327
|
)
|
|
343
|
-
if (response) send(response)
|
|
344
328
|
// Ask for the client's workspace roots once the handshake completes, and again whenever the
|
|
345
|
-
// client says they changed. The answer comes back through
|
|
346
|
-
if (method.startsWith("notifications/")) requestRoots()
|
|
347
|
-
return
|
|
329
|
+
// client says they changed. The answer comes back through `dispatchRootsAnswer`.
|
|
330
|
+
if (method.startsWith("notifications/")) requestRoots(connection)
|
|
331
|
+
return response
|
|
348
332
|
}
|
|
349
333
|
const project = await ctx
|
|
350
334
|
let activeTools: McpTool[]
|
|
@@ -386,76 +370,28 @@ export async function runMcpServer(
|
|
|
386
370
|
...project.features,
|
|
387
371
|
instructions: rootInstructions(rootState, drift),
|
|
388
372
|
}
|
|
389
|
-
|
|
373
|
+
return handleRpc(message, activeTools, serverInfo, features, {
|
|
390
374
|
state,
|
|
391
|
-
sendNotification: send,
|
|
375
|
+
sendNotification: connection.send,
|
|
392
376
|
// The stdio caller is the developer's own agent: a failure it cannot read it cannot fix.
|
|
393
377
|
exposeToolErrors: true,
|
|
394
378
|
})
|
|
395
|
-
if (response) send(response)
|
|
396
379
|
}
|
|
397
380
|
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
send(rpcError(null, -32700, "parse error"))
|
|
414
|
-
return
|
|
415
|
-
}
|
|
416
|
-
if (isJsonRpcRequest(parsed)) {
|
|
417
|
-
void dispatch(parsed).catch((err) => {
|
|
418
|
-
const msg = err instanceof Error ? err.message : String(err)
|
|
419
|
-
send(rpcError(parsed.id ?? null, -32603, msg))
|
|
420
|
-
})
|
|
421
|
-
} else if (isJsonRpcResponse(parsed) && parsed.id === ROOTS_REQUEST_ID) {
|
|
422
|
-
dispatchRootsAnswer(parsed)
|
|
423
|
-
} else {
|
|
424
|
-
// Do not let null, arrays, scalars, responses for unknown ids, or malformed envelopes reach the
|
|
425
|
-
// dispatch shell. In particular, reading `.id` from a hostile `null` must never crash the loop.
|
|
426
|
-
send(rpcError(null, -32600, "Invalid Request"))
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
for await (const chunk of Bun.stdin.stream()) {
|
|
430
|
-
let text = decoder.decode(chunk as Uint8Array, { stream: true })
|
|
431
|
-
if (discardingOversized) {
|
|
432
|
-
const newline = text.indexOf("\n")
|
|
433
|
-
if (newline === -1) continue
|
|
434
|
-
text = text.slice(newline + 1)
|
|
435
|
-
discardingOversized = false
|
|
436
|
-
}
|
|
437
|
-
buffer += text
|
|
438
|
-
bufferBytes += STDIO_ENCODER.encode(text).byteLength
|
|
439
|
-
let nl = buffer.indexOf("\n")
|
|
440
|
-
while (nl !== -1) {
|
|
441
|
-
const line = buffer.slice(0, nl)
|
|
442
|
-
buffer = buffer.slice(nl + 1)
|
|
443
|
-
bufferBytes = Math.max(0, bufferBytes - STDIO_ENCODER.encode(line).byteLength - 1)
|
|
444
|
-
nl = buffer.indexOf("\n")
|
|
445
|
-
processLine(line)
|
|
446
|
-
}
|
|
447
|
-
if (bufferBytes > MAX_STDIO_MESSAGE_BYTES) {
|
|
448
|
-
buffer = ""
|
|
449
|
-
bufferBytes = 0
|
|
450
|
-
discardingOversized = true
|
|
451
|
-
send(rpcError(null, -32600, "MCP message exceeded the size limit"))
|
|
452
|
-
}
|
|
381
|
+
try {
|
|
382
|
+
await runStdioTransport({
|
|
383
|
+
onRequest: async (message, connection) => {
|
|
384
|
+
try {
|
|
385
|
+
return await dispatch(message, connection)
|
|
386
|
+
} catch (err) {
|
|
387
|
+
// Same audience as `exposeToolErrors`: a project that fails to load says why.
|
|
388
|
+
if (message.id === undefined) return null
|
|
389
|
+
return rpcError(message.id, -32603, err instanceof Error ? err.message : String(err))
|
|
390
|
+
}
|
|
391
|
+
},
|
|
392
|
+
onResponse: dispatchRootsAnswer,
|
|
393
|
+
})
|
|
394
|
+
} finally {
|
|
395
|
+
closeWarmWorkers()
|
|
453
396
|
}
|
|
454
|
-
closeWarmWorkers()
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
function responseIdOf(message: JsonRpcResponse | JsonRpcNotification): string | number | null {
|
|
458
|
-
if (!("id" in message)) return null
|
|
459
|
-
const id = message.id
|
|
460
|
-
return id === null || typeof id === "string" || typeof id === "number" ? id : null
|
|
461
397
|
}
|
package/src/openapi-types.ts
CHANGED
|
@@ -39,6 +39,43 @@ type TypeChecker = TSApi.TypeChecker
|
|
|
39
39
|
|
|
40
40
|
const hasFlag = (type: Type, flag: number): boolean => (type.flags & flag) !== 0
|
|
41
41
|
|
|
42
|
+
/** A `bigint` as a nifra JSON response writes it: its decimal string. */
|
|
43
|
+
const BIGINT_SCHEMA: JsonSchema = { type: "string", pattern: "^-?(0|[1-9][0-9]*)$" }
|
|
44
|
+
|
|
45
|
+
/** `JSON.stringify` writes a typed array as an object keyed by index. */
|
|
46
|
+
const NUMBER_ARRAYS: ReadonlySet<string> = new Set([
|
|
47
|
+
"Int8Array",
|
|
48
|
+
"Uint8Array",
|
|
49
|
+
"Uint8ClampedArray",
|
|
50
|
+
"Int16Array",
|
|
51
|
+
"Uint16Array",
|
|
52
|
+
"Int32Array",
|
|
53
|
+
"Uint32Array",
|
|
54
|
+
"Float32Array",
|
|
55
|
+
"Float64Array",
|
|
56
|
+
])
|
|
57
|
+
const NUMBER_ARRAY_SCHEMA: JsonSchema = { type: "object", additionalProperties: { type: "number" } }
|
|
58
|
+
/** A `BigInt64Array` or `BigUint64Array`: keyed by index too, each entry a `bigint`'s decimal string. */
|
|
59
|
+
const BIGINT_ARRAYS: ReadonlySet<string> = new Set(["BigInt64Array", "BigUint64Array"])
|
|
60
|
+
const BIGINT_ARRAY_SCHEMA: JsonSchema = { type: "object", additionalProperties: BIGINT_SCHEMA }
|
|
61
|
+
/** What a Node `Buffer`'s `toJSON` writes in place of the index object. */
|
|
62
|
+
const BUFFER_SCHEMA: JsonSchema = {
|
|
63
|
+
type: "object",
|
|
64
|
+
properties: {
|
|
65
|
+
type: { const: "Buffer" },
|
|
66
|
+
data: { type: "array", items: { type: "integer", minimum: 0, maximum: 255 } },
|
|
67
|
+
},
|
|
68
|
+
required: ["type", "data"],
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The schema of a typed array or `Buffer` named `name`, as `JSON.stringify` writes it. */
|
|
72
|
+
function binarySchema(name: string | undefined): JsonSchema | undefined {
|
|
73
|
+
if (name === undefined) return undefined
|
|
74
|
+
if (name === "Buffer") return BUFFER_SCHEMA
|
|
75
|
+
if (BIGINT_ARRAYS.has(name)) return BIGINT_ARRAY_SCHEMA
|
|
76
|
+
return NUMBER_ARRAYS.has(name) ? NUMBER_ARRAY_SCHEMA : undefined
|
|
77
|
+
}
|
|
78
|
+
|
|
42
79
|
function schemaForType(
|
|
43
80
|
ts: TypeScriptApi,
|
|
44
81
|
checker: TypeChecker,
|
|
@@ -66,7 +103,7 @@ function schemaForType(
|
|
|
66
103
|
if (hasFlag(type, ts.TypeFlags.StringLike)) return { type: "string" }
|
|
67
104
|
if (hasFlag(type, ts.TypeFlags.NumberLike)) return { type: "number" }
|
|
68
105
|
if (hasFlag(type, ts.TypeFlags.BooleanLike)) return { type: "boolean" }
|
|
69
|
-
if (hasFlag(type, ts.TypeFlags.BigIntLike)) return
|
|
106
|
+
if (hasFlag(type, ts.TypeFlags.BigIntLike)) return BIGINT_SCHEMA
|
|
70
107
|
if (hasFlag(type, ts.TypeFlags.Null)) return { type: "null" }
|
|
71
108
|
|
|
72
109
|
if (type.isUnion()) {
|
|
@@ -93,6 +130,8 @@ function schemaForType(
|
|
|
93
130
|
}
|
|
94
131
|
|
|
95
132
|
if (type.symbol?.name === "Date") return { type: "string", format: "date-time" }
|
|
133
|
+
const binary = binarySchema(type.symbol?.name)
|
|
134
|
+
if (binary !== undefined) return binary
|
|
96
135
|
if ((type.flags & ts.TypeFlags.Object) === 0) return undefined
|
|
97
136
|
|
|
98
137
|
seen.add(type)
|
|
@@ -255,7 +294,7 @@ async function schemaForType7(
|
|
|
255
294
|
if ((input.flags & stringLikeFlag) !== 0) return { type: "string" }
|
|
256
295
|
if ((input.flags & numberLikeFlag) !== 0) return { type: "number" }
|
|
257
296
|
if ((input.flags & booleanLikeFlag) !== 0) return { type: "boolean" }
|
|
258
|
-
if ((input.flags & bigintLikeFlag) !== 0) return
|
|
297
|
+
if ((input.flags & bigintLikeFlag) !== 0) return BIGINT_SCHEMA
|
|
259
298
|
if ((input.flags & nullFlag) !== 0) return { type: "null" }
|
|
260
299
|
|
|
261
300
|
if ((input.flags & unionFlag) !== 0) {
|
|
@@ -291,6 +330,8 @@ async function schemaForType7(
|
|
|
291
330
|
|
|
292
331
|
const symbol = await input.getSymbol()
|
|
293
332
|
if (symbol?.name === "Date") return { type: "string", format: "date-time" }
|
|
333
|
+
const binary = binarySchema(symbol?.name)
|
|
334
|
+
if (binary !== undefined) return binary
|
|
294
335
|
if ((input.flags & objectFlag) === 0) return undefined
|
|
295
336
|
|
|
296
337
|
seen.add(input)
|
package/src/output-schemas.ts
CHANGED
|
@@ -496,6 +496,8 @@ interface ProbeRoute {
|
|
|
496
496
|
readonly file: string
|
|
497
497
|
readonly fns: readonly RouteFn[]
|
|
498
498
|
readonly schemas: readonly OutputExport[]
|
|
499
|
+
/** Holes whose `boundaryLoaders` entry's `load` return type to read. */
|
|
500
|
+
readonly holes?: readonly string[]
|
|
499
501
|
}
|
|
500
502
|
|
|
501
503
|
/** Shapes keyed `<file>#<export>`: a route function's return type, or a schema's output type. */
|
|
@@ -532,6 +534,13 @@ function probeSource(routes: readonly ProbeRoute[]): {
|
|
|
532
534
|
names.set(name, `${route.file}#${schema}`)
|
|
533
535
|
lines.push(`declare const ${name}: __NifraDeclared<typeof __nifra_m${i}, "${schema}">`)
|
|
534
536
|
}
|
|
537
|
+
route.holes?.forEach((hole, j) => {
|
|
538
|
+
const name = `__nifra_h${i}_${j}`
|
|
539
|
+
names.set(name, `${route.file}#boundaryLoaders.${hole}`)
|
|
540
|
+
lines.push(
|
|
541
|
+
`declare const ${name}: __NifraReturned<(typeof __nifra_m${i})["boundaryLoaders"][${JSON.stringify(hole)}], "load">`,
|
|
542
|
+
)
|
|
543
|
+
})
|
|
535
544
|
})
|
|
536
545
|
return { text: `${lines.join("\n")}\n`, names }
|
|
537
546
|
}
|
|
@@ -1147,6 +1156,25 @@ export async function planOutputSchemas(
|
|
|
1147
1156
|
return { entries, edits, warnings }
|
|
1148
1157
|
}
|
|
1149
1158
|
|
|
1159
|
+
/**
|
|
1160
|
+
* What hole `hole`'s loader in the backend half `file` returns, with `text` read as that file: the
|
|
1161
|
+
* data a fix that writes the loader drafts its output schema from.
|
|
1162
|
+
*/
|
|
1163
|
+
export async function holeLoaderShape(
|
|
1164
|
+
root: string,
|
|
1165
|
+
file: string,
|
|
1166
|
+
hole: string,
|
|
1167
|
+
text: string,
|
|
1168
|
+
): Promise<{ readonly shape?: Shape; readonly warnings: readonly string[] }> {
|
|
1169
|
+
const { shapes, warnings } = await readShapes(
|
|
1170
|
+
root,
|
|
1171
|
+
[{ file, fns: [], schemas: [], holes: [hole] }],
|
|
1172
|
+
new Map([[file, text]]),
|
|
1173
|
+
)
|
|
1174
|
+
const shape = shapes.get(`${file}#boundaryLoaders.${hole}`)
|
|
1175
|
+
return shape === undefined ? { warnings } : { shape: dataShape(shape), warnings }
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1150
1178
|
// ---- entry points ------------------------------------------------------------------------------
|
|
1151
1179
|
|
|
1152
1180
|
const ROUTE_BACKEND_TS = /\.backend\.[cm]?ts$/
|