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

"Response body object should not be disturbed or locked" using body destructuring assignment in derive({ body }) #31

Open
ggondim opened this issue Feb 7, 2025 · 1 comment
Assignees
Labels
bug Something isn't working question Further information is requested

Comments

@ggondim
Copy link

ggondim commented Feb 7, 2025

What version of Elysia is running?

1.2.10

What platform is your computer?

Darwin 24.3.0 arm64 arm

What steps can reproduce the bug?

  1. Create an Elysia server using Node adapter with a derive and a post. Use destructuring assignment with the body property from the Context.
import { node } from '@elysiajs/node';
import { Elysia } from 'elysia';

const app = new Elysia({ adapter: node() })
  .onError(({ error }) => {
    return error;
  })
  .derive(({ body }) => {
    return {
      operation: 'test'
    };
  })
  .post(`/bug`, ({ operation }) => {
    return operation;
  })
  .listen(3777);
  1. Run the server with Node.js and make an HTTP request to POST /bug.

What is the expected behavior?

The expected behavior is to return "test' in the HTTP response, using the code above.

What do you see instead?

The onError handler will be caught with the error "Response body object should not be disturbed or locked".

Additional information

If you remove the destructuring assignment, the server responds normally.

import { node } from '@elysiajs/node';
import { Elysia } from 'elysia';

const app = new Elysia({ adapter: node() })
  .onError(({ error }) => {
    return error;
  })
  .derive((context) => {
    return {
      operation: 'test',
      body2: context.body,
    };
  })
  .post(`/bug`, ({ operation }) => {
    return operation;
  })
  .listen(3777);

⚠ The body destructuring assignment is working normally running with Bun and without the Node adapter.

💡 Digging on the Web, I found this could be probably related to Fetch's Request body cloning. The destructuring assignment could be cloning the Request body without the proper .clone() call.

Have you try removing the node_modules and bun.lockb and try again yet?

Sure.

@ggondim ggondim added the bug Something isn't working label Feb 7, 2025
@SaltyAom
Copy link
Member

SaltyAom commented Feb 21, 2025

I'm unable to reproduce the bug with the following code:

new Elysia({ adapter: node() })
	.onError(({ error }) => {
		return error
	})
	.derive(({ body }) => {
		return {
			operation: {
				a: 'test',
				body
			}
		}
	})
	.post(`/bug`, ({ operation }) => {
		return operation
	})
	.listen(3777)

fetch('http://localhost:3777/bug', {
	method: 'POST',
	headers: {
		'Content-Type': 'application/json'
	},
	body: JSON.stringify({ operation: 'test' })
})
	.then((x) => x.text())
	.then(console.log)

I suspected this might be related to #23 which was fixed with node adapter 1.2.5.

What version of Node adapter are you using? @ggondim

@SaltyAom SaltyAom self-assigned this Feb 21, 2025
@SaltyAom SaltyAom added the question Further information is requested label Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants