Merge pull request #10231 from overleaf/jpa-pdf-caching-bounds-debug

[web] pdf-caching: debug bounds when reassembling the range from chunks

GitOrigin-RevId: 11871241d07880ebd2659cc5d0feb1f2c29014b6
This commit is contained in:
Jakob Ackermann 2022-10-31 11:47:53 +00:00 committed by Copybot
parent 0e09c25a97
commit 79e6fecc4a

View file

@ -843,13 +843,36 @@ export async function fetchRange({
// | REQUESTED_RANGE |
// | CHUNK |
const offsetEnd = Math.max(chunk.end - end, 0)
const oldDataLength = data.length
if (offsetStart > 0 || offsetEnd > 0) {
// compute index positions for slice to handle case where offsetEnd=0
const chunkSize = chunk.end - chunk.start
data = data.subarray(offsetStart, chunkSize - offsetEnd)
}
const newDataLength = data.length
const insertPosition = Math.max(chunk.start - start, 0)
reassembledBlob.set(data, insertPosition)
try {
reassembledBlob.set(data, insertPosition)
} catch (err) {
const reassembledBlobLength = reassembledBlob.length
const trimmedChunk = {
start: chunk.start,
end: chunk.end,
hash: chunk.hash,
objectId: new TextDecoder().decode(chunk.objectId),
}
throw OError.tag(err, 'broken reassembly', {
start,
end,
chunk: trimmedChunk,
oldDataLength,
newDataLength,
offsetStart,
offsetEnd,
insertPosition,
reassembledBlobLength,
})
}
})
timer.finishBlockingCompute()