nitr-cli 0.0.0-beta.3

The Nitr command-line server binary.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
# SPDX-License-Identifier: MIT OR Apache-2.0
# This file is part of Nitr.
# See https://nitrweb.com/ for more information
# Copyright (C) 2024-present Jose Quintana <joseluisq.net>

# The single source of truth for the `nitr.*` API surface.
#
# Everything generated — the LuaCATS type definitions (`nitr-types.lua`)
# and the reference page (`resources/nitr-api.md`) — comes from this file, and a
# test walks the registered namespace to assert every entry is described
# here: adding a builtin without documenting it fails the build.
#
# Schema:
#   [[table]]  a namespace table (or userdata) mounted on `nitr`;
#              `call` makes it callable. `feature` is the `[std]` name
#              gating it ("" = always registered).
#   [[fn]]     a function; a `:` in the name declares a method on the
#              parent table or class. A trailing `?` on a param type marks
#              it optional.
#   [[class]]  a value type (request, response, handles) with fields;
#              its methods are `[[fn]]` entries named `Class:method`.

# ---------------------------------------------------------------- classes

[[class]]
name = "nitr.Request"
desc = "The incoming request, passed to every handler and middleware."
fields = [
  { name = "method", type = "string", desc = "Request method, uppercase (`\"GET\"`)." },
  { name = "path", type = "string", desc = "URI path (`\"/users/42\"`)." },
  { name = "params", type = "table<string, string>", desc = "Path parameters captured by the router (`:id` → `params.id`)." },
  { name = "query", type = "table<string, string>", desc = "Parsed query string; repeated keys keep the last value." },
  { name = "headers", type = "table<string, string>", desc = "Request headers, lowercase names." },
  { name = "id", type = "string", desc = "The request id (UUIDv7, echoed as `X-Request-ID`)." },
  { name = "remote_addr", type = "string", desc = "Peer address (`\"ip:port\"`)." },
  { name = "uri", type = "table", desc = "URI components: `scheme`, `host`, `port`, `path`, `authority`, `query`." },
  { name = "cookies", type = "nitr.RequestCookies", desc = "Parsed request cookies." },
]

[[fn]]
name = "nitr.Request:json"
desc = "Reads and decodes the body as JSON. Errors on an empty or invalid body."
returns = [{ type = "table" }]

[[fn]]
name = "nitr.Request:text"
desc = "Reads the whole body as a string."
returns = [{ type = "string" }]

[[fn]]
name = "nitr.Request:form"
desc = "Reads an `application/x-www-form-urlencoded` body as a table. The parse is cached, so middleware and handler can both call it."
returns = [{ type = "table<string, string>" }]

[[fn]]
name = "nitr.Request:multipart"
feature = "http"
desc = "Invokes `fn(part)` once per part of a `multipart/form-data` body, in arrival order; returns the part count. (Needs the `multipart` Cargo feature.)"
params = [{ name = "fn", type = "fun(part: nitr.Part)" }]
returns = [{ type = "integer", desc = "Number of parts seen." }]

[[fn]]
name = "nitr.Request:read"
desc = "Streams the body: the next chunk as it arrives, or at least `n` bytes. `nil` marks the end."
params = [{ name = "n", type = "integer?", desc = "Minimum bytes to gather." }]
returns = [{ type = "string|nil" }]

[[fn]]
name = "nitr.Request:accepts"
desc = "The best match among the given media types for the `Accept` header, or nil."
params = [{ name = "...", type = "string", desc = "Offered media types." }]
returns = [{ type = "string|nil" }]

[[fn]]
name = "nitr.Request:fresh"
desc = "Whether the client's cached copy is current, per `If-None-Match`/`If-Modified-Since`."
params = [
  { name = "etag", type = "string?", desc = "Entity tag of the current representation." },
  { name = "last_modified", type = "integer?", desc = "Unix seconds of the last change." },
]
returns = [{ type = "boolean" }]

[[class]]
name = "nitr.RequestCookies"
desc = "Cookies from the `Cookie` header: index by name (`req.cookies.session`)."

[[fn]]
name = "nitr.RequestCookies:verify"
desc = "The verified value of a signed cookie, or nil when missing or tampered."
params = [
  { name = "name", type = "string" },
  { name = "secret", type = "string" },
]
returns = [{ type = "string|nil" }]

