@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.
- package/CHANGELOG.md +45 -0
- package/dist/cli.d.ts +9 -2
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +140 -19
- package/dist/cli.js.map +1 -1
- package/dist/command-catalog.d.ts +29 -1
- package/dist/command-catalog.d.ts.map +1 -1
- package/dist/command-catalog.js +102 -4
- package/dist/command-catalog.js.map +1 -1
- package/dist/deployment-adapters.d.ts +12 -0
- package/dist/deployment-adapters.d.ts.map +1 -0
- package/dist/deployment-adapters.js +92 -0
- package/dist/deployment-adapters.js.map +1 -0
- package/dist/dev-bun-config.d.ts +4 -3
- package/dist/dev-bun-config.d.ts.map +1 -1
- package/dist/dev-bun-config.js +14 -4
- package/dist/dev-bun-config.js.map +1 -1
- package/dist/init-agents.d.ts +5 -0
- package/dist/init-agents.d.ts.map +1 -1
- package/dist/init-agents.js +1 -1
- package/dist/init-agents.js.map +1 -1
- package/dist/load.d.ts +12 -3
- package/dist/load.d.ts.map +1 -1
- package/dist/load.js +19 -0
- package/dist/load.js.map +1 -1
- package/dist/mcp-http.js +1 -1
- package/dist/mcp-root.d.ts.map +1 -1
- package/dist/mcp-root.js +21 -3
- package/dist/mcp-root.js.map +1 -1
- package/dist/port.d.ts +6 -3
- package/dist/port.d.ts.map +1 -1
- package/dist/port.js +19 -7
- package/dist/port.js.map +1 -1
- package/dist/recipes/5.13.0.d.ts +9 -0
- package/dist/recipes/5.13.0.d.ts.map +1 -0
- package/dist/recipes/5.13.0.js +40 -0
- package/dist/recipes/5.13.0.js.map +1 -0
- package/dist/recipes/index.d.ts.map +1 -1
- package/dist/recipes/index.js +2 -0
- package/dist/recipes/index.js.map +1 -1
- package/dist/rules/routes.js +2 -2
- package/dist/shell-files.d.ts +44 -0
- package/dist/shell-files.d.ts.map +1 -0
- package/dist/shell-files.js +201 -0
- package/dist/shell-files.js.map +1 -0
- package/dist/shell-init.d.ts +33 -0
- package/dist/shell-init.d.ts.map +1 -0
- package/dist/shell-init.js +99 -0
- package/dist/shell-init.js.map +1 -0
- package/docs/examples.json +97 -1
- package/docs/llms-full.txt +989 -40
- package/docs/types.json +606 -36
- package/package.json +11 -11
- package/src/cli.ts +185 -35
- package/src/command-catalog.ts +144 -7
- package/src/deployment-adapters.ts +107 -0
- package/src/dev-bun-config.ts +21 -2
- package/src/init-agents.ts +1 -1
- package/src/load.ts +36 -3
- package/src/mcp-http.ts +1 -1
- package/src/mcp-root.ts +21 -3
- package/src/port.ts +25 -8
- package/src/recipes/5.13.0.ts +41 -0
- package/src/recipes/index.ts +2 -0
- package/src/rules/routes.ts +2 -2
- package/src/shell-files.ts +225 -0
- package/src/shell-init.ts +139 -0
package/docs/llms-full.txt
CHANGED
|
@@ -165,7 +165,7 @@ from one core, with loaders, actions, streaming, and a typed data layer - and a
|
|
|
165
165
|
### Nifra's lane
|
|
166
166
|
|
|
167
167
|
Every other full-stack framework here is **built around one UI library** (TanStack Start
|
|
168
|
-
supports
|
|
168
|
+
supports three), and most reach other hosts through adapters. Nifra bets on the other axis: one
|
|
169
169
|
core, five front-ends, many runtimes, with a contract-first typed backend in the same project.
|
|
170
170
|
|
|
171
171
|
Framework
|
|
@@ -188,12 +188,12 @@ SvelteKit
|
|
|
188
188
|
Svelte
|
|
189
189
|
adapters
|
|
190
190
|
|
|
191
|
-
React Router
|
|
191
|
+
React Router 8 (Remix)
|
|
192
192
|
React
|
|
193
193
|
adapters
|
|
194
194
|
|
|
195
195
|
TanStack Start
|
|
196
|
-
React · Solid
|
|
196
|
+
React · Solid · Vue
|
|
197
197
|
Nitro · host plugins
|
|
198
198
|
|
|
199
199
|
### Full-stack parity
|
|
@@ -294,14 +294,14 @@ to full-stack later without a rewrite.
|
|
|
294
294
|
**Throughput - the realistic case.** Router micro-benchmarks flatter Hono (a single compiled
|
|
295
295
|
regex), but a real request also pays for middleware and validation,
|
|
296
296
|
context, and serialization. In the current matrix Nifra runs **level with Elysia on Bun** -
|
|
297
|
-
|
|
297
|
+
99% of Elysia on `GET /users/:id` and
|
|
298
298
|
103% on the validated `POST` - and
|
|
299
|
-
**level with Fastify on Node** (
|
|
300
|
-
|
|
301
|
-
and Express behind. On **Deno** it runs at
|
|
302
|
-
GET and
|
|
299
|
+
**level with Fastify on Node** (109% of Fastify on GET,
|
|
300
|
+
105% on the validated `POST`), with Elysia, Hono,
|
|
301
|
+
and Express behind. On **Deno** it runs at 117% of Elysia on
|
|
302
|
+
GET and 143% on the validated `POST`. In the realistic shape (security headers + CORS +
|
|
303
303
|
bearer auth + cookies + validated query/body + a ~2.4 KB JSON response, measured on
|
|
304
|
-
**Bun** with `oha`) Nifra runs at **
|
|
304
|
+
**Bun** with `oha`) Nifra runs at **118% of Elysia on GET and 119% on POST**. Treat benchmark
|
|
305
305
|
rows as same-run evidence, not a permanent law of nature.
|
|
306
306
|
|
|
307
307
|
-
|
|
@@ -1409,7 +1409,7 @@ export const app = server()
|
|
|
1409
1409
|
For a route whose response is always empty, use `staticResponse()` instead of
|
|
1410
1410
|
creating a `Response` in a handler on every request. Pass fixed status and headers
|
|
1411
1411
|
to the helper; caller-supplied `Content-Length` and `Transfer-Encoding`
|
|
1412
|
-
are rejected. On Bun, a plain literal-path GET or
|
|
1412
|
+
are rejected. On Bun, a plain literal-path GET, HEAD, or bodyless POST route can use Bun's static response
|
|
1413
1413
|
table when no request policy, validation, response middleware, or app-wide
|
|
1414
1414
|
`.responseHeaders(...)` defaults need to run. Null-body status codes use the
|
|
1415
1415
|
regular handler path to preserve framing on Bun 1.3. Otherwise Nifra keeps the regular route
|
|
@@ -1418,7 +1418,9 @@ pipeline. `app.fetch` and other runtimes also produce a fresh response per call.
|
|
|
1418
1418
|
```ts
|
|
1419
1419
|
import { server, staticResponse } from "@nifrajs/core/server"
|
|
1420
1420
|
|
|
1421
|
-
export const app = server()
|
|
1421
|
+
export const app = server()
|
|
1422
|
+
.get("/health", staticResponse())
|
|
1423
|
+
.post("/flush", staticResponse())
|
|
1422
1424
|
app.listen(3000)
|
|
1423
1425
|
```
|
|
1424
1426
|
|
|
@@ -1723,11 +1725,12 @@ whole path: `RequestPath<"/files/:name.json">` is
|
|
|
1723
1725
|
_before_ path segments: the seven HTTP verbs
|
|
1724
1726
|
(`get`/`post`/`put`/`patch`/`delete`/
|
|
1725
1727
|
`head`/`options`, any casing) call the route, and
|
|
1726
|
-
`subscribe`, `ws`, `index`,
|
|
1727
|
-
match) are the SSE, WebSocket, root-path,
|
|
1728
|
+
`subscribe`, `ws`, `index`, `then`, and
|
|
1729
|
+
`$query` (exact match) are the SSE, WebSocket, root-path, thenable-guard, and QUERY
|
|
1730
|
+
keys. A route whose path
|
|
1728
1731
|
contains a static segment spelling one of these - `.post("/api/delete", …)` -
|
|
1729
|
-
cannot be reached by **dot access**: `api.delete
|
|
1730
|
-
`delete` verb,
|
|
1732
|
+
cannot be reached by **dot access**: `api.delete()` invokes the
|
|
1733
|
+
`delete` verb, while `api.delete.post` is undefined. The typed spelling is a **call on the parent
|
|
1731
1734
|
node** - `api.api("delete").post()` sends `POST /api/delete` - the
|
|
1732
1735
|
same call params use, accepting exactly the colliding segment names. The client type rejects
|
|
1733
1736
|
the dot access at compile time with that guidance, and `nifra check` reports the
|
|
@@ -1769,6 +1772,11 @@ client sends nothing rather than reach a route the call does not name.
|
|
|
1769
1772
|
`.subscribe()` reports it through `onError` and closes;
|
|
1770
1773
|
`.ws()` throws.
|
|
1771
1774
|
|
|
1775
|
+
A `.subscribe()` handle and a `.ws()` handle also close at the end of a
|
|
1776
|
+
scope when held with `using`, the same as calling `close()`. A
|
|
1777
|
+
browser without `Symbol.dispose` needs a polyfill first; `close()`
|
|
1778
|
+
works without one.
|
|
1779
|
+
|
|
1772
1780
|
The same client runs in the browser and on the server. During SSR, a route's
|
|
1773
1781
|
loader calls it **in-process** (no network hop) via
|
|
1774
1782
|
`ctx.api`. Next: file routing,
|
|
@@ -3328,6 +3336,12 @@ third argument's option), a background refetch, and bounded GC. The query cache
|
|
|
3328
3336
|
mutation and infinite-query bindings across the adapters, using each framework's reactive
|
|
3329
3337
|
model.
|
|
3330
3338
|
|
|
3339
|
+
Query keys are serialized structurally. Keep key arrays and objects stable when practical, and
|
|
3340
|
+
treat objects as immutable after the first query uses them: Nifra caches object hashes by
|
|
3341
|
+
identity, so mutating a reused object can leave it under its old hash, while creating a fresh
|
|
3342
|
+
equivalent key repeats serialization work. For example, derive one key from the current
|
|
3343
|
+
identity and reuse it for the render rather than mutating it after the query starts.
|
|
3344
|
+
|
|
3331
3345
|
```ts
|
|
3332
3346
|
import { useQuery, useQueryClient } from "@nifrajs/web-react/query"
|
|
3333
3347
|
|
|
@@ -4771,11 +4785,20 @@ the part names none. Only a type that `accept` matched is one the bytes proved.
|
|
|
4771
4785
|
-
|
|
4772
4786
|
**A form post is a simple cross-origin request.** A browser sends
|
|
4773
4787
|
`multipart/form-data`, urlencoded and `text/plain` with cookies and no
|
|
4774
|
-
preflight.
|
|
4775
|
-
|
|
4776
|
-
|
|
4777
|
-
|
|
4778
|
-
|
|
4788
|
+
preflight. Core refuses one whose `Origin` is another site's with
|
|
4789
|
+
`403` on a body-schema route before reading it, and on any other
|
|
4790
|
+
`POST`, `PUT`, `PATCH` or `DELETE` route before
|
|
4791
|
+
the handler runs. A route without a body schema refuses a request with no content type from
|
|
4792
|
+
such a site too, unless it carries an `Authorization` header a script set (a
|
|
4793
|
+
browser re-sends `Basic`, `Digest`, `Negotiate` and
|
|
4794
|
+
`NTLM` credentials from its own cache, so those do not count). List a site that
|
|
4795
|
+
must post to the app in the server's `formOrigins`; the origins
|
|
4796
|
+
`cors()` and `csrf({ origins })` name are trusted without a second
|
|
4797
|
+
list, for that server's own routes only: a mounted server keeps its own policy, a
|
|
4798
|
+
parent must list the same sites itself, and a web page action checks the backend's
|
|
4799
|
+
`allowedOrigins` instead. Audit those origins: one named there may post a form
|
|
4800
|
+
whatever methods or credentials the CORS policy allows. A request without an `Origin` passes, so a cookie-authenticated upload
|
|
4801
|
+
route still needs `csrf()` like any other state change.
|
|
4779
4802
|
|
|
4780
4803
|
-
|
|
4781
4804
|
**The form is held in memory.** Budget roughly twice `bodyLimit` per request in
|
|
@@ -7038,6 +7061,29 @@ export default { fetch: (req: Request, env: Env, ctx: ExecutionContext) =>
|
|
|
7038
7061
|
app.fetch(req, { env, waitUntil: ctx.waitUntil.bind(ctx) }) }
|
|
7039
7062
|
```
|
|
7040
7063
|
|
|
7064
|
+
For environment variables represented as strings, `@nifrajs/env` can validate the
|
|
7065
|
+
Workers binding object with `defineEnv(shape, { source: bindings })`. If the
|
|
7066
|
+
platform env also contains KV, D1, or Durable Object bindings, select only the string keys
|
|
7067
|
+
into the source object; keep the full binding type on `server<Env>()` for
|
|
7068
|
+
`c.env`.
|
|
7069
|
+
|
|
7070
|
+
```ts
|
|
7071
|
+
import { defineEnv, env as schemaEnv } from "@nifrajs/env"
|
|
7072
|
+
|
|
7073
|
+
// Select string bindings when the Workers env also contains KV/D1 objects.
|
|
7074
|
+
type StringBindings = Record<string, string | undefined>
|
|
7075
|
+
|
|
7076
|
+
export default {
|
|
7077
|
+
fetch(_request: Request, bindings: StringBindings) {
|
|
7078
|
+
const config = defineEnv(
|
|
7079
|
+
{ API_URL: schemaEnv.url(), API_TOKEN: schemaEnv.string() },
|
|
7080
|
+
{ source: bindings },
|
|
7081
|
+
)
|
|
7082
|
+
return new Response(config.API_URL)
|
|
7083
|
+
},
|
|
7084
|
+
}
|
|
7085
|
+
```
|
|
7086
|
+
|
|
7041
7087
|
Export through `toFetchHandler` rather than handing the app out directly. Besides
|
|
7042
7088
|
`env` and `ctx.waitUntil`, it tells nifra the request came from the
|
|
7043
7089
|
platform's own HTTP parser, which lets the
|
|
@@ -7614,6 +7660,64 @@ none (Build Output API v3)
|
|
|
7614
7660
|
pages, the client bundle and `public/`, for any static host. See
|
|
7615
7661
|
SSG & ISR.
|
|
7616
7662
|
|
|
7663
|
+
### Public build-target adapters
|
|
7664
|
+
|
|
7665
|
+
A deployment adapter adds host packaging around an existing Nifra build target. It does not
|
|
7666
|
+
replace the runtime, generated server entry, or Nifra's build checks. The versioned contract
|
|
7667
|
+
in `@nifrajs/web/build` lets an adapter declare its base target, validate options,
|
|
7668
|
+
and return extra files plus deploy guidance. Nifra checks paths and collisions, scans text
|
|
7669
|
+
artifacts for credentials, and only swaps the output directory after the full build passes.
|
|
7670
|
+
|
|
7671
|
+
Install the Fly adapter with its required Node runtime and create the named Fly app before
|
|
7672
|
+
its first deploy:
|
|
7673
|
+
|
|
7674
|
+
```sh
|
|
7675
|
+
bun add @nifrajs/deploy-fly @nifrajs/node
|
|
7676
|
+
fly apps create my-nifra-app # once, before the first deploy
|
|
7677
|
+
```
|
|
7678
|
+
|
|
7679
|
+
```ts
|
|
7680
|
+
// nifra.config.ts - keep your existing adapter and clientModule exports, and add:
|
|
7681
|
+
import { flyAdapter } from "@nifrajs/deploy-fly"
|
|
7682
|
+
|
|
7683
|
+
export const target = "fly"
|
|
7684
|
+
export const deploymentAdapter = {
|
|
7685
|
+
adapter: flyAdapter,
|
|
7686
|
+
options: { app: "my-nifra-app", primaryRegion: "iad" },
|
|
7687
|
+
}
|
|
7688
|
+
|
|
7689
|
+
// Build locally; then deploy the output directory yourself.
|
|
7690
|
+
// nifra build
|
|
7691
|
+
// cd dist && fly deploy
|
|
7692
|
+
```
|
|
7693
|
+
|
|
7694
|
+
The Fly reference package wraps the ordinary `node` target and emits a Dockerfile
|
|
7695
|
+
plus `fly.toml`. The `deploymentAdapter` configuration above registers
|
|
7696
|
+
the custom `fly` target. Run `fly deploy` from the emitted output
|
|
7697
|
+
directory; Nifra never logs in or deploys for you. Adapter code is trusted build-time code
|
|
7698
|
+
running in the CLI process. Each adapter must explicitly declare whether it supports the
|
|
7699
|
+
coordinated component pipeline; unsupported combinations fail before the previous output is
|
|
7700
|
+
replaced. Set Fly's optional `healthCheckPath` only when that route returns 2xx
|
|
7701
|
+
while the app is ready.
|
|
7702
|
+
|
|
7703
|
+
The emitted Dockerfile sets `NODE_ENV=production`, and production refuses
|
|
7704
|
+
in-memory stores. An app that uses one, such as the site scaffold's rate limiter, must
|
|
7705
|
+
configure a shared store, or for a single machine allow it explicitly (the scaffold reads
|
|
7706
|
+
`NIFRA_ALLOW_MEMORY_RATE_LIMIT=true`, set with `fly secrets set`). An
|
|
7707
|
+
in-memory limiter keeps one quota per machine, and `fly deploy` adds a spare
|
|
7708
|
+
machine by default. Deploy a new app with `fly deploy --ha=false`; for an app
|
|
7709
|
+
that already has several machines, run `fly scale count 1`. Confirm with
|
|
7710
|
+
`fly status`, or use a shared store.
|
|
7711
|
+
|
|
7712
|
+
The generated Node server derives `c.clientIp` from the socket peer only, which
|
|
7713
|
+
behind Fly's proxy is the proxy. Visitors then share one rate-limit bucket and get 429
|
|
7714
|
+
once it is spent, and a shared store does not change the keying. Passing
|
|
7715
|
+
`header: "fly-client-ip"` to `rateLimit()` keys requests that arrive
|
|
7716
|
+
over HTTP per visitor, but only while Fly's proxy is the sole path to the app; behind a CDN
|
|
7717
|
+
or another proxy the header holds that proxy's address. The server's own page-loader calls
|
|
7718
|
+
to the backend carry no such header, so they stay keyed by the proxy peer. Raise
|
|
7719
|
+
`max` for an app that serves its pages from the same server.
|
|
7720
|
+
|
|
7617
7721
|
The Bun, Node and Deno entries serve the client bundle from disk next to themselves; on
|
|
7618
7722
|
Cloudflare and Vercel the platform serves it.
|
|
7619
7723
|
|
|
@@ -7892,7 +7996,7 @@ nifra dev # true-HMR dev server (Bun native HMR + Nifra SSR) - http://local
|
|
|
7892
7996
|
nifra dev --vite # the Vite middleware pipeline instead (automatic when vitePlugins are your ONLY transforms)
|
|
7893
7997
|
nifra build # full Bun deploy → dist/server.js + content-hashed dist/assets/ (default target: bun)
|
|
7894
7998
|
nifra start # run dist/server.js on Bun
|
|
7895
|
-
nifra build --target cloudflare # also: node | deno | vercel | static;
|
|
7999
|
+
nifra build --target cloudflare # also: node | deno | vercel | static; configure custom adapters in nifra.config.ts first
|
|
7896
8000
|
|
|
7897
8001
|
# dev + start share the default port 4321. Override per run: --port <n> (alias -p) or the PORT env var.
|
|
7898
8002
|
# flags: --port <n> (dev/start) · --out <dir> (build/start) · --target <t> (build) · --poll (dev)
|
|
@@ -8003,9 +8107,9 @@ What it does
|
|
|
8003
8107
|
|
|
8004
8108
|
The dev server with true HMR, on port 4321. Bun runs it unless `vitePlugins` are the app's only transforms. `--no-indicator` hides the in-page issues badge.
|
|
8005
8109
|
|
|
8006
|
-
`nifra build [--target <t>] [--out <dir>] [--report]`
|
|
8110
|
+
`nifra build [--target <t>] [--out <dir>] [--transport <module>] [--prerender <paths>] [--report]`
|
|
8007
8111
|
|
|
8008
|
-
A complete deploy directory for `bun`, `node`, `deno`, `cloudflare`, `vercel` or `static`. The server entry is generated from `backend/framework.ts`, `backend/app.ts` and `routes/`. `--report` prints each chunk's size.
|
|
8112
|
+
A complete deploy directory for `bun`, `node`, `deno`, `cloudflare`, `vercel` or `static`. The server entry is generated from `backend/framework.ts`, `backend/app.ts` and `routes/`. `--report` prints each chunk's size. `--transport` builds the client to reach its server through that module's `fetch`, for a native shell. `--prerender` (static target) also prerenders the named pages, comma-separated, whether or not their route exports `prerender`.
|
|
8009
8113
|
|
|
8010
8114
|
`nifra start [--port <n>] [--out <dir>]`
|
|
8011
8115
|
|
|
@@ -8163,6 +8267,10 @@ Print the project's route index and conventions for an agent's prompt.
|
|
|
8163
8267
|
|
|
8164
8268
|
Print the guided build-an-app path, or one step's goal, actions and check.
|
|
8165
8269
|
|
|
8270
|
+
`nifra shell tauri [--origin <url>] [--name <n>] [--id <bundle>] [--mobile android|ios|all] [--force]`
|
|
8271
|
+
|
|
8272
|
+
Add a Tauri 2 shell to an existing app: `frontend/shell-transport.ts` and a `src-tauri/` project that wraps a static build made with `--transport`. Writes files only and keeps any that exist unless `--force`; `--mobile` then runs `tauri android init` and `tauri ios init`. See native shells.
|
|
8273
|
+
|
|
8166
8274
|
`nifra init-agents [--force] [--sync-mcp]`
|
|
8167
8275
|
|
|
8168
8276
|
Add the agent files a new app ships (`.mcp.json`, the `AGENTS.md` sections, each agent's pointer) to an existing app. `--sync-mcp` re-pins the MCP launch to the installed nifra.
|
|
@@ -8268,8 +8376,8 @@ starts from unless nothing else, installed or declared, names one.
|
|
|
8268
8376
|
|
|
8269
8377
|
```bash
|
|
8270
8378
|
nifra upgrade --list # the release targets this CLI knows
|
|
8271
|
-
nifra upgrade 5.
|
|
8272
|
-
nifra upgrade 5.
|
|
8379
|
+
nifra upgrade 5.13.0 # dry-run: every planned edit, nothing written
|
|
8380
|
+
nifra upgrade 5.13.0 --write # apply; it verifies with nifra check once the target is installed
|
|
8273
8381
|
# if pins were already bumped by hand, add --from <installed-version>
|
|
8274
8382
|
bun install # install the new pins
|
|
8275
8383
|
nifra check # verify against them
|
|
@@ -11869,6 +11977,40 @@ delay is the time a load balancer gets to see that and route elsewhere. Register
|
|
|
11869
11977
|
`app.onStop(fn)` still runs after the drain, on Bun, Node and Deno alike.
|
|
11870
11978
|
`delayMs + drainMs` has to fit in the kill grace period.
|
|
11871
11979
|
|
|
11980
|
+
To stop at the end of a scope instead of on a signal, hold the handle with
|
|
11981
|
+
`await using`. The handle `listen()` returns, the one
|
|
11982
|
+
`serve()` returns on Node and Deno, and the worker `@nifrajs/jobs`
|
|
11983
|
+
starts all stop when the scope ends, on a throw or an early return too. On Bun, disposal runs
|
|
11984
|
+
the app's graceful `app.stop()` with its default options, not Bun's raw stop. On
|
|
11985
|
+
Node and Deno it calls the `stop()` of the `serve()` handle, also with
|
|
11986
|
+
default options, which runs the same `onStopping` and `onStop` hooks and
|
|
11987
|
+
drains the same way; it does not call `app.stop()`, which would leave that listener
|
|
11988
|
+
open. If a shutdown is already under way, disposal waits for that one rather than starting
|
|
11989
|
+
another. A `@nifrajs/cron` scheduler is synchronous, so it takes
|
|
11990
|
+
`using`.
|
|
11991
|
+
|
|
11992
|
+
```ts
|
|
11993
|
+
import { server } from "@nifrajs/core"
|
|
11994
|
+
import { createScheduler } from "@nifrajs/cron"
|
|
11995
|
+
import { createQueue } from "@nifrajs/jobs"
|
|
11996
|
+
|
|
11997
|
+
const app = server().get("/", () => "ok")
|
|
11998
|
+
const queue = createQueue()
|
|
11999
|
+
|
|
12000
|
+
export async function runUntilDone(done: Promise<void>): Promise<void> {
|
|
12001
|
+
await using running = app.listen(3000) // graceful stop() when the scope ends
|
|
12002
|
+
await using worker = queue.start() // stops polling, waits for jobs in flight
|
|
12003
|
+
using scheduler = createScheduler() // stop() when the scope ends
|
|
12004
|
+
scheduler.start()
|
|
12005
|
+
await done
|
|
12006
|
+
console.log(running.port, worker.running)
|
|
12007
|
+
}
|
|
12008
|
+
```
|
|
12009
|
+
|
|
12010
|
+
This needs a runtime and a TypeScript `lib` that know explicit resource management
|
|
12011
|
+
(`esnext.disposable`). Nifra's own declaration files reference that lib, so
|
|
12012
|
+
importing them is enough for the types.
|
|
12013
|
+
|
|
11872
12014
|
### Letting check and assure read the app
|
|
11873
12015
|
|
|
11874
12016
|
`nifra check`, `assure`, `routes` and the other commands that
|
|
@@ -12214,6 +12356,294 @@ Complete runnable references - a typed CRUD API you can diff your own against:
|
|
|
12214
12356
|
`examples/db-sqlite` (the same API on raw `bun:sqlite`, parameterized
|
|
12215
12357
|
queries, boundary validation).
|
|
12216
12358
|
|
|
12359
|
+
## Nifra - Browser end-to-end tests
|
|
12360
|
+
|
|
12361
|
+
> Browser tests for a Nifra app with typed paths, locators, retrying assertions, goal-driven agent steps and a replay cache that makes the second run model-free, with @nifrajs/e2e.
|
|
12362
|
+
|
|
12363
|
+
`@nifrajs/e2e` serves your app on an ephemeral port, drives it through a browser,
|
|
12364
|
+
and gives you typed paths, locators and retrying assertions. On top it adds goal-driven agent
|
|
12365
|
+
steps and a replay cache, so a test can say what it wants in words and still run model-free
|
|
12366
|
+
after the first pass. It is a library: use `bun:test`, Vitest or anything that
|
|
12367
|
+
runs `async` functions.
|
|
12368
|
+
|
|
12369
|
+
### A first test
|
|
12370
|
+
|
|
12371
|
+
```ts
|
|
12372
|
+
import { test } from "bun:test"
|
|
12373
|
+
import { server } from "@nifrajs/core/server"
|
|
12374
|
+
import { e2e, expect } from "@nifrajs/e2e"
|
|
12375
|
+
|
|
12376
|
+
const html = (body: string) => new Response(body, { headers: { "content-type": "text/html" } })
|
|
12377
|
+
|
|
12378
|
+
const app = server().get("/users/:id", (c) =>
|
|
12379
|
+
html(`<input aria-label="Nickname" value="ada"><h1>User ${c.params.id}`),
|
|
12380
|
+
)
|
|
12381
|
+
|
|
12382
|
+
test("a user page renders", async () => {
|
|
12383
|
+
await using t = await e2e(app)
|
|
12384
|
+
await t.open("/users/:id", { params: { id: 42 } }) // a path or param the app lacks fails typecheck
|
|
12385
|
+
await expect(t.screen.getByRole("heading")).toHaveText("User 42")
|
|
12386
|
+
await t.screen.getByLabel("Nickname").fill("grace")
|
|
12387
|
+
await expect(t).toHaveURL("/users/42")
|
|
12388
|
+
})
|
|
12389
|
+
```
|
|
12390
|
+
|
|
12391
|
+
`open()` takes only paths the app declares and requires their
|
|
12392
|
+
`:params`. `await using` closes the browser and the server at scope exit.
|
|
12393
|
+
Names and text match as a case-insensitive substring; pass `{ exact: true }` for
|
|
12394
|
+
a full match. A locator that matches several elements is an error for `tap`,
|
|
12395
|
+
`fill`, `press` and the one-element assertions, never a guess at the
|
|
12396
|
+
first. Assertions retry until `assertionTimeout` (5000 ms by default).
|
|
12397
|
+
|
|
12398
|
+
On a page that loads a module script, actions wait for `data-nifra-hydrated` on
|
|
12399
|
+
`<html>`, so a tap never lands before its handler exists. Pass
|
|
12400
|
+
`hydration: "off"` for a page that is not a hydrated Nifra page.
|
|
12401
|
+
|
|
12402
|
+
### Signed-in sessions
|
|
12403
|
+
|
|
12404
|
+
```ts
|
|
12405
|
+
import { server } from "@nifrajs/core/server"
|
|
12406
|
+
import { testSession } from "@nifrajs/testing"
|
|
12407
|
+
import { e2e, expect } from "@nifrajs/e2e"
|
|
12408
|
+
|
|
12409
|
+
const app = server().get("/me", (c) =>
|
|
12410
|
+
new Response(`<p>session ${c.cookies.sid ?? "none"}`, { headers: { "content-type": "text/html" } }),
|
|
12411
|
+
)
|
|
12412
|
+
|
|
12413
|
+
// Log in through the API, in process, then hand the cookie jar to the browser.
|
|
12414
|
+
const { cookies } = testSession<typeof app>(app)
|
|
12415
|
+
cookies.set("sid", "abc123")
|
|
12416
|
+
|
|
12417
|
+
await using t = await e2e(app, { cookies })
|
|
12418
|
+
await t.open("/me")
|
|
12419
|
+
await expect(t.screen.getByText("session abc123")).toBeVisible()
|
|
12420
|
+
```
|
|
12421
|
+
|
|
12422
|
+
### Agent steps
|
|
12423
|
+
|
|
12424
|
+
`t.agent.act` takes one goal; `assert` and `waitFor` take a
|
|
12425
|
+
statement; `extract` returns data your schema accepts. The model is a port you
|
|
12426
|
+
supply: `{ decide(request) }`. The request carries a pruned outline of the page
|
|
12427
|
+
(at most `snapshotChars`, 12000 by default) and what was already tried; the answer is
|
|
12428
|
+
a small JSON decision.
|
|
12429
|
+
|
|
12430
|
+
```ts
|
|
12431
|
+
import { server } from "@nifrajs/core/server"
|
|
12432
|
+
import { e2e, expect, Secret, type E2EModel } from "@nifrajs/e2e"
|
|
12433
|
+
|
|
12434
|
+
const html = (body: string) => new Response(body, { headers: { "content-type": "text/html" } })
|
|
12435
|
+
|
|
12436
|
+
// A sign-in form that lands on a dashboard, so a model has controls to find and a place to end.
|
|
12437
|
+
const app = server()
|
|
12438
|
+
.get("/login", () =>
|
|
12439
|
+
html(`<input aria-label="Email"><input aria-label="Password" type="password"><button onclick="location.href='/dashboard'">Sign in`),
|
|
12440
|
+
)
|
|
12441
|
+
.get("/dashboard", () => html("<h1>Welcome, Ada"))
|
|
12442
|
+
|
|
12443
|
+
// The seam to a model. Nothing here names a vendor: you call your provider inside decide().
|
|
12444
|
+
// doc-check: skip - the provider call is yours; a real adapter would await a model API here.
|
|
12445
|
+
declare const askMyModel: (request: unknown) => Promise<unknown>
|
|
12446
|
+
const model: E2EModel = { decide: (request) => askMyModel(request) }
|
|
12447
|
+
|
|
12448
|
+
await using t = await e2e(app, { model, agentContext: "Acme calls a plan a seat" })
|
|
12449
|
+
await t.open("/login")
|
|
12450
|
+
|
|
12451
|
+
await t.agent.act("sign in as {email} with {password}", {
|
|
12452
|
+
params: { email: "ada@example.test", password: new Secret("password", process.env.PASSWORD ?? "") },
|
|
12453
|
+
})
|
|
12454
|
+
await t.agent.assert("the dashboard greets Ada")
|
|
12455
|
+
await expect(t.screen.getByRole("heading")).toHaveText("Welcome, Ada") // locators and agent steps mix in one test
|
|
12456
|
+
```
|
|
12457
|
+
|
|
12458
|
+
```ts
|
|
12459
|
+
// What decide() receives and returns, by request kind.
|
|
12460
|
+
//
|
|
12461
|
+
// act { goal, snapshot, history, secrets, handoff, ... }
|
|
12462
|
+
// -> { kind: "tap" | "fill" | "press", target, value?, key? }
|
|
12463
|
+
// | { kind: "goto", path } | { kind: "done", summary? } | { kind: "fail", reason }
|
|
12464
|
+
// assert { statement, snapshot } -> { pass: true | false | "inconclusive", reason }
|
|
12465
|
+
// waitFor { statement, snapshot } -> { pass, reason }
|
|
12466
|
+
// extract { prompt, snapshot } -> { value } (validated with your Standard Schema)
|
|
12467
|
+
//
|
|
12468
|
+
// target is one of { role, name? } | { label } | { text } | { testId }, plus exact? and nth?.
|
|
12469
|
+
// doc-check: skip - protocol sketch, not code.
|
|
12470
|
+
```
|
|
12471
|
+
|
|
12472
|
+
-
|
|
12473
|
+
**Budgets.** `act` stops at `maxModelCalls` (12),
|
|
12474
|
+
`maxActions` (25) or `timeout` (120000 ms), each its own error. A tap
|
|
12475
|
+
that finds nothing, or several, goes back to the model, which can retry with
|
|
12476
|
+
`nth`. The time limit is checked between decisions: it does not interrupt a
|
|
12477
|
+
`decide()` call that never resolves, so enforce your own deadline inside
|
|
12478
|
+
`decide` (the request carries a `signal`).
|
|
12479
|
+
|
|
12480
|
+
-
|
|
12481
|
+
**Secrets.** A `Secret` is typed into the page by name. The model sees
|
|
12482
|
+
`{password}`, never the value, and every outline a session sends has any secret
|
|
12483
|
+
it was given replaced by its placeholder.
|
|
12484
|
+
|
|
12485
|
+
-
|
|
12486
|
+
**Same origin.** `goto` takes an app path only; anything else is refused and
|
|
12487
|
+
reported back to the model.
|
|
12488
|
+
|
|
12489
|
+
-
|
|
12490
|
+
**No model.** Agent steps reject with `MODEL_MISSING`; locator tests need
|
|
12491
|
+
none.
|
|
12492
|
+
|
|
12493
|
+
```ts
|
|
12494
|
+
import { server } from "@nifrajs/core/server"
|
|
12495
|
+
import { t as s } from "@nifrajs/schema"
|
|
12496
|
+
import { e2e } from "@nifrajs/e2e"
|
|
12497
|
+
|
|
12498
|
+
const app = server().get("/plan", () => new Response("<p>Pro, 5 seats", {
|
|
12499
|
+
headers: { "content-type": "text/html" },
|
|
12500
|
+
}))
|
|
12501
|
+
|
|
12502
|
+
const model = {
|
|
12503
|
+
decide: () => ({ value: { plan: "Pro", seats: 5 } }), // a real model reads the outline in the request
|
|
12504
|
+
}
|
|
12505
|
+
|
|
12506
|
+
await using t = await e2e(app, { model })
|
|
12507
|
+
await t.open("/plan")
|
|
12508
|
+
const plan = await t.agent.extract("the current plan and seat count", {
|
|
12509
|
+
schema: s.object({ plan: s.string(), seats: s.number() }),
|
|
12510
|
+
})
|
|
12511
|
+
plan.seats // number
|
|
12512
|
+
```
|
|
12513
|
+
|
|
12514
|
+
### The replay cache
|
|
12515
|
+
|
|
12516
|
+
The first run of an `act` asks the model. The next runs replay what it did, with no
|
|
12517
|
+
model call. A recording is written only after a **later** locator assertion,
|
|
12518
|
+
`agent.assert` or `agent.waitFor` passes, and a failing one deletes it, so
|
|
12519
|
+
a test should end with a check.
|
|
12520
|
+
|
|
12521
|
+
```ts
|
|
12522
|
+
import { server } from "@nifrajs/core/server"
|
|
12523
|
+
import { e2e, expect, formatReport, unique, type E2EModel } from "@nifrajs/e2e"
|
|
12524
|
+
|
|
12525
|
+
const app = server().get("/signup", () => new Response("<h1>Sign up", {
|
|
12526
|
+
headers: { "content-type": "text/html" },
|
|
12527
|
+
}))
|
|
12528
|
+
const model: E2EModel = { decide: () => ({ kind: "done" }) }
|
|
12529
|
+
|
|
12530
|
+
await using t = await e2e(app, {
|
|
12531
|
+
model,
|
|
12532
|
+
cache: { testId: "a visitor signs up", mode: "read-write" },
|
|
12533
|
+
})
|
|
12534
|
+
await t.open("/signup")
|
|
12535
|
+
|
|
12536
|
+
// unique() keeps a run-varying value from missing the cache; it is typed again on replay.
|
|
12537
|
+
await t.agent.act("sign up as {email}", { params: { email: unique(`ada-${Date.now()}@example.test`) } })
|
|
12538
|
+
|
|
12539
|
+
// The recording is written only when a LATER check passes.
|
|
12540
|
+
await expect(t.screen.getByRole("heading")).toBeVisible()
|
|
12541
|
+
console.log(formatReport(t.agent.report())) // Cache: 1 replayed - 0 handed off - 0 missed (0 model calls)
|
|
12542
|
+
```
|
|
12543
|
+
|
|
12544
|
+
-
|
|
12545
|
+
**Replay** needs the same start route, finds each control by the role, name, label, text
|
|
12546
|
+
or test id it had and requires the same number of matches, then checks the end route and the
|
|
12547
|
+
controls and headings that appeared or went away. It never taps a near miss.
|
|
12548
|
+
|
|
12549
|
+
-
|
|
12550
|
+
**Hand-off.** When a replay stops (`target-not-found`, `ambiguous`,
|
|
12551
|
+
`end-mismatch`, `action-uncertain`, `route-mismatch`) the
|
|
12552
|
+
actions already done stay done, and the model finishes the goal from the current page.
|
|
12553
|
+
`request.handoff` carries the reason. A passing check re-records the repaired steps;
|
|
12554
|
+
if the goal then fails, the recording is deleted.
|
|
12555
|
+
|
|
12556
|
+
-
|
|
12557
|
+
**The key** is `cache.testId` (set it: two tests that say the same thing
|
|
12558
|
+
otherwise share a recording), the goal text, the params, `agentContext` and
|
|
12559
|
+
`cache.appIdentity`. A `unique()` value and a `Secret` do not
|
|
12560
|
+
change it, and a secret is stored by name only.
|
|
12561
|
+
|
|
12562
|
+
-
|
|
12563
|
+
**Where.** `.nifra/e2e-cache/<hash>.json` under the working directory,
|
|
12564
|
+
small and meant to be committed. `cache.store` swaps in your own;
|
|
12565
|
+
`cache: false` turns the cache off.
|
|
12566
|
+
|
|
12567
|
+
#### Modes
|
|
12568
|
+
|
|
12569
|
+
Mode
|
|
12570
|
+
Replays
|
|
12571
|
+
Writes and deletes
|
|
12572
|
+
Stale or missing recording
|
|
12573
|
+
|
|
12574
|
+
`read-write`
|
|
12575
|
+
|
|
12576
|
+
yes
|
|
12577
|
+
yes
|
|
12578
|
+
hand-off to the model
|
|
12579
|
+
|
|
12580
|
+
`read-only` (default when `CI` is set)
|
|
12581
|
+
|
|
12582
|
+
yes
|
|
12583
|
+
no
|
|
12584
|
+
hand-off to the model
|
|
12585
|
+
|
|
12586
|
+
`strict`
|
|
12587
|
+
|
|
12588
|
+
yes
|
|
12589
|
+
no
|
|
12590
|
+
|
|
12591
|
+
`REPLAY_STALE` / `REPLAY_MISSING`, no model call
|
|
12592
|
+
|
|
12593
|
+
`off`
|
|
12594
|
+
|
|
12595
|
+
no
|
|
12596
|
+
no
|
|
12597
|
+
the model runs every time
|
|
12598
|
+
|
|
12599
|
+
```ts
|
|
12600
|
+
import { server } from "@nifrajs/core/server"
|
|
12601
|
+
import { e2e, expect } from "@nifrajs/e2e"
|
|
12602
|
+
|
|
12603
|
+
const html = (body: string) => new Response(body, { headers: { "content-type": "text/html" } })
|
|
12604
|
+
|
|
12605
|
+
const app = server()
|
|
12606
|
+
.get("/", () => html('<a href="/dashboard">Dashboard'))
|
|
12607
|
+
.get("/dashboard", () => html("<h1>Dashboard"))
|
|
12608
|
+
|
|
12609
|
+
// CI replays committed recordings and never writes or deletes them. With no model, a recording that
|
|
12610
|
+
// no longer matches fails with MODEL_MISSING; mode: "strict" fails with REPLAY_STALE (or
|
|
12611
|
+
// REPLAY_MISSING when nothing was recorded) instead of ever calling a model.
|
|
12612
|
+
await using t = await e2e(app, { cache: { testId: "home to dashboard", mode: "strict" } })
|
|
12613
|
+
await t.open("/")
|
|
12614
|
+
await t.agent.act("open the dashboard") // reads the committed recording; strict mode checks it here
|
|
12615
|
+
await expect(t.screen.getByRole("heading")).toHaveText("Dashboard")
|
|
12616
|
+
```
|
|
12617
|
+
|
|
12618
|
+
`t.agent.report()` returns `{ replayed, handedOff, missed, modelCalls }`
|
|
12619
|
+
and `formatReport` prints it. Token counts are not reported: the model port returns
|
|
12620
|
+
decisions, not usage. A cache store that refuses a write is counted, never thrown.
|
|
12621
|
+
|
|
12622
|
+
### Engines
|
|
12623
|
+
|
|
12624
|
+
`e2e()` uses Playwright by default. Install it yourself (`playwright` is
|
|
12625
|
+
an optional peer dependency) and a browser with `bunx playwright install chromium`.
|
|
12626
|
+
The engine is a six-method interface, so anything else plugs in with
|
|
12627
|
+
`e2e(app, { engine })`; `@nifrajs/e2e/fake` is an in-memory engine with
|
|
12628
|
+
the same locator rules, for testing code built on this package.
|
|
12629
|
+
|
|
12630
|
+
```ts
|
|
12631
|
+
import { server } from "@nifrajs/core/server"
|
|
12632
|
+
import { e2e } from "@nifrajs/e2e"
|
|
12633
|
+
import { fakeEngine } from "@nifrajs/e2e/fake"
|
|
12634
|
+
|
|
12635
|
+
const app = server().get("/", () => new Response("ok"))
|
|
12636
|
+
|
|
12637
|
+
// An in-memory engine with the same locator rules, for testing code built on this package.
|
|
12638
|
+
const { factory } = fakeEngine({ pages: { "/": [{ role: "heading", name: "Home", text: "Home" }] } })
|
|
12639
|
+
await using t = await e2e(app, { engine: factory, cache: false })
|
|
12640
|
+
await t.open("/")
|
|
12641
|
+
await t.screen.getByRole("heading").count()
|
|
12642
|
+
```
|
|
12643
|
+
|
|
12644
|
+
An agent step is only as safe as the app state behind it. Point it at a test database and an
|
|
12645
|
+
isolated fixture, never at production, and keep credentials in `Secret` params.
|
|
12646
|
+
|
|
12217
12647
|
## Nifra - Error codes
|
|
12218
12648
|
|
|
12219
12649
|
> Every error code Nifra recognises: what each one means and how to fix it. The dev overlay, nifra_explain and nifra_errors name these codes and link here.
|
|
@@ -12790,7 +13220,9 @@ app.use(idempotency({ store: new MemoryIdempotencyStore() }))
|
|
|
12790
13220
|
|
|
12791
13221
|
Stripe redelivers; users double-click. The `idempotency` middleware replays the
|
|
12792
13222
|
first response for a retried key instead of re-running the charge, and rejects a concurrent
|
|
12793
|
-
duplicate while the first is in flight.
|
|
13223
|
+
duplicate while the first is in flight. Cached response bodies are bounded to 1 MiB by
|
|
13224
|
+
default; set `maxBytes` explicitly for a different cap, and use a separate
|
|
13225
|
+
streaming endpoint for responses that should not be replayed.
|
|
12794
13226
|
|
|
12795
13227
|
### When to write a plugin
|
|
12796
13228
|
|
|
@@ -12808,6 +13240,47 @@ executor, with your typed nifra context passed to the resolvers. Subscriptions r
|
|
|
12808
13240
|
nifra's WebSocket lane with `graphql-ws`. The request body goes through the same
|
|
12809
13241
|
bounded parser as every other route, and the query text is never logged.
|
|
12810
13242
|
|
|
13243
|
+
### Supporting packages
|
|
13244
|
+
|
|
13245
|
+
The smaller integrations stay separate so a backend only pays for the surfaces it uses. They
|
|
13246
|
+
are all listed in the generated API reference and are
|
|
13247
|
+
discoverable from this guide:
|
|
13248
|
+
|
|
13249
|
+
-
|
|
13250
|
+
`@nifrajs/env` validates and coerces environment variables once at startup. It
|
|
13251
|
+
is edge-safe when you pass Workers bindings as `source`.
|
|
13252
|
+
|
|
13253
|
+
-
|
|
13254
|
+
`@nifrajs/proxy` forwards to one fixed upstream with SSRF-safe URL construction
|
|
13255
|
+
and hop-by-hop header hygiene.
|
|
13256
|
+
|
|
13257
|
+
-
|
|
13258
|
+
`@nifrajs/island-trigger` is the framework-neutral scheduling primitive behind
|
|
13259
|
+
web islands; most apps can use the higher-level `@nifrajs/web/islands` API.
|
|
13260
|
+
|
|
13261
|
+
-
|
|
13262
|
+
`@nifrajs/mcp-db` exposes an explicit SQLite table allowlist as fail-closed MCP
|
|
13263
|
+
schema tools, with read-only queries opt-in and bounded.
|
|
13264
|
+
|
|
13265
|
+
-
|
|
13266
|
+
`@nifrajs/agent-review` parses and composes bounded, content-free review reports
|
|
13267
|
+
for agent and CI tooling.
|
|
13268
|
+
|
|
13269
|
+
```ts
|
|
13270
|
+
import { defineEnv, env } from "@nifrajs/env"
|
|
13271
|
+
|
|
13272
|
+
export const config = defineEnv({
|
|
13273
|
+
DATABASE_URL: env.url(),
|
|
13274
|
+
PORT: env.port({ default: 3000 }),
|
|
13275
|
+
NODE_ENV: env.enum(["development", "production", "test"], { default: "development" }),
|
|
13276
|
+
})
|
|
13277
|
+
```
|
|
13278
|
+
|
|
13279
|
+
Keep the returned config frozen and server-only; never copy secret values into a
|
|
13280
|
+
`PUBLIC_*` client module. See
|
|
13281
|
+
Edge & bindings for the Workers binding form
|
|
13282
|
+
and Security for boundary rules.
|
|
13283
|
+
|
|
12811
13284
|
## Nifra - Keep-alive
|
|
12812
13285
|
|
|
12813
13286
|
> Pages that stay mounted after the visitor leaves them: back and forward bring back the same page, with its scroll, form input and component state, and a sign-out drops them in every tab.
|
|
@@ -13907,7 +14380,7 @@ the command below, which runs every recipe since its installed version. The 5.6
|
|
|
13907
14380
|
its imports and dependencies; the follow-up changes below need a hand.
|
|
13908
14381
|
|
|
13909
14382
|
```sh
|
|
13910
|
-
nifra upgrade 5.
|
|
14383
|
+
nifra upgrade 5.13.0
|
|
13911
14384
|
```
|
|
13912
14385
|
|
|
13913
14386
|
-
|
|
@@ -14339,8 +14812,8 @@ rendering and route model, see Loaders & actions
|
|
|
14339
14812
|
and Routing.
|
|
14340
14813
|
|
|
14341
14814
|
The current dynamic SSR snapshot reports Nifra at 18× the throughput
|
|
14342
|
-
of Next.js,
|
|
14343
|
-
2.
|
|
14815
|
+
of Next.js, 8.3× Nuxt, 5.3× SvelteKit, and
|
|
14816
|
+
2.8× SolidStart on the measured pages. See the
|
|
14344
14817
|
benchmark methodology and full results.
|
|
14345
14818
|
|
|
14346
14819
|
## Nifra - Migrate route by route
|
|
@@ -14418,6 +14891,270 @@ handler. Pass its fetch-compatible function directly. For a Workers-style handle
|
|
|
14418
14891
|
platform argument carries bindings and `waitUntil`; mounted code remains responsible
|
|
14419
14892
|
for its own framework-specific request and response conventions.
|
|
14420
14893
|
|
|
14894
|
+
## Nifra - Native shells
|
|
14895
|
+
|
|
14896
|
+
> Ship an app's pages inside Tauri, Capacitor or Electron while its server stays on the web: one transport for route data, actions, server functions and the live socket.
|
|
14897
|
+
|
|
14898
|
+
A shell wraps an app's pages in a native window or a phone app: Tauri, Capacitor and
|
|
14899
|
+
Electron all load the built client from disk. The pages then live on one origin, such as
|
|
14900
|
+
`tauri://localhost`, and the server on another. A `transport` tells
|
|
14901
|
+
the client where the server is. The router's requests, server functions, the live socket and
|
|
14902
|
+
the calls you make with `appFetch` go through it; a plain
|
|
14903
|
+
`fetch("/api/todos")` does not, and goes to the shell's own origin.
|
|
14904
|
+
|
|
14905
|
+
### Build the pages
|
|
14906
|
+
|
|
14907
|
+
Write the transport in a frontend file with `serverFetch` from
|
|
14908
|
+
`@nifrajs/web/shell`. A path-absolute URL, such as `/todos`, goes to
|
|
14909
|
+
`origin`. The headers are added to each request to that origin, and may be a
|
|
14910
|
+
function that returns them; a request to any other host goes out as written, without the
|
|
14911
|
+
headers or credentials, so a call to a third party never carries your token.
|
|
14912
|
+
|
|
14913
|
+
```ts
|
|
14914
|
+
// frontend/transport.ts
|
|
14915
|
+
import { serverFetch } from "@nifrajs/web/shell"
|
|
14916
|
+
|
|
14917
|
+
export default serverFetch({ origin: "https://api.example.com" })
|
|
14918
|
+
```
|
|
14919
|
+
|
|
14920
|
+
With a token that refreshes:
|
|
14921
|
+
|
|
14922
|
+
```ts
|
|
14923
|
+
// frontend/transport.ts
|
|
14924
|
+
import { serverFetch } from "@nifrajs/web/shell"
|
|
14925
|
+
|
|
14926
|
+
export default serverFetch({
|
|
14927
|
+
origin: "https://api.example.com",
|
|
14928
|
+
// A bearer token that refreshes: the function runs before each request.
|
|
14929
|
+
headers: async () => ({ authorization: `Bearer ${await currentToken()}` }),
|
|
14930
|
+
})
|
|
14931
|
+
|
|
14932
|
+
// The app's own: where it keeps and refreshes its token.
|
|
14933
|
+
declare function currentToken(): Promise<string>
|
|
14934
|
+
```
|
|
14935
|
+
|
|
14936
|
+
An app that signs in with a cookie sets `credentials: "include"`. The
|
|
14937
|
+
server then needs `credentials: true` in its CORS options and a cookie a
|
|
14938
|
+
cross-site request may carry (`SameSite=None; Secure`), and a web view may
|
|
14939
|
+
restrict cookies further, so a bearer token is the safer choice in a shell.
|
|
14940
|
+
|
|
14941
|
+
```ts
|
|
14942
|
+
// doc-check: skip - a fragment of frontend/transport.ts.
|
|
14943
|
+
export default serverFetch({ origin: "https://api.example.com", credentials: "include" })
|
|
14944
|
+
```
|
|
14945
|
+
|
|
14946
|
+
Pass the module to the shell build with `--transport`, and name the pages the
|
|
14947
|
+
shell opens on with
|
|
14948
|
+
`--prerender` (comma-separated paths such as `/,/about`). Only this
|
|
14949
|
+
build prerenders them, so a web build of the same app keeps rendering them per request; a
|
|
14950
|
+
route that exports `prerender = true` is included too. A path that is not a
|
|
14951
|
+
static page route fails the build.
|
|
14952
|
+
|
|
14953
|
+
```bash
|
|
14954
|
+
nifra build --target static --out dist --prerender /
|
|
14955
|
+
```
|
|
14956
|
+
|
|
14957
|
+
`transport` in the framework config does the same for an app that ships nothing
|
|
14958
|
+
but the shell. It applies to every build, and `--transport` replaces it but
|
|
14959
|
+
cannot turn it off, so leave it out of an app that also ships a web build.
|
|
14960
|
+
|
|
14961
|
+
```ts
|
|
14962
|
+
// doc-check: skip - nifra.config.ts for the CLI; the adapter is the app's own.
|
|
14963
|
+
// Only for an app that ships nothing but the shell: it applies to every build.
|
|
14964
|
+
export { adapter } from "./backend/framework"
|
|
14965
|
+
export const clientModule = "@nifrajs/web-react/client"
|
|
14966
|
+
export const transport = "./frontend/transport.ts"
|
|
14967
|
+
```
|
|
14968
|
+
|
|
14969
|
+
The output has an `index.html` for each prerendered route and the assets they
|
|
14970
|
+
load, which is what a shell's `frontendDist` points at. The same option reaches
|
|
14971
|
+
the Bun and Vite builds and both dev servers, and `buildClient` takes it as
|
|
14972
|
+
`transport`.
|
|
14973
|
+
|
|
14974
|
+
A path the shell has no page for, such as a reload at `/todos`, hydrates the
|
|
14975
|
+
document the shell ships and then navigates to that URL. A prerendered page's route data
|
|
14976
|
+
loads from the bundle, except a navigation with a query string and, in an app with
|
|
14977
|
+
`live`, the loads the live endpoint makes: those go to the server.
|
|
14978
|
+
|
|
14979
|
+
`--transport` names the module for one run and wins over the config, so an app
|
|
14980
|
+
can ship a web build and a shell build from one tree. `--prerender` only
|
|
14981
|
+
applies to `--target static`:
|
|
14982
|
+
|
|
14983
|
+
```bash
|
|
14984
|
+
nifra build --target static --out dist-shell --transport ./frontend/shell-transport.ts --prerender /
|
|
14985
|
+
```
|
|
14986
|
+
|
|
14987
|
+
### What goes through the transport
|
|
14988
|
+
|
|
14989
|
+
- Route data, prefetches and action posts of the router.
|
|
14990
|
+
|
|
14991
|
+
-
|
|
14992
|
+
Server functions: the stub that stands in for a
|
|
14993
|
+
`*.fn.ts` module calls through it.
|
|
14994
|
+
|
|
14995
|
+
-
|
|
14996
|
+
The live socket and its load endpoint. The
|
|
14997
|
+
socket connects to the transport's origin.
|
|
14998
|
+
|
|
14999
|
+
-
|
|
15000
|
+
Keep-alive pages, layered over the
|
|
15001
|
+
transport.
|
|
15002
|
+
|
|
15003
|
+
-
|
|
15004
|
+
Your own calls, with `appFetch`. Hand it to a `useQuery` fetcher or
|
|
15005
|
+
to the typed client: `client(url, { fetch: appFetch })`.
|
|
15006
|
+
|
|
15007
|
+
```ts
|
|
15008
|
+
import { appFetch } from "@nifrajs/web/shell"
|
|
15009
|
+
|
|
15010
|
+
// The transport in a shell build, the global fetch anywhere else.
|
|
15011
|
+
export const todos = (): Promise<unknown> => appFetch("/api/todos").then((res) => res.json())
|
|
15012
|
+
```
|
|
15013
|
+
|
|
15014
|
+
### Let the server accept the shell
|
|
15015
|
+
|
|
15016
|
+
A shell's requests carry its own scheme as their `Origin`, so the server refuses
|
|
15017
|
+
them until it lists that origin. Tauri uses `tauri://localhost` on macOS, iOS
|
|
15018
|
+
and Linux and `http://tauri.localhost` on Windows and Android; Capacitor uses
|
|
15019
|
+
`capacitor://localhost` on iOS.
|
|
15020
|
+
|
|
15021
|
+
-
|
|
15022
|
+
CORS on the web app, as the `use` hook of `backend/framework.ts`:
|
|
15023
|
+
page data and form posts are its routes, not the backend's. List the headers the router
|
|
15024
|
+
reads in `exposedHeaders`; `"*"` is read literally by a credentialed
|
|
15025
|
+
request.
|
|
15026
|
+
|
|
15027
|
+
-
|
|
15028
|
+
A page action checks the route's own `allowedOrigins`, which replaces the
|
|
15029
|
+
same-origin default. Its second argument says whether that default would admit the
|
|
15030
|
+
request, so a route can keep its own site and add the shell.
|
|
15031
|
+
|
|
15032
|
+
-
|
|
15033
|
+
`serverFunctions` takes `origins`.
|
|
15034
|
+
|
|
15035
|
+
-
|
|
15036
|
+
`live` takes `trustedOrigins`, for the load endpoint and the
|
|
15037
|
+
socket.
|
|
15038
|
+
|
|
15039
|
+
```ts
|
|
15040
|
+
// backend/framework.ts: the web app answers page data and form posts, so the CORS goes on it.
|
|
15041
|
+
import { cors } from "@nifrajs/middleware"
|
|
15042
|
+
|
|
15043
|
+
const SHELLS = ["tauri://localhost", "http://tauri.localhost", "capacitor://localhost"]
|
|
15044
|
+
|
|
15045
|
+
export const use = (app: { use: (plugin: ReturnType<typeof cors>) => unknown }): void => {
|
|
15046
|
+
app.use(
|
|
15047
|
+
cors({
|
|
15048
|
+
origin: SHELLS,
|
|
15049
|
+
// Named, not "*": a credentialed request reads "*" as a header called "*".
|
|
15050
|
+
exposedHeaders: ["x-nifra-redirect", "x-nifra-status", "x-nifra-revalidate"],
|
|
15051
|
+
}),
|
|
15052
|
+
)
|
|
15053
|
+
}
|
|
15054
|
+
```
|
|
15055
|
+
|
|
15056
|
+
```ts
|
|
15057
|
+
// doc-check: skip - a route's backend half; the action is the app's own.
|
|
15058
|
+
// routes/todos.backend.ts
|
|
15059
|
+
// allowedOrigins replaces the same-origin default, so the site's own posts come in as sameOrigin.
|
|
15060
|
+
const SHELLS = ["tauri://localhost", "http://tauri.localhost", "capacitor://localhost"]
|
|
15061
|
+
|
|
15062
|
+
export const allowedOrigins = (origin: string | null, sameOrigin: boolean): boolean =>
|
|
15063
|
+
sameOrigin || (origin !== null && SHELLS.includes(origin))
|
|
15064
|
+
```
|
|
15065
|
+
|
|
15066
|
+
```ts
|
|
15067
|
+
import { serverFunctions } from "@nifrajs/web/fn"
|
|
15068
|
+
import * as todos from "./todos.fn.ts"
|
|
15069
|
+
|
|
15070
|
+
export const fns = serverFunctions("todos", todos, {
|
|
15071
|
+
origins: ["tauri://localhost", "http://tauri.localhost", "capacitor://localhost"],
|
|
15072
|
+
})
|
|
15073
|
+
```
|
|
15074
|
+
|
|
15075
|
+
```ts
|
|
15076
|
+
import { live } from "@nifrajs/web/live"
|
|
15077
|
+
|
|
15078
|
+
export const runtime = live({
|
|
15079
|
+
// Required in production, at least 32 bytes: set LIVE_SECRET (openssl rand -base64 32).
|
|
15080
|
+
secret: process.env.LIVE_SECRET ?? "",
|
|
15081
|
+
// Anonymous viewers. An app with accounts returns the session's user id, so a change of account
|
|
15082
|
+
// clears the client's cache and revoke(userId) reaches this viewer's grants.
|
|
15083
|
+
subject: () => null,
|
|
15084
|
+
trustedOrigins: ["tauri://localhost", "http://tauri.localhost", "capacitor://localhost"],
|
|
15085
|
+
})
|
|
15086
|
+
```
|
|
15087
|
+
|
|
15088
|
+
### Tauri 2
|
|
15089
|
+
|
|
15090
|
+
`nifra shell tauri` adds the transport and a `src-tauri/` project to
|
|
15091
|
+
an existing app. It writes files only, skips any that exist unless `--force`,
|
|
15092
|
+
and leaves `package.json` and the config alone. The project builds the static
|
|
15093
|
+
target with `--transport` into `dist-shell/`, and its content
|
|
15094
|
+
security policy allows the server's origin and socket. The window opens hidden and shows
|
|
15095
|
+
on the page's first paint, with a three second timer as the fallback, so there is no blank
|
|
15096
|
+
window while the web view starts.
|
|
15097
|
+
|
|
15098
|
+
```bash
|
|
15099
|
+
nifra shell tauri --origin https://api.example.com
|
|
15100
|
+
```
|
|
15101
|
+
|
|
15102
|
+
```bash
|
|
15103
|
+
bun add -d @tauri-apps/cli
|
|
15104
|
+
bunx tauri icon path/to/icon.png # a square PNG, 1024x1024 recommended
|
|
15105
|
+
bunx tauri dev
|
|
15106
|
+
```
|
|
15107
|
+
|
|
15108
|
+
A new site can start with a shell: `--shell tauri` adds the same project, lets
|
|
15109
|
+
the server's CORS accept the shell origins in `backend/shell-origins.ts`, and
|
|
15110
|
+
adds `shell:dev` and `shell:build` scripts.
|
|
15111
|
+
|
|
15112
|
+
```bash
|
|
15113
|
+
bun create nifra my-app --shell tauri
|
|
15114
|
+
cd my-app && bun install
|
|
15115
|
+
bunx tauri icon path/to/icon.png # a square PNG, 1024x1024 recommended
|
|
15116
|
+
bun run shell:dev
|
|
15117
|
+
```
|
|
15118
|
+
|
|
15119
|
+
`--mobile` also runs `tauri android init` or
|
|
15120
|
+
`tauri ios init` with the project's Tauri CLI, which needs the platform
|
|
15121
|
+
toolchain: the Android SDK and NDK, or Xcode. A platform that fails is reported and the
|
|
15122
|
+
files already written stay.
|
|
15123
|
+
|
|
15124
|
+
```bash
|
|
15125
|
+
nifra shell tauri --mobile android # or ios, or all
|
|
15126
|
+
```
|
|
15127
|
+
|
|
15128
|
+
By hand, point `frontendDist` at the build and allow the server in the content
|
|
15129
|
+
security policy. On iOS, a development server on a local address needs
|
|
15130
|
+
`NSAllowsLocalNetworking` in the app's `Info.plist`.
|
|
15131
|
+
|
|
15132
|
+
```json
|
|
15133
|
+
// doc-check: skip - src-tauri/tauri.conf.json
|
|
15134
|
+
{
|
|
15135
|
+
"build": { "frontendDist": "../dist-shell" },
|
|
15136
|
+
"app": { "security": { "csp": "default-src 'self'; connect-src 'self' https://api.example.com wss://api.example.com" } }
|
|
15137
|
+
}
|
|
15138
|
+
```
|
|
15139
|
+
|
|
15140
|
+
### Limits
|
|
15141
|
+
|
|
15142
|
+
-
|
|
15143
|
+
The transport module must sit in a zone: `frontend/`, or a package that
|
|
15144
|
+
declares `"nifra": { "environment": "frontend" }`.
|
|
15145
|
+
|
|
15146
|
+
-
|
|
15147
|
+
Tauri on macOS, the iOS Simulator and an Android emulator are what this was run on.
|
|
15148
|
+
Windows and Linux use the same transport but have not been run.
|
|
15149
|
+
|
|
15150
|
+
-
|
|
15151
|
+
On Android the web view draws under the status bar, so a page that starts at the top
|
|
15152
|
+
needs its own top padding.
|
|
15153
|
+
|
|
15154
|
+
-
|
|
15155
|
+
A page in a shell starts in the system web view, so its first paint comes after the
|
|
15156
|
+
view does.
|
|
15157
|
+
|
|
14421
15158
|
## Nifra - Tracing & observability
|
|
14422
15159
|
|
|
14423
15160
|
> One trace from the request through the cache, the job queue and the worker, plus the OpenTelemetry SDK's own database and HTTP spans, without bundling the SDK.
|
|
@@ -15391,7 +16128,7 @@ Nifra side: an implementation-level read of every security-relevant module acros
|
|
|
15391
16128
|
`core`, `middleware`, `auth`, `node`,
|
|
15392
16129
|
`web`, `uploads`, and `cli` - defaults and failure modes, not
|
|
15393
16130
|
marketing claims. Competitor side: current official documentation for defaults, verified as of
|
|
15394
|
-
|
|
16131
|
+
November 2026 against the published packages of Hono 4.13.17, Fastify 5.12.8, Express 5.3.0 / 4.22.3, and Elysia 1.4.31 (the Elysia 2.0 release candidate is not covered). The
|
|
15395
16132
|
comparison unit is what a developer gets **by default** and what the framework makes
|
|
15396
16133
|
**impossible to get wrong**, not what a maximally careful expert could configure - because
|
|
15397
16134
|
most real-world security incidents are misconfiguration-by-default.
|
|
@@ -15491,8 +16228,8 @@ O
|
|
|
15491
16228
|
ahead
|
|
15492
16229
|
|
|
15493
16230
|
CSRF
|
|
15494
|
-
B
|
|
15495
|
-
b (
|
|
16231
|
+
B core refuses cross-site form posts on every unsafe route; `csrf()` adds a signed double-submit
|
|
16232
|
+
b (Origin / Sec-Fetch-Site on form content types)
|
|
15496
16233
|
O token-based
|
|
15497
16234
|
3 (csurf deprecated)
|
|
15498
16235
|
-
|
|
@@ -15622,9 +16359,9 @@ ahead (unique)
|
|
|
15622
16359
|
Supply chain (runtime deps in core)
|
|
15623
16360
|
0
|
|
15624
16361
|
0
|
|
15625
|
-
|
|
15626
|
-
|
|
15627
|
-
|
|
16362
|
+
15
|
|
16363
|
+
28
|
|
16364
|
+
4
|
|
15628
16365
|
par with Hono
|
|
15629
16366
|
|
|
15630
16367
|
Machine-checkable route security evidence
|
|
@@ -15638,7 +16375,7 @@ B (`nifra assure`)
|
|
|
15638
16375
|
unique
|
|
15639
16376
|
|
|
15640
16377
|
Security lint (fail-open, non-constant-time, PII logs)
|
|
15641
|
-
B (NF-S001..
|
|
16378
|
+
B (NF-S001..009)
|
|
15642
16379
|
-
|
|
15643
16380
|
-
|
|
15644
16381
|
-
|
|
@@ -15689,9 +16426,10 @@ Nifra's `cors()` throws at construction when `credentials: true`
|
|
|
15689
16426
|
meets `origin: "*"`, never reflects an origin by default with credentials,
|
|
15690
16427
|
and lands headers on error responses too - it is **the only one of the five where the
|
|
15691
16428
|
dangerous configuration is a construction-time error**. The other four leave
|
|
15692
|
-
reflect-with-credentials configurable.
|
|
15693
|
-
|
|
15694
|
-
|
|
16429
|
+
reflect-with-credentials configurable. Core refuses a cross-site HTML form post to every
|
|
16430
|
+
unsafe route before the handler runs, and `csrf()` adds an Origin/Referer check
|
|
16431
|
+
plus a signed double-submit token, timing-safe; Hono's CSRF is an Origin /
|
|
16432
|
+
Sec-Fetch-Site check on form content types, Fastify's official token package is comparable, Express's
|
|
15695
16433
|
csurf is deprecated, and Elysia ships none in-tree. Uncaught errors become a flat 500
|
|
15696
16434
|
unconditionally - not env-gated - so no configuration leaks a stack to the wire; Express's
|
|
15697
16435
|
dev handler printing stacks when `NODE_ENV` is unset is the perennial
|
|
@@ -15742,7 +16480,7 @@ shedding; nothing else in the field defaults to an unspoofable key or fails clos
|
|
|
15742
16480
|
**Route assurance evidence.** Middleware publishes machine-checkable per-route claims
|
|
15743
16481
|
(`AUTHENTICATED`, `BODY_BOUNDED`, `CSRF`,
|
|
15744
16482
|
`RATE_LIMITED`, `IP_RESTRICTED`, `SECURITY_HEADERS`,
|
|
15745
|
-
`IDEMPOTENCY_KEY`), and `nifra assure` fails CI when a policy is unmet.
|
|
16483
|
+
`IDEMPOTENCY_KEY`, `DURABLE_COMMAND`, `RESPONSE_CONTRACT`), and `nifra assure` fails CI when a policy is unmet.
|
|
15746
16484
|
Evidence is honest: an escape hatch suppresses its own claim.
|
|
15747
16485
|
|
|
15748
16486
|
-
|
|
@@ -16351,6 +17089,8 @@ browser: `serveTestApp` binds the app to an ephemeral port,
|
|
|
16351
17089
|
`e2eUrl` typechecks the visited path against the route registry, and the
|
|
16352
17090
|
session jar crosses into the browser as a `Cookie` header. Bring Playwright
|
|
16353
17091
|
(or Vitest Browser) for the browser itself - nifra supplies the glue, not another runner.
|
|
17092
|
+
@nifrajs/e2e wraps that glue into locators, assertions, agent steps
|
|
17093
|
+
and a replay cache.
|
|
16354
17094
|
|
|
16355
17095
|
```ts
|
|
16356
17096
|
import { chromium } from "@playwright/test"
|
|
@@ -18645,6 +19385,78 @@ one cheap read instead of the whole corpus. For the wider framework: the repo's
|
|
|
18645
19385
|
[`llms-full.txt`](../../llms-full.txt) is the full machine-readable corpus. Run `nifra check` as the
|
|
18646
19386
|
done-gate, or `nifra mcp` to give the agent live project tools.
|
|
18647
19387
|
|
|
19388
|
+
## @nifrajs/deploy-fly
|
|
19389
|
+
|
|
19390
|
+
Build-time Fly.io packaging for a Nifra app using the existing Node deployment target. It emits a
|
|
19391
|
+
Dockerfile and `fly.toml`; it does not deploy, contact Fly.io, or manage credentials.
|
|
19392
|
+
|
|
19393
|
+
```sh
|
|
19394
|
+
bun add @nifrajs/deploy-fly @nifrajs/node
|
|
19395
|
+
```
|
|
19396
|
+
|
|
19397
|
+
```ts
|
|
19398
|
+
// backend/framework.ts: runtime-only exports imported by the generated server.
|
|
19399
|
+
import { reactAdapter } from "@nifrajs/web-react"
|
|
19400
|
+
export const adapter = reactAdapter
|
|
19401
|
+
export const clientModule = "@nifrajs/web-react/client"
|
|
19402
|
+
```
|
|
19403
|
+
|
|
19404
|
+
```ts
|
|
19405
|
+
// nifra.config.ts: CLI-only deployment configuration.
|
|
19406
|
+
import { flyAdapter } from "@nifrajs/deploy-fly"
|
|
19407
|
+
export { adapter, clientModule } from "./backend/framework.ts"
|
|
19408
|
+
|
|
19409
|
+
export const target = "fly"
|
|
19410
|
+
export const deploymentAdapter = {
|
|
19411
|
+
adapter: flyAdapter,
|
|
19412
|
+
options: { app: "my-nifra-app", primaryRegion: "iad" },
|
|
19413
|
+
}
|
|
19414
|
+
```
|
|
19415
|
+
|
|
19416
|
+
Create the Fly app once before its first deploy if it does not already exist, then build and deploy
|
|
19417
|
+
the emitted directory:
|
|
19418
|
+
|
|
19419
|
+
```sh
|
|
19420
|
+
fly apps create my-nifra-app
|
|
19421
|
+
nifra build
|
|
19422
|
+
cd dist
|
|
19423
|
+
fly deploy
|
|
19424
|
+
```
|
|
19425
|
+
|
|
19426
|
+
`app` is required. `primaryRegion`, `internalPort` (default `3000`), `forceHttps` (default `true`),
|
|
19427
|
+
`healthCheckPath`, and `image` (default `node:22-slim`) are optional. Pin `image` to an exact tag or
|
|
19428
|
+
a `node@sha256:<digest>` reference for reproducible images. Set `healthCheckPath` only when the app has a route that
|
|
19429
|
+
returns 2xx while ready. The adapter emits Fly service-level checks for that path; without it, no
|
|
19430
|
+
health endpoint is assumed. Put secrets in Fly secrets with `fly secrets set`; do not put them in
|
|
19431
|
+
adapter options. Nifra scans emitted adapter files for credentials before publishing the output.
|
|
19432
|
+
|
|
19433
|
+
The adapter uses Nifra's Node target, so the built server and its static assets stay under Nifra's
|
|
19434
|
+
normal build and portability checks. The adapter only adds deployment metadata. It supports ordinary
|
|
19435
|
+
SSR; it currently declares no support for the coordinated component pipeline.
|
|
19436
|
+
|
|
19437
|
+
The emitted Dockerfile sets `NODE_ENV=production`, and production refuses in-memory stores. An app
|
|
19438
|
+
that uses one (the site scaffold's rate limiter, for example) must configure a shared store or, for
|
|
19439
|
+
a single machine, allow it explicitly: the scaffold reads `NIFRA_ALLOW_MEMORY_RATE_LIMIT=true`, set
|
|
19440
|
+
with `fly secrets set`. An in-memory limiter keeps one quota per machine and
|
|
19441
|
+
`fly deploy` adds a spare machine by default. Deploy a new app with `fly deploy --ha=false`; for an
|
|
19442
|
+
app that already has several machines, run `fly scale count 1`. Confirm with `fly status`, or use a
|
|
19443
|
+
shared store.
|
|
19444
|
+
|
|
19445
|
+
The generated Node server derives `c.clientIp` from the socket peer only, which behind Fly's proxy
|
|
19446
|
+
is the proxy. Visitors then share one rate-limit bucket and get 429 once it is spent, and a shared
|
|
19447
|
+
store does not change the keying. Passing `header: "fly-client-ip"` to `rateLimit()` keys requests
|
|
19448
|
+
that arrive over HTTP per visitor, but only while Fly's proxy is the sole path to the app; behind a
|
|
19449
|
+
CDN or another proxy the header holds that proxy's address. The server's own page-loader calls to
|
|
19450
|
+
the backend carry no such header, so they stay keyed by the proxy peer. Raise `max` for an app that
|
|
19451
|
+
serves its pages from the same server.
|
|
19452
|
+
|
|
19453
|
+
Adapters execute as trusted build-time code in the Nifra CLI process. Their API is versioned, their
|
|
19454
|
+
output is declarative, and Nifra rejects unsafe paths, collisions, credential findings, and failed
|
|
19455
|
+
builds before replacing the last successful deploy directory.
|
|
19456
|
+
|
|
19457
|
+
For agent-oriented package guidance, see [`LLM.md`](./LLM.md) and the full
|
|
19458
|
+
[`llms-full.txt`](../../llms-full.txt) corpus.
|
|
19459
|
+
|
|
18648
19460
|
## @nifrajs/devtools
|
|
18649
19461
|
|
|
18650
19462
|
Interactive request-trace DevTools for `nifra dev`, with a secured SSE stream and browser overlay.
|
|
@@ -18682,6 +19494,135 @@ one cheap read instead of the whole corpus. For the wider framework: the repo's
|
|
|
18682
19494
|
[`llms-full.txt`](../../llms-full.txt) is the full machine-readable corpus. Run `nifra check` as the
|
|
18683
19495
|
done-gate, or `nifra mcp` to give the agent live project tools.
|
|
18684
19496
|
|
|
19497
|
+
## @nifrajs/e2e
|
|
19498
|
+
|
|
19499
|
+
Browser end-to-end tests for Nifra apps. It serves your app on an ephemeral port, drives it through
|
|
19500
|
+
a browser engine, and gives you locators and retrying assertions. The route table types `open()`.
|
|
19501
|
+
|
|
19502
|
+
```ts
|
|
19503
|
+
import { test } from "bun:test"
|
|
19504
|
+
import { e2e, expect } from "@nifrajs/e2e"
|
|
19505
|
+
import { app } from "../src/app"
|
|
19506
|
+
|
|
19507
|
+
test("a user page renders", async () => {
|
|
19508
|
+
await using t = await e2e(app)
|
|
19509
|
+
await t.open("/users/:id", { params: { id: 42 } }) // a path or param the app lacks fails typecheck
|
|
19510
|
+
await expect(t.screen.getByRole("heading")).toHaveText("User 42")
|
|
19511
|
+
await t.screen.getByLabel("Nickname").fill("grace")
|
|
19512
|
+
await expect(t).toHaveURL("/users/42")
|
|
19513
|
+
})
|
|
19514
|
+
```
|
|
19515
|
+
|
|
19516
|
+
`await using` closes the browser and the server at scope exit; `t.close()` does the same by hand.
|
|
19517
|
+
|
|
19518
|
+
## Locators and assertions
|
|
19519
|
+
|
|
19520
|
+
`getByRole(role, name)`, `getByLabel`, `getByText`, `getByTestId`, `.filter({ hasText })`, and chaining
|
|
19521
|
+
to scope into a row. Names and text match as a case-insensitive substring; pass `{ exact: true }` for
|
|
19522
|
+
a full match. Actions: `tap`, `fill`, `press`. Assertions retry until they pass or `assertionTimeout`
|
|
19523
|
+
(default 5000 ms) runs out: `toBeVisible`, `toBeHidden`, `toHaveText`, `toContainText`, `toHaveCount`,
|
|
19524
|
+
`toHaveValue`, `toHaveURL`, each with `.not`.
|
|
19525
|
+
|
|
19526
|
+
A locator that matches several elements is an error for `tap`, `fill`, `press` and the one-element
|
|
19527
|
+
assertions (`LOCATOR_AMBIGUOUS`, or a failed assertion naming the count).
|
|
19528
|
+
|
|
19529
|
+
## Hydration
|
|
19530
|
+
|
|
19531
|
+
On a page that loads a module script, the Playwright engine waits for `data-nifra-hydrated` on `<html>`
|
|
19532
|
+
after each navigation and before each action, so a tap does not land before its handler exists. Pass
|
|
19533
|
+
`hydration: "off"` for a page that is not a hydrated Nifra page.
|
|
19534
|
+
|
|
19535
|
+
## Signed-in sessions
|
|
19536
|
+
|
|
19537
|
+
```ts
|
|
19538
|
+
const { client, cookies } = testSession<typeof app>(app)
|
|
19539
|
+
await client.auth.login.post(credentials)
|
|
19540
|
+
await using t = await e2e(app, { cookies })
|
|
19541
|
+
```
|
|
19542
|
+
|
|
19543
|
+
## Agent steps
|
|
19544
|
+
|
|
19545
|
+
Say what you want in words; a model you supply drives the page. Locators and agent steps mix in one test.
|
|
19546
|
+
|
|
19547
|
+
```ts
|
|
19548
|
+
await using t = await e2e(app, { model, agentContext: "Acme calls a plan a seat" })
|
|
19549
|
+
await t.open("/login")
|
|
19550
|
+
await t.agent.act("sign in as {email} with {password}", {
|
|
19551
|
+
params: { email: "ada@example.test", password: new Secret("password", process.env.PASSWORD ?? "") },
|
|
19552
|
+
})
|
|
19553
|
+
await t.agent.assert("the dashboard greets Ada")
|
|
19554
|
+
await t.agent.waitFor("the export is finished", { timeout: 60_000 })
|
|
19555
|
+
const plan = await t.agent.extract("the current plan", { schema: PlanSchema })
|
|
19556
|
+
```
|
|
19557
|
+
|
|
19558
|
+
`model` is `{ decide(request) }`: the request carries the goal, a pruned outline of the page and what was
|
|
19559
|
+
already tried, and the answer is `{ kind: "tap" | "fill" | "press" | "goto" | "done" | "fail", ... }` for
|
|
19560
|
+
`act`, `{ pass, reason }` for `assert` and `waitFor`, and `{ value }` for `extract`. Nothing here names a
|
|
19561
|
+
vendor; you own the credentials, the retries and the cost. Without `model`, agent steps reject with
|
|
19562
|
+
`MODEL_MISSING` and locator tests are unaffected.
|
|
19563
|
+
|
|
19564
|
+
- **Budgets.** `act` stops at `maxModelCalls` (12), `maxActions` (25) or `timeout` (120000 ms), each a
|
|
19565
|
+
distinct error. The time limit is checked between decisions; it does not interrupt a `decide()` call
|
|
19566
|
+
that never resolves, so enforce a deadline inside `decide` (the request carries a `signal`). A tap that finds no element or several goes back to the model, which can retry with `nth`.
|
|
19567
|
+
- **Secrets.** A `Secret` is typed into the page by name. The model sees `{password}`, never the value,
|
|
19568
|
+
and every snapshot a session sends has any secret it was given replaced by its placeholder.
|
|
19569
|
+
- **Same origin.** `goto` takes an app path only; anything else is refused and reported to the model.
|
|
19570
|
+
- **`extract`** validates the answer with your Standard Schema (`EXTRACT_INVALID` on a mismatch).
|
|
19571
|
+
- **`waitFor`** asks the model again only when the page outline changed.
|
|
19572
|
+
|
|
19573
|
+
## Replay cache
|
|
19574
|
+
|
|
19575
|
+
The first run of an `act` asks the model; the next runs replay what it did, with no model call.
|
|
19576
|
+
|
|
19577
|
+
```ts
|
|
19578
|
+
await using t = await e2e(app, { model, cache: { testId: "member upgrades to Pro" } })
|
|
19579
|
+
await t.open("/settings/billing")
|
|
19580
|
+
await t.agent.act("upgrade the workspace to the {plan} plan", { params: { plan: "Pro" } })
|
|
19581
|
+
await expect(t.screen.getByRole("status")).toHaveText("Pro") // a later check that passes saves the recording
|
|
19582
|
+
console.log(formatReport(t.agent.report())) // Cache: 1 replayed - 0 handed off - 0 missed (0 model calls)
|
|
19583
|
+
```
|
|
19584
|
+
|
|
19585
|
+
- **Written only after a later check passes.** A locator `expect` or `agent.assert`/`waitFor` that passes
|
|
19586
|
+
saves what the `act`s before it recorded. One that fails deletes them. A test that ends with no check
|
|
19587
|
+
records nothing.
|
|
19588
|
+
- **Replay** needs the same start route, finds each control by the role, name, label, text or test id
|
|
19589
|
+
it had, and requires it to match exactly as often as it did. It never taps a near miss. It then checks
|
|
19590
|
+
the end route and the controls and headings that appeared or went away.
|
|
19591
|
+
- **Hand-off.** When a replay stops (`target-not-found`, `ambiguous`, `end-mismatch`, `action-uncertain`,
|
|
19592
|
+
`route-mismatch`) the actions it already did stay done, and the model finishes the goal from the current
|
|
19593
|
+
page. `ActRequest.handoff` carries the reason and how many actions to leave alone. A passing check
|
|
19594
|
+
re-records the repaired steps. If the goal then fails, the recording is deleted.
|
|
19595
|
+
- **Modes** (`cache.mode`): `read-write` (default; `read-only` when `CI` is set), `read-only` (replays and
|
|
19596
|
+
hands off, never writes or deletes), `strict` (replay only: a stale recording is `REPLAY_STALE`, a
|
|
19597
|
+
missing one `REPLAY_MISSING`), `off`.
|
|
19598
|
+
- **The key** is the test (`cache.testId`: pass it), the goal text, the params, `agentContext`, and
|
|
19599
|
+
`cache.appIdentity`. Wrap run-varying data in `unique(value)` so a new value does not miss; it is
|
|
19600
|
+
typed again on replay. A `Secret` is stored by name only, never by value.
|
|
19601
|
+
- **Where.** `.nifra/e2e-cache/<hash>.json` under the working directory (`cache.dir`), small and meant to
|
|
19602
|
+
be committed. `cache.store` swaps in your own store; `memoryCacheStore()` is one for tests. A store
|
|
19603
|
+
that refuses a write is counted in the cache, not thrown. `cache: false` turns the whole thing off.
|
|
19604
|
+
- Replay needs no model: in CI `model` can be left out and a stale recording then fails with
|
|
19605
|
+
`MODEL_MISSING`. `assert`, `waitFor` and `extract` always ask the model.
|
|
19606
|
+
|
|
19607
|
+
## Engines
|
|
19608
|
+
|
|
19609
|
+
`e2e()` uses the Playwright engine by default; install `playwright` yourself (optional peer). The engine
|
|
19610
|
+
is a six-method interface (`goto`, `url`, `resolve`, `act`, `snapshot`, `close`), so anything else can plug in with
|
|
19611
|
+
`e2e(app, { engine })`. `@nifrajs/e2e/fake` is an in-memory engine with the same locator semantics, for
|
|
19612
|
+
testing code built on this package without a browser.
|
|
19613
|
+
|
|
19614
|
+
## Not included yet
|
|
19615
|
+
|
|
19616
|
+
Token counts (the model port returns decisions, not usage) and a test runner. Use `bun:test` or Vitest.
|
|
19617
|
+
|
|
19618
|
+
## For AI agents
|
|
19619
|
+
|
|
19620
|
+
Start with [`LLM.md`](./LLM.md) - this package's contract card (the exports you call + its footguns),
|
|
19621
|
+
one cheap read instead of the whole corpus. For the wider framework: the repo's
|
|
19622
|
+
[`AGENTS.md`](../../AGENTS.md) is the copy-paste quick reference, and
|
|
19623
|
+
[`llms-full.txt`](../../llms-full.txt) is the full machine-readable corpus. Run `nifra check` as the
|
|
19624
|
+
done-gate, or `nifra mcp` to give the agent live project tools.
|
|
19625
|
+
|
|
18685
19626
|
## @nifrajs/edge
|
|
18686
19627
|
|
|
18687
19628
|
Compact fetch-handler server for edge and serverless runtimes - Cloudflare Workers, Vercel Edge, Deno Deploy, Bun. Keeps the `server().get().post()` DX and the full request trust boundary (bounded body read, Content-Length pre-reject, prototype-pollution guard, JSON / urlencoded framing) in a fraction of the bundle. Zero runtime dependencies beyond `@nifrajs/core`.
|
|
@@ -21492,10 +22433,18 @@ done-gate, or `nifra mcp` to give the agent live project tools.
|
|
|
21492
22433
|
|
|
21493
22434
|
`DenoServer`, `DenoStopOptions`, `FetchHandler`, `HashedFiles`, `ServeOptions`, `ServeStaticOptions`, `serve`, `serveStatic`
|
|
21494
22435
|
|
|
22436
|
+
## @nifrajs/deploy-fly
|
|
22437
|
+
|
|
22438
|
+
`FlyOptions`, `flyAdapter`
|
|
22439
|
+
|
|
21495
22440
|
## @nifrajs/devtools
|
|
21496
22441
|
|
|
21497
22442
|
`DevToolsClientOptions`, `DevToolsEvent`, `DevToolsOptions`, `DevToolsQuery`, `devtools`, `devtoolsClientScript`, `filterDevToolsEvents`
|
|
21498
22443
|
|
|
22444
|
+
## @nifrajs/e2e
|
|
22445
|
+
|
|
22446
|
+
`ActDecision`, `ActOptions`, `ActRequest`, `ActResult`, `ActionRecord`, `Agent`, `AgentReport`, `CacheEntry`, `CacheMode`, `CacheOptions`, `CacheStore`, `Clock`, `E2EError`, `E2EErrorCode`, `E2EModel`, `E2EOptions`, `E2ESession`, `ElementAction`, `ElementState`, `Engine`, `EngineFactory`, `EngineInit`, `ExtractOptions`, `ExtractRequest`, `HandOff`, `HydrationMode`, `JudgeRequest`, `Locator`, `LocatorContext`, `LocatorMatchers`, `MatcherOptions`, `ModelRequest`, `OpenArgs`, `OpenOptions`, `Param`, `Params`, `PathParams`, `PerformedDecision`, `RecordedAction`, `RoleOptions`, `Screen`, `Secret`, `Selector`, `Step`, `StepOptions`, `StopReason`, `Target`, `TextOptions`, `Unique`, `UrlMatchers`, `UrlSource`, `Verdict`, `WaitForOptions`, `e2e`, `expect`, `fileCacheStore`, `formatReport`, `memoryCacheStore`, `unique`
|
|
22447
|
+
|
|
21499
22448
|
## @nifrajs/edge
|
|
21500
22449
|
|
|
21501
22450
|
`EdgeContext`, `EdgeHandler`, `EdgeNotFound`, `EdgeOptions`, `EdgeServer`, `Method`, `NotFoundHandler`, `NotFoundInput`, `Params`, `QueryValue`, `StandardSchemaV1`, `notFound`, `server`, `toFetchHandler`
|