pax_global_header00006660000000000000000000000064126132625120014512gustar00rootroot0000000000000052 comment=1efe3bf507bf3a1161f8473908b60e881d41422b StringStream-0.0.5/000077500000000000000000000000001261326251200141365ustar00rootroot00000000000000StringStream-0.0.5/.gitignore000066400000000000000000000001401261326251200161210ustar00rootroot00000000000000lib-cov *.seed *.log *.csv *.dat *.out *.pid *.gz pids logs results node_modules npm-debug.logStringStream-0.0.5/.travis.yml000066400000000000000000000000531261326251200162450ustar00rootroot00000000000000language: node_js node_js: - 0.4 - 0.6 StringStream-0.0.5/LICENSE.txt000066400000000000000000000020741261326251200157640ustar00rootroot00000000000000Copyright (c) 2012 Michael Hart (michael.hart.au@gmail.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. StringStream-0.0.5/README.md000066400000000000000000000020461261326251200154170ustar00rootroot00000000000000# Decode streams into strings The Right Way(tm) ```javascript var fs = require('fs') var zlib = require('zlib') var strs = require('stringstream') var utf8Stream = fs.createReadStream('massiveLogFile.gz') .pipe(zlib.createGunzip()) .pipe(strs('utf8')) ``` No need to deal with `setEncoding()` weirdness, just compose streams like they were supposed to be! Handles input and output encoding: ```javascript // Stream from utf8 to hex to base64... Why not, ay. var hex64Stream = fs.createReadStream('myFile') .pipe(strs('utf8', 'hex')) .pipe(strs('hex', 'base64')) ``` Also deals with `base64` output correctly by aligning each emitted data chunk so that there are no dangling `=` characters: ```javascript var stream = fs.createReadStream('myFile').pipe(strs('base64')) var base64Str = '' stream.on('data', function(data) { base64Str += data }) stream.on('end', function() { console.log('My base64 encoded file is: ' + base64Str) // Wouldn't work with setEncoding() console.log('Original file is: ' + new Buffer(base64Str, 'base64')) }) ``` StringStream-0.0.5/example.js000066400000000000000000000014561261326251200161350ustar00rootroot00000000000000var fs = require('fs') var zlib = require('zlib') var strs = require('stringstream') var utf8Stream = fs.createReadStream('massiveLogFile.gz') .pipe(zlib.createGunzip()) .pipe(strs('utf8')) utf8Stream.pipe(process.stdout) // Stream from utf8 to hex to base64... Why not, ay. var hex64Stream = fs.createReadStream('myFile') .pipe(strs('utf8', 'hex')) .pipe(strs('hex', 'base64')) hex64Stream.pipe(process.stdout) // Deals with base64 correctly by aligning chunks var stream = fs.createReadStream('myFile').pipe(strs('base64')) var base64Str = '' stream.on('data', function(data) { base64Str += data }) stream.on('end', function() { console.log('My base64 encoded file is: ' + base64Str) // Wouldn't work with setEncoding() console.log('Original file is: ' + new Buffer(base64Str, 'base64')) }) StringStream-0.0.5/package.json000066400000000000000000000006431261326251200164270ustar00rootroot00000000000000{ "name": "stringstream", "version": "0.0.5", "description": "Encode and decode streams into string streams", "author": "Michael Hart (http://github.com/mhart)", "main": "stringstream.js", "keywords": [ "string", "stream", "base64", "gzip" ], "repository": { "type": "git", "url": "https://github.com/mhart/StringStream.git" }, "license": "MIT" } StringStream-0.0.5/stringstream.js000066400000000000000000000053501261326251200172210ustar00rootroot00000000000000var util = require('util') var Stream = require('stream') var StringDecoder = require('string_decoder').StringDecoder module.exports = StringStream module.exports.AlignedStringDecoder = AlignedStringDecoder function StringStream(from, to) { if (!(this instanceof StringStream)) return new StringStream(from, to) Stream.call(this) if (from == null) from = 'utf8' this.readable = this.writable = true this.paused = false this.toEncoding = (to == null ? from : to) this.fromEncoding = (to == null ? '' : from) this.decoder = new AlignedStringDecoder(this.toEncoding) } util.inherits(StringStream, Stream) StringStream.prototype.write = function(data) { if (!this.writable) { var err = new Error('stream not writable') err.code = 'EPIPE' this.emit('error', err) return false } if (this.fromEncoding) { if (Buffer.isBuffer(data)) data = data.toString() data = new Buffer(data, this.fromEncoding) } var string = this.decoder.write(data) if (string.length) this.emit('data', string) return !this.paused } StringStream.prototype.flush = function() { if (this.decoder.flush) { var string = this.decoder.flush() if (string.length) this.emit('data', string) } } StringStream.prototype.end = function() { if (!this.writable && !this.readable) return this.flush() this.emit('end') this.writable = this.readable = false this.destroy() } StringStream.prototype.destroy = function() { this.decoder = null this.writable = this.readable = false this.emit('close') } StringStream.prototype.pause = function() { this.paused = true } StringStream.prototype.resume = function () { if (this.paused) this.emit('drain') this.paused = false } function AlignedStringDecoder(encoding) { StringDecoder.call(this, encoding) switch (this.encoding) { case 'base64': this.write = alignedWrite this.alignedBuffer = new Buffer(3) this.alignedBytes = 0 break } } util.inherits(AlignedStringDecoder, StringDecoder) AlignedStringDecoder.prototype.flush = function() { if (!this.alignedBuffer || !this.alignedBytes) return '' var leftover = this.alignedBuffer.toString(this.encoding, 0, this.alignedBytes) this.alignedBytes = 0 return leftover } function alignedWrite(buffer) { var rem = (this.alignedBytes + buffer.length) % this.alignedBuffer.length if (!rem && !this.alignedBytes) return buffer.toString(this.encoding) var returnBuffer = new Buffer(this.alignedBytes + buffer.length - rem) this.alignedBuffer.copy(returnBuffer, 0, 0, this.alignedBytes) buffer.copy(returnBuffer, this.alignedBytes, 0, buffer.length - rem) buffer.copy(this.alignedBuffer, 0, buffer.length - rem, buffer.length) this.alignedBytes = rem return returnBuffer.toString(this.encoding) }