barejs 0.1.38 โ†’ 0.1.40

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,6 +1,6 @@
1
1
  <div align="center">
2
2
  <br />
3
- <h1>Bare<span style="color: #F7DF1E;">JS</span></h1>
3
+ <h1>Bare<span style="color: #F7DF1E;">JS</span></h1>
4
4
  <p><strong>The "Metal" of Web Frameworks</strong></p>
5
5
  <p><i>An ultra-high-performance web engine built for Bun, architected strictly for Mechanical Sympathy.</i></p>
6
6
 
@@ -29,14 +29,10 @@
29
29
  ---
30
30
  </div>
31
31
 
32
-
33
-
34
32
  ## ๐Ÿ“Š Benchmarks: Real-World Performance
35
33
 
36
- BareJS leads in complex, real-world scenarios. We test using a **"Real-World" Stress Test** involving **10 middlewares** and **Deep Path Routing** to ensure we measure engine efficiency, not just hello-world speed.
37
-
34
+ BareJS leads in complex, real-world scenarios. We measure engine efficiency using a **stress test** involving **10+ middlewares** and **deep radix tree routing** to ensure performance holds under high concurrency, not just in isolated "Hello World" loops.
38
35
  <!-- MARKER: PERFORMANCE_TABLE_START -->
39
-
40
36
  | Framework | Latency | Speed |
41
37
  | :--- | :--- | :--- |
42
38
  | **BareJS** | **1.15 ยตs** | **Baseline** |
@@ -48,27 +44,32 @@ BareJS leads in complex, real-world scenarios. We test using a **"Real-World" St
48
44
  <!-- MARKER: PERFORMANCE_TABLE_END -->
49
45
  > [!TIP]
