Plan resumable upload chunks
Build a gap-free chunk plan for a Dropbox-inspired resumable upload flow.Description
Requirements
The solution exports a callable function named planUploadChunks.
Chunk offsets start at zero and cover every byte exactly once without gaps or overlaps.
Every returned chunk has a positive integer size no greater than config.chunkSize.
Only the last chunk is marked final, including when the file size is an exact chunk-size multiple.
A fileSize of zero returns an empty array.
Invalid, fractional, unsafe, or out-of-range configuration values throw a RangeError.
The function returns a new result array containing newly created chunk records.
The config object and its property descriptors are not mutated.
Upload with a partial final chunk
{ fileSize: 11, chunkSize: 4 }[{ offset: 0, size: 4, final: false }, { offset: 4, size: 4, final: false }, { offset: 8, size: 3, final: true }]The first two chunks use the maximum size. The final chunk covers the remaining three bytes and is the only record marked final.
Empty file
{ fileSize: 0, chunkSize: 4 }[]
There are no byte ranges to upload.
Constraints
config is an object with fileSize and chunkSize propertiesfileSize must be a safe integer from 0 through Number.MAX_SAFE_INTEGERchunkSize must be a safe integer from 1 through Number.MAX_SAFE_INTEGERThe number of generated chunks will not exceed 2500Do not mutate, freeze, seal, or redefine the caller-owned config object
Hints
Hint 1
Advance a byte offset by one bounded chunk at a time until it reaches the file size.
Hint 2
Derive each end offset from the smaller of the next chunk boundary and the file boundary.
Hint 3
Decide whether a chunk is final by comparing its end offset with the total size.