# The compression engine

`compression.js` is small on purpose. It chooses between three things and never
re-implements a codec.

## 1. The platform's Compression Streams

`CompressionStream('gzip' | 'deflate' | 'deflate-raw')` is native code. It is fast, it is
maintained by the browser vendor, and its output is the same bytes every other app in the
fleet produces, because [export-kit](https://github.com/drsamonline/export-kit) uses the
same API.

It has one gap: **it takes no level.** There is no way to ask for level 9.

## 2. fflate, vendored

`vendor/fflate.min.js` covers what Compression Streams cannot:

- **a level**, 0–9, for gzip, zlib and ZIP entries;
- **ZIP writing** with per-entry options;
- **ZIP reading** — which is what makes extraction and Office repacking possible.

So an explicit level routes through fflate, and the default uses the native path. Both
produce a standards-compliant stream; either decompresses the other.

## 3. Store, as the floor

Some inputs cannot be compressed, because they already are. Deflating a JPEG costs time
and returns the input plus a few bytes. So `zip()` stores them at method 0 — and for
anything at or under 4 MiB it checks rather than assumes (see *The store rule, measured*
below). The list:

```
zip gz tgz bz2 xz zst br lz lzma lz4 7z rar
jpg jpeg png webp avif heic heif jxl gif
mp3 mp4 m4a aac ogg opus webm flac wma mkv avi mov
dr jar war apk aab docx xlsx pptx odt ods odp epub
```

PDF is deliberately **not** on that list: a PDF is usually compressible, and a PDF that
is not simply comes out about the same size. The app reports the number either way.

The `storedNames` array on a built archive records which entries were stored, so a test
can assert the rule rather than trust it.

## Where it runs

`worker.js` runs this module in a worker thread and `engine.js` talks to it: one job at a
time, progress per entry, and a cancel that rejects instead of resolving. The archive a
worker produces is byte-identical to the one built on the main thread — asserted, not
assumed — because both call the same functions with the same arguments.

If a worker cannot be created (`file://` origins refuse), `Engine.available()` is false and
the app runs the same code on the main thread. The page still works; it just stops painting
while a large archive is built.

## The store rule, measured

`PRECOMPRESSED` used to be a list of extensions reasoned to be "already compressed". It was
wrong in both directions: a `.woff2` was being deflated (it gains 0.1%), and a PNG was being
stored (the two in this repository deflate by 2.1% and 8.5%, because a PNG's own deflate is
per scanline, not over the whole file).

So the rule is no longer a guess for anything small enough to check. For an entry on the list
at or under **4 MiB**, `storeDecision()` computes both outcomes and keeps the smaller one,
with a 20-byte margin for the header a deflated entry costs. Above that the list stands,
because deflating 200 MB twice to save a percent is not a trade a tab can make.

`node scripts/measure-store-rule.js` reports the decision the code makes, per file, against
level-9 deflate:


| ext | files | mean saving | stored |
|---|---|---|---|
| css | 2 | 70.8% | 0/2 |
| dr | 3 | -0.1% | 3/3 |
| html | 2 | 62.0% | 0/2 |
| js | 14 | 63.9% | 0/14 |
| json | 1 | 78.7% | 0/1 |
| md | 12 | 53.3% | 0/12 |
| png | 2 | 5.3% | 0/2 |
| py | 2 | 58.6% | 0/2 |
| sh | 1 | 50.1% | 0/1 |
| woff2 | 2 | 0.1% | 1/2 |
| yml | 1 | 61.1% | 0/1 |

42 files measured, 0 where deflating was chosen and made the file bigger

every entry the rule chose to deflate actually got smaller

The corpus is what is in this repository — a biased sample, and a checkable one. It is why
`woff2` and `woff` are on the list now, and why PNG is not trusted either way.

## What the engine does not do

- It does not choose a level by looking at the file. Fast, Balanced and Maximum are the
  user's call, and the app says which is which.
- It does not implement DEFLATE, gzip, or zlib. Those are the platform's and fflate's.
- It does not call ziptastic. `dr-inspect.js` reads the format; the engine does not write
  it, and nothing here pretends a `.dr` came out of a browser.

## Repacking a package

A `.docx` is a ZIP. Repacking it entry by entry at Maximum sometimes wins and sometimes
loses — the images inside are already compressed. `repack()` unpacks, re-zips, and the
caller compares. If the result is not smaller, the original bytes are handed back with a
note saying so, which is the only honest outcome.
