UNPKG

unzipper-esm

Version:
302 lines (297 loc) 19.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = pMap; function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); } function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); } r ? i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n : (o("next", 0), o("throw", 1), o("return", 2)); }, _regeneratorDefine2(e, r, n, t); } function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, 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 o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } 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 asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); } function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; } // This code was copy-pasted from `p-map` on Jun 19th, 2026: // https://github.com/sindresorhus/p-map/blob/main/index.js // Rationale: // // * `p-map` doesn't support CommonJS environment. // https://gitlab.com/catamphetamine/read-excel-file/-/work_items/113 // // * `p-map` supports Node.js version >= 18 // while `unzipper` supports Node.js version >= 8. // // * Removed used of `Symbol.asyncIterator` // because `Symbol.asyncIterator` has been natively supported without flags since Node.js version `10.0.0`. // // * Replaced `AggregateError` with just `Error` // because the `AggregateError` object was officially added to Node.js in version `15.0.0`. // // * Removed function `pMapIterable()` because it's not used. // // Maps an `iterable` of `Promise`s with an optional `concurrency` cap. // // Resolves when all the promises resolve. // // If any of the promises reject, it either stops and rejects (default behavior) // or waits for all promises in the `iterable` to resolve or reject, and then rejects // (when passing `stopOnError: false`). // function pMap(_x, _x2) { return _pMap.apply(this, arguments); } function _pMap() { _pMap = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(iterable, mapper) { var _ref, _ref$concurrency, concurrency, _ref$stopOnError, stopOnError, _args4 = arguments; return _regenerator().w(function (_context4) { while (1) switch (_context4.n) { case 0: _ref = _args4.length > 2 && _args4[2] !== undefined ? _args4[2] : {}, _ref$concurrency = _ref.concurrency, concurrency = _ref$concurrency === void 0 ? Number.POSITIVE_INFINITY : _ref$concurrency, _ref$stopOnError = _ref.stopOnError, stopOnError = _ref$stopOnError === void 0 ? true : _ref$stopOnError; return _context4.a(2, new Promise(function (resolve_, reject_) { if (iterable[Symbol.iterator] === undefined) { throw new TypeError("Expected `input` to be an `Iterable`, got (".concat(_typeof(iterable), ")")); } if (typeof mapper !== 'function') { throw new TypeError('Mapper function is required'); } if (!(Number.isSafeInteger(concurrency) && concurrency >= 1 || concurrency === Number.POSITIVE_INFINITY)) { throw new TypeError("Expected `concurrency` to be an integer from 1 and up or `Infinity`, got `".concat(concurrency, "` (").concat(_typeof(concurrency), ")")); } var result = []; var errors = []; var skippedIndexesMap = new Map(); var isRejected = false; var isResolved = false; var isIterableDone = false; var resolvingCount = 0; var currentIndex = 0; var iterator = iterable[Symbol.iterator](); var cleanup = function cleanup() {}; var resolve = function resolve(value) { resolve_(value); cleanup(); }; var reject = function reject(reason) { isRejected = true; isResolved = true; reject_(reason); cleanup(); }; var _next2 = /*#__PURE__*/function () { var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2() { var nextItem, index, pureResult, _iterator, _step, _step$value, _index, value, _t3; return _regenerator().w(function (_context2) { while (1) switch (_context2.p = _context2.n) { case 0: if (!isResolved) { _context2.n = 1; break; } return _context2.a(2); case 1: _context2.n = 2; return iterator.next(); case 2: nextItem = _context2.v; index = currentIndex; currentIndex++; // Note: `iterator.next()` can be called many times in parallel. // This can cause multiple calls to this `next()` function to // receive a `nextItem` with `done === true`. // The shutdown logic that rejects/resolves must be protected // so it runs only one time as the `skippedIndex` logic is // non-idempotent. if (!nextItem.done) { _context2.n = 14; break; } isIterableDone = true; if (!(resolvingCount === 0 && !isResolved)) { _context2.n = 13; break; } if (!(!stopOnError && errors.length > 0)) { _context2.n = 3; break; } reject(errors[0]); return _context2.a(2); case 3: isResolved = true; if (!(skippedIndexesMap.size === 0)) { _context2.n = 4; break; } resolve(result); return _context2.a(2); case 4: pureResult = []; // Support multiple `pMapSkip`'s. _iterator = _createForOfIteratorHelper(result.entries()); _context2.p = 5; _iterator.s(); case 6: if ((_step = _iterator.n()).done) { _context2.n = 9; break; } _step$value = _slicedToArray(_step.value, 2), _index = _step$value[0], value = _step$value[1]; if (!(skippedIndexesMap.get(_index) === pMapSkip)) { _context2.n = 7; break; } return _context2.a(3, 8); case 7: pureResult.push(value); case 8: _context2.n = 6; break; case 9: _context2.n = 11; break; case 10: _context2.p = 10; _t3 = _context2.v; _iterator.e(_t3); case 11: _context2.p = 11; _iterator.f(); return _context2.f(11); case 12: resolve(pureResult); case 13: return _context2.a(2); case 14: resolvingCount++; // Intentionally detached _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() { var element, _value, _t, _t2; return _regenerator().w(function (_context) { while (1) switch (_context.p = _context.n) { case 0: _context.p = 0; _context.n = 1; return nextItem.value; case 1: element = _context.v; if (!isResolved) { _context.n = 2; break; } return _context.a(2); case 2: _context.n = 3; return mapper(element, index); case 3: _value = _context.v; // Use Map to stage the index of the element. if (_value === pMapSkip) { skippedIndexesMap.set(index, _value); } result[index] = _value; resolvingCount--; _context.n = 4; return _next2(); case 4: _context.n = 10; break; case 5: _context.p = 5; _t = _context.v; if (!stopOnError) { _context.n = 6; break; } reject(_t); _context.n = 10; break; case 6: errors.push(_t); resolvingCount--; // In that case we can't really continue regardless of `stopOnError` state // since an iterable is likely to continue throwing after it throws once. // If we continue calling `next()` indefinitely we will likely end up // in an infinite loop of failed iteration. _context.p = 7; _context.n = 8; return _next2(); case 8: _context.n = 10; break; case 9: _context.p = 9; _t2 = _context.v; reject(_t2); case 10: return _context.a(2); } }, _callee, null, [[7, 9], [0, 5]]); }))(); case 15: return _context2.a(2); } }, _callee2, null, [[5, 10, 11, 12]]); })); return function next() { return _ref2.apply(this, arguments); }; }(); // Create the concurrent runners in a detached (non-awaited) // promise. We need this so we can await the `next()` calls // to stop creating runners before hitting the concurrency limit // if the iterable has already been marked as done. // NOTE: We *must* do this for async iterators otherwise we'll spin up // infinite `next()` calls by default and never start the event loop. _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3() { var index, _t4; return _regenerator().w(function (_context3) { while (1) switch (_context3.p = _context3.n) { case 0: index = 0; case 1: if (!(index < concurrency)) { _context3.n = 7; break; } _context3.p = 2; _context3.n = 3; return _next2(); case 3: _context3.n = 5; break; case 4: _context3.p = 4; _t4 = _context3.v; reject(_t4); return _context3.a(3, 7); case 5: if (!(isIterableDone || isRejected)) { _context3.n = 6; break; } return _context3.a(3, 7); case 6: index++; _context3.n = 1; break; case 7: return _context3.a(2); } }, _callee3, null, [[2, 4]]); }))(); })); } }, _callee4); })); return _pMap.apply(this, arguments); } var pMapSkip = Symbol('skip'); //# sourceMappingURL=mapPromises.js.map