fix: 不支持流传输时的处理

This commit is contained in:
unanmed 2025-01-19 15:15:18 +08:00
parent 08b30c5f7e
commit 4d0331d5e8
2 changed files with 17 additions and 14 deletions

View File

@ -205,9 +205,9 @@ export class ByteResource extends Resource<Uint8Array> {
async load(_onProgress?: ProgressFn): Promise<Uint8Array> {
const response = await fetch(this.resolveURI());
const data = await response.bytes();
this.resource = data;
return data;
const data = await response.arrayBuffer();
this.resource = new Uint8Array(data);
return this.resource;
}
resolveURI(): string {

View File

@ -99,21 +99,24 @@ export class StreamLoader
this.stream = stream;
const reader = response.body?.getReader();
const targets = [...this.target];
// try {
await Promise.all(targets.map(v => v.start(stream, this, response)));
// 开始流传输
while (true) {
const { value, done } = await reader.read();
await Promise.all(targets.map(v => v.pump(value, done, response)));
if (done) break;
if (reader && reader.read) {
// 开始流传输
while (true) {
const { value, done } = await reader.read();
await Promise.all(
targets.map(v => v.pump(value, done, response))
);
if (done) break;
}
} else {
// 如果不支持流传输
const buffer = await response.arrayBuffer();
const data = new Uint8Array(buffer);
await Promise.all(targets.map(v => v.pump(data, true, response)));
}
this.loading = false;
targets.forEach(v => v.end(true));
// } catch (e) {
// logger.error(26, this.url, String(e));
// }
}
cancel(reason?: string) {