barejs 0.1.44 → 0.1.45

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,3 +1,4 @@
1
+
1
2
  <div align="center">
2
3
  <br />
3
4
  <h1>Bare<span style="color: #F7DF1E;">JS</span></h1>
@@ -8,14 +9,14 @@
8
9
  <a href="https://www.npmjs.com/package/barejs">
9
10
  <img src="https://img.shields.io/npm/v/barejs?style=for-the-badge&logo=npm&color=CB3837" alt="NPM Version">
10
11
  </a>
11
- <a href="https://github.com/xarhang/bareJS/actions/workflows/bench.yml">
12
- <img src="https://img.shields.io/github/actions/workflow/status/xarhang/bareJS/bench.yml?branch=main&label=Performance&style=for-the-badge&logo=github" alt="Performance">
12
+ <a href="https://github.com/xarhang/barejs/actions/workflows/bench.yml">
13
+ <img src="https://img.shields.io/github/actions/workflow/status/xarhang/barejs/bench.yml?branch=main&label=Performance&style=for-the-badge&logo=github" alt="Performance">
13
14
  </a>
14
15
  <a href="https://bun.sh">
15
16
  <img src="https://img.shields.io/badge/Bun-%3E%3D1.0.0-black?style=for-the-badge&logo=bun" alt="Bun Version">
16
17
  </a>
17
- <a href="https://github.com/xarhang/bareJS/blob/main/LICENSE">
18
- <img src="https://img.shields.io/github/license/xarhang/bareJS?style=for-the-badge&color=blue" alt="License">
18
+ <a href="https://github.com/xarhang/barejs/blob/main/LICENSE">
19
+ <img src="https://img.shields.io/github/license/xarhang/barejs?style=for-the-badge&color=blue" alt="License">
19
20
  </a>
20
21
  </p>
21
22
 
@@ -23,6 +24,7 @@
23
24
  <a href="#-benchmarks">Benchmarks</a> •
24
25
  <a href="#-features">Features</a> •
25
26
  <a href="#-quick-start">Quick Start</a> •
27
+ <a href="#-configuration">Configuration</a> •
26
28
  <a href="#-architecture">Architecture</a>
27
29
  </p>
28
30
 
@@ -31,7 +33,8 @@
31
33
 
32
34
  ## 📊 Benchmarks: Real-World Performance
33
35
 
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.
36
+ 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.
37
+
35
38
  <!-- MARKER: PERFORMANCE_TABLE_START -->
36
39
 
37
40
  | Framework | Latency | Speed |
@@ -40,18 +43,19 @@ BareJS leads in complex, real-world scenarios. We measure engine efficiency usin
40
43
  | Elysia | 2.43 µs | 1.55x slower |
41
44
  | Hono | 10.63 µs | 6.76x slower |
42
45
 
43
- > Last Updated: 2026-01-12 (github action)
46
+ > Last Updated: 2026-01-12
44
47
 
45
48
  <!-- MARKER: PERFORMANCE_TABLE_END -->
46
49
  > [!TIP]
47
50
  > **View our [Continuous Benchmark Dashboard](https://xarhang.github.io/bareJS/dev/benchmarks/)** for historical data and detailed performance trends across different hardware.
48
51
 
52
+
49
53
  ## 🚀 Key Features
50
54
 
51
- * **JIT Pipeline Compilation**: Routes and middleware chains are compiled into a single, flattened JavaScript function at runtime to eliminate recursive call overhead.
52
- * **Object Pooling**: Recycles `Context` objects via a circular buffer, significantly reducing Garbage Collection (GC) pressure.
53
- * **Lazy Body Parsing**: Requests are processed instantly. Payloads are only parsed on-demand via `ctx.jsonBody()`, maintaining peak speed for GET requests.
54
- * **Mechanical Sympathy**: Intentionally designed to align with V8's optimization heuristics and Bun's internal I/O architecture.
55
+ * **JIT Pipeline Compilation**: Routes and middleware chains are flattened into a single function at runtime.
56
+ * **Object Pooling**: Recycles `Context` objects via a circular buffer, drastically reducing GC pressure.
57
+ * **Secure by Default**: Built-in **Argon2id (64MB)** hashing for maximum production security.
58
+ * **Mechanical Sympathy**: Intentionally designed to align with V8's optimization and Bun's I/O.
55
59
 
56
60
  ## 🛠️ Installation
57
61
 
@@ -66,9 +70,7 @@ bun add barejs
66
70
  import { BareJS, type Context } from 'barejs';
67
71
 
68
72
  const app = new BareJS();
69
-
70
73
  app.get('/', (ctx: Context) => ctx.json({ hello: "world" }));
71
-
72
74
  app.listen(3000);
73
75
 
74
76
  ```
@@ -77,116 +79,77 @@ app.listen(3000);
77
79
 
78
80
  ## 📘 Comprehensive Guide
79
81
 
80
- ### 1. 🔀 Advanced Routing
81
-
82
- Modularize your application and maintain clean codebases using `BareRouter`.
83
-
84
- ```typescript
85
- import { BareJS, BareRouter, type Context } from 'barejs';
86
-
87
- const app = new BareJS();
88
- const api = new BareRouter("/api/v1");
89
-
90
- api.get("/status", (ctx: Context) => ({ status: "ok" }));
91
-
92
- app.use(api); // Accessible at /api/v1/status
93
-
94
- ```
95
-
96
- ### 2. 🛡️ Security & Authentication
82
+ ### 1. 🛡️ Security & Authentication (Dual-API)
97
83
 
98
- Built-in utilities for secure password hashing (Argon2/Bcrypt via Bun) and RFC-compliant JWT handling.
84
+ BareJS provides high-level security utilities. You can use `Hash` or `Password` interchangeably for semantic clarity.
99
85
 
100
86
  ```typescript
101
- import { bareAuth, createToken, Password,Hash, type Context } from 'barejs';
87
+ import { bareAuth, createToken, Password, Hash, type Context } from 'barejs';
102
88
 
103
- const SECRET = "your-ultra-secure-secret";
89
+ const SECRET = process.env.JWT_SECRET || "your-secret";
104
90
 
105
- app.post('/login', async (ctx: Context) => {
91
+ app.post('/register', async (ctx: Context) => {
106
92
  const body = await ctx.jsonBody();
107
- //const hash = await Password.hash(body.password); //you can replace with Hash
108
- const hash = await Hash.hash(body.password);
109
- // const isValid = await Password.verify(body.password, hash); //you can replace with Hash
93
+
94
+ // High-performance Argon2id Hashing (64MB default)
95
+ const hash = await Password.make(body.password);
96
+
97
+ // Verify with ease
110
98
  const isValid = await Hash.verify(body.password, hash);
99
+
111
100
  if (isValid) {
112
101
  const token = await createToken({ id: 1 }, SECRET);
113
102
  return { token };
114
103
  }
115
104
  });
116
105
 
117
- // Protect routes with bareAuth middleware
106
+ // Protect routes
118
107
  app.get('/me', bareAuth(SECRET), (ctx: Context) => {
119
- const user = ctx.get('user'); // Identity injected by bareAuth
120
- return { user };
108
+ return { user: ctx.get('user') };
121
109
  });
122
110
 
123
111
  ```
124
112
 
125
- ### 3. Data Validation (3 Styles)
113
+ ### 2. ⚙️ Configuration
126
114
 
127
- BareJS integrates deeply with TypeBox for JIT-level speeds but remains compatible with the broader ecosystem.
115
+ Customize your engine by creating a `bare.config.ts` in your root directory.
128
116
 
129
117
  ```typescript
130
- import { typebox, zod, native, t, type Context } from 'barejs';
131
- import { z } from 'zod';
132
-
133
- // Style A: TypeBox (Highest Performance - Recommended)
134
- const TypeBoxSchema = t.Object({ name: t.String() });
135
- app.post('/typebox', typebox(TypeBoxSchema), async (ctx: Context) => {
136
- const body = await ctx.jsonBody();
137
- return body;
138
- });
139
-
140
- // Style B: Zod (Industry Standard)
141
- const ZodSchema = z.object({ age: z.number() });
142
- app.post('/zod', zod(ZodSchema), async (ctx: Context) => {
143
- const body = await ctx.jsonBody();
144
- return body;
145
- });
146
-
147
- // Style C: Native (Zero Dependency / JSON Schema)
148
- const NativeSchema = {
149
- type: "object",
150
- properties: { id: { type: 'number' } },
151
- required: ["id"]
118
+ // bare.config.ts
119
+ export default {
120
+ port: 3000,
121
+ hash: {
122
+ algorithm: "argon2id",
123
+ memoryCost: 65536, // 64MB
124
+ timeCost: 2
125
+ }
152
126
  };
153
- app.post('/native', native(NativeSchema), async (ctx: Context) => {
154
- const body = await ctx.jsonBody();
155
- return body;
156
- });
157
127
 
158
128
  ```
159
129
 
160
- ### 4. 🔌 Essential Plugins
161
-
162
- Standard utilities optimized for the BareJS engine's execution model.
163
-
164
- #### **Logger**
165
-
166
- High-precision terminal logging with color-coded status codes and microsecond timing.
130
+ ### 3. Data Validation (3 Styles)
167
131
 
168
132
  ```typescript
169
- import { logger } from 'barejs';
170
- app.use(logger);
171
-
172
- ```
173
-
174
- #### **CORS**
133
+ import { typebox, zod, native, t, type Context } from 'barejs';
134
+ import { z } from 'zod';
175
135
 
176
- Highly optimized Cross-Origin Resource Sharing middleware.
136
+ // Style A: TypeBox (JIT Optimized - Recommended)
137
+ const Schema = t.Object({ name: t.String() });
138
+ app.post('/user', typebox(Schema), (ctx) => ctx.json(ctx.body));
177
139
 
178
- ```typescript
179
- import { cors } from 'barejs';
180
- app.use(cors({ origin: "*", methods: ["GET", "POST"] }));
140
+ // Style B: Zod (Industry Standard)
141
+ const ZodSchema = z.object({ age: z.number() });
142
+ app.post('/age', zod(ZodSchema), (ctx) => ctx.json(ctx.body));
181
143
 
182
144
  ```
183
145
 
184
- #### **Static Files**
185
-
186
- Serves static assets with zero-overhead using Bun's native file system implementation.
146
+ ### 4. 🔌 Essential Plugins
187
147
 
188
148
  ```typescript
189
- import { staticFile } from 'barejs';
149
+ import { logger, cors, staticFile } from 'barejs';
150
+
151
+ app.use(logger);
152
+ app.use(cors());
190
153
  app.use(staticFile("public"));
191
154
 
192
155
  ```
@@ -195,27 +158,22 @@ app.use(staticFile("public"));
195
158
 
196
159
  ## 🧠 Context API
197
160
 
198
- The `Context` object is pre-allocated in a circular pool to eliminate memory fragmentation.
199
-
200
161
  | Method / Property | Description |
201
162
  | --- | --- |
202
163
  | `ctx.req` | Raw incoming Bun `Request` object. |
203
- | `ctx.params` | Object containing route parameters (e.g., `:id`). |
204
- | **`ctx.jsonBody()`** | **[Async]** Parses the JSON body on-demand and caches it for the lifecycle. |
205
- | `ctx.status(code)` | Sets the HTTP status code (Chainable). |
206
- | `ctx.json(data)` | Finalizes and returns an optimized JSON response. |
207
- | `ctx.set(k, v)` | Stores metadata in the request-scoped store. |
208
- | `ctx.get(k)` | Retrieves stored data from the lifecycle store. |
164
+ | `ctx.params` | Route parameters (e.g., `:id`). |
165
+ | `ctx.jsonBody()` | **[Async]** Parses and caches JSON body. |
166
+ | `ctx.status(code)` | Sets the HTTP status code. |
167
+ | `ctx.json(data)` | Returns an optimized JSON response. |
209
168
 
210
169
  ---
211
170
 
212
171
  ## ⚙️ Performance Tuning
213
172
 
214
- | OS Variable | Default | Description |
173
+ | OS Variable / File | Default | Description |
215
174
  | --- | --- | --- |
216
- | `BARE_POOL_SIZE` | `1024` | Pre-allocated context pool size. Must be a **Power of 2**. |
217
- | `NODE_ENV` | `development` | Use `production` to hide stack traces and enable V8's hot-path optimizations. |
218
-
219
- ---
175
+ | `bare.config.ts` | - | Centralized config for Port and Hash. |
176
+ | `BARE_POOL_SIZE` | `1024` | Context pool size (Must be Power of 2). |
177
+ | `NODE_ENV` | `development` | Set to `production` for peak JIT speed. |
220
178
 
221
- **Maintained by [xarhang](https://github.com/xarhang) | **License: MIT**
179
+ **Maintained by [xarhang](https://github.com/xarhang) | License: MIT**