@nifrajs/cli 5.12.1 → 5.13.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 (67) hide show
  1. package/CHANGELOG.md +45 -0
  2. package/dist/cli.d.ts +9 -2
  3. package/dist/cli.d.ts.map +1 -1
  4. package/dist/cli.js +140 -19
  5. package/dist/cli.js.map +1 -1
  6. package/dist/command-catalog.d.ts +29 -1
  7. package/dist/command-catalog.d.ts.map +1 -1
  8. package/dist/command-catalog.js +102 -4
  9. package/dist/command-catalog.js.map +1 -1
  10. package/dist/deployment-adapters.d.ts +12 -0
  11. package/dist/deployment-adapters.d.ts.map +1 -0
  12. package/dist/deployment-adapters.js +92 -0
  13. package/dist/deployment-adapters.js.map +1 -0
  14. package/dist/dev-bun-config.d.ts +4 -3
  15. package/dist/dev-bun-config.d.ts.map +1 -1
  16. package/dist/dev-bun-config.js +14 -4
  17. package/dist/dev-bun-config.js.map +1 -1
  18. package/dist/init-agents.d.ts +5 -0
  19. package/dist/init-agents.d.ts.map +1 -1
  20. package/dist/init-agents.js +1 -1
  21. package/dist/init-agents.js.map +1 -1
  22. package/dist/load.d.ts +12 -3
  23. package/dist/load.d.ts.map +1 -1
  24. package/dist/load.js +19 -0
  25. package/dist/load.js.map +1 -1
  26. package/dist/mcp-http.js +1 -1
  27. package/dist/mcp-root.d.ts.map +1 -1
  28. package/dist/mcp-root.js +21 -3
  29. package/dist/mcp-root.js.map +1 -1
  30. package/dist/port.d.ts +6 -3
  31. package/dist/port.d.ts.map +1 -1
  32. package/dist/port.js +19 -7
  33. package/dist/port.js.map +1 -1
  34. package/dist/recipes/5.13.0.d.ts +9 -0
  35. package/dist/recipes/5.13.0.d.ts.map +1 -0
  36. package/dist/recipes/5.13.0.js +40 -0
  37. package/dist/recipes/5.13.0.js.map +1 -0
  38. package/dist/recipes/index.d.ts.map +1 -1
  39. package/dist/recipes/index.js +2 -0
  40. package/dist/recipes/index.js.map +1 -1
  41. package/dist/rules/routes.js +2 -2
  42. package/dist/shell-files.d.ts +44 -0
  43. package/dist/shell-files.d.ts.map +1 -0
  44. package/dist/shell-files.js +201 -0
  45. package/dist/shell-files.js.map +1 -0
  46. package/dist/shell-init.d.ts +33 -0
  47. package/dist/shell-init.d.ts.map +1 -0
  48. package/dist/shell-init.js +99 -0
  49. package/dist/shell-init.js.map +1 -0
  50. package/docs/examples.json +97 -1
  51. package/docs/llms-full.txt +989 -40
  52. package/docs/types.json +606 -36
  53. package/package.json +11 -11
  54. package/src/cli.ts +185 -35
  55. package/src/command-catalog.ts +144 -7
  56. package/src/deployment-adapters.ts +107 -0
  57. package/src/dev-bun-config.ts +21 -2
  58. package/src/init-agents.ts +1 -1
  59. package/src/load.ts +36 -3
  60. package/src/mcp-http.ts +1 -1
  61. package/src/mcp-root.ts +21 -3
  62. package/src/port.ts +25 -8
  63. package/src/recipes/5.13.0.ts +41 -0
  64. package/src/recipes/index.ts +2 -0
  65. package/src/rules/routes.ts +2 -2
  66. package/src/shell-files.ts +225 -0
  67. package/src/shell-init.ts +139 -0
@@ -63,7 +63,7 @@
63
63
  "name": "STATIC_RESPONSE",
64
64
  "topic": "Nifra - API & typed client",
65
65
  "slug": "api",
66
- "code": "import { server, staticResponse } from \"@nifrajs/core/server\"\n\nexport const app = server().get(\"/health\", staticResponse())\napp.listen(3000)"
66
+ "code": "import { server, staticResponse } from \"@nifrajs/core/server\"\n\nexport const app = server()\n .get(\"/health\", staticResponse())\n .post(\"/flush\", staticResponse())\napp.listen(3000)"
67
67
  },
