barejs 0.1.58 → 0.1.59

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/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  <div align="center">
2
2
  <br />
3
3
  <h1>Bare<span style="color: #F7DF1E;">JS</span></h1>
4
- <p><strong>The "Metal" of Web Frameworks</strong></p>
5
- <p><i>An ultra-high-performance JIT-compiled web engine built for Bun, architected strictly for Mechanical Sympathy, Zero-Trust Security, and Effortless Developer Experience.</i></p>
4
+ <p><strong>The batteries-included Bun web framework engineered for zero-overhead speed.</strong></p>
5
+ <p><i>BareJS delivers everything you need for production APIs out of the box—built-in <strong>TypeBox &amp; Zod validation</strong>, <strong>end-to-end type safety</strong> (<code>bareClient</code>), <strong>auto-generated OpenAPI 3.1</strong>, <strong>native WebSockets</strong>, <strong>JSON-RPC 2.0</strong>, <strong>resilience primitives</strong>, and <strong>zero-trust security</strong>—powered by a JIT-compiled O(1) router and LIFO object pooling.</i></p>
6
6
 
7
7
  <p>
8
8
  <a href="https://www.npmjs.com/package/barejs">
@@ -20,6 +20,23 @@
20
20
  </p>
21
21
  </div>
22
22
 
23
+ > [!TIP]
24
+ > **Using AI Coding Assistants (Cursor, Copilot, Claude, Windsurf)?** Point them to [`llms.txt`](./llms.txt) for a complete, token-efficient reference of every BareJS API.
25
+
26
+ ---
27
+
28
+ ## 🚀 Batteries Included, Zero Overhead
29
+
30
+ * **Core Performance**: $O(1)$ Radix Jump-Table Router (inline `switch` dispatch for static, `:params`, and `/*` wildcards), JIT-compiled route/middleware pipelines (`AsyncFunction` fast paths), and concurrency-safe lazy LIFO `Context` object pooling (`0..65536`) with dev-mode Use-After-Free Proxy guard.
31
+ * **Type Safety & Validation**: Unified declarative schema validation for `body`, `query`, `params`, `headers`, and `response` with **TypeBox**, **Zod**, or **Native JSON Schema**—featuring automatic `number`/`boolean` coercion, `maxBodySize` (`413`) DoS guard, `stripUnknown` Mass-Assignment protection, and **end-to-end type inference** (`bareClient(app)`) with zero runtime overhead.
32
+ * **API Documentation & RPC**: Automatic **OpenAPI 3.1** (`app.docs`) generation with `securitySchemes`, `servers`, and `tags` directly from route schemas, plus built-in **JSON-RPC 2.0** (`app.rpc`).
33
+ * **Real-Time & Streaming**: Native parameterized **WebSockets** (`app.ws('/ws/:room')`), **Server-Sent Events** (`ctx.sse`), and native zero-dependency Gzip compression (`compress` via `Bun.gzipSync`).
34
+ * **Built-In Resilience & Jobs**: Production `circuitBreaker` (`503 Retry-After`), `idempotency` (`409 IDEMPOTENT_IN_FLIGHT`), `createSingleflight` cache-stampede protection, and overlap-safe `app.cron` background jobs.
35
+ * **Zero-Trust Security & Utilities**: Built-in `securityHeaders()`, `cors()`, `csrf()`, `timeout()`, `rateLimit()`, `trustProxy(false)` anti-spoofing, `bareAuth()` (constant-time HS256 JWT + `revokeToken()`/`isRevoked`), `Hash` (Argon2id), HMAC-signed cookies, `ctx.upload()` / streaming `ctx.uploadStream()`, and symlink-safe `staticFile()`.
36
+ * **Ergonomics & Observability**: Dual response style (return plain objects/strings Elysia-style OR `ctx.json()`/`ctx.html()`/`ctx.text()`/`ctx.redirect()`/`ctx.send()`), `logger()` (`pretty`/`json` + `X-Request-Id`), `healthcheck()` (`/health` + `/ready`), and `app.onDrain()` + IPC `DRAIN_REQUEST` graceful draining.
37
+
38
+ > Verified with **423 Automated Tests (6,249 assertions)** and **100.00% Function & 100.00% Line Coverage**.
39
+
23
40
  ---
24
41
 
25
42
  ## 📊 Benchmarks: Multi-Scenario & Real-World Performance
