1'use strict'; 2require('../common'); 3const fixtures = require('../common/fixtures'); 4const assert = require('assert'); 5const zlib = require('zlib'); 6 7// Test some brotli-specific properties of the brotli streams that can not 8// be easily covered through expanding zlib-only tests. 9 10const sampleBuffer = fixtures.readSync('/pss-vectors.json'); 11 12{ 13 // Test setting the quality parameter at stream creation: 14 const sizes = []; 15 for (let quality = zlib.constants.BROTLI_MIN_QUALITY; 16 quality <= zlib.constants.BROTLI_MAX_QUALITY; 17 quality++) { 18 const encoded = zlib.brotliCompressSync(sampleBuffer, { 19 params: { 20 [zlib.constants.BROTLI_PARAM_QUALITY]: quality 21 } 22 }); 23 sizes.push(encoded.length); 24 } 25 26 // Increasing quality should roughly correspond to decreasing compressed size: 27 for (let i = 0; i < sizes.length - 1; i++) { 28 assert(sizes[i + 1] <= sizes[i] * 1.05, sizes); // 5 % margin of error. 29 } 30 assert(sizes[0] > sizes[sizes.length - 1], sizes); 31} 32 33{ 34 // Test that setting out-of-bounds option values or keys fails. 35 assert.throws(() => { 36 zlib.createBrotliCompress({ 37 params: { 38 10000: 0 39 } 40 }); 41 }, { 42 code: 'ERR_BROTLI_INVALID_PARAM', 43 name: 'RangeError', 44 message: '10000 is not a valid Brotli parameter' 45 }); 46 47 // Test that accidentally using duplicate keys fails. 48 assert.throws(() => { 49 zlib.createBrotliCompress({ 50 params: { 51 '0': 0, 52 '00': 0 53 } 54 }); 55 }, { 56 code: 'ERR_BROTLI_INVALID_PARAM', 57 name: 'RangeError', 58 message: '00 is not a valid Brotli parameter' 59 }); 60 61 assert.throws(() => { 62 zlib.createBrotliCompress({ 63 params: { 64 // This is a boolean flag 65 [zlib.constants.BROTLI_PARAM_DISABLE_LITERAL_CONTEXT_MODELING]: 42 66 } 67 }); 68 }, { 69 code: 'ERR_ZLIB_INITIALIZATION_FAILED', 70 name: 'Error', 71 message: 'Initialization failed' 72 }); 73} 74