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: [#1565] Strictly check the type of blob parameters #1575

Merged
merged 5 commits into from
Nov 6, 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
20 changes: 20 additions & 0 deletions packages/happy-dom/src/file/FileReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ export default class FileReader extends EventTarget {
* @param blob Blob.
*/
public readAsArrayBuffer(blob: Blob): void {
if (!(blob instanceof Blob)) {
throw new this[PropertySymbol.window].TypeError(
`Failed to execute 'readAsArrayBuffer' on 'FileReader': parameter 1 is not of type 'Blob'.`
);
}
this.#readFile(blob, FileReaderFormatEnum.buffer);
}

Expand All @@ -58,6 +63,11 @@ export default class FileReader extends EventTarget {
* @param blob Blob.
*/
public readAsBinaryString(blob: Blob): void {
if (!(blob instanceof Blob)) {
throw new this[PropertySymbol.window].TypeError(
`Failed to execute 'readAsBinaryString' on 'FileReader': parameter 1 is not of type 'Blob'.`
);
}
this.#readFile(blob, FileReaderFormatEnum.binaryString);
}

Expand All @@ -67,6 +77,11 @@ export default class FileReader extends EventTarget {
* @param blob Blob.
*/
public readAsDataURL(blob: Blob): void {
if (!(blob instanceof Blob)) {
throw new this[PropertySymbol.window].TypeError(
`Failed to execute 'readAsDataURL' on 'FileReader': parameter 1 is not of type 'Blob'.`
);
}
this.#readFile(blob, FileReaderFormatEnum.dataURL);
}

Expand All @@ -77,6 +92,11 @@ export default class FileReader extends EventTarget {
* @param [encoding] Encoding.
*/
public readAsText(blob: Blob, encoding: string | null = null): void {
if (!(blob instanceof Blob)) {
throw new this[PropertySymbol.window].TypeError(
`Failed to execute 'readAsText' on 'FileReader': parameter 1 is not of type 'Blob'.`
);
}
this.#readFile(blob, FileReaderFormatEnum.text, encoding || 'UTF-8');
}

Expand Down
77 changes: 77 additions & 0 deletions packages/happy-dom/test/file/FileReader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,82 @@ describe('FileReader', () => {
await window.happyDOM?.waitUntilComplete();
expect(result).toBe('data:text/plain;charset=utf-8;base64,VEVTVA==');
});

it('Reads Blob as data URL passing invalid parameter.', () => {
expect(() => {
fileReader.readAsDataURL(<any>'invalid');
}).toThrow(
`Failed to execute 'readAsDataURL' on 'FileReader': parameter 1 is not of type 'Blob'.`
);
});
});

describe('readAsText()', () => {
it('Reads Blob as text.', async () => {
const blob = new Blob(['TEST'], {
type: 'text/plain;charset=utf-8'
});
let result: string | null = null;
fileReader.addEventListener('load', () => {
result = <string>fileReader.result;
});
fileReader.readAsText(blob);
await window.happyDOM?.waitUntilComplete();
expect(result).toBe('TEST');
});

it('Reads Blob as text passing invalid parameter.', () => {
expect(() => {
fileReader.readAsText(<any>'invalid');
}).toThrow(
`Failed to execute 'readAsText' on 'FileReader': parameter 1 is not of type 'Blob'.`
);
});
});

describe('readAsArrayBuffer()', () => {
it('Reads Blob as array buffer.', async () => {
const blob = new Blob(['TEST'], {
type: 'text/plain;charset=utf-8'
});
let result: ArrayBuffer | null = null;
fileReader.addEventListener('load', () => {
result = <ArrayBuffer>fileReader.result;
});
fileReader.readAsArrayBuffer(blob);
await window.happyDOM?.waitUntilComplete();
expect(result).toBeInstanceOf(ArrayBuffer);
});

it('Reads Blob as array buffer passing invalid parameter.', () => {
expect(() => {
fileReader.readAsArrayBuffer(<any>'invalid');
}).toThrow(
`Failed to execute 'readAsArrayBuffer' on 'FileReader': parameter 1 is not of type 'Blob'.`
);
});
});

describe('readAsBinaryString()', () => {
it('Reads Blob as binary string.', async () => {
const blob = new Blob(['TEST'], {
type: 'text/plain;charset=utf-8'
});
let result: string | null = null;
fileReader.addEventListener('load', () => {
result = <string>fileReader.result;
});
fileReader.readAsBinaryString(blob);
await window.happyDOM?.waitUntilComplete();
expect(result).toBe('TEST');
});

it('Reads Blob as binary string passing invalid parameter.', () => {
expect(() => {
fileReader.readAsBinaryString(<any>'invalid');
}).toThrow(
`Failed to execute 'readAsBinaryString' on 'FileReader': parameter 1 is not of type 'Blob'.`
);
});
});
});
Loading