From 28fa7a2224caa95f840a1da744d3e1c072fdaa3f Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Tue, 7 May 2024 22:19:44 -0700 Subject: [PATCH] Simplify last file chunk logic Allowing the file stream to manage its own internal chunks is also more performant --- index.html | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) 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)); } } -- 2.47.3