50
46
  > **View our [Continuous Benchmark Dashboard](https://xarhang.github.io/bareJS/dev/benchmarks/)** for historical data and detailed performance trends across different hardware.
51
- ---
52
- ### Why BareJS is Faster
53
- * **Flat Pipeline:** No recursive middleware overhead.
54
- * **JIT Route Compilation:** Pre-calculates route matching before the first request hits.
55
- * **Zero Dependency:** Built purely for the Bun runtime.
56
47
 
57
- ## ๐Ÿ› ๏ธ Installation & Setup
48
+ ## ๐Ÿš€ Key Features
49
+
50
+ * **JIT Pipeline Compilation**: Routes and middleware chains are compiled into a single, flattened JavaScript function at runtime to eliminate recursive call overhead.
51
+ * **Object Pooling**: Recycles `Context` objects via a circular buffer, significantly reducing Garbage Collection (GC) pressure.
52
+ * **Lazy Body Parsing**: Requests are processed instantly. Payloads are only parsed on-demand via `ctx.jsonBody()`, maintaining peak speed for GET requests.
53
+ * **Mechanical Sympathy**: Intentionally designed to align with V8's optimization heuristics and Bun's internal I/O architecture.
54
+
55
+ ## ๐Ÿ› ๏ธ Installation
58
56
 
59
57
  ```bash
60
58
  bun add barejs
59
+
61
60
  ```
62
61
 
63
62
  ### The "Bare" Minimum
63
+
64
64
  ```typescript
65
- import { BareJS,type Context } from 'barejs';
65
+ import { BareJS, type Context } from 'barejs';
66
66
 
67
67
  const app = new BareJS();
68
68
 
69
69
  app.get('/', (ctx: Context) => ctx.json({ hello: "world" }));
70
70
 
71
71
  app.listen(3000);
72
+
72
73
  ```
73
74
 
74
75
  ---
@@ -76,7 +77,8 @@ app.listen(3000);
76
77
  ## ๐Ÿ“˜ Comprehensive Guide
77
78
 
78
79
  ### 1. ๐Ÿ”€ Advanced Routing
79
- Use `BareRouter` for modularity and nesting.
80
+
81
+ Modularize your application and maintain clean codebases using `BareRouter`.
80
82
 
81
83
  ```typescript
82
84
  import { BareJS, BareRouter, type Context } from 'barejs';
@@ -86,21 +88,23 @@ const api = new BareRouter("/api/v1");
86
88
 
87
89
  api.get("/status", (ctx: Context) => ({ status: "ok" }));
88
90
 
89
- app.use(api); // Result: /api/v1/status
91
+ app.use(api); // Accessible at /api/v1/status
92
+
90
93
  ```
91
94
 
92
95
  ### 2. ๐Ÿ›ก๏ธ Security & Authentication
93
- Full RFC 7515 compliant JWT support and secure password utilities.
96
+
97
+ Built-in utilities for secure password hashing (Argon2/Bcrypt via Bun) and RFC-compliant JWT handling.
94
98
 
95
99
  ```typescript
96
100
  import { bareAuth, createToken, Password, type Context } from 'barejs';
97
101
 
98
- const SECRET = "your-secret";
102
+ const SECRET = "your-ultra-secure-secret";
99
103
 
100
- // JWT Generation
101
104
  app.post('/login', async (ctx: Context) => {
102
- const hash = await Password.hash("password123");
103
- const isValid = await Password.verify("password123", hash);
105
+ const body = await ctx.jsonBody();
106
+ const hash = await Password.hash(body.password);
107
+ const isValid = await Password.verify(body.password, hash);
104
108
 
105
109
  if (isValid) {
106
110
  const token = await createToken({ id: 1 }, SECRET);
@@ -108,78 +112,108 @@ app.post('/login', async (ctx: Context) => {
108
112
  }
109
113
  });
110
114
 
111
- // Protection Middleware
115
+ // Protect routes with bareAuth middleware
112
116
  app.get('/me', bareAuth(SECRET), (ctx: Context) => {
113
- const user = ctx.get('user'); // Set by bareAuth
117
+ const user = ctx.get('user'); // Identity injected by bareAuth
114
118
  return { user };
115
119
  });
120
+
116
121
  ```
117
122
 
118
123
  ### 3. โœ… Data Validation (3 Styles)
119
- Choose the validation style that fits your workflow.
124
+
125
+ BareJS integrates deeply with TypeBox for JIT-level speeds but remains compatible with the broader ecosystem.
120
126
 
121
127
  ```typescript
122
128
  import { typebox, zod, native, t, type Context } from 'barejs';
123
129
  import { z } from 'zod';
124
130
 
125
- // Style A: TypeBox (Fastest, Highly Recommended)
131
+ // Style A: TypeBox (Highest Performance - Recommended)
126
132
  const TypeBoxSchema = t.Object({ name: t.String() });
127
- app.post('/typebox', typebox(TypeBoxSchema), (ctx: Context) => ctx.body);
133
+ app.post('/typebox', typebox(TypeBoxSchema), async (ctx: Context) => {
134
+ const body = await ctx.jsonBody();
135
+ return body;
136
+ });
128
137
 
129
- // Style B: Zod (Standard)
138
+ // Style B: Zod (Industry Standard)
130
139
  const ZodSchema = z.object({ age: z.number() });
131
- app.post('/zod', zod(ZodSchema), (ctx: Context) => ctx.body);
140
+ app.post('/zod', zod(ZodSchema), async (ctx: Context) => {
141
+ const body = await ctx.jsonBody();
142
+ return body;
143
+ });
144
+
145
+ // Style C: Native (Zero Dependency / JSON Schema)
146
+ const NativeSchema = {
147
+ type: "object",
148
+ properties: { id: { type: 'number' } },
149
+ required: ["id"]
150
+ };
151
+ app.post('/native', native(NativeSchema), async (ctx: Context) => {
152
+ const body = await ctx.jsonBody();
153
+ return body;
154
+ });
132
155
 
133
- // Style C: Native (Zero Dependency)
134
- const NativeSchema = { properties: { id: { type: 'number' } } };
135
- app.post('/native', native(NativeSchema), (ctx: Context) => ctx.body);
136
156
  ```
137
157
 
138
158
  ### 4. ๐Ÿ”Œ Essential Plugins
139
- Built-in utilities for modern web development.
159
+
160
+ Standard utilities optimized for the BareJS engine's execution model.
140
161
 
141
162
  #### **Logger**
142
- Color-coded terminal logging with millisecond-precision timing.
163
+
164
+ High-precision terminal logging with color-coded status codes and microsecond timing.
165
+
143
166
  ```typescript
144
167
  import { logger } from 'barejs';
145
168
  app.use(logger);
169
+
146
170
  ```
147
171
 
148
172
  #### **CORS**
173
+
174
+ Highly optimized Cross-Origin Resource Sharing middleware.
175
+
149
176
  ```typescript
150
177
  import { cors } from 'barejs';
151
- app.use(cors({ origin: "*", methods: "GET,POST" }));
178
+ app.use(cors({ origin: "*", methods: ["GET", "POST"] }));
179
+
152
180
  ```
153
181
 
154
182
  #### **Static Files**
155
- Efficiently serve files from any directory.
183
+
184
+ Serves static assets with zero-overhead using Bun's native file system implementation.
185
+
156
186
  ```typescript
157
187
  import { staticFile } from 'barejs';
158
- app.use(staticFile("public")); // Serves ./public/* at /
188
+ app.use(staticFile("public"));
189
+
159
190
  ```
160
191
 
161
- ### 5. ๐Ÿง  Context API
162
- The `Context` object is recycled to eliminate GC overhead.
192
+ ---
193
+
194
+ ## ๐Ÿง  Context API
195
+
196
+ The `Context` object is pre-allocated in a circular pool to eliminate memory fragmentation.
163
197
 
164
198
  | Method / Property | Description |
165
- | :--- | :--- |
166
- | `ctx.req` | Raw Bun `Request` object |
167
- | `ctx.params` | Route parameters |
168
- | `ctx.body` | Validated body (from `typebox`, `zod`, etc.) |
169
- | `ctx.status(code)` | Sets response status |
170
- | `ctx.json(data)` | Returns an optimized JSON response |
171
- | `ctx.set(k, v)` | Stores data in request lifecycle |
172
- | `ctx.get(k)` | Retrieves stored data |
199
+ | --- | --- |
200
+ | `ctx.req` | Raw incoming Bun `Request` object. |
201
+ | `ctx.params` | Object containing route parameters (e.g., `:id`). |
202
+ | **`ctx.jsonBody()`** | **[Async]** Parses the JSON body on-demand and caches it for the lifecycle. |
203
+ | `ctx.status(code)` | Sets the HTTP status code (Chainable). |
204
+ | `ctx.json(data)` | Finalizes and returns an optimized JSON response. |
205
+ | `ctx.set(k, v)` | Stores metadata in the request-scoped store. |
206
+ | `ctx.get(k)` | Retrieves stored data from the lifecycle store. |
173
207
 
174
208
  ---
175
209
 
176
210
  ## โš™๏ธ Performance Tuning
177
211
 
178
212
  | OS Variable | Default | Description |
179
- | :--- | :--- | :--- |
180
- | `BARE_POOL_SIZE` | `1024` | Pre-allocated context pool. Use power of 2. |
181
- | `NODE_ENV` | `development` | Use `production` to enable max JIT stability. |
213
+ | --- | --- | --- |
214
+ | `BARE_POOL_SIZE` | `1024` | Pre-allocated context pool size. Must be a **Power of 2**. |
215
+ | `NODE_ENV` | `development` | Use `production` to hide stack traces and enable V8's hot-path optimizations. |
182
216
 
183
217
  ---
184
218
 
185
- **Maintained by [xarhang](https://github.com/xarhang)** | **License: MIT**
219
+ **Maintained by [xarhang](https://github.com/xarhang) | **License: MIT**