all files / lib/ proxy-reader.js

87.23% Statements 41/47
42.86% Branches 6/14
66.67% Functions 6/9
87.23% Lines 41/47
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96                                                                              33×                                
// A reader for when we don't yet know what kind of thing
// the thing is.
 
module.exports = ProxyReader
 
var Reader = require('./reader.js')
var getType = require('./get-type.js')
var inherits = require('inherits')
var fs = require('graceful-fs')
 
inherits(ProxyReader, Reader)
 
function ProxyReader (props) {
  var self = this
  Iif (!(self instanceof ProxyReader)) {
    throw new Error('ProxyReader must be called as constructor.')
  }
 
  self.props = props
  self._buffer = []
  self.ready = false
 
  Reader.call(self, props)
}
 
ProxyReader.prototype._stat = function () {
  var self = this
  var props = self.props
  // stat the thing to see what the proxy should be.
  var stat = props.follow ? 'stat' : 'lstat'
 
  fs[stat](props.path, function (er, current) {
    var type
    Iif (er || !current) {
      type = 'File'
    } else {
      type = getType(current)
    }
 
    props[type] = true
    props.type = self.type = type
 
    self._old = current
    self._addProxy(Reader(props, current))
  })
}
 
ProxyReader.prototype._addProxy = function (proxy) {
  var self = this
  Iif (self._proxyTarget) {
    return self.error('proxy already set')
  }
 
  self._proxyTarget = proxy
  proxy._proxy = self
 
  ;[
    'error',
    'data',
    'end',
    'close',
    'linkpath',
    'entry',
    'entryEnd',
    'child',
    'childEnd',
    'warn',
    'stat'
  ].forEach(function (ev) {
    // console.error('~~ proxy event', ev, self.path)
    proxy.on(ev, self.emit.bind(self, ev))
  })
 
  self.emit('proxy', proxy)
 
  proxy.on('ready', function () {
    // console.error("~~ proxy is ready!", self.path)
    self.ready = true
    self.emit('ready')
  })
 
  var calls = self._buffer
  self._buffer.length = 0
  calls.forEach(function (c) {
    proxy[c[0]].apply(proxy, c[1])
  })
}
 
ProxyReader.prototype.pause = function () {
  return this._proxyTarget ? this._proxyTarget.pause() : false
}
 
ProxyReader.prototype.resume = function () {
  return this._proxyTarget ? this._proxyTarget.resume() : false
}