Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 26 additions & 18 deletions src/Fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,21 +576,23 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
// This receives a condition, which determines whether to save the xhr's
// response, or just 0.
function saveResponseAndStatus() {
var ptr = 0;
var ptrLen = 0;
if (xhr.response && fetchAttrLoadToMemory && {{{ makeGetValue('fetch', C_STRUCTS.emscripten_fetch_t.data, '*') }}} === 0) {
let ptrLen = 0;
if (fetchAttrLoadToMemory && xhr.response) {
let ptr = {{{ makeGetValue('fetch', C_STRUCTS.emscripten_fetch_t.data, '*') }}};
ptrLen = xhr.response.byteLength;
}
if (ptrLen > 0) {
#if FETCH_DEBUG
dbg(`fetch: allocating ${ptrLen} bytes in Emscripten heap for xhr data`);
#endif

// The data pointer malloc()ed here has the same lifetime as the emscripten_fetch_t structure itself has, and is
// freed when emscripten_fetch_close() is called.
ptr = _realloc({{{ makeGetValue('fetch', C_STRUCTS.emscripten_fetch_t.data, '*') }}}, ptrLen);
if (ptrLen > readI53FromI64(fetch + {{{ C_STRUCTS.emscripten_fetch_t.numBytes }}})) {
#if FETCH_DEBUG
dbg(`fetch: allocating ${ptrLen} bytes in Emscripten heap for xhr data`);
#endif
ptr = _realloc(ptr, ptrLen);
{{{ makeSetValue('fetch', C_STRUCTS.emscripten_fetch_t.data, 'ptr', '*') }}}
}

HEAPU8.set(new Uint8Array(/** @type{Array<number>} */(xhr.response)), ptr);
}
{{{ makeSetValue('fetch', C_STRUCTS.emscripten_fetch_t.data, 'ptr', '*') }}}
writeI53ToI64(fetch + {{{ C_STRUCTS.emscripten_fetch_t.numBytes }}}, ptrLen);
writeI53ToI64(fetch + {{{ C_STRUCTS.emscripten_fetch_t.dataOffset }}}, 0);
var len = xhr.response ? xhr.response.byteLength : 0;
Expand Down Expand Up @@ -660,21 +662,27 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
if (!Fetch.xhrs.has(id)) {
return;
}
var ptrLen = (fetchAttrLoadToMemory && fetchAttrStreamData && xhr.response) ? xhr.response.byteLength : 0;
var ptr = 0;
if (ptrLen > 0 && fetchAttrLoadToMemory && fetchAttrStreamData) {
#if FETCH_DEBUG
dbg(`fetch: allocating ${ptrLen} bytes in Emscripten heap for xhr data`);
#endif
let ptrLen = 0;
if (fetchAttrLoadToMemory && fetchAttrStreamData && xhr.response) {
let ptr = {{{ makeGetValue('fetch', C_STRUCTS.emscripten_fetch_t.data, '*') }}};
ptrLen = xhr.response.byteLength;

#if ASSERTIONS
assert(onprogress, 'When doing a streaming fetch, you should have an onprogress handler registered to receive the chunks!');
#endif

// The data pointer malloc()ed here has the same lifetime as the emscripten_fetch_t structure itself has, and is
// freed when emscripten_fetch_close() is called.
ptr = _realloc({{{ makeGetValue('fetch', C_STRUCTS.emscripten_fetch_t.data, '*') }}}, ptrLen);
if (ptrLen > readI53FromI64(fetch + {{{ C_STRUCTS.emscripten_fetch_t.numBytes }}})) {
#if FETCH_DEBUG
dbg(`fetch: allocating ${ptrLen} bytes in Emscripten heap for xhr data`);
#endif
ptr = _realloc(ptr, ptrLen);
{{{ makeSetValue('fetch', C_STRUCTS.emscripten_fetch_t.data, 'ptr', '*') }}}
}

HEAPU8.set(new Uint8Array(/** @type{Array<number>} */(xhr.response)), ptr);
}
{{{ makeSetValue('fetch', C_STRUCTS.emscripten_fetch_t.data, 'ptr', '*') }}}
writeI53ToI64(fetch + {{{ C_STRUCTS.emscripten_fetch_t.numBytes }}}, ptrLen);
writeI53ToI64(fetch + {{{ C_STRUCTS.emscripten_fetch_t.dataOffset }}}, e.loaded - ptrLen);
writeI53ToI64(fetch + {{{ C_STRUCTS.emscripten_fetch_t.totalBytes }}}, e.total);
Expand Down
1 change: 1 addition & 0 deletions src/lib/libfetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var LibraryFetch = {
'$Fetch',
'$fetchXHR',
'$callUserCallback',
'$readI53FromI64',
'$writeI53ToI64',
'$stringToUTF8',
'$stringToNewUTF8',
Expand Down
1 change: 0 additions & 1 deletion test/fetch/test_fetch_stream_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ int main() {
attr.onsuccess = [](emscripten_fetch_t *fetch) {
printf("Finished downloading %llu bytes\n", fetch->totalBytes);
printf("Data checksum: %08X\n", checksum);
assert(fetch->data == 0); // The data was streamed via onprogress, no bytes available here.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like the idea is that with streaming you get the bytes only in the progress events and not in the onsuccess event.

At least that has been the way the API has been since this test was added 10 years ago: 786f2fe

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, but the same structure (with the ->data pointer) is passed to both events. To keep the test as is, ->data would have to be free'd and NULL'd before calling onsuccess.

As I said in a previous reply, I think of ->data as the persistent buffer between events and ->numBytes as the amount of data in that buffer that's valid for the current event. ->numBytes being 0 is important, but ->data being required to be NULL in onload seems odd.

assert(fetch->numBytes == 0);
assert(fetch->totalBytes == 134217728);
assert(checksum == 0xA7F8E858U);
Expand Down