Last active
August 10, 2021 23:21
-
-
Save mattfysh/47fc4b27598189ac5992b1ff19788b1d to your computer and use it in GitHub Desktop.
Shouty Stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { Transform } = require('stream') | |
const { createGzip } = require('zlib') | |
const fs = require('fs') | |
const testfile = fs.createWriteStream('test.gz') | |
const createShoutyStream = () => new Transform({ | |
construct(cb) { | |
this.data = '' | |
cb() | |
}, | |
transform(chunk, enc, cb) { | |
this.data += chunk | |
cb() | |
}, | |
flush(cb) { | |
this.push(this.data.toUpperCase()) | |
this.push(null) | |
} | |
}) | |
const shout1 = createShoutyStream() | |
const shout2 = createShoutyStream() | |
shout1.pipe(createGzip()).pipe(testfile, { end: false }) | |
shout2.pipe(createGzip()).pipe(testfile, { end: false }) | |
shout1.end('abc') | |
shout2.end('xyz') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { Transform, PassThrough, pipeline } = require('stream') | |
const { createGzip } = require('zlib') | |
const fs = require('fs') | |
const testfile = fs.createWriteStream('test.gz') | |
const createShoutyStream = () => new Transform({ | |
construct(cb) { | |
this.data = '' | |
cb() | |
}, | |
transform(chunk, enc, cb) { | |
this.data += chunk | |
cb() | |
}, | |
flush(cb) { | |
this.push(this.data.toUpperCase()) | |
this.push(null) | |
} | |
}) | |
const shout1 = createShoutyStream() | |
const shout2 = createShoutyStream() | |
const concat1 = new PassThrough() | |
const concat2 = new PassThrough() | |
concat1.cork() | |
concat2.cork() | |
const plcb = err => { | |
if (err) { | |
console.error('Pipeline failed:', err) | |
} else { | |
console.log('Pipeline success.') | |
} | |
} | |
pipeline(shout1, createGzip(), concat1, plcb).pipe(testfile, { end: false }) | |
pipeline(shout2, createGzip(), concat2, plcb).pipe(testfile, { end: false }) | |
shout1.end('abc') | |
shout2.end('xyz') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment