-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Fix MultipartReader for big files #4865
Conversation
I'm working on a test that will correctly check this since the current one is working with a small file. You can check the correct behaviour by using
|
std/mime/multipart.ts
Outdated
@@ -308,22 +308,22 @@ export class MultipartReader { | |||
} | |||
// file | |||
let formFile: FormFile | undefined; | |||
const n = await copy(buf, p); | |||
const n = await copyN(buf, p, maxValueBytes); |
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.
While testing I noticed that when parsing big files, it was crashing with: error: Uncaught Error: The buffer cannot be grown beyond the maximum size.
This happens because copy
was being used instead of copyN
, and was trying to fill the buffer with the whole file.
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.
Hi @marcosc90 - thanks for this patch - it looks good at first glance. I wonder if you could add a test to std/mime/multipart_test.ts to demonstrate the bug?
Hello @ry you're welcome. Just added the test, it fails on master as expected. Regarding the max buffer size error, don't know an efficient way to create a big file (> Buffer limit) on the tests, any guidance is appreciated. If you don't think that test is worth it on this PR, I can add it later on. |
std/mime/multipart.ts
Outdated
@@ -281,10 +281,10 @@ export class MultipartReader { | |||
* null value means parsing or writing to file was failed in some reason. | |||
* @param maxMemory maximum memory size to store file in memory. bytes. @default 1048576 (1MB) |
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.
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.
We should definitely follow Go's example. 10MB sounds good to me.
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.
Done. Reverted back to 10MB
and updated comment.
@ry I've been working on a test to check against max buffer error, the only problem is that it takes over a minute to run.
The problem is that Line 75 in 62150dd
Let me know if you think it should be added even if it takes too long. |
Related to (big) files in multipartrequest, I have noticed that tempFiles are created at
Wouldn't it be worth it to add a parameter for this in the readForm method (and default to |
@npup I think it's worth adding an options argument, but definitely not on this PR. If you open an issue I can work on it after this get merged :) |
With #4978 & #4979 merged now all tests are passing, and was able to simplify the code a bit. From: Lines 322 to 325 in 509dce0
to Line 322 in 669311a
Also added a Lines 331 to 334 in 669311a
|
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.
LGTM - sorry for the delay! Thank you
Fixes #4864
MultipartReader
is truncating big files. This happens because instead of callingcopyN
withn
it's usingmaxValueBytes
.Aside from that
maxMemory
was fixed to1MB
instead of10MB
which is incorrect according to the comments.