• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2// Test unzipping a gzip file that contains multiple concatenated "members"
3
4const common = require('../common');
5const assert = require('assert');
6const zlib = require('zlib');
7const fs = require('fs');
8const fixtures = require('../common/fixtures');
9
10const abc = 'abc';
11const def = 'def';
12
13const abcEncoded = zlib.gzipSync(abc);
14const defEncoded = zlib.gzipSync(def);
15
16const data = Buffer.concat([
17  abcEncoded,
18  defEncoded
19]);
20
21assert.strictEqual(zlib.gunzipSync(data).toString(), (abc + def));
22
23zlib.gunzip(data, common.mustCall((err, result) => {
24  assert.ifError(err);
25  assert.strictEqual(result.toString(), (abc + def));
26}));
27
28zlib.unzip(data, common.mustCall((err, result) => {
29  assert.ifError(err);
30  assert.strictEqual(result.toString(), (abc + def));
31}));
32
33// Multi-member support does not apply to zlib inflate/deflate.
34zlib.unzip(Buffer.concat([
35  zlib.deflateSync('abc'),
36  zlib.deflateSync('def')
37]), common.mustCall((err, result) => {
38  assert.ifError(err);
39  assert.strictEqual(result.toString(), abc);
40}));
41
42// Files that have the "right" magic bytes for starting a new gzip member
43// in the middle of themselves, even if they are part of a single
44// regularly compressed member
45const pmmFileZlib = fixtures.path('pseudo-multimember-gzip.z');
46const pmmFileGz = fixtures.path('pseudo-multimember-gzip.gz');
47
48const pmmExpected = zlib.inflateSync(fs.readFileSync(pmmFileZlib));
49const pmmResultBuffers = [];
50
51fs.createReadStream(pmmFileGz)
52  .pipe(zlib.createGunzip())
53  .on('error', (err) => {
54    assert.ifError(err);
55  })
56  .on('data', (data) => pmmResultBuffers.push(data))
57  .on('finish', common.mustCall(() => {
58    // Result should match original random garbage
59    assert.deepStrictEqual(Buffer.concat(pmmResultBuffers), pmmExpected);
60  }));
61
62// Test that the next gzip member can wrap around the input buffer boundary
63[0, 1, 2, 3, 4, defEncoded.length].forEach((offset) => {
64  const resultBuffers = [];
65
66  const unzip = zlib.createGunzip()
67    .on('error', (err) => {
68      assert.ifError(err);
69    })
70    .on('data', (data) => resultBuffers.push(data))
71    .on('finish', common.mustCall(() => {
72      assert.strictEqual(
73        Buffer.concat(resultBuffers).toString(),
74        'abcdef',
75        `result should match original input (offset = ${offset})`
76      );
77    }));
78
79  // First write: write "abc" + the first bytes of "def"
80  unzip.write(Buffer.concat([
81    abcEncoded, defEncoded.slice(0, offset)
82  ]));
83
84  // Write remaining bytes of "def"
85  unzip.end(defEncoded.slice(offset));
86});
87