• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const zlib = require('zlib');
4const assert = require('assert');
5
6const input = '0123456789'.repeat(4);
7
8for (const [ compress, decompressor ] of [
9  [ zlib.deflateRawSync, zlib.createInflateRaw ],
10  [ zlib.deflateSync, zlib.createInflate ],
11  [ zlib.brotliCompressSync, zlib.createBrotliDecompress ],
12]) {
13  const compressed = compress(input);
14  const trailingData = Buffer.from('not valid compressed data');
15
16  for (const variant of [
17    (stream) => { stream.end(compressed); },
18    (stream) => { stream.write(compressed); stream.write(trailingData); },
19    (stream) => { stream.write(compressed); stream.end(trailingData); },
20    (stream) => { stream.write(Buffer.concat([compressed, trailingData])); },
21    (stream) => { stream.end(Buffer.concat([compressed, trailingData])); },
22  ]) {
23    let output = '';
24    const stream = decompressor();
25    stream.setEncoding('utf8');
26    stream.on('data', (chunk) => output += chunk);
27    stream.on('end', common.mustCall(() => {
28      assert.strictEqual(output, input);
29      assert.strictEqual(stream.bytesWritten, compressed.length);
30    }));
31    variant(stream);
32  }
33}
34