Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kran6a opened this issue May 15, 2024 · 1 comment
Open

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

kran6a opened this issue May 15, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@kran6a
Copy link

kran6a commented May 15, 2024

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 kran6a added the bug Something isn't working label May 15, 2024
@sirenkovladd
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants