Repository navigation
fix: a single-file download reads the file only as fast as the client reads it - #533
Merged
Merged
Conversation
… reads it (#500) The single-file branch of GET /api/requests/:id/download queued every chunk read from disk for the client, so a paused or slow client made the server read the whole file into memory. It now uses the same pull-based stream as the multi-file zip (#490): one shared pullFrom helper reads the next chunk only when the client takes the last one. A cancel destroys the file stream without logging an error; a read error is logged with the request id and ends the response with that error. Headers unchanged. Based on #510 by @pintu544. Co-authored-by: pintu544 <64580363+pintu544@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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
A single-file download now reads the file only as fast as the client reads it. Before, the route queued every chunk it read from disk for the client, so a paused or slow client made the server read the whole file into memory. The multi-file zip already worked this way (#490); this does the same for one file.
Changes
src/app/api/requests/[id]/download/route.ts: the zip's pull loop moves into a sharedpullFromhelper, and the single-file branch uses it through a newstreamFile. The next chunk is read only when the client takes the last one.Content-Type,Content-Disposition,Content-Length) and the token and account checks are unchanged.documentation/backend/services/auth.md: new "Request File Download Link" section (both branches stream with backpressure) and a Fixed Issues entry for Single-file request download reads the whole file into memory when the client stops reading #500.TABLEOFCONTENTS.mdlinks to it.Tests
New
tests/api/requests-download.file-stream.test.ts(4 tests, real route):Against main's route, 3 of the 4 fail: both slow-reader tests, and the read-error test only on the request id now in the log line. The cancel test passes on main.
Checks
npx vitest related --runon the route and the new test: 4 files, 21 passed, 2 skipped (Windows-only skips in the zip tests).npx tsc --noEmit: clean.npx eslinton the changed files: 0 errors, 0 warnings.node:24-bookworm(Node v24.22.2, the image's base): a client that reads one chunk of a 64 MiB file and pauses for 1 s leaves 192 KiB read from disk (main's code: all 64 MiB), and a cancel leaves the stream destroyed and closed.Risk and rollback
Low. Only the single-file response body changes, and it now uses the same streaming code the zip has used since #490. Rollback is a revert of this commit.
Closes #500
Based on #510 by @pintu544.
🤖 Generated with Claude Code