Skip to content

Commit

Permalink
Added logic to decompress request body if it has gzip encoding header (
Browse files Browse the repository at this point in the history
  • Loading branch information
ygrik authored Sep 25, 2023
1 parent d86cb77 commit 68e1254
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -538,9 +538,18 @@ export class OperationConsole {
: consoleOperation.name;

saveAs(blob, fileName);
}
else {
const responseBody = response.body.toString();
} else {
let responseBody: string;
if(this.useCorsProxy()) {
const contentEncodingHeader = response.headers.find(x => x.name === KnownHttpHeaders.ContentEncoding.toLowerCase());
if (contentEncodingHeader?.value === "gzip") {
responseBody = await this.decompressBody(response.body);
} else {
responseBody = response.body.toString();
}
} else {
responseBody = response.body.toString();
}

if (responseContentType && Utils.isJsonContentType(responseContentType)) {
this.responseBody(Utils.formatJson(responseBody));
Expand Down Expand Up @@ -572,6 +581,31 @@ export class OperationConsole {

private ws: WebsocketClient;

private async decompressBody(body: Buffer): Promise<string> {
const ds = new DecompressionStream("gzip");
const dsWriter = ds.writable.getWriter();
dsWriter.write(body);
dsWriter.close();
const output: Uint8Array[] = [];
const reader = ds.readable.getReader();
let totalSize = 0;

while (true) {
const { value, done } = await reader.read();
if (done) break;
output.push(value);
totalSize += value.byteLength;
}
const concatenated = new Uint8Array(totalSize);
let offset = 0;
for (const array of output) {
concatenated.set(array, offset);
offset += array.byteLength;
}
const decoder = new TextDecoder("utf-8");
return decoder.decode(concatenated);
}

public async wsConnect(): Promise<void> {
const operation = this.consoleOperation();
const templateParameters = operation.templateParameters();
Expand Down
1 change: 1 addition & 0 deletions src/models/knownHttpHeaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export enum KnownHttpHeaders {
Authorization = "Authorization",
CacheControl = "Cache-Control",
ContentType = "Content-Type",
ContentEncoding = "Content-Encoding",
OcpApimSubscriptionKey = "Ocp-Apim-Subscription-Key",
OcpApimTrace = "Ocp-Apim-Trace",
OcpApimTraceLocation = "Ocp-Apim-Trace-Location",
Expand Down
4 changes: 3 additions & 1 deletion src/modules.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ declare module "*.txt" {
declare module "*.raw" {
const content: string;
export default content;
}
}

declare let DecompressionStream: any;

0 comments on commit 68e1254

Please sign in to comment.