The TextDecoderStream()
constructor creates a new TextDecoderStream
object which is used to convert a stream of text in a binary encoding into strings.
On this page
TextDecoderStream: TextDecoderStream() constructor
Syntax
js
new TextDecoderStream(label)
new TextDecoderStream(label, options)
Parameters
-
label
-
A string defaulting to
utf-8
. This may be any valid label. options
Optional-
An object with the property:
Exceptions
-
RangeError
-
Thrown 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"
).
Examples
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.
js
const response = await fetch("https://example.com");
const stream = response.body.pipeThrough(new TextDecoderStream());
Specifications
Specification |
---|
Encoding Standard # ref-for-dom-textdecoderstream① |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
TextDecoderStream |
71 | 79 | 105 | No | 58 | 14.1 | 71 | 71 | 105 | 50 | 14.5 | 10.0 |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/TextDecoderStream/TextDecoderStream