Streaming TextDecoder drops U+FEFF at the start of a chunk that follows an ASCII-only chunk

Open Beginner friendly
#8,279 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@oliarmak-web is already working on this.

Since Nov 8, 2026.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp, javascript
Domain
backend, testing

Research direction

Start in src/workerd/api/encoding.c++ at IcuDecoder::decode and trace the UTF-8 ASCII fast path and bomSeen handling. Reproduce the reported outputs with the supplied worker.js and config.capnp using npx workerd serve, then compare streaming behavior with the Encoding Standard. Done when the later U+FEFF is preserved after an ASCII-only chunk and regression coverage exercises both ASCII and non-ASCII first chunks.

Written by the indexing model from the issue text.

Description

A streaming UTF-8 TextDecoder (and so TextDecoderStream) strips EF BB BF at the start of a later chunk as if it were a BOM, when every earlier chunk was ASCII. After a non-ASCII first chunk the same bytes are kept as U+FEFF, which is the expected result.

worker.js:

export default {
  async fetch() {
    const out = [];
    const d = new TextDecoder();
    out.push(JSON.stringify(d.decode(new Uint8Array([0x41]), { stream: true })));
    out.push(JSON.stringify(d.decode(new Uint8Array([0xEF, 0xBB, 0xBF, 0x42]))));
    const e = new TextDecoder();
    out.push(JSON.stringify(e.decode(new Uint8Array([0xC3, 0xA9]), { stream: true })));
    out.push(JSON.stringify(e.decode(new Uint8Array([0xEF, 0xBB, 0xBF, 0x42]))));
    return new Response(out.join('\n') + '\n');
  }
};

config.capnp:

using Workerd = import "/workerd/workerd.capnp";

const config :Workerd.Config = (
  services = [(name = "main", worker = .worker)],
  sockets = [(name = "http", address = "127.0.0.1:18787", http = (), service = "main")],
);

const worker :Workerd.Worker = (
  modules = [(name = "worker.js", esModule = embed "worker.js")],
  compatibilityDate = "2026-11-01",
);

npx workerd serve config.capnp and curl http://127.0.0.1:18787/

Actual:

"A"
"B"
"é"
"\ufeffB"

(JSON.stringify prints U+FEFF literally; it is written as \ufeff here)

Expected: the second line is also "\ufeffB", as in Node.js, Bun and browsers. Per https://encoding.spec.whatwg.org/#concept-td-serialize, BOM seen is set after the first code point, ASCII or not.

Tested with workerd 1.20261107.1 from npm (workerd 2026-11-07), Linux x64. The code is unchanged on main bf15531.

In IcuDecoder::decode (src/workerd/api/encoding.c++), the UTF-8 ASCII fast path returns without setting bomSeen = true, so the first non-ASCII chunk that reaches the ICU path still strips a leading U+FEFF.

Credit: https://ucsbplse.github.io/

Dominant language
C++
Stars
9.3k
Forks
845
Avg merge
1d 23h
Merged PRs (30d)
558

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from cloudflare/workerd

All issues in cloudflare/workerd

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.