An inventory can answer a narrow question before anyone tries to optimize a document: how many compressed entry-payload bytes were reported, and how many belong to rows the caller marked as candidates for rewriting? That’s useful accounting, and it isn’t a prediction of the output file size.
The function below takes metadata that is already normalized. It doesn’t open an Office document or decide whether an entry is an image. The caller supplies rewriteCandidate; a true value classifies the row for this calculation and doesn’t prove that the entry can actually be replaced or made smaller.
export function summarizePayloads(entries) {
if (!Array.isArray(entries) || entries.length > 1000) {
throw new TypeError('Expected at most 1000 metadata rows');
}
const seen = new Set();
let payloadBytes = 0;
let candidateBytes = 0;
for (const row of entries) {
if (!row || typeof row.path !== 'string' || row.path.length === 0 ||
row.path.length > 200 || row.path.includes('\0') || seen.has(row.path) ||
!Number.isSafeInteger(row.compressedBytes) || row.compressedBytes < 0 ||
typeof row.rewriteCandidate !== 'boolean') {
throw new TypeError('Invalid or duplicate metadata row');
}
seen.add(row.path);
if (row.compressedBytes > Number.MAX_SAFE_INTEGER - payloadBytes) {
throw new RangeError('Payload total exceeds safe integer range');
}
payloadBytes += row.compressedBytes;
if (row.rewriteCandidate) candidateBytes += row.compressedBytes;
}
return { entryCount: entries.length, payloadBytes, candidateBytes,
otherBytes: payloadBytes - candidateBytes };
}
The synthetic fixture below has three rows. To try it as one module, append this second block after the function in an .mjs file; the function is then already in scope. The duplicate assertion is there so that counting the same key twice can’t silently inflate a total.
import assert from 'node:assert/strict';
const fixture = [
{ path: 'ppt/presentation.xml', compressedBytes: 400, rewriteCandidate: false },
{ path: 'ppt/media/image1.jpeg', compressedBytes: 1200, rewriteCandidate: true },
{ path: 'ppt/media/video1.mp4', compressedBytes: 8400, rewriteCandidate: false },
];
const expected = { entryCount: 3, payloadBytes: 10000, candidateBytes: 1200, otherBytes: 8800 };
assert.deepEqual(summarizePayloads(fixture), expected);
assert.deepEqual(summarizePayloads([]), { entryCount: 0, payloadBytes: 0, candidateBytes: 0, otherBytes: 0 });
assert.equal(summarizePayloads([{ path: 'a', compressedBytes: 0, rewriteCandidate: true }]).candidateBytes, 0);
assert.equal(summarizePayloads([{ path: 'a', compressedBytes: Number.MAX_SAFE_INTEGER, rewriteCandidate: true }]).candidateBytes, Number.MAX_SAFE_INTEGER);
assert.throws(() => summarizePayloads([fixture[0], fixture[0]]), TypeError);
The result splits 10,000 reported payload bytes into 1,200 candidate bytes and 8,800 other bytes. The extension in a path plays no part in the classification; change the supplied boolean and the accounting changes. A candidate may still be unsuitable for rewriting, and a rewrite may fail to save bytes.
The checks make exact integer arithmetic a precondition. Each byte count must be a nonnegative safe integer, and the function rejects a row if adding it would push the total beyond Number.MAX_SAFE_INTEGER. Because candidate rows are a nonnegative subset of the total, a safe total also bounds the candidate sum. Paths are compared as exact strings to find duplicates. Empty paths, NUL characters, non-boolean flags, and more than 1,000 rows are rejected. The 1,000-row and 200-character limits are illustrative choices for this example; they are not Office format or product limits.
An empty array returns zero totals. It is valid input to this metadata function, but it is not a valid Office package. Paths are opaque accounting keys, not validated safe filenames or extraction destinations.
Compressed entry payloads omit ZIP structures and other archive overhead. The candidate total is neither a forecast of the next ZIP’s size nor a strict achievable lower bound. Parsing archives, validating untrusted files or relationships, performing file I/O, rewriting content, and predicting output are outside this function’s scope.
Disclosure: This manuscript was substantially drafted by GPT-6.1 Terra for Yan Wang / LensUp, with agent execution and fact review; Yan Wang is affiliated with LensUp. Source and live-page text were inspected on 2026-10-30. On 2026-10-30, a separately retained full metadata-test harness, code/checks.mjs, ran on Node 22.23.4 on Darwin: 4 valid assertions, 16 rejected cases, and 2 negative controls passed. The two code blocks shown in this article contain only 4 valid assertions and 1 rejection assertion. It did not test real Office optimization or archive, relationship, or renderer acceptance. The text was polished by an independent editorial AI and fact-checked by Codex, with an independent code review. No human editor or owner reviewed or tested it.
Top comments (0)