Skip to content

Commit

Permalink
Change: split index.ts into mutiple ts files
Browse files Browse the repository at this point in the history
  • Loading branch information
NobleMajo committed Jan 18, 2025
1 parent 457fdd0 commit 6c2863c
Show file tree
Hide file tree
Showing 7 changed files with 1,683 additions and 1,661 deletions.
55 changes: 29 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,58 +1,60 @@

# bun-router
![MIT](https://img.shields.io/badge/license-MIT-blue.svg)
![typescript](https://img.shields.io/badge/dynamic/json?style=plastic&color=blue&label=Typescript&prefix=v&query=peerDependencies.typescript&url=https%3A%2F%2Fraw.githubusercontent.com%2Fnoblemajo%2Fbunrouter%2Fmain%2Fpackage.json)
![](https://img.shields.io/badge/dynamic/json?color=green&label=watchers&query=watchers&suffix=x&url=https%3A%2F%2Fapi.github.com%2Frepos%2Fnoblemajo%2Fbunrouter)
![](https://img.shields.io/badge/dynamic/json?color=yellow&label=stars&query=stargazers_count&suffix=x&url=https%3A%2F%2Fapi.github.com%2Frepos%2Fnoblemajo%2Fbunrouter)
![](https://img.shields.io/badge/dynamic/json?color=navy&label=forks&query=forks&suffix=x&url=https%3A%2F%2Fapi.github.com%2Frepos%2Fnoblemajo%2Fbunrouter)
<!-- ![](https://img.shields.io/badge/dynamic/json?color=darkred&label=open%20issues&query=open_issues&suffix=x&url=https%3A%2F%2Fapi.github.com%2Frepos%2Fnoblemajo%2Fbunrouter)
![](https://img.shields.io/badge/dynamic/json?color=orange&label=subscribers&query=subscribers_count&suffix=x&url=https%3A%2F%2Fapi.github.com%2Frepos%2Fnoblemajo%2Fbunrouter) -->
![typescript](https://img.shields.io/badge/dynamic/json?style=plastic&color=blue&label=Typescript&prefix=v&query=peerDependencies.typescript&url=https%3A%2F%2Fraw.githubusercontent.com%2Fnoblemajo%2Fbun-router%2Fmain%2Fpackage.json)
![](https://img.shields.io/badge/dynamic/json?color=green&label=watchers&query=watchers&suffix=x&url=https%3A%2F%2Fapi.github.com%2Frepos%2Fnoblemajo%2Fbun-router)
![](https://img.shields.io/badge/dynamic/json?color=yellow&label=stars&query=stargazers_count&suffix=x&url=https%3A%2F%2Fapi.github.com%2Frepos%2Fnoblemajo%2Fbun-router)
![](https://img.shields.io/badge/dynamic/json?color=navy&label=forks&query=forks&suffix=x&url=https%3A%2F%2Fapi.github.com%2Frepos%2Fnoblemajo%2Fbun-router)
<!-- ![](https://img.shields.io/badge/dynamic/json?color=darkred&label=open%20issues&query=open_issues&suffix=x&url=https%3A%2F%2Fapi.github.com%2Frepos%2Fnoblemajo%2Fbun-router)
![](https://img.shields.io/badge/dynamic/json?color=orange&label=subscribers&query=subscribers_count&suffix=x&url=https%3A%2F%2Fapi.github.com%2Frepos%2Fnoblemajo%2Fbun-router) -->

# bunrouter
**A fast, Express-like router for the high-performance `bun.serve()` HTTP server.**

A fast express-like router written for the bun.serve http server.
Uses the bun.js high-performance [bun.serve()](https://bun.sh/docs/api/http) http server for a fast, familiar and solid routing experience.
Bun-Router leverages Bun.js's powerful `bun.serve` to deliver a fast, familiar, and reliable routing experience. It provides developers with an easy-to-use, Express-like API, tailored for high-performance applications built with Bun.js.

Bun does not have a built-in router, so I created this one without any runtime dependencie.
*At the **time of Bun-Router's creation**, Bun.js did not include a built-in router, so Bun-Router was designed as a lightweight, dependency-free solution to fill that gap.*

# features

- **non-async-first**: Tries to resolve a request in a non-async way until an async handler is hit.
## life improvement

- **wildcards**: Can handle double wildcards (`**`) as any recursive path and
single wildcards (`*`) as any path part. *It also provides a path params string array.*
- **cookie-handling**: Cookie parsing can be enabled via a middleware ([example](https://github.com/NobleMajo/bunrouter/blob/main/examples/cookies.ts)).
If enabled cookies can also automatically be set/unset to the response headers.
- **static-serve**: Serves static files via a middleware ([example](https://github.com/NobleMajo/bunrouter/blob/main/examples/static-serve.ts)).
single wildcards (`*`) as any path part. *It also provides a path parameter string array".*
If enabled, cookies can also automatically be set/unset to the response headers.
- **static-serve**: Serves static files via a middleware ([example](https://github.com/NobleMajo/bun-router/blob/main/examples/static-serve.ts)).
- **dump-router**: You can create a string router dump that lists the defined routes.
If you provide a bun server, it also adds a `server-is-running-on` message.
- **basic-auth**: Protects the following via HTTP basic auth ([example](https://github.com/NobleMajo/bun-router/blob/main/examples/basic-auth.ts)).
- **cookie-handling**: Cookie parsing can be enabled via a middleware ([example](https://github.com/NobleMajo/bun-router/blob/main/examples/cookies.ts)).
- **redirect-handler**: You can redirect via the `ResponseBuilder` or
via a redirect middleware ([example](https://github.com/NobleMajo/bun-router/blob/main/examples/redirect.ts)).
- **websocket-support**: Can handle websocket request via a middleware ([example](https://github.com/NobleMajo/bun-router/blob/main/examples/websocket.ts)).
- **basic-auth**: Protects the following via http basic auth ([example](https://github.com/NobleMajo/bun-router/blob/main/examples/basic-auth.ts)).

## performance

- **non-async-first**: Tries to resolve a request in a non-async way until an async handler is hit.
- **merged-routes**: If 2 or more routes are defined one after the other with the same method and path,
they will be merged into a single handler to avoid re-checking.
- **method-enum**: You just write `GET`, but in the background it is converted to an enum.
*This is for faster method comparison.*
- **dump-router**: You can create a string router dump that lists the defined routes.
If you provide a bun server, it also adds a `server-is-running-on` message.

# how it works

# usage

## install

```sh
bun i github:NobleMajo/bunrouter
bun i github:NobleMajo/bun-router
```

## import

```ts
import { Router } from "bunrouter/src/index";
import { Router } from "bun-router/src/index";
```

## example

```ts
import { Router } from "bunrouter/src/index";
import { Router } from "bun-router/src/index";

const router = new Router()

Expand All @@ -61,7 +63,8 @@ router.get("/", (req, res) => {
res.send("Root request")
})

export const server = Bun.serve({
// use the native bun.serve function the router as request handler
const server = Bun.serve({
fetch: router.handle,
})

Expand All @@ -81,7 +84,7 @@ Checkout the bun-router [examples](https://github.com/NobleMajo/bun-router/tree/

Run a example:
```sh
git clone https://github.com/NobleMajo/bunrouter.git
git clone https://github.com/NobleMajo/bun-router.git

bun run examples/simple.ts
```
Expand Down
Loading

0 comments on commit 6c2863c

Please sign in to comment.