BunSai
Bonsai is a japanese art of growing and shaping miniature trees in containers
BIG NOTE
As the version implies (v0.x.x), this API is not yet stable and can be breaking changed without warnings.
Quick start
BunSai is a full-stack agnostic framework for the web, built upon Bun (in fact, it has Nunjucks and Sass as optional dependencies). You can install it:
bun add bunsai
And use it as a handler:
import BunSai from "bunsai";
const { fetch } = new BunSai({
loaders,
});
Bun.serve({
fetch,
});
How it works?
Powered by Bun.FileSystemRouter
and some fancy tricks, BunSai takes an approach where you declare the files you want to become "routes"
loaders: {
".ext": loaderInitiator
}
And all files with that file extension will be served as routes.
Example
Lets say you have the following files:
pages
├── index.njk
├── settings.tsx
├── blog
│ ├── [slug].svelte
│ └── index.ts
└── [[...catchall]].vue
You can configure BonSai to serve those files:
new BunSai({
loaders: {
".njk": nunjucksLoaderInit,
".ts": apiLoaderInit,
".tsx": reactLoaderInit,
".svelte": svelteLoaderInit,
".vue": vueLoaderInit,
},
});
Check the LoaderInitiator
interface
You can also specify file extensions that will be served staticly (return new Response(Bun.file(filePath))
), like so:
staticFiles: [".jpg", ".css", ".aac"];
There is a caveat around staticFiles
: as all files are served using the FileSystemRouter, pages/pic.jpeg
will be served as /pic
Built-in loaders
BunSai is 100% flexible, but this does not mean that it cannot be opinionated. BunSai ships with built-in (optional) loaders:
Since v0.1.0. Last change v0.2.0
Nunjucks is a rich powerful templating language with block inheritance, autoescaping, macros, asynchronous control, and more. Heavily inspired by jinja2.
bun add nunjucks @types/nunjucks
import getNunjucksLoader from "bunsai/loaders/nunjucks";
const nunjucksLoader =
getNunjucksLoader();
nunjucksLoader.env;
new BunSai({
loaders: {
".njk": nunjucksLoader.loaderInit,
},
});
<body>
{# 'server', 'route' and 'request' #}
<p>
All those objects are passed to the Nunjucks renderer to be available
globally
</p>
</body>
Since v0.1.0. Last change v0.2.0
Sass is the most mature, stable, and powerful professional grade CSS extension language in the world.
bun add sass @types/sass
import getSassLoader from "bunsai/loaders/sass";
const loaderInit = getSassLoader();
new BunSai({
loaders: {
".scss": loaderInit,
},
});
Module
Since v0.1.0. Last change v0.2.0
BonSai offers a simple module implementation to handle .ts
, .tsx
, .js
and .node
files:
import { ModuleLoader } from "bunsai/loaders";
new BunSai({
loaders: {
".ts": ModuleLoader,
},
});
A server module is a regular TS/TSX/JS/NAPI (anything that Bun can import) file that have the following structure:
export const headers = {
};
export function handler(data: ModuleData) {
}
Recommended
Since v0.1.0. Last change v0.2.0
If you liked BunSai's opinion and want to enjoy all this beauty, you can use the recommended configuration:
import getRecommended from "bunsai/recommended";
const { loaders, staticFiles } =
getRecommended();
new BunSai({
loaders,
staticFiles,
});
Check the Recommended
interface.
Middlewares
Since v0.1.0. Last change v0.2.0
Response Middlewares
You can use response middlewares to override or customize the response given by the loader.
const { middlewares } = new BunSai();
middlewares.response
.add("name", (data) => {
return new Response();
return data.response;
})
.add();
middlewares.response.remove("name").remove();
Request Middlewares
You can use request middlewares to do things before anything else, like sending an early response (e.g. 429 Too Many Requests).
const { middlewares } = new BunSai();
middlewares.request
.add("name", (data) => {
return new Response();
})
.add();
middlewares.request.remove("name").remove();
"Not Found" Middlewares
"Not Found" middlewares are only called when the router did not found the asset. The main purpose of the NF middleware is to override the default behavior (sending an empty 404 response).
const { middlewares } = new BunSai();
middlewares.notFound
.add("name", (data) => {
return new Response();
})
.add();
middlewares.notFound.remove("name").remove();