@@ -30,13 +47,13 @@ All benchmark numbers below are **automatically generated on every push to `main
30
47
 
31
48
  | Scenario | **BareJS** | **ElysiaJS** | **Hono** |
32
49
  | :--- | :---: | :---: | :---: |
33
- | **1. 100 Routes + Dynamic Params** | **`1.34 µs`**<br><sub>`p99: 8.14 µs`</sub> | `1.81 µs`<br><sub>`p99: 8.57 µs`</sub> | `3.63 µs`<br><sub>`p99: 13.85 µs`</sub> |
34
- | **2. 10 Middlewares + Deep Path** | **`1.34 µs`**<br><sub>`p99: 7.45 µs`</sub> | `1.88 µs`<br><sub>`p99: 8.95 µs`</sub> | `3.91 µs`<br><sub>`p99: 14.28 µs`</sub> |
35
- | **3. POST JSON + Schema Validation** | `3.94 µs`<br><sub>`p99: 14.14 µs`</sub> | `4.87 µs`<br><sub>`p99: 16.20 µs`</sub> | **`3.17 µs`**<br><sub>`p99: 3.54 µs`</sub> |
36
- | **4. Async Handler + Query Parsing** | `2.02 µs`<br><sub>`p99: 9.35 µs`</sub> | `2.26 µs`<br><sub>`p99: 9.39 µs`</sub> | **`1.99 µs`**<br><sub>`p99: 2.48 µs`</sub> |
37
- | **5. Response Schema Secret Stripping** | **`1.08 µs`**<br><sub>`p99: 6.49 µs`</sub> | `1.57 µs`<br><sub>`p99: 7.03 µs`</sub> | — |
38
- | **6. SQLite Report (`SUM`/`AVG`/`GROUP BY`)** | `22.36 µs`<br><sub>`p99: 45.38 µs`</sub> | **`22.00 µs`**<br><sub>`p99: 41.81 µs`</sub> | `27.79 µs`<br><sub>`p99: 52.80 µs`</sub> |
39
- | **7. SQLite Write (`POST` + Validate + `INSERT`)** | `13.46 µs`<br><sub>`p99: 33.35 µs`</sub> | `18.33 µs`<br><sub>`p99: 47.89 µs`</sub> | **`12.79 µs`**<br><sub>`p99: 13.04 µs`</sub> |
50
+ | **1. 100 Routes + Dynamic Params** | **`840 ns`**<br><sub>`p99: 5.14 µs`</sub> | `1.08 µs`<br><sub>`p99: 5.68 µs`</sub> | `2.22 µs`<br><sub>`p99: 10.76 µs`</sub> |
51
+ | **2. 10 Middlewares + Deep Path** | **`769 ns`**<br><sub>`p99: 4.30 µs`</sub> | `1.10 µs`<br><sub>`p99: 5.48 µs`</sub> | `2.46 µs`<br><sub>`p99: 11.15 µs`</sub> |
52
+ | **3. POST JSON + Schema Validation** | **`2.09 µs`**<br><sub>`p99: 8.99 µs`</sub> | `2.52 µs`<br><sub>`p99: 9.64 µs`</sub> | `2.65 µs`<br><sub>`p99: 10.97 µs`</sub> |
53
+ | **4. Async Handler + Query Parsing** | **`1.32 µs`**<br><sub>`p99: 6.52 µs`</sub> | `1.40 µs`<br><sub>`p99: 6.71 µs`</sub> | `1.83 µs`<br><sub>`p99: 8.79 µs`</sub> |
54
+ | **5. Response Schema Secret Stripping** | **`803 ns`**<br><sub>`p99: 5.86 µs`</sub> | `986 ns`<br><sub>`p99: 4.28 µs`</sub> | — |
55
+ | **6. SQLite Report (`SUM`/`AVG`/`GROUP BY`)** | `12.70 µs`<br><sub>`p99: 21.52 µs`</sub> | **`12.64 µs`**<br><sub>`p99: 20.67 µs`</sub> | `15.65 µs`<br><sub>`p99: 33.22 µs`</sub> |
56
+ | **7. SQLite Write (`POST` + Validate + `INSERT`)** | **`4.79 µs`**<br><sub>`p99: 15.30 µs`</sub> | `6.33 µs`<br><sub>`p99: 20.52 µs`</sub> | `8.41 µs`<br><sub>`p99: 30.16 µs`</sub> |
40
57
 
41
58
  > Last Updated: 2026-11-24 (github action)
42
59
 
@@ -48,38 +65,17 @@ All benchmark numbers below are **automatically generated on every push to `main
48
65
 
49
66
  | Framework | Latency | Speed |
50
67
  | :--- | :--- | :--- |
51
- | **BareJS** | **1.46 µs** | **Baseline** |
52
- | Elysia | 1.63 µs | 1.11x slower |
53
- | Hono | 5.41 µs | 3.70x slower |
68
+ | **BareJS** | **0.75 µs** | **Baseline** |
69
+ | Elysia | 0.92 µs | 1.22x slower |
70
+ | Hono | 3.16 µs | 4.20x slower |
54
71
 
55
72
  > Last Updated: 2026-11-24 (github action)
56
73
 
57
74
  <!-- MARKER: PERFORMANCE_TABLE_END -->
58
75
 
59
- > Verified with **419 Automated Tests (6,231 assertions)** and **100.00% Function & 100.00% Line Coverage**.
60
- >
61
76
  > [!TIP]
