read-excel-file
Version:
Read `.xlsx` files in a web browser or in Node.js
96 lines (93 loc) • 7.69 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 _createForOfIteratorHelperLoose(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (it) return (it = it.call(o)).next.bind(it); if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
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 _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 _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
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 _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 _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
import { Transform } from 'node:stream';
import createFileTypeDetector from './createFileTypeDetector.js';
import { validateByte, noFileTypeCouldBeDetermined } from './validateLeadingBytes.js';
import InvalidInputError from './InvalidInputError.js';
/**
* Verifies that an input stream represents a valid `.xlsx` file.
*
* Throws an `InvalidInputError` when the input isn't an `.xlsx` file.
*/
var InputValidationStream = /*#__PURE__*/function (_Transform) {
_inherits(InputValidationStream, _Transform);
var _super = _createSuper(InputValidationStream);
function InputValidationStream() {
var _this;
_classCallCheck(this, InputValidationStream);
_this = _super.call(this);
_this.d = createFileTypeDetector();
_this.i = 0;
_this.chunks = [];
return _this;
}
_createClass(InputValidationStream, [{
key: "_transform",
value: function _transform(chunk, encoding, callback) {
if (this.d) {
// Don't send this chunk to the unzipper stream yet,
// otherwise it might throw an invalid `.zip` archive error
// before it has detected an `.xls` file.
this.chunks.push(chunk);
// Validate the bytes of the chunk.
for (var _iterator = _createForOfIteratorHelperLoose(chunk), _step; !(_step = _iterator()).done;) {
var _byte = _step.value;
try {
if (validateByte(_byte, this.d)) {
this.d = undefined;
chunk = Buffer.concat(this.chunks);
this.chunks = undefined;
break;
}
} catch (error) {
// Destroy the stream with a validation error,
// halt further processing and emit an "error" event.
return callback(error);
}
this.i++;
}
if (this.d) {
return callback();
}
}
// Pass the chunk down to the next stream destination
this.push(chunk);
callback();
}
// `_flush()` is called when there will be no more input, before the "end" event is emitted.
}, {
key: "_flush",
value: function _flush(callback) {
// If it's still deciding on the file type and there will be no more bytes to read
// then the file is too short and it should throw an error.
if (this.d) {
try {
noFileTypeCouldBeDetermined(this.i);
} catch (error) {
// Destroy the stream with a validation error
// and emit an "error" event instead of an "end" event.
return callback(error);
}
}
// Call the callback to signal that flushing is complete.
callback();
}
}]);
return InputValidationStream;
}(Transform);
export { InputValidationStream as default };
//# sourceMappingURL=InputValidationStream.js.map