Skip to content

Commit

Permalink
Merge pull request #1548 from microsoft/feat/profile_compression
Browse files Browse the repository at this point in the history
feat: disable default compression
  • Loading branch information
baywet authored Jan 21, 2025
2 parents 8f7f78f + 8ed62c2 commit 72e7011
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 37 deletions.
2 changes: 2 additions & 0 deletions packages/http/fetch/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";
Expand Down
4 changes: 4 additions & 0 deletions packages/http/fetch/src/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions packages/http/fetch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";
Expand Down
13 changes: 12 additions & 1 deletion packages/http/fetch/src/middlewares/browser/middlewareFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Response> = (...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<Response> = (...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;
}
}
13 changes: 12 additions & 1 deletion packages/http/fetch/src/middlewares/middlewareFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Response> = (...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<Response> = (...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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
/**
Expand Down
33 changes: 23 additions & 10 deletions packages/http/fetch/test/browser/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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", () => {
Expand All @@ -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", () => {
Expand All @@ -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);
});
});
});
8 changes: 3 additions & 5 deletions packages/http/fetch/test/browser/kiotaClientFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand All @@ -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);
});
});
14 changes: 12 additions & 2 deletions packages/http/fetch/test/browser/middlewareFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 13 additions & 2 deletions packages/http/fetch/test/node/MiddlewareFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
33 changes: 23 additions & 10 deletions packages/http/fetch/test/node/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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", () => {
Expand All @@ -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", () => {
Expand All @@ -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);
});
});
});
8 changes: 3 additions & 5 deletions packages/http/fetch/test/node/kiotaClientFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand All @@ -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", () => {
Expand Down

0 comments on commit 72e7011

Please sign in to comment.