[[class]]
name = "nitr.Response"
desc = "A response: `{ status, headers, body }` plus a cookie builder. Helpers build these; handlers may also build them by hand."
fields = [
  { name = "status", type = "integer", desc = "HTTP status code." },
  { name = "headers", type = "table<string, string>", desc = "Response headers." },
  { name = "body", type = "string|fun(...)|nil", desc = "Body string, or a streaming body function." },
  { name = "cookies", type = "nitr.ResponseCookies", desc = "Set-Cookie builder (attached by the helpers)." },
]

[[class]]
name = "nitr.ResponseCookies"
desc = "Builder for `Set-Cookie` headers on a response."

[[fn]]
name = "nitr.ResponseCookies:set"
desc = "Adds a cookie. Options: `http_only`, `secure`, `path`, `domain`, `max_age` (seconds), `same_site` (\"Strict\"/\"Lax\"/\"None\")."
params = [
  { name = "name", type = "string" },
  { name = "value", type = "string" },
  { name = "opts", type = "table?" },
]

[[fn]]
name = "nitr.ResponseCookies:set_signed"
desc = "Adds an HMAC-signed cookie, verifiable later with `req.cookies:verify`."
params = [
  { name = "name", type = "string" },
  { name = "value", type = "string" },
  { name = "secret", type = "string" },
  { name = "opts", type = "table?" },
]

[[class]]
name = "nitr.App"
desc = "The application: routes, middleware, error handling, static mounts. Return it from the handler script."

[[fn]]
name = "nitr.App:get"
desc = "Registers a GET route: `middleware..., handler` plus an optional trailing `{ on_error = fn }`. Paths take `:name` parameters and a trailing `*` catch-all."
params = [
  { name = "path", type = "string" },
  { name = "...", type = "fun(req: nitr.Request): nitr.Response|table" },
]

[[fn]]
name = "nitr.App:post"
desc = "Registers a POST route (see `get`)."
params = [
  { name = "path", type = "string" },
  { name = "...", type = "fun(req: nitr.Request): nitr.Response|table" },
]

[[fn]]
name = "nitr.App:put"
desc = "Registers a PUT route (see `get`)."
params = [
  { name = "path", type = "string" },
  { name = "...", type = "fun(req: nitr.Request): nitr.Response|table" },
]

[[fn]]
name = "nitr.App:delete"
desc = "Registers a DELETE route (see `get`)."
params = [
  { name = "path", type = "string" },
  { name = "...", type = "fun(req: nitr.Request): nitr.Response|table" },
]

[[fn]]
name = "nitr.App:patch"
desc = "Registers a PATCH route (see `get`)."
params = [
  { name = "path", type = "string" },
  { name = "...", type = "fun(req: nitr.Request): nitr.Response|table" },
]

[[fn]]
name = "nitr.App:head"
desc = "Registers a HEAD route (see `get`). Without one, HEAD reuses the GET route with the body stripped."
params = [
  { name = "path", type = "string" },
  { name = "...", type = "fun(req: nitr.Request): nitr.Response|table" },
]

[[fn]]
name = "nitr.App:options"
desc = "Registers an OPTIONS route (see `get`). Without one, OPTIONS answers 204 with `Allow`."
params = [
  { name = "path", type = "string" },
  { name = "...", type = "fun(req: nitr.Request): nitr.Response|table" },
]

[[fn]]
name = "nitr.App:use"
desc = "Adds app-wide middleware: a factory `fn(next) -> fn(req)`. Must be called before any route."
params = [{ name = "mw", type = "fun(next: fun): fun(req: nitr.Request): any" }]

[[fn]]
name = "nitr.App:on_error"
desc = "Sets the app-wide error handler: `fn(err, req)` where `err` is the structured error (`kind`, `message`, `source`, `line`, `traceback`, ...)."
params = [{ name = "handler", type = "fun(err: table, req: nitr.Request): nitr.Response|table" }]

[[fn]]
name = "nitr.App:static"
desc = "Mounts a static directory, served in Rust. Options: `{ spa = boolean, cache_control = string }`."
params = [
  { name = "mount", type = "string" },
  { name = "dir", type = "string" },
  { name = "opts", type = "table?" },
]

