You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/network/ts-p2p/CHANGELOG.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ All notable changes to this project will be documented in this file. The format
10
10
## [Unreleased]
11
11
12
12
### Added
13
-
-(Include new features or significant user-visible enhancements here.)
13
+
-Optional typed decoder for the two-layer JSON wire format. New `decodeMessage()` / `tryDecodeMessage()` helpers and topic payload interfaces (`MessageEnvelope`, `BlockMessage`, `SubtreeMessage`, `RejectedTxMessage`, `NodeStatusMessage`, `FeePolicy`). Set `decodeMessages: true` on the listener to receive a typed `DecodedMessage` instead of raw `Uint8Array`. Backward compatible (defaults to off).
14
14
15
15
### Changed
16
16
- (Detail modifications that are non-breaking but relevant to the end-users.)
Copy file name to clipboardExpand all lines: packages/network/ts-p2p/README.md
+37-2Lines changed: 37 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -70,6 +70,27 @@ await listener.start();
70
70
console.log('Listener started and waiting for messages...');
71
71
```
72
72
73
+
### Decoding messages
74
+
75
+
By default, callbacks receive the raw GossipSub bytes (`Uint8Array`). Pass `decodeMessages: true` to have the listener decode the two-layer JSON wire format for you. Callbacks then receive a typed `DecodedMessage` (the sender name plus a typed payload):
console.log(`Block #${msg.payload.Height} (${msg.payload.Hash}) from ${msg.sender}`);
84
+
}
85
+
},
86
+
{ decodeMessages: true }
87
+
);
88
+
89
+
awaitlistener.start();
90
+
```
91
+
92
+
The exported `decodeMessage()` / `tryDecodeMessage()` helpers can also be used to decode a message manually. Frames that are not valid JSON (e.g. libp2p control frames) are skipped when `decodeMessages` is on.
93
+
73
94
### Function-Based API
74
95
75
96
Alternatively, you can use the original function-based API:
@@ -400,16 +421,30 @@ npm install
400
421
npm run build
401
422
```
402
423
424
+
### Testing
425
+
426
+
This package uses [Jest](https://jestjs.io/) with `ts-jest`. Run the suite with:
427
+
428
+
```bash
429
+
npm test
430
+
```
431
+
432
+
The tests exercise the message decoder (`decodeMessage` / `tryDecodeMessage`) end to end, building real two-layer wire frames and decoding them back: the PascalCase (block) and snake_case (node_status) payload shapes, multi-byte UTF-8, every base64 padding length, and the malformed / non-JSON frame paths.
0 commit comments