Coder Social home page Coder Social logo

stringstream's Introduction

Decode streams into strings without setEncoding

const fs = require('fs')
const zlib = require('zlib')
const strs = require('stringstream')

const utf8Stream = fs.createReadStream('massiveLogFile.gz')
  .pipe(zlib.createGunzip())
  .pipe(strs('utf8'))

utf8Stream.on('data', str => console.log(`This will be a string: ${str}`))

API

  • 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

options can be anything compatible with the standard Node.js new stream.Transform([options]) constructor

NB: This library was originally written before Node.js correctly encoded base64 strings from streams

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:

// Stream from utf8 to hex to base64... Why not, ay.
const hex64Stream = fs.createReadStream('myFile.txt')
  .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:

const stream = fs.createReadStream('myFile.jpg').pipe(strs('base64'))

let base64Str = ''

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'))
})

stringstream's People

Contributors

mhart avatar piotr1212 avatar

Stargazers

José Ícaro B. C. avatar Alexander1984z avatar Cat  avatar Satoshi nakamoto avatar Norbert de Langen avatar JamesLinus avatar saif avatar Nicola Del Gobbo avatar Glauber Mota avatar Joshua Bartlett avatar Julien avatar Adrian Perez avatar Vincenzo Ferrari avatar 谷文佳 avatar  avatar 张娜-class2 avatar Shane Holloway avatar Franco Correa avatar Gustavo Gard avatar anton avatar Angus H. avatar Nicolas Lochet avatar Yaniv Kessler avatar Favi_ty avatar Eric Bailey avatar debuggingfuture (Vincent LCY) avatar  avatar

Watchers

 avatar James Cloos avatar Rachel Morrell avatar  avatar Alexander1984z avatar The Don avatar

stringstream's Issues

Is pipe from stringstream work correctly?

Well, I have large gzip archive of kern.log kern.log.2.gz (72 megabytes).
Code for this file.

const fs = require("fs")
const zlib = require('zlib')
const sstream = require("stringstream")

fs.createReadStream('kern.log.2.gz')
    .pipe(zlib.createGunzip())
    .pipe(sstream('utf8')) // stringstream
    .pipe(fs.createWriteStream('kern.log.2.sstream'))

fs.createReadStream('kern.log.2.gz')
    .pipe(zlib.createGunzip())
    .pipe(fs.createWriteStream('kern.log.2.try'))

After run I get: kern.log.2.sstream (480 kilobytes) and kern.log.2.try (1,4 gigabytes).
kern.log.2.sstream didn't ended correctly: the file contain only part of kern.log.2.gz information.
Is it conceived behavior?

no method 'on'

Trying to run the example, getting this:

TypeError: Object function StringStream(from, to) {
  if (!(this instanceof StringStream)) return new StringStream(from, to)

  Stream.call(this)

  from = from || 'utf8'

  this.readable = this.writable = true
  this.paused = false
  this.toEncoding = (typeof to === 'undefined' ? from : to)
  this.fromEncoding = (typeof to === 'undefined' ? '' : from)
  this.decoder = new AlignedStringDecoder(this.toEncoding)
} has no method 'on'

node v0.8.17

Time for 1.0.0?

I noticed you just pushed a 0.0.6 (I assume in response to https://nodesecurity.io/advisories/664?), but, a semver dependency on ^0.0.5 will not automatically pick up 0.0.6. So to get your bugfix, I'm going to have to wait for the libraries I use which depend on stringstream to be updated.

It's been almost 6 years. I think it's safe to declare 1.0.0. ;)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.