Socket
Socket
Sign inDemoInstall

bonsai.js

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    bonsai.js

BonSai is a full-stack agnostic framework for the web, built upon Bun.


Version published
Maintainers
1
Created

Readme

Source

BonSai.js

Japanese art of growing and shaping miniature trees in containers

Quick start

BonSai 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 bonsai.js

And use it as a handler:

import BonSai from "bonsai.js";

const { fetch } = BonSai({
  /*
    "loaders" is the only required property, as it configures BonSai's behavior
  */
  loaders,
});

Bun.serve({
  fetch,
});

How it works?

Powered by Bun.FileSystemRouter and some fancy tricks, BonSai takes an approach where you declare the files you want to become "routes"

loaders: {
  ".ext": loader
}

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:

BonSai({
  loaders: {
    ".njk": nunjucksLoader,
    ".ts": apiLoader,
    ".tsx": reactLoader,
    ".svelte": svelteLoader,
    ".vue": vueLoader,
  },
});

Check the Loader 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

BonSai is 100% flexible, but this does not mean that it cannot be opinionated. BonSai ships with built-in (optional) loaders:

Nunjucks

Since v0.1.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 "bonsai.js/loaders/nunjucks";

const { loader, env } =
  getNunjucksLoader(/* (optional) root path and nunjucks configure options */);

// you can make changes on the nunjucks Environment object (the 'env' object).
// See https://mozilla.github.io/nunjucks/api.html#environment

BonSai({
  loaders: {
    ".njk": loader,
  },
});
<body>
  {# 'server', 'route' and 'request' #}

  <p>
    All those objects are passed to the Nunjucks renderer to be available
    globally
  </p>
</body>

Sass

Since v0.1.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 "bonsai.js/loaders/sass";

const loader = getSassLoader(/* (optional) sass compiler options */);

BonSai({
  loaders: {
    ".scss": loader,
  },
});

Module

Since v0.1.0

BonSai offers a simple module implementation to handle .ts, .tsx, .js and .node files:

import { ModuleLoader } from "bonsai.js/loaders";

BonSai({
  loaders: {
    ".ts": ModuleLoader,
  },
});

A server module is a regular TS/TSX/JS/NAPI (anything that Bun can import) file that have the following structure:

// optional
export const headers = {
  // All reponse headers go here.
  // The default Content-Type header is "text/html; charset=utf-8", but you can override it.
};

// required
export function handler(data: ModuleData) {
  // data.server => Server
  // data.route => MatchedRoute
  // data.request => Request
  // The handler must return a BodyInit or an instance of Response, whether synchronously or asynchronously.
  // If Response is returned, the loader will send it and the "headers" export will be ignored.
}

Since v0.1.0

If you liked BonSai's opinion and want to enjoy all this beauty, you can use the recommended configuration:

import getRecommended from "bonsai.js/recommended";

const { loaders, staticFiles, nunjucksEnv } =
  getRecommended(/* (optional) nunjucks and sass options */);

BonSai({
  loaders,
  staticFiles,
});

Check the Recommended interface.

Middlewares

Since v0.2.0

You can use middlewares to override or customize the response given by the loader.

const { addMiddleware, removeMiddleware } = BonSai(/* ... */);

addMiddleware(
  "name it so you can remove it later",
  (response, request, server) => {
    // you can stop the middleware execution chain by returning a Response

    // if you want to stop the chain and override the response, return a new Response object
    return new Response();

    // if you want to just stop the chain, return the same Response object
    return response;
  }
)
  .addMiddleware(/* can be chained */);

removeMiddleware(
  "name it so you can remove it later"
)
  .removeMiddleware(/* can be chained */);

Keywords

FAQs

Last updated on 12 Feb 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc