Skip to content

isFormDataLike fails with bundled code #1952

@penalosa

Description

@penalosa

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions