1'use strict'; 2 3require('../common'); 4 5const zlib = require('zlib'); 6const assert = require('assert'); 7 8// Work with and without `new` keyword 9assert.ok(zlib.Deflate() instanceof zlib.Deflate); 10assert.ok(new zlib.Deflate() instanceof zlib.Deflate); 11 12assert.ok(zlib.DeflateRaw() instanceof zlib.DeflateRaw); 13assert.ok(new zlib.DeflateRaw() instanceof zlib.DeflateRaw); 14 15// Throws if `options.chunkSize` is invalid 16assert.throws( 17 () => new zlib.Deflate({ chunkSize: 'test' }), 18 { 19 code: 'ERR_INVALID_ARG_TYPE', 20 name: 'TypeError', 21 message: 'The "options.chunkSize" property must be of type number. ' + 22 "Received type string ('test')" 23 } 24); 25 26assert.throws( 27 () => new zlib.Deflate({ chunkSize: -Infinity }), 28 { 29 code: 'ERR_OUT_OF_RANGE', 30 name: 'RangeError', 31 message: 'The value of "options.chunkSize" is out of range. It must ' + 32 'be a finite number. Received -Infinity' 33 } 34); 35 36assert.throws( 37 () => new zlib.Deflate({ chunkSize: 0 }), 38 { 39 code: 'ERR_OUT_OF_RANGE', 40 name: 'RangeError', 41 message: 'The value of "options.chunkSize" is out of range. It must ' + 42 'be >= 64. Received 0' 43 } 44); 45 46// Confirm that maximum chunk size cannot be exceeded because it is `Infinity`. 47assert.strictEqual(zlib.constants.Z_MAX_CHUNK, Infinity); 48 49// Throws if `options.windowBits` is invalid 50assert.throws( 51 () => new zlib.Deflate({ windowBits: 'test' }), 52 { 53 code: 'ERR_INVALID_ARG_TYPE', 54 name: 'TypeError', 55 message: 'The "options.windowBits" property must be of type number. ' + 56 "Received type string ('test')" 57 } 58); 59 60assert.throws( 61 () => new zlib.Deflate({ windowBits: -Infinity }), 62 { 63 code: 'ERR_OUT_OF_RANGE', 64 name: 'RangeError', 65 message: 'The value of "options.windowBits" is out of range. It must ' + 66 'be a finite number. Received -Infinity' 67 } 68); 69 70assert.throws( 71 () => new zlib.Deflate({ windowBits: Infinity }), 72 { 73 code: 'ERR_OUT_OF_RANGE', 74 name: 'RangeError', 75 message: 'The value of "options.windowBits" is out of range. It must ' + 76 'be a finite number. Received Infinity' 77 } 78); 79 80assert.throws( 81 () => new zlib.Deflate({ windowBits: 0 }), 82 { 83 code: 'ERR_OUT_OF_RANGE', 84 name: 'RangeError', 85 message: 'The value of "options.windowBits" is out of range. It must ' + 86 'be >= 8 and <= 15. Received 0' 87 } 88); 89 90// Throws if `options.level` is invalid 91assert.throws( 92 () => new zlib.Deflate({ level: 'test' }), 93 { 94 code: 'ERR_INVALID_ARG_TYPE', 95 name: 'TypeError', 96 message: 'The "options.level" property must be of type number. ' + 97 "Received type string ('test')" 98 } 99); 100 101assert.throws( 102 () => new zlib.Deflate({ level: -Infinity }), 103 { 104 code: 'ERR_OUT_OF_RANGE', 105 name: 'RangeError', 106 message: 'The value of "options.level" is out of range. It must ' + 107 'be a finite number. Received -Infinity' 108 } 109); 110 111assert.throws( 112 () => new zlib.Deflate({ level: Infinity }), 113 { 114 code: 'ERR_OUT_OF_RANGE', 115 name: 'RangeError', 116 message: 'The value of "options.level" is out of range. It must ' + 117 'be a finite number. Received Infinity' 118 } 119); 120 121assert.throws( 122 () => new zlib.Deflate({ level: -2 }), 123 { 124 code: 'ERR_OUT_OF_RANGE', 125 name: 'RangeError', 126 message: 'The value of "options.level" is out of range. It must ' + 127 'be >= -1 and <= 9. Received -2' 128 } 129); 130 131// Throws if `level` invalid in `Deflate.prototype.params()` 132assert.throws( 133 () => new zlib.Deflate().params('test'), 134 { 135 code: 'ERR_INVALID_ARG_TYPE', 136 name: 'TypeError', 137 message: 'The "level" argument must be of type number. ' + 138 "Received type string ('test')" 139 } 140); 141 142assert.throws( 143 () => new zlib.Deflate().params(-Infinity), 144 { 145 code: 'ERR_OUT_OF_RANGE', 146 name: 'RangeError', 147 message: 'The value of "level" is out of range. It must ' + 148 'be a finite number. Received -Infinity' 149 } 150); 151 152assert.throws( 153 () => new zlib.Deflate().params(Infinity), 154 { 155 code: 'ERR_OUT_OF_RANGE', 156 name: 'RangeError', 157 message: 'The value of "level" is out of range. It must ' + 158 'be a finite number. Received Infinity' 159 } 160); 161 162assert.throws( 163 () => new zlib.Deflate().params(-2), 164 { 165 code: 'ERR_OUT_OF_RANGE', 166 name: 'RangeError', 167 message: 'The value of "level" is out of range. It must ' + 168 'be >= -1 and <= 9. Received -2' 169 } 170); 171 172// Throws if options.memLevel is invalid 173assert.throws( 174 () => new zlib.Deflate({ memLevel: 'test' }), 175 { 176 code: 'ERR_INVALID_ARG_TYPE', 177 name: 'TypeError', 178 message: 'The "options.memLevel" property must be of type number. ' + 179 "Received type string ('test')" 180 } 181); 182 183assert.throws( 184 () => new zlib.Deflate({ memLevel: -Infinity }), 185 { 186 code: 'ERR_OUT_OF_RANGE', 187 name: 'RangeError', 188 message: 'The value of "options.memLevel" is out of range. It must ' + 189 'be a finite number. Received -Infinity' 190 } 191); 192 193assert.throws( 194 () => new zlib.Deflate({ memLevel: Infinity }), 195 { 196 code: 'ERR_OUT_OF_RANGE', 197 name: 'RangeError', 198 message: 'The value of "options.memLevel" is out of range. It must ' + 199 'be a finite number. Received Infinity' 200 } 201); 202 203assert.throws( 204 () => new zlib.Deflate({ memLevel: -2 }), 205 { 206 code: 'ERR_OUT_OF_RANGE', 207 name: 'RangeError', 208 message: 'The value of "options.memLevel" is out of range. It must ' + 209 'be >= 1 and <= 9. Received -2' 210 } 211); 212 213// Does not throw if opts.strategy is valid 214new zlib.Deflate({ strategy: zlib.constants.Z_FILTERED }); 215new zlib.Deflate({ strategy: zlib.constants.Z_HUFFMAN_ONLY }); 216new zlib.Deflate({ strategy: zlib.constants.Z_RLE }); 217new zlib.Deflate({ strategy: zlib.constants.Z_FIXED }); 218new zlib.Deflate({ strategy: zlib.constants.Z_DEFAULT_STRATEGY }); 219 220// Throws if options.strategy is invalid 221assert.throws( 222 () => new zlib.Deflate({ strategy: 'test' }), 223 { 224 code: 'ERR_INVALID_ARG_TYPE', 225 name: 'TypeError', 226 message: 'The "options.strategy" property must be of type number. ' + 227 "Received type string ('test')" 228 } 229); 230 231assert.throws( 232 () => new zlib.Deflate({ strategy: -Infinity }), 233 { 234 code: 'ERR_OUT_OF_RANGE', 235 name: 'RangeError', 236 message: 'The value of "options.strategy" is out of range. It must ' + 237 'be a finite number. Received -Infinity' 238 } 239); 240 241assert.throws( 242 () => new zlib.Deflate({ strategy: Infinity }), 243 { 244 code: 'ERR_OUT_OF_RANGE', 245 name: 'RangeError', 246 message: 'The value of "options.strategy" is out of range. It must ' + 247 'be a finite number. Received Infinity' 248 } 249); 250 251assert.throws( 252 () => new zlib.Deflate({ strategy: -2 }), 253 { 254 code: 'ERR_OUT_OF_RANGE', 255 name: 'RangeError', 256 message: 'The value of "options.strategy" is out of range. It must ' + 257 'be >= 0 and <= 4. Received -2' 258 } 259); 260 261// Throws TypeError if `strategy` is invalid in `Deflate.prototype.params()` 262assert.throws( 263 () => new zlib.Deflate().params(0, 'test'), 264 { 265 code: 'ERR_INVALID_ARG_TYPE', 266 name: 'TypeError', 267 message: 'The "strategy" argument must be of type number. ' + 268 "Received type string ('test')" 269 } 270); 271 272assert.throws( 273 () => new zlib.Deflate().params(0, -Infinity), 274 { 275 code: 'ERR_OUT_OF_RANGE', 276 name: 'RangeError', 277 message: 'The value of "strategy" is out of range. It must ' + 278 'be a finite number. Received -Infinity' 279 } 280); 281 282assert.throws( 283 () => new zlib.Deflate().params(0, Infinity), 284 { 285 code: 'ERR_OUT_OF_RANGE', 286 name: 'RangeError', 287 message: 'The value of "strategy" is out of range. It must ' + 288 'be a finite number. Received Infinity' 289 } 290); 291 292assert.throws( 293 () => new zlib.Deflate().params(0, -2), 294 { 295 code: 'ERR_OUT_OF_RANGE', 296 name: 'RangeError', 297 message: 'The value of "strategy" is out of range. It must ' + 298 'be >= 0 and <= 4. Received -2' 299 } 300); 301 302// Throws if opts.dictionary is not a Buffer 303assert.throws( 304 () => new zlib.Deflate({ dictionary: 'not a buffer' }), 305 { 306 code: 'ERR_INVALID_ARG_TYPE', 307 name: 'TypeError', 308 message: 'The "options.dictionary" property must be an instance of Buffer' + 309 ', TypedArray, DataView, or ArrayBuffer. Received type string ' + 310 "('not a buffer')" 311 } 312); 313