stringstream
Advanced tools
Comparing version 0.0.6 to 1.0.0
@@ -1,6 +0,6 @@ | ||
var fs = require('fs') | ||
var zlib = require('zlib') | ||
var strs = require('stringstream') | ||
const fs = require('fs') | ||
const zlib = require('zlib') | ||
const strs = require('stringstream') | ||
var utf8Stream = fs.createReadStream('massiveLogFile.gz') | ||
const utf8Stream = fs.createReadStream('massiveLogFile.gz') | ||
.pipe(zlib.createGunzip()) | ||
@@ -12,3 +12,3 @@ .pipe(strs('utf8')) | ||
// Stream from utf8 to hex to base64... Why not, ay. | ||
var hex64Stream = fs.createReadStream('myFile') | ||
const hex64Stream = fs.createReadStream('myFile.txt') | ||
.pipe(strs('utf8', 'hex')) | ||
@@ -20,10 +20,10 @@ .pipe(strs('hex', 'base64')) | ||
// Deals with base64 correctly by aligning chunks | ||
var stream = fs.createReadStream('myFile').pipe(strs('base64')) | ||
const stream = fs.createReadStream('myFile.jpg').pipe(strs('base64')) | ||
var base64Str = '' | ||
let 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')) | ||
stream.on('data', data => base64Str += data) | ||
stream.on('end', () => { | ||
console.log('My base64 encoded file is: ' + base64Str) | ||
console.log('Original file size: ' + Buffer.from(base64Str, 'base64').length) | ||
}) |
{ | ||
"name": "stringstream", | ||
"version": "0.0.6", | ||
"version": "1.0.0", | ||
"description": "Encode and decode streams into string streams", | ||
"author": "Michael Hart <michael.hart.au@gmail.com> (http://github.com/mhart)", | ||
"repository": "mhart/StringStream", | ||
"author": "Michael Hart <michael.hart.au@gmail.com>", | ||
"license": "MIT", | ||
"main": "stringstream.js", | ||
"keywords": [ | ||
"string", | ||
"stream", | ||
"base64", | ||
"gzip" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/mhart/StringStream.git" | ||
"engines": { | ||
"node": ">=4.0.0" | ||
}, | ||
"license": "MIT" | ||
"scripts": { | ||
"test": "node test.js" | ||
} | ||
} |
@@ -1,21 +0,35 @@ | ||
# Decode streams into strings The Right Way(tm) | ||
# Decode streams into strings without setEncoding | ||
```javascript | ||
var fs = require('fs') | ||
var zlib = require('zlib') | ||
var strs = require('stringstream') | ||
```js | ||
const fs = require('fs') | ||
const zlib = require('zlib') | ||
const strs = require('stringstream') | ||
var utf8Stream = fs.createReadStream('massiveLogFile.gz') | ||
const utf8Stream = fs.createReadStream('massiveLogFile.gz') | ||
.pipe(zlib.createGunzip()) | ||
.pipe(strs('utf8')) | ||
utf8Stream.on('data', str => console.log(`This will be a string: ${str}`)) | ||
``` | ||
No need to deal with `setEncoding()` weirdness, just compose streams | ||
like they were supposed to be! | ||
## API | ||
Handles input and output encoding: | ||
- `strs(to, [options])` – creates a transform stream that converts the input into strings in `to` encoding (eg, `utf8`, `hex`, `base64`) | ||
- `strs(from, to, [options])` – creates a transform stream converts the input from strings in `from` encoding to strings in `to` encoding | ||
```javascript | ||
`options` can be anything compatible with the standard Node.js [`new stream.Transform([options])` constructor](https://nodejs.org/api/stream.html#stream_new_stream_transform_options) | ||
## NB: This library was originally written before Node.js [correctly encoded base64 strings from streams](https://github.com/nodejs/node/commit/061f2075cf81017cdb40de80533ba18746743c94) | ||
Back in the day, calling `.setEncoding('base64')` on a readable stream didn't | ||
align correctly, which was one of the main reasons I wrote this library – | ||
however this hasn't been the case for a long time, so this library is | ||
now really only useful in scenarios where you don't want to call | ||
`.setEncoding()` for whatever reason. | ||
It also handles input and output text encodings: | ||
```js | ||
// Stream from utf8 to hex to base64... Why not, ay. | ||
var hex64Stream = fs.createReadStream('myFile') | ||
const hex64Stream = fs.createReadStream('myFile.txt') | ||
.pipe(strs('utf8', 'hex')) | ||
@@ -28,12 +42,12 @@ .pipe(strs('hex', 'base64')) | ||
```javascript | ||
var stream = fs.createReadStream('myFile').pipe(strs('base64')) | ||
```js | ||
const stream = fs.createReadStream('myFile.jpg').pipe(strs('base64')) | ||
var base64Str = '' | ||
let 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')) | ||
stream.on('data', data => base64Str += data) | ||
stream.on('end', () => { | ||
console.log('My base64 encoded file is: ' + base64Str) | ||
console.log('Original file is: ' + Buffer.from(base64Str, 'base64')) | ||
}) | ||
``` |
@@ -1,102 +0,54 @@ | ||
var util = require('util') | ||
var Stream = require('stream') | ||
var StringDecoder = require('string_decoder').StringDecoder | ||
'use strict' | ||
const util = require('util') | ||
const Transform = require('stream').Transform | ||
module.exports = StringStream | ||
module.exports.AlignedStringDecoder = AlignedStringDecoder | ||
function StringStream(from, to) { | ||
if (!(this instanceof StringStream)) return new StringStream(from, to) | ||
const CHAR_ALIGN = { | ||
'hex': 2, | ||
'base64': 4, | ||
} | ||
Stream.call(this) | ||
function StringStream(from, to, options) { | ||
if (!(this instanceof StringStream)) return new StringStream(from, to, options) | ||
if (from == null) from = 'utf8' | ||
Transform.call(this, options) | ||
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) | ||
if (typeof to !== 'string') { | ||
options = to | ||
to = from || 'utf8' | ||
from = null | ||
} | ||
this.setEncoding(to) | ||
this.fromEncoding = from | ||
this.fromBuffer = '' | ||
this.fromAlign = CHAR_ALIGN[this.fromEncoding] | ||
} | ||
util.inherits(StringStream, Stream) | ||
util.inherits(StringStream, Transform) | ||
StringStream.prototype.write = function(data) { | ||
if (!this.writable) { | ||
var err = new Error('stream not writable') | ||
err.code = 'EPIPE' | ||
this.emit('error', err) | ||
return false | ||
StringStream.prototype._transform = function(chunk, encoding, cb) { | ||
if (!this.fromEncoding) { | ||
return cb(null, chunk) | ||
} | ||
if (this.fromEncoding) { | ||
if (Buffer.isBuffer(data) || typeof data === 'number') data = data.toString() | ||
data = new Buffer(data, this.fromEncoding) | ||
let str = '' + chunk | ||
if (!this.fromAlign) { | ||
return cb(null, Buffer.from(str, 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) | ||
this.fromBuffer += str | ||
if (this.fromBuffer.length < this.fromAlign) { | ||
return cb() | ||
} | ||
const len = this.fromBuffer.length - (this.fromBuffer.length % this.fromAlign) | ||
str = this.fromBuffer.slice(0, len) | ||
this.fromBuffer = this.fromBuffer.slice(len) | ||
cb(null, Buffer.from(str, this.fromEncoding)) | ||
} | ||
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 | ||
StringStream.prototype._flush = function(cb) { | ||
if (this.fromBuffer) { | ||
const str = Buffer.from(this.fromBuffer, this.fromEncoding) | ||
str && this.push(str) | ||
} | ||
cb() // Can only supply data to callback from Node.js v7.0 onwards | ||
} | ||
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) | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
11112
7
141
1
53
2
1