MCPcopy Create free account
hub / github.com/ChainSafe/nodewatch-api / EncodeHeaderAndPayload

Function EncodeHeaderAndPayload

crawler/rpc/request/encode.go:49–69  ·  view source on GitHub ↗

EncodeHeaderAndPayload reads a payload, buffers (and optionally compresses) the payload, then computes the header-data (varint of byte size). And then writes header and payload.

(r io.Reader, w io.Writer, comp Compression)

Source from the content-addressed store, hash-verified

47// EncodeHeaderAndPayload reads a payload, buffers (and optionally compresses) the payload,
48// then computes the header-data (varint of byte size). And then writes header and payload.
49func EncodeHeaderAndPayload(r io.Reader, w io.Writer, comp Compression) error {
50 var buf payloadBuffer
51 if _, err := io.Copy(&buf, r); err != nil {
52 return err
53 }
54 if err := buf.OutputSizeVarint(w); err != nil {
55 return err
56 }
57 if comp != nil {
58 compressedWriter := comp.Compress(&noCloseWriter{w: w})
59 defer func() {
60 _ = compressedWriter.Close()
61 }()
62 if _, err := buf.WriteTo(compressedWriter); err != nil {
63 return err
64 }
65 } else if _, err := buf.WriteTo(w); err != nil {
66 return err
67 }
68 return nil
69}
70
71// StreamHeaderAndPayload reads a payload and streams (and optionally compresses) it to the writer.
72// To do so, it requires the (uncompressed) payload length to be known in advance.

Callers 4

RequestMethod · 0.85
EncodeChunkFunction · 0.85

Calls 4

OutputSizeVarintMethod · 0.95
WriteToMethod · 0.95
CompressMethod · 0.65
CloseMethod · 0.45

Tested by 2