From e0143c9f597acb5341f13d7d31995ee462cb0917 Mon Sep 17 00:00:00 2001 From: Anthony Date: Thu, 28 Jan 2021 10:17:30 +0700 Subject: [PATCH 1/2] Switch to fetch to retrieve files --- src/load_stl.js | 54 ++++++++++++++++++------------------------------- 1 file changed, 20 insertions(+), 34 deletions(-) diff --git a/src/load_stl.js b/src/load_stl.js index 1a0c8e2..da6c080 100644 --- a/src/load_stl.js +++ b/src/load_stl.js @@ -74,44 +74,30 @@ self.addEventListener("message", function(e) function isNumeric(n) { return !isNaN(parseFloat(n)) && isFinite(n); -} +} + function send_error(s) { postMessage({msg_type:MSG_ERROR, data:s}); } -function download_from_local(filename) -{ - var xhr = new XMLHttpRequest(); - - if (get_progress) - { - xhr.onprogress = - function(e) - { - postMessage({msg_type:MSG_LOAD_IN_PROGRESS, id:model_id, loaded:e.loaded, total:e.total}); - } - } - - xhr.onreadystatechange = - function(e) - { - if (xhr.readyState == 4) - { - //console.log('status: '+xhr.status); - if (xhr.status==200) - { - //console.log('done'); - after_file_load(xhr.response); - } - } - } - - xhr.open("GET", filename, true); - xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); - xhr.responseType = "arraybuffer"; - - xhr.send(null); +async function download_from_local(filename) { + const response = await fetch(filename) + const reader = response.body.getReader() + const total = Number(response.headers.get('content-length')) + const chunksAll = new Uint8Array(total) + let position = 0 + while (true) { + const { done, value } = await reader.read() + if (done) break + if (!value) continue + chunksAll.set(value, position) + position += value.length + if (get_progress) { + postMessage({ msg_type: MSG_LOAD_IN_PROGRESS, id: model_id, loaded: position, total: total }) + } + } + after_file_load(chunksAll.buffer) } function after_file_load(s) @@ -190,4 +176,4 @@ function read_file(f) }; reader.readAsArrayBuffer(f); -} \ No newline at end of file +} From 733d981b816e0f096f93eef209d3db35272f519d Mon Sep 17 00:00:00 2001 From: antho1404 Date: Fri, 29 Jan 2021 10:28:59 +0700 Subject: [PATCH 2/2] download compatibility with fetch and xhr --- src/load_stl.js | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/load_stl.js b/src/load_stl.js index da6c080..f0c827d 100644 --- a/src/load_stl.js +++ b/src/load_stl.js @@ -81,7 +81,50 @@ function send_error(s) postMessage({msg_type:MSG_ERROR, data:s}); } -async function download_from_local(filename) { +function download_from_local(filename) +{ + if (fetch) { + download_from_local_with_fetch(filename) + } else { + download_from_local_with_xhr(filename) + } +} + +function download_from_local_with_xhr(filename) +{ + var xhr = new XMLHttpRequest(); + + if (get_progress) + { + xhr.onprogress = + function(e) + { + postMessage({msg_type:MSG_LOAD_IN_PROGRESS, id:model_id, loaded:e.loaded, total:e.total}); + } + } + + xhr.onreadystatechange = + function(e) + { + if (xhr.readyState == 4) + { + //console.log('status: '+xhr.status); + if (xhr.status==200) + { + //console.log('done'); + after_file_load(xhr.response); + } + } + } + + xhr.open("GET", filename, true); + xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); + xhr.responseType = "arraybuffer"; + + xhr.send(null); +} + +async function download_from_local_with_fetch(filename) { const response = await fetch(filename) const reader = response.body.getReader() const total = Number(response.headers.get('content-length'))