barejs 0.1.57 → 0.1.58

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
@@ -28,17 +28,17 @@ All benchmark numbers below are **automatically generated on every push to `main
28
28
 
29
29
  <!-- MARKER: MULTI_BENCH_START -->
30
30
 
31
- | Scenario | **BareJS** 🏆 | **ElysiaJS** | **Hono** |
31
+ | Scenario | **BareJS** | **ElysiaJS** | **Hono** |
32
32
  | :--- | :---: | :---: | :---: |
33
- | **1. 100 Routes + Dynamic Params** | **`532 ns`**<br><sub>`p99: 2.99 µs`</sub> | `899 ns`<br><sub>`p99: 4.41 µs`</sub> | `2.13 µs`<br><sub>`p99: 9.98 µs`</sub> |
34
- | **2. 10 Middlewares + Deep Path** | **`529 ns`**<br><sub>`p99: 3.01 µs`</sub> | `887 ns`<br><sub>`p99: 4.20 µs`</sub> | `2.51 µs`<br><sub>`p99: 10.51 µs`</sub> |
35
- | **3. POST JSON + Schema Validation** | **`1.71 µs`**<br><sub>`p99: 7.36 µs`</sub> | `2.25 µs`<br><sub>`p99: 9.77 µs`</sub> | `2.29 µs`<br><sub>`p99: 11.39 µs`</sub> |
36
- | **4. Async Handler + Query Parsing** | **`970 ns`**<br><sub>`p99: 3.94 µs`</sub> | `1.11 µs`<br><sub>`p99: 4.30 µs`</sub> | `1.59 µs`<br><sub>`p99: 7.08 µs`</sub> |
37
- | **5. Response Schema Secret Stripping** | **`485 ns`**<br><sub>`p99: 2.92 µs`</sub> | `799 ns`<br><sub>`p99: 3.63 µs`</sub> | — |
38
- | **6. SQLite Report (`SUM`/`AVG`/`GROUP BY`)** | **`14.96 µs`**<br><sub>`p99: 23.58 µs`</sub> | `15.11 µs`<br><sub>`p99: 24.29 µs`</sub> | `18.11 µs`<br><sub>`p99: 35.08 µs`</sub> |
39
- | **7. SQLite Write (`POST` + Validate + `INSERT`)** | **`5.27 µs`**<br><sub>`p99: 14.31 µs`</sub> | `6.39 µs`<br><sub>`p99: 17.90 µs`</sub> | `8.32 µs`<br><sub>`p99: 26.56 µs`</sub> |
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> |
40
40
 
41
- > Last Updated: 2026-11-20 (github action)
41
+ > Last Updated: 2026-11-24 (github action)
42
42
 
43
43
  <!-- MARKER: MULTI_BENCH_END -->
44
44
 
@@ -48,15 +48,15 @@ All benchmark numbers below are **automatically generated on every push to `main
48
48
 
49
49
  | Framework | Latency | Speed |
50
50
  | :--- | :--- | :--- |
51
- | **BareJS** | **0.52 µs** | **Baseline** |
52
- | Elysia | 0.85 µs | 1.64x slower |
53
- | Hono | 3.30 µs | 6.32x slower |
51
+ | **BareJS** | **1.46 µs** | **Baseline** |
52
+ | Elysia | 1.63 µs | 1.11x slower |
53
+ | Hono | 5.41 µs | 3.70x slower |
54
54
 
55
- > Last Updated: 2026-11-20 (github action)
55
+ > Last Updated: 2026-11-24 (github action)
56
56
 
57
57
  <!-- MARKER: PERFORMANCE_TABLE_END -->
58
58
 
59
- > Verified with **401 Automated Tests (6,129 assertions)** and **100.00% Function & 100.00% Line Coverage**.
59
+ > Verified with **419 Automated Tests (6,231 assertions)** and **100.00% Function & 100.00% Line Coverage**.
60
60
  >
61
61
  > [!TIP]
