()
| 576 | // This receives a condition, which determines whether to save the xhr's |
| 577 | // response, or just 0. |
| 578 | function saveResponseAndStatus() { |
| 579 | var ptr = 0; |
| 580 | var ptrLen = 0; |
| 581 | if (xhr.response && fetchAttrLoadToMemory && {{{ makeGetValue('fetch', C_STRUCTS.emscripten_fetch_t.data, '*') }}} === 0) { |
| 582 | ptrLen = xhr.response.byteLength; |
| 583 | } |
| 584 | if (ptrLen > 0) { |
| 585 | #if FETCH_DEBUG |
| 586 | dbg(`fetch: allocating ${ptrLen} bytes in Emscripten heap for xhr data`); |
| 587 | #endif |
| 588 | // The data pointer malloc()ed here has the same lifetime as the emscripten_fetch_t structure itself has, and is |
| 589 | // freed when emscripten_fetch_close() is called. |
| 590 | ptr = _realloc({{{ makeGetValue('fetch', C_STRUCTS.emscripten_fetch_t.data, '*') }}}, ptrLen); |
| 591 | HEAPU8.set(new Uint8Array(/** @type{Array<number>} */(xhr.response)), ptr); |
| 592 | } |
| 593 | {{{ makeSetValue('fetch', C_STRUCTS.emscripten_fetch_t.data, 'ptr', '*') }}} |
| 594 | writeI53ToI64(fetch + {{{ C_STRUCTS.emscripten_fetch_t.numBytes }}}, ptrLen); |
| 595 | writeI53ToI64(fetch + {{{ C_STRUCTS.emscripten_fetch_t.dataOffset }}}, 0); |
| 596 | var len = xhr.response?.byteLength ?? 0; |
| 597 | if (len) { |
| 598 | // If the final XHR.onload handler receives the bytedata to compute total length, report that, |
| 599 | // otherwise don't write anything out here, which will retain the latest byte size reported in |
| 600 | // the most recent XHR.onprogress handler. |
| 601 | writeI53ToI64(fetch + {{{ C_STRUCTS.emscripten_fetch_t.totalBytes }}}, len); |
| 602 | } |
| 603 | {{{ makeSetValue('fetch', C_STRUCTS.emscripten_fetch_t.readyState, 'xhr.readyState', 'i16') }}} |
| 604 | {{{ makeSetValue('fetch', C_STRUCTS.emscripten_fetch_t.status, 'xhr.status', 'i16') }}} |
| 605 | if (xhr.statusText) stringToUTF8(xhr.statusText, fetch + {{{ C_STRUCTS.emscripten_fetch_t.statusText }}}, 64); |
| 606 | if (fetchAttrSynchronous) { |
| 607 | // The response url pointer malloc()ed here has the same lifetime as the emscripten_fetch_t structure itself has, and is |
| 608 | // freed when emscripten_fetch_close() is called. |
| 609 | var ruPtr = stringToNewUTF8(xhr.responseURL); |
| 610 | {{{ makeSetValue('fetch', C_STRUCTS.emscripten_fetch_t.responseUrl, 'ruPtr', '*') }}} |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | xhr.onload = (e) => { |
| 615 | // check if xhr was aborted by user and don't try to call back |
no test coverage detected