Created
November 22, 2014 12:54
-
-
Save alessioalex/34ec0de70ed3012c889e to your computer and use it in GitHub Desktop.
custom-transform-stream.js
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
// http://codewinds.com/blog/2013-08-20-nodejs-transform-streams.html | |
"use strict"; | |
var crypto = require('crypto'); | |
var stream = require('stream'); | |
var util = require('util'); | |
var Transform = stream.Transform; | |
function ShaSum(options) { | |
// allow use without new | |
if (!(this instanceof ShaSum)) { | |
return new ShaSum(options); | |
} | |
// init Transform | |
Transform.call(this, options); | |
this.digester = crypto.createHash('sha1'); | |
} | |
util.inherits(ShaSum, Transform); | |
/* during each chunk, update the digest */ | |
ShaSum.prototype._transform = function (chunk, enc, cb) { | |
// if is Buffer use it, otherwise coerce | |
var buffer = (Buffer.isBuffer(chunk)) ? | |
chunk : | |
new Buffer(chunk, enc); | |
this.digester.update(buffer); // update hash | |
// we are not writing anything out at this | |
// time, only at end during _flush | |
// so we don't need to call push | |
cb(); | |
}; | |
/* at the end, output the hex digest */ | |
ShaSum.prototype._flush = function (cb) { | |
this.push(this.digester.digest('hex')); | |
cb(); | |
}; | |
// try it out | |
var shasum = new ShaSum(); | |
shasum.pipe(process.stdout); // output to stdout | |
shasum.write('hello world\n'); // input line 1 | |
shasum.write('another line'); // input line 2 | |
shasum.end(); // finish |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment