Skip to content

Commit

Permalink
fix: do not try to slice in chunk larger than the File itself
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ <[email protected]>
  • Loading branch information
skjnldsv committed Jan 24, 2024
1 parent b4b1120 commit 3e40a97
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
14 changes: 12 additions & 2 deletions __tests__/utils/upload.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,19 @@ describe('Get chunk from file', () => {
expect(chunk.size).toBe(10 * 1024 * 1024)
})

test('Chunking an invalid file', async () => {
test('Requesting a chunk bigger than the file', async () => {
const blob = new Blob([new ArrayBuffer(5 * 1024 * 1024)])
const file = new File([blob as BlobPart], 'image.jpg')
const file = new File([blob as BlobPart], 'image.jpg', { type: 'image/jpeg' })

const chunk = await getChunk(file, 0, 10 * 1024 * 1024)
expect(chunk.size).toBe(5 * 1024 * 1024)
expect(chunk.type).toBe('image/jpeg')

})

test('Chunking an unknown file', async () => {
const blob = new Blob([new ArrayBuffer(5 * 1024 * 1024)])
const file = new File([blob as BlobPart], 'image.jpg', { })

const chunk = await getChunk(file, 0, 10 * 1024 * 1024)
expect(chunk.size).toBe(5 * 1024 * 1024)
Expand Down
4 changes: 4 additions & 0 deletions lib/utils/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ export const uploadData = async function(
* garbage collection
*/
export const getChunk = function(file: File, start: number, length: number): Promise<Blob> {
if (file.size <= start + length) {
return Promise.resolve(new Blob([file], { type: file.type || 'application/octet-stream'}))
}

// Since we use a global FileReader, we need to only read one chunk at a time
return readerLimit(() => new Promise((resolve, reject) => {
reader.onload = () => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"cypress": "cypress run --component",
"cypress:gui": "cypress open --component",
"test": "vitest run",
"test:watch": "vitest run --watch",
"test:watch": "vitest watch",
"test:coverage": "vitest run --coverage",
"l10n:extract": "node build/extract-l10n.js"
},
Expand Down

0 comments on commit 3e40a97

Please sign in to comment.