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

RequestInit: duplex option is required when sending a body #46221

Open
cyco130 opened this issue Jan 15, 2023 · 6 comments
Open

RequestInit: duplex option is required when sending a body #46221

cyco130 opened this issue Jan 15, 2023 · 6 comments

Comments

@cyco130
Copy link

cyco130 commented Jan 15, 2023

Version

v18.13.0

Platform

Linux xxx 5.15.0-58-generic #64~20.04.1-Ubuntu SMP Fri Jan 6 16:42:31 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

undici

What steps will reproduce the bug?

Run the following line of code (in REPL or a script)

new Request("http://example.com", { method: "POST", body: new ReadableStream() });

How often does it reproduce? Is there a required condition?

All the time.

What is the expected behavior?

No error thrown.

What do you see instead?

Uncaught TypeError: RequestInit: duplex option is required when sending a body.
    at new Request (node:internal/deps/undici/undici:7090:19)

Additional information

Behavior of other versions:

  • v16.19.0 fails, v.16.18.1 works (with --experimental-fetch option and ReadableStream imported from stream/web)
  • >= 19.1 fails, 19.0.1 works
  • v18.12.1 and lower works
@KhafraDev
Copy link
Member

The error is expected, see whatwg/fetch#1457

@cyco130
Copy link
Author

cyco130 commented Jan 15, 2023

So this happens because Node can't do full duplex when streaming so duplex: "half" has to be explicitly set, is that it?

Ugh, it's gonna break so many things :/

@climba03003
Copy link
Contributor

So this happens because Node can't do full duplex when streaming so duplex: "half" has to be explicitly set, is that it?

I believe it is a changes from specification to force it to be a must. You may see the same in foreseeable future for other implementer of fetch if they follow specification.

@nedataghizadeh79
Copy link

maybe This error occurs because you are passing a ReadableStream as the body of the request without providing the duplex option in the RequestInit object.

To fix the error, you can provide the duplex option and set it to true, like this:

new Request("http://example.com", { method: "POST", body: new ReadableStream(), duplex: true });

This tells the Request constructor that the body is a duplex stream that can both be read from and written to.

Note that the duplex option is required when sending a ReadableStream as the body of the request, as stated in the error message.

@hillar
Copy link

hillar commented Mar 24, 2023

The error is expected, see whatwg/fetch#1457

just do not forget duplex: 'half', for example piping one fetch to another

async function pipeUrlToUrlwithFetch(url1, url2) {
  const getResponse = await fetch(url1);
  const options = {
    method: 'POST',
    body: getResponse.body,
    duplex: 'half',
  };
  const postResponse = await fetch(url2, options);
  return postResponse.json();
}

pipeUrlToUrlwithFetch('https://jsonplaceholder.typicode.com/posts/1', 'https://jsonplaceholder.typicode.com/posts')
.then(result => {console.log(result);})
.catch(error => {console.error(error);});
@KevinBLT
Copy link

Why the spec did not just came up with applying the only sane default value to duplex: 'half' (and that is easy, because it's the only actual value at the moment) but instead forcing everybody to insert this options with only one available value is out of my mind.

vicrep added a commit to vicrep/logrocket-cli that referenced this issue Apr 28, 2023
Node sneakily introduced a change to the fetch API that now requires that the `duplex` option be set on all requests with contain a payload body: nodejs/node#46221 , which means that the CLI now fails on node >18.13, >19.1, 20, and running the CLI on those versions will cause the following exception:
```
RequestInit: duplex option is required when sending a body.

Exited with code exit status 1
```

This configures all fetch requests with bodies to set the duplex option to the only value that is allowed by the spec for now, which is `"half"`. This should have no impact on older node versions since it's the only option available, but will allow the CLI to run on those newer versions without throwing an error.
Yuphonic pushed a commit to LogRocket/logrocket-cli that referenced this issue May 17, 2023
Node sneakily introduced a change to the fetch API that now requires that the `duplex` option be set on all requests with contain a payload body: nodejs/node#46221 , which means that the CLI now fails on node >18.13, >19.1, 20, and running the CLI on those versions will cause the following exception:
```
RequestInit: duplex option is required when sending a body.

Exited with code exit status 1
```

This configures all fetch requests with bodies to set the duplex option to the only value that is allowed by the spec for now, which is `"half"`. This should have no impact on older node versions since it's the only option available, but will allow the CLI to run on those newer versions without throwing an error.
EvanBacon added a commit to expo/expo that referenced this issue Mar 5, 2024
# Why

- Hitting this issue when invoking server actions using the new req/res
system nodejs/node#46221
- Hitting issues with running `tsc` on compiled code. These are caused
by importing types from the build directory. Fixed it by duplicating the
types to the install.d.ts file.

---------

Co-authored-by: Expo Bot <34669131+expo-bot@users.noreply.github.com>
pdonias added a commit to vatesfr/xen-orchestra that referenced this issue Apr 29, 2024
If the body of a request is a stream, we could start receiving the response
before the request has been fully sent (full duplex).
However, Node does not support full duplex. Since other JS runtimes do support
full duplex, Node now requires to pass "duplex: 'half'" for compatibility
concerns with those JS runtimes.

Spec: https://fetch.spec.whatwg.org/#ref-for-dom-requestinit-duplex
WHATWG discussion: whatwg/fetch#1254
Node discussion: nodejs/node#46221
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
6 participants