Release 4.0.0
[skip ci]
4.0.0 (2025-02-03)
⚠ BREAKING CHANGES
- drop Node.js < 18.19.0 support
part of https://github.com/eggjs/egg/issues/3644
https://github.com/eggjs/egg/issues/5257
Summary by CodeRabbit
New Features
- Introduced a new middleware for handling multipart requests.
- Added improved error handling for oversized file uploads.
Refactor
Streamlined configuration and context enhancements for better stability and TypeScript support.
Modernized the codebase by transitioning to ES modules and updating type definitions.
Chores
Updated package metadata, dependencies, and continuous integration settings to support newer Node.js versions.
Introduced a new TypeScript configuration for stricter type-checking.
Tests
Added unit tests to validate application behavior under incorrect configurations.
Established comprehensive tests for multipart form handling to ensure correct processing of file uploads.
Transitioned existing tests to TypeScript and updated assertions for consistency.
Features
版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9
京公网安备 11010802032778号
@eggjs/multipart
Use co-busboy to upload file by streaming and process it without save to disk(using the
streammode).Just use
ctx.multipart()to got file stream, then pass to image processing module such asgmor upload to cloud storage such asoss.Whitelist of file extensions
For security, if uploading file extension is not in white list, will response as
400 Bad request.Default Whitelist:
fileSize
The default fileSize that multipart can accept is
10mb. if you upload a large file, you should specify this config.Custom Config
Developer can custom additional file extensions:
Can also override built-in whitelist, such as only allow png:
Or by function:
Note: if define
whitelist, thenfileExtensionswill be ignored.Examples
More examples please follow:
streammodefilemodefilemode: the easy wayIf you don’t know the Node.js Stream work, maybe you should use the
filemode to get started.The usage very similar to bodyParser.
ctx.request.body: Get all the multipart fields and values, exceptfile.ctx.request.files: Contains allfilefrom the multipart request, it’s an Array object.WARNING: you should remove the temporary upload files after you use it, the
async ctx.cleanupRequestFiles()method will be very helpful.Enable
filemode on configYou need to set
config.multipart.mode = 'file'to enablefilemode:After
filemode enable, egg will remove the old temporary files(don’t include today’s files) on04:30 AMevery day by default.Default will use the last field which has same name, if need the all fields value, please set
allowArrayFieldin config.Upload One File
Controller which hanlder
POST /upload:Upload Multiple Files
Controller which hanlder
POST /upload:streammode: the hard wayIf you’re well-known about know the Node.js Stream work, you should use the
streammode.Use with
for await...ofController which hanlder
POST /upload:Upload One File (DEPRECATED)
You can got upload stream by
ctx.getFileStream*().Controller which handler
POST /upload:Upload Multiple Files (DEPRECATED)
Controller which hanlder
POST /upload:Support
fileandstreammode in the same timeIf the default
modeisstream, use thefileModeMatchoptions to match the request urls switch tofilemode.NOTICE:
fileModeMatchoptions only work onstreammode.License
MIT
Contributors
Made with contributors-img.