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

isFormDataLike fails with bundled code #1952

Closed
penalosa opened this issue Feb 21, 2023 · 3 comments · Fixed by #1953
Closed

isFormDataLike fails with bundled code #1952

penalosa opened this issue Feb 21, 2023 · 3 comments · Fixed by #1953
Labels
bug Something isn't working

Comments

@penalosa
Copy link

Bug Description

If you bundle undici with esbuild (or other bundlers that mangle names), the check in https://github.com/nodejs/undici/blob/main/lib/core/util.js#L378 fails, since the name of the FormData class is not necessarily equal to "FormData". Previously, the name of the FormData class was hardcoded with

class FormData {
  static name = 'FormData'
  ...
}

but this was removed in #1742 (https://github.com/nodejs/undici/pull/1742/files#diff-bf6d8875713a8e4d7917687069b9d3c16323b34155573b442c156d77765aa999).

For instance, esbuild bundles:

class FormData {
  ...
}

to

var FormData4 = class {
  ...
}

(or some other number).

Reproducible By

// index.js
import { FormData, Request } from 'undici';
import { writeFileSync } from 'fs';

const formData = new FormData();
formData.append('hello', 'world');
new Request('http://example.com', {
  method: 'POST',
  body: formData,
})
  .arrayBuffer()
  .then((r) => writeFileSync('./body.bin', new Uint8Array(r)));

Run npx esbuild --bundle index.js --outfile=out.js --platform=node --format=cjs && node out.js

The file body.bin contains the content:

[object FormData]

Expected Behavior

It should contain:

------formdata-undici-082107041324
Content-Disposition: form-data; name="hello"

world
------formdata-undici-082107041324--

Environment

macOS Ventura arm64, node 18.7

Additional context

This is fixed by changing isFormDataLike to check chunk[Symbol.toStringTag] rather than chunk.constructor.name, or re-adding the hardcoded class name, but I'm not sure if that might break other stuff.

@KhafraDev
Copy link
Member

Adding in the hardcoded class name is incorrect, however using Symbol.toStringTag over the constructor name is a valid fix. Would you like to send in a PR?

@ronag
Copy link
Member

ronag commented Feb 21, 2023

@KhafraDev I believe our isFormData

@ronag ronag reopened this Feb 21, 2023
ronag added a commit that referenced this issue Feb 21, 2023
ronag added a commit that referenced this issue Feb 21, 2023
@penalosa
Copy link
Author

Amazing, thank you @ronag! Do you know when this might be in the next release?

anonrig pushed a commit to anonrig/undici that referenced this issue Apr 4, 2023
metcoder95 pushed a commit to metcoder95/undici that referenced this issue Jul 21, 2023
crysmags pushed a commit to crysmags/undici that referenced this issue Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants