barejs 0.1.8 → 0.1.10
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 +87 -21
- package/package.json +2 -2
- package/src/bare.ts +3 -0
- package/src/index.ts +4 -39
package/README.md
CHANGED
|
@@ -68,14 +68,42 @@ bun add barejs
|
|
|
68
68
|
Create an `index.ts` and get your server running in seconds:
|
|
69
69
|
|
|
70
70
|
```typescript
|
|
71
|
-
import { BareJS } from 'barejs';
|
|
71
|
+
import { BareJS, type Context, typebox } from 'barejs';
|
|
72
|
+
import * as TB from '@sinclair/typebox';
|
|
72
73
|
|
|
73
74
|
const app = new BareJS();
|
|
74
75
|
|
|
75
|
-
//
|
|
76
|
-
|
|
76
|
+
// Create Schema with TypeBox
|
|
77
|
+
const UserSchema = TB.Type.Object({
|
|
78
|
+
name: TB.Type.String(),
|
|
79
|
+
age: TB.Type.Number()
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// ✅ Route 1: Using TypeBox Validator (Fastest for Bun)
|
|
83
|
+
app.post('/users-tb', typebox(UserSchema), (ctx: Context) => {
|
|
84
|
+
return ctx.json({ message: "Saved via TypeBox", user: ctx.body });
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
// ✅ Route 2: Using Native Validator (Safe alternative if TypeBox has issues)
|
|
89
|
+
app.post('/users-native', native(UserSchema), (ctx: Context) => {
|
|
90
|
+
return ctx.json({ message: "Saved via Native", user: ctx.body });
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// ✅ Route 3: No Validator (Pure speed, 0 ns overhead)
|
|
94
|
+
app.get('/ping', (ctx: Context) => ctx.json({ message: "pong" }));
|
|
95
|
+
// Dynamic Path
|
|
96
|
+
app.get('/user/:id', (ctx: Context) => {
|
|
97
|
+
const userId = ctx.params.id;
|
|
98
|
+
return ctx.json({ user: userId, status: 'active' });
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// Multiple Params
|
|
102
|
+
app.get('/post/:category/:id', (ctx: Context) => {
|
|
103
|
+
return ctx.json(ctx.params); // { category: 'tech', id: '1' }
|
|
104
|
+
});
|
|
105
|
+
|
|
77
106
|
|
|
78
|
-
// Start Server
|
|
79
107
|
app.listen('0.0.0.0', 3000);
|
|
80
108
|
|
|
81
109
|
```
|
|
@@ -87,20 +115,28 @@ app.listen('0.0.0.0', 3000);
|
|
|
87
115
|
|
|
88
116
|
Every handler receives a `Context` object, providing a high-speed interface to the request:
|
|
89
117
|
|
|
90
|
-
* `ctx.req`: The
|
|
91
|
-
* `ctx.json(data)`:
|
|
92
|
-
* `ctx.
|
|
93
|
-
* `ctx.
|
|
118
|
+
* `ctx.req`: The Native Bun Request.
|
|
119
|
+
* `ctx.json(data)`: High-speed JSON helper with pre-defined headers.
|
|
120
|
+
* `ctx.params`: Object containing route parameters (e.g., { id: "123" }).
|
|
121
|
+
* `ctx.body`: Validated payload (populated by typebox, zod, or native).
|
|
122
|
+
* `ctx.status(code)`: Chainable status setter (e.g., ctx.status(201).json(...)).
|
|
123
|
+
* `ctx.setResHeader(key, value)`: Direct response header manipulation.
|
|
94
124
|
|
|
95
125
|
### 2. Middleware (The Onion Model)
|
|
96
126
|
|
|
97
|
-
BareJS supports an asynchronous recursive pipeline, allowing you to wrap logic before and after the handler.
|
|
127
|
+
BareJS supports an asynchronous recursive pipeline, allowing you to wrap logic before and after the handler. Using Context from the main package ensures full type safety.
|
|
98
128
|
|
|
99
129
|
```typescript
|
|
100
|
-
|
|
130
|
+
import { type Context, type Next } from 'barejs';
|
|
131
|
+
|
|
132
|
+
app.use(async (ctx: Context, next: Next) => {
|
|
101
133
|
const start = performance.now();
|
|
134
|
+
|
|
102
135
|
const response = await next(); // Proceed to next middleware or handler
|
|
103
|
-
|
|
136
|
+
|
|
137
|
+
const duration = (performance.now() - start).toFixed(2);
|
|
138
|
+
console.log(`Latency: ${duration}ms`);
|
|
139
|
+
|
|
104
140
|
return response;
|
|
105
141
|
});
|
|
106
142
|
|
|
@@ -108,15 +144,17 @@ app.use(async (ctx, next) => {
|
|
|
108
144
|
|
|
109
145
|
### 3. Full Plugin System
|
|
110
146
|
|
|
111
|
-
Encapsulate complex logic into reusable modules
|
|
147
|
+
Encapsulate complex logic into reusable modules. Plugins have direct access to the BareJS instance during the installation phase.
|
|
112
148
|
|
|
113
149
|
```typescript
|
|
150
|
+
import { type Context, type Next, type BareJS } from 'barejs';
|
|
151
|
+
|
|
114
152
|
const databasePlugin = {
|
|
115
153
|
name: 'barejs-db',
|
|
116
154
|
version: '1.0.0',
|
|
117
155
|
install: (app: BareJS) => {
|
|
118
|
-
app.use(async (ctx, next) => {
|
|
119
|
-
ctx.db = "CONNECTED";
|
|
156
|
+
app.use(async (ctx: Context, next: Next) => {
|
|
157
|
+
ctx.db = "CONNECTED"; // Attach custom properties to context
|
|
120
158
|
return await next();
|
|
121
159
|
});
|
|
122
160
|
}
|
|
@@ -126,6 +164,27 @@ app.use(databasePlugin);
|
|
|
126
164
|
|
|
127
165
|
```
|
|
128
166
|
|
|
167
|
+
### 4. Native WebSocket Support (MAX Speed)
|
|
168
|
+
|
|
169
|
+
BareJS leverages Bun's native WebSocket implementation, allowing you to handle real-time binary and text streams with minimal overhead.
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
import { BareJS } from 'barejs';
|
|
173
|
+
|
|
174
|
+
const app = new BareJS();
|
|
175
|
+
|
|
176
|
+
app.ws('/chat', {
|
|
177
|
+
open: (ws) => console.log('Client connected!'),
|
|
178
|
+
message: (ws, msg) => {
|
|
179
|
+
ws.send(`Echo: ${msg}`);
|
|
180
|
+
},
|
|
181
|
+
close: (ws, code, reason) => console.log('Closed')
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
app.listen();
|
|
185
|
+
|
|
186
|
+
```
|
|
187
|
+
|
|
129
188
|
---
|
|
130
189
|
|
|
131
190
|
## 🛡️ Schema Validation
|
|
@@ -133,20 +192,27 @@ app.use(databasePlugin);
|
|
|
133
192
|
BareJS allows you to use different validators for different routes, focusing on high-speed schema checks.
|
|
134
193
|
|
|
135
194
|
```typescript
|
|
136
|
-
import { typebox, zod, native } from 'barejs
|
|
195
|
+
import { BareJS, typebox, zod, native, type Context } from 'barejs';
|
|
137
196
|
import { Type } from '@sinclair/typebox';
|
|
138
197
|
import { z } from 'zod';
|
|
139
198
|
|
|
140
|
-
|
|
199
|
+
const app = new BareJS();
|
|
200
|
+
|
|
201
|
+
// 🚀 Tier 1: High Performance (TypeBox)
|
|
202
|
+
// Best for Bun. Compiled JIT validation.
|
|
141
203
|
const UserSchema = Type.Object({ name: Type.String() });
|
|
142
|
-
app.post('/tb', typebox(UserSchema), (ctx) => ctx.json(ctx.body));
|
|
204
|
+
app.post('/tb', typebox(UserSchema), (ctx: Context) => ctx.json(ctx.body));
|
|
143
205
|
|
|
144
|
-
// Popular Choice
|
|
206
|
+
// 💎 Tier 2: Popular Choice (Zod)
|
|
207
|
+
// Flexible and feature-rich.
|
|
145
208
|
const ZodSchema = z.object({ age: z.number() });
|
|
146
|
-
app.post('/zod', zod(ZodSchema), (ctx) => ctx.json(ctx.body));
|
|
209
|
+
app.post('/zod', zod(ZodSchema), (ctx: Context) => ctx.json(ctx.body));
|
|
147
210
|
|
|
148
|
-
// Zero Dependency
|
|
149
|
-
|
|
211
|
+
// 🍃 Tier 3: Zero Dependency (Native)
|
|
212
|
+
// Extremely lightweight. Uses built-in typeof checks.
|
|
213
|
+
app.post('/native', native({ properties: { id: { type: 'number' } } }), (ctx: Context) => {
|
|
214
|
+
return ctx.json(ctx.body);
|
|
215
|
+
});
|
|
150
216
|
|
|
151
217
|
```
|
|
152
218
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "barejs",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.10",
|
|
4
4
|
"author": "xarhang",
|
|
5
5
|
"description": "High-performance JIT-specialized web framework for Bun",
|
|
6
6
|
"main": "./src/bare.ts",
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"type": "module",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": "./src/bare.ts",
|
|
11
|
-
"./middleware": "./src/
|
|
11
|
+
"./middleware": "./src/validators.ts"
|
|
12
12
|
},
|
|
13
13
|
"sideEffects": false,
|
|
14
14
|
"repository": {
|
package/src/bare.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,39 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const app = new BareJS();
|
|
7
|
-
|
|
8
|
-
// Create Schema with TypeBox
|
|
9
|
-
const UserSchema = TB.Type.Object({
|
|
10
|
-
name: TB.Type.String(),
|
|
11
|
-
age: TB.Type.Number()
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
// ✅ Route 1: Using TypeBox Validator (Fastest for Bun)
|
|
15
|
-
app.post('/users-tb', typebox(UserSchema), (ctx: Context) => {
|
|
16
|
-
return ctx.json({ message: "Saved via TypeBox", user: ctx.body });
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
// ✅ Route 2: Using Native Validator (Safe alternative if TypeBox has issues)
|
|
21
|
-
app.post('/users-native', native(UserSchema), (ctx: Context) => {
|
|
22
|
-
return ctx.json({ message: "Saved via Native", user: ctx.body });
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
// ✅ Route 3: No Validator (Pure speed, 0 ns overhead)
|
|
26
|
-
app.get('/ping', (ctx: Context) => ctx.json({ message: "pong" }));
|
|
27
|
-
// Dynamic Path
|
|
28
|
-
app.get('/user/:id', (ctx: Context) => {
|
|
29
|
-
const userId = ctx.params.id;
|
|
30
|
-
return ctx.json({ user: userId, status: 'active' });
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
// Multiple Params
|
|
34
|
-
app.get('/post/:category/:id', (ctx: Context) => {
|
|
35
|
-
return ctx.json(ctx.params); // { category: 'tech', id: '1' }
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
app.listen('0.0.0.0', 3000);
|
|
1
|
+
export { BareJS } from './bare';
|
|
2
|
+
export { BareContext } from './context';
|
|
3
|
+
export type { Context, Middleware, Handler, Next } from './context';
|
|
4
|
+
export { typebox, native, zod } from './validators';
|