zip-stream.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 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); } }
  4. 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); }); }; }
  5. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  6. 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); } }
  7. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  8. 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); }
  9. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  10. 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); }; }
  11. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  12. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  13. 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; } }
  14. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  15. var events = require('events');
  16. var JSZip = require('jszip');
  17. var StreamBuf = require('./stream-buf');
  18. var _require = require('./browser-buffer-encode'),
  19. stringToBuffer = _require.stringToBuffer; // =============================================================================
  20. // The ZipWriter class
  21. // Packs streamed data into an output zip stream
  22. var ZipWriter = /*#__PURE__*/function (_events$EventEmitter) {
  23. _inherits(ZipWriter, _events$EventEmitter);
  24. var _super = _createSuper(ZipWriter);
  25. function ZipWriter(options) {
  26. var _this;
  27. _classCallCheck(this, ZipWriter);
  28. _this = _super.call(this);
  29. _this.options = Object.assign({
  30. type: 'nodebuffer',
  31. compression: 'DEFLATE'
  32. }, options);
  33. _this.zip = new JSZip();
  34. _this.stream = new StreamBuf();
  35. return _this;
  36. }
  37. _createClass(ZipWriter, [{
  38. key: "append",
  39. value: function append(data, options) {
  40. if (options.hasOwnProperty('base64') && options.base64) {
  41. this.zip.file(options.name, data, {
  42. base64: true
  43. });
  44. } else {
  45. // https://www.npmjs.com/package/process
  46. if (process.browser && typeof data === 'string') {
  47. // use TextEncoder in browser
  48. data = stringToBuffer(data);
  49. }
  50. this.zip.file(options.name, data);
  51. }
  52. }
  53. }, {
  54. key: "finalize",
  55. value: function () {
  56. var _finalize = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
  57. var content;
  58. return regeneratorRuntime.wrap(function _callee$(_context) {
  59. while (1) {
  60. switch (_context.prev = _context.next) {
  61. case 0:
  62. _context.next = 2;
  63. return this.zip.generateAsync(this.options);
  64. case 2:
  65. content = _context.sent;
  66. this.stream.end(content);
  67. this.emit('finish');
  68. case 5:
  69. case "end":
  70. return _context.stop();
  71. }
  72. }
  73. }, _callee, this);
  74. }));
  75. function finalize() {
  76. return _finalize.apply(this, arguments);
  77. }
  78. return finalize;
  79. }() // ==========================================================================
  80. // Stream.Readable interface
  81. }, {
  82. key: "read",
  83. value: function read(size) {
  84. return this.stream.read(size);
  85. }
  86. }, {
  87. key: "setEncoding",
  88. value: function setEncoding(encoding) {
  89. return this.stream.setEncoding(encoding);
  90. }
  91. }, {
  92. key: "pause",
  93. value: function pause() {
  94. return this.stream.pause();
  95. }
  96. }, {
  97. key: "resume",
  98. value: function resume() {
  99. return this.stream.resume();
  100. }
  101. }, {
  102. key: "isPaused",
  103. value: function isPaused() {
  104. return this.stream.isPaused();
  105. }
  106. }, {
  107. key: "pipe",
  108. value: function pipe(destination, options) {
  109. return this.stream.pipe(destination, options);
  110. }
  111. }, {
  112. key: "unpipe",
  113. value: function unpipe(destination) {
  114. return this.stream.unpipe(destination);
  115. }
  116. }, {
  117. key: "unshift",
  118. value: function unshift(chunk) {
  119. return this.stream.unshift(chunk);
  120. }
  121. }, {
  122. key: "wrap",
  123. value: function wrap(stream) {
  124. return this.stream.wrap(stream);
  125. }
  126. }]);
  127. return ZipWriter;
  128. }(events.EventEmitter); // =============================================================================
  129. module.exports = {
  130. ZipWriter: ZipWriter
  131. };
  132. //# sourceMappingURL=zip-stream.js.map