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

Question about streams - "This ReadableStream is disturbed" #3245

Open
vicb opened this issue Dec 14, 2024 · 0 comments
Open

Question about streams - "This ReadableStream is disturbed" #3245

vicb opened this issue Dec 14, 2024 · 0 comments

Comments

@vicb
Copy link
Contributor

vicb commented Dec 14, 2024

ref issue: #2746

^ this issue is about workerd streams not compatible with Node stream. The reason is that we want to stick to the specs while Node (/undici) does not.

In workerd, it is not possible to initialize the body with a Readable as in:

const req = new Request('http://example.com', { method: 'POST', body: readable });

We have used a workaround for NextJS: { body: Readable.toWeb(readable) }

The workaround stopped working following some updates in the Next adapter code base.

There is a repro in https://github.com/vicb/workerd-stream

  • checkout the code
  • npm i
  • npx wrangler dev
  • hit b in the console to send a request (or use curl)

You will see the following error:

[wrangler:err] TypeError: This ReadableStream is disturbed (has already been read from), and cannot be used as a body.
    at Object.fetch (file:///Users/vberchet/code/work/extends-readable/src/index.ts:80:15)
    at fetchDispatcher (file:///Users/vberchet/code/work/extends-readable/.wrangler/tmp/bundle-IHVd6O/middleware-loader.entry.ts:54:17)

It originates in:

// Error with:
// TypeError: This ReadableStream is disturbed (has already been read from), and cannot be used as a body.
const req = new Request('http://example.com', { method: 'POST', body: Readable.toWeb(ic) });

// Works
//const req = new Request('http://example.com', { method: 'POST', body: ReadableStream.from(ic) });

You can see that the first snippet that used to work now error.

ic is an instance of IncomingMessage (local) < http.IncomingMessage (unenv) < Readable (workerd)

Note that before unjs/unenv#363 (2w ago), the Readable implementation was coming from unenv. It might explain why this code used to work.

Still I think { body: Readable.toWeb(ic) } should work and we should understand why it doesn't.

Other info:

I have tried to call Readable.isDisturbed(ic) right before the super constructor call and it returns false.

@jasnell could you please help investigate?

@vicb vicb changed the title Question about streams Question about streams - "This ReadableStream is disturbed" Dec 14, 2024
vicb added a commit to opennextjs/opennextjs-cloudflare that referenced this issue Dec 14, 2024
Use ReadableStream.from instead of Readable.toWeb
The latter triggers a "cloudflare/workerd#3245" error
vicb added a commit to opennextjs/opennextjs-cloudflare that referenced this issue Dec 15, 2024
Use ReadableStream.from instead of Readable.toWeb
The latter triggers a "This ReadableStream is disturbed" error
See cloudflare/workerd#3245
vicb added a commit to opennextjs/opennextjs-cloudflare that referenced this issue Dec 16, 2024
Use ReadableStream.from instead of Readable.toWeb
The latter triggers a "This ReadableStream is disturbed" error
See cloudflare/workerd#3245
vicb added a commit to opennextjs/opennextjs-cloudflare that referenced this issue Dec 20, 2024
Use ReadableStream.from instead of Readable.toWeb
The latter triggers a "This ReadableStream is disturbed" error
See cloudflare/workerd#3245
vicb added a commit to opennextjs/opennextjs-cloudflare that referenced this issue Dec 20, 2024
Use ReadableStream.from instead of Readable.toWeb
The latter triggers a "This ReadableStream is disturbed" error
See cloudflare/workerd#3245
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant