TransformStream is now supported cross-browser
Now that transform streams are supported in Chrome, Safari, and Firefox, they're finally ready for prime time!
The Streams API allows you to break down a resource that you want to receive, send, or transform into small chunks, and then process these chunks bit by bit. Recently, Firefox 102 started to support TransformStream
, which means TransformStream
is now finally usable across browsers. Transform streams allow you to pipe from a ReadableStream
to a WritableStream
, executing a transformation on the chunks, or consume the transformed result directly, as shown in the following example.
class UpperCaseTransformStream {
constructor() {
return new TransformStream({
transform(chunk, controller) {
controller.enqueue(chunk.toUpperCase());
},
});
}
}
button.addEventListener('click', async () => {
const response = await fetch('/script.js');
const readableStream = response.body
.pipeThrough(new TextDecoderStream())
.pipeThrough(new UpperCaseTransformStream());
const reader = readableStream.getReader();
pre.textContent = '';
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
pre.textContent += value;
}
});
Demo #
Browser support #
Browser support
- Chrome 67, Supported 67
- Firefox 102, Supported 102
- Edge 79, Supported 79
- Safari 14.1, Supported 14.1
Acknowledgements #
Hero image by Arseny Togulev on Unsplash