[[class]]
name = "nitr.Part"
desc = "One part of a multipart upload, delivered to the `req:multipart` callback."
fields = [
  { name = "name", type = "string|nil", desc = "Form field name." },
  { name = "filename", type = "string|nil", desc = "Client-supplied file name." },
  { name = "content_type", type = "string|nil", desc = "Part content type." },
]

[[fn]]
name = "nitr.Part:text"
desc = "Reads a non-file field as a string (bounded by `[limits] max_field_bytes`)."
returns = [{ type = "string" }]

[[fn]]
name = "nitr.Part:save"
desc = "Streams a file part to `path` without entering the Lua heap; returns the bytes written."
params = [{ name = "path", type = "string" }]
returns = [{ type = "integer" }]

[[fn]]
name = "nitr.Part:discard"
desc = "Drains and drops the part."

[[class]]
name = "nitr.FetchHandle"
desc = "An unsent outbound request from `nitr.fetch`; send it, or hand it to `nitr.await_all`."

[[fn]]
name = "nitr.FetchHandle:send"
desc = "Performs the request."
returns = [{ type = "nitr.FetchResponse" }]

[[class]]
name = "nitr.FetchResponse"
desc = "An outbound response."
fields = [
  { name = "status", type = "integer", desc = "HTTP status code." },
  { name = "headers", type = "table<string, string>", desc = "Response headers." },
  { name = "url", type = "string", desc = "Final URL after redirects." },
]

[[fn]]
name = "nitr.FetchResponse:text"
desc = "The body as a string (bounded by `[fetch] max_response_bytes`)."
returns = [{ type = "string" }]

[[fn]]
name = "nitr.FetchResponse:json"
desc = "The body decoded as JSON."
returns = [{ type = "table" }]

[[fn]]
name = "nitr.FetchResponse:read"
desc = "Streams the body chunk by chunk; `nil` at the end."
returns = [{ type = "string|nil" }]

[[class]]
name = "nitr.Schema"
desc = "A compiled validation schema from `nitr.validate.schema`."

[[fn]]
name = "nitr.Schema:check"
desc = "Validates a value. Returns the data (declared fields only) or nil plus `{ message, fields }` mapping each failing path to its message."
params = [{ name = "value", type = "any" }]
returns = [
  { type = "table|nil", desc = "Validated data." },
  { type = "table|nil", desc = "The error, when validation failed." },
]

[[class]]
name = "nitr.Session"
desc = "A stateless signed-cookie session from `nitr.session`. Assign fields directly (`session.user_id = 42`)."

[[fn]]
name = "nitr.Session:save"
desc = "Serializes the session into a signed cookie on the response. An empty session deletes the cookie."
params = [{ name = "resp", type = "nitr.Response|table" }]

[[fn]]
name = "nitr.Session:clear"
desc = "Removes every field; `save` then writes the deletion cookie."

[[class]]
name = "nitr.Tx"
desc = "A database transaction handle inside `nitr.db:transaction`; same query API as `nitr.db`, plus nesting via savepoints."

# ---------------------------------------------------------- response helpers

[[fn]]
name = "nitr.json"
feature = "json"
desc = "As a function: a JSON response (`nitr.json({ ok = true })`). Also the codec: `nitr.json:encode(v)` / `nitr.json:decode(s)`."
params = [
  { name = "value", type = "any" },
  { name = "status", type = "integer?", desc = "Status code (default 200)." },
]
returns = [{ type = "nitr.Response" }]
methods = [
  { name = "encode", params = [{ name = "value", type = "any" }], returns = [{ type = "string" }], desc = "Encodes a value as JSON." },
  { name = "decode", params = [{ name = "s", type = "string" }], returns = [{ type = "any" }], desc = "Decodes JSON; errors on invalid input." },
]

[[fn]]
name = "nitr.text"
feature = "http"
desc = "A `text/plain` response."
params = [
  { name = "body", type = "string" },
  { name = "status", type = "integer?" },
]
returns = [{ type = "nitr.Response" }]

[[fn]]
name = "nitr.html"
feature = "http"
desc = "A `text/html` response."
params = [
  { name = "body", type = "string" },
  { name = "status", type = "integer?" },
]
returns = [{ type = "nitr.Response" }]

