Fix CBOR Encode for cbor v9: use streaming Encoder with pre-sorted Maps#2423
Open
J8k3 wants to merge 1 commit into
Open
Fix CBOR Encode for cbor v9: use streaming Encoder with pre-sorted Maps#2423J8k3 wants to merge 1 commit into
J8k3 wants to merge 1 commit into
Conversation
cbor v9 changed Encoder.encode() and encodeCanonical() from sync to async
stream-based, but the static sync wrappers still exist and silently return
only the first chunk (~1 byte) of the output.
Fix: drive a streaming Encoder directly —
- Pre-sort object keys into Maps using RFC 7049 canonical order
(length-first, then lexicographic byte comparison) before encoding,
so insertion order gives the canonical result.
- Register a custom Map semantic type that bypasses the encoder's own
canonical sort (which has the same single-byte bug in v9).
- Buffer all output chunks, then return the correctly sliced ArrayBuffer
(avoiding Node.js Buffer pool aliasing via byteOffset/byteLength).
The run() method becomes async; the framework already awaits run().
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
cborv9 changedEncoder.encode()andencodeCanonical()from synchronous to async stream-based, but the static sync wrappers were kept and silently return only the first chunk (~1 byte) of output. The existingCBOREncodeoperation calledCbor.encodeCanonical(input)synchronously, so all multi-byte values were truncated.Affected test cases (currently failing):
CBOR Encode: Can encode text— expected64 54 65 78 74, received64CBOR Encode: Can encode map— expecteda3 61 61 01 ..., receiveda3CBOR Encode: Can encode list— expected83 00 01 02, received83CBOR Decode: Can encode decimal— input truncated before decodeSingle-byte values (
0ffor integer 15,f4/f5for booleans) still passed by coincidence.Fix
Drive a streaming
Encoderdirectly:Maps using RFC 7049 canonical order (shortest encoded key first, then lexicographic byte comparison) before encoding — so insertion order produces the canonical result.Mapsemantic type that bypasses the encoder's own canonical sort (which has the same first-byte-only bug in v9 due to an internalNoFilterstream).dataevent, then return a correctly slicedArrayBuffer(avoiding Node.js Buffer pool aliasing viabyteOffset/byteLength).run()becomesasync; the CyberChef framework alreadyawaitsrun().Test plan
CBOR Encode: Can encode integer—0f✓CBOR Decode: Can encode decimal—f9 3e 00✓CBOR Encode: Can encode text—64 54 65 78 74✓CBOR Encode: Can encode boolean true—f5✓CBOR Encode: Can encode boolean false—f4✓CBOR Encode: Can encode map—a3 61 61 01 61 62 02 61 63 03✓CBOR Encode: Can encode list—83 00 01 02✓🤖 Generated with Claude Code