UNPKG

read-excel-file

Version:

Read `.xlsx` files in a web browser or in Node.js

48 lines (47 loc) 2.25 kB
import unzipFromStream from '../zip/unzipFromStream.js'; import UnzipError from '../zip/UnzipError.js'; import InvalidInputError from '../xlsx/file/InvalidInputError.js'; import convertInputToNodeStream from './convertInputToNodeStream.js'; import filterZipArchiveEntry from './filterZipArchiveEntry.js'; import InputValidationStream from '../xlsx/file/InputValidationStream.js'; import checkpoint, { resetCheckpoint } from '../utility/checkpoint.js'; /** * Unpacks `*.xlsx` file contents. * An `.xlsx` file is really just a `.zip` archive with `.xml` files inside. * @param {(string|Stream|Buffer|Blob)} input * @return {Promise<Record<string,Uint8Array>} Resolves to an object holding `*.xlsx` file entries. */ export default function unpackXlsxFile(input) { resetCheckpoint(); checkpoint('unpack files'); var stream = convertInputToNodeStream(input); return new Promise(function (resolve, reject) { var streamWithInputValidation = stream // Because the original `stream` reference is going to be replaced, // while it still has that reference here, it should set up a listener // to catch errors emitted from the input stream (for example, a file read error). // // That's because the .pipe() method does not automatically propagate errors // from a source (input) stream to the destination stream or the end of the pipeline. // You would need to attach an 'error' event handler to each stream in the chain. // // A more convenient alternative would be to use `stream.pipeline()` function: // `pipeline(stream1, stream2, (error) => { ... })` // .on('error', reject) // Pipe the input through file type validation stream. // If the file type is invalid, the updated `stream` reference will emit // an "error" event, which is going to be caught inside `unzipFromStream_()` function. .pipe(new InputValidationStream()); unzipFromStream(streamWithInputValidation, { filter: filterZipArchiveEntry }).then(resolve, function (error) { if (error instanceof UnzipError) { reject(new InvalidInputError('INVALID_ZIP', error.cause)); } else { reject(error); } }); }); } //# sourceMappingURL=unpackXlsxFileNode.js.map