(url, outputPath, ctxId)
| 145 | } |
| 146 | |
| 147 | var web_req_async_download = function (url, outputPath, ctxId) { |
| 148 | create_web_ctx_if_needed(ctxId); |
| 149 | var urlCpy = url; |
| 150 | url = web_req_build_url(url, ctxId); |
| 151 | |
| 152 | var headers = new Headers(); |
| 153 | for (var i = 0; i < web_req_ctxs[ctxId].headers.length; i++) { |
| 154 | headers.append(web_req_ctxs[ctxId].headers[i].key, web_req_ctxs[ctxId].headers[i].value); |
| 155 | } |
| 156 | |
| 157 | var payload = web_req_get_payload(ctxId); |
| 158 | |
| 159 | var options = { |
| 160 | method: web_req_ctxs[ctxId].method, |
| 161 | headers: headers, |
| 162 | }; |
| 163 | if (web_req_ctxs[ctxId].method != "GET" && web_req_ctxs[ctxId].method != "HEAD") { |
| 164 | options.body = payload ?? web_req_ctxs[ctxId].formdata ?? web_req_ctxs[ctxId].ody; |
| 165 | } |
| 166 | |
| 167 | const controller = new AbortController(); |
| 168 | setTimeout(() => controller.abort(), web_req_ctxs[ctxId].timeout); |
| 169 | options.signal = controller.signal; |
| 170 | |
| 171 | // TODO: upload progress callback support for Fetch API |
| 172 | var use_download_callback = web_req_ctxs[ctxId].use_download_callback; |
| 173 | |
| 174 | fetch(url, options) |
| 175 | .then(async (response) => { |
| 176 | if (!use_download_callback) { |
| 177 | return response; |
| 178 | } |
| 179 | if (!response.ok || !response.body) { |
| 180 | // nothing to process |
| 181 | return response; |
| 182 | } |
| 183 | const contentEncoding = response.headers.get('content-encoding'); |
| 184 | const contentLength = response.headers.get(contentEncoding ? 'x-file-size' : 'content-length'); |
| 185 | if (contentLength === null) { |
| 186 | return response; |
| 187 | } |
| 188 | const total = parseInt(contentLength, 10); |
| 189 | if (total == 0) { |
| 190 | return response; |
| 191 | } |
| 192 | |
| 193 | let loaded = 0; |
| 194 | return new Response( |
| 195 | new ReadableStream({ |
| 196 | async start(controller) { |
| 197 | const reader = response.body.getReader(); |
| 198 | for (; ;) { |
| 199 | const { done, value } = await reader.read(); |
| 200 | if (done) { |
| 201 | break; |
| 202 | } |
| 203 | loaded += value.byteLength; |
| 204 | var v = loaded / total; |
no test coverage detected