68
68
  {
69
69
  "name": "TYPED_ERRORS",
@@ -239,6 +239,12 @@
239
239
  "slug": "control-api",
240
240
  "code": "import { timingSafeEqual } from \"node:crypto\"\nimport { server } from \"@nifrajs/core\"\nimport { streaming } from \"@nifrajs/core/sse\"\nimport { t, toOpenAPI } from \"@nifrajs/schema\"\n\nconst token = process.env.CONTROL_TOKEN\nif (token === undefined || token.length < 32) throw new Error(\"CONTROL_TOKEN must be 32+ characters\")\nconst expected = Buffer.from(`Bearer ${token}`)\nconst authorized = (request: Request): boolean => {\n const given = Buffer.from(request.headers.get(\"authorization\") ?? \"\")\n return given.length === expected.length && timingSafeEqual(given, expected)\n}\n\nconst Session = t.object({\n id: t.string(),\n status: t.union([t.literal(\"idle\"), t.literal(\"running\")]),\n})\nconst TurnEvent = t.object({\n seq: t.integer({ minimum: 0 }),\n type: t.string(),\n text: t.optional(t.string()),\n})\n\nexport const app = server()\n .use(streaming())\n .onRequest((request) =>\n authorized(request) ? undefined : Response.json({ error: \"unauthorized\" }, { status: 401 }),\n )\n .post(\n \"/session/create\",\n { body: t.object({ cwd: t.string({ minLength: 1, maxLength: 4096 }) }), response: Session },\n () => ({ id: crypto.randomUUID(), status: \"idle\" as const }),\n )\n .post(\n \"/turn/send\",\n {\n body: t.object({\n sessionId: t.string(),\n message: t.string({ minLength: 1, maxLength: 256 * 1024 }),\n }),\n response: t.object({ turnId: t.string() }),\n bodyLimit: 512 * 1024,\n },\n () => ({ turnId: crypto.randomUUID() }),\n )\n .sse(\n \"/turn/events\",\n { query: t.object({ turnId: t.string() }), sse: TurnEvent },\n (c, stream) => {\n const after = Number(c.req.headers.get(\"last-event-id\") ?? -1)\n for (let seq = after + 1; seq < 3; seq++)\n stream.send({ seq, type: \"assistant.delta\", text: `part ${seq}` }, { id: String(seq) })\n stream.close()\n },\n )\n\nexport const spec = toOpenAPI(app, { openapiVersion: \"3.2.0\", title: \"Control API\", version: \"1.0.0\" })"
241
241
  },
242
+ {
243
+ "name": "DISPOSE",
244
+ "topic": "Nifra - Custom server (createWebApp)",
245
+ "slug": "custom-server",
246
+ "code": "import { server } from \"@nifrajs/core\"\nimport { createScheduler } from \"@nifrajs/cron\"\nimport { createQueue } from \"@nifrajs/jobs\"\n\nconst app = server().get(\"/\", () => \"ok\")\nconst queue = createQueue()\n\nexport async function runUntilDone(done: Promise<void>): Promise<void> {\n await using running = app.listen(3000) // graceful stop() when the scope ends\n await using worker = queue.start() // stops polling, waits for jobs in flight\n using scheduler = createScheduler() // stop() when the scope ends\n scheduler.start()\n await done\n console.log(running.port, worker.running)\n}"
247
+ },
242
248
  {
243
249
  "name": "FULL_APP",
244
250
  "topic": "Nifra - Custom server (createWebApp)",
@@ -293,6 +299,12 @@
293
299
  "slug": "deployment",
294
300
  "code": "// worker.ts - any nifra app as a Workers fetch handler\nimport { server, toFetchHandler } from \"@nifrajs/core/server\"\n\nconst app = server().get(\"/\", () => ({ ok: true }))\nexport default toFetchHandler(app)"
295
301
  },