[[fn]]
name = "nitr.redirect"
feature = "http"
desc = "A redirect (default 302)."
params = [
  { name = "location", type = "string" },
  { name = "status", type = "integer?" },
]
returns = [{ type = "nitr.Response" }]

[[fn]]
name = "nitr.status"
feature = "http"
desc = "An empty response with the given status."
params = [{ name = "code", type = "integer" }]
returns = [{ type = "nitr.Response" }]

[[fn]]
name = "nitr.negotiate"
feature = "http"
desc = "Picks the offer whose media type best matches the `Accept` header; function values are called with the request. No match answers 406."
params = [
  { name = "req", type = "nitr.Request" },
  { name = "offers", type = "table<string, any>" },
]
returns = [{ type = "any" }]

[[fn]]
name = "nitr.sse"
feature = "http"
desc = "A Server-Sent Events stream: `fn(send)` calls `send(event, data)`; table data is JSON-encoded."
params = [{ name = "fn", type = "fun(send: fun(event: string, data: any))" }]
returns = [{ type = "nitr.Response" }]

[[fn]]
name = "nitr.error"
feature = "http"
desc = "An error response: string body as text, table body as JSON."
params = [
  { name = "code", type = "integer" },
  { name = "body", type = "string|table|nil" },
]
returns = [{ type = "nitr.Response" }]

[[fn]]
name = "nitr.etag"
feature = "http"
desc = "A well-formed entity tag for whatever identifies the resource (a row version, an updated_at), to pair with `req:fresh`."
params = [
  { name = "value", type = "any" },
  { name = "weak", type = "boolean?" },
]
returns = [{ type = "string" }]

[[fn]]
name = "nitr.csrf"
feature = "http"
desc = "As a function: the CSRF middleware factory for `app:use` (signed double-submit cookie; unsafe methods must echo the token in `X-CSRF-Token` or a `_csrf` field). Options: `secret` (required), `cookie` (the cookie NAME, default `_csrf`), `header`, `field`, and `cookie_opts` (the cookie ATTRIBUTES, which extend the HttpOnly/SameSite=Lax defaults rather than replacing them; `http_only` cannot be un-set). Note `nitr.session` spells its attribute table `cookie` — here that key is the name."
params = [{ name = "opts", type = "table" }]
returns = [{ type = "fun" }]
methods = [
  { name = "token", dot = true, params = [{ name = "req", type = "nitr.Request" }], returns = [{ type = "string" }], desc = "The request's token, for a form or meta tag. Requires the middleware." },
]

[[fn]]
name = "nitr.session"
feature = "http"
desc = "Loads (or starts) the stateless signed-cookie session. Options: `secret` (required), `name` (the cookie name), `max_age`, and `cookie` (the cookie ATTRIBUTES table, which extends the HttpOnly/SameSite=Lax defaults; `http_only` cannot be un-set). Note `nitr.csrf` spells the attribute table `cookie_opts` and uses `cookie` for the name."
params = [
  { name = "req", type = "nitr.Request" },
  { name = "opts", type = "table" },
]
returns = [{ type = "nitr.Session" }]

# ------------------------------------------------------------------ modules

[[fn]]
name = "nitr.app"
desc = "Creates the application object the handler script must return."
returns = [{ type = "nitr.App" }]

[[fn]]
name = "nitr.errinfo"
desc = "Classifies a pcall-caught error into its structured form: `kind`, `message`, `source`, `line`, `traceback`, `cause`, `pretty`."
params = [{ name = "caught", type = "any" }]
returns = [{ type = "table" }]

[[fn]]
name = "nitr.dbg"
feature = "dbg"
desc = "Debug-prints a value (structure included) to the log; returns it unchanged."
params = [{ name = "value", type = "any" }]
returns = [{ type = "any" }]

[[fn]]
name = "nitr.fetch"
feature = "fetch"
desc = "An outbound HTTP request (SSRF-guarded, redirect-checked). Options: `headers`, `query`, `json`, `body`, `timeout`, `retry`. Returns an unsent handle."
params = [
  { name = "method", type = "string" },
  { name = "url", type = "string" },
  { name = "opts", type = "table?" },
]
returns = [{ type = "nitr.FetchHandle" }]

