middleware icon indicating copy to clipboard operation
middleware copied to clipboard

@hono/[email protected]: Response body object should not be disturbed or locked

Open DIYgod opened this issue 1 year ago • 7 comments

After upgrading to @hono/[email protected], any request with a body results in the error: "Response body object should not be disturbed or locked."

I can also confirm that it works fine in v1.0.7.

DIYgod avatar Jun 13 '24 11:06 DIYgod

@DIYgod Thank you for rasing the issue.

@divyam234 Can you take a look?

yusukebe avatar Jun 14 '24 15:06 yusukebe

@DIYgod I am not sure if passing request body causes this error as there is already a unit test covering this.Can you elaborate more which auth strategy and platform are you using?

divyam234 avatar Jun 14 '24 16:06 divyam234

same error but it only occurs when i specify schema in @hono/zod-openapi and also i use node-server instead of edge runtime

dependencies

  • @hono/zod-openapi ^0.14.2
  • @hono/node-server ^1.11.2
  • hono ^4.4.5

request: { body: { content: { 'application/json': { schema: zod.object({ a: zod.string() }) }, } } },

but if i make schema like this error disappears request: {},

lpite avatar Jun 14 '24 19:06 lpite

@lpite Where are you providing this body and to which auth provider as in your earlier comment you have mentioned any request which is not clear.Probably you are passing body object to some fetch endpoint which authjs uses , still authjs doesn't accept this format for any route.

divyam234 avatar Jun 14 '24 19:06 divyam234

@divyam234 here's complete example

i guess i need separate issue

import { handle } from '@hono/node-server/vercel'
 import { OpenAPIHono, createRoute, z as zod } from '@hono/zod-openapi'
const app = new OpenAPIHono().basePath('/api/hono')

const testRoute = createRoute({
  path: "test",
  method: "post",
  request: {
    body: {
      content: {
        'application/json': { 
          schema: zod.object({ a: zod.string() })
        },
      }
    }
  },
  responses: {
    200: {
      description: "test response",
      content: {
        "application/json": {
          schema: zod.object({})
        }
      }
    }
  }
})

app.openapi(testRoute, (c) => {
  return c.json({})
})

export default handle(app)

lpite avatar Jun 14 '24 19:06 lpite

Hi @lpite

As you said, it seems to be not related to this matter. Can you create another issue?

yusukebe avatar Jun 16 '24 01:06 yusukebe

Hi @divyam234, I'm also experiencing this issue, the cause of the error is that the newly added code will clone the request causing the body to be read twice.

The following code will cause an error (https://stackblitz.com/edit/github-r8flbh?file=src%2Findex.ts):

import { Hono } from 'hono';
import { initAuthConfig, authHandler, getAuthUser } from '@hono/auth-js';

const app = new Hono();

app.use(
  initAuthConfig(() => ({
    secret: 'test',
    providers: [],
  }))
);

app.use('/api/auth/*', authHandler());

app.post('/test', async (ctx) => {
  const text = await ctx.req.text();
  const user = await getAuthUser(ctx);
  return ctx.text(text + user);
});

app.request('/test', { method: 'POST', body: 'test' });

export default app;
TypeError: Response body object should not be disturbed or locked
    at extractBody (https://zzmivomoqpgithub-4rwa.w-corp-staticblitz.com/builtins.ddb8d84d.js:93:87917)
    at new Request (https://zzmivomoqpgithub-4rwa.w-corp-staticblitz.com/builtins.ddb8d84d.js:93:104406)
    at cloneRequest (file:///home/projects/zzmivomoqp.github/node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected]/node_modules/@hono/auth-js/dist/index.mjs:51:10)
    at reqWithEnvUrl (file:///home/projects/zzmivomoqp.github/node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected]/node_modules/@hono/auth-js/dist/index.mjs:78:12)
    at Module.getAuthUser (file:///home/projects/zzmivomoqp.github/node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected]/node_modules/@hono/auth-js/dist/index.mjs:85:25)
    at eval (file:///home/projects/zzmivomoqp.github/src/index.ts:16:344)
    at async dispatch (file:///home/projects/zzmivomoqp.github/node_modules/.pnpm/[email protected]/node_modules/hono/dist/compose.js:44:17)
    at async eval (file:///home/projects/zzmivomoqp.github/node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected]/node_modules/@hono/auth-js/dist/index.mjs:125:5)
    at async dispatch (file:///home/projects/zzmivomoqp.github/node_modules/.pnpm/[email protected]/node_modules/hono/dist/compose.js:44:17)
    at async eval (file:///home/projects/zzmivomoqp.github/node_modules/.pnpm/[email protected]/node_modules/hono/dist/hono-base.js:216:25)

syfxlin avatar Sep 10 '24 16:09 syfxlin