|
| 1 | +var Stream = require('stream').Stream; |
| 2 | +var util = require('util'); |
| 3 | +var CopyFromStream = function () { |
| 4 | + Stream.apply(this, arguments); |
| 5 | + this._buffer = new Buffer(0); |
| 6 | + this._connection = false; |
| 7 | + this._finished = false; |
| 8 | + this._error = false; |
| 9 | + this.__defineGetter__("writable", this._writable.bind(this)); |
| 10 | +}; |
| 11 | +util.inherits(CopyFromStream, Stream); |
| 12 | +CopyFromStream.prototype._writable = function () { |
| 13 | + return !this._finished && !this._error; |
| 14 | +} |
| 15 | +CopyFromStream.prototype.startStreamingToConnection = function (connection) { |
| 16 | + this._connection = connection; |
| 17 | + this._handleChunk(); |
| 18 | + this._endIfConnectionReady(); |
| 19 | +}; |
| 20 | +CopyFromStream.prototype._handleChunk = function (string, encoding) { |
| 21 | + var dataChunk, |
| 22 | + tmpBuffer; |
| 23 | + if (string !== undefined) { |
| 24 | + if (string instanceof Buffer) { |
| 25 | + dataChunk = string; |
| 26 | + } else { |
| 27 | + dataChunk = new Buffer(string, encoding); |
| 28 | + } |
| 29 | + if (this._buffer.length) { |
| 30 | + //Buffer.concat is better, but it's missing |
| 31 | + //in node v0.6.x |
| 32 | + tmpBuffer = new Buffer(this._buffer.length + dataChunk.length); |
| 33 | + tmpBuffer.copy(this._buffer); |
| 34 | + tmpBuffer.copy(dataChunk, this._buffer.length); |
| 35 | + this._buffer = tmpBuffer; |
| 36 | + } else { |
| 37 | + this._buffer = dataChunk; |
| 38 | + } |
| 39 | + } |
| 40 | + return this._sendIfConnectionReady(); |
| 41 | +}; |
| 42 | +CopyFromStream.prototype._sendIfConnectionReady = function () { |
| 43 | + var dataSent = false; |
| 44 | + if (this._connection && this._buffer.length) { |
| 45 | + dataSent = this._connection.sendCopyFromChunk(this._buffer); |
| 46 | + this._buffer = new Buffer(0); |
| 47 | + } |
| 48 | + return dataSent; |
| 49 | +}; |
| 50 | +CopyFromStream.prototype._endIfConnectionReady = function () { |
| 51 | + if (this._connection && this._finished) { |
| 52 | + //TODO change function name |
| 53 | + this._connection.endCopyFrom(); |
| 54 | + } |
| 55 | +} |
| 56 | +CopyFromStream.prototype.write = function (string, encoding) { |
| 57 | + if (!this._writable) { |
| 58 | + //TODO possibly throw exception? |
| 59 | + return false; |
| 60 | + } |
| 61 | + return this._handleChunk.apply(this, arguments); |
| 62 | +}; |
| 63 | +CopyFromStream.prototype.end = function (string, encondig) { |
| 64 | + if(!this._writable) { |
| 65 | + //TODO possibly throw exception? |
| 66 | + return false; |
| 67 | + } |
| 68 | + this._finished = true; |
| 69 | + if (string !== undefined) { |
| 70 | + this._handleChunk.apply(this, arguments); |
| 71 | + }; |
| 72 | + this._endIfConnectionReady(); |
| 73 | +}; |
| 74 | +CopyFromStream.prototype.error = function (error) { |
| 75 | + this._error = true; |
| 76 | + this.emit('error', error); |
| 77 | +}; |
| 78 | +CopyFromStream.prototype.close = function () { |
| 79 | + this.emit("close"); |
| 80 | +}; |
| 81 | +var CopyToStream = function () { |
| 82 | + Stream.apply(this, arguments); |
| 83 | + this._error = false; |
| 84 | + this._finished = false; |
| 85 | + this._paused = false; |
| 86 | + this.buffer = new Buffer(0); |
| 87 | + this._encoding = undefined; |
| 88 | + this.__defineGetter__('readable', this._readable.bind(this)); |
| 89 | +}; |
| 90 | +util.inherits(CopyToStream, Stream); |
| 91 | +CopyToStream.prototype._outputDataChunk = function () { |
| 92 | + if (this._paused) { |
| 93 | + return; |
| 94 | + } |
| 95 | + if (this.buffer.length) { |
| 96 | + if (this._encoding) { |
| 97 | + this.emit('data', this.buffer.toString(encoding)); |
| 98 | + } else { |
| 99 | + this.emit('data', this.buffer); |
| 100 | + } |
| 101 | + this.buffer = new Buffer(0); |
| 102 | + } |
| 103 | +}; |
| 104 | +CopyToStream.prototype._readable = function () { |
| 105 | + return !this._finished && !this._error; |
| 106 | +} |
| 107 | +CopyToStream.prototype.error = function (error) { |
| 108 | + if (!this.readable) { |
| 109 | + return false; |
| 110 | + } |
| 111 | + this._error = error; |
| 112 | + if (!this._paused) { |
| 113 | + this.emit('error', error); |
| 114 | + } |
| 115 | +}; |
| 116 | +CopyToStream.prototype.close = function () { |
| 117 | + if (!this.readable) { |
| 118 | + return false; |
| 119 | + } |
| 120 | + this._finished = true; |
| 121 | + if (!this._paused) { |
| 122 | + this.emit("end"); |
| 123 | + } |
| 124 | +}; |
| 125 | +CopyToStream.prototype.handleChunk = function (chunk) { |
| 126 | + var tmpBuffer; |
| 127 | + if (!this.readable) { |
| 128 | + return; |
| 129 | + } |
| 130 | + if (!this.buffer.length) { |
| 131 | + this.buffer = chunk; |
| 132 | + } else { |
| 133 | + tmpBuffer = new Buffer(this.buffer.length + chunk.length); |
| 134 | + this.buffer.copy(tmpBuffer); |
| 135 | + chunk.copy(tmpBuffer, this.buffer.length); |
| 136 | + this.buffer = tmpBuffer; |
| 137 | + } |
| 138 | + this._outputDataChunk(); |
| 139 | +}; |
| 140 | +CopyToStream.prototype.pause = function () { |
| 141 | + if (!this.readable) { |
| 142 | + return false; |
| 143 | + } |
| 144 | + this._paused = true; |
| 145 | +}; |
| 146 | +CopyToStream.prototype.resume = function () { |
| 147 | + if (!this._paused) { |
| 148 | + return false; |
| 149 | + } |
| 150 | + this._paused = false; |
| 151 | + this._outputDataChunk(); |
| 152 | + if (this._error) { |
| 153 | + return this.emit('error', this._error); |
| 154 | + } |
| 155 | + if (this._finished) { |
| 156 | + return this.emit('end'); |
| 157 | + } |
| 158 | +}; |
| 159 | +CopyToStream.prototype.setEncoding = function (encoding) { |
| 160 | + this._encoding = encoding; |
| 161 | +}; |
| 162 | + |
| 163 | +module.exports = { |
| 164 | + CopyFromStream: CopyFromStream, |
| 165 | + CopyToStream: CopyToStream |
| 166 | +}; |
0 commit comments