-
-
Notifications
You must be signed in to change notification settings - Fork 34.7k
zlib: add support for brotli compression dictionary #61763
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rokob
wants to merge
3
commits into
nodejs:main
Choose a base branch
from
rokob:aw-52250-brotli-dict
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+229
−11
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,126 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const zlib = require('zlib'); | ||
|
|
||
| const dictionary = Buffer.from( | ||
| `Lorem ipsum dolor sit amet, consectetur adipiscing elit. | ||
| Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. | ||
| Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.` | ||
| ); | ||
|
|
||
| const input = Buffer.from( | ||
| `Lorem ipsum dolor sit amet, consectetur adipiscing elit. | ||
| Lorem ipsum dolor sit amet, consectetur adipiscing elit. | ||
| Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. | ||
| Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. | ||
| Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.` | ||
| ); | ||
|
|
||
| // Test with convenience methods (async). | ||
| zlib.brotliCompress(input, { dictionary }, common.mustSucceed((compressed) => { | ||
| assert(compressed.length < input.length, | ||
| 'compressed data should be smaller with dictionary'); | ||
| zlib.brotliDecompress(compressed, { dictionary }, common.mustSucceed((decompressed) => { | ||
| assert.strictEqual(decompressed.toString(), input.toString()); | ||
| })); | ||
| })); | ||
|
|
||
| // Test with streaming API. | ||
| { | ||
| const encoder = zlib.createBrotliCompress({ dictionary }); | ||
| const decoder = zlib.createBrotliDecompress({ dictionary }); | ||
|
|
||
| const chunks = []; | ||
| decoder.on('data', (chunk) => chunks.push(chunk)); | ||
| decoder.on('end', common.mustCall(() => { | ||
| const result = Buffer.concat(chunks); | ||
| assert.strictEqual(result.toString(), input.toString()); | ||
| })); | ||
|
|
||
| encoder.pipe(decoder); | ||
| encoder.end(input); | ||
| } | ||
|
|
||
| // Test that dictionary improves compression ratio. | ||
| { | ||
| const withDict = zlib.brotliCompressSync(input, { dictionary }); | ||
| const withoutDict = zlib.brotliCompressSync(input); | ||
|
|
||
| // Dictionary-based compression should be at least as good as without. | ||
| assert(withDict.length <= withoutDict.length, | ||
| `Dictionary compression (${withDict.length}) should not be ` + | ||
| `larger than non-dictionary compression (${withoutDict.length})`); | ||
|
|
||
| // Verify decompression with dictionary works. | ||
| const decompressed = zlib.brotliDecompressSync(withDict, { dictionary }); | ||
| assert.strictEqual(decompressed.toString(), input.toString()); | ||
| } | ||
|
|
||
| // Test that decompression without matching dictionary fails. | ||
| { | ||
| const compressed = zlib.brotliCompressSync(input, { dictionary }); | ||
| assert.throws(() => { | ||
| zlib.brotliDecompressSync(compressed); | ||
| }, (err) => { | ||
| assert.match(err.code, /ERR_/); | ||
| return true; | ||
| }); | ||
| } | ||
|
|
||
| // Test that decompression with wrong dictionary fails. | ||
| { | ||
| const compressed = zlib.brotliCompressSync(input, { dictionary }); | ||
| const wrongDictionary = Buffer.from('this is the wrong dictionary'); | ||
| assert.throws(() => { | ||
| zlib.brotliDecompressSync(compressed, { dictionary: wrongDictionary }); | ||
| }, (err) => { | ||
| assert.match(err.code, /ERR_/); | ||
| return true; | ||
| }); | ||
| } | ||
|
|
||
| // Test that dictionary works with ArrayBuffer (converted to Buffer). | ||
| { | ||
| const arrayBufferDict = dictionary.buffer.slice( | ||
| dictionary.byteOffset, | ||
| dictionary.byteOffset + dictionary.byteLength, | ||
| ); | ||
| const compressed = zlib.brotliCompressSync(input, { dictionary: arrayBufferDict }); | ||
| const decompressed = zlib.brotliDecompressSync(compressed, { dictionary: arrayBufferDict }); | ||
| assert.strictEqual(decompressed.toString(), input.toString()); | ||
| } | ||
|
|
||
| // Test that dictionary works with TypedArray (Uint8Array). | ||
| { | ||
| const uint8Dict = new Uint8Array(dictionary); | ||
| const compressed = zlib.brotliCompressSync(input, { dictionary: uint8Dict }); | ||
| const decompressed = zlib.brotliDecompressSync(compressed, { dictionary: uint8Dict }); | ||
| assert.strictEqual(decompressed.toString(), input.toString()); | ||
| } | ||
|
|
||
| // Test that invalid dictionary type throws ERR_INVALID_ARG_TYPE. | ||
| for (const invalidDict of ['string', 123, true, { object: true }, [1, 2, 3]]) { | ||
| assert.throws(() => { | ||
| zlib.createBrotliCompress({ dictionary: invalidDict }); | ||
| }, { code: 'ERR_INVALID_ARG_TYPE' }); | ||
|
|
||
| assert.throws(() => { | ||
| zlib.createBrotliDecompress({ dictionary: invalidDict }); | ||
| }, { code: 'ERR_INVALID_ARG_TYPE' }); | ||
| } | ||
|
|
||
| // Test with streaming API and wrong dictionary emits error event. | ||
| { | ||
| const compressed = zlib.brotliCompressSync(input, { dictionary }); | ||
| const wrongDict = Buffer.from('wrong dictionary data'); | ||
| const decoder = zlib.createBrotliDecompress({ dictionary: wrongDict }); | ||
|
|
||
| decoder.on('error', common.mustCall((err) => { | ||
| assert.match(err.code, /ERR_/); | ||
| })); | ||
|
|
||
| decoder.write(compressed); | ||
| decoder.end(); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.