{"_id":"@calle.wester/svelte-adapter-bun","name":"@calle.wester/svelte-adapter-bun","dist-tags":{"latest":"0.5.7"},"versions":{"0.5.7":{"name":"@calle.wester/svelte-adapter-bun","version":"0.5.7","description":"Adapter for SvelteKit apps that generates a standalone Bun.js server.","main":"index.js","types":"index.d.ts","type":"module","exports":{".":{"types":"./index.d.ts","import":"./index.js"},"./package.json":"./package.json"},"scripts":{"build":"rm -fr files && bun run build.js","lint":"prettier --check .","format":"prettier --write .","prepublish":"bun run build"},"repository":{"type":"git","url":"git+https://github.com/calle/svelte-adapter-bun.git"},"keywords":["svelte","sveltekit","sveltekit-adapter","bun"],"author":{"name":"Calle Wester","email":"@calle"},"license":"MIT","bugs":{"url":"https://github.com/calle/svelte-adapter-bun/issues"},"homepage":"https://github.com/calle/svelte-adapter-bun#readme","devDependencies":{"@sveltejs/kit":"^1.27.3","bun-types":"^1.0.9","mrmime":"^1.0.1","prettier":"^3.0.3","prettier-plugin-svelte":"^3.1.2","totalist":"^3.0.1"},"dependencies":{"tiny-glob":"^0.2.9"},"_id":"@calle.wester/svelte-adapter-bun@0.5.7","gitHead":"3e4ef78a7ccb4f65372bec630d6c44b1647c7991","_nodeVersion":"21.2.0","_npmVersion":"10.2.3","dist":{"integrity":"sha512-FhiJthy/sXiAaExxo9Hviqhg5HTVO7XFdtpUPiekOZgzgcXQw8SIOxlRQ6/9byGI6xGyO1UP+C0NKAfd8LXNjA==","shasum":"9a63009f67b3fa407e8d645cc335cffed8a1500a","tarball":"https://registry.npmjs.org/@calle.wester/svelte-adapter-bun/-/svelte-adapter-bun-0.5.7.tgz","fileCount":9,"unpackedSize":52387,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIHw1GxtUZeEqHnS1NFETChu8hhrcngeViPa8ClibiyJNAiA+4POYYKgSktIILZTMn1FajPZkX0zy3lRuJhCqhu1YHg=="}]},"_npmUser":{"name":"calle.wester","email":"lennartsson@gmail.com"},"directories":{},"maintainers":[{"name":"calle.wester","email":"lennartsson@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/svelte-adapter-bun_0.5.7_1702504291537_0.8670972669148442"},"_hasShrinkwrap":false}},"time":{"created":"2023-12-13T21:51:31.403Z","0.5.7":"2023-12-13T21:51:31.713Z","modified":"2023-12-13T21:51:32.005Z"},"maintainers":[{"name":"calle.wester","email":"lennartsson@gmail.com"}],"description":"Adapter for SvelteKit apps that generates a standalone Bun.js server.","homepage":"https://github.com/calle/svelte-adapter-bun#readme","keywords":["svelte","sveltekit","sveltekit-adapter","bun"],"repository":{"type":"git","url":"git+https://github.com/calle/svelte-adapter-bun.git"},"author":{"name":"Calle Wester","email":"@calle"},"bugs":{"url":"https://github.com/calle/svelte-adapter-bun/issues"},"license":"MIT","readme":"# @calle.wester/svelte-adapter-bun\n\n[Adapter](https://kit.svelte.dev/docs/adapters) for SvelteKit apps that generates a standalone [Bun](https://github.com/oven-sh/bun) server.\n\n## :zap: Usage\n\nInstall with `bun add -d @calle.wester/svelte-adapter-bun`, then add the adapter to your `svelte.config.js`:\n\n```js\n// svelte.config.js\nimport adapter from \"@calle.wester/svelte-adapter-bun\";\n\nexport default {\n  kit: {\n    adapter: adapter(),\n  },\n};\n```\n\nAfter building the server (`vite build`), use the following command to start:\n\n```\n# go to build directory\ncd build/\n\n# run Bun\nbun run start\n```\n\n## :gear: Options\n\nThe adapter can be configured with various options:\n\n```js\n// svelte.config.js\nimport adapter from \"@calle.wester/svelte-adapter-bun\";\nexport default {\n  kit: {\n    adapter: adapter({\n      out: \"build\",\n      assets: true,\n      envPrefix: \"MY_CUSTOM_\",\n      development: true,\n      // precompress: true,\n      precompress: {\n        brotli: true,\n        gzip: true,\n        files: [\"htm\", \"html\"],\n      },\n      dynamic_origin: true,\n      xff_depth: 1,\n    }),\n  },\n};\n```\n\n### out\n\nThe directory to build the server to. It defaults to `build` — i.e. `bun run start` would start the server locally after it has been created.\n\n### assets\n\nBrowse a static assets. Default: `true`\n\n- [x] Support [HTTP range requests](https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests)\n\n### precompress\n\nEnables precompressing using gzip and brotli for assets and prerendered pages. It defaults to `false`.\n\n#### brotli\n\nEnable brotli precompressing. It defaults to `false`.\n\n#### gzip\n\nEnable gzip precompressing. It defaults to `false`.\n\n#### files\n\nfile extensions to compress.It defaults to `['html','js','json','css','svg','xml','wasm']`.\n\n### envPrefix\n\nIf you need to change the name of the environment variables used to configure the deployment (for example, to deconflict with environment variables you don't control), you can specify a prefix:\n\n```js\nenvPrefix: \"MY_CUSTOM_\";\n```\n\n```\nMY_CUSTOM_HOST=127.0.0.1 \\\nMY_CUSTOM_PORT=4000 \\\nMY_CUSTOM_ORIGIN=https://my.site \\\nbun build/index.js\n```\n\n### development\n\nThis enables bun's error page. Default: `false`\n\n### dynamic_origin\n\nIf enabled use `PROTOCOL_HEADER` `HOST_HEADER` like origin. Default: `false`\n\n### xff_depth\n\nThe default value of XFF_DEPTH if environment is not set. Default: `1`\n\n## :spider_web: WebSocket Server\n\nhttps://bun.sh/docs/api/websockets\n\n```js\n// hooks.server.js\n\n/** @type {import(\"@calle.wester/svelte-adapter-bun\").WebSocketHandler} */\nexport const handleWebsocket = {\n  open(ws) {\n    console.log(\"WebSocket opened\");\n    ws.send(\"Slava Ukraїni\");\n  },\n  /**\n   * @param {Request} request\n   * @param {Function} upgrade\n   */\n  upgrade(request, upgrade) {\n    const url = new URL(request.url);\n    if (url.pathname.startsWith(\"/ws\")) {\n      return upgrade(request);\n    }\n  },\n};\n```\n\n## Polyfills\n\nIf you need to use polyfills in your app, you can add them to the [`src/polyfills.js`](src/polyfills.js) file:\n\n```js\nclass Polifill {\n  constructor() {\n    ...\n  }\n}\n\nconst globals = {\n  Polifill,\n};\n```\n\n## :desktop_computer: Environment variables\n\n> Bun automatically reads configuration from `.env.local`, `.env.development` and `.env`\n\n### `PORT` and `HOST`\n\nBy default, the server will accept connections on `0.0.0.0` using port 3000. These can be customized with the `PORT` and `HOST` environment variables:\n\n```\nHOST=127.0.0.1 PORT=4000 bun build/index.js\n```\n\n### `ORIGIN`, `PROTOCOL_HEADER` and `HOST_HEADER`\n\nHTTP doesn't give SvelteKit a reliable way to know the URL that is currently being requested. The simplest way to tell SvelteKit where the app is being served is to set the `ORIGIN` environment variable:\n\n```\nORIGIN=https://my.site bun build/index.js\n```\n\nWith this, a request for the `/stuff` pathname will correctly resolve to `https://my.site/stuff`. Alternatively, you can specify headers that tell SvelteKit about the request protocol and host, from which it can construct the origin URL:\n\n```\nPROTOCOL_HEADER=x-forwarded-proto HOST_HEADER=x-forwarded-host bun build/index.js\n```\n\n> [`x-forwarded-proto`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Proto) and [`x-forwarded-host`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/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.\n\n### `ADDRESS_HEADER` and `XFF_DEPTH`\n\nThe [RequestEvent](https://kit.svelte.dev/docs/types#additional-types-requestevent) object passed to hooks and endpoints includes an `event.clientAddress` property representing the client's IP address. [Bun.js haven't got functionality](https://github.com/Jarred-Sumner/bun/issues/518) to get client's IP address, so SvelteKit will receive `127.0.0.1` or if your server is behind one or more proxies (such as a load balancer), you can get an IP address from headers, so we need to specify an `ADDRESS_HEADER` to read the address from:\n\n```\nADDRESS_HEADER=True-Client-IP bun build/index.js\n```\n\n> Headers can easily be spoofed. As with `PROTOCOL_HEADER` and `HOST_HEADER`, you should [know what you're doing](https://adam-p.ca/blog/2022/03/x-forwarded-for/) before setting these.\n> If the `ADDRESS_HEADER` is `X-Forwarded-For`, the header value will contain a comma-separated list of IP addresses. The `XFF_DEPTH` environment variable should specify how many trusted proxies sit in front of your server. E.g. if there are three trusted proxies, proxy 3 will forward the addresses of the original connection and the first two proxies:\n\n```\n<client address>, <proxy 1 address>, <proxy 2 address>\n```\n\nSome guides will tell you to read the left-most address, but this leaves you [vulnerable to spoofing](https://adam-p.ca/blog/2022/03/x-forwarded-for/):\n\n```\n<spoofed address>, <client address>, <proxy 1 address>, <proxy 2 address>\n```\n\nInstead, we read from the _right_, accounting for the number of trusted proxies. In this case, we would use `XFF_DEPTH=3`.\n\n> If you need to read the left-most address instead (and don't care about spoofing) — for example, to offer a geolocation service, where it's more important for the IP address to be _real_ than _trusted_, you can do so by inspecting the `x-forwarded-for` header within your app.\n\n## License\n\n[MIT](LICENSE) © [Volodymyr Palamar](https://github.com/gornostay25)\n","readmeFilename":"README.md"}