Front end of the Slack clone application.

file-writer.js 2.5KB

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