Skip to content

Commit

Permalink
Merge pull request #32 from santiq/master
Browse files Browse the repository at this point in the history
Fix error handling in getWorksheets
  • Loading branch information
jansivans authored Nov 20, 2020
2 parents dffb235 + b85650b commit f136b33
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,9 @@ export function getWorksheets(options: IWorksheetOptions) {
return new Promise<IWorksheet[]>((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'],
Expand All @@ -390,6 +393,7 @@ export function getWorksheets(options: IWorksheetOptions) {
zip.close();
resolve(sheets);
});
stream.on('error', reject);
});
}

Expand All @@ -401,6 +405,7 @@ export function getWorksheets(options: IWorksheetOptions) {
zip.on('ready', () => {
processWorkbook();
});
zip.on('error', reject);
});
}

Expand Down
3 changes: 3 additions & 0 deletions tests/assets/corrupted-file.xlsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
invalid-content, corrupted-file, column 3
0,1,3
1,2,3
6 changes: 6 additions & 0 deletions tests/xlsx-stream.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit f136b33

Please sign in to comment.