302
+ {
303
+ "name": "FLY",
304
+ "topic": "Nifra - Deployment",
305
+ "slug": "deployment",
306
+ "code": "// nifra.config.ts - keep your existing adapter and clientModule exports, and add:\nimport { flyAdapter } from \"@nifrajs/deploy-fly\"\n\nexport const target = \"fly\"\nexport const deploymentAdapter = {\n adapter: flyAdapter,\n options: { app: \"my-nifra-app\", primaryRegion: \"iad\" },\n}\n\n// Build locally; then deploy the output directory yourself.\n// nifra build\n// cd dist && fly deploy"
307
+ },
296
308
  {
297
309
  "name": "HTTPS",
298
310
  "topic": "Nifra - Deployment",
@@ -323,6 +335,48 @@
323
335
  "slug": "dev",
324
336
  "code": "// vite.config.ts - a Vite/Rollup PRODUCTION client build (the escape hatch, not the default).\n// Only reach for this when an app needs a Vite-only transform with no Bun equivalent; Nifra's default\n// production bundler stays Bun (buildClient), which is faster and Bun-native.\nimport {\n viteAssetUrlGuard,\n viteBareBuiltinExternal,\n viteLeakGuard,\n} from \"@nifrajs/web/plugins/vite-leak-guard\"\n\nexport default {\n // Keeps a bare built-in (`fs/promises`) named instead of an empty stub, so the guard sees it.\n // viteAssetUrlGuard refuses a new URL(\"../backend/x.ts\", import.meta.url) - a file Vite would copy or\n // inline with no import for the graph guard to see. Last, after any framework plugin.\n plugins: [viteBareBuiltinExternal(), viteAssetUrlGuard()],\n build: {\n // The SAME client-leak guards Nifra's Bun build runs - backend code (a route's .backend.ts half,\n // backend/) or a node: builtin reaching the browser fails the build, with the identical message. A second production\n // pipeline must not ship without them. `node:` stays external so the guard can name it.\n rollupOptions: { external: [/^node:/], plugins: [viteLeakGuard()] },\n },\n}"
325
337
  },
