diff --git a/public/gif/parseGif.js b/public/gif/parseGif.js index d4ae67d..0314c77 100644 --- a/public/gif/parseGif.js +++ b/public/gif/parseGif.js @@ -226,6 +226,19 @@ const readExtensionBlock = (bytes, startingOffset) => { } }; +/** + * @param {Uint8Array} bytes + * @param {number} offset + * @returns {{ node: GifNode, newOffset: number }} + */ +const readTrailerBlock = (bytes, offset) => ({ + newOffset: offset + 1, + node: { + type: GifNodeType.gifTerminator, + bytes: bytes.subarray(offset, offset + 1), + }, +}); + /** * @param {Uint8Array} bytes * @returns {null | GifNode} The root node of the GIF tree, or null if the GIF is invalid. @@ -302,8 +315,7 @@ export default (bytes) => { offset += sizeOfGlobalColorTableInBytes; } - readingBlocks: - while (true) { + while (offset < bytes.byteLength) { const blockType = bytes[offset]; let readerFn; @@ -315,11 +327,8 @@ export default (bytes) => { readerFn = readExtensionBlock; break; case BLOCK_TYPE_TRAILER: - children.push({ - type: GifNodeType.gifTerminator, - bytes: bytes.subarray(offset, offset + 1), - }); - break readingBlocks; + readerFn = readTrailerBlock; + break; default: throw new Error(`Unknown GIF block type: ${blockType}`); }