read-excel-file
Version:
Read `.xlsx` files in a web browser or in Node.js
100 lines (94 loc) • 9.11 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); }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }
function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct.bind(); } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
// This code was originally submitted by Etienne Prothon.
// https://gitlab.com/catamphetamine/read-excel-file/-/merge_requests/11
// An error thrown when the input passed to `readXlsxFile()` is not a valid `.xlsx` file.
//
// Carries a documented, stable `code` property that consumers can reliably
// branch on — same convention as the schema-parsing `InvalidError` class
// (`error.code`) — instead of string-matching a low-level error message
// leaked from a transitive dependency (Hyrum's law).
//
// The `name` property is set so that the error type can also be detected via
// `error.name === 'InvalidInputError'` in setups where `instanceof` is unreliable
// (e.g. when both the ES-module and the CommonJS builds of this package end up
// being loaded in the same application).
var MESSAGES = {
XLS_FILE_NOT_SUPPORTED: 'You passed a legacy `.xls` file. Only `.xlsx` files are supported',
FILE_NOT_SUPPORTED: 'Doesn\'t look like an `.xlsx` file',
INVALID_ZIP: 'Couldn\'t unzip `.xlsx` file contents',
NO_DATA: 'No data'
};
// `.xlsx` files are just ZIP archives, so every valid `.xlsx` file starts with a
// ZIP local file header signature "PK" (`0x50 0x4B`). When the input isn't a ZIP
// archive, a 3rd-party unzipper library throws an obscure low-level error
// (for example `"invalid signature: 0xe011cfd0"` — the four leading bytes of a
// binary `.xls` file read in little-endian).
// That gives the caller nothing stable to branch on, and the exact wording can change
// depending on the 3rd-party unzipper library being used.
// This error standardizes the handling of cases when an invalid file is passed.
var InvalidInputError = /*#__PURE__*/function (_Error) {
_inherits(InvalidInputError, _Error);
var _super = _createSuper(InvalidInputError);
/**
* Creates an `InvalidInputError` instance.
* @param {string} code
* @param {any} [cause]
*/
function InvalidInputError(code, cause) {
var _this;
_classCallCheck(this, InvalidInputError);
_this = _super.call(this, MESSAGES[code] || code);
// Set `code` property.
_this.code = code;
// Set `name` property.
//
// This error could be detected either by `instanceof InvalidInputError`
// or by comparing its `name` property value to "InvalidInputError".
// Why use the `name` comparison when `instanceof` operator is available?
// Google AI tells that it does make sense in the cases when an error is
// "serialized" and then "deserialized" in a "distributed" environment such as
// throwing an error in a worker thread and then handling it in a main thread,
// or when throwing it in one "microservice" and then catching it in another one,
// or just logging as in `sentry.io`. And in those cases, `error.constructor.name`
// isn't always available for same reason of "serializing" and then "deserializing".
//
// For example, even if `InvalidInputError` is a named export of this package,
// when later bunding the application code with a bundler it will still be minified and renamed.
// That's when the `name` property could be used to find out the actual type of the error
// in case it gets thrown and reported to a remote system like `sentry.io`.
//
// By the way, core Node.js errors themselves have a `name` property.
//
_this.name = 'InvalidInputError';
// Set `cause` property.
//
// `Error.prototype.cause` property indicates the specific, original reason
// a given error occurred. Standardized in ES2022, it allows you to chain errors
// by catching a low-level exception and re-throwing a meaningful, high-level error
// without wiping out the diagnostic context or stack trace of the original failure.
//
// Adding a `.cause` property mimics modern error chaining for environments that support it
// while remaining completely harmless in older systems.
//
_this.cause = cause;
return _this;
}
return _createClass(InvalidInputError);
}( /*#__PURE__*/_wrapNativeSuper(Error));
export { InvalidInputError as default };
//# sourceMappingURL=InvalidInputError.js.map