cloudflare / cloudflare/workerd
🐛 Bug Report — Runtime APIs - WritableStream DefaultWriter await write() does not copy chunk buffer
- Dominant language
- C++
- Stars
- 8.7k
- Forks
- 739
- Avg merge
- 2d 20h
- Merged PRs (30d)
- 174
Description
If you write a buffer to a TransformStream's WritableStream with `await writer.write()` and then modify the buffer after the promise completes, the output stream will be corrupted.
As best as I can tell [from the spec](https://streams.spec.whatwg.org/#default-writer-prototype), it should be safe to modify the chunk after the promise resolves:
>If chunk is mutable, [producers](https://streams.spec.whatwg.org/#producer) are advised to avoid mutating it after passing it to [write()](https://streams.spec.whatwg.org/#default-writer-write), until after the promise returned by [write()](https://streams.spec.whatwg.org/#default-writer-write) settles. This ensures that the [underlying sink](https://streams.spec.whatwg.org/#underlying-sink) receives and processes the same value that was passed in.
As it stands currently, the only safe way to do it is to `slice()` the buffer to make a copy of it when you pass it to write().
This was discovered as part of working on a [worker that uses wasm to compress the response streams](https://github.com/pmeenan/dictionary-worker/blob/main/src/index.js#L285) in a passthrough transform stream. The resulting stream would be corrupted with chunks being overwritten with later data.
Contributor guide
Assessment
This issue has not been assessed yet.