Skip to content

Latest commit

 

History

History
172 lines (146 loc) · 9.98 KB

README.md

File metadata and controls

172 lines (146 loc) · 9.98 KB

node:string_decoder — Node.js compatibility tests

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.

The oracle

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.

How they run

cargo build -p zun_cli                 # the `zun` binary
bash zun-node/tools/run_node_tests.sh \
    --zun target/debug/zun \
    --module string_decoder

A 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').

Which Node implementation this follows

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:

  1. 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.js pins it: write('f69b') then write('d1') is two, write('f4') then write('bde5') is two, but write('f0') then write('b841') is one. Classifying that is writing a UTF-8 decoder — so the aborted bytes are instead handed to the engine's own lossy toString('utf8'), which agrees with V8 byte for byte on every vector in the corpus. __sdAbort is that handoff.
  2. text(buf, offset) clears the buffered state and re-enters write on buf.slice(offset), which is Node 25's shape, not v8.x's direct call into the per-encoding text.

Verification: a differential run against Node

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 successive write() 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's testEnd matrix, all 32 two-chunk write/write/end vectors across the six encodings it uses plus latin1 and ascii. Zero divergences.
  • Re-run at c4afcca4, after the Buffer.isBuffer change 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.

Status and what blocks the rest

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.

What src/string_decoder/string_decoder.ts carries, and why

  • Every method takes a caller-supplied buffer as any, checks it with Buffer.isBuffer, and then hands it to a top-level function with a Buffer-typed parameter. A byte view read through any is completely opaque in this engine (buf.length is undefined, buf.toString(...) throws), while the same any value passed into a Buffer parameter 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's write also accepts a Uint16Array and a DataView, and handing either to the typed-array accessors is an uncatchable engine error that stops the program rather than throwing. Buffer.isBuffer is the one engine predicate that both survives the any boundary and guarantees the bytes can be read, so a non-U8 view is now reported as the wrong type — Node's answer for a shape it rejects, a divergence for a Uint16Array, and never a crash. src/buffer/buffer.ts's isUtf8/isAscii made the same change.

  • The four pending bytes are four number fields, not the lastChar Buffer Node keeps them in. A typed-array view held in a class instance field has its backing ArrayBuffer collected 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 only reference_slots(type_rid), which is empty for RID 0. The repro and the code sites are in ../buffer/README.md, group C, re-verified at c4afcca4: 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. A StringDecoder per chunk sequence is exactly that loop, so a Buffer is materialized only for the one operation that needs a real view (toString) and for the lastChar property Node exposes, which is a getter here.

  • __sdUtf16Decode replaces buf.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, and end() after it must answer that lone surrogate. String.fromCharCode does keep one, so the decode is spelled out over the bytes.

  • x as u64 on every size and offset, Number(...) on every byte and every .length — the nodeBuffer intrinsics take machine integers and the checker rejects a number there (zun#3).

  • StringDecoder is a real class and the module object carries it as a value, with StringDecoder as the module's canonical binding in the bundler, because new <identifier>() only compiles for a statically known class and never for a member expression (zun#18).

One change to shared harness code

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).