npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jonasbuerger/svelte-adapter-bun

v2.3.0

Published

Adapter for SvelteKit apps that generates a standalone Bun.js server.

Downloads

25

Readme

@jonasbuerger/svelte-adapter-bun

Release

Adapter for SvelteKit apps that generates a standalone Bun server.

Usage

Install with bun add -d @jonasbuerger/svelte-adapter-bun, then add the adapter to your svelte.config.js:

// svelte.config.js
import adapter from "@jonasbuerger/svelte-adapter-bun";

export default {
  kit: {
    adapter: adapter(),
  },
};

After building the server vite build, use vite preview to start the server.

During development, you can use vite dev, for hot-reloading:

Options

The adapter can be configured with various options:

// svelte.config.js
import adapter from "@jonasbuerger/svelte-adapter-bun";
export default {
  kit: {
    adapter: adapter({
      //build options
      out: "build",
      // precompress: true,
      precompress: {
        brotli: true,
        gzip: true,
        files: ["htm", "html"],
      },
      transpileBun: false,
      //server/adapter options
      assets: true,
      envPrefix: "MY_CUSTOM_",
      development: true, //SERVERDEV
      host: "localhost", //HOST
      port: 3000, //PORT
      tls: {
        key: "server.key",
        cert: "server.crt",
      },
      protocol_header: "X-Forwarded-Proto", //PROTOCOL_HEADER
      host_header: "X-Forwarded-Host", //HOST_HEADER
      address_header: "X-Forwarded-For", //ADDRESS_HEADER
      xff_depth: 1, //XFF_DEPTH
    }),
  },
};

out

Default: "build"

The directory to build the server to — i.e. bun run build/index.js would start the server locally after it has been created.

precompress

Default: false

Type: boolean | CompressOptions

Enables precompressing using gzip and brotli for assets and prerendered pages.

precompress.brotli

Default: false

Enable brotli precompressing.

precompress.gzip

Default: false

Enable gzip precompressing.

precompress.files

Default: ['html','js','json','css','svg','xml','wasm']

File extensions to compress.

transpileBun

Default: true

Runs buns transpiler during the server build

assets

Default: true

Serve static assets.

envPrefix

May be used to deconflict/disambiguate adapter settings with environment variables you don't control:

// svelte.config.js
export default {
  kit: {
    adapter: adapter({
      envPrefix: "MY_CUSTOM_",
    }),
  },
};
#.env
MY_CUSTOM_HOST="127.0.0.1"
MY_CUSTOM_PORT=4000
MY_CUSTOM_ORIGIN="https://my.site"

development

Default: false

Environment variable: SERVERDEV

Enables bun's error page and additional logging.

host

Default: 0.0.0.0

Environment variable: HOST

Sets the hostname on which the server accepts connections.

port

Default: 3000

Environment variable: PORT

Sets the port on which the server accepts connections.

tls

Default: []

Type: TLSOptions | TLSOptions[]

Sets the tls options for the bun server.

tls.cert

Default: ""

Type: string | string[]

The path to your certificate file relative to the project root.

tls.key

Default: ""

Type: string | string[]

The path to your key file relative to the project root.

forwarded

Default: false

Environment variable: FORWARDED

Set to true if your proxy uses the Forwarded Header. If this option is set, then protocol_header, host_header and address_header are ignored.

protocol_header

Default: ""

Environment variable: PROTOCOL_HEADER

Header set by proxy, to determine the original protocol.

host_header

Default: ""

Environment variable: HOST_HEADER

Header set by proxy, to determine the original hostname.

From SvelteKit Dokumentation:

x-forwarded-proto and x-forwarded-host are de facto standard headers that forward the original protocol and host if you're using a reverse proxy (think load balancers and CDNs). You should only set these variables if your server is behind a trusted reverse proxy; otherwise, it'd be possible for clients to spoof these headers.

address_header

Default: ""

Environment variable: ADDRESS_HEADER

Header for determining event.clientAddress behind proxies.

The RequestEvent object passed to hooks and endpoints includes an event.clientAddress property representing the client's IP address.

xff_depth

Default: 1

The count of trusted proxies before your server, used in conjunction with address_header X-Forwarded-For or the Forwarded header.

:spider_web: WebSocket Server

https://bun.sh/docs/api/websockets

// hooks.server.js, hooks.server.ts

/** @type {import("@jonasbuerger/svelte-adapter-bun").WebSocketHandler} */
export const handleWebsocket = {
  open(ws) {
    console.log("WebSocket opened");
    ws.send("Hello from Server");
  },
  /**
   * @param {Request} request
   * @param {import('bun').Server} server
   */
  upgrade(request, server) {
    const url = new URL(request.url);
    if (url.pathname.startsWith("/ws")) {
      return server.upgrade(request);
    }
  },
};

:desktop_computer: Environment variables

Some server options can also be configured via environment variables. They are documented with those settings and take precedence over the settings in svelte.config.js

If envPrefix is set, only variables starting with the prefix are used. If an unexpected environment variable is found, an Error will be thrown.

Bun automatically reads configuration from .env.local, .env.development, .env.production and .env

License

MIT © Volodymyr Palamar