-
-
Notifications
You must be signed in to change notification settings - Fork 4
Migrate from external "form-data" library to native FormData with memory-efficient file handling #19
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
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: ingalls <[email protected]>
Co-authored-by: ingalls <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Streams can potentially use considerable memory depending on file size. As such determine if it is possible to use fs.openAsBlob instead
@copilot Streams can potentially use considerable memory depending on file size. As such determine if it is possible to use fs.openAsBlob instead |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Replace stream2buffer with fs.openAsBlob when the input is a file stream, significantly reducing memory usage for large files. Falls back to the previous buffer approach for non-file streams to maintain compatibility. Co-authored-by: ingalls <[email protected]>
I've implemented the suggested optimization using
The detection logic checks if the stream is a file stream by verifying it's a Readable with a Commit: 7051727 |
This PR migrates the codebase from the external
form-data
npm package to Node.js's nativeFormData
class, reducing dependencies and bundle size while adding memory-efficient handling for large file uploads.Changes Made
Removed External Dependency
form-data
package frompackage.json
dependenciesUpdated Implementation with Memory Optimization
lib/api.ts
: Removedform-data
import and updated the fetch method to rely on automatic header management by undici instead of manually calling.getHeaders()
lib/api/files.ts
: Enhanced theuploadPackage
method with intelligent stream handling:fs.openAsBlob()
for memory-efficient processing of large filesKey Technical Details
The main challenge was handling the difference between the external library and native FormData while optimizing memory usage:
form-data
: Accepts Node.js streams directly and provides.getHeaders()
methodFormData
: Only accepts strings, Blobs, or Files, but headers are handled automatically by modern fetch implementationsThe solution intelligently handles different input types:
Benefits
Testing
This change is fully backward compatible and maintains the same API surface for consumers of the library while providing significant memory usage improvements for large file uploads.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.