barejs 0.1.43 → 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 +64 -104
- package/dist/index.js +23 -22
- package/dist/types/core/config.d.ts +11 -0
- package/dist/types/core/loader.d.ts +1 -0
- package/dist/types/{router.d.ts → core/router.d.ts} +1 -1
- package/dist/types/index.d.ts +9 -16
- package/dist/types/{auth.d.ts → security/auth.d.ts} +1 -9
- 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/dist/types/utils/logger.d.ts +2 -0
- package/package.json +68 -68
- package/src/{bare.ts → core/bare.ts} +22 -13
- package/src/core/config.ts +32 -0
- package/src/core/loader.ts +13 -0
- package/src/{router.ts → core/router.ts} +1 -1
- package/src/index.ts +10 -16
- package/src/{auth.ts → security/auth.ts} +3 -14
- package/src/{cors.ts → security/cors.ts} +1 -1
- package/src/security/hash.ts +20 -0
- package/src/{validators.ts → security/validators.ts} +1 -1
- package/src/{logger.ts → utils/logger.ts} +1 -1
- package/dist/types/logger.d.ts +0 -2
- /package/dist/types/{bare.d.ts → core/bare.d.ts} +0 -0
- /package/dist/types/{context.d.ts → core/context.d.ts} +0 -0
- /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/{context.ts → core/context.ts} +0 -0
- /package/src/{radix.ts → core/radix.ts} +0 -0
- /package/src/{static.ts → utils/static.ts} +0 -0
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/
|
|
12
|
-
<img src="https://img.shields.io/github/actions/workflow/status/xarhang/
|
|
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/
|
|
18
|
-
<img src="https://img.shields.io/github/license/xarhang/
|
|
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,26 +33,29 @@
|
|
|
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
|
|
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 -->
|
|
39
|
+
|
|
36
40
|
| Framework | Latency | Speed |
|
|
37
41
|
| :--- | :--- | :--- |
|
|
38
|
-
| **BareJS** | **1.
|
|
39
|
-
| Elysia |
|
|
40
|
-
| Hono |
|
|
42
|
+
| **BareJS** | **1.57 µs** | **Baseline** |
|
|
43
|
+
| Elysia | 2.43 µs | 1.55x slower |
|
|
44
|
+
| Hono | 10.63 µs | 6.76x slower |
|
|
41
45
|
|
|
42
|
-
> Last Updated: 2026-01-
|
|
46
|
+
> Last Updated: 2026-01-12
|
|
43
47
|
|
|
44
48
|
<!-- MARKER: PERFORMANCE_TABLE_END -->
|
|
45
49
|
> [!TIP]
|
|
46
50
|
> **View our [Continuous Benchmark Dashboard](https://xarhang.github.io/bareJS/dev/benchmarks/)** for historical data and detailed performance trends across different hardware.
|
|
47
51
|
|
|
52
|
+
|
|
48
53
|
## 🚀 Key Features
|
|
49
54
|
|
|
50
|
-
* **JIT Pipeline Compilation**: Routes and middleware chains are
|
|
51
|
-
* **Object Pooling**: Recycles `Context` objects via a circular buffer,
|
|
52
|
-
* **
|
|
53
|
-
* **Mechanical Sympathy**: Intentionally designed to align with V8's optimization
|
|
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.
|
|
54
59
|
|
|
55
60
|
## 🛠️ Installation
|
|
56
61
|
|
|
@@ -65,9 +70,7 @@ bun add barejs
|
|
|
65
70
|
import { BareJS, type Context } from 'barejs';
|
|
66
71
|
|
|
67
72
|
const app = new BareJS();
|
|
68
|
-
|
|
69
73
|
app.get('/', (ctx: Context) => ctx.json({ hello: "world" }));
|
|
70
|
-
|
|
71
74
|
app.listen(3000);
|
|
72
75
|
|
|
73
76
|
```
|
|
@@ -76,35 +79,23 @@ app.listen(3000);
|
|
|
76
79
|
|
|
77
80
|
## 📘 Comprehensive Guide
|
|
78
81
|
|
|
79
|
-
### 1.
|
|
82
|
+
### 1. 🛡️ Security & Authentication (Dual-API)
|
|
80
83
|
|
|
81
|
-
|
|
84
|
+
BareJS provides high-level security utilities. You can use `Hash` or `Password` interchangeably for semantic clarity.
|
|
82
85
|
|
|
83
86
|
```typescript
|
|
84
|
-
import {
|
|
85
|
-
|
|
86
|
-
const app = new BareJS();
|
|
87
|
-
const api = new BareRouter("/api/v1");
|
|
88
|
-
|
|
89
|
-
api.get("/status", (ctx: Context) => ({ status: "ok" }));
|
|
87
|
+
import { bareAuth, createToken, Password, Hash, type Context } from 'barejs';
|
|
90
88
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
```
|
|
89
|
+
const SECRET = process.env.JWT_SECRET || "your-secret";
|
|
94
90
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
Built-in utilities for secure password hashing (Argon2/Bcrypt via Bun) and RFC-compliant JWT handling.
|
|
98
|
-
|
|
99
|
-
```typescript
|
|
100
|
-
import { bareAuth, createToken, Password, type Context } from 'barejs';
|
|
101
|
-
|
|
102
|
-
const SECRET = "your-ultra-secure-secret";
|
|
103
|
-
|
|
104
|
-
app.post('/login', async (ctx: Context) => {
|
|
91
|
+
app.post('/register', async (ctx: Context) => {
|
|
105
92
|
const body = await ctx.jsonBody();
|
|
106
|
-
|
|
107
|
-
|
|
93
|
+
|
|
94
|
+
// High-performance Argon2id Hashing (64MB default)
|
|
95
|
+
const hash = await Password.make(body.password);
|
|
96
|
+
|
|
97
|
+
// Verify with ease
|
|
98
|
+
const isValid = await Hash.verify(body.password, hash);
|
|
108
99
|
|
|
109
100
|
if (isValid) {
|
|
110
101
|
const token = await createToken({ id: 1 }, SECRET);
|
|
@@ -112,79 +103,53 @@ app.post('/login', async (ctx: Context) => {
|
|
|
112
103
|
}
|
|
113
104
|
});
|
|
114
105
|
|
|
115
|
-
// Protect routes
|
|
106
|
+
// Protect routes
|
|
116
107
|
app.get('/me', bareAuth(SECRET), (ctx: Context) => {
|
|
117
|
-
|
|
118
|
-
return { user };
|
|
108
|
+
return { user: ctx.get('user') };
|
|
119
109
|
});
|
|
120
110
|
|
|
121
111
|
```
|
|
122
112
|
|
|
123
|
-
###
|
|
113
|
+
### 2. ⚙️ Configuration
|
|
124
114
|
|
|
125
|
-
|
|
115
|
+
Customize your engine by creating a `bare.config.ts` in your root directory.
|
|
126
116
|
|
|
127
117
|
```typescript
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
// Style B: Zod (Industry Standard)
|
|
139
|
-
const ZodSchema = z.object({ age: z.number() });
|
|
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"]
|
|
118
|
+
// bare.config.ts
|
|
119
|
+
export default {
|
|
120
|
+
port: 3000,
|
|
121
|
+
hash: {
|
|
122
|
+
algorithm: "argon2id",
|
|
123
|
+
memoryCost: 65536, // 64MB
|
|
124
|
+
timeCost: 2
|
|
125
|
+
}
|
|
150
126
|
};
|
|
151
|
-
app.post('/native', native(NativeSchema), async (ctx: Context) => {
|
|
152
|
-
const body = await ctx.jsonBody();
|
|
153
|
-
return body;
|
|
154
|
-
});
|
|
155
127
|
|
|
156
128
|
```
|
|
157
129
|
|
|
158
|
-
###
|
|
159
|
-
|
|
160
|
-
Standard utilities optimized for the BareJS engine's execution model.
|
|
161
|
-
|
|
162
|
-
#### **Logger**
|
|
163
|
-
|
|
164
|
-
High-precision terminal logging with color-coded status codes and microsecond timing.
|
|
130
|
+
### 3. ✅ Data Validation (3 Styles)
|
|
165
131
|
|
|
166
132
|
```typescript
|
|
167
|
-
import {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
```
|
|
171
|
-
|
|
172
|
-
#### **CORS**
|
|
133
|
+
import { typebox, zod, native, t, type Context } from 'barejs';
|
|
134
|
+
import { z } from 'zod';
|
|
173
135
|
|
|
174
|
-
|
|
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));
|
|
175
139
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
app.
|
|
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));
|
|
179
143
|
|
|
180
144
|
```
|
|
181
145
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
Serves static assets with zero-overhead using Bun's native file system implementation.
|
|
146
|
+
### 4. 🔌 Essential Plugins
|
|
185
147
|
|
|
186
148
|
```typescript
|
|
187
|
-
import { staticFile } from 'barejs';
|
|
149
|
+
import { logger, cors, staticFile } from 'barejs';
|
|
150
|
+
|
|
151
|
+
app.use(logger);
|
|
152
|
+
app.use(cors());
|
|
188
153
|
app.use(staticFile("public"));
|
|
189
154
|
|
|
190
155
|
```
|
|
@@ -193,27 +158,22 @@ app.use(staticFile("public"));
|
|
|
193
158
|
|
|
194
159
|
## 🧠 Context API
|
|
195
160
|
|
|
196
|
-
The `Context` object is pre-allocated in a circular pool to eliminate memory fragmentation.
|
|
197
|
-
|
|
198
161
|
| Method / Property | Description |
|
|
199
162
|
| --- | --- |
|
|
200
163
|
| `ctx.req` | Raw incoming Bun `Request` object. |
|
|
201
|
-
| `ctx.params` |
|
|
202
|
-
|
|
|
203
|
-
| `ctx.status(code)` | Sets the HTTP status code
|
|
204
|
-
| `ctx.json(data)` |
|
|
205
|
-
| `ctx.set(k, v)` | Stores metadata in the request-scoped store. |
|
|
206
|
-
| `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. |
|
|
207
168
|
|
|
208
169
|
---
|
|
209
170
|
|
|
210
171
|
## ⚙️ Performance Tuning
|
|
211
172
|
|
|
212
|
-
| OS Variable | Default | Description |
|
|
173
|
+
| OS Variable / File | Default | Description |
|
|
213
174
|
| --- | --- | --- |
|
|
214
|
-
| `
|
|
215
|
-
| `
|
|
216
|
-
|
|
217
|
-
---
|
|
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. |
|
|
218
178
|
|
|
219
|
-
**Maintained by [xarhang](https://github.com/xarhang) |
|
|
179
|
+
**Maintained by [xarhang](https://github.com/xarhang) | License: MIT**
|