1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4const zlib = require('zlib'); 5 6const expectStr = 'abcdefghijklmnopqrstuvwxyz'.repeat(2); 7const expectBuf = Buffer.from(expectStr); 8 9function createWriter(target, buffer) { 10 const writer = { size: 0 }; 11 const write = () => { 12 target.write(Buffer.from([buffer[writer.size++]]), () => { 13 if (writer.size < buffer.length) { 14 target.flush(write); 15 } else { 16 target.end(); 17 } 18 }); 19 }; 20 write(); 21 return writer; 22} 23 24common.expectWarning( 25 'DeprecationWarning', 26 'zlib.bytesRead is deprecated and will change its meaning in the ' + 27 'future. Use zlib.bytesWritten instead.', 28 'DEP0108'); 29 30for (const method of [ 31 ['createGzip', 'createGunzip', false], 32 ['createGzip', 'createUnzip', false], 33 ['createDeflate', 'createInflate', true], 34 ['createDeflateRaw', 'createInflateRaw', true], 35 ['createBrotliCompress', 'createBrotliDecompress', true], 36]) { 37 let compWriter; 38 let compData = Buffer.alloc(0); 39 40 const comp = zlib[method[0]](); 41 comp.on('data', function(d) { 42 compData = Buffer.concat([compData, d]); 43 assert.strictEqual(this.bytesWritten, compWriter.size, 44 `Should get write size on ${method[0]} data.`); 45 }); 46 comp.on('end', common.mustCall(function() { 47 assert.strictEqual(this.bytesWritten, compWriter.size, 48 `Should get write size on ${method[0]} end.`); 49 assert.strictEqual(this.bytesWritten, expectStr.length, 50 `Should get data size on ${method[0]} end.`); 51 52 { 53 let decompWriter; 54 let decompData = Buffer.alloc(0); 55 56 const decomp = zlib[method[1]](); 57 decomp.on('data', function(d) { 58 decompData = Buffer.concat([decompData, d]); 59 assert.strictEqual(this.bytesWritten, decompWriter.size, 60 `Should get write size on ${method[0]}/` + 61 `${method[1]} data.`); 62 }); 63 decomp.on('end', common.mustCall(function() { 64 assert.strictEqual(this.bytesWritten, compData.length, 65 `Should get compressed size on ${method[0]}/` + 66 `${method[1]} end.`); 67 assert.strictEqual(decompData.toString(), expectStr, 68 `Should get original string on ${method[0]}/` + 69 `${method[1]} end.`); 70 })); 71 decompWriter = createWriter(decomp, compData); 72 } 73 74 // Some methods should allow extra data after the compressed data 75 if (method[2]) { 76 const compDataExtra = Buffer.concat([compData, Buffer.from('extra')]); 77 78 let decompWriter; 79 let decompData = Buffer.alloc(0); 80 81 const decomp = zlib[method[1]](); 82 decomp.on('data', function(d) { 83 decompData = Buffer.concat([decompData, d]); 84 assert.strictEqual(this.bytesWritten, decompWriter.size, 85 `Should get write size on ${method[0]}/` + 86 `${method[1]} data.`); 87 }); 88 decomp.on('end', common.mustCall(function() { 89 assert.strictEqual(this.bytesWritten, compData.length, 90 `Should get compressed size on ${method[0]}/` + 91 `${method[1]} end.`); 92 // Checking legacy name. 93 assert.strictEqual(this.bytesWritten, this.bytesRead); 94 assert.strictEqual(decompData.toString(), expectStr, 95 `Should get original string on ${method[0]}/` + 96 `${method[1]} end.`); 97 })); 98 decompWriter = createWriter(decomp, compDataExtra); 99 } 100 })); 101 compWriter = createWriter(comp, expectBuf); 102} 103