-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Hackzzila <[email protected]> Backport-PR-URL: #27681 PR-URL: #24938 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Jan Krems <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Myles Borins <[email protected]> Reviewed-By: Ali Ijaz Sheikh <[email protected]> Reviewed-By: Daniel Bevenius <[email protected]>
- Loading branch information
1 parent
53e9c85
commit c670358
Showing
16 changed files
with
267 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict'; | ||
require('../common'); | ||
const assert = require('assert'); | ||
const zlib = require('zlib'); | ||
const fixtures = require('../common/fixtures'); | ||
|
||
const file = fixtures.readSync('person.jpg'); | ||
const chunkSize = 16; | ||
const deflater = new zlib.BrotliCompress(); | ||
|
||
const chunk = file.slice(0, chunkSize); | ||
const expectedFull = Buffer.from('iweA/9j/4AAQSkZJRgABAQEASA==', 'base64'); | ||
let actualFull; | ||
|
||
deflater.write(chunk, function() { | ||
deflater.flush(function() { | ||
const bufs = []; | ||
let buf; | ||
while (buf = deflater.read()) | ||
bufs.push(buf); | ||
actualFull = Buffer.concat(bufs); | ||
}); | ||
}); | ||
|
||
process.once('exit', function() { | ||
assert.deepStrictEqual(actualFull, expectedFull); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict'; | ||
// Test unzipping a file that was created with a non-node brotli lib, | ||
// piped in as fast as possible. | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const zlib = require('zlib'); | ||
const path = require('path'); | ||
const fixtures = require('../common/fixtures'); | ||
|
||
const tmpdir = require('../common/tmpdir'); | ||
tmpdir.refresh(); | ||
|
||
const decompress = new zlib.BrotliDecompress(); | ||
|
||
const fs = require('fs'); | ||
|
||
const fixture = fixtures.path('person.jpg.br'); | ||
const unzippedFixture = fixtures.path('person.jpg'); | ||
const outputFile = path.resolve(tmpdir.path, 'person.jpg'); | ||
const expect = fs.readFileSync(unzippedFixture); | ||
const inp = fs.createReadStream(fixture); | ||
const out = fs.createWriteStream(outputFile); | ||
|
||
inp.pipe(decompress).pipe(out); | ||
out.on('close', common.mustCall(() => { | ||
const actual = fs.readFileSync(outputFile); | ||
assert.strictEqual(actual.length, expect.length); | ||
for (let i = 0, l = actual.length; i < l; i++) { | ||
assert.strictEqual(actual[i], expect[i], `byte[${i}]`); | ||
} | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
// Test compressing and uncompressing a string with brotli | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const zlib = require('zlib'); | ||
|
||
const inputString = 'ΩΩLorem ipsum dolor sit amet, consectetur adipiscing eli' + | ||
't. Morbi faucibus, purus at gravida dictum, libero arcu ' + | ||
'convallis lacus, in commodo libero metus eu nisi. Nullam' + | ||
' commodo, neque nec porta placerat, nisi est fermentum a' + | ||
'ugue, vitae gravida tellus sapien sit amet tellus. Aenea' + | ||
'n non diam orci. Proin quis elit turpis. Suspendisse non' + | ||
' diam ipsum. Suspendisse nec ullamcorper odio. Vestibulu' + | ||
'm arcu mi, sodales non suscipit id, ultrices ut massa. S' + | ||
'ed ac sem sit amet arcu malesuada fermentum. Nunc sed. '; | ||
const expectedBase64Compress = 'G/gBQBwHdky2aHV5KK9Snf05//1pPdmNw/7232fnIm1IB' + | ||
'K1AA8RsN8OB8Nb7Lpgk3UWWUlzQXZyHQeBBbXMTQXC1j7' + | ||
'wg3LJs9LqOGHRH2bj/a2iCTLLx8hBOyTqgoVuD1e+Qqdn' + | ||
'f1rkUNyrWq6LtOhWgxP3QUwdhKGdZm3rJWaDDBV7+pDk1' + | ||
'MIkrmjp4ma2xVi5MsgJScA3tP1I7mXeby6MELozrwoBQD' + | ||
'mVTnEAicZNj4lkGqntJe2qSnGyeMmcFgraK94vCg/4iLu' + | ||
'Tw5RhKhnVY++dZ6niUBmRqIutsjf5TzwF5iAg8a9UkjF5' + | ||
'2eZ0tB2vo6v8SqVfNMkBmmhxr0NT9LkYF69aEjlYzj7IE' + | ||
'KmEUQf1HBogRYhFIt4ymRNEgHAIzOyNEsQM='; | ||
|
||
zlib.brotliCompress(inputString, common.mustCall((err, buffer) => { | ||
assert.strictEqual(buffer.toString('base64'), expectedBase64Compress); | ||
})); | ||
|
||
const buffer = Buffer.from(expectedBase64Compress, 'base64'); | ||
zlib.brotliDecompress(buffer, common.mustCall((err, buffer) => { | ||
assert.strictEqual(buffer.toString(), inputString); | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict'; | ||
require('../common'); | ||
|
||
// This test ensures that zlib throws a RangeError if the final buffer needs to | ||
// be larger than kMaxLength and concatenation fails. | ||
// https://github.com/nodejs/node/pull/1811 | ||
|
||
const assert = require('assert'); | ||
|
||
// Change kMaxLength for zlib to trigger the error without having to allocate | ||
// large Buffers. | ||
const buffer = require('buffer'); | ||
const oldkMaxLength = buffer.kMaxLength; | ||
buffer.kMaxLength = 128; | ||
const zlib = require('zlib'); | ||
buffer.kMaxLength = oldkMaxLength; | ||
|
||
const encoded = Buffer.from('G38A+CXCIrFAIAM=', 'base64'); | ||
|
||
// Async | ||
zlib.brotliDecompress(encoded, function(err) { | ||
assert.ok(err instanceof RangeError); | ||
}); | ||
|
||
// Sync | ||
assert.throws(function() { | ||
zlib.brotliDecompressSync(encoded); | ||
}, RangeError); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const fixtures = require('../common/fixtures'); | ||
const assert = require('assert'); | ||
const zlib = require('zlib'); | ||
|
||
// Test some brotli-specific properties of the brotli streams that can not | ||
// be easily covered through expanding zlib-only tests. | ||
|
||
const sampleBuffer = fixtures.readSync('/pss-vectors.json'); | ||
|
||
{ | ||
// Test setting the quality parameter at stream creation: | ||
const sizes = []; | ||
for (let quality = zlib.constants.BROTLI_MIN_QUALITY; | ||
quality <= zlib.constants.BROTLI_MAX_QUALITY; | ||
quality++) { | ||
const encoded = zlib.brotliCompressSync(sampleBuffer, { | ||
params: { | ||
[zlib.constants.BROTLI_PARAM_QUALITY]: quality | ||
} | ||
}); | ||
sizes.push(encoded.length); | ||
} | ||
|
||
// Increasing quality should roughly correspond to decreasing compressed size: | ||
for (let i = 0; i < sizes.length - 1; i++) { | ||
assert(sizes[i + 1] <= sizes[i] * 1.05, sizes); // 5 % margin of error. | ||
} | ||
assert(sizes[0] > sizes[sizes.length - 1], sizes); | ||
} | ||
|
||
{ | ||
// Test that setting out-of-bounds option values or keys fails. | ||
common.expectsError(() => { | ||
zlib.createBrotliCompress({ | ||
params: { | ||
10000: 0 | ||
} | ||
}); | ||
}, { | ||
code: 'ERR_BROTLI_INVALID_PARAM', | ||
type: RangeError, | ||
message: '10000 is not a valid Brotli parameter' | ||
}); | ||
|
||
// Test that accidentally using duplicate keys fails. | ||
common.expectsError(() => { | ||
zlib.createBrotliCompress({ | ||
params: { | ||
'0': 0, | ||
'00': 0 | ||
} | ||
}); | ||
}, { | ||
code: 'ERR_BROTLI_INVALID_PARAM', | ||
type: RangeError, | ||
message: '00 is not a valid Brotli parameter' | ||
}); | ||
|
||
common.expectsError(() => { | ||
zlib.createBrotliCompress({ | ||
params: { | ||
// This is a boolean flag | ||
[zlib.constants.BROTLI_PARAM_DISABLE_LITERAL_CONTEXT_MODELING]: 42 | ||
} | ||
}); | ||
}, { | ||
code: 'ERR_ZLIB_INITIALIZATION_FAILED', | ||
type: Error, | ||
message: 'Initialization failed' | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters