2.3.0 • Published 1 year ago

@jonasbuerger/svelte-adapter-bun v2.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

@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

2.3.0

1 year ago

2.2.0

1 year ago

2.1.0

1 year ago

2.0.4

1 year ago

2.0.3

1 year ago

2.0.2

1 year ago

2.0.0

1 year ago

1.1.13

1 year ago

1.1.12

1 year ago

1.1.11

1 year ago

1.1.10

1 year ago

1.1.9

1 year ago

1.1.8

1 year ago

1.1.7

1 year ago

1.1.6

1 year ago

1.1.5

1 year ago

1.1.4

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

0.6.0

1 year ago