Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not try to slice in chunk larger than the File itself #1057

Merged
merged 1 commit into from
Jan 24, 2024
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
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 (start === 0 && file.size <= 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
Loading