UNPKG

tar-iterator

Version:

Extract contents from tar archive type using an iterator API using streams or paths. Use stream interface and pipe transforms to add decompression algorithms

195 lines 7.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "default", { enumerable: true, get: function() { return TarIterator; } }); var _calloncefn = /*#__PURE__*/ _interop_require_default(require("call-once-fn")); var _extractbaseiterator = /*#__PURE__*/ _interop_require_wildcard(require("extract-base-iterator")); var _gracefulfs = /*#__PURE__*/ _interop_require_default(require("graceful-fs")); var _nextEntryts = /*#__PURE__*/ _interop_require_default(require("./nextEntry.js")); var _TarExtractts = /*#__PURE__*/ _interop_require_default(require("./tar/TarExtract.js")); function _assert_this_initialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _call_super(_this, derived, args) { derived = _get_prototype_of(derived); return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args)); } function _class_call_check(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _get_prototype_of(o) { _get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _get_prototype_of(o); } 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) _set_prototype_of(subClass, superClass); } function _interop_require_default(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interop_require_wildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = { __proto__: null }; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for(var key in obj){ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _possible_constructor_return(self, call) { if (call && (_type_of(call) === "object" || typeof call === "function")) { return call; } return _assert_this_initialized(self); } function _set_prototype_of(o, p) { _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _set_prototype_of(o, p); } function _type_of(obj) { "@swc/helpers - typeof"; return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; } function _is_native_reflect_construct() { try { var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {})); } catch (_) {} return (_is_native_reflect_construct = function() { return !!result; })(); } var TarIterator = /*#__PURE__*/ function(BaseIterator) { "use strict"; _inherits(TarIterator, BaseIterator); function TarIterator(source) { var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {}; _class_call_check(this, TarIterator); var _this; _this = _call_super(this, TarIterator, [ options ]); _this.lock = new _extractbaseiterator.Lock(); _this.lock.onDestroy = function(err) { return _extractbaseiterator.default.prototype.end.call(_this, err); }; var cancelled = false; var setup = function() { cancelled = true; }; _this.processing.push(setup); // Use our pure TarExtract instead of tar-stream-compat // Note: options passed here are ExtractOptions (strip, force, etc.) // TarExtract uses TarExtractOptions (filenameEncoding, etc.) which is different _this.extract = new _TarExtractts.default(); var pipe = function(cb) { try { if (typeof source === 'string') source = _gracefulfs.default.createReadStream(source); } catch (err) { cb(err); } // Register cleanup for source stream var stream = source; _this.lock.registerCleanup(function() { var s = stream; if (typeof s.destroy === 'function') s.destroy(); }); var end = (0, _calloncefn.default)(cb); var self = _this; var firstData = true; source.on('data', function onData(chunk) { try { if (self.extract) self.extract.write(chunk); if (firstData) { firstData = false; end(); } } catch (err) { // Handle synchronous errors from TarExtract (e.g., invalid format) end(err); } }); source.on('error', end); source.on('end', function onEnd() { if (self.extract) self.extract.end(); }); }; pipe(function(err) { _this.processing.remove(setup); if (_this.done || cancelled) return; // done err ? _this.end(err) : _this.push(_nextEntryts.default.bind(null, null)); }); return _this; } var _proto = TarIterator.prototype; _proto.end = function end(err) { var lock = this.lock; if (lock) { this.lock = null; // Clear FIRST to prevent re-entrancy lock.err = err; lock.release(); // Lock.__destroy() handles BaseIterator.end() } this.extract = null; }; return TarIterator; }(_extractbaseiterator.default); /* CJS INTEROP */ if (exports.__esModule && exports.default) { try { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { exports.default[key] = exports[key]; } } catch (_) {}; module.exports = exports.default; }