[[fn]]
name = "nitr.await_all"
feature = "fetch"
desc = "Runs fetch handles (and `db:query_async` handles) concurrently; returns their results in order."
params = [{ name = "handles", type = "table" }]
returns = [{ type = "table" }]

[[table]]
name = "nitr.template"
feature = "template"
desc = "The minijinja template engine, loading from `[templating] dir`."
methods = [
  { name = "render", params = [{ name = "name", type = "string" }, { name = "data", type = "table?" }], returns = [{ type = "string" }], desc = "Renders a template." },
]

[[table]]
name = "nitr.db"
feature = "db"
desc = "The SQLite database (`database` in nitr.toml): WAL, busy timeout, foreign keys on."
methods = [
  { name = "execute", params = [{ name = "sql", type = "string" }, { name = "params", type = "table?" }], returns = [{ type = "integer", desc = "Affected row count." }], desc = "Runs a statement." },
  { name = "query", params = [{ name = "sql", type = "string" }, { name = "params", type = "table?" }], returns = [{ type = "table[]" }], desc = "All rows, each a column→value table." },
  { name = "query_row", params = [{ name = "sql", type = "string" }, { name = "params", type = "table?" }], returns = [{ type = "table|nil" }], desc = "The first row, or nil." },
  { name = "query_one", params = [{ name = "sql", type = "string" }, { name = "params", type = "table?" }], returns = [{ type = "any" }], desc = "The first column of the first row." },
  { name = "transaction", params = [{ name = "fn", type = "fun(tx: nitr.Tx): any" }], returns = [{ type = "any" }], desc = "Runs `fn` atomically; rolls back on error. Nestable (savepoints). Use `tx`, not the outer `nitr.db`." },
  { name = "query_async", params = [{ name = "sql", type = "string" }, { name = "params", type = "table?" }, { name = "kind", type = "string?" }], returns = [{ type = "table", desc = "A pending handle for `nitr.await_all`." }], desc = "An unsent query to run alongside fetches." },
]

[[table]]
name = "nitr.log"
feature = "log"
desc = "Structured logging into the request span. Fields become real keys in JSON log output."
functions = [
  { name = "debug", params = [{ name = "msg", type = "string" }, { name = "fields", type = "table?" }], desc = "Debug-level record." },
  { name = "info", params = [{ name = "msg", type = "string" }, { name = "fields", type = "table?" }], desc = "Info-level record." },
  { name = "warn", params = [{ name = "msg", type = "string" }, { name = "fields", type = "table?" }], desc = "Warn-level record." },
  { name = "error", params = [{ name = "msg", type = "string" }, { name = "fields", type = "table?" }], desc = "Error-level record." },
]