62
77
  > **View our [Continuous Benchmark Dashboard](https://xarhang.github.io/bareJS/dev/benchmarks/)** for historical data and detailed performance trends.
63
78
 
64
- > [!TIP]
65
- > **Using AI Coding Assistants (Cursor, Copilot, Claude, Windsurf)?** Point them to [`llms.txt`](./llms.txt) for a complete, token-efficient reference of every BareJS API.
66
-
67
- ---
68
-
69
- ## 🚀 Key Features
70
-
71
- * **JIT Pipeline Compilation**: Flattens routes and middleware chains into direct JavaScript functions at startup, using `AsyncFunction` single-handler fast paths and conditional bytecode inlining.
72
- * **$O(1)$ Radix Jump-Table Router**: Compiles static branches, dynamic `:params`, and catch-all wildcards (`/*`) into inline `switch` jump-tables.
73
- * **Recursive JIT Response Projector & Mass-Assignment Guard**: Compiles `response` schemas into fixed-shape JS object literals to strip undeclared fields, and supports `stripUnknown: true` on `body` to prevent Mass-Assignment attacks.
74
- * **Built-In Resilience (`circuitBreaker`, `idempotency`, `createSingleflight`, `app.cron`)**: Prevent cascading failures (`503 Retry-After`), double-charges (`409 IDEMPOTENT_IN_FLIGHT`), cache stampedes (100 concurrent calls -> 1 DB hit), and overlapping background jobs.
75
- * **Native Zero-Dep Gzip (`compress`)**: Compresses responses via Bun's native C/Zig `Bun.gzipSync` with `minSize` threshold and automatic SSE/204/304 bypass.
76
- * **End-to-End Type Inference (`bareClient(app)`)**: Automatic compile-time inference of route paths (across all HTTP methods on shared paths), `params`, `query`, `headers`, `body`, and `response` types directly from your `BareJS` app or mounted `BareRouter`—with zero runtime overhead.
77
- * **Concurrency-Safe Object Pooling & Dev UAF Guard**: Recycles `Context` instances through a lazy bounded LIFO stack (`0..65536`) with acquired slot clearing (`pool[slot] = undefined` while in-flight), shared pool-state synchronization across JIT recompilations, and complete framework-managed state cleanup on release. Timed-out contexts are detached and retired from the pool, and in development mode (`NODE_ENV=development` or unset) a zero-prod-overhead Proxy guard warns if a released `ctx` is accessed after response completion.
78
- * **Dual Response Ergonomics & Full HTTP Methods**: Return plain objects/strings directly (Elysia style) OR use `ctx.json()`, `ctx.html()`, `ctx.text()`, `ctx.redirect()`, `ctx.send()`, with `get`, `post`, `put`, `patch`, `delete`, `options`, `head`, and `all`.
79
- * **Unified Schema Validation & Auto-Coercion**: Validate `body`, `query`, `params`, `headers`, `response`, and `maxBodySize` (`413` DoS guard) in one object with **TypeBox**, **Zod**, or **Native JSON Schema**—with automatic `number`/`boolean` coercion on `query` and `params`.
80
- * **Batteries-Included Zero-Trust Security Suite**: Built-in `securityHeaders()`, `cors()`, `csrf()`, `timeout()`, `rateLimit()`, `trustProxy(false)` anti-spoofing, `maxRequestBodySize` server cap, `bareAuth()` (constant-time HS256 JWT + `revokeToken()` / `isRevoked`), `Hash` (Argon2id), HMAC-signed cookies, `ctx.upload()` / streaming `ctx.uploadStream()`, and symlink-safe `staticFile()`.
81
- * **Observability, Graceful Drain, Real-Time, RPC & OpenAPI 3.1**: Built-in `logger()` (`pretty`/`json` + `X-Request-Id`), `healthcheck()` (`/health` + `/ready`), `app.onDrain()` + IPC `DRAIN_REQUEST` graceful draining, parameterized WebSockets (`app.ws('/ws/:room')`), SSE (`ctx.sse`), JSON-RPC 2.0 (`app.rpc`), and OpenAPI 3.1 (`app.docs`) with `securitySchemes`, `servers`, and `tags`.
82
-
83
79
  ---
84
80
 
85
81
  ## 🛠️ Installation & Quick Start
@@ -107,6 +103,25 @@ app.get('/assets/*', (ctx: Context) => ({ path: ctx.params['*'] }));
107
103
  app.listen(3000);
108
104
  ```
109
105
 
106
+ ### 🌐 Multi-Runtime (Bun, Node.js, Deno)
107
+
108
+ BareJS selects its runtime driver **once at startup**—preserving 100% zero-overhead native speed on Bun while running the exact same app on Node.js (`>=20`) and Deno:
109
+
110
+ ```typescript
111
+ // Bun (Native Mode — default)
112
+ import { BareJS } from 'barejs';
113
+ app.listen(3000);
114
+
115
+ // Node.js (>= 20)
116
+ import { BareJS } from 'barejs/node';
117
+ app.listen(3000);
118
+
119
+ // Deno / Web-Standard fetch hosts
120
+ Deno.serve({ port: 3000 }, (req, info) => app.fetch(req, info.remoteAddr.hostname));
121
+ ```
122
+
123
+ > Optional peer dependencies on Node.js: install `ws` if using `app.ws()`, and `@node-rs/argon2` (or `bcryptjs`) if using `Hash`.
124
+
110
125
  ---
111
126
 
112
127
  ## 📘 Comprehensive Guide