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
6 // Until then, collect all events.
8 module.exports = ProxyWriter
10 var Writer = require('./writer.js')
11 var getType = require('./get-type.js')
12 var inherits = require('inherits')
13 var collect = require('./collect.js')
14 var fs = require('fs')
16 inherits(ProxyWriter, Writer)
18 function ProxyWriter (props) {
20 if (!(self instanceof ProxyWriter)) {
21 throw new Error('ProxyWriter must be called as constructor.')
25 self._needDrain = false
27 Writer.call(self, props)
30 ProxyWriter.prototype._stat = function () {
32 var props = self.props
33 // stat the thing to see what the proxy should be.
34 var stat = props.follow ? 'stat' : 'lstat'
36 fs[stat](props.path, function (er, current) {
41 type = getType(current)
45 props.type = self.type = type
48 self._addProxy(Writer(props, current))
52 ProxyWriter.prototype._addProxy = function (proxy) {
53 // console.error("~~ set proxy", this.path)
56 return self.error('proxy already set')
67 ].forEach(function (ev) {
68 proxy.on(ev, self.emit.bind(self, ev))
71 self.emit('proxy', proxy)
73 var calls = self._buffer
74 calls.forEach(function (c) {
75 // console.error("~~ ~~ proxy buffered call", c[0], c[1])
76 proxy[c[0]].apply(proxy, c[1])
78 self._buffer.length = 0
79 if (self._needsDrain) self.emit('drain')
82 ProxyWriter.prototype.add = function (entry) {
83 // console.error("~~ proxy add")
87 this._buffer.push(['add', [entry]])
88 this._needDrain = true
91 return this._proxy.add(entry)
94 ProxyWriter.prototype.write = function (c) {
95 // console.error('~~ proxy write')
97 this._buffer.push(['write', [c]])
98 this._needDrain = true
101 return this._proxy.write(c)
104 ProxyWriter.prototype.end = function (c) {
105 // console.error('~~ proxy end')
107 this._buffer.push(['end', [c]])
110 return this._proxy.end(c)