oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.84k stars 2.73k forks source link

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

Closed kran6a closed 2 months ago

kran6a commented 5 months ago

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

sirenkovladd commented 5 months ago

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
kran6a commented 2 months ago

It already works, closing.