Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

TextDecoderStream — streaming byte decoding

TextDecoderStream converts a byte stream into text, preserving decoder state between chunks.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

new TextDecoderStream(label = "utf-8", { fatal = false, ignoreBOM = false } = {})

TextDecoderStream is a Web API from the Encoding Standard, not a particular ECMAScript edition. Check typeof TextDecoderStream !== "undefined"; Node 24 does not establish browser support.

writable accepts buffers and views such as Uint8Array; readable supplies strings. Use pipeThrough(). Incomplete sequences await more bytes; output chunks do not delimit records. Closing input finalizes decoding.

UTF-8 is the default; errors produce U+FFFD. fatal: true errors both streams with TypeError. An invalid encoding label causes a constructor RangeError.

ignoreBOM: false removes an initial UTF-8/UTF-16 BOM; true preserves it in text. encoding, fatal, and ignoreBOM are read-only.

Examples

(async () => {
  const bytes = new ReadableStream({
    start(controller) {
      controller.enqueue(new Uint8Array([0x41, 0xE2]));
      controller.enqueue(new Uint8Array([0x82, 0xAC, 0x42]));
      controller.close();
    }
  });

  const reader = bytes
    .pipeThrough(new TextDecoderStream("utf-8", { fatal: true }))
    .getReader();

  let text = "";
  try {
    while (true) {
      const { value, done } = await reader.read();
      if (done) break;
      text += value;
    }
  } finally {
    reader.releaseLock();
  }
  console.log(text);
})().catch(console.error);

Result

A€B

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources