Skip to content

probeDecompressedSize treats custom codecs as uncompressed, returning the compressed size #5

Closed

Description

@xinaesthete

Summary

probeDecompressedSize (src/get-worker.ts:301) decides whether a chunk is compressed by matching codec names against a fixed list of eight compressors. Any codec outside that list is treated as uncompressed, so the function returns rawBytes.byteLength — the compressed size — as the decompressed size.

For image codecs such as imagecodecs_jpeg2k (and HTJ2K variants) this is wrong by the whole compression ratio, and the bad number is then fed to inferChunkShape.

Where

src/get-worker.ts:318:

const hasCompression = codecMeta.codecs.some((c) => {
  const name = c.name.toLowerCase()
  return (
    name === "gzip" || name === "zlib" || name === "blosc" ||
    name === "zstd" || name === "lz4"  || name === "bz2"   ||
    name === "lzma" || name === "snappy"
  )
})
if (!hasCompression) {
  // No compression codec — raw bytes are the decompressed data
  return rawBytes.byteLength   // :336
}

Codec ids that miss the list and are affected in practice.

Why it matters

The function has a correct step-4 fallback immediately below — a full decode through the codec pipeline, which would handle any codec. It is unreachable for an unrecognised codec, because the step-3 check short-circuits first.

Downstream in probeActualChunkShape (:574), the bogus size becomes actualElements = compressedBytes / bytesPerElement, which will not equal metaElements, so every read takes the mismatch branch:

up to 5 extra store.get round-trips validating inferred candidates (:620-634), and
a console.warn on every array, and
if no candidate validates, probeActualChunkShape returns candidates[0] unvalidated (:637-643) rather than the metadata shape — so a bogus chunk shape can be adopted, not merely warned about.
It is often harmless in practice (a compressed length rarely factors into plausible candidates, and an empty candidate list does fall back to metadata at :611), but the failure is silent when it isn't.

Suggested fix

Invert the check: enumerate the codecs known not to change byte size (transpose and other array_to_array codecs, bytes/endian as array_to_bytes) and treat everything else as potentially compressed, letting it fall through to the existing step-4 full decode. That makes the fallback do the job it was written for and removes the need to keep an allowlist current as new image codecs appear.

Activity

  1. thewtex commented on Aug 13, 2026

    @thewtex
    Member

    Fixed by #9 (merged 2026-08-06, commits 44f7474 + 8429394).

    probeDecompressedSize no longer matches codec names against a compressor allowlist. The check is inverted as suggested here: hasSizeChangingCodec allowlists the codecs known to preserve byte count (bytes, transpose, with the numcodecs. prefix stripped) and treats everything else — including imagecodecs_jpeg2k, HTJ2K variants, crc32c, sharding_indexed, etc. — as size-changing, so unrecognised codecs now fall through to the step-4 full-decode fallback instead of reporting the compressed length as the decompressed size.

    Covered by the hasSizeChangingCodec suite in test/browser/chunk-shape-inference.spec.ts, including a JPEG 2000 / HTJ2K regression test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions