some basic viewer

This commit is contained in:
Robin Appelman 2023-04-29 14:54:53 +02:00
commit 5910b2f35a
45 changed files with 1089 additions and 1436 deletions

28
script/download.ts Normal file
View file

@ -0,0 +1,28 @@
export async function download(url: string, progress: (number) => void): Promise<ArrayBuffer> {
const response = await fetch(url, {mode: 'cors'});
if (!response.body || !response.headers) {
throw new Error("invalid response");
}
const contentLength = +(response.headers.get('Content-Length') || 0);
let receivedLength = 0;
let data = new Uint8Array(contentLength);
const reader = response.body.getReader();
while(true) {
const {done, value} = await reader.read();
if (done) {
break;
}
data.set(value, receivedLength);
receivedLength += value.length;
progress((receivedLength / contentLength) * 100);
}
return data.buffer;
}