1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23// Test convenience methods with and without options supplied 24 25const common = require('../common'); 26const assert = require('assert'); 27const zlib = require('zlib'); 28 29// Must be a multiple of 4 characters in total to test all ArrayBufferView 30// types. 31const expectStr = 'blah'.repeat(8); 32const expectBuf = Buffer.from(expectStr); 33 34const opts = { 35 level: 9, 36 chunkSize: 1024, 37}; 38 39const optsInfo = { 40 info: true 41}; 42 43for (const [type, expect] of [ 44 ['string', expectStr], 45 ['Buffer', expectBuf], 46 ...common.getBufferSources(expectBuf).map((obj) => 47 [obj[Symbol.toStringTag], obj] 48 ), 49]) { 50 for (const method of [ 51 ['gzip', 'gunzip', 'Gzip', 'Gunzip'], 52 ['gzip', 'unzip', 'Gzip', 'Unzip'], 53 ['deflate', 'inflate', 'Deflate', 'Inflate'], 54 ['deflateRaw', 'inflateRaw', 'DeflateRaw', 'InflateRaw'], 55 ['brotliCompress', 'brotliDecompress', 56 'BrotliCompress', 'BrotliDecompress'], 57 ]) { 58 zlib[method[0]](expect, opts, common.mustCall((err, result) => { 59 zlib[method[1]](result, opts, common.mustCall((err, result) => { 60 assert.strictEqual(result.toString(), expectStr, 61 `Should get original string after ${method[0]}/` + 62 `${method[1]} ${type} with options.`); 63 })); 64 })); 65 66 zlib[method[0]](expect, common.mustCall((err, result) => { 67 zlib[method[1]](result, common.mustCall((err, result) => { 68 assert.strictEqual(result.toString(), expectStr, 69 `Should get original string after ${method[0]}/` + 70 `${method[1]} ${type} without options.`); 71 })); 72 })); 73 74 zlib[method[0]](expect, optsInfo, common.mustCall((err, result) => { 75 assert.ok(result.engine instanceof zlib[method[2]], 76 `Should get engine ${method[2]} after ${method[0]} ` + 77 `${type} with info option.`); 78 79 const compressed = result.buffer; 80 zlib[method[1]](compressed, optsInfo, common.mustCall((err, result) => { 81 assert.strictEqual(result.buffer.toString(), expectStr, 82 `Should get original string after ${method[0]}/` + 83 `${method[1]} ${type} with info option.`); 84 assert.ok(result.engine instanceof zlib[method[3]], 85 `Should get engine ${method[3]} after ${method[0]} ` + 86 `${type} with info option.`); 87 })); 88 })); 89 90 { 91 const compressed = zlib[`${method[0]}Sync`](expect, opts); 92 const decompressed = zlib[`${method[1]}Sync`](compressed, opts); 93 assert.strictEqual(decompressed.toString(), expectStr, 94 `Should get original string after ${method[0]}Sync/` + 95 `${method[1]}Sync ${type} with options.`); 96 } 97 98 99 { 100 const compressed = zlib[`${method[0]}Sync`](expect); 101 const decompressed = zlib[`${method[1]}Sync`](compressed); 102 assert.strictEqual(decompressed.toString(), expectStr, 103 `Should get original string after ${method[0]}Sync/` + 104 `${method[1]}Sync ${type} without options.`); 105 } 106 107 108 { 109 const compressed = zlib[`${method[0]}Sync`](expect, optsInfo); 110 assert.ok(compressed.engine instanceof zlib[method[2]], 111 `Should get engine ${method[2]} after ${method[0]} ` + 112 `${type} with info option.`); 113 const decompressed = zlib[`${method[1]}Sync`](compressed.buffer, 114 optsInfo); 115 assert.strictEqual(decompressed.buffer.toString(), expectStr, 116 `Should get original string after ${method[0]}Sync/` + 117 `${method[1]}Sync ${type} without options.`); 118 assert.ok(decompressed.engine instanceof zlib[method[3]], 119 `Should get engine ${method[3]} after ${method[0]} ` + 120 `${type} with info option.`); 121 } 122 } 123} 124 125assert.throws( 126 () => zlib.gzip('abc'), 127 { 128 code: 'ERR_INVALID_ARG_TYPE', 129 name: 'TypeError', 130 message: 'The "callback" argument must be of type function. ' + 131 'Received undefined' 132 } 133); 134