refactored client side upload loop
This commit is contained in:
parent
e4231bbc0f
commit
27be72e0cd
1 changed files with 14 additions and 11 deletions
25
app/api.js
25
app/api.js
|
@ -212,32 +212,35 @@ async function upload(
|
||||||
let state = await reader.read();
|
let state = await reader.read();
|
||||||
let size = 0;
|
let size = 0;
|
||||||
while (!state.done) {
|
while (!state.done) {
|
||||||
const buf = state.value;
|
|
||||||
if (canceller.cancelled) {
|
if (canceller.cancelled) {
|
||||||
throw canceller.error;
|
ws.close();
|
||||||
}
|
}
|
||||||
|
if (ws.readyState !== WebSocket.OPEN) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
const buf = state.value;
|
||||||
ws.send(buf);
|
ws.send(buf);
|
||||||
|
|
||||||
onprogress(size);
|
onprogress(size);
|
||||||
size += buf.length;
|
size += buf.length;
|
||||||
state = await reader.read();
|
state = await reader.read();
|
||||||
while (
|
while (
|
||||||
ws.bufferedAmount > ECE_RECORD_SIZE * 2 &&
|
ws.bufferedAmount > ECE_RECORD_SIZE * 2 &&
|
||||||
ws.readyState === WebSocket.OPEN
|
ws.readyState === WebSocket.OPEN &&
|
||||||
|
!canceller.cancelled
|
||||||
) {
|
) {
|
||||||
await delay();
|
await delay();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const footer = new Uint8Array([0]);
|
if (ws.readyState === WebSocket.OPEN) {
|
||||||
ws.send(footer);
|
ws.send(new Uint8Array([0])); //EOF
|
||||||
|
}
|
||||||
|
|
||||||
await completedResponse;
|
await completedResponse;
|
||||||
ws.close();
|
|
||||||
return uploadInfo;
|
return uploadInfo;
|
||||||
} catch (e) {
|
} finally {
|
||||||
ws.close(4000);
|
if (![WebSocket.CLOSED, WebSocket.CLOSING].includes(ws.readyState)) {
|
||||||
throw e;
|
ws.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue