function fetchProgress(url, init, max) {
const form = htl.html`<form><progress value=0>`;
const progress = form.firstChild;
if (max !== undefined) progress.max = max;
fetch(url, init).then(async response => {
if (max === undefined) progress.max = max = +response.headers.get("content-length");
const reader = response.body && response.body.getReader();
if (!reader) {
const value = await response.arrayBuffer();
progress.max = progress.value = value.byteLength;
form.value = value;
form.dispatchEvent(new CustomEvent("input"));
return value;
}
const values = [];
while (true) {
const {done, value} = await reader.read();
if (done) break;
progress.value += value.length;
values.push(value);
}
form.value = concat(values);
form.dispatchEvent(new CustomEvent("input"));
});
return form;
}