Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Response.prototype.text() returning empty string in some cases #724

Open
harmony7 opened this issue Feb 7, 2024 · 1 comment
Open

Response.prototype.text() returning empty string in some cases #724

harmony7 opened this issue Feb 7, 2024 · 1 comment

Comments

@harmony7
Copy link
Contributor

harmony7 commented Feb 7, 2024

Hi all,

I think i’ve found a bug with either Response or ReadableStream, can you check this out when you have a moment?

The following code doesn't work as expected. It should return the same string as the input, but instead it returns an empty string:

  const text = "foo";

  // Create a ReadableStream from string
  const stream1 = new Response(text).body;

  // Read from stream1 using Response.prototype.text();
  const textFromStream1 = await new Response(stream1).text();

  // textFromStream1 should be 'foo', but it's ''

I've put a repro of this up in a fiddle https://fiddle.fastly.dev/fiddle/750711db
This happens for me on the newest version of js-compute-runtime: 3.8.2

@harmony7
Copy link
Contributor Author

harmony7 commented Feb 8, 2024

Something to note is that if I instead use a reader like this, I get the right result (so I'm currently using this as a workaround):

  const text = "foo";

  // Create a ReadableStream from string
  const stream2 = new Response(text).body;

  // Use a reader to read from stream2
  const stream2Reader = stream2.getReader();
  let textFromStream2 = '';
  const decoder = new TextDecoder();
  while(true) {
    const { done, value } = await stream2Reader.read();
    if (done) {
      break;
    }
    textFromStream2 += decoder.decode(value);
  }

  // textFromStream2 correctly contains 'foo'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant