The compatibility bar for string_decoder is Node.js 25's own test suite,
not tests we write. string_decoder is not finished until it passes 100% of
Node's test-string-decoder* tests.
NODE_TESTS.txt is the full inventory of Node 25's corpus for this module —
three files:
test-string-decoder.js test-string-decoder-end.js
test-string-decoder-fuzz.js
Each is fetched verbatim from nodejs/node at tag v25.0.0; nothing is
vendored here.
cargo build -p zun_cli # the `zun` binary
bash zun-node/tools/run_node_tests.sh \
--zun target/debug/zun \
--module string_decoderA zun-ts module: no --ext. Each test is bundled by
tools/bundle_node_test.sh and run as zun run <bundle.ts>.
src/string_decoder/string_decoder.ts is in the bundler's CONDITIONAL_FILES
together with src/buffer/buffer.ts, which it reads ERR_UNKNOWN_ENCODING and
__bufNormalizeEncoding out of — exactly as Node's lib/string_decoder.js
does require('buffer').
Node 25's lib/string_decoder.js is a thin wrapper over a C++ decoder
(internalBinding('string_decoder')) that keeps its state in a 7-byte Buffer.
There is no internalBinding here, so the algorithm is Node's own
pure-JavaScript decoder — lib/string_decoder.js as it stood through v8.x:
utf8CheckByte / utf8CheckIncomplete / utf8CheckExtraBytes /
utf8FillLast / utf8Text, utf16Text/utf16End, base64Text/base64End,
and the single-byte simpleWrite/simpleEnd. lastChar, lastNeed and
lastTotal are that implementation's real state, so the undocumented surface
Node still exposes is the natural shape here rather than a reconstruction.
Two places where that code is not followed, because the C++ decoder — and therefore the oracle — behaves differently:
- How many U+FFFD an aborted multi-byte character produces. The v8.x code
answers a single one. Node 25 answers one per maximal subpart of the
ill-formed sequence (Unicode's recommended practice, which is what V8 does),
and
test-string-decoder.jspins it:write('f69b')thenwrite('d1')is two,write('f4')thenwrite('bde5')is two, butwrite('f0')thenwrite('b841')is one. Classifying that is writing a UTF-8 decoder — so the aborted bytes are instead handed to the engine's own lossytoString('utf8'), which agrees with V8 byte for byte on every vector in the corpus.__sdAbortis that handoff. text(buf, offset)clears the buffered state and re-enterswriteonbuf.slice(offset), which is Node 25's shape, not v8.x's direct call into the per-encodingtext.
The three corpus files all fail to compile (below), so the module is verified
the way os is — by running the same program under node and under zun and
diffing:
test-string-decoder.js's own driver, in full. For 33 byte sequences × every possible way of splitting them across successivewrite()calls (writeSequences, up to 16 splits each) × 8 encodings —utf8,utf16le,ucs2,base64,base64url,hex,latin1,ascii— that is 1216 cases, including every invalid-UTF-8 vector the test file lists (c9b5a941,ccccb8,f0b841,f1ccb8,f0fb00,cce2b8b8,e2b8ccb8,e2fbcc01,ccb8cdb9,eda0b5edb08d), the surrogate-pair cases and both nodejs/node#22626 regressions. Zero divergences.test-string-decoder-end.js'stestEndmatrix, all 32 two-chunkwrite/write/endvectors across the six encodings it uses pluslatin1andascii. Zero divergences.- Re-run at
c4afcca4, after theBuffer.isBufferchange below: 33 byte sequences × fixed chunk widths of 1, 2, 3 and 5 bytes × the same 8 encodings — 1056 cases, compared code unit by code unit (not code point: a surrogate pair has to survive as two units, and comparing by code point hides exactly the bug a UTF-16 stream decoder is most likely to have). Zero divergences. The run is driven as 32 short processes because one process dies on the GC bug after ~256 decoders.
One of those encodings only matches because the module decodes it itself:
Node's ascii codec is one byte per character with the high bit masked
(Buffer.from('e280','hex').toString('ascii') is 62 00), while the engine's
toString('ascii') runs the bytes through the UTF-8 decoder and answers a
single U+FFFD. __sdAsciiDecode does the masking. That engine gap is listed in
../buffer/README.md, group E.
RESULTS.md is the live per-test table; it and the README bar are
regenerated by every tracker run.
Re-measured against the current release engine: test-string-decoder-end.js
passes (Buffer.from over any and Buffer.of work now), and the other two
are blocked in the engine, not in string_decoder.ts:
| Test | Blocked by |
|---|---|
test-string-decoder-fuzz.js |
zun#57 — Math.random works now, but the oracle side of the comparison, Buffer.concat(bufs).toString('utf16le'), replaces a lone surrogate with U+FFFD, while Node (and this decoder) keep it: Buffer.from([0x0a, 0xde]).toString('utf16le') is '\ude0a' in Node and '\ufffd' here. Every mismatch a probe of 3 000 random runs found was that case; every other encoding agrees. Buffer is the engine's realm global, so the codec cannot be fixed from this library (see ../buffer/README.md). |
test-string-decoder.js |
zun#154 — writeSequences(length, end, subSequence, sequences) against a 3-parameter declaration is a compile error even in plain .js; behind it, zun#155 — new StringDecoder('utf8').__proto__.write(...) is no field or getter __proto__. |
Until the harness-level function test(...) collision was fixed (see below),
test-string-decoder.js reported duplicate function declaration and hid the
real blocker.
-
Every method takes a caller-supplied buffer as
any, checks it withBuffer.isBuffer, and then hands it to a top-level function with aBuffer-typed parameter. A byte view read throughanyis completely opaque in this engine (buf.lengthisundefined,buf.toString(...)throws), while the sameanyvalue passed into aBufferparameter works. That asymmetry is the shape the whole file is built around.The predicate used to be
ArrayBuffer.isView, which was a latent crash: Node'swritealso accepts aUint16Arrayand aDataView, and handing either to the typed-array accessors is an uncatchable engine error that stops the program rather than throwing.Buffer.isBufferis the one engine predicate that both survives theanyboundary and guarantees the bytes can be read, so a non-U8view is now reported as the wrong type — Node's answer for a shape it rejects, a divergence for aUint16Array, and never a crash.src/buffer/buffer.ts'sisUtf8/isAsciimade the same change. -
The four pending bytes are four
numberfields, not thelastCharBuffer Node keeps them in. A typed-array view held in a class instance field has its backingArrayBuffercollected out from under it at the first young collection — reproducibly on the 256th instance — because the view object carries type RID 0 and the GC's mark phase traces onlyreference_slots(type_rid), which is empty for RID 0. The repro and the code sites are in../buffer/README.md, group C, re-verified atc4afcca4: the bug is unchanged, and the differential run below still hits it after ~256 decoders, which is why it is driven as 32 short processes rather than one. AStringDecoderper chunk sequence is exactly that loop, so a Buffer is materialized only for the one operation that needs a real view (toString) and for thelastCharproperty Node exposes, which is a getter here. -
__sdUtf16Decodereplacesbuf.toString('utf16le', …). The engine's UTF-16 decoder turns an unpaired surrogate into U+FFFD where V8 keeps it, and carrying an unpaired high surrogate across a chunk boundary is the entire point of a UTF-16 stream decoder:write('3dd8')must answer''and remember the surrogate, andend()after it must answer that lone surrogate.String.fromCharCodedoes keep one, so the decode is spelled out over the bytes. -
x as u64on every size and offset,Number(...)on every byte and every.length— thenodeBufferintrinsics take machine integers and the checker rejects anumberthere (zun#3). -
StringDecoderis a real class and the module object carries it as a value, withStringDecoderas the module's canonical binding in the bundler, becausenew <identifier>()only compiles for a statically known class and never for a member expression (zun#18).
tools/harness/node_test.ts (now src/test/test.ts) declares a top-level function test(...), and
test is the name several of Node's own test files give their local helper —
test-string-decoder.js, test-buffer-tostring-rangeerror.js and
test-buffer-constructor-node-modules-paths.js among them. Two top-level
declarations of one name are a compile error in a single Zun program, so every
one of those files reported duplicate function declaration and its real
blocker was invisible. The file has moved from the bundler's HARNESS_FILES to
its CONDITIONAL_FILES, under both names it provides (test and tty), so
only a test that actually requires one of them gets it. The four baseline
corpora were unchanged by the move (path 14/17, events 5/35, os 1/7, util 7/26
at that time; they have moved since for reasons unrelated to this file).