Reference for the platform primitives, defaults, and extension points.
A helper function to quickly bootstrap a standard server with common modules.
interface BunKitStandardServerOptions {
adapter?: ServerAdapter;
modules?: {
cors?: BaseServerModule | false;
security?: BaseServerModule | false;
rateLimit?: BaseServerModule | false;
fileUpload?: BaseServerModule | false;
requestContext?: BaseServerModule | false;
extra?: BaseServerModule[];
};
services?: BaseServerService[];
}
// Controllers with optional services/options
function BunKitStandardServer(
port: number,
module: ControllersModule,
servicesOrOptions?: BaseServerService[] | BunKitStandardServerOptions,
maybeOptions?: BunKitStandardServerOptions
): BunKitServer;When you only need to tweak modules, you can skip the services array and pass the options object directly as the next argument.
addModule(module: BaseServerModule): Add a single moduleaddModules(modules: BaseServerModule[]): Add multiple modulesaddService(service: BaseServerService): Add a single serviceaddServices(services: BaseServerService[]): Add multiple servicesgetApp(): Get the server application instancegetServer(): Get the underlying server instancegetModule<T>(moduleClass): Get a specific module instancehasModule(moduleClass): Check if a module is registeredstart(): Start the servergracefulShutdown(): Perform graceful shutdown
Uploads are only enabled when FileUploadModule is registered. Configure multipart limits and MIME allowlists through the module.
import { FileUploadModule } from "bun-platform-kit";
new FileUploadModule({
maxBodyBytes: 20 * 1024 * 1024,
maxFileBytes: 5 * 1024 * 1024,
maxFiles: 3,
allowedMimeTypes: ["image/*", "application/pdf"],
});Other Bun settings:
app.set("trustProxy", 1); // Trust exactly the proxy directly in front of Bun
app.set("trustProxy", ["127.0.0.1/8"]); // CIDR allowlist for proxies
// app.set("trustProxy", (ip, hop) => hop === 0 && ip === "127.0.0.1");
app.set("handlerTimeoutMs", 30_000); // 0/undefined disablestrustProxy resolves req.ip from right to left through X-Forwarded-For and
stops at the first untrusted address. A numeric value trusts that many closest
proxy hops. CIDR arrays and custom functions trust only matching hops. Do not
trust every hop: proxies can preserve unverified values supplied at the left of
the forwarded chain.
Defaults (Bun):
maxBodyBytes: 10 MBmaxFileBytes: 10 MBmaxFiles: 10allowedMimeTypes: allow all
Cookies:
req.cookiesis parsed from the incomingcookieheader.res.cookie(name, value, options)sets cookies;maxAgeis milliseconds.sameSite: "none"forcessecure: true.- When using Bun CookieMap,
maxAgeis converted to seconds before setting (Bun expects seconds).
Handler timeouts (Bun):
handlerTimeoutMstriggers a 504 response if middleware never callsnext()or ends the response.
Multipart note (Bun):
request.formData()is not streaming in Bun; payloads are fully buffered before parsing. This is fine for small/medium uploads but risky for large files or hostile traffic.- For large uploads, prefer direct-to-object-storage flows (S3/R2/MinIO) with signed URLs; keep the backend for signing and metadata validation.
Uploads shape (Bun):
req.filesisRecord<string, File | File[]>(single or array per field).- Use
getFile(req, "field")for single uploads andgetFiles(req, "field")for multiple.
Response note (Bun):
- If you
res.send(new Response(...)), do not read/consume the Response body before sending; streamed bodies cannot be reused.
priority: number: Initialization priority (default: 0)getModuleName(): string: Module identifierinit(app: ServerApp, context: ServerContext): Module initialization methodshutdown(): Optional cleanup method
name: string: Service identifierstart(server: ServerInstance): Service startup methodstop(): Optional cleanup method
Creates an initialized BunKitServer configured with the provided decorated controllers, returning the app and a stop helper for cleanup.
type DecoratedTestAppOptions = {
controllers?: Array<new (...args: any[]) => any>;
controllersModule?: ControllersModule;
port?: number;
services?: BaseServerService[];
standardOptions?: BunKitStandardServerOptions;
};
type DecoratedTestAppResult = {
app: ServerApp;
server: BunKitServer;
stop: () => Promise<void>;
};The helper disables CorsModule, SecurityModule, and RateLimitModule by default. Use standardOptions.modules to re-enable or replace them for specific scenarios.