-
-
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.
feat: use compression streams to decompress responses
- Loading branch information
1 parent
cdfdc95
commit d4d4939
Showing
3 changed files
with
142 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
function pipeline(streams: Array<TransformStream>): TransformStream { | ||
if (streams.length === 0) { | ||
throw new Error('At least one stream must be provided') | ||
} | ||
|
||
let composedStream = streams[0] | ||
|
||
for (let i = 1; i < streams.length; i++) { | ||
const currentStream = streams[i] | ||
|
||
composedStream = new TransformStream({ | ||
async start(controller) { | ||
const reader = streams[i - 1].readable.getReader() | ||
const writer = currentStream.writable.getWriter() | ||
|
||
while (true) { | ||
const { value, done } = await reader.read() | ||
if (done) { | ||
break | ||
} | ||
await writer.write(value) | ||
} | ||
|
||
await writer.close() | ||
controller.terminate() | ||
}, | ||
transform(chunk, controller) { | ||
controller.enqueue(chunk) | ||
}, | ||
}) | ||
} | ||
|
||
return composedStream | ||
} | ||
|
||
function createDecompressionStream( | ||
contentEncoding: string | ||
): TransformStream | null { | ||
if (contentEncoding === '') { | ||
return null | ||
} | ||
|
||
const codings = contentEncoding | ||
.toLowerCase() | ||
.split(',') | ||
.map((coding) => coding.trim()) | ||
|
||
if (codings.length === 0) { | ||
return null | ||
} | ||
|
||
const transformers: Array<TransformStream> = [] | ||
|
||
for (let i = codings.length - 1; i >= 0; --i) { | ||
const coding = codings[i] | ||
|
||
if (coding === 'gzip' || coding === 'x-gzip') { | ||
transformers.push(new DecompressionStream('gzip')) | ||
} else if (coding === 'deflate') { | ||
transformers.push(new DecompressionStream('deflate')) | ||
} else if (coding === 'br') { | ||
/** | ||
* @todo Support Brotli decompression. | ||
* It's not a part of the web Compression Streams API. | ||
*/ | ||
} else { | ||
transformers.length = 0 | ||
} | ||
} | ||
|
||
return pipeline(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