Skip to content

Commit

Permalink
Merge pull request #1499 from microsoft/fix/drop-zlib-compression
Browse files Browse the repository at this point in the history
fix: remove zlib compression in node
  • Loading branch information
baywet authored Nov 22, 2024
2 parents 07bdc69 + 5127199 commit e8ceb56
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 55 deletions.
37 changes: 6 additions & 31 deletions packages/http/fetch/src/middlewares/compressionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,37 +169,12 @@ export class CompressionHandler implements Middleware {
compressedBody: ArrayBuffer | Buffer;
size: number;
}> {
if (!inNodeEnv()) {
// in browser
const compressionData = this.readBodyAsBytes(body);
const compressedBody = await this.compressUsingCompressionStream(compressionData.stream);
return {
compressedBody: compressedBody.body,
size: compressedBody.size,
};
} else {
// In Node.js
const compressedBody = await this.compressUsingZlib(body);
return {
compressedBody,
size: compressedBody.length,
};
}
}

private async compressUsingZlib(body: unknown): Promise<Buffer> {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const zlib = await import("zlib");
return await new Promise((resolve, reject) => {
zlib.gzip(body as string | ArrayBuffer | NodeJS.ArrayBufferView, (err, compressed) => {
if (err) {
reject(err);
} else {
resolve(compressed);
}
});
});
const compressionData = this.readBodyAsBytes(body);
const compressedBody = await this.compressUsingCompressionStream(compressionData.stream);
return {
compressedBody: compressedBody.body,
size: compressedBody.size,
};
}

private async compressUsingCompressionStream(uint8ArrayStream: ReadableStream<Uint8Array>): Promise<{ body: ArrayBuffer; size: number }> {
Expand Down
25 changes: 1 addition & 24 deletions packages/http/fetch/test/common/middleware/compressionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { CompressionHandler } from "../../../src/middlewares/compressionHandler"
const defaultOptions = new CompressionHandlerOptions();

import { assert, describe, it, expect, beforeEach, vi } from "vitest";
import { inNodeEnv } from "@microsoft/kiota-abstractions";

describe("CompressionHandler", () => {
let compressionHandler: CompressionHandler;
Expand Down Expand Up @@ -63,7 +62,7 @@ describe("CompressionHandler", () => {

expect((requestInit.headers as Record<string, string>)["Content-Encoding"]).toBe("gzip");
const compressedBody = requestInit.body as unknown as ArrayBuffer;
const decompressedBody = inNodeEnv() ? await decompressUsingZlib(compressedBody) : await decompressUsingDecompressionStream(compressedBody);
const decompressedBody = await decompressUsingDecompressionStream(compressedBody);
expect(decompressedBody).toBe(requestBody);
});

Expand Down Expand Up @@ -103,28 +102,6 @@ describe("CompressionHandler", () => {
});
});

// helper function to decompress ArrayBuffer using zlib
async function decompressUsingZlib(arrayBuffer: ArrayBuffer): Promise<string> {
return new Promise(async (resolve, reject) => {
// @ts-ignore
const zlib = await import("zlib");
// Convert the ArrayBuffer to a Node.js Buffer
const buffer = Buffer.from(arrayBuffer);
console.log(buffer);

// Decompress the buffer
zlib.gunzip(buffer, (err, decompressedBuffer) => {
if (err) {
console.error(err);
return reject(err);
}
// Convert the decompressed Buffer to a string and resolve the promise
console.log("decompressed " + decompressedBuffer.toString());
resolve(decompressedBuffer.toString("utf-8"));
});
});
}

// helper function to convert ArrayBuffer to string using DecompressionStream
async function decompressUsingDecompressionStream(compressedArrayBuffer: ArrayBuffer): Promise<string> {
const decompressionStream = new DecompressionStream("gzip");
Expand Down

0 comments on commit e8ceb56

Please sign in to comment.