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

fix: add duplex option when sending a body #571

Merged
merged 4 commits into from
Mar 24, 2023

Conversation

codebytere
Copy link
Contributor

@codebytere codebytere commented Feb 15, 2023

Resolves #570

Refs nodejs/node#46221.

As of Node.js v18.13.0, via an update to undici, request.duplex must be set if request.body is ReadableStream or Async Iterables. This PR therefore adds missing duplex option to fetch-wrapper as per spec

I'm not sure if it's possible to test for this since responses are mocked.

Behavior

Before the change?

fetch calls fail with the following type of error:

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

After the change?

fetch calls should no longer fail.

Other information

I'm not sure if it's possible to test for this since responses are mocked.


Additional info

Pull request checklist

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been reviewed and added / updated if needed (for bug fixes / features)
  • Added the appropriate label for the given change

Does this introduce a breaking change?

  • Yes (Please add the Type: Breaking change label)
  • No

Pull request type

Please add the corresponding label for change this PR introduces:

  • Bugfix: Type: Bug
  • Feature/model/API additions: Type: Feature
  • Updates to docs or samples: Type: Documentation
  • Dependencies/code cleanup: Type: Maintenance

@gr2m gr2m added Type: Bug Something isn't working as documented Priority: High labels Feb 15, 2023
@gr2m
Copy link
Contributor

gr2m commented Feb 15, 2023

What is the node version where it starts failing? We should add it to the test matrix to avoid regressions

@@ -39,6 +39,7 @@ export default function fetchWrapper(
body: requestOptions.body,
headers: requestOptions.headers as HeadersInit,
redirect: requestOptions.redirect,
...(requestOptions.body && { duplex: 'half' })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add a comment about why the duplex option is added here, with a URL that holds more information. Will half always work, or are there different values that need to be set based on different types of request body?

Copy link
Contributor Author

@codebytere codebytere Feb 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To my understanding, based on the spec, half should be appropriate to set for all cases in Node.js (it is the also only option available at the moment)

@codebytere
Copy link
Contributor Author

Oops, I thought i added that to the PR body but i missed it! It was Node.js v18.13.0, via an update to undici

Copy link
Contributor

@gr2m gr2m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much Shelley!

And thanks a lot for all your great work in Electron and Beyond!

@gr2m
Copy link
Contributor

gr2m commented Feb 15, 2023

It was Node.js v18.13.0

In that case our current CI should cover it. Node 18 currently loads 18.14.0

@gr2m
Copy link
Contributor

gr2m commented Feb 15, 2023

Hmm do you have a test script that fails for you in Node 18.14?

I tried this code on the main branch

  await request("POST /repos/{owner}/{repo}/issues/{issue_number}/comments", {
    owner: "gr2m",
    repo: "sandbox",
    issue_number: 240,
    body: "Hello World",
    headers: {
      authorization: `token ${process.env.GITHUB_TOKEN}`,
    },
  });

And it worked

@codebytere
Copy link
Contributor Author

@nickfloyd @gr2m do you know when we can expect a new version? We're having to patch around this locally: electron/electron#38250

@gr2m
Copy link
Contributor

gr2m commented May 16, 2023

hey sorry we have automated releases, but this pull request was merged with an incorrect commit message. It was before we set commit messages to default to the pull request title. I'll have that sorted out in a moment

@github-actions
Copy link

🎉 This PR is included in version 6.2.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

@gr2m
Copy link
Contributor

gr2m commented May 16, 2023

all set, very sorry for the delay 😞

@codebytere
Copy link
Contributor Author

@gr2m no worries - thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released Type: Bug Something isn't working as documented
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

[BUG]: fetch fails on latest Node.js when sending a body
3 participants