[[table]]
name = "nitr.crypto"
feature = "crypto"
desc = "Crypto primitives (RustCrypto): compose them; never reimplement them in Lua."
functions = [
  { name = "sha256", params = [{ name = "data", type = "string" }], returns = [{ type = "string" }], desc = "SHA-256 digest, lowercase hex." },
  { name = "hmac_sha256", params = [{ name = "key", type = "string" }, { name = "data", type = "string" }], returns = [{ type = "string" }], desc = "HMAC-SHA256, lowercase hex." },
  { name = "random_bytes", params = [{ name = "n", type = "integer" }], returns = [{ type = "string" }], desc = "N random bytes from the OS (1..=65536)." },
  { name = "constant_time_eq", params = [{ name = "a", type = "string" }, { name = "b", type = "string" }], returns = [{ type = "boolean" }], desc = "Timing-safe comparison — what `==` on secrets is not." },
  { name = "password_hash", params = [{ name = "password", type = "string", desc = "At most 1024 bytes; longer is an error, never a silent truncation." }], returns = [{ type = "string" }], desc = "argon2id hash for storage (m=19456, t=2, p=1). Also available as `nitr hash-password`. ASYNC: the argon2 work runs on the blocking pool, so this yields — call it from a handler or middleware, never from the top level of a handler script (that chunk runs outside the async executor and a yield there fails). Store hashes minted by `nitr hash-password` instead of hashing at boot." },
  { name = "password_verify", params = [{ name = "password", type = "string" }, { name = "hash", type = "string" }], returns = [{ type = "boolean", desc = "Whether the password matches." }, { type = "string|nil", desc = "Set only when the *stored hash* is unusable (bcrypt, truncated, absurd parameters) — nil for an ordinary wrong password, including one over `max_password_bytes` (answered false before argon2 runs, never an error)." }], desc = "Verifies a password against a stored hash. Total on login input: no password, whatever its size, makes it raise. ASYNC: the argon2 work runs on the blocking pool, so this yields — call it from a handler or middleware, never from the top level of a handler script (that chunk runs outside the async executor and a yield there fails). Store hashes minted by `nitr hash-password` instead of hashing at boot." },
  { name = "password_verify_dummy", params = [{ name = "password", type = "string" }], returns = [{ type = "boolean", desc = "Always false." }], desc = "Spends one argon2 hash against a process-private decoy and returns false. Call it on the no-such-user branch of a login: skipping the hash there leaks your user list through response time. ASYNC: the argon2 work runs on the blocking pool, so this yields — call it from a handler or middleware, never from the top level of a handler script (that chunk runs outside the async executor and a yield there fails). Store hashes minted by `nitr hash-password` instead of hashing at boot." },
  { name = "seal", params = [{ name = "key", type = "string", desc = "Exactly 32 bytes." }, { name = "plaintext", type = "string" }, { name = "aad", type = "string?" }], returns = [{ type = "string" }], desc = "Authenticated encryption (XChaCha20-Poly1305); printable token." },
  { name = "open", params = [{ name = "key", type = "string" }, { name = "sealed", type = "string" }, { name = "aad", type = "string?" }], returns = [{ type = "string|nil" }], desc = "Opens a sealed token; nil on any tampering." },
]
fields = [
  { name = "max_password_bytes", type = "integer", desc = "The password cap (1024 bytes): password_hash raises above it, password_verify answers a plain false. Read it to size a registration field — `#pw > nitr.crypto.max_password_bytes` is the pre-check that turns password_hash's error into a 400." },
]

[[table]]
name = "nitr.crypto.jwt"
feature = "crypto"
desc = "HMAC JWTs (HS256/384/512). Verification demands an explicit algorithm allow-list and checks `exp`/`nbf` when present. It does NOT check `iss`, `aud` or `typ` — those are the caller's job — and a token with no `exp` never expires. See docs-feat/jwt.md."
functions = [
  { name = "sign", params = [{ name = "claims", type = "table" }, { name = "key", type = "string" }, { name = "opts", type = "table?", desc = "`{ alg = \"HS256\" }`" }], returns = [{ type = "string" }], desc = "Signs a token." },
  { name = "verify", params = [{ name = "token", type = "string" }, { name = "key", type = "string" }, { name = "opts", type = "table", desc = "`{ algorithms = {...}, leeway? }` — the allow-list is required." }], returns = [{ type = "table|nil", desc = "The claims." }, { type = "string|nil", desc = "The rejection reason." }], desc = "Verifies the signature, the `alg` against the allow-list, and `exp`/`nbf` if the token carries them. Checks no other claim: compare `iss`/`aud` yourself, and require `exp` if your tokens must expire (`aud` may be a string or an array)." },
]

[[table]]
name = "nitr.auth"
feature = "crypto"
desc = "`Authorization` header parsing."
functions = [
  { name = "basic", params = [{ name = "req", type = "nitr.Request|string" }], returns = [{ type = "string|nil", desc = "User." }, { type = "string|nil", desc = "Password." }], desc = "Basic credentials, or nil. Credentials must be UTF-8, and the scheme must be separated from the value by at least one space (extra spaces are trimmed; a tab is not a separator). Anything unparseable reads as no credentials, never a partial one." },
  { name = "bearer", params = [{ name = "req", type = "nitr.Request|string" }], returns = [{ type = "string|nil" }], desc = "The bearer token, or nil. Compare it with nitr.crypto.constant_time_eq, not ==; a length mismatch still returns early, so a variable-length secret is compared as a digest. See examples/bearer-auth." },
]

[[table]]
name = "nitr.cache"
feature = "cache"
desc = "The bounded TTL+LRU cache shared by every state. Entries are plain data; per-process, so not a session store."
methods = [
  { name = "get", params = [{ name = "key", type = "string" }], returns = [{ type = "any" }], desc = "The cached value, or nil." },
  { name = "set", params = [{ name = "key", type = "string" }, { name = "value", type = "any" }, { name = "ttl", type = "integer?" }], desc = "Stores a value (ttl in seconds)." },
  { name = "delete", params = [{ name = "key", type = "string" }], desc = "Removes a key." },
  { name = "clear", desc = "Empties the cache." },
  { name = "remember", params = [{ name = "key", type = "string" }, { name = "ttl", type = "integer|nil" }, { name = "fn", type = "fun(): any" }], returns = [{ type = "any" }], desc = "The cached value, or `fn()`'s result, stored and returned." },
  { name = "stats", returns = [{ type = "table" }], desc = "Hit/miss/entry counters." },
]

[[table]]
name = "nitr.time"
feature = "time"
desc = "Safe clocks and time formatting (UTC), so scripts never need the `os` library for a date."
functions = [
  { name = "now", returns = [{ type = "integer" }], desc = "Unix seconds." },
  { name = "monotonic", returns = [{ type = "number" }], desc = "Monotonic seconds (sub-second precision), for measuring durations." },
  { name = "format", params = [{ name = "ts", type = "integer" }, { name = "fmt", type = "string?", desc = "strftime; default ISO-like." }], returns = [{ type = "string" }], desc = "Formats a timestamp in UTC." },
  { name = "parse", params = [{ name = "value", type = "string" }, { name = "fmt", type = "string" }], returns = [{ type = "integer|nil" }, { type = "string|nil", desc = "Reason when nil." }], desc = "Parses per a strftime format (UTC unless the input carries an offset)." },
  { name = "http", params = [{ name = "ts", type = "integer" }], returns = [{ type = "string" }], desc = "IMF-fixdate (`Tue, 15 Nov 1994 08:12:31 GMT`)." },
  { name = "parse_http", params = [{ name = "value", type = "string" }], returns = [{ type = "integer|nil" }], desc = "Parses the three HTTP date forms." },
  { name = "iso8601", params = [{ name = "ts", type = "integer" }], returns = [{ type = "string" }], desc = "RFC 3339 in UTC (`1994-11-15T08:12:31Z`)." },
]

[[table]]
name = "nitr.validate"
feature = "validate"
desc = "Declarative validation, compiled once and checked in Rust."
functions = [
  { name = "schema", params = [{ name = "fields", type = "table", desc = "`{ name = { type = ..., ... } }` rules; unknown rule keys fail at load." }], returns = [{ type = "nitr.Schema" }], desc = "Compiles a schema." },
]

[[table]]
name = "nitr.base64"
feature = "base64"
desc = "Base64 encoding and decoding."
functions = [
  { name = "encode", params = [{ name = "data", type = "string" }, { name = "opts", type = "table?", desc = "`{ url = true }` for the URL-safe alphabet." }], returns = [{ type = "string" }], desc = "Encodes bytes." },
  { name = "decode", params = [{ name = "value", type = "string" }, { name = "opts", type = "table?" }], returns = [{ type = "string|nil" }, { type = "string|nil", desc = "Reason when nil." }], desc = "Decodes; forgiving about padding, never about the alphabet." },
]

[[table]]
name = "nitr.path"
feature = "path"
desc = "Lexical path manipulation (POSIX and Windows styles). Pure text: nothing touches the filesystem."
functions = [
  { name = "join", params = [{ name = "...", type = "string" }], returns = [{ type = "string" }], desc = "Joins segments; a later absolute segment never discards the base." },
  { name = "basename", params = [{ name = "path", type = "string" }], returns = [{ type = "string" }], desc = "The final component." },
  { name = "dirname", params = [{ name = "path", type = "string" }], returns = [{ type = "string" }], desc = "The directory part." },
  { name = "extension", params = [{ name = "path", type = "string" }], returns = [{ type = "string|nil" }], desc = "The extension without the dot; nil for none (dotfiles included)." },
  { name = "normalize", params = [{ name = "path", type = "string" }], returns = [{ type = "string" }], desc = "Resolves `.`/`..` lexically; `..` cannot climb out of the root. Text-only: before real file access on Windows also reject segments with trailing dots/spaces or colons, which Win32 rewrites." },
  { name = "is_absolute", params = [{ name = "path", type = "string" }], returns = [{ type = "boolean" }], desc = "Whether the path is absolute (`/x`, `C:\\x`, UNC)." },
]

