Closed cyco130 closed 1 year ago
Just stumbled upon hattip and really like the concept. Maybe Remix implementation might be inspirational as well https://remix.run/docs/en/v1/api/remix#unstable_parsemultipartformdata
Maybe Remix implementation might be inspirational as well
Thanks, I've already dug it up of course :D Progress report: I have a mostly working implementation, I'll clean it up and release it after I'm done with the CLI.
Amazing 😍 already curious how I'd be used in rakkas 😁
We should implement a
multipart/form-data
parser.The problem
Real-world server server-side applications have to have a way of parsing
multipart/form-data
, including file uploads. Thefetch
standard providesrequest.formData
but the API is not adequate for server-side use:multipart/form-data
is a streaming format butformData
is a "random access" one. You're supposed to be able to access uploaded files at any time. That is, you can callformData.get("file1")
orformData.get("file2")
at any time. This requires buffering the whole request which is prohibitively expensive in many environments.To add to the inadequacy of the API itself, existing implementations are also lacking:
multipart/form-data
at all (callingrequest.formData
throws).node-fetch
does a good job within the limitations of the API by buffering the entire request in memory.Suggested solution
We can implement a streaming parser for
multipart/*
as an async iterator (or equivalent interface):On top of this, we can implement an easier-to-use interface specifically for
multipart/form-data
and on a third layer, we can implement file stores for Node FS, Deno FS, Cloudflare Workers KV, AWS S3, etc.Impact
Such a solution would be very useful for non-HatTip environments too.
TODO
Prior art and references
streamsearch