read-excel-file
Version:
Read `.xlsx` files in a web browser or in Node.js
73 lines (70 loc) • 4.79 kB
JavaScript
;
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 _unzipFromArrayBuffer = _interopRequireDefault(require("../zip/unzipFromArrayBuffer.js"));
var _UnzipError = _interopRequireDefault(require("../zip/UnzipError.js"));
var _InvalidInputError = _interopRequireDefault(require("../xlsx/file/InvalidInputError.js"));
var _filterZipArchiveEntry = _interopRequireDefault(require("./filterZipArchiveEntry.js"));
var _validateLeadingBytes = _interopRequireDefault(require("../xlsx/file/validateLeadingBytes.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 }; }
// Here it uses the "async" version of the unzipper function
// just because it unzips the files inside an archive in parallel
// rather than sequentially. It uses web workers for that.
// Smaller files are still handled synchronously under the hood.
//
// It did condict a couple of basic manual tests in a web browser
// and the results showed that a `1 MB` `.xlsx` file is read in `100 ms`
// both using "sync" and "async" versions of the unzipper function,
// and reading a `200 KB` `.xlsx` file is done in `70 ms` when using
// the "sync" version and in `80 ms" when using the "async" verison.
//
// So the "sync" version of the unzipper function is faster on smaller file sizes
// of about < 1 MB. Still, the rest of the parsing code smoothes out this difference,
// and the end result is not much different. Does the difference of `10 ms` justify
// the mental overhead of choosing the right variant for a file of a given size?
// I'd say "not really". Sure, performance fans wouldn't scuff on those `10 ms`
// but it's literally "a blink of an eye" and about a duration of a single frame
// on a 120 FPS screen.
//
// Hence, it simply uses the "async" variant of the unzipper function in any case.
//
// import unzipFromArrayBufferSync from '../zip/unzipFromArrayBufferSync.js'
/**
* Unpacks `*.xlsx` file contents.
* An `.xlsx` file is really just a `.zip` archive with `.xml` files inside.
* @param {(File|Blob|ArrayBuffer)} 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');
if (input instanceof File || input instanceof Blob) {
return input.arrayBuffer().then(getResultFromArrayBuffer);
}
return Promise.resolve(input).then(getResultFromArrayBuffer);
}
function getResultFromArrayBuffer(arrayBuffer) {
(0, _validateLeadingBytes["default"])(new Uint8Array(arrayBuffer));
return (0, _unzipFromArrayBuffer["default"])(arrayBuffer, {
filter: _filterZipArchiveEntry["default"]
}).then(function (result) {
return result;
}, function (error) {
if (error instanceof _UnzipError["default"]) {
throw new _InvalidInputError["default"]('INVALID_ZIP', error.cause);
} else {
throw error;
}
});
}
// function getResultFromArrayBuffer(arrayBuffer) {
// const result = unzipFromArrayBufferSync(arrayBuffer, { filter: filterZipArchiveEntry })
// return Promise.resolve(result)
// }
//# sourceMappingURL=unpackXlsxFileBrowser.js.map