Skip to content

Instantly share code, notes, and snippets.

@annevk
Created May 25, 2022 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save annevk/ca5475aa173820effdd90b5298bf49c1 to your computer and use it in GitHub Desktop.
Save annevk/ca5475aa173820effdd90b5298bf49c1 to your computer and use it in GitHub Desktop.
  1. Let first1024Bytes be null.

  2. In parallel:

    1. Let bytes be the empty byte sequence.

    2. Let transformStream be a new TransformStream.

    3. Let transformAlgorithm given a chunk be these steps:

      1. Enqueue chunk in transformStream.

      2. If first1024Bytes is null, then:

        1. Let chunkBytes be the result of "get a copy of the bytes held by the buffer source" given chunk.

        2. Append chunkBytes to bytes.

        3. If bytes's length is greater than 1024, then:

          1. Truncate bytes so that it only contains the first 1024 bytes.

          2. Set first1024Bytes to bytes.

    4. Set up transformStream with transformAlgorithm set to transformAlgorithm.

    5. Set response's body's stream to the result of piping response's body's stream through transformStream.

  3. Wait until first1024Bytes is non-null or response's body's stream is errored.

@annevk
Copy link
Author

annevk commented May 30, 2022

This did not account for unexpected EOF. With that fixed it's now being iterated on in whatwg/fetch#1442.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment