workbook-reader.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  1. "use strict";
  2. function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  3. function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
  4. 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); }
  5. 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; }
  6. function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
  7. function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
  8. function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
  9. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  10. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  11. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  12. 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, descriptor.key, descriptor); } }
  13. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  14. 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 } }); if (superClass) _setPrototypeOf(subClass, superClass); }
  15. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  16. 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); }; }
  17. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  18. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  19. function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
  20. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  21. function _asyncGeneratorDelegate(inner, awaitWrap) { var iter = {}, waiting = false; function pump(key, value) { waiting = true; value = new Promise(function (resolve) { resolve(inner[key](value)); }); return { done: false, value: awaitWrap(value) }; } ; if (typeof Symbol === "function" && Symbol.iterator) { iter[Symbol.iterator] = function () { return this; }; } iter.next = function (value) { if (waiting) { waiting = false; return value; } return pump("next", value); }; if (typeof inner.throw === "function") { iter.throw = function (value) { if (waiting) { waiting = false; throw value; } return pump("throw", value); }; } if (typeof inner.return === "function") { iter.return = function (value) { if (waiting) { waiting = false; return value; } return pump("return", value); }; } return iter; }
  22. function _awaitAsyncGenerator(value) { return new _AwaitValue(value); }
  23. function _wrapAsyncGenerator(fn) { return function () { return new _AsyncGenerator(fn.apply(this, arguments)); }; }
  24. function _AsyncGenerator(gen) { var front, back; function send(key, arg) { return new Promise(function (resolve, reject) { var request = { key: key, arg: arg, resolve: resolve, reject: reject, next: null }; if (back) { back = back.next = request; } else { front = back = request; resume(key, arg); } }); } function resume(key, arg) { try { var result = gen[key](arg); var value = result.value; var wrappedAwait = value instanceof _AwaitValue; Promise.resolve(wrappedAwait ? value.wrapped : value).then(function (arg) { if (wrappedAwait) { resume(key === "return" ? "return" : "next", arg); return; } settle(result.done ? "return" : "normal", arg); }, function (err) { resume("throw", err); }); } catch (err) { settle("throw", err); } } function settle(type, value) { switch (type) { case "return": front.resolve({ value: value, done: true }); break; case "throw": front.reject(value); break; default: front.resolve({ value: value, done: false }); break; } front = front.next; if (front) { resume(front.key, front.arg); } else { back = null; } } this._invoke = send; if (typeof gen.return !== "function") { this.return = undefined; } }
  25. if (typeof Symbol === "function" && Symbol.asyncIterator) { _AsyncGenerator.prototype[Symbol.asyncIterator] = function () { return this; }; }
  26. _AsyncGenerator.prototype.next = function (arg) { return this._invoke("next", arg); };
  27. _AsyncGenerator.prototype.throw = function (arg) { return this._invoke("throw", arg); };
  28. _AsyncGenerator.prototype.return = function (arg) { return this._invoke("return", arg); };
  29. function _AwaitValue(value) { this.wrapped = value; }
  30. function _asyncIterator(iterable) { var method; if (typeof Symbol !== "undefined") { if (Symbol.asyncIterator) { method = iterable[Symbol.asyncIterator]; if (method != null) return method.call(iterable); } if (Symbol.iterator) { method = iterable[Symbol.iterator]; if (method != null) return method.call(iterable); } } throw new TypeError("Object is not async iterable"); }
  31. var fs = require('fs');
  32. var _require = require('events'),
  33. EventEmitter = _require.EventEmitter;
  34. var _require2 = require('readable-stream'),
  35. PassThrough = _require2.PassThrough,
  36. Readable = _require2.Readable;
  37. var nodeStream = require('stream');
  38. var unzip = require('unzipper');
  39. var tmp = require('tmp');
  40. var iterateStream = require('../../utils/iterate-stream');
  41. var parseSax = require('../../utils/parse-sax');
  42. var StyleManager = require('../../xlsx/xform/style/styles-xform');
  43. var WorkbookXform = require('../../xlsx/xform/book/workbook-xform');
  44. var RelationshipsXform = require('../../xlsx/xform/core/relationships-xform');
  45. var WorksheetReader = require('./worksheet-reader');
  46. var HyperlinkReader = require('./hyperlink-reader');
  47. tmp.setGracefulCleanup();
  48. var WorkbookReader = /*#__PURE__*/function (_EventEmitter) {
  49. _inherits(WorkbookReader, _EventEmitter);
  50. var _super = _createSuper(WorkbookReader);
  51. function WorkbookReader(input) {
  52. var _this4;
  53. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  54. _classCallCheck(this, WorkbookReader);
  55. _this4 = _super.call(this);
  56. _this4.input = input;
  57. _this4.options = _objectSpread({
  58. worksheets: 'emit',
  59. sharedStrings: 'cache',
  60. hyperlinks: 'ignore',
  61. styles: 'ignore',
  62. entries: 'ignore'
  63. }, options);
  64. _this4.styles = new StyleManager();
  65. _this4.styles.init();
  66. return _this4;
  67. }
  68. _createClass(WorkbookReader, [{
  69. key: "_getStream",
  70. value: function _getStream(input) {
  71. if (input instanceof nodeStream.Readable || input instanceof Readable) {
  72. return input;
  73. }
  74. if (typeof input === 'string') {
  75. return fs.createReadStream(input);
  76. }
  77. throw new Error("Could not recognise input: ".concat(input));
  78. }
  79. }, {
  80. key: "read",
  81. value: function () {
  82. var _read = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(input, options) {
  83. var _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, _value5, eventType, value;
  84. return regeneratorRuntime.wrap(function _callee$(_context) {
  85. while (1) {
  86. switch (_context.prev = _context.next) {
  87. case 0:
  88. _context.prev = 0;
  89. _iteratorNormalCompletion = true;
  90. _didIteratorError = false;
  91. _context.prev = 3;
  92. _iterator = _asyncIterator(this.parse(input, options));
  93. case 5:
  94. _context.next = 7;
  95. return _iterator.next();
  96. case 7:
  97. _step = _context.sent;
  98. _iteratorNormalCompletion = _step.done;
  99. _context.next = 11;
  100. return _step.value;
  101. case 11:
  102. _value = _context.sent;
  103. if (_iteratorNormalCompletion) {
  104. _context.next = 28;
  105. break;
  106. }
  107. _value5 = _value, eventType = _value5.eventType, value = _value5.value;
  108. _context.t0 = eventType;
  109. _context.next = _context.t0 === 'shared-strings' ? 17 : _context.t0 === 'worksheet' ? 19 : _context.t0 === 'hyperlinks' ? 23 : 25;
  110. break;
  111. case 17:
  112. this.emit(eventType, value);
  113. return _context.abrupt("break", 25);
  114. case 19:
  115. this.emit(eventType, value);
  116. _context.next = 22;
  117. return value.read();
  118. case 22:
  119. return _context.abrupt("break", 25);
  120. case 23:
  121. this.emit(eventType, value);
  122. return _context.abrupt("break", 25);
  123. case 25:
  124. _iteratorNormalCompletion = true;
  125. _context.next = 5;
  126. break;
  127. case 28:
  128. _context.next = 34;
  129. break;
  130. case 30:
  131. _context.prev = 30;
  132. _context.t1 = _context["catch"](3);
  133. _didIteratorError = true;
  134. _iteratorError = _context.t1;
  135. case 34:
  136. _context.prev = 34;
  137. _context.prev = 35;
  138. if (!(!_iteratorNormalCompletion && _iterator.return != null)) {
  139. _context.next = 39;
  140. break;
  141. }
  142. _context.next = 39;
  143. return _iterator.return();
  144. case 39:
  145. _context.prev = 39;
  146. if (!_didIteratorError) {
  147. _context.next = 42;
  148. break;
  149. }
  150. throw _iteratorError;
  151. case 42:
  152. return _context.finish(39);
  153. case 43:
  154. return _context.finish(34);
  155. case 44:
  156. this.emit('end');
  157. this.emit('finished');
  158. _context.next = 51;
  159. break;
  160. case 48:
  161. _context.prev = 48;
  162. _context.t2 = _context["catch"](0);
  163. this.emit('error', _context.t2);
  164. case 51:
  165. case "end":
  166. return _context.stop();
  167. }
  168. }
  169. }, _callee, this, [[0, 48], [3, 30, 34, 44], [35,, 39, 43]]);
  170. }));
  171. function read(_x, _x2) {
  172. return _read.apply(this, arguments);
  173. }
  174. return read;
  175. }()
  176. }, {
  177. key: Symbol.asyncIterator,
  178. value: function value() {
  179. var _this = this;
  180. return _wrapAsyncGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
  181. var _iteratorNormalCompletion2, _didIteratorError2, _iteratorError2, _iterator2, _step2, _value2, _value6, eventType, value;
  182. return regeneratorRuntime.wrap(function _callee2$(_context2) {
  183. while (1) {
  184. switch (_context2.prev = _context2.next) {
  185. case 0:
  186. _iteratorNormalCompletion2 = true;
  187. _didIteratorError2 = false;
  188. _context2.prev = 2;
  189. _iterator2 = _asyncIterator(_this.parse());
  190. case 4:
  191. _context2.next = 6;
  192. return _awaitAsyncGenerator(_iterator2.next());
  193. case 6:
  194. _step2 = _context2.sent;
  195. _iteratorNormalCompletion2 = _step2.done;
  196. _context2.next = 10;
  197. return _awaitAsyncGenerator(_step2.value);
  198. case 10:
  199. _value2 = _context2.sent;
  200. if (_iteratorNormalCompletion2) {
  201. _context2.next = 19;
  202. break;
  203. }
  204. _value6 = _value2, eventType = _value6.eventType, value = _value6.value;
  205. if (!(eventType === 'worksheet')) {
  206. _context2.next = 16;
  207. break;
  208. }
  209. _context2.next = 16;
  210. return value;
  211. case 16:
  212. _iteratorNormalCompletion2 = true;
  213. _context2.next = 4;
  214. break;
  215. case 19:
  216. _context2.next = 25;
  217. break;
  218. case 21:
  219. _context2.prev = 21;
  220. _context2.t0 = _context2["catch"](2);
  221. _didIteratorError2 = true;
  222. _iteratorError2 = _context2.t0;
  223. case 25:
  224. _context2.prev = 25;
  225. _context2.prev = 26;
  226. if (!(!_iteratorNormalCompletion2 && _iterator2.return != null)) {
  227. _context2.next = 30;
  228. break;
  229. }
  230. _context2.next = 30;
  231. return _awaitAsyncGenerator(_iterator2.return());
  232. case 30:
  233. _context2.prev = 30;
  234. if (!_didIteratorError2) {
  235. _context2.next = 33;
  236. break;
  237. }
  238. throw _iteratorError2;
  239. case 33:
  240. return _context2.finish(30);
  241. case 34:
  242. return _context2.finish(25);
  243. case 35:
  244. case "end":
  245. return _context2.stop();
  246. }
  247. }
  248. }, _callee2, null, [[2, 21, 25, 35], [26,, 30, 34]]);
  249. }))();
  250. }
  251. }, {
  252. key: "parse",
  253. value: function parse(input, options) {
  254. var _this2 = this;
  255. return _wrapAsyncGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() {
  256. var stream, zip, waitingWorkSheets, _iteratorNormalCompletion3, _didIteratorError3, _iteratorError3, _loop, _iterator3, _step3, _value3, _i, _waitingWorkSheets, _waitingWorkSheets$_i, sheetNo, path, tempFileCleanupCallback, fileStream;
  257. return regeneratorRuntime.wrap(function _callee3$(_context4) {
  258. while (1) {
  259. switch (_context4.prev = _context4.next) {
  260. case 0:
  261. if (options) _this2.options = options;
  262. stream = _this2.stream = _this2._getStream(input || _this2.input);
  263. zip = unzip.Parse({
  264. forceStream: true
  265. });
  266. stream.pipe(zip); // worksheets, deferred for parsing after shared strings reading
  267. waitingWorkSheets = [];
  268. _iteratorNormalCompletion3 = true;
  269. _didIteratorError3 = false;
  270. _context4.prev = 7;
  271. _loop = /*#__PURE__*/regeneratorRuntime.mark(function _loop() {
  272. var entry, match, sheetNo;
  273. return regeneratorRuntime.wrap(function _loop$(_context3) {
  274. while (1) {
  275. switch (_context3.prev = _context3.next) {
  276. case 0:
  277. entry = _value3;
  278. match = void 0;
  279. sheetNo = void 0;
  280. _context3.t0 = entry.path;
  281. _context3.next = _context3.t0 === '_rels/.rels' ? 6 : _context3.t0 === 'xl/_rels/workbook.xml.rels' ? 7 : _context3.t0 === 'xl/workbook.xml' ? 10 : _context3.t0 === 'xl/sharedStrings.xml' ? 13 : _context3.t0 === 'xl/styles.xml' ? 15 : 18;
  282. break;
  283. case 6:
  284. return _context3.abrupt("break", 34);
  285. case 7:
  286. _context3.next = 9;
  287. return _awaitAsyncGenerator(_this2._parseRels(entry));
  288. case 9:
  289. return _context3.abrupt("break", 34);
  290. case 10:
  291. _context3.next = 12;
  292. return _awaitAsyncGenerator(_this2._parseWorkbook(entry));
  293. case 12:
  294. return _context3.abrupt("break", 34);
  295. case 13:
  296. return _context3.delegateYield(_asyncGeneratorDelegate(_asyncIterator(_this2._parseSharedStrings(entry)), _awaitAsyncGenerator), "t1", 14);
  297. case 14:
  298. return _context3.abrupt("break", 34);
  299. case 15:
  300. _context3.next = 17;
  301. return _awaitAsyncGenerator(_this2._parseStyles(entry));
  302. case 17:
  303. return _context3.abrupt("break", 34);
  304. case 18:
  305. if (!entry.path.match(/xl\/worksheets\/sheet\d+[.]xml/)) {
  306. _context3.next = 29;
  307. break;
  308. }
  309. match = entry.path.match(/xl\/worksheets\/sheet(\d+)[.]xml/);
  310. sheetNo = match[1];
  311. if (!(_this2.sharedStrings && _this2.workbookRels)) {
  312. _context3.next = 25;
  313. break;
  314. }
  315. return _context3.delegateYield(_asyncGeneratorDelegate(_asyncIterator(_this2._parseWorksheet(iterateStream(entry), sheetNo)), _awaitAsyncGenerator), "t2", 23);
  316. case 23:
  317. _context3.next = 27;
  318. break;
  319. case 25:
  320. _context3.next = 27;
  321. return _awaitAsyncGenerator(new Promise(function (resolve, reject) {
  322. tmp.file(function (err, path, fd, tempFileCleanupCallback) {
  323. if (err) {
  324. return reject(err);
  325. }
  326. waitingWorkSheets.push({
  327. sheetNo: sheetNo,
  328. path: path,
  329. tempFileCleanupCallback: tempFileCleanupCallback
  330. });
  331. var tempStream = fs.createWriteStream(path);
  332. entry.pipe(tempStream);
  333. return tempStream.on('finish', function () {
  334. return resolve();
  335. });
  336. });
  337. }));
  338. case 27:
  339. _context3.next = 33;
  340. break;
  341. case 29:
  342. if (!entry.path.match(/xl\/worksheets\/_rels\/sheet\d+[.]xml.rels/)) {
  343. _context3.next = 33;
  344. break;
  345. }
  346. match = entry.path.match(/xl\/worksheets\/_rels\/sheet(\d+)[.]xml.rels/);
  347. sheetNo = match[1];
  348. return _context3.delegateYield(_asyncGeneratorDelegate(_asyncIterator(_this2._parseHyperlinks(iterateStream(entry), sheetNo)), _awaitAsyncGenerator), "t3", 33);
  349. case 33:
  350. return _context3.abrupt("break", 34);
  351. case 34:
  352. entry.autodrain();
  353. case 35:
  354. case "end":
  355. return _context3.stop();
  356. }
  357. }
  358. }, _loop);
  359. });
  360. _iterator3 = _asyncIterator(iterateStream(zip));
  361. case 10:
  362. _context4.next = 12;
  363. return _awaitAsyncGenerator(_iterator3.next());
  364. case 12:
  365. _step3 = _context4.sent;
  366. _iteratorNormalCompletion3 = _step3.done;
  367. _context4.next = 16;
  368. return _awaitAsyncGenerator(_step3.value);
  369. case 16:
  370. _value3 = _context4.sent;
  371. if (_iteratorNormalCompletion3) {
  372. _context4.next = 22;
  373. break;
  374. }
  375. return _context4.delegateYield(_loop(), "t0", 19);
  376. case 19:
  377. _iteratorNormalCompletion3 = true;
  378. _context4.next = 10;
  379. break;
  380. case 22:
  381. _context4.next = 28;
  382. break;
  383. case 24:
  384. _context4.prev = 24;
  385. _context4.t1 = _context4["catch"](7);
  386. _didIteratorError3 = true;
  387. _iteratorError3 = _context4.t1;
  388. case 28:
  389. _context4.prev = 28;
  390. _context4.prev = 29;
  391. if (!(!_iteratorNormalCompletion3 && _iterator3.return != null)) {
  392. _context4.next = 33;
  393. break;
  394. }
  395. _context4.next = 33;
  396. return _awaitAsyncGenerator(_iterator3.return());
  397. case 33:
  398. _context4.prev = 33;
  399. if (!_didIteratorError3) {
  400. _context4.next = 36;
  401. break;
  402. }
  403. throw _iteratorError3;
  404. case 36:
  405. return _context4.finish(33);
  406. case 37:
  407. return _context4.finish(28);
  408. case 38:
  409. _i = 0, _waitingWorkSheets = waitingWorkSheets;
  410. case 39:
  411. if (!(_i < _waitingWorkSheets.length)) {
  412. _context4.next = 48;
  413. break;
  414. }
  415. _waitingWorkSheets$_i = _waitingWorkSheets[_i], sheetNo = _waitingWorkSheets$_i.sheetNo, path = _waitingWorkSheets$_i.path, tempFileCleanupCallback = _waitingWorkSheets$_i.tempFileCleanupCallback;
  416. fileStream = fs.createReadStream(path); // TODO: Remove once node v8 is deprecated
  417. // Detect and upgrade old fileStreams
  418. if (!fileStream[Symbol.asyncIterator]) {
  419. fileStream = fileStream.pipe(new PassThrough());
  420. }
  421. return _context4.delegateYield(_asyncGeneratorDelegate(_asyncIterator(_this2._parseWorksheet(fileStream, sheetNo)), _awaitAsyncGenerator), "t2", 44);
  422. case 44:
  423. tempFileCleanupCallback();
  424. case 45:
  425. _i++;
  426. _context4.next = 39;
  427. break;
  428. case 48:
  429. case "end":
  430. return _context4.stop();
  431. }
  432. }
  433. }, _callee3, null, [[7, 24, 28, 38], [29,, 33, 37]]);
  434. }))();
  435. }
  436. }, {
  437. key: "_emitEntry",
  438. value: function _emitEntry(payload) {
  439. if (this.options.entries === 'emit') {
  440. this.emit('entry', payload);
  441. }
  442. }
  443. }, {
  444. key: "_parseRels",
  445. value: function () {
  446. var _parseRels2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4(entry) {
  447. var xform;
  448. return regeneratorRuntime.wrap(function _callee4$(_context5) {
  449. while (1) {
  450. switch (_context5.prev = _context5.next) {
  451. case 0:
  452. xform = new RelationshipsXform();
  453. _context5.next = 3;
  454. return xform.parseStream(iterateStream(entry));
  455. case 3:
  456. this.workbookRels = _context5.sent;
  457. case 4:
  458. case "end":
  459. return _context5.stop();
  460. }
  461. }
  462. }, _callee4, this);
  463. }));
  464. function _parseRels(_x3) {
  465. return _parseRels2.apply(this, arguments);
  466. }
  467. return _parseRels;
  468. }()
  469. }, {
  470. key: "_parseWorkbook",
  471. value: function () {
  472. var _parseWorkbook2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5(entry) {
  473. var workbook;
  474. return regeneratorRuntime.wrap(function _callee5$(_context6) {
  475. while (1) {
  476. switch (_context6.prev = _context6.next) {
  477. case 0:
  478. this._emitEntry({
  479. type: 'workbook'
  480. });
  481. workbook = new WorkbookXform();
  482. _context6.next = 4;
  483. return workbook.parseStream(iterateStream(entry));
  484. case 4:
  485. this.properties = workbook.map.workbookPr;
  486. this.model = workbook.model;
  487. case 6:
  488. case "end":
  489. return _context6.stop();
  490. }
  491. }
  492. }, _callee5, this);
  493. }));
  494. function _parseWorkbook(_x4) {
  495. return _parseWorkbook2.apply(this, arguments);
  496. }
  497. return _parseWorkbook;
  498. }()
  499. }, {
  500. key: "_parseSharedStrings",
  501. value: function _parseSharedStrings(entry) {
  502. var _this3 = this;
  503. return _wrapAsyncGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6() {
  504. var text, richText, index, font, _iteratorNormalCompletion4, _didIteratorError4, _iteratorError4, _iterator4, _step4, _value4, events, _iterator5, _step5, _step5$value, eventType, value, node, _node;
  505. return regeneratorRuntime.wrap(function _callee6$(_context7) {
  506. while (1) {
  507. switch (_context7.prev = _context7.next) {
  508. case 0:
  509. _this3._emitEntry({
  510. type: 'shared-strings'
  511. });
  512. _context7.t0 = _this3.options.sharedStrings;
  513. _context7.next = _context7.t0 === 'cache' ? 4 : _context7.t0 === 'emit' ? 6 : 7;
  514. break;
  515. case 4:
  516. _this3.sharedStrings = [];
  517. return _context7.abrupt("break", 8);
  518. case 6:
  519. return _context7.abrupt("break", 8);
  520. case 7:
  521. return _context7.abrupt("return");
  522. case 8:
  523. text = null;
  524. richText = [];
  525. index = 0;
  526. font = null;
  527. _iteratorNormalCompletion4 = true;
  528. _didIteratorError4 = false;
  529. _context7.prev = 14;
  530. _iterator4 = _asyncIterator(parseSax(iterateStream(entry)));
  531. case 16:
  532. _context7.next = 18;
  533. return _awaitAsyncGenerator(_iterator4.next());
  534. case 18:
  535. _step4 = _context7.sent;
  536. _iteratorNormalCompletion4 = _step4.done;
  537. _context7.next = 22;
  538. return _awaitAsyncGenerator(_step4.value);
  539. case 22:
  540. _value4 = _context7.sent;
  541. if (_iteratorNormalCompletion4) {
  542. _context7.next = 114;
  543. break;
  544. }
  545. events = _value4;
  546. _iterator5 = _createForOfIteratorHelper(events);
  547. _context7.prev = 26;
  548. _iterator5.s();
  549. case 28:
  550. if ((_step5 = _iterator5.n()).done) {
  551. _context7.next = 103;
  552. break;
  553. }
  554. _step5$value = _step5.value, eventType = _step5$value.eventType, value = _step5$value.value;
  555. if (!(eventType === 'opentag')) {
  556. _context7.next = 77;
  557. break;
  558. }
  559. node = value;
  560. _context7.t1 = node.name;
  561. _context7.next = _context7.t1 === 'b' ? 35 : _context7.t1 === 'charset' ? 38 : _context7.t1 === 'color' ? 41 : _context7.t1 === 'family' ? 47 : _context7.t1 === 'i' ? 50 : _context7.t1 === 'outline' ? 53 : _context7.t1 === 'rFont' ? 56 : _context7.t1 === 'si' ? 59 : _context7.t1 === 'sz' ? 63 : _context7.t1 === 'strike' ? 66 : _context7.t1 === 't' ? 67 : _context7.t1 === 'u' ? 69 : _context7.t1 === 'vertAlign' ? 72 : 75;
  562. break;
  563. case 35:
  564. font = font || {};
  565. font.bold = true;
  566. return _context7.abrupt("break", 75);
  567. case 38:
  568. font = font || {};
  569. font.charset = parseInt(node.attributes.charset, 10);
  570. return _context7.abrupt("break", 75);
  571. case 41:
  572. font = font || {};
  573. font.color = {};
  574. if (node.attributes.rgb) {
  575. font.color.argb = node.attributes.argb;
  576. }
  577. if (node.attributes.val) {
  578. font.color.argb = node.attributes.val;
  579. }
  580. if (node.attributes.theme) {
  581. font.color.theme = node.attributes.theme;
  582. }
  583. return _context7.abrupt("break", 75);
  584. case 47:
  585. font = font || {};
  586. font.family = parseInt(node.attributes.val, 10);
  587. return _context7.abrupt("break", 75);
  588. case 50:
  589. font = font || {};
  590. font.italic = true;
  591. return _context7.abrupt("break", 75);
  592. case 53:
  593. font = font || {};
  594. font.outline = true;
  595. return _context7.abrupt("break", 75);
  596. case 56:
  597. font = font || {};
  598. font.name = node.value;
  599. return _context7.abrupt("break", 75);
  600. case 59:
  601. font = null;
  602. richText = [];
  603. text = null;
  604. return _context7.abrupt("break", 75);
  605. case 63:
  606. font = font || {};
  607. font.size = parseInt(node.attributes.val, 10);
  608. return _context7.abrupt("break", 75);
  609. case 66:
  610. return _context7.abrupt("break", 75);
  611. case 67:
  612. text = null;
  613. return _context7.abrupt("break", 75);
  614. case 69:
  615. font = font || {};
  616. font.underline = true;
  617. return _context7.abrupt("break", 75);
  618. case 72:
  619. font = font || {};
  620. font.vertAlign = node.attributes.val;
  621. return _context7.abrupt("break", 75);
  622. case 75:
  623. _context7.next = 101;
  624. break;
  625. case 77:
  626. if (!(eventType === 'text')) {
  627. _context7.next = 81;
  628. break;
  629. }
  630. text = text ? text + value : value;
  631. _context7.next = 101;
  632. break;
  633. case 81:
  634. if (!(eventType === 'closetag')) {
  635. _context7.next = 101;
  636. break;
  637. }
  638. _node = value;
  639. _context7.t2 = _node.name;
  640. _context7.next = _context7.t2 === 'r' ? 86 : _context7.t2 === 'si' ? 90 : 101;
  641. break;
  642. case 86:
  643. richText.push({
  644. font: font,
  645. text: text
  646. });
  647. font = null;
  648. text = null;
  649. return _context7.abrupt("break", 101);
  650. case 90:
  651. if (!(_this3.options.sharedStrings === 'cache')) {
  652. _context7.next = 94;
  653. break;
  654. }
  655. _this3.sharedStrings.push(richText.length ? {
  656. richText: richText
  657. } : text);
  658. _context7.next = 97;
  659. break;
  660. case 94:
  661. if (!(_this3.options.sharedStrings === 'emit')) {
  662. _context7.next = 97;
  663. break;
  664. }
  665. _context7.next = 97;
  666. return {
  667. index: index++,
  668. text: richText.length ? {
  669. richText: richText
  670. } : text
  671. };
  672. case 97:
  673. richText = [];
  674. font = null;
  675. text = null;
  676. return _context7.abrupt("break", 101);
  677. case 101:
  678. _context7.next = 28;
  679. break;
  680. case 103:
  681. _context7.next = 108;
  682. break;
  683. case 105:
  684. _context7.prev = 105;
  685. _context7.t3 = _context7["catch"](26);
  686. _iterator5.e(_context7.t3);
  687. case 108:
  688. _context7.prev = 108;
  689. _iterator5.f();
  690. return _context7.finish(108);
  691. case 111:
  692. _iteratorNormalCompletion4 = true;
  693. _context7.next = 16;
  694. break;
  695. case 114:
  696. _context7.next = 120;
  697. break;
  698. case 116:
  699. _context7.prev = 116;
  700. _context7.t4 = _context7["catch"](14);
  701. _didIteratorError4 = true;
  702. _iteratorError4 = _context7.t4;
  703. case 120:
  704. _context7.prev = 120;
  705. _context7.prev = 121;
  706. if (!(!_iteratorNormalCompletion4 && _iterator4.return != null)) {
  707. _context7.next = 125;
  708. break;
  709. }
  710. _context7.next = 125;
  711. return _awaitAsyncGenerator(_iterator4.return());
  712. case 125:
  713. _context7.prev = 125;
  714. if (!_didIteratorError4) {
  715. _context7.next = 128;
  716. break;
  717. }
  718. throw _iteratorError4;
  719. case 128:
  720. return _context7.finish(125);
  721. case 129:
  722. return _context7.finish(120);
  723. case 130:
  724. case "end":
  725. return _context7.stop();
  726. }
  727. }
  728. }, _callee6, null, [[14, 116, 120, 130], [26, 105, 108, 111], [121,, 125, 129]]);
  729. }))();
  730. }
  731. }, {
  732. key: "_parseStyles",
  733. value: function () {
  734. var _parseStyles2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee7(entry) {
  735. return regeneratorRuntime.wrap(function _callee7$(_context8) {
  736. while (1) {
  737. switch (_context8.prev = _context8.next) {
  738. case 0:
  739. this._emitEntry({
  740. type: 'styles'
  741. });
  742. if (!(this.options.styles === 'cache')) {
  743. _context8.next = 5;
  744. break;
  745. }
  746. this.styles = new StyleManager();
  747. _context8.next = 5;
  748. return this.styles.parseStream(iterateStream(entry));
  749. case 5:
  750. case "end":
  751. return _context8.stop();
  752. }
  753. }
  754. }, _callee7, this);
  755. }));
  756. function _parseStyles(_x5) {
  757. return _parseStyles2.apply(this, arguments);
  758. }
  759. return _parseStyles;
  760. }()
  761. }, {
  762. key: "_parseWorksheet",
  763. value: /*#__PURE__*/regeneratorRuntime.mark(function _parseWorksheet(iterator, sheetNo) {
  764. var worksheetReader, matchingRel, matchingSheet;
  765. return regeneratorRuntime.wrap(function _parseWorksheet$(_context9) {
  766. while (1) {
  767. switch (_context9.prev = _context9.next) {
  768. case 0:
  769. this._emitEntry({
  770. type: 'worksheet',
  771. id: sheetNo
  772. });
  773. worksheetReader = new WorksheetReader({
  774. workbook: this,
  775. id: sheetNo,
  776. iterator: iterator,
  777. options: this.options
  778. });
  779. matchingRel = (this.workbookRels || []).find(function (rel) {
  780. return rel.Target === "worksheets/sheet".concat(sheetNo, ".xml");
  781. });
  782. matchingSheet = matchingRel && (this.model.sheets || []).find(function (sheet) {
  783. return sheet.rId === matchingRel.Id;
  784. });
  785. if (matchingSheet) {
  786. worksheetReader.id = matchingSheet.id;
  787. worksheetReader.name = matchingSheet.name;
  788. worksheetReader.state = matchingSheet.state;
  789. }
  790. if (!(this.options.worksheets === 'emit')) {
  791. _context9.next = 8;
  792. break;
  793. }
  794. _context9.next = 8;
  795. return {
  796. eventType: 'worksheet',
  797. value: worksheetReader
  798. };
  799. case 8:
  800. case "end":
  801. return _context9.stop();
  802. }
  803. }
  804. }, _parseWorksheet, this);
  805. })
  806. }, {
  807. key: "_parseHyperlinks",
  808. value: /*#__PURE__*/regeneratorRuntime.mark(function _parseHyperlinks(iterator, sheetNo) {
  809. var hyperlinksReader;
  810. return regeneratorRuntime.wrap(function _parseHyperlinks$(_context10) {
  811. while (1) {
  812. switch (_context10.prev = _context10.next) {
  813. case 0:
  814. this._emitEntry({
  815. type: 'hyperlinks',
  816. id: sheetNo
  817. });
  818. hyperlinksReader = new HyperlinkReader({
  819. workbook: this,
  820. id: sheetNo,
  821. iterator: iterator,
  822. options: this.options
  823. });
  824. if (!(this.options.hyperlinks === 'emit')) {
  825. _context10.next = 5;
  826. break;
  827. }
  828. _context10.next = 5;
  829. return {
  830. eventType: 'hyperlinks',
  831. value: hyperlinksReader
  832. };
  833. case 5:
  834. case "end":
  835. return _context10.stop();
  836. }
  837. }
  838. }, _parseHyperlinks, this);
  839. })
  840. }]);
  841. return WorkbookReader;
  842. }(EventEmitter); // for reference - these are the valid values for options
  843. WorkbookReader.Options = {
  844. worksheets: ['emit', 'ignore'],
  845. sharedStrings: ['cache', 'emit', 'ignore'],
  846. hyperlinks: ['cache', 'emit', 'ignore'],
  847. styles: ['cache', 'ignore'],
  848. entries: ['emit', 'ignore']
  849. };
  850. module.exports = WorkbookReader;
  851. //# sourceMappingURL=workbook-reader.js.map