feat(ts-p2p): add typed decoder for the two-layer JSON wire format#251
Open
zcoolz wants to merge 3 commits into
Open
feat(ts-p2p): add typed decoder for the two-layer JSON wire format#251zcoolz wants to merge 3 commits into
zcoolz wants to merge 3 commits into
Conversation
Adds optional, typed decoding of the two-layer JSON wire format Teranode broadcasts over GossipSub. New decodeMessage()/tryDecodeMessage() helpers and topic payload interfaces (BlockMessage, SubtreeMessage, RejectedTxMessage, NodeStatusMessage, FeePolicy). Opt in via decodeMessages: true; callbacks then receive a typed DecodedMessage instead of raw bytes. Backward compatible. Payload interfaces verified against teranode/services/p2p/message_types.go. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Resolves the SonarCloud S8786 super-linear-regex finding on messages.ts. Replaces the trailing-padding regex with a plain trailing-'=' scan: provably linear and behavior-identical. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Wire up the package's first test harness (ESM ts-jest, mirroring the overlays/topics config) and cover decodeMessage/tryDecodeMessage: PascalCase block and snake_case node_status payloads, nested fee_policy, multi-byte UTF-8, all base64 padding lengths, malformed input, and the null-on-bad-frame paths. Adds the test/test:coverage scripts plus jest, ts-jest, @jest/globals and @types/jest dev deps, a README testing section and project-structure update, and the ts-p2p importer entries in the lockfile. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
ty-everett
approved these changes
Jun 30, 2026
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.



What
Adds an optional, typed decoder for the two-layer JSON wire format Teranode broadcasts over GossipSub, plus typed interfaces for each topic payload.
Today
TeranodeListenercallbacks receive rawUint8Array, so every consumer has to hand-decode the envelope and inner payload. This adds:src/messages.ts: typed interfaces (MessageEnvelope,BlockMessage,SubtreeMessage,RejectedTxMessage,NodeStatusMessage,FeePolicy) and two helpers,decodeMessage()andtryDecodeMessage(). No new dependencies (base64 is implemented inline, so it runs in any JS runtime).decodeMessages?: booleanflag. When set, callbacks receive a typedDecodedMessage { sender, payload }instead of raw bytes; frames that are not valid JSON (e.g. libp2p control frames) are skipped.Why
The format is two layers: a message-bus envelope
{ name, data: base64 }wrapping a topic-specific JSON payload. Decoding it correctly, including the PascalCase vs snake_case difference between block/subtree/rejected-tx and node_status, is non-obvious. Shipping the decoder saves every downstream user from reimplementing it.Compatibility
Fully backward compatible.
decodeMessagesdefaults tofalse, so existing callbacks keep receivingUint8Arrayunchanged.Verification
Payload interfaces verified field-for-field against the current
teranode/services/p2p/message_types.go(including the recently addedfee_policy). The added and changed files compile cleanly under the package'stscconfig. README and CHANGELOG updated.Closes #250