[[table]]
name = "nitr.url"
feature = "url"
desc = "Percent-encoding, query strings, and a lexical URL splitter."
functions = [
  { name = "encode", params = [{ name = "value", type = "string" }], returns = [{ type = "string" }], desc = "Percent-encodes a component (like `encodeURIComponent`)." },
  { name = "decode", params = [{ name = "value", type = "string" }], returns = [{ type = "string" }], desc = "Percent-decodes (`+` is left alone — that is a form convention)." },
  { name = "query_parse", params = [{ name = "query", type = "string" }], returns = [{ type = "table<string, string>" }], desc = "Parses a query string (`+` as space; last duplicate wins)." },
  { name = "query_build", params = [{ name = "params", type = "table" }], returns = [{ type = "string" }], desc = "Builds a query string, keys sorted." },
  { name = "parse", params = [{ name = "value", type = "string" }], returns = [{ type = "table|nil", desc = "`{ scheme?, userinfo?, host?, port?, path, query?, fragment? }`" }, { type = "string|nil", desc = "Reason when nil." }], desc = "Splits a URL lexically (not a WHATWG parser)." },
]

[[table]]
name = "nitr.env"
feature = "env"
desc = "Read-only environment variable access. Opt-in; reads are filtered by `[env] allow`, and `NITR_*` internals are never visible. Getters only: no setter, no enumeration."
functions = [
  { name = "get", params = [{ name = "name", type = "string" }, { name = "default", type = "string?" }], returns = [{ type = "string|nil", desc = "The value, or the default (nil without one) when unset or not readable." }], desc = "Reads one variable." },
  { name = "has", params = [{ name = "name", type = "string" }], returns = [{ type = "boolean" }], desc = "Whether the variable is set and readable; a policy-hidden name reports false." },
  { name = "number", params = [{ name = "name", type = "string" }, { name = "default", type = "number?" }], returns = [{ type = "number|nil" }], desc = "Reads and parses a number; unset or unparseable answers the default." },
  { name = "bool", params = [{ name = "name", type = "string" }, { name = "default", type = "boolean?" }], returns = [{ type = "boolean|nil" }], desc = "Reads a flag: 1/true/yes/on and 0/false/no/off (any case); anything else answers the default." },
]

[[table]]
name = "nitr.test"
feature = "test"
desc = "The `nitr test` framework: available in test files only."
functions = [
  { name = "request", params = [{ name = "method", type = "string" }, { name = "path", type = "string" }, { name = "opts", type = "table?", desc = "`{ headers?, body?, json? }`." }], returns = [{ type = "table", desc = "`{ status, headers, body }` plus `:json()`." }], desc = "Dispatches through the real router/middleware/handler path." },
  { name = "describe", params = [{ name = "name", type = "string" }, { name = "fn", type = "fun()" }], desc = "Groups tests; names are prefixed." },
  { name = "it", params = [{ name = "name", type = "string" }, { name = "fn", type = "fun()" }], desc = "One test case." },
  { name = "expect", params = [{ name = "actual", type = "any" }], returns = [{ type = "table", desc = "Matchers: `to_equal`, `to_not_equal`, `to_be_nil`, `to_be_truthy`, `to_match`, `to_contain`." }], desc = "Starts an assertion." },
  { name = "before_each", params = [{ name = "fn", type = "fun()" }], desc = "Runs before every test in the file." },
  { name = "after_each", params = [{ name = "fn", type = "fun()" }], desc = "Runs after every test in the file (even failing ones)." },
]

[[table]]
name = "nitr.cfg"
feature = "cfg"
desc = "The configuration snapshot returned by `config.lua` (nil without one)."

[[table]]
name = "nitr.ext"
desc = "User-defined Rust extension modules (`ServerBuilder::module`), one level below the standard library so no future builtin can collide with them. Each module is a table: `nitr.ext.<name>`. Absent until a module is registered."