diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-05-31 18:19:53 -0400 |
---|---|---|
committer | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-05-31 18:22:46 -0400 |
commit | 13b59dacb2cd8f907eaba6b97f927b72f02fe672 (patch) | |
tree | 4212842c599cea507b96fd8b2bb10163d2607236 /ext/mixed | |
parent | d0dcff765f740bf6f0f6523b09cb8b21eb85cd93 (diff) |
Limit action port message size (#587)
* Add onDisconnect handler
* Update how error is posted
* Update action ports to send long messages in fragments
* Remove ack timer
* Move message destructuring into try block
Diffstat (limited to 'ext/mixed')
-rw-r--r-- | ext/mixed/js/api.js | 26 |
1 files changed, 9 insertions, 17 deletions
diff --git a/ext/mixed/js/api.js b/ext/mixed/js/api.js index 0bc91759..d2be12aa 100644 --- a/ext/mixed/js/api.js +++ b/ext/mixed/js/api.js @@ -204,7 +204,6 @@ function _apiCreateActionPort(timeout=5000) { function _apiInvokeWithProgress(action, params, onProgress, timeout=5000) { return new Promise((resolve, reject) => { - let timer = null; let port = null; if (typeof onProgress !== 'function') { @@ -213,12 +212,6 @@ function _apiInvokeWithProgress(action, params, onProgress, timeout=5000) { const onMessage = (message) => { switch (message.type) { - case 'ack': - if (timer !== null) { - clearTimeout(timer); - timer = null; - } - break; case 'progress': try { onProgress(...message.data); @@ -243,10 +236,6 @@ function _apiInvokeWithProgress(action, params, onProgress, timeout=5000) { }; const cleanup = () => { - if (timer !== null) { - clearTimeout(timer); - timer = null; - } if (port !== null) { port.onMessage.removeListener(onMessage); port.onDisconnect.removeListener(onDisconnect); @@ -256,17 +245,20 @@ function _apiInvokeWithProgress(action, params, onProgress, timeout=5000) { onProgress = null; }; - timer = setTimeout(() => { - cleanup(); - reject(new Error('Timeout')); - }, timeout); - (async () => { try { port = await _apiCreateActionPort(timeout); port.onMessage.addListener(onMessage); port.onDisconnect.addListener(onDisconnect); - port.postMessage({action, params}); + + // Chrome has a maximum message size that can be sent, so longer messages must be fragmented. + const messageString = JSON.stringify({action, params}); + const fragmentSize = 1e7; // 10 MB + for (let i = 0, ii = messageString.length; i < ii; i += fragmentSize) { + const data = messageString.substring(i, i + fragmentSize); + port.postMessage({action: 'fragment', data}); + } + port.postMessage({action: 'invoke'}); } catch (e) { cleanup(); reject(e); |