barejs 0.1.48 → 0.1.49
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 +63 -26
- package/dist/index.js +80 -47
- package/dist/types/client/index.d.ts +48 -0
- package/dist/types/core/bare.d.ts +14 -1
- package/dist/types/core/context.d.ts +9 -1
- package/dist/types/core/router.d.ts +1 -0
- package/dist/types/index.d.ts +6 -2
- package/dist/types/security/headers.d.ts +9 -0
- package/dist/types/security/rateLimit.d.ts +9 -0
- package/dist/types/utils/openapi.d.ts +15 -0
- package/package.json +1 -1
- package/src/client/index.ts +121 -0
- package/src/core/bare.ts +167 -19
- package/src/core/context.ts +100 -6
- package/src/core/router.ts +1 -0
- package/src/index.ts +15 -3
- package/src/security/headers.ts +59 -0
- package/src/security/rateLimit.ts +76 -0
- package/src/security/validators.ts +9 -5
- package/src/utils/openapi.ts +116 -0
package/README.md
CHANGED
|
@@ -31,11 +31,11 @@ BareJS leads in complex, real-world scenarios. We measure engine efficiency usin
|
|
|
31
31
|
|
|
32
32
|
| Framework | Latency | Speed |
|
|
33
33
|
| :--- | :--- | :--- |
|
|
34
|
-
| **BareJS** | **
|
|
35
|
-
| Elysia | 1.
|
|
36
|
-
| Hono | 5.
|
|
34
|
+
| **BareJS** | **0.85 µs** | **Baseline** |
|
|
35
|
+
| Elysia | 1.30 µs | 1.52x slower |
|
|
36
|
+
| Hono | 5.00 µs | 5.88x slower |
|
|
37
37
|
|
|
38
|
-
> Last Updated: 2026-11-
|
|
38
|
+
> Last Updated: 2026-11-17 (github action)
|
|
39
39
|
|
|
40
40
|
<!-- MARKER: PERFORMANCE_TABLE_END -->
|
|
41
41
|
|
|
@@ -45,16 +45,17 @@ BareJS leads in complex, real-world scenarios. We measure engine efficiency usin
|
|
|
45
45
|
## 🚀 Key Features
|
|
46
46
|
|
|
47
47
|
* **JIT Pipeline Compilation**: Routes and middleware chains are flattened into a single function at runtime.
|
|
48
|
-
* **Object Pooling**: Recycles `Context` objects via a
|
|
48
|
+
* **Concurrency-Safe Object Pooling**: Recycles `Context` objects via a free-list stack, drastically reducing GC pressure with zero cross-request leakage.
|
|
49
49
|
* **Internal High-Performance Logger**: Zero-overhead logging integrated directly into the JIT engine.
|
|
50
|
-
* **Precise Radix Router**:
|
|
51
|
-
* **
|
|
50
|
+
* **Precise Radix Router**: Optimized segment matching for deep-nested paths and lazy query parsing (`ctx.query`).
|
|
51
|
+
* **Real-Time & RPC Ready**: Built-in **Native WebSocket (`app.ws`)**, **SSE (`ctx.sse`)**, **JSON-RPC 2.0 (`app.rpc`)**, and **Type-Safe `bareClient`**.
|
|
52
|
+
* **AOT OpenAPI 3.1 Docs**: Zero-runtime-cost `app.docs()` with Scalar UI generated from TypeBox, Zod, and Native schemas.
|
|
53
|
+
* **Mechanical Sympathy**: Intentionally designed to align with JSC/V8 optimization and Bun's I/O.
|
|
52
54
|
|
|
53
55
|
## 🛠️ Installation
|
|
54
56
|
|
|
55
57
|
```bash
|
|
56
58
|
bun add barejs
|
|
57
|
-
|
|
58
59
|
```
|
|
59
60
|
|
|
60
61
|
### The "Bare" Minimum
|
|
@@ -70,33 +71,37 @@ app.useLog(true);
|
|
|
70
71
|
app.get('/', (ctx: Context) => ctx.json({ hello: "world" }));
|
|
71
72
|
|
|
72
73
|
app.listen(3000);
|
|
73
|
-
|
|
74
74
|
```
|
|
75
75
|
|
|
76
76
|
---
|
|
77
77
|
|
|
78
78
|
## 📘 Comprehensive Guide
|
|
79
79
|
|
|
80
|
-
### 1. ⚡ Standardized Response &
|
|
80
|
+
### 1. ⚡ Standardized Response, Lazy Query & Error Hook
|
|
81
81
|
|
|
82
|
-
BareJS v0.1.
|
|
82
|
+
BareJS v0.1.49 provides a fluent API for responses, lazy query parsing, and centralized error handling.
|
|
83
83
|
|
|
84
84
|
```typescript
|
|
85
|
+
app.onError((err, ctx) => {
|
|
86
|
+
ctx.status(500);
|
|
87
|
+
return { status: "error", message: err.message };
|
|
88
|
+
});
|
|
89
|
+
|
|
85
90
|
app.get('/api/v1/health', (ctx: Context) => {
|
|
86
|
-
|
|
87
|
-
return ctx.status(200).send("System is healthy", {
|
|
88
|
-
|
|
91
|
+
const verbose = ctx.getQuery('verbose'); // Lazy-parsed only when accessed
|
|
92
|
+
return ctx.status(200).send("System is healthy", {
|
|
93
|
+
verbose: verbose === 'true',
|
|
94
|
+
uptime: process.uptime()
|
|
89
95
|
});
|
|
90
96
|
});
|
|
91
97
|
|
|
92
|
-
// Output: { "status": "success", "msg": "System is healthy", "uptime": ... }
|
|
93
|
-
|
|
98
|
+
// Output: { "status": "success", "msg": "System is healthy", "verbose": ..., "uptime": ... }
|
|
94
99
|
```
|
|
95
100
|
|
|
96
101
|
### 2. 🛡️ Security & Authentication (Dual-API)
|
|
97
102
|
|
|
98
103
|
```typescript
|
|
99
|
-
import { bareAuth, createToken, Password, Hash, type Context } from 'barejs';
|
|
104
|
+
import { bareAuth, createToken, basicAuth, hasRole, Password, Hash, type Context } from 'barejs';
|
|
100
105
|
|
|
101
106
|
const SECRET = process.env.JWT_SECRET || "your-secret";
|
|
102
107
|
|
|
@@ -110,16 +115,15 @@ app.post('/register', async (ctx: Context) => {
|
|
|
110
115
|
const isValid = await Hash.verify(body.password, hash);
|
|
111
116
|
|
|
112
117
|
if (isValid) {
|
|
113
|
-
const token = await createToken({ id: 1 }, SECRET);
|
|
118
|
+
const token = await createToken({ id: 1, role: 'admin' }, SECRET);
|
|
114
119
|
return { token };
|
|
115
120
|
}
|
|
116
121
|
});
|
|
117
122
|
|
|
118
|
-
// Protect routes with
|
|
119
|
-
app.get('/me', bareAuth(SECRET), (ctx: Context) => {
|
|
123
|
+
// Protect routes with constant-time JWT middleware & RBAC
|
|
124
|
+
app.get('/me', bareAuth(SECRET), hasRole('admin'), (ctx: Context) => {
|
|
120
125
|
return ctx.send("Authenticated", { user: ctx.get('user') });
|
|
121
126
|
});
|
|
122
|
-
|
|
123
127
|
```
|
|
124
128
|
|
|
125
129
|
### 3. ✅ Data Validation (3 Styles)
|
|
@@ -131,7 +135,6 @@ import { typebox, zod, native, t, type Context } from 'barejs';
|
|
|
131
135
|
import { z } from 'zod';
|
|
132
136
|
|
|
133
137
|
// Style A: TypeBox (JIT Optimized - Recommended)
|
|
134
|
-
// Pre-compiled validation to outperform competitors by 55%
|
|
135
138
|
const Schema = t.Object({ name: t.String() });
|
|
136
139
|
app.post('/user', typebox(Schema), (ctx) => ctx.json(ctx.body));
|
|
137
140
|
|
|
@@ -141,17 +144,48 @@ app.post('/age', zod(ZodSchema), (ctx) => ctx.json(ctx.body));
|
|
|
141
144
|
|
|
142
145
|
// Style C: Native (Zero Dependency - Simple Checks)
|
|
143
146
|
app.post('/native', native({ properties: { id: { type: 'number' } } }), (ctx) => ctx.json(ctx.body));
|
|
144
|
-
|
|
145
147
|
```
|
|
146
148
|
|
|
147
|
-
### 4. 🔌 Essential Middleware
|
|
149
|
+
### 4. 🔌 Essential Production Middleware
|
|
148
150
|
|
|
149
151
|
```typescript
|
|
150
|
-
import { cors, staticFile } from 'barejs';
|
|
152
|
+
import { cors, rateLimit, securityHeaders, staticFile } from 'barejs';
|
|
151
153
|
|
|
152
|
-
app.use(
|
|
154
|
+
app.use(securityHeaders());
|
|
155
|
+
app.use(cors({ origin: 'https://example.com' }));
|
|
156
|
+
app.use(rateLimit({ windowMs: 60_000, max: 100 }));
|
|
153
157
|
app.use(staticFile("public"));
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### 5. 📡 Real-Time (WebSocket, SSE), OpenAPI & RPC
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
import { BareJS, bareClient } from 'barejs';
|
|
164
|
+
|
|
165
|
+
const app = new BareJS();
|
|
166
|
+
|
|
167
|
+
// Native Bun WebSocket
|
|
168
|
+
app.ws('/ws/chat', {
|
|
169
|
+
open: (ws) => ws.send('connected'),
|
|
170
|
+
message: (ws, msg) => ws.send(`echo: ${msg}`)
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
// Server-Sent Events (SSE)
|
|
174
|
+
app.get('/events', (ctx) => ctx.sse(async (send) => {
|
|
175
|
+
send({ step: 1, msg: 'streaming...' }, 'progress', '1');
|
|
176
|
+
}));
|
|
177
|
+
|
|
178
|
+
// JSON-RPC 2.0 Endpoint (single & batch)
|
|
179
|
+
app.rpc('/rpc', {
|
|
180
|
+
add: ({ a, b }: { a: number; b: number }) => a + b
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
// Auto OpenAPI 3.1 Spec (/docs/json) + Scalar UI (/docs)
|
|
184
|
+
app.docs('/docs', { title: 'My BareJS API', version: '1.0.0' });
|
|
154
185
|
|
|
186
|
+
// Type-Safe HTTP RPC Client
|
|
187
|
+
const client = bareClient('http://localhost:3000');
|
|
188
|
+
const user = await client.get('/users/:id', { params: { id: 42 }, query: { full: true } });
|
|
155
189
|
```
|
|
156
190
|
|
|
157
191
|
---
|
|
@@ -162,10 +196,13 @@ app.use(staticFile("public"));
|
|
|
162
196
|
| --- | --- |
|
|
163
197
|
| `ctx.req` | Raw incoming Bun `Request` object. |
|
|
164
198
|
| `ctx.params` | Route parameters (e.g., `:id`). |
|
|
199
|
+
| `ctx.query` | **Lazy** parsed query parameters object. |
|
|
200
|
+
| `ctx.getQuery(key)` | Returns a single query parameter value lazily. |
|
|
165
201
|
| `ctx.jsonBody()` | **[Async]** Parses and caches JSON body for performance. |
|
|
166
202
|
| `ctx.status(code)` | Sets the HTTP status code (**Chainable**). |
|
|
167
203
|
| `ctx.send(msg, ext)` | Returns a standardized JSON response. |
|
|
168
204
|
| `ctx.json(data)` | Returns an optimized raw JSON response. |
|
|
205
|
+
| `ctx.sse(source)` | Streams Server-Sent Events (`text/event-stream`). |
|
|
169
206
|
| `ctx.setHeader(k, v)` | Sets a response header. |
|
|
170
207
|
| `ctx.set / ctx.get` | Manual storage within the request lifecycle. |
|
|
171
208
|
|