tapCodecBytes
Observes the raw bytes of a codec on both sides, without modifying them.
This function takes an existing codec and returns a new codec of the exact same type that:
- Calls
encodeTapwith the byte array after each value is written. - Calls
decodeTap(if provided) with the byte array before each value is read.
The bytes are provided as ReadonlyUint8Arrays and are passed through unchanged, making this useful for adding validation guards, logging, or other read-only side effects. If either tap function throws, the corresponding operation is aborted and the error propagates to the caller.
Type Parameters
| Type Parameter | Description |
|---|---|
TCodec extends AnyCodec | The type of the codec being tapped. The returned codec has the same type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
codec | TCodec | The codec whose raw bytes should be observed. |
encodeTap | (bytes, preOffset, postOffset) => void | A function called with the byte array and offsets after writing. It may throw to abort encoding. |
decodeTap? | (bytes, offset) => void | An optional function called with the byte array and offset before reading. It may throw to abort decoding. |
Returns
TCodec
A new codec of the same type that observes its raw bytes.
Throws
Whatever error the tap functions throw, aborting the corresponding operation.
Example
Guarding the raw bytes of a boolean codec before decoding.
Remarks
If only the encoded bytes need observing, use tapEncoderBytes. If only the raw bytes before decoding need observing, use tapDecoderBytes. To observe the values instead of the raw bytes, use tapCodec.