Repository navigation
Transcode ASCII and Latin-1 like Node in node:buffer transcode() - #8687
Conversation
transcode() opened ICU converters named "us-ascii" and "iso8859-1", but
Chromium's ICU data makes both aliases of windows-1252. So every pair with
ASCII or Latin-1 on either side, apart from widening into UTF-16LE, gave
different bytes from Node:
transcode(Buffer.from([0x80]), 'latin1', 'utf8') // Node: U+0080, workerd: U+20AC
transcode(Buffer.from('€'), 'utf8', 'latin1') // Node: '?', workerd: 0x80
transcode(Buffer.from([0xe9]), 'ascii', 'utf8') // Node: U+FFFD, workerd: U+00E9
ascii->ascii, ascii->latin1 and latin1->ascii passed bytes 0x80-0xFF
through, where Node gives '?'. This was also true of the C++
implementation, which used the same names.
In this patch these pairs no longer use ICU. A small safe-Rust converter
decodes Latin-1 bytes to U+0000-U+00FF, and non-ASCII bytes, ill-formed
UTF-8 (per maximal subpart) and unpaired surrogates to U+FFFD. It encodes
into ASCII or Latin-1 with '?' for anything the target can't represent,
and drops an unrepresentable default-ignorable code point, as ICU's
substitute callback does. ASCII and Latin-1 to UTF-8 are now sized
exactly. A differential run of 2240 transcode() calls against Node 26
now gives the same bytes for every call that succeeds in both.
This changes the output of existing Workers that transcode ASCII or
Latin-1 text containing non-ASCII bytes. We accept that without a compat
flag: the old output is wrong for code written against Node, and doesn't
agree with workerd's own Buffer#toString('latin1').
ICU now converts only the UTF-8 and UTF-16LE identity pairs in transcode(). Their targets can represent every code point, so the '?' substitute set on the target converter is never used. In this patch Converter::set_subst_chars, Converter::min_char_size and the SetSubstituteCharsFailed error go away.
There was a problem hiding this comment.
✅ Approved — No issues found. The converter sizes and bounds its output correctly, unsafe ICU code is removed, and the commit message explicitly accepts the compatibility change.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
The new code is safe Rust, and its destination sizing covers every write. Removing the raw-pointer ICU wrappers reduces the unsafe surface.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md updates needed.
📋 Code Quality: ✅ LGTM
The converter and its tests are correct. dest_len bounds every write transcode_into makes, and the reworked Transcoder::new match handles invalid encodings correctly. The first commit message records the decision to change behaviour without a compatibility flag.
⚡ Performance: ✅ LGTM
No performance concerns.
📝 Documentation: ✅ LGTM
No documentation issues.
Reviewed commit: c5e85c34
guybedford
left a comment
There was a problem hiding this comment.
LGTM. I checked this against Node on a set of edge cases (CESU surrogates, overlong and truncated UTF-8, lone surrogates at EOF, BOMs, U+E0001 vs U+E1000, U+00AD into ASCII vs Latin-1), and they all match.
Optional follow-ons, not blocking:
- Latin-1 to UTF-8 could use simdutf's
convert_latin1_to_utf8/utf8_length_from_latin1, alongside the existingconvert_latin1_to_utf16. An all-ASCII memcpy fast path for ascii->ascii/utf8 would be a similar win. - The generic decode/encode loop in
single_bytecould also cover the UTF-8 and UTF-16LE identity pairs, which would let the crate dropConverterand the remainingucnv_*FFI.
transcode()opens ICU converters namedus-asciiandiso8859-1, but Chromium's ICU data makes both aliases of windows-1252. So every pair with ASCII or Latin-1 on either side, apart from widening into UTF-16LE, gave different bytes from Node:asciitoascii,asciitolatin1andlatin1toasciialso passed bytes 0x80-0xFF through unchanged, where Node gives?. The C++ implementation used the same converter names and had the same problem.In this patch these pairs no longer use ICU. A small safe-Rust converter decodes Latin-1 bytes to U+0000-U+00FF. Non-ASCII bytes, ill-formed UTF-8 (one per maximal subpart) and unpaired surrogates decode to U+FFFD. Encoding into ASCII or Latin-1 writes
?for anything the target can't represent, and drops a default-ignorable code point it can't represent, as ICU's substitute callback does. ICU is left with the UTF-8 and UTF-16LE identity pairs, so the second commit removes the substitute setup that nothing uses any more.I compared workerd with Node 26 in two runs: 2240 hand-picked
transcode()calls covering every encoding pair and alias, and 4000 seeded random calls built from raw bytes and code points at the edges of every default-ignorable range, plus surrogates, C1 controls, noncharacters and U+10FFFF. Every call that succeeds in both now gives the same bytes. Before, 190 of the hand-picked calls didn't.This changes the output for existing Workers that transcode ASCII or Latin-1 text with non-ASCII bytes. We accept that without a compat flag: the old output is wrong for code written for Node, and it doesn't match workerd's own
buffer.toString('latin1').