-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
32 lines (28 loc) · 875 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
var zlib = require('zlib')
var peek = require('peek-stream')
var through = require('through2')
var pumpify = require('pumpify')
var isGzip = require('is-gzip')
var isDeflate = require('is-deflate')
var isCompressed = function (data) {
if (isGzip(data)) return 1
if (isDeflate(data)) return 2
return 0
}
var gunzip = function (maxRecursion) {
if (maxRecursion === undefined) maxRecursion = 3
return peek({newline: false, maxBuffer: 10}, function (data, swap) {
if (maxRecursion < 0) return swap(new Error('Maximum recursion reached'))
switch (isCompressed(data)) {
case 1:
swap(null, pumpify(zlib.createGunzip(), gunzip(maxRecursion - 1)))
break
case 2:
swap(null, pumpify(zlib.createInflate(), gunzip(maxRecursion - 1)))
break
default:
swap(null, through())
}
})
}
module.exports = gunzip