UNPKG

read-excel-file

Version:

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

57 lines (56 loc) 4.21 kB
"use strict"; function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = unpackXlsxFile; var _unzipFromStream = _interopRequireDefault(require("../zip/unzipFromStream.js")); var _UnzipError = _interopRequireDefault(require("../zip/UnzipError.js")); var _InvalidInputError = _interopRequireDefault(require("../xlsx/file/InvalidInputError.js")); var _convertInputToNodeStream = _interopRequireDefault(require("./convertInputToNodeStream.js")); var _filterZipArchiveEntry = _interopRequireDefault(require("./filterZipArchiveEntry.js")); var _InputValidationStream = _interopRequireDefault(require("../xlsx/file/InputValidationStream.js")); var _checkpoint = _interopRequireWildcard(require("../utility/checkpoint.js")); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } /** * 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. */ function unpackXlsxFile(input) { (0, _checkpoint.resetCheckpoint)(); (0, _checkpoint["default"])('unpack files'); var stream = (0, _convertInputToNodeStream["default"])(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["default"]()); (0, _unzipFromStream["default"])(streamWithInputValidation, { filter: _filterZipArchiveEntry["default"] }).then(resolve, function (error) { if (error instanceof _UnzipError["default"]) { reject(new _InvalidInputError["default"]('INVALID_ZIP', error.cause)); } else { reject(error); } }); }); } //# sourceMappingURL=unpackXlsxFileNode.js.map