T
TanStack8mo ago
optimistic-gold

Setting Bun server options?

How can I set the server options for the bun adapter? I need to change the idleTimeout because I have a server fn that takes longer than 10s to run which keeps timing out. I tracked this down to Nitro but can't find any documentation or examples there of how to actually set options
4 Replies
xenial-black
xenial-black8mo ago
how does it work with pure nitro?
optimistic-gold
optimistic-goldOP8mo ago
Not sure, I posted a question on their Github discussions there. I don't see anything in the docs that indicates how to do this.
xenial-black
xenial-black8mo ago
ok we need to know this since we are "just" wrapping nitro
optimistic-gold
optimistic-goldOP8mo ago
I was able to figure this out, I cloned the Bun nitro preset with an idleTimeout:
import "#nitro-internal-pollyfills";
import { useNitroApp } from "nitropack/runtime";

import wsAdapter from "crossws/adapters/bun";

const nitroApp = useNitroApp();

const ws =
import.meta._websocket ? wsAdapter(nitroApp.h3App.websocket) : undefined;

// @ts-expect-error
const server = Bun.serve({
port: process.env.NITRO_PORT || process.env.PORT || 3000,
websocket: import.meta._websocket ? ws!.websocket : (undefined as any),
idleTimeout: 255,

async fetch(req: Request, server: any) {
// https://crossws.unjs.io/adapters/bun
if (import.meta._websocket && req.headers.get("upgrade") === "websocket") {
return ws!.handleUpgrade(req, server);
}

const url = new URL(req.url);

let body;
if (req.body) {
body = await req.arrayBuffer();
}

return nitroApp.localFetch(url.pathname + url.search, {
host: url.hostname,
protocol: url.protocol,
headers: req.headers,
method: req.method,
redirect: req.redirect,
body,
});
},
});

console.log(`Listening on http://localhost:${server.port}...`);
import "#nitro-internal-pollyfills";
import { useNitroApp } from "nitropack/runtime";

import wsAdapter from "crossws/adapters/bun";

const nitroApp = useNitroApp();

const ws =
import.meta._websocket ? wsAdapter(nitroApp.h3App.websocket) : undefined;

// @ts-expect-error
const server = Bun.serve({
port: process.env.NITRO_PORT || process.env.PORT || 3000,
websocket: import.meta._websocket ? ws!.websocket : (undefined as any),
idleTimeout: 255,

async fetch(req: Request, server: any) {
// https://crossws.unjs.io/adapters/bun
if (import.meta._websocket && req.headers.get("upgrade") === "websocket") {
return ws!.handleUpgrade(req, server);
}

const url = new URL(req.url);

let body;
if (req.body) {
body = await req.arrayBuffer();
}

return nitroApp.localFetch(url.pathname + url.search, {
host: url.hostname,
protocol: url.protocol,
headers: req.headers,
method: req.method,
redirect: req.redirect,
body,
});
},
});

console.log(`Listening on http://localhost:${server.port}...`);
Then set it as the server entry point in app.config.ts:
export default defineConfig({
server: {
entry: "./bun.nitro.ts",
},
...
});
export default defineConfig({
server: {
entry: "./bun.nitro.ts",
},
...
});

Did you find this page helpful?