Fix mismatching gif response byte length

Co-authored-by: trevor-signal <131492920+trevor-signal@users.noreply.github.com>
This commit is contained in:
Jamie Kyle
2025-05-05 14:35:35 -07:00
committed by GitHub
parent 511486c894
commit d91c28bae9

View File

@@ -81,14 +81,22 @@ export function _getSegmentRanges(
return segmentRanges; return segmentRanges;
} }
function assertExpected<T>(actual: T, expected: T, message: string) {
strictAssert(
Object.is(actual, expected),
`${message}: ${actual} (expected: ${expected})`
);
}
async function fetchSegment( async function fetchSegment(
url: string, url: string,
segmentRange: _SegmentRange, segmentRange: _SegmentRange,
contentLength: number,
signal?: AbortSignal signal?: AbortSignal
): Promise<ArrayBufferView> { ): Promise<ArrayBufferView> {
const { messaging } = window.textsecure; const { messaging } = window.textsecure;
strictAssert(messaging, 'Missing window.textsecure.messaging'); strictAssert(messaging, 'Missing window.textsecure.messaging');
const { data } = await messaging.server.fetchBytesViaProxy({ const { data, response } = await messaging.server.fetchBytesViaProxy({
method: 'GET', method: 'GET',
url, url,
signal, signal,
@@ -97,9 +105,22 @@ async function fetchSegment(
}, },
}); });
strictAssert( assertExpected(
data.buffer.byteLength === segmentRange.segmentSize, response.headers.get('Content-Length'),
'Response buffer should be exact length of segment range' `${segmentRange.segmentSize}`,
'Unexpected Content-Length header'
);
assertExpected(
response.headers.get('Content-Range'),
`bytes ${segmentRange.startIndex}-${segmentRange.endIndexInclusive}/${contentLength}`,
'Unexpected Content-Range header'
);
assertExpected(
data.byteLength,
segmentRange.segmentSize,
'Unexpected response buffer byte length'
); );
let slice: ArrayBufferView; let slice: ArrayBufferView;
@@ -125,7 +146,7 @@ export async function fetchInSegments(
const segmentRanges = _getSegmentRanges(contentLength, segmentSize); const segmentRanges = _getSegmentRanges(contentLength, segmentSize);
const segmentBuffers = await Promise.all( const segmentBuffers = await Promise.all(
segmentRanges.map(segmentRange => { segmentRanges.map(segmentRange => {
return fetchSegment(url, segmentRange, signal); return fetchSegment(url, segmentRange, contentLength, signal);
}) })
); );
return new Blob(segmentBuffers); return new Blob(segmentBuffers);