All files index.js

100% Statements 15/15
83.33% Branches 5/6
100% Functions 6/6
100% Lines 15/15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79                                    1x   1x                               1x   1x     1x                         3x   3x 2x   2x       3x   2x   2x   2x 2x   2x            
// @flow
 
import {PassThrough} from 'stream';
 
import type {IUploadTarball, IReadTarball} from '@verdaccio/streams';
 
/**
 * This stream is used to read tarballs from repository.
 * @param {*} options
 * @return {Stream}
 */
class ReadTarball extends PassThrough {
 
  /**
   *
   * @param {Object} options
   */
  constructor(options: duplexStreamOptions): IReadTarball {
    super(options);
    // called when data is not needed anymore
    addAbstractMethods(this, 'abort');
  }
}
 
/**
 * This stream is used to upload tarballs to a repository.
 * @param {*} options
 * @return {Stream}
 */
class UploadTarball extends PassThrough {
 
  /**
   *
   * @param {Object} options
   */
  constructor(options: duplexStreamOptions): IUploadTarball {
    super(options);
    // called when user closes connection before upload finishes
    addAbstractMethods(this, 'abort');
 
    // called when upload finishes successfully
    addAbstractMethods(this, 'done');
  }
}
 
/**
 * This function intercepts abstract calls and replays them allowing.
 * us to attach those functions after we are ready to do so
 * @param {*} self
 * @param {*} name
 */
// Perhaps someone knows a better way to write this
function addAbstractMethods(self, name) {
  // $FlowFixMe
  self._called_methods = self._called_methods || {};
  // $FlowFixMe
  self.__defineGetter__(name, function() {
    return function() {
      // $FlowFixMe
      self._called_methods[name] = true;
    };
  });
  // $FlowFixMe
  self.__defineSetter__(name, function(fn) {
    // $FlowFixMe
    delete self[name];
    // $FlowFixMe
    self[name] = fn;
    // $FlowFixMe
    Eif (self._called_methods && self._called_methods[name]) {
      delete self._called_methods[name];
      // $FlowFixMe
      self[name]();
    }
  });
}
 
export {ReadTarball, UploadTarball};