Fix error when downloading with no Content-Length

This commit is contained in:
Jerome Wu 2022-10-06 11:05:30 +08:00
parent e8f615f0c1
commit a40571f1b9
2 changed files with 12 additions and 5 deletions

View File

@ -2,8 +2,8 @@ export const HeaderContentLength = "Content-Length";
export const MIME_TYPE_JAVASCRIPT = "text/javascript";
export const MIME_TYPE_WASM = "application/wasm";
export const CORE_VERSION = "0.12.0";
export const CORE_URL = `https://unpkg.com/@ffmpeg/core@${CORE_VERSION}/dist/ffmpeg-core.js`;
export const CORE_VERSION = "0.12.0-alpha.2";
export const CORE_URL = `https://unpkg.com/@ffmpeg/core@${CORE_VERSION}/dist/umd/ffmpeg-core.js`;
export enum FFMessageType {
LOAD = "load",

View File

@ -33,23 +33,30 @@ export const downloadWithProgress = async (
const reader = resp.body?.getReader();
if (!reader) throw ERROR_RESPONSE_BODY_READER;
const data = new Uint8Array(total);
const chunks = [];
let received = 0;
for (;;) {
const { done, value } = await reader.read();
const delta = value ? value.length : 0;
if (done) {
if (total !== received) throw ERROR_INCOMPLETED_DOWNLOAD;
if (total != -1 && total !== received) throw ERROR_INCOMPLETED_DOWNLOAD;
cb({ url, total, received, delta, done });
break;
}
data.set(value, received);
chunks.push(value);
received += delta;
cb({ url, total, received, delta, done });
}
const data = new Uint8Array(received);
let position = 0;
for (const chunk of chunks) {
data.set(chunk, position);
position += chunk.length;
}
buf = data.buffer;
} catch (e) {
console.log(`failed to send download progress event: `, e);