proxy-writer.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // A writer for when we don't know what kind of thing
  2. // the thing is. That is, it's not explicitly set,
  3. // so we're going to make it whatever the thing already
  4. // is, or "File"
  5. //
  6. // Until then, collect all events.
  7. module.exports = ProxyWriter
  8. var Writer = require('./writer.js')
  9. var getType = require('./get-type.js')
  10. var inherits = require('inherits')
  11. var collect = require('./collect.js')
  12. var fs = require('fs')
  13. inherits(ProxyWriter, Writer)
  14. function ProxyWriter (props) {
  15. var self = this
  16. if (!(self instanceof ProxyWriter)) {
  17. throw new Error('ProxyWriter must be called as constructor.')
  18. }
  19. self.props = props
  20. self._needDrain = false
  21. Writer.call(self, props)
  22. }
  23. ProxyWriter.prototype._stat = function () {
  24. var self = this
  25. var props = self.props
  26. // stat the thing to see what the proxy should be.
  27. var stat = props.follow ? 'stat' : 'lstat'
  28. fs[stat](props.path, function (er, current) {
  29. var type
  30. if (er || !current) {
  31. type = 'File'
  32. } else {
  33. type = getType(current)
  34. }
  35. props[type] = true
  36. props.type = self.type = type
  37. self._old = current
  38. self._addProxy(Writer(props, current))
  39. })
  40. }
  41. ProxyWriter.prototype._addProxy = function (proxy) {
  42. // console.error("~~ set proxy", this.path)
  43. var self = this
  44. if (self._proxy) {
  45. return self.error('proxy already set')
  46. }
  47. self._proxy = proxy
  48. ;[
  49. 'ready',
  50. 'error',
  51. 'close',
  52. 'pipe',
  53. 'drain',
  54. 'warn'
  55. ].forEach(function (ev) {
  56. proxy.on(ev, self.emit.bind(self, ev))
  57. })
  58. self.emit('proxy', proxy)
  59. var calls = self._buffer
  60. calls.forEach(function (c) {
  61. // console.error("~~ ~~ proxy buffered call", c[0], c[1])
  62. proxy[c[0]].apply(proxy, c[1])
  63. })
  64. self._buffer.length = 0
  65. if (self._needsDrain) self.emit('drain')
  66. }
  67. ProxyWriter.prototype.add = function (entry) {
  68. // console.error("~~ proxy add")
  69. collect(entry)
  70. if (!this._proxy) {
  71. this._buffer.push(['add', [entry]])
  72. this._needDrain = true
  73. return false
  74. }
  75. return this._proxy.add(entry)
  76. }
  77. ProxyWriter.prototype.write = function (c) {
  78. // console.error('~~ proxy write')
  79. if (!this._proxy) {
  80. this._buffer.push(['write', [c]])
  81. this._needDrain = true
  82. return false
  83. }
  84. return this._proxy.write(c)
  85. }
  86. ProxyWriter.prototype.end = function (c) {
  87. // console.error('~~ proxy end')
  88. if (!this._proxy) {
  89. this._buffer.push(['end', [c]])
  90. return false
  91. }
  92. return this._proxy.end(c)
  93. }