---
title: "Limits and partial reads"
description: "An output limit so a bomb stays small and partial reads for streams that break halfway"
canonical_url: "https://compressions.agntn.dev/guide/limits"
---
# Limits and partial reads

> An output limit so a bomb stays small and partial reads for streams that break halfway

## A limit on the way out

A megabyte of zeros gzips to about a thousand bytes. That ratio is the whole point of compression, and also the whole point of a zip bomb. So `decompress` stops at 256 MiB by default, and you can set it lower:

```ts
decompress("deflate", bomb, { container: "gzip", limit: 100_000 });
// LimitError: deflate: output passes the limit of 100000 bytes
```

Output fills right up to the limit and stops there. A match that runs across the line gets cut at the line, so that error carries exactly the first 100,000 bytes. Only want the start of something huge? Pass `partial: true` and take that prefix as the answer. A size the stream claims up front only sizes the first buffer, and never past the limit or 64 MiB. A liar gets a small buffer and the same error.

`identify` and `peel` take the same `limit`, applied to every attempt. A stream that hits it stays on the list, marked `limited`. [Identify](https://compressions.agntn.dev/guide/identify) shows how.

## The tools are stricter

<table>
<thead>
  <tr>
    <th>
      Bound
    </th>
    
    <th>
      Value
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      Output of one stream
    </td>
    
    <td>
      64 MiB
    </td>
  </tr>
  
  <tr>
    <td>
      Decompressed bytes shown in one answer
    </td>
    
    <td>
      16 KiB by default, up to 1 MiB with <code>
        length
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      Input to one call
    </td>
    
    <td>
      4,000,000 characters of base64, hex or text
    </td>
  </tr>
  
  <tr>
    <td>
      Layers <code>
        peel
      </code>
      
       takes off
    </td>
    
    <td>
      10
    </td>
  </tr>
</tbody>
</table>

Long output comes in windows. The header says `showing 0 to 16384; pass offset for the rest`, and the model asks for the next window with `offset`.

## Partial reads

A stream cut in half still holds half the data. Ask for it:

```ts
decompress("deflate", cut, { container: "gzip", partial: true });
// { bytes: …everything before the cut…,
//   details: { error: "deflate: gzip member ends before its CRC-32 and size" } }
```

Without `partial`, the same bytes ride on the error:

```ts
try {
  decompress("deflate", cut, { container: "gzip" });
} catch (error) {
  if (error instanceof DecompressError) error.partial;  // the same bytes
}
```

Every format writes as it goes, so what comes out before the damage is real output, not a guess. A wrong checksum works the same way: the bytes are all there, the check just failed. LZMA2 decodes a cut chunk as far as it gets. Inflate never writes a block end it didn't actually read.

## Errors

<table>
<thead>
  <tr>
    <th>
      Error
    </th>
    
    <th>
      When
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        DecompressError
      </code>
    </td>
    
    <td>
      The stream is broken. Carries <code>
        format
      </code>
      
      , <code>
        offset
      </code>
      
       and <code>
        partial
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        ChecksumError
      </code>
    </td>
    
    <td>
      A checksum or a stored size doesn't match. A <code>
        DecompressError
      </code>
      
       too
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        LimitError
      </code>
    </td>
    
    <td>
      The output passes <code>
        limit
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        UnsupportedError
      </code>
    </td>
    
    <td>
      The stream uses something the format allows and this package doesn't do, such as a zstd dictionary
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        UnknownFormatError
      </code>
    </td>
    
    <td>
      No format by that name. gzip, xz and friends get pointed to their family
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        InvalidOptionError
      </code>
    </td>
    
    <td>
      An option out of range or for another container
    </td>
  </tr>
</tbody>
</table>

All of them extend `CompressionError`, so one `catch` takes them all. Each [format subpath](https://compressions.agntn.dev/guide/compressing#format-objects-and-subpaths) exports them too, all but `UnknownFormatError`.


## Sitemap

See the full [sitemap](https://compressions.agntn.dev/sitemap.md) for all pages.
