archive-output-stream.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * node-compress-commons
  3. *
  4. * Copyright (c) 2014 Chris Talkington, contributors.
  5. * Licensed under the MIT license.
  6. * https://github.com/archiverjs/node-compress-commons/blob/master/LICENSE-MIT
  7. */
  8. var inherits = require('util').inherits;
  9. var isStream = require('is-stream');
  10. var Transform = require('readable-stream').Transform;
  11. var ArchiveEntry = require('./archive-entry');
  12. var util = require('../util');
  13. var ArchiveOutputStream = module.exports = function(options) {
  14. if (!(this instanceof ArchiveOutputStream)) {
  15. return new ArchiveOutputStream(options);
  16. }
  17. Transform.call(this, options);
  18. this.offset = 0;
  19. this._archive = {
  20. finish: false,
  21. finished: false,
  22. processing: false
  23. };
  24. };
  25. inherits(ArchiveOutputStream, Transform);
  26. ArchiveOutputStream.prototype._appendBuffer = function(zae, source, callback) {
  27. // scaffold only
  28. };
  29. ArchiveOutputStream.prototype._appendStream = function(zae, source, callback) {
  30. // scaffold only
  31. };
  32. ArchiveOutputStream.prototype._emitErrorCallback = function(err) {
  33. if (err) {
  34. this.emit('error', err);
  35. }
  36. };
  37. ArchiveOutputStream.prototype._finish = function(ae) {
  38. // scaffold only
  39. };
  40. ArchiveOutputStream.prototype._normalizeEntry = function(ae) {
  41. // scaffold only
  42. };
  43. ArchiveOutputStream.prototype._transform = function(chunk, encoding, callback) {
  44. callback(null, chunk);
  45. };
  46. ArchiveOutputStream.prototype.entry = function(ae, source, callback) {
  47. source = source || null;
  48. if (typeof callback !== 'function') {
  49. callback = this._emitErrorCallback.bind(this);
  50. }
  51. if (!(ae instanceof ArchiveEntry)) {
  52. callback(new Error('not a valid instance of ArchiveEntry'));
  53. return;
  54. }
  55. if (this._archive.finish || this._archive.finished) {
  56. callback(new Error('unacceptable entry after finish'));
  57. return;
  58. }
  59. if (this._archive.processing) {
  60. callback(new Error('already processing an entry'));
  61. return;
  62. }
  63. this._archive.processing = true;
  64. this._normalizeEntry(ae);
  65. this._entry = ae;
  66. source = util.normalizeInputSource(source);
  67. if (Buffer.isBuffer(source)) {
  68. this._appendBuffer(ae, source, callback);
  69. } else if (isStream(source)) {
  70. this._appendStream(ae, source, callback);
  71. } else {
  72. this._archive.processing = false;
  73. callback(new Error('input source must be valid Stream or Buffer instance'));
  74. return;
  75. }
  76. return this;
  77. };
  78. ArchiveOutputStream.prototype.finish = function() {
  79. if (this._archive.processing) {
  80. this._archive.finish = true;
  81. return;
  82. }
  83. this._finish();
  84. };
  85. ArchiveOutputStream.prototype.getBytesWritten = function() {
  86. return this.offset;
  87. };
  88. ArchiveOutputStream.prototype.write = function(chunk, cb) {
  89. if (chunk) {
  90. this.offset += chunk.length;
  91. }
  92. return Transform.prototype.write.call(this, chunk, cb);
  93. };