UI for Zipcoin Blue

tar.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * TAR Format Plugin
  3. *
  4. * @module plugins/tar
  5. * @license [MIT]{@link https://github.com/archiverjs/node-archiver/blob/master/LICENSE}
  6. * @copyright (c) 2012-2014 Chris Talkington, contributors.
  7. */
  8. var zlib = require('zlib');
  9. var engine = require('tar-stream');
  10. var util = require('archiver-utils');
  11. /**
  12. * @constructor
  13. * @param {TarOptions} options
  14. */
  15. var Tar = function(options) {
  16. if (!(this instanceof Tar)) {
  17. return new Tar(options);
  18. }
  19. options = this.options = util.defaults(options, {
  20. gzip: false
  21. });
  22. if (typeof options.gzipOptions !== 'object') {
  23. options.gzipOptions = {};
  24. }
  25. this.supports = {
  26. directory: true,
  27. symlink: true
  28. };
  29. this.engine = engine.pack(options);
  30. this.compressor = false;
  31. if (options.gzip) {
  32. this.compressor = zlib.createGzip(options.gzipOptions);
  33. this.compressor.on('error', this._onCompressorError.bind(this));
  34. }
  35. };
  36. /**
  37. * [_onCompressorError description]
  38. *
  39. * @private
  40. * @param {Error} err
  41. * @return void
  42. */
  43. Tar.prototype._onCompressorError = function(err) {
  44. this.engine.emit('error', err);
  45. };
  46. /**
  47. * [append description]
  48. *
  49. * @param {(Buffer|Stream)} source
  50. * @param {TarEntryData} data
  51. * @param {Function} callback
  52. * @return void
  53. */
  54. Tar.prototype.append = function(source, data, callback) {
  55. var self = this;
  56. data.mtime = data.date;
  57. function append(err, sourceBuffer) {
  58. if (err) {
  59. callback(err);
  60. return;
  61. }
  62. self.engine.entry(data, sourceBuffer, function(err) {
  63. callback(err, data);
  64. });
  65. }
  66. if (data.sourceType === 'buffer') {
  67. append(null, source);
  68. } else if (data.sourceType === 'stream' && data._stats) {
  69. data.size = data._stats.size;
  70. var entry = self.engine.entry(data, function(err) {
  71. callback(err, data);
  72. });
  73. source.pipe(entry);
  74. } else if (data.sourceType === 'stream') {
  75. util.collectStream(source, append);
  76. }
  77. };
  78. /**
  79. * [finalize description]
  80. *
  81. * @return void
  82. */
  83. Tar.prototype.finalize = function() {
  84. this.engine.finalize();
  85. };
  86. /**
  87. * [on description]
  88. *
  89. * @return this.engine
  90. */
  91. Tar.prototype.on = function() {
  92. return this.engine.on.apply(this.engine, arguments);
  93. };
  94. /**
  95. * [pipe description]
  96. *
  97. * @param {String} destination
  98. * @param {Object} options
  99. * @return this.engine
  100. */
  101. Tar.prototype.pipe = function(destination, options) {
  102. if (this.compressor) {
  103. return this.engine.pipe.apply(this.engine, [this.compressor]).pipe(destination, options);
  104. } else {
  105. return this.engine.pipe.apply(this.engine, arguments);
  106. }
  107. };
  108. /**
  109. * [unpipe description]
  110. *
  111. * @return this.engine
  112. */
  113. Tar.prototype.unpipe = function() {
  114. if (this.compressor) {
  115. return this.compressor.unpipe.apply(this.compressor, arguments);
  116. } else {
  117. return this.engine.unpipe.apply(this.engine, arguments);
  118. }
  119. };
  120. module.exports = Tar;
  121. /**
  122. * @typedef {Object} TarOptions
  123. * @global
  124. * @property {Boolean} [gzip=false] Compress the tar archive using gzip.
  125. * @property {Object} [gzipOptions] Passed to [zlib]{@link https://nodejs.org/api/zlib.html#zlib_class_options}
  126. * to control compression.
  127. * @property {*} [*] See [tar-stream]{@link https://github.com/mafintosh/tar-stream} documentation for additional properties.
  128. */
  129. /**
  130. * @typedef {Object} TarEntryData
  131. * @global
  132. * @property {String} name Sets the entry name including internal path.
  133. * @property {(String|Date)} [date=NOW()] Sets the entry date.
  134. * @property {Number} [mode=D:0755/F:0644] Sets the entry permissions.
  135. * @property {String} [prefix] Sets a path prefix for the entry name. Useful
  136. * when working with methods like `directory` or `glob`.
  137. * @property {fs.Stats} [stats] Sets the fs stat data for this entry allowing
  138. * for reduction of fs stat calls when stat data is already known.
  139. */
  140. /**
  141. * TarStream Module
  142. * @external TarStream
  143. * @see {@link https://github.com/mafintosh/tar-stream}
  144. */