Note: This feature is available in Web Workers.
The TextDecoderStream() constructor creates a new TextDecoderStream object which is used to convert a stream of text in a binary encoding into strings.
new TextDecoderStream(label)
new TextDecoderStream(label, options)labelA string defaulting to utf-8. This may be any valid label.
options OptionalAn object with the following properties:
fatal OptionalA boolean value indicating if the TextDecoder.decode() method must throw a TypeError when decoding invalid data. It defaults to false, which means that the decoder will substitute malformed data with a replacement character.
ignoreBOM OptionalA boolean value indicating whether the byte order mark will be included in the output or skipped over. It defaults to false, which means that the byte order mark will be skipped over when decoding and will not be included in the decoded text.
RangeErrorThrown if the value of label is unknown, or is one of the values leading to a 'replacement' decoding algorithm ("iso-2022-cn" or "iso-2022-cn-ext").
The following example demonstrates how to decode binary data retrieved from a fetch() call. The data will be interpreted as UTF-8, as no label has been passed.
const response = await fetch("https://example.com");
const stream = response.body.pipeThrough(new TextDecoderStream());