3737MAX_SYNC_CHUNK_SIZE = 4096
3838DEFAULT_MAX_DECOMPRESS_SIZE = 2 ** 20 # 1MiB
3939
40+ # Unlimited decompression constants - different libraries use different conventions
41+ ZLIB_MAX_LENGTH_UNLIMITED = 0 # zlib uses 0 to mean unlimited
42+ ZSTD_MAX_LENGTH_UNLIMITED = - 1 # zstd uses -1 to mean unlimited
43+
4044
4145class ZLibCompressObjProtocol (Protocol ):
4246 def compress (self , data : Buffer ) -> bytes : ...
@@ -158,10 +162,14 @@ def __init__(
158162 self ._max_sync_chunk_size = max_sync_chunk_size
159163
160164 @abstractmethod
161- def decompress_sync (self , data : bytes , max_length : int = 0 ) -> bytes :
165+ def decompress_sync (
166+ self , data : bytes , max_length : int = ZLIB_MAX_LENGTH_UNLIMITED
167+ ) -> bytes :
162168 """Decompress the given data."""
163169
164- async def decompress (self , data : bytes , max_length : int = 0 ) -> bytes :
170+ async def decompress (
171+ self , data : bytes , max_length : int = ZLIB_MAX_LENGTH_UNLIMITED
172+ ) -> bytes :
165173 """Decompress the given data."""
166174 if (
167175 self ._max_sync_chunk_size is not None
@@ -261,7 +269,9 @@ def __init__(
261269 self ._zlib_backend : Final = ZLibBackendWrapper (ZLibBackend ._zlib_backend )
262270 self ._decompressor = self ._zlib_backend .decompressobj (wbits = self ._mode )
263271
264- def decompress_sync (self , data : Buffer , max_length : int = 0 ) -> bytes :
272+ def decompress_sync (
273+ self , data : Buffer , max_length : int = ZLIB_MAX_LENGTH_UNLIMITED
274+ ) -> bytes :
265275 return self ._decompressor .decompress (data , max_length )
266276
267277 def flush (self , length : int = 0 ) -> bytes :
@@ -294,7 +304,9 @@ def __init__(
294304 self ._obj = brotli .Decompressor ()
295305 super ().__init__ (executor = executor , max_sync_chunk_size = max_sync_chunk_size )
296306
297- def decompress_sync (self , data : Buffer , max_length : int = 0 ) -> bytes :
307+ def decompress_sync (
308+ self , data : Buffer , max_length : int = ZLIB_MAX_LENGTH_UNLIMITED
309+ ) -> bytes :
298310 """Decompress the given data."""
299311 if hasattr (self ._obj , "decompress" ):
300312 return cast (bytes , self ._obj .decompress (data , max_length ))
@@ -321,8 +333,17 @@ def __init__(
321333 self ._obj = ZstdDecompressor ()
322334 super ().__init__ (executor = executor , max_sync_chunk_size = max_sync_chunk_size )
323335
324- def decompress_sync (self , data : bytes , max_length : int = 0 ) -> bytes :
325- return self ._obj .decompress (data , max_length )
336+ def decompress_sync (
337+ self , data : bytes , max_length : int = ZLIB_MAX_LENGTH_UNLIMITED
338+ ) -> bytes :
339+ # zstd uses -1 for unlimited, while zlib uses 0 for unlimited
340+ # Convert the zlib convention (0=unlimited) to zstd convention (-1=unlimited)
341+ zstd_max_length = (
342+ ZSTD_MAX_LENGTH_UNLIMITED
343+ if max_length == ZLIB_MAX_LENGTH_UNLIMITED
344+ else max_length
345+ )
346+ return self ._obj .decompress (data , zstd_max_length )
326347
327348 def flush (self ) -> bytes :
328349 return b""
0 commit comments