High-performance JIT-specialized web framework for Bun
npm install barejsThe "Metal" of Web Frameworks
An ultra-high-performance web engine built for Bun, architected strictly for Mechanical Sympathy.
Context objects via a circular buffer, drastically reducing GC pressure.
bash
bun add barejs
`
$3
`typescript
import { BareJS, type Context } from 'barejs';
const app = new BareJS();
// Enable Internal Logger
app.useLog(true);
app.get('/', (ctx: Context) => ctx.json({ hello: "world" }));
app.listen(3000);
`
---
๐ Comprehensive Guide
$3
BareJS v0.1.46 provides a fluent API for building responses.
`typescript
app.get('/api/v1/health', (ctx: Context) => {
// Chainable status and standardized send helper
return ctx.status(200).send("System is healthy", {
uptime: process.uptime()
});
});
// Output: { "status": "success", "msg": "System is healthy", "uptime": ... }
`
$3
`typescript
import { bareAuth, createToken, Password, Hash, type Context } from 'barejs';
const SECRET = process.env.JWT_SECRET || "your-secret";
app.post('/register', async (ctx: Context) => {
const body = await ctx.jsonBody();
// High-performance Argon2id Hashing (64MB default)
const hash = await Password.make(body.password);
// Verify with ease
const isValid = await Hash.verify(body.password, hash);
if (isValid) {
const token = await createToken({ id: 1 }, SECRET);
return { token };
}
});
// Protect routes with built-in JWT middleware
app.get('/me', bareAuth(SECRET), (ctx: Context) => {
return ctx.send("Authenticated", { user: ctx.get('user') });
});
`
$3
BareJS is the only engine that offers JIT-optimized validation paths.
`typescript
import { typebox, zod, native, t, type Context } from 'barejs';
import { z } from 'zod';
// Style A: TypeBox (JIT Optimized - Recommended)
// Pre-compiled validation to outperform competitors by 55%
const Schema = t.Object({ name: t.String() });
app.post('/user', typebox(Schema), (ctx) => ctx.json(ctx.body));
// Style B: Zod (Industry Standard)
const ZodSchema = z.object({ age: z.number() });
app.post('/age', zod(ZodSchema), (ctx) => ctx.json(ctx.body));
// Style C: Native (Zero Dependency - Simple Checks)
app.post('/native', native({ properties: { id: { type: 'number' } } }), (ctx) => ctx.json(ctx.body));
`
$3
`typescript
import { cors, staticFile } from 'barejs';
app.use(cors());
app.use(staticFile("public"));
`
---
๐ง Context API
| Method / Property | Description |
| --- | --- |
| ctx.req | Raw incoming Bun Request object. |
| ctx.params | Route parameters (e.g., :id). |
| ctx.jsonBody() | [Async] Parses and caches JSON body for performance. |
| ctx.status(code) | Sets the HTTP status code (Chainable). |
| ctx.send(msg, ext) | Returns a standardized JSON response. |
| ctx.json(data) | Returns an optimized raw JSON response. |
| ctx.setHeader(k, v) | Sets a response header. |
| ctx.set / ctx.get | Manual storage within the request lifecycle. |
---
โ๏ธ Performance Tuning
| OS Variable / File | Default | Description |
| --- | --- | --- |
| bare.config.ts | - | Centralized config for Port and Hash algorithms. |
| BARE_POOL_SIZE | 1024 | Context pool size (Must be Power of 2). |
| NODE_ENV | development | Set to production` to enable peak JIT optimizations. |