]> git.ardis.lu - targz/commitdiff
Simplify last file chunk logic
authorArdis Lu <ardisglu@gmail.com>
Wed, 8 May 2024 05:19:44 +0000 (22:19 -0700)
committerArdis Lu <ardisglu@gmail.com>
Wed, 8 May 2024 05:19:44 +0000 (22:19 -0700)
Allowing the file stream to manage its own internal chunks is also more performant

index.html

index be15be0dff24f13cf179504274c08b991f2c1ff2..c6f62a772ab2a4dda6b3c78cc02d271e8f6b62ed 100644 (file)
             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));
             }
           }
 
This page took 0.035188 seconds and 4 git commands to generate.