Skip to content

Commit a78636f

Browse files
committed
Handle raw gzip deflate stream
1 parent ab97c01 commit a78636f

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

src/gzip.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,12 @@ function shft(p) {
5757
* @returns {number}
5858
*/
5959
function gzipStart(input, i) {
60-
if (input[i++] !== 31 || input[i++] !== 139 || input[i++] !== 8) throw new Error('invalid gzip data')
60+
// if missing gzip header, assume raw deflate stream
61+
if (input[i++] !== 31 || input[i++] !== 139 || input[i++] !== 8) return 0
6162
const flag = input[i++]
62-
i += 6
63-
if (flag & 4) i += (input[i + 10] | input[i + 11] << 8) + 2
63+
i += 6 // skip header
64+
if (flag & 4) i += (input[i + 10] | input[i + 11] << 8) + 2 // skip extra
65+
// skip name and comment
6466
for (let zs = (flag >> 3 & 1) + (flag >> 4 & 1); zs > 0; zs -= Number(!input[i++]));
6567
return i + (flag & 2)
6668
}

test/gzip.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,45 @@ describe('gzip compressor', () => {
6060
const resized = gunzip(input)
6161
expect(resized).toEqual(new Uint8Array([11, 11, 22, 33]))
6262
})
63+
64+
it('gzip raw deflate stream (from avro)', () => {
65+
const input = new Uint8Array([
66+
13, 200, 193, 13, 130, 48, 20, 0,
67+
208, 210, 113, 76, 190, 191, 41, 173,
68+
5, 183, 249, 45, 63, 194, 161, 208,
69+
180, 128, 113, 19, 227, 213, 33, 188,
70+
58, 5, 94, 117, 3, 71, 208, 119, 124,
71+
207, 170, 212, 116, 68, 236, 47, 137,
72+
115, 162, 76, 17, 134, 192, 158, 243,
73+
9, 207, 148, 185, 159, 150, 194, 232,
74+
151, 113, 28, 184, 96, 228, 153, 58,
75+
154, 9, 169, 177, 173, 171, 217, 128,
76+
81, 170, 5, 19, 60, 65, 227, 44,
77+
131, 213, 174, 33, 29, 14, 74, 187,
78+
14, 162, 218, 211, 154, 167, 45, 9,
79+
33, 229, 245, 241, 250, 188, 111, 219,
80+
253, 91, 73, 33, 118, 255, 17, 63,
81+
])
82+
const expected = new Uint8Array([
83+
192, 1, 115, 51, 97, 58, 47, 47,
84+
104, 121, 112, 101, 114, 112, 97, 114,
85+
97, 109, 45, 105, 99, 101, 98, 101,
86+
114, 103, 47, 119, 97, 114, 101, 104,
87+
111, 117, 115, 101, 47, 98, 117, 110,
88+
110, 105, 101, 115, 47, 109, 101, 116,
89+
97, 100, 97, 116, 97, 47, 97, 56,
90+
53, 57, 55, 51, 101, 52, 45, 52,
91+
48, 48, 57, 45, 52, 99, 98, 97,
92+
45, 56, 55, 53, 101, 45, 53, 50,
93+
55, 56, 97, 50, 99, 54, 48, 50,
94+
55, 100, 45, 109, 48, 46, 97, 118,
95+
114, 111, 214, 112, 0, 0, 2, 2,
96+
152, 183, 215, 225, 224, 154, 214, 163,
97+
240, 1, 2, 0, 0, 42, 0, 0,
98+
2, 0,
99+
])
100+
101+
const output = gunzip(input)
102+
expect(output).toEqual(expected)
103+
})
63104
})

0 commit comments

Comments
 (0)