-
-
Notifications
You must be signed in to change notification settings - Fork 674
Decompression Interceptor #4317
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
Changes from 20 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
9c4b391
undici vs fetch
FelixVaughan d9a9921
snackin
FelixVaughan cbcbe2a
nothing serious
FelixVaughan 58553bc
todo: tests
FelixVaughan fa633ec
testing
FelixVaughan f0e3f6a
resolve merge conflict
FelixVaughan c640901
cleanup
FelixVaughan f0266eb
Update lib/interceptor/decompress.js
FelixVaughan 572eb75
resolve conflict
FelixVaughan 1accabb
documentation
FelixVaughan d5bf430
formatting
FelixVaughan 74cc73f
pr suggestions
FelixVaughan ae7847c
pr suggestions as well as zstd support
FelixVaughan 8294096
update documatation
FelixVaughan d229312
personal pedancy
FelixVaughan af7c01b
pr suggestions
FelixVaughan 88c6170
tidied up decompression chaining logic
FelixVaughan 13783dc
wip
FelixVaughan 8d003fb
pr suggestions and tests
FelixVaughan fa8db99
skip createZstdCompress tests when unavailable (pre v22)
FelixVaughan 8ca18ec
tidy up
FelixVaughan 08d34d1
conditional usage of createZstdDecompress
FelixVaughan a411ae1
added some comments (mostly to re-trigger CI/CD)
FelixVaughan 083073c
Added tests with fetch()
FelixVaughan 70a291d
refactored createDecompressionChain to use a basic for loop
FelixVaughan dddd2a0
Merge branch 'main' of github.com:nodejs/undici into decomp-interceptor
FelixVaughan 9d5255a
apply changes
Uzlopak 87fb219
improved jsdocs, removed non-critical handler return statements
FelixVaughan 2bc4323
experimental flag in dispatcher.md for decompress
FelixVaughan 00d869e
plain object use
FelixVaughan c07802d
readble event
FelixVaughan 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,188 @@ | ||
| 'use strict' | ||
|
|
||
| const { createInflate, createGunzip, createBrotliDecompress, createZstdDecompress } = require('node:zlib') | ||
| const { pipeline } = require('node:stream') | ||
| const DecoratorHandler = require('../handler/decorator-handler') | ||
|
|
||
| class DecompressHandler extends DecoratorHandler { | ||
metcoder95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #decompressors = [] | ||
| #pipelineStream = null | ||
| #skipStatusCodes | ||
| #skipErrorResponses | ||
|
|
||
| constructor (handler, { skipStatusCodes = [204, 304], skipErrorResponses = true } = {}) { | ||
| super(handler) | ||
| this.#skipStatusCodes = skipStatusCodes | ||
| this.#skipErrorResponses = skipErrorResponses | ||
| } | ||
|
|
||
| #shouldSkipDecompression (contentEncoding, statusCode) { | ||
| if (!contentEncoding || statusCode < 200) return true | ||
| if (this.#skipStatusCodes.includes(statusCode)) return true | ||
| if (this.#skipErrorResponses && statusCode >= 400) return true | ||
| return false | ||
| } | ||
|
|
||
| #createDecompressor (encoding) { | ||
| const supportedEncodings = { | ||
| gzip: createGunzip, | ||
| 'x-gzip': createGunzip, | ||
| br: createBrotliDecompress, | ||
| zstd: createZstdDecompress, | ||
Uzlopak marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| deflate: createInflate, | ||
| compress: createInflate, | ||
| 'x-compress': createInflate | ||
| } | ||
|
|
||
| const createDecompressor = supportedEncodings[encoding.toLowerCase()] | ||
| return createDecompressor ? createDecompressor() : null | ||
| } | ||
|
|
||
| #createDecompressionChain (encodings) { | ||
| const encodingList = encodings.split(',').map(e => e.trim()).filter(Boolean) | ||
|
|
||
| const decompressors = encodingList | ||
| .reverse() | ||
| .map(encoding => this.#createDecompressor(encoding)) | ||
|
|
||
| return decompressors.some(d => !d) ? null : decompressors | ||
Uzlopak marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| onResponseStart (controller, statusCode, headers, statusMessage) { | ||
| const contentEncoding = headers['content-encoding'] | ||
|
|
||
| if (this.#shouldSkipDecompression(contentEncoding, statusCode)) { | ||
| return super.onResponseStart(controller, statusCode, headers, statusMessage) | ||
| } | ||
|
|
||
| const decompressors = this.#createDecompressionChain(contentEncoding) | ||
|
|
||
| if (!decompressors) { | ||
| this.#decompressors = [] | ||
| return super.onResponseStart(controller, statusCode, headers, statusMessage) | ||
| } | ||
|
|
||
| this.#decompressors = decompressors | ||
|
|
||
| const { 'content-encoding': _, 'content-length': __, ...newHeaders } = headers | ||
|
|
||
| const superPause = controller.pause.bind(controller) | ||
| const superResume = controller.resume.bind(controller) | ||
|
|
||
| controller.pause = () => { | ||
| const result = superPause() | ||
| if (this.#decompressors.length > 0) { | ||
| const head = this.#decompressors[0] | ||
| if (!head.readableEnded && !head.destroyed) { | ||
| head.pause() | ||
| } | ||
| if (this.#pipelineStream && !this.#pipelineStream.destroyed) { | ||
| this.#pipelineStream.pause() | ||
| } | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| controller.resume = () => { | ||
| const result = superResume() | ||
| if (this.#decompressors.length > 0) { | ||
| const head = this.#decompressors[0] | ||
| if (!head.readableEnded && !head.destroyed) { | ||
| head.resume() | ||
| } | ||
| if (this.#pipelineStream && !this.#pipelineStream.destroyed) { | ||
| this.#pipelineStream.resume() | ||
| } | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| if (this.#decompressors.length === 1) { | ||
| const decompressor = this.#decompressors[0] | ||
| decompressor.on('data', (chunk) => { | ||
| const result = super.onResponseData(controller, chunk) | ||
| if (result === false) { | ||
| decompressor.pause() | ||
| } | ||
| }) | ||
|
|
||
| decompressor.on('drain', () => { | ||
| controller.resume() | ||
| }) | ||
|
|
||
| decompressor.on('error', (error) => { | ||
| super.onResponseError(controller, error) | ||
| }) | ||
|
|
||
| decompressor.on('finish', () => { | ||
| super.onResponseEnd(controller, {}) | ||
| }) | ||
| } else { | ||
| const lastDecompressor = this.#decompressors[this.#decompressors.length - 1] | ||
metcoder95 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| lastDecompressor.on('data', (chunk) => { | ||
| const result = super.onResponseData(controller, chunk) | ||
| if (result === false) { | ||
| lastDecompressor.pause() | ||
| } | ||
| }) | ||
|
|
||
| lastDecompressor.on('drain', () => { | ||
| controller.resume() | ||
| }) | ||
|
|
||
| this.#pipelineStream = pipeline(this.#decompressors, (err) => { | ||
| if (err) { | ||
| super.onResponseError(controller, err) | ||
| return | ||
| } | ||
| super.onResponseEnd(controller, {}) | ||
| }) | ||
| } | ||
|
|
||
| return super.onResponseStart(controller, statusCode, newHeaders, statusMessage) | ||
| } | ||
|
|
||
| onResponseData (controller, chunk) { | ||
| if (this.#decompressors.length > 0) { | ||
| const writeResult = this.#decompressors[0].write(chunk) | ||
| if (writeResult === false) { | ||
| controller.pause() | ||
| } | ||
| return true | ||
| } | ||
| return super.onResponseData(controller, chunk) | ||
| } | ||
|
|
||
| onResponseEnd (controller, trailers) { | ||
| if (this.#decompressors.length > 0) { | ||
| this.#decompressors[0].end() | ||
| this.#decompressors = [] | ||
| this.#pipelineStream = null | ||
| return | ||
| } | ||
| return super.onResponseEnd(controller, trailers) | ||
| } | ||
|
|
||
| onResponseError (controller, err) { | ||
| if (this.#decompressors.length > 0) { | ||
| this.#decompressors.forEach(d => { | ||
| d.destroy(err) | ||
| }) | ||
| this.#decompressors = [] | ||
| this.#pipelineStream = null | ||
| } | ||
| return super.onResponseError(controller, err) | ||
| } | ||
| } | ||
|
|
||
| function createDecompressInterceptor (options = {}) { | ||
| return (dispatch) => { | ||
| return (opts, handler) => { | ||
| const decompressHandler = new DecompressHandler(handler, options) | ||
| return dispatch(opts, decompressHandler) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| module.exports = createDecompressInterceptor | ||
Oops, something went wrong.
Oops, something went wrong.
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.