UNPKG

unzipper-esm

Version:
157 lines (153 loc) 7.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _stream = require("stream"); 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 _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); } function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); } function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } // 'node:stream' // It's not clear why did they extract a string into a variable. var FUNCTION_STRING = 'function'; var PullStream = exports["default"] = /*#__PURE__*/function (_Duplex) { function PullStream() { var _this; _classCallCheck(this, PullStream); _this = _callSuper(this, PullStream, [{ decodeStrings: false, objectMode: true }]); _this.buffer = Buffer.from(''); var self = _this; self.on('finish', function () { self.finished = true; self.emit('chunk', false); }); return _this; } _inherits(PullStream, _Duplex); return _createClass(PullStream, [{ key: "_write", value: function _write(chunk, e, cb) { this.buffer = Buffer.concat([this.buffer, chunk]); this.cb = cb; this.emit('chunk'); } }, { key: "stream", value: // The `eof` parameter is interpreted as `file_length` if the type is number // otherwise (i.e. buffer) it is interpreted as a pattern signaling end of stream function stream(eof, includeEof) { var p = new _stream.PassThrough(); var done; var self = this; function cb() { if (_typeof(self.cb) === FUNCTION_STRING) { var callback = self.cb; self.cb = undefined; return callback(); } } function pull() { var packet; if (self.buffer && self.buffer.length) { if (typeof eof === 'number') { packet = self.buffer.slice(0, eof); self.buffer = self.buffer.slice(eof); eof -= packet.length; done = done || !eof; } else { var match = self.buffer.indexOf(eof); if (match !== -1) { // store signature match byte offset to allow us to reference // this for zip64 offset self.match = match; if (includeEof) match = match + eof.length; packet = self.buffer.slice(0, match); self.buffer = self.buffer.slice(match); done = true; } else { var len = self.buffer.length - eof.length; if (len <= 0) { cb(); } else { packet = self.buffer.slice(0, len); self.buffer = self.buffer.slice(len); } } } if (packet) p.write(packet, function () { if (self.buffer.length === 0 || eof.length && self.buffer.length <= eof.length) cb(); }); } if (!done) { if (self.finished) { self.removeListener('chunk', pull); self.emit('error', new Error('FILE_ENDED')); return; } } else { self.removeListener('chunk', pull); p.end(); } } self.on('chunk', pull); pull(); return p; } }, { key: "pull", value: function pull(eof, includeEof) { if (eof === 0) return Promise.resolve(''); // If we already have the required data in buffer // we can resolve the request immediately if (!isNaN(eof) && this.buffer.length > eof) { var data = this.buffer.slice(0, eof); this.buffer = this.buffer.slice(eof); return Promise.resolve(data); } // Otherwise we stream until we have it var buffer = Buffer.from(''); var self = this; var concatStream = new _stream.Transform({ transform: function transform(d, e, cb) { buffer = Buffer.concat([buffer, d]); cb(); } }); var rejectHandler; var pullStreamRejectHandler; return new Promise(function (resolve, reject) { rejectHandler = reject; pullStreamRejectHandler = function pullStreamRejectHandler(e) { self.__emittedError = e; reject(e); }; if (self.finished) return reject(new Error('FILE_ENDED')); self.once('error', pullStreamRejectHandler); // reject any errors from pullstream itself self.stream(eof, includeEof).on('error', reject).pipe(concatStream).on('finish', function () { resolve(buffer); }).on('error', reject); })["finally"](function () { self.removeListener('error', rejectHandler); self.removeListener('error', pullStreamRejectHandler); }); } }, { key: "_read", value: function _read() {} }]); }(_stream.Duplex); //# sourceMappingURL=PullStream.js.map