diff options
| author | Kumar Priyansh <[email protected]> | 2019-01-19 12:37:14 +0530 |
|---|---|---|
| committer | Kumar Priyansh <[email protected]> | 2019-01-19 12:37:14 +0530 |
| commit | dcdfc94cb39dfe2c39925a0145ffa45e2d061c30 (patch) | |
| tree | 4f6379d955555b298c0e7b83a67e264240ee5614 /cordova/node_modules/stream-buffers/lib | |
| parent | 76f7b3678d3f1ff99c3935a774d420453b0c3cb9 (diff) | |
| download | WeatherApp-dcdfc94cb39dfe2c39925a0145ffa45e2d061c30.tar.xz WeatherApp-dcdfc94cb39dfe2c39925a0145ffa45e2d061c30.zip | |
Initial Upload via GIT
Diffstat (limited to 'cordova/node_modules/stream-buffers/lib')
4 files changed, 242 insertions, 0 deletions
diff --git a/cordova/node_modules/stream-buffers/lib/constants.js b/cordova/node_modules/stream-buffers/lib/constants.js new file mode 100755 index 0000000..6bf81a4 --- /dev/null +++ b/cordova/node_modules/stream-buffers/lib/constants.js @@ -0,0 +1,6 @@ +module.exports = { + DEFAULT_INITIAL_SIZE: (8 * 1024), + DEFAULT_INCREMENT_AMOUNT: (8 * 1024), + DEFAULT_FREQUENCY: 1, + DEFAULT_CHUNK_SIZE: 1024 +}; diff --git a/cordova/node_modules/stream-buffers/lib/readable_streambuffer.js b/cordova/node_modules/stream-buffers/lib/readable_streambuffer.js new file mode 100755 index 0000000..5d5669a --- /dev/null +++ b/cordova/node_modules/stream-buffers/lib/readable_streambuffer.js @@ -0,0 +1,136 @@ +var stream = require("stream"), + constants = require("./constants"), + util = require("util"); + +var ReadableStreamBuffer = module.exports = function(opts) { + var that = this; + + stream.Stream.call(this); + + opts = opts || {}; + var frequency = opts.hasOwnProperty("frequency") ? opts.frequency : constants.DEFAULT_FREQUENCY; + var chunkSize = opts.chunkSize || constants.DEFAULT_CHUNK_SIZE; + var initialSize = opts.initialSize || constants.DEFAULT_INITIAL_SIZE; + var incrementAmount = opts.incrementAmount || constants.DEFAULT_INCREMENT_AMOUNT; + + var size = 0; + var buffer = new Buffer(initialSize); + var encoding = null; + + this.readable = true; + this.writable = false; + + var sendData = function() { + var amount = Math.min(chunkSize, size); + + if (amount > 0) { + var chunk = null; + if(encoding) { + chunk = buffer.toString(encoding, 0, amount); + } + else { + chunk = new Buffer(amount); + buffer.copy(chunk, 0, 0, amount); + } + + that.emit("data", chunk); + + if(amount < buffer.length) + buffer.copy(buffer, 0, amount, size); + size -= amount; + } + + if(size === 0 && !that.readable) { + that.emit("end"); + that.emit("close"); + if (sendData && sendData.interval) { + clearInterval(sendData.interval); + sendData.interval = null; + } + } + }; + + this.size = function() { + return size; + }; + + this.maxSize = function() { + return buffer.length; + }; + + var increaseBufferIfNecessary = function(incomingDataSize) { + if((buffer.length - size) < incomingDataSize) { + var factor = Math.ceil((incomingDataSize - (buffer.length - size)) / incrementAmount); + + var newBuffer = new Buffer(buffer.length + (incrementAmount * factor)); + buffer.copy(newBuffer, 0, 0, size); + buffer = newBuffer; + } + }; + + this.put = function(data, encoding) { + if(!that.readable) return; + + var wasEmpty = size === 0; + if(Buffer.isBuffer(data)) { + increaseBufferIfNecessary(data.length); + data.copy(buffer, size, 0); + size += data.length; + } + else { + data = data + ""; + var dataSizeInBytes = Buffer.byteLength(data); + increaseBufferIfNecessary(dataSizeInBytes); + buffer.write(data, size, encoding || "utf8"); + size += dataSizeInBytes; + } + + if (wasEmpty && size > 0) { + this.emit('readable') + } + + if (!this.isPaused && !frequency) { + while (size > 0) { + sendData(); + } + } + }; + + this.pause = function() { + this.isPaused = true; + if(sendData && sendData.interval) { + clearInterval(sendData.interval); + delete sendData.interval; + } + }; + + this.resume = function() { + this.isPaused = false; + if(sendData && !sendData.interval && frequency > 0) { + sendData.interval = setInterval(sendData, frequency); + } + }; + + this.destroy = function() { + that.emit("end"); + if(sendData.interval) clearInterval(sendData.interval); + sendData = null; + that.readable = false; + that.emit("close"); + }; + + this.destroySoon = function() { + that.readable = false; + if (!sendData.interval) { + that.emit("end"); + that.emit("close"); + } + }; + + this.setEncoding = function(_encoding) { + encoding = _encoding; + }; + + this.resume(); +}; +util.inherits(ReadableStreamBuffer, stream.Stream); diff --git a/cordova/node_modules/stream-buffers/lib/streambuffer.js b/cordova/node_modules/stream-buffers/lib/streambuffer.js new file mode 100755 index 0000000..d5a3ebb --- /dev/null +++ b/cordova/node_modules/stream-buffers/lib/streambuffer.js @@ -0,0 +1,3 @@ +module.exports = require("./constants"); +module.exports.ReadableStreamBuffer = require("./readable_streambuffer"); +module.exports.WritableStreamBuffer = require("./writable_streambuffer"); diff --git a/cordova/node_modules/stream-buffers/lib/writable_streambuffer.js b/cordova/node_modules/stream-buffers/lib/writable_streambuffer.js new file mode 100755 index 0000000..de1e997 --- /dev/null +++ b/cordova/node_modules/stream-buffers/lib/writable_streambuffer.js @@ -0,0 +1,97 @@ +var util = require("util"), + stream = require("stream"), + constants = require("./constants"); + +// TODO: clear up specs on returning false from a write and emitting a drain event. +// Does this mean if I return false from a write, I should ignore any write requests between that false return and the drain event? +var WritableStreamBuffer = module.exports = function(opts) { + var that = this; + + stream.Stream.call(this); + + opts = opts || {}; + var initialSize = opts.initialSize || constants.DEFAULT_INITIAL_SIZE; + var incrementAmount = opts.incrementAmount || constants.DEFAULT_INCREMENT_AMOUNT; + + var buffer = new Buffer(initialSize); + var size = 0; + + this.writable = true; + this.readable = false; + + this.size = function() { + return size; + }; + + this.maxSize = function() { + return buffer.length; + }; + + this.getContents = function(length) { + if(!size) return false; + + var data = new Buffer(Math.min(length || size, size)); + buffer.copy(data, 0, 0, data.length); + + if(data.length < size) + buffer.copy(buffer, 0, data.length); + + size -= data.length; + + return data; + }; + + this.getContentsAsString = function(encoding, length) { + if(!size) return false; + + var data = buffer.toString(encoding || "utf8", 0, Math.min(length || size, size)); + var dataLength = Buffer.byteLength(data); + + if(dataLength < size) + buffer.copy(buffer, 0, dataLength); + + size -= dataLength; + return data; + }; + + var increaseBufferIfNecessary = function(incomingDataSize) { + if((buffer.length - size) < incomingDataSize) { + var factor = Math.ceil((incomingDataSize - (buffer.length - size)) / incrementAmount); + + var newBuffer = new Buffer(buffer.length + (incrementAmount * factor)); + buffer.copy(newBuffer, 0, 0, size); + buffer = newBuffer; + } + }; + + this.write = function(data, encoding, callback) { + if(!that.writable) return; + + if(Buffer.isBuffer(data)) { + increaseBufferIfNecessary(data.length); + data.copy(buffer, size, 0); + size += data.length; + } + else { + data = data + ""; + increaseBufferIfNecessary(Buffer.byteLength(data)); + buffer.write(data, size, encoding || "utf8"); + size += Buffer.byteLength(data); + } + + if(typeof callback === "function") { callback() ;} + }; + + this.end = function() { + var args = Array.prototype.slice.apply(arguments); + if(args.length) that.write.apply(that, args); + that.emit('finish'); + that.destroy(); + }; + + this.destroySoon = this.destroy = function() { + that.writable = false; + that.emit("close"); + }; +}; +util.inherits(WritableStreamBuffer, stream.Stream); |
