-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
34 lines (28 loc) · 914 Bytes
/
index.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
/* jshint node: true */
var zlib = require('zlib');
var through = require('through2');
var isGzipped = require('is-gzip-stream');
module.exports = function ensureGunzip(stream) {
var final = through();
// we will let is-gzip-stream handle the validation here,
// since it would be redundant
var known = isGzipped(stream, function(err, isGzipped) {
if (err) {
return final.emit('error', err);
}
if (isGzipped) {
known.pipe(zlib.createGunzip()).pipe(final);
} else {
known.pipe(final);
}
});
// in case the original `stream` was not a stream,
// we should check to make sure this is possible first
if (known.on) {
known.on('error', function() {
var args = [].slice.call(arguments);
final.emit.apply(final, ['error'].concat(args));
});
}
return final;
};