Skip to content

encode streamed chunks - #9136

Merged
dummdidumm merged 1 commit into
masterfrom
encode-chunks
Feb 20, 2023
Merged

encode streamed chunks#9136
dummdidumm merged 1 commit into
masterfrom
encode-chunks

Conversation

@Rich-Harris

Copy link
Copy Markdown
Member

It turns out that if response.body is a ReadableStream, it must be a ReadableStream<Uint8Array>. This escaped notice before, because Node does its own thing, quelle surprise

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

@dummdidumm
dummdidumm merged commit cf4a843 into master Feb 20, 2023
@dummdidumm
dummdidumm deleted the encode-chunks branch February 20, 2023 19:34
@github-actions github-actions Bot mentioned this pull request Feb 20, 2023
Rich-Harris added a commit that referenced this pull request Aug 20, 2026
Four places hand-write the producer side of a stream.
`server/page/render.js` and `server/data/index.js` each construct a
`ReadableStream` whose `start` enqueues an encoded head, loops an async
iterable of chunks and closes; `form-utils.js` `LazyFile.stream()`
drives `read_range` through a `pull` adapter that re-tracks a cursor the
generator already knows; and `utils/streaming.js`
`create_async_iterator` implements `[Symbol.asyncIterator]`/`next` by
hand with two index cursors.

This replaces them with async generators and `ReadableStream.from`,
which `server/index.js` already relies on for promised `read` results.
The two response pumps share a `stream_text(head, chunks)` helper,
`LazyFile.stream()` becomes a `for await` over `read_range`, and
`iterate` becomes an `async function*` over the deferred list.

Two deliberate deltas: the response streams lose `type: 'bytes'` (added
alongside the encoding fix in #9136; `Response` only requires
`Uint8Array` chunks, which `.from` over encoded strings provides), and
an incomplete `LazyFile` now errors with an `Error` instead of a bare
string.

---

### Please don't delete this checklist! Before submitting the PR, please
make sure you do the following:
- [x] It's really useful if your PR references an issue where it is
discussed ahead of time. In many cases, features are absent for a
reason. For large changes, please create an RFC:
https://github.com/sveltejs/rfcs
- [x] This message body should clearly illustrate what problems it
solves.
- [ ] Ideally, include a test that fails without this PR but passes with
it.

### Tests
- [x] Run the tests with `pnpm test` and lint the project with `pnpm
lint` and `pnpm check`

### Changesets
- [x] If your PR makes a change that should be noted in one or more
packages' changelogs, generate a changeset by running `pnpm changeset`
and following the prompts. Changesets that add features should be
`minor` and those that fix bugs should be `patch`. Please prefix
changeset messages with `feat:`, `fix:`, or `chore:`.

---------

Co-authored-by: Rich Harris <rich.harris@vercel.com>
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

Successfully merging this pull request may close these issues.

2 participants