"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/fstream/lib/file-writer.js" (7 Feb 2017, 2593 Bytes) of archive /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 module.exports = FileWriter
    2 
    3 var fs = require('graceful-fs')
    4 var Writer = require('./writer.js')
    5 var inherits = require('inherits')
    6 var EOF = {}
    7 
    8 inherits(FileWriter, Writer)
    9 
   10 function FileWriter (props) {
   11   var self = this
   12   if (!(self instanceof FileWriter)) {
   13     throw new Error('FileWriter must be called as constructor.')
   14   }
   15 
   16   // should already be established as a File type
   17   if (props.type !== 'File' || !props.File) {
   18     throw new Error('Non-file type ' + props.type)
   19   }
   20 
   21   self._buffer = []
   22   self._bytesWritten = 0
   23 
   24   Writer.call(this, props)
   25 }
   26 
   27 FileWriter.prototype._create = function () {
   28   var self = this
   29   if (self._stream) return
   30 
   31   var so = {}
   32   if (self.props.flags) so.flags = self.props.flags
   33   so.mode = Writer.filemode
   34   if (self._old && self._old.blksize) so.bufferSize = self._old.blksize
   35 
   36   self._stream = fs.createWriteStream(self._path, so)
   37 
   38   self._stream.on('open', function () {
   39     // console.error("FW open", self._buffer, self._path)
   40     self.ready = true
   41     self._buffer.forEach(function (c) {
   42       if (c === EOF) self._stream.end()
   43       else self._stream.write(c)
   44     })
   45     self.emit('ready')
   46     // give this a kick just in case it needs it.
   47     self.emit('drain')
   48   })
   49 
   50   self._stream.on('error', function (er) { self.emit('error', er) })
   51 
   52   self._stream.on('drain', function () { self.emit('drain') })
   53 
   54   self._stream.on('close', function () {
   55     // console.error('\n\nFW Stream Close', self._path, self.size)
   56     self._finish()
   57   })
   58 }
   59 
   60 FileWriter.prototype.write = function (c) {
   61   var self = this
   62 
   63   self._bytesWritten += c.length
   64 
   65   if (!self.ready) {
   66     if (!Buffer.isBuffer(c) && typeof c !== 'string') {
   67       throw new Error('invalid write data')
   68     }
   69     self._buffer.push(c)
   70     return false
   71   }
   72 
   73   var ret = self._stream.write(c)
   74   // console.error('\t-- fw wrote, _stream says', ret, self._stream._queue.length)
   75 
   76   // allow 2 buffered writes, because otherwise there's just too
   77   // much stop and go bs.
   78   if (ret === false && self._stream._queue) {
   79     return self._stream._queue.length <= 2
   80   } else {
   81     return ret
   82   }
   83 }
   84 
   85 FileWriter.prototype.end = function (c) {
   86   var self = this
   87 
   88   if (c) self.write(c)
   89 
   90   if (!self.ready) {
   91     self._buffer.push(EOF)
   92     return false
   93   }
   94 
   95   return self._stream.end()
   96 }
   97 
   98 FileWriter.prototype._finish = function () {
   99   var self = this
  100   if (typeof self.size === 'number' && self._bytesWritten !== self.size) {
  101     self.error(
  102       'Did not get expected byte count.\n' +
  103       'expect: ' + self.size + '\n' +
  104       'actual: ' + self._bytesWritten)
  105   }
  106   Writer.prototype._finish.call(self)
  107 }