62
62
  > **View our [Continuous Benchmark Dashboard](https://xarhang.github.io/bareJS/dev/benchmarks/)** for historical data and detailed performance trends.
@@ -74,11 +74,11 @@ All benchmark numbers below are **automatically generated on every push to `main
74
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
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
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**: Recycles `Context` instances via a LIFO free-list stack with automatic cleanup on release—zero cross-request state leakage.
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
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
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()`, and symlink-safe `staticFile()`.
81
- * **Observability, Real-Time, RPC & OpenAPI 3.1**: Built-in `logger()` (`pretty`/`json` + `X-Request-Id`), `healthcheck()` (`/health` + `/ready`), 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`.
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
82
 
83
83
  ---
84
84
 
@@ -187,6 +187,18 @@ app.post('/upload', async (ctx) => {
187
187
  uploaded: files.map((f) => ({ safeName: f.safeName, size: f.size, type: f.type }))
188
188
  };
189
189
  });
190
+
191
+ // True Streaming Multipart Upload (constant O(chunk) memory for large files)
192
+ app.post('/upload-stream', async (ctx) => {
193
+ const uploaded: string[] = [];
194
+ for await (const part of ctx.uploadStream({ maxFileSize: 500 * 1024 * 1024 })) {
195
+ if (part.type === 'file' && part.stream) {
196
+ await Bun.write(`./uploads/${part.safeName}`, new Response(part.stream));
197
+ uploaded.push(part.safeName!);
198
+ }
199
+ }
200
+ return { uploaded };
201
+ });
190
202
  ```
191
203
 
192
204
  ---
@@ -272,6 +284,7 @@ app.use({
272
284
 
273
285
  // Server Lifecycle & Error Masking
274
286
  app.onStart(() => console.log('Database ready'));
287
+ app.onDrain(async () => console.log('Draining in-flight requests...')); // Triggered on app.drain() or IPC DRAIN_REQUEST
275
288
  app.onStop(async () => console.log('Cleaned up resources'));
276
289
  app.onError((err, ctx) => ctx.status(err.status || 500).json({
277
290
  status: 'error',
@@ -370,6 +383,7 @@ app.listen(3000, '0.0.0.0', true, { maxRequestBodySize: 2 * 1024 * 1024 });
370
383
  | `ctx.query` / `ctx.getQuery(k)` | Lazy, prototype-pollution-safe parsed query parameters. |
371
384
  | `await ctx.jsonBody(maxBytes?)` | Parses & caches JSON body (enforces `413` limit, default 1MB). |
372
385
  | `await ctx.upload(options?)` | Safe `multipart/form-data` upload parser (`maxFileSize`, `maxFiles`, `allowedTypes`). |
386
+ | `ctx.uploadStream(options?)` | True streaming `multipart/form-data` async generator (`ReadableStream` per file part, $O(\text{chunk})$ memory). |
373
387
  | `ctx.cookies` | Lazy parsed request cookies (`Record<string, string>`). |
374
388
  | `await ctx.setCookie(k, v, opts?)` | Sets a cookie (`HttpOnly; SameSite=Lax` default, optional HMAC `secret`). |
375
389
  | `await ctx.getSignedCookie(k, s)` | Verifies HMAC-SHA256 signed cookie in constant time. |
@@ -393,7 +407,7 @@ app.listen(3000, '0.0.0.0', true, { maxRequestBodySize: 2 * 1024 * 1024 });
393
407
  | Variable / File | Default | Description |
394
408
  | :--- | :--- | :--- |
395
409
  | `bare.config.ts` | Optional | Centralized config for `port` and Argon2/bcrypt `hash` parameters. |
396
- | `poolSize` | `1024` | `new BareJS(poolSize)` — rounded up to power of 2 for $O(1)$ pooling. |
410
+ | `poolSize` | `1024` | `new BareJS(poolSize)` — defines the maximum number of released `Context` instances retained in the lazy bounded LIFO stack (`0..65536`; `0` disables retention). Push and pop operations are $O(1)$ with acquired slots cleared while in-flight and shared pool state across JIT recompilations. |
397
411
  | `NODE_ENV` | `development` | Set to `production` to mask 500 stack traces and disable startup banner. |
398
412
 
399
413
  ---