Skip to content

Instantly share code, notes, and snippets.

@PatrickHeneise
Last active September 17, 2020 07:35
Show Gist options
  • Save PatrickHeneise/7a93158515a1c4d070128e5bff227397 to your computer and use it in GitHub Desktop.
Save PatrickHeneise/7a93158515a1c4d070128e5bff227397 to your computer and use it in GitHub Desktop.
stream transform
const { PerformanceObserver, performance } = require('perf_hooks')
const getJSONParse = (data) => {
performance.mark('getJSONParse init')
JSON.parse(data)
performance.mark('getJSONParse end')
performance.measure('getJSONParse', 'getJSONParse init', 'getJSONParse end')
}
const { spawn } = require('child_process');
const child = spawn('wc');
process.stdin.pipe(child.stdin)
child.stdout.on('data', (data) => {
console.log(`child stdout:\n${data}`);
});
const {Transform} = require('stream')
const transform = new Transform({
transform(chunk, encoding, callback) {
this.push(chunk.toString().replace(/[1-9]/g,'*'))
callback()
}
})
process.stdin.pipe(transform).pipe(process.stdout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment