From 8ed62c219dcc586f32494669910cd62f87f5cd47 Mon Sep 17 00:00:00 2001 From: rkodev <43806892+rkodev@users.noreply.github.com> Date: Tue, 14 Jan 2025 16:16:29 +0300 Subject: [PATCH] feat: disable default compression --- packages/http/fetch/src/browser/index.ts | 2 ++ packages/http/fetch/src/httpClient.ts | 4 +++ packages/http/fetch/src/index.ts | 2 ++ .../middlewares/browser/middlewareFactory.ts | 13 +++++++- .../src/middlewares/middlewareFactory.ts | 13 +++++++- .../options/chaosHandlerOptions.ts | 2 +- .../http/fetch/test/browser/httpClient.ts | 33 +++++++++++++------ .../fetch/test/browser/kiotaClientFactory.ts | 8 ++--- .../fetch/test/browser/middlewareFactory.ts | 14 ++++++-- .../http/fetch/test/node/MiddlewareFactory.ts | 15 +++++++-- packages/http/fetch/test/node/httpClient.ts | 33 +++++++++++++------ .../fetch/test/node/kiotaClientFactory.ts | 8 ++--- 12 files changed, 110 insertions(+), 37 deletions(-) diff --git a/packages/http/fetch/src/browser/index.ts b/packages/http/fetch/src/browser/index.ts index 64d89bb3c..a248f712f 100644 --- a/packages/http/fetch/src/browser/index.ts +++ b/packages/http/fetch/src/browser/index.ts @@ -13,6 +13,7 @@ export * from "../httpClient"; export * from "../middlewares/middleware"; export * from "../middlewares/chaosHandler"; export * from "../middlewares/customFetchHandler"; +export * from "../middlewares/compressionHandler"; export * from "../middlewares/headersInspectionHandler"; export * from "../middlewares/parametersNameDecodingHandler"; export * from "../middlewares/redirectHandler"; @@ -21,6 +22,7 @@ export * from "../middlewares/userAgentHandler"; export * from "../middlewares/urlReplaceHandler"; export * from "../middlewares/options/chaosHandlerOptions"; export * from "../middlewares/options/chaosStrategy"; +export * from "../middlewares/options/compressionHandlerOptions"; export * from "../middlewares/options/headersInspectionOptions"; export * from "../middlewares/options/parametersNameDecodingOptions"; export * from "../middlewares/options/redirectHandlerOptions"; diff --git a/packages/http/fetch/src/httpClient.ts b/packages/http/fetch/src/httpClient.ts index 1ae0fcfe1..461fd90f9 100644 --- a/packages/http/fetch/src/httpClient.ts +++ b/packages/http/fetch/src/httpClient.ts @@ -31,6 +31,10 @@ export class HttpClient { // eslint-disable-next-line @typescript-eslint/no-unsafe-argument middlewares.push(new CustomFetchHandler(customFetch as any)); } + // eslint-disable-next-line no-console + console.debug("Registered middlewares: " + middlewares.map((m) => m.constructor.name).join(", ")); + // eslint-disable-next-line no-console + console.debug("Hint: To improve performance, use MiddlewareFactory.getPerformanceMiddlewares(customFetch) instead of MiddlewareFactory.getDefaultMiddlewares(customFetch)"); // Set the middleware chain this.setMiddleware(...middlewares); diff --git a/packages/http/fetch/src/index.ts b/packages/http/fetch/src/index.ts index 4c882cbfa..3f711ec23 100644 --- a/packages/http/fetch/src/index.ts +++ b/packages/http/fetch/src/index.ts @@ -13,6 +13,7 @@ export * from "./httpClient"; export * from "./middlewares/middleware"; export * from "./middlewares/chaosHandler"; export * from "./middlewares/customFetchHandler"; +export * from "./middlewares/compressionHandler"; export * from "./middlewares/headersInspectionHandler"; export * from "./middlewares/parametersNameDecodingHandler"; export * from "./middlewares/redirectHandler"; @@ -21,6 +22,7 @@ export * from "./middlewares/userAgentHandler"; export * from "./middlewares/urlReplaceHandler"; export * from "./middlewares/options/chaosHandlerOptions"; export * from "./middlewares/options/chaosStrategy"; +export * from "./middlewares/options/compressionHandlerOptions"; export * from "./middlewares/options/headersInspectionOptions"; export * from "./middlewares/options/parametersNameDecodingOptions"; export * from "./middlewares/options/redirectHandlerOptions"; diff --git a/packages/http/fetch/src/middlewares/browser/middlewareFactory.ts b/packages/http/fetch/src/middlewares/browser/middlewareFactory.ts index 89ac132a2..229f8d9fb 100644 --- a/packages/http/fetch/src/middlewares/browser/middlewareFactory.ts +++ b/packages/http/fetch/src/middlewares/browser/middlewareFactory.ts @@ -27,6 +27,17 @@ export class MiddlewareFactory { // eslint-disable-next-line @typescript-eslint/no-unsafe-return public static getDefaultMiddlewares(customFetch: (request: string, init: RequestInit) => Promise = (...args) => fetch(...args) as any): Middleware[] { // Browsers handles redirection automatically and do not require the redirectionHandler - return [new RetryHandler(), new ParametersNameDecodingHandler(), new UserAgentHandler(), new CompressionHandler(), new HeadersInspectionHandler(), new UrlReplaceHandler(), new CustomFetchHandler(customFetch)]; + return [new RetryHandler(), new ParametersNameDecodingHandler(), new UserAgentHandler(), new HeadersInspectionHandler(), new UrlReplaceHandler(), new CustomFetchHandler(customFetch)]; + } + /** + * @param customFetch - The custom fetch implementation + * Returns the default middleware chain + performance middleware + * @returns an array of the middleware handlers of the default + performance middleware chain + */ + // eslint-disable-next-line @typescript-eslint/no-unsafe-return + public static getPerformanceMiddlewares(customFetch: (request: string, init: RequestInit) => Promise = (...args) => fetch(...args) as any): Middleware[] { + const middlewares = MiddlewareFactory.getDefaultMiddlewares(customFetch); + middlewares.splice(middlewares.length - 3, 0, new CompressionHandler()); // insert CompressionHandler before HeadersInspectionHandler + return middlewares; } } diff --git a/packages/http/fetch/src/middlewares/middlewareFactory.ts b/packages/http/fetch/src/middlewares/middlewareFactory.ts index a922918b4..aed05b820 100644 --- a/packages/http/fetch/src/middlewares/middlewareFactory.ts +++ b/packages/http/fetch/src/middlewares/middlewareFactory.ts @@ -27,6 +27,17 @@ export class MiddlewareFactory { */ // eslint-disable-next-line @typescript-eslint/no-unsafe-return public static getDefaultMiddlewares(customFetch: (request: string, init: RequestInit) => Promise = (...args) => fetch(...args) as any): Middleware[] { - return [new RetryHandler(), new RedirectHandler(), new ParametersNameDecodingHandler(), new UserAgentHandler(), new CompressionHandler(), new HeadersInspectionHandler(), new UrlReplaceHandler(), new CustomFetchHandler(customFetch)]; + return [new RetryHandler(), new RedirectHandler(), new ParametersNameDecodingHandler(), new UserAgentHandler(), new HeadersInspectionHandler(), new UrlReplaceHandler(), new CustomFetchHandler(customFetch)]; + } + /** + * @param customFetch - The custom fetch implementation + * Returns the default middleware chain + performance middleware + * @returns an array of the middleware handlers of the default + performance middleware chain + */ + // eslint-disable-next-line @typescript-eslint/no-unsafe-return + public static getPerformanceMiddlewares(customFetch: (request: string, init: RequestInit) => Promise = (...args) => fetch(...args) as any): Middleware[] { + const middlewares = MiddlewareFactory.getDefaultMiddlewares(customFetch); + middlewares.splice(middlewares.length - 3, 0, new CompressionHandler()); // insert CompressionHandler before HeadersInspectionHandler + return middlewares; } } diff --git a/packages/http/fetch/src/middlewares/options/chaosHandlerOptions.ts b/packages/http/fetch/src/middlewares/options/chaosHandlerOptions.ts index e797b1f93..870ea0950 100644 --- a/packages/http/fetch/src/middlewares/options/chaosHandlerOptions.ts +++ b/packages/http/fetch/src/middlewares/options/chaosHandlerOptions.ts @@ -19,7 +19,7 @@ export const ChaosHandlerOptionsKey = "ChaosHandlerOptionsKey"; */ export interface ChaosHandlerOptions { /** - * Speficies the base url path for the destination server, used when relative paths are preffered to strip out paths + * Specifies the base url path for the destination server, used when relative paths are preferred to strip out paths */ baseUrl?: string; /** diff --git a/packages/http/fetch/test/browser/httpClient.ts b/packages/http/fetch/test/browser/httpClient.ts index 139d2243a..4daecbbde 100644 --- a/packages/http/fetch/test/browser/httpClient.ts +++ b/packages/http/fetch/test/browser/httpClient.ts @@ -7,9 +7,8 @@ import { assert, describe, it } from "vitest"; -import { CustomFetchHandler, HeadersInspectionHandler, HttpClient, Middleware, ParametersNameDecodingHandler, RedirectHandler, RetryHandler, UrlReplaceHandler, UserAgentHandler } from "../../src"; +import { CustomFetchHandler, HeadersInspectionHandler, HttpClient, MiddlewareFactory, ParametersNameDecodingHandler, RedirectHandler, RetryHandler, UrlReplaceHandler, UserAgentHandler, CompressionHandler } from "../../src"; import { DummyFetchHandler } from "../common/middleware/dummyFetchHandler"; -import { CompressionHandler } from "../../src/middlewares/compressionHandler"; describe("browser - HTTPClient.ts", () => { describe("constructor", () => { @@ -40,10 +39,9 @@ describe("browser - HTTPClient.ts", () => { assert.isTrue(next instanceof RedirectHandler); assert.isTrue(next?.next instanceof ParametersNameDecodingHandler); assert.isTrue(next?.next?.next instanceof UserAgentHandler); - assert.isTrue(next?.next?.next?.next instanceof CompressionHandler); - assert.isTrue(next?.next?.next?.next?.next instanceof HeadersInspectionHandler); - assert.isTrue(next?.next?.next?.next?.next?.next instanceof UrlReplaceHandler); - assert.isTrue(next?.next?.next?.next?.next?.next?.next instanceof CustomFetchHandler); + assert.isTrue(next?.next?.next?.next instanceof HeadersInspectionHandler); + assert.isTrue(next?.next?.next?.next?.next instanceof UrlReplaceHandler); + assert.isTrue(next?.next?.next?.next?.next?.next instanceof CustomFetchHandler); }); it("Should set default middleware array with customFetchHandler if middleware parameter is undefined && customFetch is defined", () => { @@ -57,10 +55,9 @@ describe("browser - HTTPClient.ts", () => { assert.isTrue(next instanceof RedirectHandler); assert.isTrue(next?.next instanceof ParametersNameDecodingHandler); assert.isTrue(next?.next?.next instanceof UserAgentHandler); - assert.isTrue(next?.next?.next?.next instanceof CompressionHandler); - assert.isTrue(next?.next?.next?.next?.next instanceof HeadersInspectionHandler); - assert.isTrue(next?.next?.next?.next?.next?.next instanceof UrlReplaceHandler); - assert.isTrue(next?.next?.next?.next?.next?.next?.next instanceof CustomFetchHandler); + assert.isTrue(next?.next?.next?.next instanceof HeadersInspectionHandler); + assert.isTrue(next?.next?.next?.next?.next instanceof UrlReplaceHandler); + assert.isTrue(next?.next?.next?.next?.next?.next instanceof CustomFetchHandler); }); it("Should set to default fetch handler middleware array if middleware parameter is null && customFetch is undefined", () => { @@ -78,5 +75,21 @@ describe("browser - HTTPClient.ts", () => { assert.equal(client["customFetch"], dummyCustomFetch); assert.isTrue(client["middleware"]?.next?.next?.next?.next?.next?.next?.next instanceof CustomFetchHandler); }); + + it("Should set performance middleware for the http client", () => { + const client = new HttpClient(null, ...MiddlewareFactory.getPerformanceMiddlewares()); + + assert.isNotNull(client["middleware"]); + const next = client["middleware"].next; + + assert.isTrue(client["middleware"] instanceof RetryHandler); + assert.isTrue(next instanceof RedirectHandler); + assert.isTrue(next?.next instanceof ParametersNameDecodingHandler); + assert.isTrue(next?.next?.next instanceof UserAgentHandler); + assert.isTrue(next?.next?.next?.next instanceof CompressionHandler); + assert.isTrue(next?.next?.next?.next?.next instanceof HeadersInspectionHandler); + assert.isTrue(next?.next?.next?.next?.next?.next instanceof UrlReplaceHandler); + assert.isTrue(next?.next?.next?.next?.next?.next?.next instanceof CustomFetchHandler); + }); }); }); diff --git a/packages/http/fetch/test/browser/kiotaClientFactory.ts b/packages/http/fetch/test/browser/kiotaClientFactory.ts index 74d0c0835..a9761b76d 100644 --- a/packages/http/fetch/test/browser/kiotaClientFactory.ts +++ b/packages/http/fetch/test/browser/kiotaClientFactory.ts @@ -8,7 +8,6 @@ import { assert, describe, it } from "vitest"; import { CustomFetchHandler, HeadersInspectionHandler, KiotaClientFactory, ParametersNameDecodingHandler, RedirectHandler, RetryHandler, UrlReplaceHandler, UserAgentHandler } from "../../src"; -import { CompressionHandler } from "../../src/middlewares/compressionHandler"; describe("browser - KiotaClientFactory", () => { it("Should return the http client", () => { @@ -20,9 +19,8 @@ describe("browser - KiotaClientFactory", () => { assert.isTrue(middleware?.next instanceof RedirectHandler); assert.isTrue(middleware?.next?.next instanceof ParametersNameDecodingHandler); assert.isTrue(middleware?.next?.next?.next instanceof UserAgentHandler); - assert.isTrue(middleware?.next?.next?.next?.next instanceof CompressionHandler); - assert.isTrue(middleware?.next?.next?.next?.next?.next instanceof HeadersInspectionHandler); - assert.isTrue(middleware?.next?.next?.next?.next?.next?.next instanceof UrlReplaceHandler); - assert.isTrue(middleware?.next?.next?.next?.next?.next?.next?.next instanceof CustomFetchHandler); + assert.isTrue(middleware?.next?.next?.next?.next instanceof HeadersInspectionHandler); + assert.isTrue(middleware?.next?.next?.next?.next?.next instanceof UrlReplaceHandler); + assert.isTrue(middleware?.next?.next?.next?.next?.next?.next instanceof CustomFetchHandler); }); }); diff --git a/packages/http/fetch/test/browser/middlewareFactory.ts b/packages/http/fetch/test/browser/middlewareFactory.ts index 9f8243c55..2db36557e 100644 --- a/packages/http/fetch/test/browser/middlewareFactory.ts +++ b/packages/http/fetch/test/browser/middlewareFactory.ts @@ -6,12 +6,22 @@ */ import { assert, describe, it } from "vitest"; -import { CustomFetchHandler, HeadersInspectionHandler, MiddlewareFactory, ParametersNameDecodingHandler, RedirectHandler, RetryHandler, UrlReplaceHandler, UserAgentHandler } from "../../src"; -import { CompressionHandler } from "../../src/middlewares/compressionHandler"; +import { CustomFetchHandler, HeadersInspectionHandler, MiddlewareFactory, ParametersNameDecodingHandler, RedirectHandler, RetryHandler, UrlReplaceHandler, UserAgentHandler, CompressionHandler } from "../../src"; describe("browser - MiddlewareFactory", () => { it("Should return the default pipeline", () => { const defaultMiddleWareArray = MiddlewareFactory.getDefaultMiddlewares(); + assert.equal(defaultMiddleWareArray.length, 7); + assert.isTrue(defaultMiddleWareArray[0] instanceof RetryHandler); + assert.isTrue(defaultMiddleWareArray[1] instanceof RedirectHandler); + assert.isTrue(defaultMiddleWareArray[2] instanceof ParametersNameDecodingHandler); + assert.isTrue(defaultMiddleWareArray[3] instanceof UserAgentHandler); + assert.isTrue(defaultMiddleWareArray[4] instanceof HeadersInspectionHandler); + assert.isTrue(defaultMiddleWareArray[5] instanceof UrlReplaceHandler); + assert.isTrue(defaultMiddleWareArray[6] instanceof CustomFetchHandler); + }); + it("Should return the performance pipeline", () => { + const defaultMiddleWareArray = MiddlewareFactory.getPerformanceMiddlewares(); assert.equal(defaultMiddleWareArray.length, 8); assert.isTrue(defaultMiddleWareArray[0] instanceof RetryHandler); assert.isTrue(defaultMiddleWareArray[1] instanceof RedirectHandler); diff --git a/packages/http/fetch/test/node/MiddlewareFactory.ts b/packages/http/fetch/test/node/MiddlewareFactory.ts index 1e19850a8..ac485741a 100644 --- a/packages/http/fetch/test/node/MiddlewareFactory.ts +++ b/packages/http/fetch/test/node/MiddlewareFactory.ts @@ -7,12 +7,23 @@ import { assert, describe, it } from "vitest"; -import { CustomFetchHandler, HeadersInspectionHandler, MiddlewareFactory, ParametersNameDecodingHandler, RedirectHandler, RetryHandler, UrlReplaceHandler, UserAgentHandler } from "../../src"; -import { CompressionHandler } from "../../src/middlewares/compressionHandler"; +import { CustomFetchHandler, HeadersInspectionHandler, MiddlewareFactory, ParametersNameDecodingHandler, RedirectHandler, RetryHandler, UrlReplaceHandler, UserAgentHandler, CompressionHandler } from "../../src"; describe("node - MiddlewareFactory", () => { it("Should return the default pipeline", () => { const defaultMiddleWareArray = MiddlewareFactory.getDefaultMiddlewares(); + assert.equal(defaultMiddleWareArray.length, 7); + + assert.isTrue(defaultMiddleWareArray[0] instanceof RetryHandler); + assert.isTrue(defaultMiddleWareArray[1] instanceof RedirectHandler); + assert.isTrue(defaultMiddleWareArray[2] instanceof ParametersNameDecodingHandler); + assert.isTrue(defaultMiddleWareArray[3] instanceof UserAgentHandler); + assert.isTrue(defaultMiddleWareArray[4] instanceof HeadersInspectionHandler); + assert.isTrue(defaultMiddleWareArray[5] instanceof UrlReplaceHandler); + assert.isTrue(defaultMiddleWareArray[6] instanceof CustomFetchHandler); + }); + it("Should return the performance pipeline", () => { + const defaultMiddleWareArray = MiddlewareFactory.getPerformanceMiddlewares(); assert.equal(defaultMiddleWareArray.length, 8); assert.isTrue(defaultMiddleWareArray[0] instanceof RetryHandler); diff --git a/packages/http/fetch/test/node/httpClient.ts b/packages/http/fetch/test/node/httpClient.ts index 4345d9b95..d873850d9 100644 --- a/packages/http/fetch/test/node/httpClient.ts +++ b/packages/http/fetch/test/node/httpClient.ts @@ -7,9 +7,8 @@ import { assert, describe, it } from "vitest"; -import { CustomFetchHandler, HeadersInspectionHandler, HttpClient, ParametersNameDecodingHandler, RedirectHandler, RetryHandler, UrlReplaceHandler, UserAgentHandler } from "../../src"; +import { CustomFetchHandler, HeadersInspectionHandler, HttpClient, MiddlewareFactory, ParametersNameDecodingHandler, RedirectHandler, RetryHandler, UrlReplaceHandler, UserAgentHandler, CompressionHandler } from "../../src"; import { DummyFetchHandler } from "../common/middleware/dummyFetchHandler"; -import { CompressionHandler } from "../../src/middlewares/compressionHandler"; describe("node - HTTPClient.ts", () => { describe("constructor", () => { @@ -40,10 +39,9 @@ describe("node - HTTPClient.ts", () => { assert.isTrue(next instanceof RedirectHandler); assert.isTrue(next?.next instanceof ParametersNameDecodingHandler); assert.isTrue(next?.next?.next instanceof UserAgentHandler); - assert.isTrue(next?.next?.next?.next instanceof CompressionHandler); - assert.isTrue(next?.next?.next?.next?.next instanceof HeadersInspectionHandler); - assert.isTrue(next?.next?.next?.next?.next?.next instanceof UrlReplaceHandler); - assert.isTrue(next?.next?.next?.next?.next?.next?.next instanceof CustomFetchHandler); + assert.isTrue(next?.next?.next?.next instanceof HeadersInspectionHandler); + assert.isTrue(next?.next?.next?.next?.next instanceof UrlReplaceHandler); + assert.isTrue(next?.next?.next?.next?.next?.next instanceof CustomFetchHandler); }); it("Should set default middleware array with customFetchHandler if middleware parameter is undefined && customFetch is defined", () => { @@ -58,10 +56,9 @@ describe("node - HTTPClient.ts", () => { assert.isTrue(next instanceof RedirectHandler); assert.isTrue(next?.next instanceof ParametersNameDecodingHandler); assert.isTrue(next?.next?.next instanceof UserAgentHandler); - assert.isTrue(next?.next?.next?.next instanceof CompressionHandler); - assert.isTrue(next?.next?.next?.next?.next instanceof HeadersInspectionHandler); - assert.isTrue(next?.next?.next?.next?.next?.next instanceof UrlReplaceHandler); - assert.isTrue(next?.next?.next?.next?.next?.next?.next instanceof CustomFetchHandler); + assert.isTrue(next?.next?.next?.next instanceof HeadersInspectionHandler); + assert.isTrue(next?.next?.next?.next?.next instanceof UrlReplaceHandler); + assert.isTrue(next?.next?.next?.next?.next?.next instanceof CustomFetchHandler); }); it("Should set to default fetch handler middleware array if middleware parameter is null && customFetch is undefined", () => { @@ -78,5 +75,21 @@ describe("node - HTTPClient.ts", () => { assert.isDefined(client["middleware"]); assert.equal(client["customFetch"], dummyCustomFetch); }); + + it("Should set performance middleware for the http client", () => { + const client = new HttpClient(null, ...MiddlewareFactory.getPerformanceMiddlewares()); + + assert.isNotNull(client["middleware"]); + const next = client["middleware"].next; + + assert.isTrue(client["middleware"] instanceof RetryHandler); + assert.isTrue(next instanceof RedirectHandler); + assert.isTrue(next?.next instanceof ParametersNameDecodingHandler); + assert.isTrue(next?.next?.next instanceof UserAgentHandler); + assert.isTrue(next?.next?.next?.next instanceof CompressionHandler); + assert.isTrue(next?.next?.next?.next?.next instanceof HeadersInspectionHandler); + assert.isTrue(next?.next?.next?.next?.next?.next instanceof UrlReplaceHandler); + assert.isTrue(next?.next?.next?.next?.next?.next?.next instanceof CustomFetchHandler); + }); }); }); diff --git a/packages/http/fetch/test/node/kiotaClientFactory.ts b/packages/http/fetch/test/node/kiotaClientFactory.ts index 47f8966cf..668e6d215 100644 --- a/packages/http/fetch/test/node/kiotaClientFactory.ts +++ b/packages/http/fetch/test/node/kiotaClientFactory.ts @@ -7,7 +7,6 @@ import { assert, describe, it } from "vitest"; import { CustomFetchHandler, HeadersInspectionHandler, KiotaClientFactory, ParametersNameDecodingHandler, RedirectHandler, RetryHandler, UrlReplaceHandler, UserAgentHandler } from "../../src"; -import { CompressionHandler } from "../../src/middlewares/compressionHandler"; describe("browser - KiotaClientFactory", () => { it("Should return the http client", () => { @@ -19,10 +18,9 @@ describe("browser - KiotaClientFactory", () => { assert.isTrue(middleware?.next instanceof RedirectHandler); assert.isTrue(middleware?.next?.next instanceof ParametersNameDecodingHandler); assert.isTrue(middleware?.next?.next?.next instanceof UserAgentHandler); - assert.isTrue(middleware?.next?.next?.next?.next instanceof CompressionHandler); - assert.isTrue(middleware?.next?.next?.next?.next?.next instanceof HeadersInspectionHandler); - assert.isTrue(middleware?.next?.next?.next?.next?.next?.next instanceof UrlReplaceHandler); - assert.isTrue(middleware?.next?.next?.next?.next?.next?.next?.next instanceof CustomFetchHandler); + assert.isTrue(middleware?.next?.next?.next?.next instanceof HeadersInspectionHandler); + assert.isTrue(middleware?.next?.next?.next?.next?.next instanceof UrlReplaceHandler); + assert.isTrue(middleware?.next?.next?.next?.next?.next?.next instanceof CustomFetchHandler); }); it("Should maintain the middleware array order", () => {