-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #272 from happo/asset-size
Validate assets before uploading them
- Loading branch information
Showing
4 changed files
with
88 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
export default function validateArchive(totalBytes, entries) { | ||
const totalMegaBytes = Math.round(totalBytes / 1024 / 1024); | ||
if (totalMegaBytes < 30) { | ||
return; | ||
} | ||
const messageBits = [ | ||
`Package size is ${totalMegaBytes} MB (${totalBytes} bytes), maximum is 60 MB.`, | ||
"Here are the largest 20 files in the archive. Consider removing ones that aren't necessary.", | ||
]; | ||
const fileSizes = entries.map((entry) => ({ | ||
name: entry.name, | ||
size: entry.stats ? entry.stats.size : (entry.size || 0), | ||
})); | ||
fileSizes | ||
.sort((a, b) => b.size - a.size) | ||
.slice(0, 20) | ||
.forEach((file) => { | ||
messageBits.push( | ||
`${file.name}: ${Math.round(file.size / 1024 / 1024)} MB (${ | ||
file.size | ||
} bytes)`, | ||
); | ||
}); | ||
|
||
if (totalMegaBytes > 60) { | ||
throw new Error(messageBits.join('\n')); | ||
} | ||
console.warn(messageBits.join('\n')); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import validateArchive from '../src/validateArchive'; | ||
|
||
describe('validateArchive', () => { | ||
let totalBytes; | ||
let entries; | ||
let subject; | ||
|
||
beforeEach(() => { | ||
totalBytes = 100; | ||
entries = []; | ||
subject = () => validateArchive(totalBytes, entries); | ||
}); | ||
|
||
it('does not throw when totalBytes is lower than 30 MB', () => { | ||
expect(subject()).toBe(undefined); | ||
}); | ||
|
||
describe('when the size is larger than 60 MB', () => { | ||
beforeEach(() => { | ||
totalBytes = 73 * 1024 * 1024 + 2346; | ||
entries = [ | ||
{ name: 'rar.png', stats: { size: 95000000 } }, // inside stats object | ||
{ name: 'dar.png', stats: { size: 78000000 } }, | ||
{ name: 'foo.png', size: 98000000 }, // outside stats object | ||
{ name: 'bar.png', stats: { size: 8000000 } }, | ||
{ name: 'scar.png', size: 88000000 }, | ||
{ name: 'car.png' }, // no size | ||
]; | ||
}); | ||
|
||
it('throws an error with a list of sorted files by size', () => { | ||
expect(subject).toThrow( | ||
/Package size is 73 MB.*maximum is 60 MB.*foo.png: 93 MB.*rar.png: 91 MB.*scar.png: 84 MB/s, | ||
); | ||
}); | ||
}); | ||
}); |