Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80529 views
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
22
var crypto = require('crypto');
23
var stream = require('stream');
24
var Stream = stream.Stream;
25
var util = require('util');
26
var tape = require('tape');
27
var zlib = require('../');
28
29
30
31
// emit random bytes, and keep a shasum
32
function RandomReadStream(opt) {
33
Stream.call(this);
34
35
this.readable = true;
36
this._paused = false;
37
this._processing = false;
38
39
this._hasher = crypto.createHash('sha1');
40
opt = opt || {};
41
42
// base block size.
43
opt.block = opt.block || 256 * 1024;
44
45
// total number of bytes to emit
46
opt.total = opt.total || 256 * 1024 * 1024;
47
this._remaining = opt.total;
48
49
// how variable to make the block sizes
50
opt.jitter = opt.jitter || 1024;
51
52
this._opt = opt;
53
54
this._process = this._process.bind(this);
55
56
process.nextTick(this._process);
57
}
58
59
util.inherits(RandomReadStream, Stream);
60
61
RandomReadStream.prototype.pause = function() {
62
this._paused = true;
63
this.emit('pause');
64
};
65
66
RandomReadStream.prototype.resume = function() {
67
this._paused = false;
68
this.emit('resume');
69
this._process();
70
};
71
72
RandomReadStream.prototype._process = function() {
73
if (this._processing) return;
74
if (this._paused) return;
75
76
this._processing = true;
77
78
if (!this._remaining) {
79
this._hash = this._hasher.digest('hex').toLowerCase().trim();
80
this._processing = false;
81
82
this.emit('end');
83
return;
84
}
85
86
// figure out how many bytes to output
87
// if finished, then just emit end.
88
var block = this._opt.block;
89
var jitter = this._opt.jitter;
90
if (jitter) {
91
block += Math.ceil(Math.random() * jitter - (jitter / 2));
92
}
93
block = Math.min(block, this._remaining);
94
var buf = new Buffer(block);
95
for (var i = 0; i < block; i++) {
96
buf[i] = Math.random() * 256;
97
}
98
99
this._hasher.update(buf);
100
101
this._remaining -= block;
102
103
this._processing = false;
104
105
this.emit('data', buf);
106
process.nextTick(this._process);
107
};
108
109
110
// a filter that just verifies a shasum
111
function HashStream() {
112
Stream.call(this);
113
114
this.readable = this.writable = true;
115
this._hasher = crypto.createHash('sha1');
116
}
117
118
util.inherits(HashStream, Stream);
119
120
HashStream.prototype.write = function(c) {
121
// Simulate the way that an fs.ReadStream returns false
122
// on *every* write like a jerk, only to resume a
123
// moment later.
124
this._hasher.update(c);
125
process.nextTick(this.resume.bind(this));
126
return false;
127
};
128
129
HashStream.prototype.resume = function() {
130
this.emit('resume');
131
process.nextTick(this.emit.bind(this, 'drain'));
132
};
133
134
HashStream.prototype.end = function(c) {
135
if (c) {
136
this.write(c);
137
}
138
this._hash = this._hasher.digest('hex').toLowerCase().trim();
139
this.emit('data', this._hash);
140
this.emit('end');
141
};
142
143
tape('random byte pipes', function(t) {
144
var inp = new RandomReadStream({ total: 1024, block: 256, jitter: 16 });
145
var out = new HashStream();
146
var gzip = zlib.createGzip();
147
var gunz = zlib.createGunzip();
148
149
inp.pipe(gzip).pipe(gunz).pipe(out);
150
151
inp.on('data', function(c) {
152
t.ok(c.length);
153
});
154
155
gzip.on('data', function(c) {
156
t.ok(c.length);
157
});
158
159
gunz.on('data', function(c) {
160
t.ok(c.length);
161
});
162
163
out.on('data', function(c) {
164
t.ok(c.length);
165
});
166
167
out.on('data', function(c) {
168
t.ok(c.length);
169
t.equal(c, inp._hash, 'hashes should match');
170
});
171
172
out.on('end', function() {
173
t.end();
174
})
175
});
176
177
178