barejs 0.1.44 → 0.1.47
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 +82 -119
- package/dist/index.js +158 -140
- package/dist/types/{bare.d.ts → core/bare.d.ts} +2 -1
- package/dist/types/core/config.d.ts +11 -0
- package/dist/types/{context.d.ts → core/context.d.ts} +2 -0
- package/dist/types/core/loader.d.ts +1 -0
- package/dist/types/{router.d.ts → core/router.d.ts} +2 -10
- package/dist/types/index.d.ts +8 -16
- package/dist/types/{auth.d.ts → security/auth.d.ts} +1 -10
- package/dist/types/{cors.d.ts → security/cors.d.ts} +1 -1
- package/dist/types/security/hash.d.ts +5 -0
- package/dist/types/{validators.d.ts → security/validators.d.ts} +1 -1
- package/package.json +68 -68
- package/src/{bare.ts → core/bare.ts} +105 -63
- package/src/core/config.ts +32 -0
- package/src/{context.ts → core/context.ts} +45 -15
- package/src/core/loader.ts +13 -0
- package/src/{radix.ts → core/radix.ts} +4 -9
- package/src/core/router.ts +62 -0
- package/src/index.ts +10 -16
- package/src/{auth.ts → security/auth.ts} +12 -38
- package/src/{cors.ts → security/cors.ts} +1 -1
- package/src/security/hash.ts +20 -0
- package/src/{validators.ts → security/validators.ts} +3 -2
- package/dist/types/logger.d.ts +0 -2
- package/src/logger.ts +0 -23
- package/src/router.ts +0 -68
- /package/dist/types/{radix.d.ts → core/radix.d.ts} +0 -0
- /package/dist/types/{static.d.ts → utils/static.d.ts} +0 -0
- /package/src/{static.ts → utils/static.ts} +0 -0
package/README.md
CHANGED
|
@@ -1,57 +1,54 @@
|
|
|
1
1
|
<div align="center">
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
<a href="#-quick-start">Quick Start</a> •
|
|
26
|
-
<a href="#-architecture">Architecture</a>
|
|
27
|
-
</p>
|
|
28
|
-
|
|
29
|
-
---
|
|
2
|
+
<br />
|
|
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 web engine built for Bun, architected strictly for Mechanical Sympathy.</i></p>
|
|
6
|
+
|
|
7
|
+
<p>
|
|
8
|
+
<a href="https://www.npmjs.com/package/barejs">
|
|
9
|
+
<img src="https://img.shields.io/npm/v/barejs?style=for-the-badge&logo=npm&color=CB3837" alt="NPM Version">
|
|
10
|
+
</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">
|
|
13
|
+
</a>
|
|
14
|
+
<a href="https://bun.sh">
|
|
15
|
+
<img src="https://img.shields.io/badge/Bun-%3E%3D1.0.0-black?style=for-the-badge&logo=bun" alt="Bun Version">
|
|
16
|
+
</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">
|
|
19
|
+
</a>
|
|
20
|
+
</p>
|
|
21
|
+
</div>
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
30
25
|
</div>
|
|
31
26
|
|
|
32
27
|
## 📊 Benchmarks: Real-World Performance
|
|
33
28
|
|
|
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
|
|
29
|
+
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.
|
|
35
30
|
<!-- MARKER: PERFORMANCE_TABLE_START -->
|
|
36
31
|
|
|
37
32
|
| Framework | Latency | Speed |
|
|
38
33
|
| :--- | :--- | :--- |
|
|
39
|
-
| **BareJS** | **1.
|
|
40
|
-
| Elysia | 2.
|
|
41
|
-
| Hono |
|
|
34
|
+
| **BareJS** | **1.79 µs** | **Baseline** |
|
|
35
|
+
| Elysia | 2.44 µs | 1.36x slower |
|
|
36
|
+
| Hono | 9.86 µs | 5.52x slower |
|
|
42
37
|
|
|
43
|
-
> Last Updated: 2026-01-
|
|
38
|
+
> Last Updated: 2026-01-14 (github action)
|
|
44
39
|
|
|
45
40
|
<!-- MARKER: PERFORMANCE_TABLE_END -->
|
|
41
|
+
|
|
46
42
|
> [!TIP]
|
|
47
|
-
> **View our [Continuous Benchmark Dashboard](https://xarhang.github.io/bareJS/dev/benchmarks/)
|
|
43
|
+
> **View our [Continuous Benchmark Dashboard**](https://xarhang.github.io/bareJS/dev/benchmarks/) for historical data and detailed performance trends.
|
|
48
44
|
|
|
49
45
|
## 🚀 Key Features
|
|
50
46
|
|
|
51
|
-
* **JIT Pipeline Compilation**: Routes and middleware chains are
|
|
52
|
-
* **Object Pooling**: Recycles `Context` objects via a circular buffer,
|
|
53
|
-
* **
|
|
54
|
-
* **
|
|
47
|
+
* **JIT Pipeline Compilation**: Routes and middleware chains are flattened into a single function at runtime.
|
|
48
|
+
* **Object Pooling**: Recycles `Context` objects via a circular buffer, drastically reducing GC pressure.
|
|
49
|
+
* **Internal High-Performance Logger**: Zero-overhead logging integrated directly into the JIT engine.
|
|
50
|
+
* **Precise Radix Router**: v0.1.46 introduces optimized segment matching for deep-nested paths.
|
|
51
|
+
* **Mechanical Sympathy**: Intentionally designed to align with V8's optimization and Bun's I/O.
|
|
55
52
|
|
|
56
53
|
## 🛠️ Installation
|
|
57
54
|
|
|
@@ -67,6 +64,9 @@ import { BareJS, type Context } from 'barejs';
|
|
|
67
64
|
|
|
68
65
|
const app = new BareJS();
|
|
69
66
|
|
|
67
|
+
// Enable Internal Logger
|
|
68
|
+
app.useLog(true);
|
|
69
|
+
|
|
70
70
|
app.get('/', (ctx: Context) => ctx.json({ hello: "world" }));
|
|
71
71
|
|
|
72
72
|
app.listen(3000);
|
|
@@ -77,116 +77,79 @@ app.listen(3000);
|
|
|
77
77
|
|
|
78
78
|
## 📘 Comprehensive Guide
|
|
79
79
|
|
|
80
|
-
### 1.
|
|
80
|
+
### 1. ⚡ Standardized Response & Chaining
|
|
81
81
|
|
|
82
|
-
|
|
82
|
+
BareJS v0.1.46 provides a fluent API for building responses.
|
|
83
83
|
|
|
84
84
|
```typescript
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
85
|
+
app.get('/api/v1/health', (ctx: Context) => {
|
|
86
|
+
// Chainable status and standardized send helper
|
|
87
|
+
return ctx.status(200).send("System is healthy", {
|
|
88
|
+
uptime: process.uptime()
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
91
|
|
|
92
|
-
|
|
92
|
+
// Output: { "status": "success", "msg": "System is healthy", "uptime": ... }
|
|
93
93
|
|
|
94
94
|
```
|
|
95
95
|
|
|
96
|
-
### 2. 🛡️ Security & Authentication
|
|
97
|
-
|
|
98
|
-
Built-in utilities for secure password hashing (Argon2/Bcrypt via Bun) and RFC-compliant JWT handling.
|
|
96
|
+
### 2. 🛡️ Security & Authentication (Dual-API)
|
|
99
97
|
|
|
100
98
|
```typescript
|
|
101
|
-
import { bareAuth, createToken, Password,Hash, type Context } from 'barejs';
|
|
99
|
+
import { bareAuth, createToken, Password, Hash, type Context } from 'barejs';
|
|
102
100
|
|
|
103
|
-
const SECRET = "your-
|
|
101
|
+
const SECRET = process.env.JWT_SECRET || "your-secret";
|
|
104
102
|
|
|
105
|
-
app.post('/
|
|
103
|
+
app.post('/register', async (ctx: Context) => {
|
|
106
104
|
const body = await ctx.jsonBody();
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
105
|
+
|
|
106
|
+
// High-performance Argon2id Hashing (64MB default)
|
|
107
|
+
const hash = await Password.make(body.password);
|
|
108
|
+
|
|
109
|
+
// Verify with ease
|
|
110
110
|
const isValid = await Hash.verify(body.password, hash);
|
|
111
|
+
|
|
111
112
|
if (isValid) {
|
|
112
113
|
const token = await createToken({ id: 1 }, SECRET);
|
|
113
114
|
return { token };
|
|
114
115
|
}
|
|
115
116
|
});
|
|
116
117
|
|
|
117
|
-
// Protect routes with
|
|
118
|
+
// Protect routes with built-in JWT middleware
|
|
118
119
|
app.get('/me', bareAuth(SECRET), (ctx: Context) => {
|
|
119
|
-
|
|
120
|
-
return { user };
|
|
120
|
+
return ctx.send("Authenticated", { user: ctx.get('user') });
|
|
121
121
|
});
|
|
122
122
|
|
|
123
123
|
```
|
|
124
124
|
|
|
125
125
|
### 3. ✅ Data Validation (3 Styles)
|
|
126
126
|
|
|
127
|
-
BareJS
|
|
127
|
+
BareJS is the only engine that offers JIT-optimized validation paths.
|
|
128
128
|
|
|
129
129
|
```typescript
|
|
130
130
|
import { typebox, zod, native, t, type Context } from 'barejs';
|
|
131
131
|
import { z } from 'zod';
|
|
132
132
|
|
|
133
|
-
// Style A: TypeBox (
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
return body;
|
|
138
|
-
});
|
|
133
|
+
// Style A: TypeBox (JIT Optimized - Recommended)
|
|
134
|
+
// Pre-compiled validation to outperform competitors by 55%
|
|
135
|
+
const Schema = t.Object({ name: t.String() });
|
|
136
|
+
app.post('/user', typebox(Schema), (ctx) => ctx.json(ctx.body));
|
|
139
137
|
|
|
140
138
|
// Style B: Zod (Industry Standard)
|
|
141
139
|
const ZodSchema = z.object({ age: z.number() });
|
|
142
|
-
app.post('/
|
|
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"]
|
|
152
|
-
};
|
|
153
|
-
app.post('/native', native(NativeSchema), async (ctx: Context) => {
|
|
154
|
-
const body = await ctx.jsonBody();
|
|
155
|
-
return body;
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
```
|
|
159
|
-
|
|
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.
|
|
140
|
+
app.post('/age', zod(ZodSchema), (ctx) => ctx.json(ctx.body));
|
|
167
141
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
app.use(logger);
|
|
142
|
+
// Style C: Native (Zero Dependency - Simple Checks)
|
|
143
|
+
app.post('/native', native({ properties: { id: { type: 'number' } } }), (ctx) => ctx.json(ctx.body));
|
|
171
144
|
|
|
172
145
|
```
|
|
173
146
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
Highly optimized Cross-Origin Resource Sharing middleware.
|
|
147
|
+
### 4. 🔌 Essential Middleware
|
|
177
148
|
|
|
178
149
|
```typescript
|
|
179
|
-
import { cors } from 'barejs';
|
|
180
|
-
app.use(cors({ origin: "*", methods: ["GET", "POST"] }));
|
|
181
|
-
|
|
182
|
-
```
|
|
150
|
+
import { cors, staticFile } from 'barejs';
|
|
183
151
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
Serves static assets with zero-overhead using Bun's native file system implementation.
|
|
187
|
-
|
|
188
|
-
```typescript
|
|
189
|
-
import { staticFile } from 'barejs';
|
|
152
|
+
app.use(cors());
|
|
190
153
|
app.use(staticFile("public"));
|
|
191
154
|
|
|
192
155
|
```
|
|
@@ -195,27 +158,27 @@ 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` |
|
|
204
|
-
|
|
|
205
|
-
| `ctx.status(code)` | Sets the HTTP status code (Chainable). |
|
|
206
|
-
| `ctx.
|
|
207
|
-
| `ctx.
|
|
208
|
-
| `ctx.
|
|
164
|
+
| `ctx.params` | Route parameters (e.g., `:id`). |
|
|
165
|
+
| `ctx.jsonBody()` | **[Async]** Parses and caches JSON body for performance. |
|
|
166
|
+
| `ctx.status(code)` | Sets the HTTP status code (**Chainable**). |
|
|
167
|
+
| `ctx.send(msg, ext)` | Returns a standardized JSON response. |
|
|
168
|
+
| `ctx.json(data)` | Returns an optimized raw JSON response. |
|
|
169
|
+
| `ctx.setHeader(k, v)` | Sets a response header. |
|
|
170
|
+
| `ctx.set / ctx.get` | Manual storage within the request lifecycle. |
|
|
209
171
|
|
|
210
172
|
---
|
|
211
173
|
|
|
212
174
|
## ⚙️ Performance Tuning
|
|
213
175
|
|
|
214
|
-
| OS Variable | Default | Description |
|
|
176
|
+
| OS Variable / File | Default | Description |
|
|
215
177
|
| --- | --- | --- |
|
|
216
|
-
| `
|
|
217
|
-
| `
|
|
178
|
+
| `bare.config.ts` | - | Centralized config for Port and Hash algorithms. |
|
|
179
|
+
| `BARE_POOL_SIZE` | `1024` | Context pool size (**Must be Power of 2**). |
|
|
180
|
+
| `NODE_ENV` | `development` | Set to `production` to enable peak JIT optimizations. |
|
|
218
181
|
|
|
219
182
|
---
|
|
220
183
|
|
|
221
|
-
**Maintained by [xarhang](https://github.com/xarhang) |
|
|
184
|
+
**Maintained by [xarhang](https://github.com/xarhang) | License: MIT**
|