feat(byte-codec): portable byte-faithful codec (xxj.1)#62
Merged
Conversation
…] for bytes↔string
Introduces `src/protocol/byte-codec.ts` with `bytesToLatin1` and `latin1ToBytes`,
the single authoritative site for the contract "each byte 0x00-0xFF maps 1:1
to code unit U+0000-U+00FF".
Uses chunked fromCharCode/charCodeAt (not TextDecoder('latin1')) to remain
portable across Node, browser, and Deno — TextDecoder('latin1') silently
remaps 0x80-0x9F via windows-1252 in browsers. Chunks of 8192 avoid
call-stack overflow on large inputs.
Closes tmux-byte-codec-xxj.1.
There was a problem hiding this comment.
Pull request overview
Introduces a single, portable byte-faithful codec module that converts between Uint8Array and a latin1-container string without the windows-1252 remapping pitfalls of TextDecoder('latin1'). This is a preparatory change; downstream transports will be routed through these helpers in a follow-up PR.
Changes:
- Add
src/protocol/byte-codec.tsexportingbytesToLatin1(chunkedString.fromCharCode) andlatin1ToBytes(charCodeAt & 0xff). - Re-export both helpers from the protocol barrel.
- Add 11 unit tests covering full 0x00–0xFF range, 0x80–0x9F fidelity, >8192-byte chunking, and round-trips.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/protocol/byte-codec.ts | New module implementing the byte↔latin1-string contract with LAW comments. |
| src/protocol/index.ts | Re-exports bytesToLatin1/latin1ToBytes from the protocol barrel. |
| tests/unit/byte-codec.test.ts | Unit tests asserting the 1:1 contract, high-byte fidelity, large-input chunking, and round-trips. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Summary
src/protocol/byte-codec.ts— the single enforcer for thebytes ↔ latin1-container stringcontractbytesToLatin1(Uint8Array → string)andlatin1ToBytes(string → Uint8Array), both using chunkedcharCodeAt/fromCharCodefor portability and stack safetyWhy not TextDecoder('latin1')
Browsers alias
latin1towindows-1252, silently remapping bytes 0x80–0x9F to non-latin1 code points. Any transport using it to decode binary tmux output frames would corrupt high-byte content. ThecharCodeAt/fromCharCodeapproach is the only portable path that is literally 1:1.Architectural laws
[LAW:single-enforcer]— one site for the byte-container contract, none re-derive it[LAW:one-source-of-truth]— the 1:1 byte↔code-unit invariant is stated once in the module header[LAW:behavior-not-structure]— tests assert the observable contract (round-trip, 0x80-0x9F fidelity, chunk boundary correctness), not implementation internalsTest plan
pnpm run test:all— 565 tests pass (30 test files)tests/unit/byte-codec.test.ts— 11/11 passpnpm run build— clean typecheckNext
tmux-byte-codec-xxj.2will route all transports (websocket transport, websocket server) through these functions, replacing thenew TextDecoder()calls that are currently wrong.