Aucune description

test-zlib-random-byte-pipes.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright Joyent, Inc. and other Node contributors.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a
  4. // copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to permit
  8. // persons to whom the Software is furnished to do so, subject to the
  9. // following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included
  12. // in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  17. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. var crypto = require('crypto');
  22. var stream = require('stream');
  23. var Stream = stream.Stream;
  24. var util = require('util');
  25. var tape = require('tape');
  26. var zlib = require('../');
  27. // emit random bytes, and keep a shasum
  28. function RandomReadStream(opt) {
  29. Stream.call(this);
  30. this.readable = true;
  31. this._paused = false;
  32. this._processing = false;
  33. this._hasher = crypto.createHash('sha1');
  34. opt = opt || {};
  35. // base block size.
  36. opt.block = opt.block || 256 * 1024;
  37. // total number of bytes to emit
  38. opt.total = opt.total || 256 * 1024 * 1024;
  39. this._remaining = opt.total;
  40. // how variable to make the block sizes
  41. opt.jitter = opt.jitter || 1024;
  42. this._opt = opt;
  43. this._process = this._process.bind(this);
  44. process.nextTick(this._process);
  45. }
  46. util.inherits(RandomReadStream, Stream);
  47. RandomReadStream.prototype.pause = function() {
  48. this._paused = true;
  49. this.emit('pause');
  50. };
  51. RandomReadStream.prototype.resume = function() {
  52. this._paused = false;
  53. this.emit('resume');
  54. this._process();
  55. };
  56. RandomReadStream.prototype._process = function() {
  57. if (this._processing) return;
  58. if (this._paused) return;
  59. this._processing = true;
  60. if (!this._remaining) {
  61. this._hash = this._hasher.digest('hex').toLowerCase().trim();
  62. this._processing = false;
  63. this.emit('end');
  64. return;
  65. }
  66. // figure out how many bytes to output
  67. // if finished, then just emit end.
  68. var block = this._opt.block;
  69. var jitter = this._opt.jitter;
  70. if (jitter) {
  71. block += Math.ceil(Math.random() * jitter - (jitter / 2));
  72. }
  73. block = Math.min(block, this._remaining);
  74. var buf = new Buffer(block);
  75. for (var i = 0; i < block; i++) {
  76. buf[i] = Math.random() * 256;
  77. }
  78. this._hasher.update(buf);
  79. this._remaining -= block;
  80. this._processing = false;
  81. this.emit('data', buf);
  82. process.nextTick(this._process);
  83. };
  84. // a filter that just verifies a shasum
  85. function HashStream() {
  86. Stream.call(this);
  87. this.readable = this.writable = true;
  88. this._hasher = crypto.createHash('sha1');
  89. }
  90. util.inherits(HashStream, Stream);
  91. HashStream.prototype.write = function(c) {
  92. // Simulate the way that an fs.ReadStream returns false
  93. // on *every* write like a jerk, only to resume a
  94. // moment later.
  95. this._hasher.update(c);
  96. process.nextTick(this.resume.bind(this));
  97. return false;
  98. };
  99. HashStream.prototype.resume = function() {
  100. this.emit('resume');
  101. process.nextTick(this.emit.bind(this, 'drain'));
  102. };
  103. HashStream.prototype.end = function(c) {
  104. if (c) {
  105. this.write(c);
  106. }
  107. this._hash = this._hasher.digest('hex').toLowerCase().trim();
  108. this.emit('data', this._hash);
  109. this.emit('end');
  110. };
  111. tape('random byte pipes', function(t) {
  112. var inp = new RandomReadStream({ total: 1024, block: 256, jitter: 16 });
  113. var out = new HashStream();
  114. var gzip = zlib.createGzip();
  115. var gunz = zlib.createGunzip();
  116. inp.pipe(gzip).pipe(gunz).pipe(out);
  117. inp.on('data', function(c) {
  118. t.ok(c.length);
  119. });
  120. gzip.on('data', function(c) {
  121. t.ok(c.length);
  122. });
  123. gunz.on('data', function(c) {
  124. t.ok(c.length);
  125. });
  126. out.on('data', function(c) {
  127. t.ok(c.length);
  128. });
  129. out.on('data', function(c) {
  130. t.ok(c.length);
  131. t.equal(c, inp._hash, 'hashes should match');
  132. });
  133. out.on('end', function() {
  134. t.end();
  135. })
  136. });