| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 7edf8c7 commit 9a627a4
16 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,7 +4,8 @@ const zlib = require('zlib'); | |||
| 4 | 4 | ||
| 5 | 5 | const bench = common.createBenchmark(main, { | |
| 6 | 6 | type: [ | |
| 7 | - 'Deflate', 'DeflateRaw', 'Inflate', 'InflateRaw', 'Gzip', 'Gunzip', 'Unzip' | ||
| 7 | + 'Deflate', 'DeflateRaw', 'Inflate', 'InflateRaw', 'Gzip', 'Gunzip', 'Unzip', | ||
| 8 | + 'BrotliCompress', 'BrotliDecompress' | ||
| 8 | 9 | ], | |
| 9 | 10 | options: ['true', 'false'], | |
| 10 | 11 | n: [5e5] | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,15 +6,18 @@ const zlib = require('zlib'); | |||
| 6 | 6 | const bench = common.createBenchmark(main, { | |
| 7 | 7 | inputLen: [1024], | |
| 8 | 8 | duration: [5], | |
| 9 | - type: ['string', 'buffer'] | ||
| 9 | + type: ['string', 'buffer'], | ||
| 10 | + algorithm: ['gzip', 'brotli'] | ||
| 10 | 11 | }); | |
| 11 | 12 | ||
| 12 | - function main({ inputLen, duration, type }) { | ||
| 13 | + function main({ inputLen, duration, type, algorithm }) { | ||
| 13 | 14 | const buffer = Buffer.alloc(inputLen, fs.readFileSync(__filename)); | |
| 14 | 15 | const chunk = type === 'buffer' ? buffer : buffer.toString('utf8'); | |
| 15 | 16 | ||
| 16 | - const input = zlib.createGzip(); | ||
| 17 | - const output = zlib.createGunzip(); | ||
| 17 | + const input = algorithm === 'gzip' ? | ||
| 18 | + zlib.createGzip() : zlib.createBrotliCompress(); | ||
| 19 | + const output = algorithm === 'gzip' ? | ||
| 20 | + zlib.createGunzip() : zlib.createBrotliDecompress(); | ||
| 18 | 21 | ||
| 19 | 22 | let readFromOutput = 0; | |
| 20 | 23 | input.pipe(output); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,27 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + require('../common'); | ||
| 3 | + const assert = require('assert'); | ||
| 4 | + const zlib = require('zlib'); | ||
| 5 | + const fixtures = require('../common/fixtures'); | ||
| 6 | + | ||
| 7 | + const file = fixtures.readSync('person.jpg'); | ||
| 8 | + const chunkSize = 16; | ||
| 9 | + const deflater = new zlib.BrotliCompress(); | ||
| 10 | + | ||
| 11 | + const chunk = file.slice(0, chunkSize); | ||
| 12 | + const expectedFull = Buffer.from('iweA/9j/4AAQSkZJRgABAQEASA==', 'base64'); | ||
| 13 | + let actualFull; | ||
| 14 | + | ||
| 15 | + deflater.write(chunk, function() { | ||
| 16 | + deflater.flush(function() { | ||
| 17 | + const bufs = []; | ||
| 18 | + let buf; | ||
| 19 | + while (buf = deflater.read()) | ||
| 20 | + bufs.push(buf); | ||
| 21 | + actualFull = Buffer.concat(bufs); | ||
| 22 | + }); | ||
| 23 | + }); | ||
| 24 | + | ||
| 25 | + process.once('exit', function() { | ||
| 26 | + assert.deepStrictEqual(actualFull, expectedFull); | ||
| 27 | + }); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,32 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + // Test unzipping a file that was created with a non-node brotli lib, | ||
| 3 | + // piped in as fast as possible. | ||
| 4 | + | ||
| 5 | + const common = require('../common'); | ||
| 6 | + const assert = require('assert'); | ||
| 7 | + const zlib = require('zlib'); | ||
| 8 | + const path = require('path'); | ||
| 9 | + const fixtures = require('../common/fixtures'); | ||
| 10 | + | ||
| 11 | + const tmpdir = require('../common/tmpdir'); | ||
| 12 | + tmpdir.refresh(); | ||
| 13 | + | ||
| 14 | + const decompress = new zlib.BrotliDecompress(); | ||
| 15 | + | ||
| 16 | + const fs = require('fs'); | ||
| 17 | + | ||
| 18 | + const fixture = fixtures.path('person.jpg.br'); | ||
| 19 | + const unzippedFixture = fixtures.path('person.jpg'); | ||
| 20 | + const outputFile = path.resolve(tmpdir.path, 'person.jpg'); | ||
| 21 | + const expect = fs.readFileSync(unzippedFixture); | ||
| 22 | + const inp = fs.createReadStream(fixture); | ||
| 23 | + const out = fs.createWriteStream(outputFile); | ||
| 24 | + | ||
| 25 | + inp.pipe(decompress).pipe(out); | ||
| 26 | + out.on('close', common.mustCall(() => { | ||
| 27 | + const actual = fs.readFileSync(outputFile); | ||
| 28 | + assert.strictEqual(actual.length, expect.length); | ||
| 29 | + for (let i = 0, l = actual.length; i < l; i++) { | ||
| 30 | + assert.strictEqual(actual[i], expect[i], `byte[${i}]`); | ||
| 31 | + } | ||
| 32 | + })); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,34 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + // Test compressing and uncompressing a string with brotli | ||
| 3 | + | ||
| 4 | + const common = require('../common'); | ||
| 5 | + const assert = require('assert'); | ||
| 6 | + const zlib = require('zlib'); | ||
| 7 | + | ||
| 8 | + const inputString = 'ΩΩLorem ipsum dolor sit amet, consectetur adipiscing eli' + | ||
| 9 | + 't. Morbi faucibus, purus at gravida dictum, libero arcu ' + | ||
| 10 | + 'convallis lacus, in commodo libero metus eu nisi. Nullam' + | ||
| 11 | + ' commodo, neque nec porta placerat, nisi est fermentum a' + | ||
| 12 | + 'ugue, vitae gravida tellus sapien sit amet tellus. Aenea' + | ||
| 13 | + 'n non diam orci. Proin quis elit turpis. Suspendisse non' + | ||
| 14 | + ' diam ipsum. Suspendisse nec ullamcorper odio. Vestibulu' + | ||
| 15 | + 'm arcu mi, sodales non suscipit id, ultrices ut massa. S' + | ||
| 16 | + 'ed ac sem sit amet arcu malesuada fermentum. Nunc sed. '; | ||
| 17 | + const expectedBase64Compress = 'G/gBQBwHdky2aHV5KK9Snf05//1pPdmNw/7232fnIm1IB' + | ||
| 18 | + 'K1AA8RsN8OB8Nb7Lpgk3UWWUlzQXZyHQeBBbXMTQXC1j7' + | ||
| 19 | + 'wg3LJs9LqOGHRH2bj/a2iCTLLx8hBOyTqgoVuD1e+Qqdn' + | ||
| 20 | + 'f1rkUNyrWq6LtOhWgxP3QUwdhKGdZm3rJWaDDBV7+pDk1' + | ||
| 21 | + 'MIkrmjp4ma2xVi5MsgJScA3tP1I7mXeby6MELozrwoBQD' + | ||
| 22 | + 'mVTnEAicZNj4lkGqntJe2qSnGyeMmcFgraK94vCg/4iLu' + | ||
| 23 | + 'Tw5RhKhnVY++dZ6niUBmRqIutsjf5TzwF5iAg8a9UkjF5' + | ||
| 24 | + '2eZ0tB2vo6v8SqVfNMkBmmhxr0NT9LkYF69aEjlYzj7IE' + | ||
| 25 | + 'KmEUQf1HBogRYhFIt4ymRNEgHAIzOyNEsQM='; | ||
| 26 | + | ||
| 27 | + zlib.brotliCompress(inputString, common.mustCall((err, buffer) => { | ||
| 28 | + assert.strictEqual(buffer.toString('base64'), expectedBase64Compress); | ||
| 29 | + })); | ||
| 30 | + | ||
| 31 | + const buffer = Buffer.from(expectedBase64Compress, 'base64'); | ||
| 32 | + zlib.brotliDecompress(buffer, common.mustCall((err, buffer) => { | ||
| 33 | + assert.strictEqual(buffer.toString(), inputString); | ||
| 34 | + })); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,28 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + require('../common'); | ||
| 3 | + | ||
| 4 | + // This test ensures that zlib throws a RangeError if the final buffer needs to | ||
| 5 | + // be larger than kMaxLength and concatenation fails. | ||
| 6 | + // https://github.com/nodejs/node/pull/1811 | ||
| 7 | + | ||
| 8 | + const assert = require('assert'); | ||
| 9 | + | ||
| 10 | + // Change kMaxLength for zlib to trigger the error without having to allocate | ||
| 11 | + // large Buffers. | ||
| 12 | + const buffer = require('buffer'); | ||
| 13 | + const oldkMaxLength = buffer.kMaxLength; | ||
| 14 | + buffer.kMaxLength = 128; | ||
| 15 | + const zlib = require('zlib'); | ||
| 16 | + buffer.kMaxLength = oldkMaxLength; | ||
| 17 | + | ||
| 18 | + const encoded = Buffer.from('G38A+CXCIrFAIAM=', 'base64'); | ||
| 19 | + | ||
| 20 | + // Async | ||
| 21 | + zlib.brotliDecompress(encoded, function(err) { | ||
| 22 | + assert.ok(err instanceof RangeError); | ||
| 23 | + }); | ||
| 24 | + | ||
| 25 | + // Sync | ||
| 26 | + assert.throws(function() { | ||
| 27 | + zlib.brotliDecompressSync(encoded); | ||
| 28 | + }, RangeError); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,73 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + const fixtures = require('../common/fixtures'); | ||
| 4 | + const assert = require('assert'); | ||
| 5 | + const 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 | + | ||
| 10 | + const 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 | + common.expectsError(() => { | ||
| 36 | + zlib.createBrotliCompress({ | ||
| 37 | + params: { | ||
| 38 | + 10000: 0 | ||
| 39 | + } | ||
| 40 | + }); | ||
| 41 | + }, { | ||
| 42 | + code: 'ERR_BROTLI_INVALID_PARAM', | ||
| 43 | + type: RangeError, | ||
| 44 | + message: '10000 is not a valid Brotli parameter' | ||
| 45 | + }); | ||
| 46 | + | ||
| 47 | + // Test that accidentally using duplicate keys fails. | ||
| 48 | + common.expectsError(() => { | ||
| 49 | + zlib.createBrotliCompress({ | ||
| 50 | + params: { | ||
| 51 | + '0': 0, | ||
| 52 | + '00': 0 | ||
| 53 | + } | ||
| 54 | + }); | ||
| 55 | + }, { | ||
| 56 | + code: 'ERR_BROTLI_INVALID_PARAM', | ||
| 57 | + type: RangeError, | ||
| 58 | + message: '00 is not a valid Brotli parameter' | ||
| 59 | + }); | ||
| 60 | + | ||
| 61 | + common.expectsError(() => { | ||
| 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 | + type: Error, | ||
| 71 | + message: 'Initialization failed' | ||
| 72 | + }); | ||
| 73 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -31,7 +31,8 @@ for (const method of [ | |||
| 31 | 31 | ['createGzip', 'createGunzip', false], | |
| 32 | 32 | ['createGzip', 'createUnzip', false], | |
| 33 | 33 | ['createDeflate', 'createInflate', true], | |
| 34 | - ['createDeflateRaw', 'createInflateRaw', true] | ||
| 34 | + ['createDeflateRaw', 'createInflateRaw', true], | ||
| 35 | + ['createBrotliCompress', 'createBrotliDecompress', true] | ||
| 35 | 36 | ]) { | |
| 36 | 37 | let compWriter; | |
| 37 | 38 | let compData = Buffer.alloc(0); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -52,6 +52,8 @@ for (const [type, expect] of [ | |||
| 52 | 52 | ['gzip', 'unzip', 'Gzip', 'Unzip'], | |
| 53 | 53 | ['deflate', 'inflate', 'Deflate', 'Inflate'], | |
| 54 | 54 | ['deflateRaw', 'inflateRaw', 'DeflateRaw', 'InflateRaw'], | |
| 55 | + ['brotliCompress', 'brotliDecompress', | ||
| 56 | + 'BrotliCompress', 'BrotliDecompress'], | ||
| 55 | 57 | ]) { | |
| 56 | 58 | zlib[method[0]](expect, opts, common.mustCall((err, result) => { | |
| 57 | 59 | zlib[method[1]](result, opts, common.mustCall((err, result) => { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments