From: Ardis Lu Date: Wed, 8 May 2024 05:19:44 +0000 (-0700) Subject: Simplify last file chunk logic X-Git-Url: https://git.ardis.lu/?a=commitdiff_plain;h=28fa7a2224caa95f840a1da744d3e1c072fdaa3f;p=targz Simplify last file chunk logic Allowing the file stream to manage its own internal chunks is also more performant --- diff --git a/index.html b/index.html index be15be0..c6f62a7 100644 --- a/index.html +++ b/index.html @@ -120,14 +120,16 @@ controller.enqueue(header); // Enqueue the file data, with last chunk padded to 512 bytes - const reader = file.stream().getReader({ mode: 'byob' }); + const reader = file.stream().getReader(); while (true) { - const chunk = new Uint8Array(512); - const { done, value } = await reader.read(chunk); - if (done) { // Final value will be empty, skip it + const { done, value } = await reader.read(); + if (done) { + if (file.size > 0) { + controller.enqueue(new Uint8Array(512 - (file.size % 512))); + } break; } - controller.enqueue(new Uint8Array(value.buffer)); // Original view is detached, must instantiate new view on buffer + controller.enqueue(new Uint8Array(value.buffer)); } }