Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/__tests__/formatPkg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ it('truncates long readmes', () => {
readme: 'Hello, World! '.repeat(40000),
};
const formatted = formatPkg(object);
const truncatedEnding = '**TRUNCATED**';
const postfix = ' **TRUNCATED**';
const ending = formatted.readme.substr(
formatted.readme.length - truncatedEnding.length
formatted.readme.length - postfix.length
);

const readmeLength = formatted.readme.length;

expect(readmeLength).toBeLessThan(475000);
expect(ending).toBe(truncatedEnding);
expect(ending).toBe(postfix);

expect(formatted).toMatchSnapshot({
readme: expect.any(String),
Expand Down
75 changes: 67 additions & 8 deletions src/formatPkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,75 @@ export default function formatPkg(pkg) {
},
};

const totalSize = sizeof(rawPkg);
if (totalSize > config.maxObjSize) {
const sizeDiff = sizeof(rawPkg.readme) - totalSize;
rawPkg.readme = `${truncate(
rawPkg.readme,
config.maxObjSize - sizeDiff
)} **TRUNCATED**`;
const truncated = truncatePackage(rawPkg);

return traverse(truncated).forEach(maybeEscape);
}

function checkSize(pkg) {
const size = sizeof(pkg);
const diff = size - config.maxObjSize;

return {
size,
diff,
isTooBig: diff > 0,
};
}

function truncatePackage(pkg) {
let smallerPkg = { ...pkg };

{
const { diff, isTooBig } = checkSize(smallerPkg);
if (isTooBig) {
const postfix = ' **TRUNCATED**';
// sizeof is * 2 what truncate expects
const maxReadmeLength = (sizeof(pkg.readme) - diff - sizeof(postfix)) / 2;

smallerPkg.readme = truncate(pkg.readme, maxReadmeLength) + postfix;
}
}

{
const { isTooBig } = checkSize(smallerPkg);
if (isTooBig) {
smallerPkg.readme =
'** TRUNCATED ** this package was too big, so non-essential information was removed';
smallerPkg.versions =
pkg.versions && pkg.version
? {
[pkg.version]: pkg.versions[pkg.version],
}
: null;
smallerPkg.tags =
pkg.tags && pkg.tags.latest
? {
latest: pkg.tags.latest,
}
: null;
smallerPkg.owners = [smallerPkg.owner];
}
}

{
const { isTooBig } = checkSize(smallerPkg);
if (isTooBig) {
smallerPkg = {
name: smallerPkg.name,
readme: smallerPkg.readme,
};
}
}

{
const { isTooBig } = checkSize(smallerPkg);
if (isTooBig) {
return null;
}
}

return traverse(rawPkg).forEach(maybeEscape);
return smallerPkg;
}

function maybeEscape(node) {
Expand Down