Advertisement
Guest User

Untitled

a guest
May 30th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. // Require the core node modules.
  2. var stream = require( "stream" );
  3. var util = require( "util" );
  4. var crypto = require( "crypto" );
  5. var fileSystem = require( "fs" );
  6.  
  7.  
  8. // ----------------------------------------------------------------------------------- //
  9. // ----------------------------------------------------------------------------------- //
  10.  
  11.  
  12. // CAUTION: Run the code in the next tick to give the full prototype chain a chance to
  13. // initialize. If we try to run immediately, we'll get the function hoisting for the
  14. // ETagStream constructor, but the prototype chain will not yet be defined fully.
  15. process.nextTick(
  16. function run() {
  17.  
  18. var fileReadStream = fileSystem.createReadStream( "./gina-carano.jpg" );
  19.  
  20. // Once the file is finished piping into the etagStream, it will emit an etag
  21. // event with the computed MD5 hash.
  22. var etagStream = new ETagStream()
  23. .on(
  24. "etag",
  25. function handleETag( etag ) {
  26.  
  27. console.log( "ETag:", etag );
  28.  
  29. }
  30. )
  31. ;
  32.  
  33. fileReadStream.pipe( etagStream );
  34.  
  35. }
  36. );
  37.  
  38.  
  39. // ----------------------------------------------------------------------------------- //
  40. // ----------------------------------------------------------------------------------- //
  41.  
  42.  
  43. // I provide a writable stream that will emit an "etag" event once the stream is closed.
  44. // The etag will be an MD5 hash of the content that was written to the stream.
  45. // --
  46. // NOTE: In this version, we'll be using the "legacy" methods of the underlying Hash
  47. // object which allow for intuitive .update() and .digest() methods.
  48. function ETagStream() {
  49.  
  50. // Call the super constructor.
  51. stream.Writable.call( this );
  52.  
  53. this._hasher = crypto.createHash( "md5" );
  54.  
  55. // Listen for the "finish" event, which will indicate that we have all the data that
  56. // we need in order to generate the MD5 has of the stream content.
  57. this.once( "finish", this._handleFinish.bind( this ) );
  58.  
  59. }
  60.  
  61. util.inherits( ETagStream, stream.Writable );
  62.  
  63.  
  64. // ---
  65. // PRIVATE METHODS.
  66. // ---
  67.  
  68.  
  69. // I handle the finish event, which, in turn, emits an "etag" event.
  70. ETagStream.prototype._handleFinish = function() {
  71.  
  72. // When dealing with "legacy" crypto methods, all we have to do is digest all of
  73. // the data that has been aggregated in the _write() method.
  74. this.emit( "etag", this._hasher.digest( "hex" ) );
  75.  
  76. };
  77.  
  78.  
  79. // I write data to the etag stream.
  80. ETagStream.prototype._write = function( chunk, encoding, writeComplete ) {
  81.  
  82. // When dealing with "legacy" crypto methods, we can simply pass the chunk into
  83. // the underlying hash without giving any concern to back-pressure.
  84. this._hasher.update( chunk, encoding );
  85.  
  86. writeComplete();
  87.  
  88. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement