diff --git a/src/index.ts b/src/index.ts index b0ca8a9..760ee95 100644 --- a/src/index.ts +++ b/src/index.ts @@ -377,6 +377,9 @@ export function getWorksheets(options: IWorksheetOptions) { return new Promise((resolve, reject) => { function processWorkbook() { zip.stream('xl/workbook.xml', (err: any, stream: ReadStream) => { + if (err) { + reject(err); + } stream.pipe(saxStream({ strict: true, tag: ['x:sheet', 'sheet'], @@ -390,6 +393,7 @@ export function getWorksheets(options: IWorksheetOptions) { zip.close(); resolve(sheets); }); + stream.on('error', reject); }); } @@ -401,6 +405,7 @@ export function getWorksheets(options: IWorksheetOptions) { zip.on('ready', () => { processWorkbook(); }); + zip.on('error', reject); }); } diff --git a/tests/assets/corrupted-file.xlsx b/tests/assets/corrupted-file.xlsx new file mode 100644 index 0000000..a626735 --- /dev/null +++ b/tests/assets/corrupted-file.xlsx @@ -0,0 +1,3 @@ +invalid-content, corrupted-file, column 3 +0,1,3 +1,2,3 \ No newline at end of file diff --git a/tests/xlsx-stream.spec.ts b/tests/xlsx-stream.spec.ts index 7ba75f0..08155d7 100644 --- a/tests/xlsx-stream.spec.ts +++ b/tests/xlsx-stream.spec.ts @@ -95,6 +95,12 @@ it('gets worksheets with correct hidden info', async (done) => { done(); }); +it('get worksheets should fail gracefully for a corrupted xlsx', () => { + return getWorksheets({ + filePath: './tests/assets/corrupted-file.xlsx', + }).catch(e => expect(e).toMatch('Bad archive')); +}); + it('gets worksheet by name, even if they are reordered', async (done) => { const data: any = []; const stream = await getXlsxStream({