UNPKG

1.06 kBMarkdownView Raw
1# Decode streams into strings The Right Way(tm)
2
3```javascript
4var fs = require('fs')
5var zlib = require('zlib')
6var strs = require('stringstream')
7
8var utf8Stream = fs.createReadStream('massiveLogFile.gz')
9 .pipe(zlib.createGunzip())
10 .pipe(strs('utf8'))
11```
12
13No need to deal with `setEncoding()` weirdness, just compose streams
14like they were supposed to be!
15
16Handles input and output encoding:
17
18```javascript
19// Stream from utf8 to hex to base64... Why not, ay.
20var hex64Stream = fs.createReadStream('myFile')
21 .pipe(strs('utf8', 'hex'))
22 .pipe(strs('hex', 'base64'))
23```
24
25Also deals with `base64` output correctly by aligning each emitted data
26chunk so that there are no dangling `=` characters:
27
28```javascript
29var stream = fs.createReadStream('myFile').pipe(strs('base64'))
30
31var base64Str = ''
32
33stream.on('data', function(data) { base64Str += data })
34stream.on('end', function() {
35 console.log('My base64 encoded file is: ' + base64Str) // Wouldn't work with setEncoding()
36 console.log('Original file is: ' + new Buffer(base64Str, 'base64'))
37})
38```