-
Notifications
You must be signed in to change notification settings - Fork 3
/
gzip.js
executable file
·63 lines (50 loc) · 1.53 KB
/
gzip.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
var spawn = require('child_process').spawn,
Buffer = require('buffer').Buffer;
module.exports = function(data) {
var rate = 8,
enc = 'utf8',
isBuffer = Buffer.isBuffer(data),
args = Array.prototype.slice.call(arguments, 1),
callback;
if (!isBuffer && typeof args[0] === 'string') {
enc = args.shift();
}
if (typeof args[0] === 'number') {
rate = args.shift() - 0;
}
callback = args[0];
var gzip = spawn('gzip', ['-' + (rate-0),'-c', '-']);
var promise = new process.EventEmitter,
output = [],
output_len = 0;
// No need to use buffer if no callback was provided
if (callback) {
gzip.stdout.on('data', function(data) {
output.push(data);
output_len += data.length;
});
gzip.on('exit', function(code) {
var buf = new Buffer(output_len);
for (var a = 0, p = 0; p < output_len; p += output[a++].length) {
output[a].copy(buf, p, 0);
}
callback(code, buf);
});
}
// Promise events
gzip.stdout.on('data', function(data) {
promise.emit('data', data);
});
gzip.on('exit', function(code) {
promise.emit('end');
});
if (isBuffer) {
gzip.stdin.encoding = 'binary';
gzip.stdin.end(data.length ? data: '');
} else {
gzip.stdin.end(data ? data.toString() : '', enc);
}
// Return EventEmitter, so node.gzip can be used for streaming
// (thx @indexzero for that tip)
return promise;
};