Skip to content

Commit e8ceb56

Browse files
authored
Merge pull request #1499 from microsoft/fix/drop-zlib-compression
fix: remove zlib compression in node
2 parents 07bdc69 + 5127199 commit e8ceb56

File tree

2 files changed

+7
-55
lines changed

2 files changed

+7
-55
lines changed

packages/http/fetch/src/middlewares/compressionHandler.ts

+6-31
Original file line numberDiff line numberDiff line change
@@ -169,37 +169,12 @@ export class CompressionHandler implements Middleware {
169169
compressedBody: ArrayBuffer | Buffer;
170170
size: number;
171171
}> {
172-
if (!inNodeEnv()) {
173-
// in browser
174-
const compressionData = this.readBodyAsBytes(body);
175-
const compressedBody = await this.compressUsingCompressionStream(compressionData.stream);
176-
return {
177-
compressedBody: compressedBody.body,
178-
size: compressedBody.size,
179-
};
180-
} else {
181-
// In Node.js
182-
const compressedBody = await this.compressUsingZlib(body);
183-
return {
184-
compressedBody,
185-
size: compressedBody.length,
186-
};
187-
}
188-
}
189-
190-
private async compressUsingZlib(body: unknown): Promise<Buffer> {
191-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
192-
// @ts-ignore
193-
const zlib = await import("zlib");
194-
return await new Promise((resolve, reject) => {
195-
zlib.gzip(body as string | ArrayBuffer | NodeJS.ArrayBufferView, (err, compressed) => {
196-
if (err) {
197-
reject(err);
198-
} else {
199-
resolve(compressed);
200-
}
201-
});
202-
});
172+
const compressionData = this.readBodyAsBytes(body);
173+
const compressedBody = await this.compressUsingCompressionStream(compressionData.stream);
174+
return {
175+
compressedBody: compressedBody.body,
176+
size: compressedBody.size,
177+
};
203178
}
204179

205180
private async compressUsingCompressionStream(uint8ArrayStream: ReadableStream<Uint8Array>): Promise<{ body: ArrayBuffer; size: number }> {

packages/http/fetch/test/common/middleware/compressionHandler.ts

+1-24
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { CompressionHandler } from "../../../src/middlewares/compressionHandler"
55
const defaultOptions = new CompressionHandlerOptions();
66

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

109
describe("CompressionHandler", () => {
1110
let compressionHandler: CompressionHandler;
@@ -63,7 +62,7 @@ describe("CompressionHandler", () => {
6362

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

@@ -103,28 +102,6 @@ describe("CompressionHandler", () => {
103102
});
104103
});
105104

106-
// helper function to decompress ArrayBuffer using zlib
107-
async function decompressUsingZlib(arrayBuffer: ArrayBuffer): Promise<string> {
108-
return new Promise(async (resolve, reject) => {
109-
// @ts-ignore
110-
const zlib = await import("zlib");
111-
// Convert the ArrayBuffer to a Node.js Buffer
112-
const buffer = Buffer.from(arrayBuffer);
113-
console.log(buffer);
114-
115-
// Decompress the buffer
116-
zlib.gunzip(buffer, (err, decompressedBuffer) => {
117-
if (err) {
118-
console.error(err);
119-
return reject(err);
120-
}
121-
// Convert the decompressed Buffer to a string and resolve the promise
122-
console.log("decompressed " + decompressedBuffer.toString());
123-
resolve(decompressedBuffer.toString("utf-8"));
124-
});
125-
});
126-
}
127-
128105
// helper function to convert ArrayBuffer to string using DecompressionStream
129106
async function decompressUsingDecompressionStream(compressedArrayBuffer: ArrayBuffer): Promise<string> {
130107
const decompressionStream = new DecompressionStream("gzip");

0 commit comments

Comments
 (0)