338
+ {
339
+ "name": "CACHE",
340
+ "topic": "Nifra - Browser end-to-end tests",
341
+ "slug": "e2e",
342
+ "code": "import { server } from \"@nifrajs/core/server\"\nimport { e2e, expect, formatReport, unique, type E2EModel } from \"@nifrajs/e2e\"\n\nconst app = server().get(\"/signup\", () => new Response(\"<h1>Sign up\", {\n headers: { \"content-type\": \"text/html\" },\n}))\nconst model: E2EModel = { decide: () => ({ kind: \"done\" }) }\n\nawait using t = await e2e(app, {\n model,\n cache: { testId: \"a visitor signs up\", mode: \"read-write\" },\n})\nawait t.open(\"/signup\")\n\n// unique() keeps a run-varying value from missing the cache; it is typed again on replay.\nawait t.agent.act(\"sign up as {email}\", { params: { email: unique(`ada-${Date.now()}@example.test`) } })\n\n// The recording is written only when a LATER check passes.\nawait expect(t.screen.getByRole(\"heading\")).toBeVisible()\nconsole.log(formatReport(t.agent.report())) // Cache: 1 replayed - 0 handed off - 0 missed (0 model calls)"
343
+ },
344
+ {
345
+ "name": "CI",
346
+ "topic": "Nifra - Browser end-to-end tests",
347
+ "slug": "e2e",
348
+ "code": "import { server } from \"@nifrajs/core/server\"\nimport { e2e, expect } from \"@nifrajs/e2e\"\n\nconst html = (body: string) => new Response(body, { headers: { \"content-type\": \"text/html\" } })\n\nconst app = server()\n .get(\"/\", () => html('<a href=\"/dashboard\">Dashboard'))\n .get(\"/dashboard\", () => html(\"<h1>Dashboard\"))\n\n// CI replays committed recordings and never writes or deletes them. With no model, a recording that\n// no longer matches fails with MODEL_MISSING; mode: \"strict\" fails with REPLAY_STALE (or\n// REPLAY_MISSING when nothing was recorded) instead of ever calling a model.\nawait using t = await e2e(app, { cache: { testId: \"home to dashboard\", mode: \"strict\" } })\nawait t.open(\"/\")\nawait t.agent.act(\"open the dashboard\") // reads the committed recording; strict mode checks it here\nawait expect(t.screen.getByRole(\"heading\")).toHaveText(\"Dashboard\")"
349
+ },
350
+ {
351
+ "name": "ENGINE",
352
+ "topic": "Nifra - Browser end-to-end tests",
353
+ "slug": "e2e",
354
+ "code": "import { server } from \"@nifrajs/core/server\"\nimport { e2e } from \"@nifrajs/e2e\"\nimport { fakeEngine } from \"@nifrajs/e2e/fake\"\n\nconst app = server().get(\"/\", () => new Response(\"ok\"))\n\n// An in-memory engine with the same locator rules, for testing code built on this package.\nconst { factory } = fakeEngine({ pages: { \"/\": [{ role: \"heading\", name: \"Home\", text: \"Home\" }] } })\nawait using t = await e2e(app, { engine: factory, cache: false })\nawait t.open(\"/\")\nawait t.screen.getByRole(\"heading\").count()"
355
+ },
356
+ {
357
+ "name": "EXTRACT",
358
+ "topic": "Nifra - Browser end-to-end tests",
359
+ "slug": "e2e",
360
+ "code": "import { server } from \"@nifrajs/core/server\"\nimport { t as s } from \"@nifrajs/schema\"\nimport { e2e } from \"@nifrajs/e2e\"\n\nconst app = server().get(\"/plan\", () => new Response(\"<p>Pro, 5 seats\", {\n headers: { \"content-type\": \"text/html\" },\n}))\n\nconst model = {\n decide: () => ({ value: { plan: \"Pro\", seats: 5 } }), // a real model reads the outline in the request\n}\n\nawait using t = await e2e(app, { model })\nawait t.open(\"/plan\")\nconst plan = await t.agent.extract(\"the current plan and seat count\", {\n schema: s.object({ plan: s.string(), seats: s.number() }),\n})\nplan.seats // number"
361
+ },
362
+ {
363
+ "name": "FIRST",
364
+ "topic": "Nifra - Browser end-to-end tests",
365
+ "slug": "e2e",
366
+ "code": "import { test } from \"bun:test\"\nimport { server } from \"@nifrajs/core/server\"\nimport { e2e, expect } from \"@nifrajs/e2e\"\n\nconst html = (body: string) => new Response(body, { headers: { \"content-type\": \"text/html\" } })\n\nconst app = server().get(\"/users/:id\", (c) =>\n html(`<input aria-label=\"Nickname\" value=\"ada\"><h1>User ${c.params.id}`),\n)\n\ntest(\"a user page renders\", async () => {\n await using t = await e2e(app)\n await t.open(\"/users/:id\", { params: { id: 42 } }) // a path or param the app lacks fails typecheck\n await expect(t.screen.getByRole(\"heading\")).toHaveText(\"User 42\")\n await t.screen.getByLabel(\"Nickname\").fill(\"grace\")\n await expect(t).toHaveURL(\"/users/42\")\n})"
367
+ },
368
+ {
369
+ "name": "SESSION",
370
+ "topic": "Nifra - Browser end-to-end tests",
371
+ "slug": "e2e",
372
+ "code": "import { server } from \"@nifrajs/core/server\"\nimport { testSession } from \"@nifrajs/testing\"\nimport { e2e, expect } from \"@nifrajs/e2e\"\n\nconst app = server().get(\"/me\", (c) =>\n new Response(`<p>session ${c.cookies.sid ?? \"none\"}`, { headers: { \"content-type\": \"text/html\" } }),\n)\n\n// Log in through the API, in process, then hand the cookie jar to the browser.\nconst { cookies } = testSession<typeof app>(app)\ncookies.set(\"sid\", \"abc123\")\n\nawait using t = await e2e(app, { cookies })\nawait t.open(\"/me\")\nawait expect(t.screen.getByText(\"session abc123\")).toBeVisible()"
373
+ },
374
+ {
375
+ "name": "WORKERS_ENV",
376
+ "topic": "Nifra - Edge & bindings",
377
+ "slug": "edge",
378
+ "code": "import { defineEnv, env as schemaEnv } from \"@nifrajs/env\"\n\n// Select string bindings when the Workers env also contains KV/D1 objects.\ntype StringBindings = Record<string, string | undefined>\n\nexport default {\n fetch(_request: Request, bindings: StringBindings) {\n const config = defineEnv(\n { API_URL: schemaEnv.url(), API_TOKEN: schemaEnv.string() },\n { source: bindings },\n )\n return new Response(config.API_URL)\n },\n}"
379
+ },
326
380
  {
327
381
  "name": "LAB",
328
382
  "topic": "Nifra - Failure laboratory",
@@ -449,6 +503,12 @@
449
503
  "slug": "index",
450
504
  "code": "// routes/index.backend.ts - the loader runs on the server (in-process during SSR, no network),\n// fully typed against your contract. This file never reaches the browser.\nimport { t } from \"@nifrajs/schema\"\nimport type { Route } from \"./+types/index\"\n\n// The page receives exactly this shape; anything else the loader returns stays on the server.\nexport const loaderOutput = t.object({ id: t.string() })\n\nexport async function loader({ api }: Route.LoaderArgs) {\n const res = await api.users({ id: \"7\" }).get()\n return { id: res.ok ? res.data.id : \"unknown\" }\n}"
451
505
  },
