elysia icon indicating copy to clipboard operation
elysia copied to clipboard

body issue for mounted apps - SyntaxError: Unexpected end of JSON input

Open stewones opened this issue 1 year ago • 4 comments

What version of Elysia.JS is running?

0.8.15

What platform is your computer?

Darwin 23.2.0 arm64 arm

What steps can reproduce the bug?

import { Elysia } from "elysia";

const app2 = new Elysia().post("/", ({ body }) => body);

const app1 = new Elysia()
  .mount("/app2", app2)
  .post("/", ({ body }) => body)
  .listen(3000);

console.log(
  `🦊 Elysia is running at ${app1.server?.hostname}:${app1.server?.port}`
);

bun run the app and post to /app2 with some body. here's a quick video

https://github.com/elysiajs/elysia/assets/719763/efb4a562-24f5-41d2-92bf-ad453476293d

What is the expected behavior?

to get the body json parsed correctly

What do you see instead?

SyntaxError: Unexpected end of JSON input

Additional information

this only happens when mounting a secondary app

repo with reproduction https://github.com/stewones/elysia-body-issue/blob/main/src/index.ts

stewones avatar Feb 01 '24 01:02 stewones

use is a workaround

const app2 = new Elysia({ prefix: '/app2' }).post('/', ({ body }) => body)
const app1 = new Elysia()
	.use(app2)
	.post('/', ({ body }) => body)
	.listen(3000)

stewones avatar Feb 01 '24 04:02 stewones

This issue seems to be related to scoped being true. Here's a simple example:

import { edenTreaty } from "@elysiajs/eden"
import { Elysia, t } from "elysia"

const a = new Elysia({ prefix: "/a", scoped: true }).post(
  "/b",
  ({ body }) => body.name,
  {
    body: t.Object({
      name: t.String(),
    }),
  },
)

const app = new Elysia().use(a).listen(3000)

const api = edenTreaty<typeof app>("http://localhost:3000")

const { data, error } = await api.a.b.post({ name: "Elysia" })
console.log("data:", data)
console.log("error:", error)

Running the above code results in:

1 | var c=(t,e,r)=>{if(t.endsWith("/")||(t+="/"),e==="index"&&(e=""),!r||!Object.keys(r).length)return`${t}${e}`;let s="";for(let[i,u]of Object.entries(r))s+=`${i}=${u}&`;return`${t}${e}?${s.slice(0,-1)}`},o=t=>t.trim().length!==0&&!Number.isNaN(Number(t));var n=class extends Error{constructor(r,s){super(s+"");this.status=r;this.value=s}};export{c as a,o as b,n as c};
                                                                                                                                                                                                                                                                                                            ^
error: {"name":"SyntaxError","message":"Unexpected end of JSON input"}
      at new n (.../node_modules/@elysiajs/eden/dist/chunk-HUZ6GXDC.mjs:1:297)
      at .../node_modules/@elysiajs/eden/dist/chunk-Q3DJMDQ2.mjs:1:2695

But after changing scoped: true to scoped: false the error goes away, and running the above code results in:

data: Elysia
error: null

elliotwaite avatar Feb 22 '24 09:02 elliotwaite

I am not using the scoped: true at all and still getting the error

EDIT: Oh, but I noticed I was still using mount instead of use in a part of the hierarchy

msanchezdev avatar Mar 07 '24 22:03 msanchezdev

@elliotwaite I have the same issue with scoped: true not parsing the JSON correctly. ~I did not spend 2 hours debugging the JSON parse issue.~

cybercoder-naj avatar Mar 09 '24 02:03 cybercoder-naj

this is the line responsible for this bug. Constructing new Request object discards body stream.

ayZagen avatar Aug 12 '24 16:08 ayZagen

Fixed with ba09081, going to published under 1.1.11.

SaltyAom avatar Aug 30 '24 18:08 SaltyAom