Repository navigation
Description
Describe the bug
When a LargeFileUploadTask with a StreamUpload is resumed and the upload session's nextExpectedRanges starts inside the previously sent chunk (the server stored part of it), StreamUpload.sliceFile() builds the wrong body.
With a 6 MiB file, rangeSize 2 MiB, the second chunk (2097152-4194303) failing and the server then reporting nextExpectedRanges: ["3145728-"], the retry request has:
Content-Range: bytes 3145728-5242879/6291456Content-Length: 2097152- a body of only 1048576 bytes, which are file bytes 4194304-5242879 (not 3145728-4194303 plus the following MiB).
The cause is in src/tasks/FileUploadTask/FileObjectClasses/StreamUpload.ts: the cached previousSlice.fileSlice buffer starts at previousSlice.range.minValue, but it is indexed with whole-file offsets:
return this.previousSlice.fileSlice.slice(range.minValue, range.maxValue + 1);
...
bufs.push(this.previousSlice.fileSlice.slice(range.minValue, previousRangeMax + 1));For a cached slice starting at 2097152, slice(3145728, 4194304) on a 2097152-byte buffer is empty, so the cached part is dropped and only the bytes read from the stream (1 MiB) are sent.
A related edge case: the guard if (range.minValue < this.previousSlice.range.maxValue) uses <, so a resume at exactly the last cached byte (4194303-) skips the cache and reads from the stream, sending bytes shifted by one.
The existing tests in test/node/tasks/StreamUpload.ts only use a previous slice that starts at 0, where whole-file and slice-relative offsets are the same, which is why this is not caught.
Expected behavior
When the next range starts inside the cached chunk, the request body should be the remaining cached bytes (from range.minValue) followed by the next bytes from the stream, and its length should match Content-Length. For the example above: 2097152 bytes, equal to file bytes 3145728-5242879.
How to reproduce
Minimal, no network (@microsoft/microsoft-graph-client@3.1.0, Node 22):
const fs = require("fs");
const { StreamUpload } = require("@microsoft/microsoft-graph-client");
const { Range } = require("@microsoft/microsoft-graph-client/lib/src/tasks/FileUploadTask/Range");
// head -c 6291456 /dev/urandom > file6m.bin
const MiB = 1024 * 1024;
const file = fs.readFileSync("file6m.bin");
(async () => {
const s = new StreamUpload(fs.createReadStream("file6m.bin"), "file6m.bin", file.length);
await s.sliceFile(new Range(0, 2 * MiB - 1));
await s.sliceFile(new Range(2 * MiB, 4 * MiB - 1)); // this chunk "fails"
// server: nextExpectedRanges ["3145728-"]
const retry = await s.sliceFile(new Range(3 * MiB, 5 * MiB - 1));
console.log(retry.length); // 1048576, expected 2097152
console.log(retry.equals(file.subarray(3 * MiB, 5 * MiB))); // false
console.log(retry.equals(file.subarray(4 * MiB, 5 * MiB))); // true: cached part dropped
})();I also ran the real LargeFileUploadTask.upload() and resume() against a stub middleware acting as the upload session (second PUT returns 500, GET then returns the nextExpectedRanges):
| server resumes at | PUT after resume | Content-Length | body bytes | body matches file at range start |
|---|---|---|---|---|
| 2097152 (chunk boundary) | 2097152-4194303 |
2097152 | 2097152 | yes |
| 3145728 (inside cached chunk) | 3145728-5242879 |
2097152 | 1048576 | no |
SDK Version
3.1.0 (also present in StreamUpload.ts on dev today)
Latest version known to work for scenario above?
Not known. The cached-slice handling came from #460 (fix for #452); its tests cover a previous slice starting at 0 only.
Known Workarounds
Use FileUpload with a Buffer instead of StreamUpload when the whole file can be held in memory, or restart the upload session instead of resuming it.
Debug output
Click to expand log
```</details>
### Configuration
_No response_
### Other information
Suggested fix in `StreamUpload.sliceFile()` (checked locally: both cases above then send the correct 2097152 bytes, and the chunk-boundary case is unchanged):
```diff
- if (range.minValue < this.previousSlice.range.maxValue) {
+ if (range.minValue <= this.previousSlice.range.maxValue) {
const previousRangeMin = this.previousSlice.range.minValue;
const previousRangeMax = this.previousSlice.range.maxValue;
+ const offset = range.minValue - previousRangeMin;
...
if (range.maxValue === previousRangeMax) {
- return this.previousSlice.fileSlice.slice(range.minValue, range.maxValue + 1);
+ return this.previousSlice.fileSlice.slice(offset);
}
- bufs.push(this.previousSlice.fileSlice.slice(range.minValue, previousRangeMax + 1));
+ bufs.push(this.previousSlice.fileSlice.slice(offset));
rangeSize = range.maxValue - previousRangeMax;
A regression test would use a previous slice that does not start at 0, e.g. slices 0-9, 10-19, then 15-24.
Scope: the wrong request body is confirmed locally with the published package. The upload-session server was simulated, so I have not observed what the live Graph upload endpoint does with the short body.
Reported by Emails.help (https://emails.help).
Activity