506
+ {
507
+ "name": "ENV",
508
+ "topic": "Nifra - Integrations (Stripe, and any npm library)",
509
+ "slug": "integrations",
510
+ "code": "import { defineEnv, env } from \"@nifrajs/env\"\n\nexport const config = defineEnv({\n DATABASE_URL: env.url(),\n PORT: env.port({ default: 3000 }),\n NODE_ENV: env.enum([\"development\", \"production\", \"test\"], { default: \"development\" }),\n})"
511
+ },
452
512
  {
453
513
  "name": "BUS_PATTERN",
454
514
  "topic": "Nifra - Islands: interactivity without hydration",
@@ -533,6 +593,42 @@
533
593
  "slug": "migrate-route-by-route",
534
594
  "code": "import { Hono } from \"hono\"\nimport { server } from \"@nifrajs/core/server\"\nimport { t } from \"@nifrajs/schema\"\n\nconst legacy = new Hono()\nlegacy.get(\"/users\", (c) => c.json({ source: \"hono\", users: [] }))\nlegacy.post(\"/users\", async (c) => c.json(await c.req.json()))\n\nexport const app = server()\n // The wildcard is a literal mount prefix, not a typed Nifra route.\n .mountFetch(\"/legacy/*\", legacy.fetch, { stripPrefix: true })\n // Typed routes take precedence, so move one route out of Hono at a time.\n .get(\"/legacy/users/:id\", (c) => ({ id: c.params.id, source: \"nifra\" }))\n .post(\n \"/users\",\n { body: t.object({ name: t.string() }) },\n (c) => ({ id: crypto.randomUUID(), name: c.body.name }),\n )"
535
595
  },
596
+ {
597
+ "name": "BASE_TRANSPORT",
598
+ "topic": "Nifra - Native shells",
599
+ "slug": "native-shells",
600
+ "code": "// frontend/transport.ts\nimport { serverFetch } from \"@nifrajs/web/shell\"\n\nexport default serverFetch({ origin: \"https://api.example.com\" })"
601
+ },
602
+ {
603
+ "name": "FUNCTIONS",
604
+ "topic": "Nifra - Native shells",
605
+ "slug": "native-shells",
606
+ "code": "import { serverFunctions } from \"@nifrajs/web/fn\"\nimport * as todos from \"./todos.fn.ts\"\n\nexport const fns = serverFunctions(\"todos\", todos, {\n origins: [\"tauri://localhost\", \"http://tauri.localhost\", \"capacitor://localhost\"],\n})"
607
+ },
608
+ {
609
+ "name": "LIVE",
610
+ "topic": "Nifra - Native shells",
611
+ "slug": "native-shells",
612
+ "code": "import { live } from \"@nifrajs/web/live\"\n\nexport const runtime = live({\n // Required in production, at least 32 bytes: set LIVE_SECRET (openssl rand -base64 32).\n secret: process.env.LIVE_SECRET ?? \"\",\n // Anonymous viewers. An app with accounts returns the session's user id, so a change of account\n // clears the client's cache and revoke(userId) reaches this viewer's grants.\n subject: () => null,\n trustedOrigins: [\"tauri://localhost\", \"http://tauri.localhost\", \"capacitor://localhost\"],\n})"
613
+ },
614
+ {
615
+ "name": "QUERY",
616
+ "topic": "Nifra - Native shells",
617
+ "slug": "native-shells",
618
+ "code": "import { appFetch } from \"@nifrajs/web/shell\"\n\n// The transport in a shell build, the global fetch anywhere else.\nexport const todos = (): Promise<unknown> => appFetch(\"/api/todos\").then((res) => res.json())"
619
+ },
620
+ {
621
+ "name": "SERVER",
622
+ "topic": "Nifra - Native shells",
623
+ "slug": "native-shells",
624
+ "code": "// backend/framework.ts: the web app answers page data and form posts, so the CORS goes on it.\nimport { cors } from \"@nifrajs/middleware\"\n\nconst SHELLS = [\"tauri://localhost\", \"http://tauri.localhost\", \"capacitor://localhost\"]\n\nexport const use = (app: { use: (plugin: ReturnType<typeof cors>) => unknown }): void => {\n app.use(\n cors({\n origin: SHELLS,\n // Named, not \"*\": a credentialed request reads \"*\" as a header called \"*\".\n exposedHeaders: [\"x-nifra-redirect\", \"x-nifra-status\", \"x-nifra-revalidate\"],\n }),\n )\n}"
625
+ },
626
+ {
627
+ "name": "TRANSPORT",
628
+ "topic": "Nifra - Native shells",
629
+ "slug": "native-shells",
630
+ "code": "// frontend/transport.ts\nimport { serverFetch } from \"@nifrajs/web/shell\"\n\nexport default serverFetch({\n origin: \"https://api.example.com\",\n // A bearer token that refreshes: the function runs before each request.\n headers: async () => ({ authorization: `Bearer ${await currentToken()}` }),\n})\n\n// The app's own: where it keeps and refreshes its token.\ndeclare function currentToken(): Promise<string>"
631
+ },
536
632
  {
537
633
  "name": "BACKEND",
538
634
  "topic": "Nifra - Partial prerendering",