-
-
Notifications
You must be signed in to change notification settings - Fork 758
Added support for brotli ('br') content-encoding #406
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
Merged
Merged
Added support for brotli ('br') content-encoding #406
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
5 changes: 5 additions & 0 deletions
HISTORY.md
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 |
|---|---|---|
| @@ -1,3 +1,8 @@ | ||
| unreleased | ||
| ========================= | ||
|
|
||
| * add brotli support #406 | ||
|
|
||
| 2.0.0-beta.2 / 2023-02-23 | ||
| ========================= | ||
|
|
||
|
|
||
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 |
|---|---|---|
|
|
@@ -64,8 +64,8 @@ The various errors returned by this module are described in the | |
|
|
||
| Returns middleware that only parses `json` and only looks at requests where | ||
| the `Content-Type` header matches the `type` option. This parser accepts any | ||
| Unicode encoding of the body and supports automatic inflation of `gzip` and | ||
| `deflate` encodings. | ||
| Unicode encoding of the body and supports automatic inflation of `gzip`, | ||
| `br` (brotli) and `deflate` encodings. | ||
|
|
||
| A new `body` object containing the parsed data is populated on the `request` | ||
| object after the middleware (i.e. `req.body`). | ||
|
|
@@ -119,7 +119,8 @@ encoding of the request. The parsing can be aborted by throwing an error. | |
|
|
||
| Returns middleware that parses all bodies as a `Buffer` and only looks at | ||
| requests where the `Content-Type` header matches the `type` option. This | ||
| parser supports automatic inflation of `gzip` and `deflate` encodings. | ||
| parser supports automatic inflation of `gzip`, `br` (brotli) and `deflate` | ||
| encodings. | ||
|
|
||
| A new `body` object containing the parsed data is populated on the `request` | ||
| object after the middleware (i.e. `req.body`). This will be a `Buffer` object | ||
|
|
@@ -164,7 +165,8 @@ encoding of the request. The parsing can be aborted by throwing an error. | |
|
|
||
| Returns middleware that parses all bodies as a string and only looks at | ||
| requests where the `Content-Type` header matches the `type` option. This | ||
| parser supports automatic inflation of `gzip` and `deflate` encodings. | ||
| parser supports automatic inflation of `gzip`, `br` (brotli) and `deflate` | ||
| encodings. | ||
|
|
||
| A new `body` string containing the parsed data is populated on the `request` | ||
| object after the middleware (i.e. `req.body`). This will be a string of the | ||
|
|
@@ -214,7 +216,7 @@ encoding of the request. The parsing can be aborted by throwing an error. | |
| Returns middleware that only parses `urlencoded` bodies and only looks at | ||
| requests where the `Content-Type` header matches the `type` option. This | ||
| parser accepts only UTF-8 encoding of the body and supports automatic | ||
| inflation of `gzip` and `deflate` encodings. | ||
| inflation of `gzip`, `br` (brotli) and `deflate` encodings. | ||
|
|
||
| A new `body` object containing the parsed data is populated on the `request` | ||
| object after the middleware (i.e. `req.body`). This object will contain | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -25,6 +25,12 @@ var zlib = require('zlib') | |
|
|
||
| module.exports = read | ||
|
|
||
| /** | ||
| * @const | ||
| * whether current node version has brotli support | ||
| */ | ||
| var hasBrotliSupport = 'createBrotliDecompress' in zlib | ||
|
|
||
| /** | ||
| * Read a request into a buffer and parse. | ||
| * | ||
|
|
@@ -174,11 +180,20 @@ function contentstream (req, debug, inflate) { | |
| stream = req | ||
| stream.length = length | ||
| break | ||
| default: | ||
| throw createError(415, 'unsupported content encoding "' + encoding + '"', { | ||
| encoding: encoding, | ||
| type: 'encoding.unsupported' | ||
| }) | ||
| case 'br': | ||
| if (hasBrotliSupport) { | ||
| stream = zlib.createBrotliDecompress() | ||
| debug('brotli decompress body') | ||
| req.pipe(stream) | ||
| } | ||
| break | ||
| } | ||
|
|
||
| if (stream === undefined) { | ||
| throw createError(415, 'unsupported content encoding "' + encoding + '"', { | ||
| encoding: encoding, | ||
| type: 'encoding.unsupported' | ||
| }) | ||
| } | ||
|
|
||
| return stream | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -12,6 +12,10 @@ var describeAsyncHooks = typeof asyncHooks.AsyncLocalStorage === 'function' | |
| ? describe | ||
| : describe.skip | ||
|
|
||
| var hasBrotliSupport = 'createBrotliDecompress' in require('zlib') | ||
| var brotlit = hasBrotliSupport ? it : it.skip | ||
| var nobrotlit = !hasBrotliSupport ? it : it.skip | ||
|
|
||
| describe('bodyParser.json()', function () { | ||
| it('should parse JSON', function (done) { | ||
| request(createServer()) | ||
|
|
@@ -683,6 +687,22 @@ describe('bodyParser.json()', function () { | |
| test.expect(200, '{"name":"Lun "}', done) | ||
| }) | ||
|
|
||
| brotlit('should support brotli encoding', function (done) { | ||
| var test = request(this.server).post('/') | ||
| test.set('Content-Encoding', 'br') | ||
| test.set('Content-Type', 'application/json') | ||
| test.write(Buffer.from('8b06807b226e616d65223a22e8aeba227d03 ', 'hex')) | ||
| test.expect(200, '{"name":"Lun "}', done) | ||
| }) | ||
|
|
||
| nobrotlit('should throw 415 if there\'s no brotli support', function (done) { | ||
| var test = request(this.server).post('/') | ||
| test.set('Content-Encoding', 'br') | ||
| test.set('Content-Type', 'application/json') | ||
| test.write(Buffer.from('8b06807b226e616d65223a22e8aeba227d03 ', 'hex')) | ||
| test.expect(415, 'unsupported content encoding "br"', done) | ||
| }) | ||
|
|
||
| it('should be case-insensitive', function (done) { | ||
| var test = request(this.server).post('/') | ||
| test.set('Content-Encoding', 'GZIP') | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -12,6 +12,10 @@ var describeAsyncHooks = typeof asyncHooks.AsyncLocalStorage === 'function' | |
| ? describe | ||
| : describe.skip | ||
|
|
||
| var hasBrotliSupport = 'createBrotliDecompress' in require('zlib') | ||
| var brotlit = hasBrotliSupport ? it : it.skip | ||
| var nobrotlit = !hasBrotliSupport ? it : it.skip | ||
|
|
||
| describe('bodyParser.raw()', function () { | ||
| before(function () { | ||
| this.server = createServer() | ||
|
|
@@ -455,6 +459,22 @@ describe('bodyParser.raw()', function () { | |
| test.expect(200, 'buf:6e616d653de8aeba', done) | ||
| }) | ||
|
|
||
| brotlit('should support brotli encoding', function (done) { | ||
| var test = request(this.server).post('/') | ||
| test.set('Content-Encoding', 'br') | ||
| test.set('Content-Type', 'application/octet-stream') | ||
| test.write(Buffer.from('8b03806e616d653de8aeba03', 'hex')) | ||
| test.expect(200, 'buf:6e616d653de8aeba', done) | ||
| }) | ||
|
|
||
| nobrotlit('should throw 415 if there\'s no brotli support', function (done) { | ||
| var test = request(this.server).post('/') | ||
| test.set('Content-Encoding', 'br') | ||
| test.set('Content-Type', 'application/octet-stream') | ||
| test.write(Buffer.from('8b03806e616d653de8aeba03', 'hex')) | ||
| test.expect(415, 'unsupported content encoding "br"', done) | ||
| }) | ||
|
|
||
| it('should be case-insensitive', function (done) { | ||
| var test = request(this.server).post('/') | ||
| test.set('Content-Encoding', 'GZIP') | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -12,6 +12,10 @@ var describeAsyncHooks = typeof asyncHooks.AsyncLocalStorage === 'function' | |
| ? describe | ||
| : describe.skip | ||
|
|
||
| var hasBrotliSupport = 'createBrotliDecompress' in require('zlib') | ||
| var brotlit = hasBrotliSupport ? it : it.skip | ||
| var nobrotlit = !hasBrotliSupport ? it : it.skip | ||
|
|
||
| describe('bodyParser.text()', function () { | ||
| before(function () { | ||
| this.server = createServer() | ||
|
|
@@ -525,6 +529,22 @@ describe('bodyParser.text()', function () { | |
| test.expect(200, '"name is Lun "', done) | ||
| }) | ||
|
|
||
| brotlit('should support brotli encoding', function (done) { | ||
| var test = request(this.server).post('/') | ||
| test.set('Content-Encoding', 'br') | ||
| test.set('Content-Type', 'text/plain') | ||
| test.write(Buffer.from('0b05806e616d6520697320e8aeba03', 'hex')) | ||
| test.expect(200, '"name is Lun "', done) | ||
| }) | ||
|
|
||
| nobrotlit('should throw 415 if there\'s no brotli support', function (done) { | ||
| var test = request(this.server).post('/') | ||
| test.set('Content-Encoding', 'br') | ||
| test.set('Content-Type', 'text/plain') | ||
| test.write(Buffer.from('0b05806e616d6520697320e8aeba03', 'hex')) | ||
| test.expect(415, 'unsupported content encoding "br"', done) | ||
| }) | ||
|
|
||
| it('should be case-insensitive', function (done) { | ||
| var test = request(this.server).post('/') | ||
| test.set('Content-Encoding', 'GZIP') | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -12,6 +12,10 @@ var describeAsyncHooks = typeof asyncHooks.AsyncLocalStorage === 'function' | |
| ? describe | ||
| : describe.skip | ||
|
|
||
| var hasBrotliSupport = 'createBrotliDecompress' in require('zlib') | ||
| var brotlit = hasBrotliSupport ? it : it.skip | ||
| var nobrotlit = !hasBrotliSupport ? it : it.skip | ||
|
|
||
| describe('bodyParser.urlencoded()', function () { | ||
| before(function () { | ||
| this.server = createServer() | ||
|
|
@@ -831,6 +835,22 @@ describe('bodyParser.urlencoded()', function () { | |
| test.expect(200, '{"name":"Lun "}', done) | ||
| }) | ||
|
|
||
| brotlit('should support brotli encoding', function (done) { | ||
| var test = request(this.server).post('/') | ||
| test.set('Content-Encoding', 'br') | ||
| test.set('Content-Type', 'application/x-www-form-urlencoded') | ||
| test.write(Buffer.from('8b03806e616d653de8aeba03', 'hex')) | ||
| test.expect(200, '{"name":"Lun "}', done) | ||
| }) | ||
|
|
||
| nobrotlit('should throw 415 if there\'s no brotli support', function (done) { | ||
| var test = request(this.server).post('/') | ||
| test.set('Content-Encoding', 'br') | ||
| test.set('Content-Type', 'application/x-www-form-urlencoded') | ||
| test.write(Buffer.from('789ccb4bcc4db57db16e17001068042f', 'hex')) | ||
| test.expect(415, 'unsupported content encoding "br"', done) | ||
| }) | ||
|
|
||
| it('should be case-insensitive', function (done) { | ||
| var test = request(this.server).post('/') | ||
| test.set('Content-Encoding', 'GZIP') | ||
|
|
||
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.