"""Bounded, negotiated compression for the existing full Node heartbeat.""" import zlib MAX_HEARTBEAT_BYTES = 8 * 1024 * 1024 def decode_heartbeat(body: bytes, encoding: str, path: str) -> bytes: if not encoding: return body if encoding != "gzip" or path != "/v1/node/heartbeat": raise ValueError("Unsupported encoding") try: stream = zlib.decompressobj(16 + zlib.MAX_WBITS) decoded = stream.decompress(body, MAX_HEARTBEAT_BYTES + 1) if (len(decoded) > MAX_HEARTBEAT_BYTES or not stream.eof or stream.unused_data or stream.unconsumed_tail): raise ValueError("Invalid compressed heartbeat") return decoded except zlib.error as error: raise ValueError("Invalid compressed heartbeat") from error