mozilla/pdf.js · error · Error

Unknown type: ${type}

Error message

Unknown type: ${type}

What it means

compress.mjs decodes the binary `.bcmap` format. Each record's high 3 bits (`b >> 5`) form a `type` field; only types 0-5 (range/char mappings) and type 7 (comment/usecmap metadata) are defined by the Adobe CMap binary spec. Hitting the `default` branch means the decoded type is 6 or 8-31, which the format does not define — almost always a sign that the input byte stream is misaligned or the file is not a real bcmap. The reader then cannot continue safely, so it aborts.

Source

Thrown at external/cmapscompress/compress.mjs:350

          subitems.push({ char, code });
        }
        break;
      case 5:
        start = reader.readHex(ucs2DataSize);
        end = addHex(reader.readHexNumber(ucs2DataSize), start);
        code = reader.readHex(dataSize);
        subitems.push({ start, end, code });
        for (let i = 1; i < subitemsCount; i++) {
          start = sequence
            ? incHex(end)
            : addHex(reader.readHexNumber(ucs2DataSize), incHex(end));
          end = addHex(reader.readHexNumber(ucs2DataSize), start);
          code = reader.readHex(dataSize);
          subitems.push({ start, end, code });
        }
        break;
      default:
        throw new Error("Unknown type: " + type);
    }
    result.body.push(item);
  }

  return result;
}

function toHexDigit(n) {
  return n.toString(16);
}
function fromHexDigit(s) {
  return parseInt(s, 16);
}
function getHexSize(s) {
  return (s.length >> 1) - 1;
}
function writeByte(b) {
  return toHexDigit((b >> 4) & 15) + toHexDigit(b & 15);

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Regenerate the bcmap from source: run `gulp cmaps` so text CMaps in external/cmaps are recompressed fresh.
  2. Verify the input is actually a binary bcmap (starts with the header byte, no UTF-8 BOM, not gzipped) — `file external/bcmaps/<name>.bcmap` and a hexdump of the first bytes.
  3. Re-fetch the file from a trusted source (git checkout / redownload) to rule out transfer corruption.
  4. If you maintain the compressor, audit the byte-writing code: a type value of 6 indicates the high bits were written incorrectly.
Defensive patterns

Strategy: try-catch

Validate before calling

// Cheap header sanity check before invoking the bcmap reader.
function looksLikeBcmap(buf) {
  return buf && buf.length > 0 && (buf[0] & 0xfe) >>> 1 <= 1; // header type field sanity
}

Type guard

/** @param {unknown} f */
function isBcmapFile(f) {
  return typeof f === 'string' && f.toLowerCase().endsWith('.bcmap');
}

Try / catch

try {
  result = readBcmap(buf);
} catch (e) {
  if (/Unknown type/.test(e.message)) {
    // regenerate from source rather than trusting the corrupt artifact
    await runGulp('cmaps');
    result = readBcmap(fs.readFileSync(path));
  } else {
    throw e;
  }
}

Prevention

When it happens

Trigger: Feeding a non-bcmap file (e.g. a plain-text CMap, a gzipped blob, a truncated file) into the bcmap reader; bit-level corruption from a bad git checkout, transfer, or partial write; an endianness/encoding mismatch; reading past EOF so `readByte` returns garbage rather than -1.

Common situations: Running `gulp cmaps` after `external/cmaps` was populated with text CMap sources but the binary reader is invoked on the wrong artifact; a corrupted `external/bcmaps` shipped in a dist build; a fork that hand-edited bcmap files.

Related errors


AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13). Data as JSON: /api/errors/00fe0770ad1bbdb0. Report an issue: GitHub.