"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/fstream/lib/file-reader.js" (11 Apr 2017, 3984 Bytes) of package /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Basically just a wrapper around an fs.ReadStream
    2 
    3 module.exports = FileReader
    4 
    5 var fs = require('graceful-fs')
    6 var inherits = require('inherits')
    7 var Reader = require('./reader.js')
    8 var EOF = {EOF: true}
    9 var CLOSE = {CLOSE: true}
   10 
   11 inherits(FileReader, Reader)
   12 
   13 function FileReader (props) {
   14   // console.error("    FR create", props.path, props.size, new Error().stack)
   15   var self = this
   16   if (!(self instanceof FileReader)) {
   17     throw new Error('FileReader must be called as constructor.')
   18   }
   19 
   20   // should already be established as a File type
   21   // XXX Todo: preserve hardlinks by tracking dev+inode+nlink,
   22   // with a HardLinkReader class.
   23   if (!((props.type === 'Link' && props.Link) ||
   24     (props.type === 'File' && props.File))) {
   25     throw new Error('Non-file type ' + props.type)
   26   }
   27 
   28   self._buffer = []
   29   self._bytesEmitted = 0
   30   Reader.call(self, props)
   31 }
   32 
   33 FileReader.prototype._getStream = function () {
   34   var self = this
   35   var stream = self._stream = fs.createReadStream(self._path, self.props)
   36 
   37   if (self.props.blksize) {
   38     stream.bufferSize = self.props.blksize
   39   }
   40 
   41   stream.on('open', self.emit.bind(self, 'open'))
   42 
   43   stream.on('data', function (c) {
   44     // console.error('\t\t%d %s', c.length, self.basename)
   45     self._bytesEmitted += c.length
   46     // no point saving empty chunks
   47     if (!c.length) {
   48       return
   49     } else if (self._paused || self._buffer.length) {
   50       self._buffer.push(c)
   51       self._read()
   52     } else self.emit('data', c)
   53   })
   54 
   55   stream.on('end', function () {
   56     if (self._paused || self._buffer.length) {
   57       // console.error('FR Buffering End', self._path)
   58       self._buffer.push(EOF)
   59       self._read()
   60     } else {
   61       self.emit('end')
   62     }
   63 
   64     if (self._bytesEmitted !== self.props.size) {
   65       self.error("Didn't get expected byte count\n" +
   66         'expect: ' + self.props.size + '\n' +
   67         'actual: ' + self._bytesEmitted)
   68     }
   69   })
   70 
   71   stream.on('close', function () {
   72     if (self._paused || self._buffer.length) {
   73       // console.error('FR Buffering Close', self._path)
   74       self._buffer.push(CLOSE)
   75       self._read()
   76     } else {
   77       // console.error('FR close 1', self._path)
   78       self.emit('close')
   79     }
   80   })
   81 
   82   stream.on('error', function (e) {
   83     self.emit('error', e)
   84   })
   85 
   86   self._read()
   87 }
   88 
   89 FileReader.prototype._read = function () {
   90   var self = this
   91   // console.error('FR _read', self._path)
   92   if (self._paused) {
   93     // console.error('FR _read paused', self._path)
   94     return
   95   }
   96 
   97   if (!self._stream) {
   98     // console.error('FR _getStream calling', self._path)
   99     return self._getStream()
  100   }
  101 
  102   // clear out the buffer, if there is one.
  103   if (self._buffer.length) {
  104     // console.error('FR _read has buffer', self._buffer.length, self._path)
  105     var buf = self._buffer
  106     for (var i = 0, l = buf.length; i < l; i++) {
  107       var c = buf[i]
  108       if (c === EOF) {
  109         // console.error('FR Read emitting buffered end', self._path)
  110         self.emit('end')
  111       } else if (c === CLOSE) {
  112         // console.error('FR Read emitting buffered close', self._path)
  113         self.emit('close')
  114       } else {
  115         // console.error('FR Read emitting buffered data', self._path)
  116         self.emit('data', c)
  117       }
  118 
  119       if (self._paused) {
  120         // console.error('FR Read Re-pausing at '+i, self._path)
  121         self._buffer = buf.slice(i)
  122         return
  123       }
  124     }
  125     self._buffer.length = 0
  126   }
  127 // console.error("FR _read done")
  128 // that's about all there is to it.
  129 }
  130 
  131 FileReader.prototype.pause = function (who) {
  132   var self = this
  133   // console.error('FR Pause', self._path)
  134   if (self._paused) return
  135   who = who || self
  136   self._paused = true
  137   if (self._stream) self._stream.pause()
  138   self.emit('pause', who)
  139 }
  140 
  141 FileReader.prototype.resume = function (who) {
  142   var self = this
  143   // console.error('FR Resume', self._path)
  144   if (!self._paused) return
  145   who = who || self
  146   self.emit('resume', who)
  147   self._paused = false
  148   if (self._stream) self._stream.resume()
  149   self._read()
  150 }