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

Reject with error message if stream is missing #57

Closed
Closed
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
147 changes: 85 additions & 62 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import path from 'path';
import ssf from 'ssf';
import { Transform } from 'stream';
import { ReadStream } from 'tty';
import { inspect } from 'util';


import { IMergedCellDictionary, IWorksheet, IWorksheetOptions, IXlsxStreamOptions, IXlsxStreamsOptions, numberFormatType } from './types';

Expand Down Expand Up @@ -244,75 +246,83 @@ export async function* getXlsxStreams(options: IXlsxStreamsOptions): AsyncGenera
formats[i] = format;
}
}
zip.stream('xl/sharedStrings.xml', (err: any, stream: ReadStream) => {
if (stream) {
if (options.encoding) {
stream.setEncoding(options.encoding);
}
stream.pipe(saxStream({
strict: true,
tag: ['x:si', 'si']
})).on('data', (x: any) => {
const record = x.record;
if (record.children.t) {
strings.push(record.children.t.value);
} else if (!record.children.r.length) {
strings.push(record.children.r.children.t.value);
} else {
let str = '';
for (let i = 0; i < record.children.r.length; i++) {
str += record.children.r[i].children.t.value;
}
strings.push(str);
}
});
stream.on('end', () => {
resolve();
});
} else {
zip.stream('xl/sharedStrings.xml', (err: any, stream?: ReadStream) => {
if (!stream) {
console.warn('xlstream failed to find stream: ' + inspect(err, undefined, 4));
resolve();
return;
}

if (options.encoding) {
stream.setEncoding(options.encoding);
}
stream.pipe(saxStream({
strict: true,
tag: ['x:si', 'si']
})).on('data', (x: any) => {
const record = x.record;
if (record.children.t) {
strings.push(record.children.t.value);
} else if (!record.children.r.length) {
strings.push(record.children.r.children.t.value);
} else {
let str = '';
for (let i = 0; i < record.children.r.length; i++) {
str += record.children.r[i].children.t.value;
}
strings.push(str);
}
});
stream.on('end', () => {
resolve();
});
});
}

function processStyles() {
zip.stream(`xl/styles.xml`, (err: any, stream: ReadStream) => {
if (stream) {
if (options.encoding) {
stream.setEncoding(options.encoding);
}
stream.pipe(saxStream({
strict: true,
tag: ['x:cellXfs', 'x:numFmts', 'cellXfs', 'numFmts']
})).on('data', (x: any) => {
if ((x.tag === 'numFmts' || x.tag === 'x:numFmts') && x.record.children) {
let numFmtField = x.record.children['x:numFmt'] ? 'x:numFmt' : 'numFmt';
var children = x.record.children[numFmtField].length ? x.record.children[numFmtField] : [x.record.children[numFmtField]];
for (var i = 0; i < children.length; i++) {
numberFormats[Number(children[i].attribs.numFmtId)] = children[i].attribs.formatCode;
}
zip.stream(`xl/styles.xml`, (err: any, stream?: ReadStream) => {
if (!stream) {
console.warn('xlstream failed to find stream: ' + inspect(err, undefined, 4));
processSharedStrings(numberFormats, formats);
return;
}

if (options.encoding) {
stream.setEncoding(options.encoding);
}
stream.pipe(saxStream({
strict: true,
tag: ['x:cellXfs', 'x:numFmts', 'cellXfs', 'numFmts']
})).on('data', (x: any) => {
if ((x.tag === 'numFmts' || x.tag === 'x:numFmts') && x.record.children) {
let numFmtField = x.record.children['x:numFmt'] ? 'x:numFmt' : 'numFmt';
var children = x.record.children[numFmtField].length ? x.record.children[numFmtField] : [x.record.children[numFmtField]];
for (var i = 0; i < children.length; i++) {
numberFormats[Number(children[i].attribs.numFmtId)] = children[i].attribs.formatCode;
}
else if ((x.tag === 'cellXfs' || x.tag === 'x:cellXfs') && x.record.children) {
const xfField = x.record.children['x:xf'] ? 'x:xf' : 'xf';
for (var i = 0; i < x.record.children[xfField].length; i++) {
var ch = x.record.children[xfField][i];
if (ch.attribs?.numFmtId) {
formats[i] = ch.attribs?.numFmtId ? Number(ch.attribs?.numFmtId) : '';
}
}
else if ((x.tag === 'cellXfs' || x.tag === 'x:cellXfs') && x.record.children) {
const xfField = x.record.children['x:xf'] ? 'x:xf' : 'xf';
for (var i = 0; i < x.record.children[xfField].length; i++) {
var ch = x.record.children[xfField][i];
if (ch.attribs?.numFmtId) {
formats[i] = ch.attribs?.numFmtId ? Number(ch.attribs?.numFmtId) : '';
}
}
});
stream.on('end', () => {
processSharedStrings(numberFormats, formats);
});
} else {
}
});
stream.on('end', () => {
processSharedStrings(numberFormats, formats);
}
});
});
}

function processWorkbook() {
zip.stream('xl/workbook.xml', (err: any, stream: ReadStream) => {
zip.stream('xl/workbook.xml', (err: any, stream?: ReadStream) => {
if (!stream || err) {
reject('Received error from node-stream-zip: ' + inspect(err, undefined, 4));
return;
}
if (options.encoding) {
stream.setEncoding(options.encoding);
}
Expand All @@ -330,7 +340,11 @@ export async function* getXlsxStreams(options: IXlsxStreamsOptions): AsyncGenera
}

function getRels() {
zip.stream('xl/_rels/workbook.xml.rels', (err: any, stream: ReadStream) => {
zip.stream('xl/_rels/workbook.xml.rels', (err: any, stream?: ReadStream) => {
if (!stream || err) {
reject('Received error from node-stream-zip: ' + inspect(err, undefined, 4));
return;
}
if (options.encoding) {
stream.setEncoding(options.encoding);
}
Expand Down Expand Up @@ -358,7 +372,11 @@ export async function* getXlsxStreams(options: IXlsxStreamsOptions): AsyncGenera

function getMergedCellDictionary(sheetFileName: string) {
return new Promise<IMergedCellDictionary>((resolve, reject) => {
zip.stream(`xl/worksheets/${sheetFileName}`, (err: any, stream: ReadStream) => {
zip.stream(`xl/worksheets/${sheetFileName}`, (err: any, stream?: ReadStream) => {
if (!stream || err) {
reject('Received error from node-stream-zip: ' + inspect(err, undefined, 4));
return;
}
if (options.encoding) {
stream.setEncoding(options.encoding);
}
Expand Down Expand Up @@ -411,7 +429,11 @@ export async function* getXlsxStreams(options: IXlsxStreamsOptions): AsyncGenera
}
return new Promise<Transform>((resolve, reject) => {
const sheetFullFileName = `xl/worksheets/${sheetFileName}`;
zip.stream(sheetFullFileName, (err: any, stream: ReadStream) => {
zip.stream(sheetFullFileName, (err: any, stream?: ReadStream) => {
if (!stream || err) {
reject('Received error from node-stream-zip: ' + inspect(err, undefined, 4));
return;
}
if (options.encoding) {
stream.setEncoding(options.encoding);
}
Expand Down Expand Up @@ -459,13 +481,14 @@ export async function* getXlsxStreams(options: IXlsxStreamsOptions): AsyncGenera
export function getWorksheets(options: IWorksheetOptions) {
return new Promise<IWorksheet[]>((resolve, reject) => {
function processWorkbook() {
zip.stream('xl/workbook.xml', (err: any, stream: ReadStream) => {
zip.stream('xl/workbook.xml', (err: any, stream?: ReadStream) => {
if (!stream || err) {
reject('Received error from node-stream-zip: ' + inspect(err, undefined, 4));
return;
}
if (options.encoding) {
stream.setEncoding(options.encoding);
}
if (err) {
reject(err);
}
stream.pipe(saxStream({
strict: true,
tag: ['x:sheet', 'sheet'],
Expand Down