web-streams-polyfill icon indicating copy to clipboard operation
web-streams-polyfill copied to clipboard

Example for polyfilling TextDecoderStream in previous browsers

Open Krithika3 opened this issue 2 years ago • 3 comments

Hi I was looking to see if we could get an example to polyfill web-streams TextDecoderStream for browsers like firefox as listed here: https://developer.mozilla.org/en-US/docs/Web/API/TransformStream

It would be great if I could find an example for the same

Krithika3 avatar May 11 '22 18:05 Krithika3

There's a polyfill for TextEncoderStream and TextDecoderStream in the demos of the Streams standard. You can first load web-streams-polyfill to define TransformStream, and then load the encoding stream polyfill.

Even better: the encoding streams polyfill is available as an npm package over at @stardazed/streams-text-encoding. It should be compatible with web-streams-polyfill, so you can do something like:

import 'web-streams-polyfill';
import { TextDecoderStream } from '@stardazed/streams-text-encoding';

readable.pipeThrough(new TextDecoderStream()).pipeTo(/* ... */);

MattiasBuelens avatar May 12 '22 07:05 MattiasBuelens

Thanks @MattiasBuelens :) I will try it out. Appreciate your help

Krithika3 avatar May 12 '22 16:05 Krithika3

@MattiasBuelens I think it would be nice to have both of these polyfills included too, could be useful in some other runtimes, like Bun, which is still has no TextEncoderStream and TextDecoderStream. Here's the issue I am referring: https://github.com/oven-sh/bun/issues/5648 and here's my proposal for implementation (basically same Node.js' implementation, but TS flavored) in comments: https://github.com/oven-sh/bun/issues/5648#issuecomment-2143413569

octet-stream avatar Jun 10 '24 11:06 octet-stream