From 27be72e0cd7c29b6bc0f033c0c5ae1c60a6a9fa7 Mon Sep 17 00:00:00 2001 From: Danny Coates Date: Wed, 7 Aug 2019 13:47:59 -0700 Subject: [PATCH] refactored client side upload loop --- app/api.js | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/app/api.js b/app/api.js index d7e5c215..b7915fa2 100644 --- a/app/api.js +++ b/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(); + } } }