bun icon indicating copy to clipboard operation
bun copied to clipboard

@trpc/server does not work on bun while it works on ts-node

Open kran6a opened this issue 9 months ago • 1 comments

What version of Bun is running?

1.1.8+89d25807f

What platform is your computer?

Linux 5.15.146.1-microsoft-standard-WSL2 x86_64 x86_64

What steps can reproduce the bug?

  1. pnpm init
  2. pnpm install @trpc/server@next @trpc/client@next
  3. Create an index.ts file
import { initTRPC } from '@trpc/server';
import { createHTTPServer } from '@trpc/server/adapters/standalone';
const t = initTRPC.create();

export const router = t.router;
export const publicProcedure = t.procedure;

const appRouter = router({
    greeting: publicProcedure
        .input({parse: (c)=>c})
        .mutation(({input}) => `Hello ${input.name}`),
});

createHTTPServer({
    router: appRouter,
    createContext() {
        return {};
    },
}).listen(2022);

export type AppRouter = typeof appRouter;
  1. Create a client.ts file
import { createTRPCClient, httpBatchLink } from '@trpc/client';
import type { AppRouter } from './index';
const client = createTRPCClient<AppRouter>({
    links: [
        httpBatchLink({
            url: 'http://localhost:2022/',
        }),
    ],
});

console.log(await client.greeting.mutate({name: 'Kranga'}));

What is the expected behavior?

The same as when index.ts is executed with ts-node.

ts-node index.ts bun client.ts

console logs Hello Kranga on the client

What do you see instead?

bun index.ts bun client.s

An error is thrown:

42 |     constructor(message, opts){
43 |         const cause = opts?.cause;
44 |         // eslint-disable-next-line @typescript-eslint/ban-ts-comment
45 |
46 |         // @ts-ignore https://github.com/tc39/proposal-error-cause
47 |         super(message, {
             ^
TRPCClientError: JSON Parse error: Unexpected EOF

Apparently it has to do with streams since it seems the request body is empty when trpc attempts to read it.

Additional information

No response

kran6a avatar May 15 '24 08:05 kran6a

minimal reproduce

const http = require("http");

const server = http.Server((req, res) => {
  setTimeout(() => {
    console.log(req.read()?.toString());
    res.end()
  })
}).listen(0, async () => {
  const req = http.request({ port: server.address().port, method: 'POST' }, () => server.close());
  req.write('test message');
  req.end();
})
❯ bun reproduce.cjs 
undefined
❯ node reproduce.cjs
test message

sirenkovladd avatar May 15 '24 20:05 sirenkovladd

It already works, closing.

kran6a avatar Aug 10 '24 13:08 kran6a