
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
trpc-bun-adapter
Advanced tools
Leverage TRPC on Bun with ease.
Supports both HTTP and WebSockets transports.
Install the package:
bun install @trpc/server trpc-bun-adapter
Paste the following code into server.ts
:
import {createBunServeHandler} from 'trpc-bun-adapter';
import {router} from './router';
Bun.serve(createBunServeHandler({ router }));
Run the server with HTTP and WebSocket transports:
bun run server.ts
Creates a Bun serve handler for HTTP and WebSocket transports:
import {createBunServeHandler} from 'trpc-bun-adapter';
import {router} from './router';
const createContext = (opts: { req: Request }) => ({
user: 1,
});
Bun.serve(
createBunServeHandler(
{
router,
// optional arguments:
endpoint: '/trpc', // Default to ""
createContext,
onError: console.error,
responseMeta(opts) {
return {
status: 202,
headers: {},
}
},
batching: {
enabled: true,
},
},
{
// Bun serve options
port: 3001,
fetch(request, server) {
// will be fired if it's not a TRPC request
return new Response("Hello world");
},
},
),
);
Arguments are:
options
- TRPC optionsbunOptions
- Bun serve optionsCreates a Bun HTTP handler for HTTP transport:
import {createBunHttpHandler} from 'trpc-bun-adapter';
import {router} from './router';
const createContext = (opts: { req: Request }) => ({
user: 1,
});
const bunHandler = createBunHttpHandler({
router,
// optional arguments:
endpoint: '/trpc', // Default to ""
createContext,
onError: console.error,
responseMeta(opts) {
return {
status: 202,
headers: {},
}
},
batching: {
enabled: true,
},
emitWsUpgrades: false, // pass true to upgrade to WebSocket
});
Bun.serve({
fetch(request, response) {
return bunHandler(request, response) ?? new Response("Not found", {status: 404});
}
});
Creates a Bun WebSocket handler for WebSocket transport:
import {ServerWebSocket} from "bun";
import {createBunWSHandler, BunWSClientCtx} from './src';
import {router} from './router';
const createContext = (opts: { req: Request, client: ServerWebSocket<BunWSClientCtx> }) => ({
user: 1,
});
const websocket = createBunWSHandler({
router,
// optional arguments:
createContext,
onError: console.error,
batching: {
enabled: true,
},
});
Bun.serve({
fetch(request, server) {
if (server.upgrade(request, {data: {req: request}})) {
return;
}
return new Response("Please use websocket protocol", {status: 404});
},
websocket,
});
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Feel free to open issues and pull requests.
FAQs
TRPC adapter for bun js runtime
The npm package trpc-bun-adapter receives a total of 219 weekly downloads. As such, trpc-bun-adapter popularity was classified as not popular.
We found that trpc-bun-adapter demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.