You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When decoding a Response body with .text() method, output may not be what is actually expected.
Below is a repro:
constfile=awaitDeno.open(newURL("test.json",import.meta.url),{read: true})constoriginal=awaitDeno.readTextFile("test.json")letcopy=""constbody=newReadableStream<Uint8Array>({start: async(controller)=>{forawait(constchunkofDeno.iter(file)){copy+=newTextDecoder().decode(chunk)controller.enqueue(chunk)}file.close();controller.close();}});constfetched=awaitnewResponse(body,{status: 200}).text()//Testsconsole.log(original===copy)//Should be trueconsole.log(original===fetched)//Should be trueconsole.log(original.length,copy.length,fetched.length)//Should all be equalsconsole.log(original.substring(0,20))console.log("======================")console.log(copy.substring(0,20))console.log("======================")console.log(fetched.substring(0,20))
Original, copy and fetched strings should be the same but for some reason fetched content doesn't start at correct place.
Since it works fine by pushing directly to a string variable with TextDecoder, I assume it's come either from controller.enqueue or the Response.body handling
I think the bug comes somewhere from how body is consumed or packaged in extensions/fetch/22_body.js because I get the bug in .text(), .json() and .blob(). Looking at source code I figured out that .body does neither of them so you can just consume the stream yourself to make it work 🙂
The text was updated successfully, but these errors were encountered:
After breaking my head over this for about an hour:
Deno.iter
Iterator uses an internal buffer of fixed size for efficiency; it returns a view on that buffer on each iteration. It is therefore caller's responsibility to copy contents of the buffer if needed; otherwise the next iteration will overwrite contents of previously returned chunk.
Nice thanks a lot for debugging it and sorry for this 😅
I submitted a PR to lucacasonato/deno_local_file_fetch#10 to apply your patch since originally I opened this because it was the implementation of local files fetching in deno deploy
When decoding a
Response
body with.text()
method, output may not be what is actually expected.Below is a repro:
Outputs the following:
Original, copy and fetched strings should be the same but for some reason fetched content doesn't start at correct place.
Since it works fine by pushing directly to a string variable with
TextDecoder
, I assume it's come either fromcontroller.enqueue
or theResponse.body
handlingLink to tested JSON file
(Original issue lucacasonato/deno_local_file_fetch#8 (comment))
Edit: Here's a workaround in case anyone need it:
I think the bug comes somewhere from how body is consumed or packaged in extensions/fetch/22_body.js because I get the bug in
.text()
,.json()
and.blob()
. Looking at source code I figured out that.body
does neither of them so you can just consume the stream yourself to make it work 🙂The text was updated successfully, but these errors were encountered: