@cacheable/net - fix: send FormData/Blob correctly using the runtime's own fetch#1636
Merged
Conversation
…he runtime's own fetch The package imported fetch from a standalone undici version whose FormData/Blob/ReadableStream classes differ from Node's bundled globals. Its instanceof checks rejected the user-constructed globals and the body fell through to string coercion, so requests went out as "[object FormData]" with Content-Type: text/plain instead of multipart/form-data. Switch the internal fetch to globalThis.fetch so the body classes and fetch implementation share a realm, and add a local-server regression test that asserts the wire-level body and content-type for FormData, FormData containing a File, URLSearchParams, Blob, and JSON across post/patch/del.
Contributor
There was a problem hiding this comment.
Code Review
This pull request switches from using a standalone undici fetch to the runtime's global fetch to ensure compatibility with global body classes like FormData, preventing incorrect coercion to strings. It also adds a suite of regression tests using a local HTTP server to verify correct body and header handling for various data types. Feedback suggests binding the captured fetch to globalThis for improved context safety and expanding the input type to support URL objects.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1636 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 26 26
Lines 2496 2497 +1
Branches 554 555 +1
=========================================
+ Hits 2496 2497 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
@cacheable/net@2.0.7silently coercedFormDatato itstoString()value ("[object FormData]") and sent it withContent-Type: text/plain;charset=UTF-8. Nativefetchhandles the same input correctly withmultipart/form-data; boundary=….Root cause
fetch.tsimportedfetchfrom the standaloneundici@^7package. Node 18+ ships its own bundled undici (6.x in our test env) that producesglobalThis.FormData,globalThis.Blob,globalThis.File,globalThis.ReadableStream. The standalone undici@7'sfetchchecksvalue instanceof <its own FormData>. The user-constructednew FormData()is from Node's bundled realm, so that check fails and the body falls through to a string coercion —"[object FormData]"— withtext/plain.Fix
Use
globalThis.fetchinternally. Now the fetch implementation and the body classes (FormData,Blob,File,URLSearchParams,ReadableStream) come from the same realm, and everyBodyInitshape is recognized correctly:FormDatatext/plain+"[object FormData]"multipart/form-data; boundary=…+ real multipartFormData(withFile)text/plain+"[object FormData]"multipart/form-datawithfilename="…"URLSearchParamsapplication/x-www-form-urlencoded;charset=UTF-8Blobstring, JSON objectundici'sRequestInitandResponsetypes are still imported as type-only so the public API surface is unchanged.Test plan
Content-Typeand body forFormData,FormDatacontainingFile,URLSearchParams,Blob, and a JSON object acrosspost,patch,delpnpm buildclean (CJS + ESM + DTS)https://claude.ai/code/session_01XVcwdY82D89NiYwinHSBvA
Generated by Claude Code