Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/thanhhoajs/thanhhoa

ThanhHoa is a lightweight, high-performance web framework for Bun, designed to make server-side development simple and enjoyable.
https://github.com/thanhhoajs/thanhhoa

bun http server thanhhoa thanhhoajs

Last synced: 18 days ago
JSON representation

ThanhHoa is a lightweight, high-performance web framework for Bun, designed to make server-side development simple and enjoyable.

Awesome Lists containing this project

README

        


ThanhHoa Logo

# @thanhhoajs/thanhhoa

ThanhHoa is a high-performance, lightweight web framework for Bun, crafted to simplify server-side development while delivering maximum speed.

## Features

- ๐Ÿš€ **Built for Speed**: Utilizes Bun's non-blocking I/O for ultra-fast request processing.
- ๐Ÿงฉ **Modular Design**: Simple and flexible middleware system.
- ๐Ÿ›ฃ๏ธ **Intuitive Routing**: Supports parameters and dynamic paths.
- ๐ŸŽ›๏ธ **Full HTTP Support**: Handles GET, POST, PUT, PATCH, and DELETE methods.
- ๐Ÿ”’ **Standardized Error Handling**: Comes with `HttpException` for structured error responses.
- ๐ŸŽญ **TypeScript Support**: Fully typed for a streamlined developer experience.
- ๐Ÿ—„๏ธ **Built-in Caching**: URL caching for optimized performance.
- โฑ๏ธ **Request Timeout**: Configurable timeout for managing long-running requests.
- ๐Ÿงน **Automatic Cache Cleanup**: Regular cleanup of stale cache entries.
- ๐ŸŒ **CORS Middleware**: Flexible configuration for CORS and security headers.
- ๐Ÿ›ก๏ธ **Helmet Middleware**: Enhanced HTTP security headers.
- ๐Ÿ“ˆ **Rate Limiting**: Middleware for managing request rates from clients.
- ๐Ÿ—œ๏ธ **Response Compression**: Gzip compression middleware to reduce response size.
- ๐Ÿ—‚๏ธ **Custom Static Directories**: Supports multiple static directories for organized file management.

## Installation

Install ThanhHoa with Bun:

```bash
bun add @thanhhoajs/thanhhoa
```

## Quick Start

Hereโ€™s a quick setup to get started with ThanhHoa:

```typescript
import { ThanhHoa, type IRequestContext } from '@thanhhoajs/thanhhoa';

const app = new ThanhHoa();

app.get('/', (ctx: IRequestContext) => {
return new Response('Hello, ThanhHoa!', {
headers: { 'Content-Type': 'text/plain' },
});
});

app.listen({ port: 3000 });
```

Run your app:

```bash
bun run app.ts
```

Visit `http://localhost:3000` to see "Hello, ThanhHoa!" in your browser.

## Routing

ThanhHoa offers flexible routing with support for dynamic parameters:

```typescript
app.get('/user/:id', (ctx: IRequestContext) => {
return new Response(`User ID: ${ctx.params.id}`);
});

app.post('/user', async (ctx: IRequestContext) => {
const body = await ctx.request.json();
// Process the body...
return new Response('User created', { status: 201 });
});
```

## Middleware

ThanhHoa allows you to add middleware globally or for specific routes:

```typescript
// Custom middleware
const logger = async (ctx: IRequestContext, next: INextFunction) => {
console.log(`${ctx.request.method} ${ctx.request.url}`);
return next();
};

app.use(logger);
app.use(corsMiddleware());
app.use(helmetMiddleware());
app.use(rateLimiter({...}));
app.use(cacheMiddleware());
app.use(compression({...}));

app.get('/protected', authMiddleware, (ctx: IRequestContext) => {
return new Response('Protected route');
});
```

## Static Directory Support

Easily serve static files from multiple directories:

```typescript
const app = new ThanhHoa();

app.listen({
port: 3000,
staticDirectories: [
{
path: '/images',
directory: 'public/images',
},
{
path: '/assets',
directory: 'public/assets',
},
],
});
```

## Error Handling

Built-in error handling using `HttpException`:

```typescript
app.get('/error', () => {
throw new HttpException('Something went wrong', 500);
});
```

## Performance Benchmark

**Handling 10,000 concurrent requests:**

- **Average Latency**: 1.03ms
- **Memory Usage**: 0.01 MB

The **ThanhHoa framework** shines with sub-2ms response times and minimal memory usage, making it perfect for high-throughput applications.

_Setup_: Simple GET route (`/test`) over 5,000 iterations, 2 requests per iterationโ€”showcasing its stability and lightweight nature. ๐Ÿš€โœจ

## Author

Nguyen Nhu Khanh

## License

[MIT License](https://github.com/thanhhoajs/thanhhoa?tab=MIT-1-ov-file)