-
-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(fetch): support
Content-Encoding
response header (#604)
Co-authored-by: Artem Zakharchenko <[email protected]>
- Loading branch information
1 parent
6198df0
commit b8f8d51
Showing
12 changed files
with
579 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -200,4 +200,4 @@ | |
"path": "./node_modules/cz-conventional-changelog" | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export class BrotliDecompressionStream extends TransformStream { | ||
constructor() { | ||
console.warn( | ||
'[Interceptors]: Brotli decompression of response streams is not supported in the browser' | ||
) | ||
|
||
super({ | ||
transform(chunk, controller) { | ||
// Keep the stream as passthrough, it does nothing. | ||
controller.enqueue(chunk) | ||
}, | ||
}) | ||
} | ||
} |
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,31 @@ | ||
import zlib from 'node:zlib' | ||
|
||
export class BrotliDecompressionStream extends TransformStream { | ||
constructor() { | ||
const decompress = zlib.createBrotliDecompress({ | ||
flush: zlib.constants.BROTLI_OPERATION_FLUSH, | ||
finishFlush: zlib.constants.BROTLI_OPERATION_FLUSH, | ||
}) | ||
|
||
super({ | ||
async transform(chunk, controller) { | ||
const buffer = Buffer.from(chunk) | ||
|
||
const decompressed = await new Promise<Buffer>((resolve, reject) => { | ||
decompress.write(buffer, (error) => { | ||
if (error) reject(error) | ||
}) | ||
|
||
decompress.flush() | ||
decompress.once('data', (data) => resolve(data)) | ||
decompress.once('error', (error) => reject(error)) | ||
decompress.once('end', () => controller.terminate()) | ||
}).catch((error) => { | ||
controller.error(error) | ||
}) | ||
|
||
controller.enqueue(decompressed) | ||
}, | ||
}) | ||
} | ||
} |
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,85 @@ | ||
// Import from an internal alias that resolves to different modules | ||
// depending on the environment. This way, we can keep the fetch interceptor | ||
// intact while using different strategies for Brotli decompression. | ||
import { BrotliDecompressionStream } from 'internal:brotli-decompress' | ||
|
||
class PipelineStream extends TransformStream { | ||
constructor( | ||
transformStreams: Array<TransformStream>, | ||
...strategies: Array<QueuingStrategy> | ||
) { | ||
super({}, ...strategies) | ||
|
||
const readable = [super.readable as any, ...transformStreams].reduce( | ||
(readable, transform) => readable.pipeThrough(transform) | ||
) | ||
|
||
Object.defineProperty(this, 'readable', { | ||
get() { | ||
return readable | ||
}, | ||
}) | ||
} | ||
} | ||
|
||
export function parseContentEncoding(contentEncoding: string): Array<string> { | ||
return contentEncoding | ||
.toLowerCase() | ||
.split(',') | ||
.map((coding) => coding.trim()) | ||
} | ||
|
||
function createDecompressionStream( | ||
contentEncoding: string | ||
): TransformStream | null { | ||
if (contentEncoding === '') { | ||
return null | ||
} | ||
|
||
const codings = parseContentEncoding(contentEncoding) | ||
|
||
if (codings.length === 0) { | ||
return null | ||
} | ||
|
||
const transformers = codings.reduceRight<Array<TransformStream>>( | ||
(transformers, coding) => { | ||
if (coding === 'gzip' || coding === 'x-gzip') { | ||
return transformers.concat(new DecompressionStream('gzip')) | ||
} else if (coding === 'deflate') { | ||
return transformers.concat(new DecompressionStream('deflate')) | ||
} else if (coding === 'br') { | ||
return transformers.concat(new BrotliDecompressionStream()) | ||
} else { | ||
transformers.length = 0 | ||
} | ||
|
||
return transformers | ||
}, | ||
[] | ||
) | ||
|
||
return new PipelineStream(transformers) | ||
} | ||
|
||
export function decompressResponse( | ||
response: Response | ||
): ReadableStream<any> | null { | ||
if (response.body === null) { | ||
return null | ||
} | ||
|
||
const decompressionStream = createDecompressionStream( | ||
response.headers.get('content-encoding') || '' | ||
) | ||
|
||
if (!decompressionStream) { | ||
return null | ||
} | ||
|
||
// Use `pipeTo` and return the decompression stream's readable | ||
// instead of `pipeThrough` because that will lock the original | ||
// response stream, making it unusable as the input to Response. | ||
response.body.pipeTo(decompressionStream.writable) | ||
return decompressionStream.readable | ||
} |
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
Oops, something went wrong.