summaryrefslogtreecommitdiff
path: root/test/parallel/test-zlib-bytes-read.js
blob: a7a4e523f7060b7303a25346345f6cbcd1e07b2e (plain)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
'use strict';
const common = require('../common');
const assert = require('assert');
const zlib = require('zlib');

const expectStr = 'abcdefghijklmnopqrstuvwxyz'.repeat(2);
const expectBuf = Buffer.from(expectStr);

function createWriter(target, buffer) {
  const writer = { size: 0 };
  const write = () => {
    target.write(Buffer.from([buffer[writer.size++]]), () => {
      if (writer.size < buffer.length) {
        target.flush(write);
      } else {
        target.end();
      }
    });
  };
  write();
  return writer;
}

common.expectWarning(
  'DeprecationWarning',
  'zlib.bytesRead is deprecated and will change its meaning in the ' +
  'future. Use zlib.bytesWritten instead.',
  'DEP0108');

for (const method of [
  ['createGzip', 'createGunzip', false],
  ['createGzip', 'createUnzip', false],
  ['createDeflate', 'createInflate', true],
  ['createDeflateRaw', 'createInflateRaw', true],
  ['createBrotliCompress', 'createBrotliDecompress', true]
]) {
  let compWriter;
  let compData = Buffer.alloc(0);

  const comp = zlib[method[0]]();
  comp.on('data', function(d) {
    compData = Buffer.concat([compData, d]);
    assert.strictEqual(this.bytesWritten, compWriter.size,
                       `Should get write size on ${method[0]} data.`);
  });
  comp.on('end', common.mustCall(function() {
    assert.strictEqual(this.bytesWritten, compWriter.size,
                       `Should get write size on ${method[0]} end.`);
    assert.strictEqual(this.bytesWritten, expectStr.length,
                       `Should get data size on ${method[0]} end.`);

    {
      let decompWriter;
      let decompData = Buffer.alloc(0);

      const decomp = zlib[method[1]]();
      decomp.on('data', function(d) {
        decompData = Buffer.concat([decompData, d]);
        assert.strictEqual(this.bytesWritten, decompWriter.size,
                           `Should get write size on ${method[0]}/` +
                           `${method[1]} data.`);
      });
      decomp.on('end', common.mustCall(function() {
        assert.strictEqual(this.bytesWritten, compData.length,
                           `Should get compressed size on ${method[0]}/` +
                           `${method[1]} end.`);
        assert.strictEqual(decompData.toString(), expectStr,
                           `Should get original string on ${method[0]}/` +
                           `${method[1]} end.`);
      }));
      decompWriter = createWriter(decomp, compData);
    }

    // Some methods should allow extra data after the compressed data
    if (method[2]) {
      const compDataExtra = Buffer.concat([compData, Buffer.from('extra')]);

      let decompWriter;
      let decompData = Buffer.alloc(0);

      const decomp = zlib[method[1]]();
      decomp.on('data', function(d) {
        decompData = Buffer.concat([decompData, d]);
        assert.strictEqual(this.bytesWritten, decompWriter.size,
                           `Should get write size on ${method[0]}/` +
                           `${method[1]} data.`);
      });
      decomp.on('end', common.mustCall(function() {
        assert.strictEqual(this.bytesWritten, compData.length,
                           `Should get compressed size on ${method[0]}/` +
                           `${method[1]} end.`);
        // Checking legacy name.
        assert.strictEqual(this.bytesWritten, this.bytesRead);
        assert.strictEqual(decompData.toString(), expectStr,
                           `Should get original string on ${method[0]}/` +
                           `${method[1]} end.`);
      }));
      decompWriter = createWriter(decomp, compDataExtra);
    }
  }));
  compWriter = createWriter(comp, expectBuf);
}