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