|
| 1 | +/** |
| 2 | + * ------------------------------------------------------------------------------------------- |
| 3 | + * Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. |
| 4 | + * See License in the project root for license information. |
| 5 | + * ------------------------------------------------------------------------------------------- |
| 6 | + */ |
| 7 | + |
| 8 | +import { type RequestOption, inNodeEnv } from "@microsoft/kiota-abstractions"; |
| 9 | +import { Span, trace } from "@opentelemetry/api"; |
| 10 | + |
| 11 | +import { getObservabilityOptionsFromRequest } from "../observabilityOptions"; |
| 12 | +import type { Middleware } from "./middleware"; |
| 13 | +import { CompressionHandlerOptions, CompressionHandlerOptionsKey } from "./options/compressionHandlerOptions"; |
| 14 | +import type { FetchHeadersInit, FetchRequestInit } from "../utils/fetchDefinitions"; |
| 15 | +import { deleteRequestHeader, getRequestHeader, setRequestHeader } from "../utils/headersUtil"; |
| 16 | + |
| 17 | +/** |
| 18 | + * Compress the url content. |
| 19 | + */ |
| 20 | +export class CompressionHandler implements Middleware { |
| 21 | + next: Middleware | undefined; |
| 22 | + |
| 23 | + /** |
| 24 | + * @private |
| 25 | + * @static |
| 26 | + * A member holding the name of content range header |
| 27 | + */ |
| 28 | + private static readonly CONTENT_RANGE_HEADER = "Content-Range"; |
| 29 | + |
| 30 | + /** |
| 31 | + * @private |
| 32 | + * @static |
| 33 | + * A member holding the name of content encoding header |
| 34 | + */ |
| 35 | + private static readonly CONTENT_ENCODING_HEADER = "Content-Encoding"; |
| 36 | + |
| 37 | + /** |
| 38 | + * @public |
| 39 | + * @constructor |
| 40 | + * Creates a new instance of the CompressionHandler class |
| 41 | + * @param {CompressionHandlerOptions} handlerOptions The options for the compression handler. |
| 42 | + * @returns An instance of the CompressionHandler class |
| 43 | + */ |
| 44 | + public constructor(private readonly handlerOptions: CompressionHandlerOptions = new CompressionHandlerOptions()) { |
| 45 | + if (!handlerOptions) { |
| 46 | + throw new Error("handlerOptions cannot be undefined"); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @inheritdoc |
| 52 | + */ |
| 53 | + public execute(url: string, requestInit: RequestInit, requestOptions?: Record<string, RequestOption> | undefined): Promise<Response> { |
| 54 | + let currentOptions = this.handlerOptions; |
| 55 | + if (requestOptions?.[CompressionHandlerOptionsKey]) { |
| 56 | + currentOptions = requestOptions[CompressionHandlerOptionsKey] as CompressionHandlerOptions; |
| 57 | + } |
| 58 | + const obsOptions = getObservabilityOptionsFromRequest(requestOptions); |
| 59 | + if (obsOptions) { |
| 60 | + return trace.getTracer(obsOptions.getTracerInstrumentationName()).startActiveSpan("compressionHandler - execute", (span) => { |
| 61 | + try { |
| 62 | + span.setAttribute("com.microsoft.kiota.handler.compression.enable", currentOptions.ShouldCompress); |
| 63 | + return this.executeInternal(currentOptions, url, requestInit as FetchRequestInit, requestOptions, span); |
| 64 | + } finally { |
| 65 | + span.end(); |
| 66 | + } |
| 67 | + }); |
| 68 | + } |
| 69 | + return this.executeInternal(currentOptions, url, requestInit as FetchRequestInit, requestOptions); |
| 70 | + } |
| 71 | + |
| 72 | + private async executeInternal(options: CompressionHandlerOptions, url: string, requestInit: FetchRequestInit, requestOptions?: Record<string, RequestOption>, span?: Span): Promise<Response> { |
| 73 | + if (!options.ShouldCompress || this.contentRangeBytesIsPresent(requestInit.headers) || this.contentEncodingIsPresent(requestInit.headers) || requestInit.body === null || requestInit.body === undefined) { |
| 74 | + return this.next?.execute(url, requestInit as RequestInit, requestOptions) ?? Promise.reject(new Error("Response is undefined")); |
| 75 | + } |
| 76 | + |
| 77 | + span?.setAttribute("http.request.body.compressed", true); |
| 78 | + |
| 79 | + const unCompressedBody = requestInit.body; |
| 80 | + const unCompressedBodySize = this.getRequestBodySize(unCompressedBody); |
| 81 | + |
| 82 | + // compress the request body |
| 83 | + const compressedBody = await this.compressRequestBody(unCompressedBody); |
| 84 | + |
| 85 | + // add Content-Encoding to request header |
| 86 | + setRequestHeader(requestInit, CompressionHandler.CONTENT_ENCODING_HEADER, "gzip"); |
| 87 | + requestInit.body = compressedBody.compressedBody; |
| 88 | + |
| 89 | + span?.setAttribute("http.request.body.size", compressedBody.size); |
| 90 | + |
| 91 | + // execute the next middleware and check if the response code is 415 |
| 92 | + let response = await this.next?.execute(url, requestInit as RequestInit, requestOptions); |
| 93 | + if (!response) { |
| 94 | + throw new Error("Response is undefined"); |
| 95 | + } |
| 96 | + if (response.status === 415) { |
| 97 | + // remove the Content-Encoding header |
| 98 | + deleteRequestHeader(requestInit, CompressionHandler.CONTENT_ENCODING_HEADER); |
| 99 | + requestInit.body = unCompressedBody; |
| 100 | + span?.setAttribute("http.request.body.compressed", false); |
| 101 | + span?.setAttribute("http.request.body.size", unCompressedBodySize); |
| 102 | + |
| 103 | + response = await this.next?.execute(url, requestInit as RequestInit, requestOptions); |
| 104 | + } |
| 105 | + return response !== undefined && response !== null ? Promise.resolve(response) : Promise.reject(new Error("Response is undefined")); |
| 106 | + } |
| 107 | + |
| 108 | + private contentRangeBytesIsPresent(header: FetchHeadersInit | undefined): boolean { |
| 109 | + if (!header) { |
| 110 | + return false; |
| 111 | + } |
| 112 | + const contentRange = getRequestHeader(header, CompressionHandler.CONTENT_RANGE_HEADER); |
| 113 | + return contentRange?.toLowerCase().includes("bytes") ?? false; |
| 114 | + } |
| 115 | + |
| 116 | + private contentEncodingIsPresent(header: FetchHeadersInit | undefined): boolean { |
| 117 | + if (!header) { |
| 118 | + return false; |
| 119 | + } |
| 120 | + return getRequestHeader(header, CompressionHandler.CONTENT_ENCODING_HEADER) !== undefined; |
| 121 | + } |
| 122 | + |
| 123 | + private getRequestBodySize(body: unknown): number { |
| 124 | + if (!body) { |
| 125 | + return 0; |
| 126 | + } |
| 127 | + if (typeof body === "string") { |
| 128 | + return body.length; |
| 129 | + } |
| 130 | + if (body instanceof Blob) { |
| 131 | + return body.size; |
| 132 | + } |
| 133 | + if (body instanceof ArrayBuffer) { |
| 134 | + return body.byteLength; |
| 135 | + } |
| 136 | + if (ArrayBuffer.isView(body)) { |
| 137 | + return body.byteLength; |
| 138 | + } |
| 139 | + if (inNodeEnv() && Buffer.isBuffer(body)) { |
| 140 | + return body.byteLength; |
| 141 | + } |
| 142 | + throw new Error("Unsupported body type"); |
| 143 | + } |
| 144 | + |
| 145 | + private readBodyAsBytes(body: unknown): { stream: ReadableStream<Uint8Array>; size: number } { |
| 146 | + if (!body) { |
| 147 | + return { stream: new ReadableStream<Uint8Array>(), size: 0 }; |
| 148 | + } |
| 149 | + |
| 150 | + const uint8ArrayToStream = (uint8Array: Uint8Array): ReadableStream<Uint8Array> => { |
| 151 | + return new ReadableStream({ |
| 152 | + start(controller) { |
| 153 | + controller.enqueue(uint8Array); |
| 154 | + controller.close(); |
| 155 | + }, |
| 156 | + }); |
| 157 | + }; |
| 158 | + |
| 159 | + if (typeof body === "string") { |
| 160 | + return { stream: uint8ArrayToStream(new TextEncoder().encode(body)), size: body.length }; |
| 161 | + } |
| 162 | + if (body instanceof Blob) { |
| 163 | + return { stream: body.stream(), size: body.size }; |
| 164 | + } |
| 165 | + if (body instanceof ArrayBuffer) { |
| 166 | + return { stream: uint8ArrayToStream(new Uint8Array(body)), size: body.byteLength }; |
| 167 | + } |
| 168 | + if (ArrayBuffer.isView(body)) { |
| 169 | + return { stream: uint8ArrayToStream(new Uint8Array(body.buffer, body.byteOffset, body.byteLength)), size: body.byteLength }; |
| 170 | + } |
| 171 | + throw new Error("Unsupported body type"); |
| 172 | + } |
| 173 | + |
| 174 | + private async compressRequestBody(body: unknown): Promise<{ |
| 175 | + compressedBody: ArrayBuffer | Buffer; |
| 176 | + size: number; |
| 177 | + }> { |
| 178 | + if (!inNodeEnv()) { |
| 179 | + // in browser |
| 180 | + const compressionData = this.readBodyAsBytes(body); |
| 181 | + const compressedBody = await this.compressUsingCompressionStream(compressionData.stream); |
| 182 | + return { |
| 183 | + compressedBody: compressedBody.body, |
| 184 | + size: compressedBody.size, |
| 185 | + }; |
| 186 | + } else { |
| 187 | + // In Node.js |
| 188 | + const compressedBody = await this.compressUsingZlib(body); |
| 189 | + return { |
| 190 | + compressedBody, |
| 191 | + size: compressedBody.length, |
| 192 | + }; |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + private async compressUsingZlib(body: unknown): Promise<Buffer> { |
| 197 | + // @ts-ignore |
| 198 | + const zlib = await import("zlib"); |
| 199 | + return await new Promise((resolve, reject) => { |
| 200 | + zlib.gzip(body as string | ArrayBuffer | NodeJS.ArrayBufferView, (err, compressed) => { |
| 201 | + if (err) { |
| 202 | + reject(err); |
| 203 | + } else { |
| 204 | + resolve(compressed); |
| 205 | + } |
| 206 | + }); |
| 207 | + }); |
| 208 | + } |
| 209 | + |
| 210 | + private async compressUsingCompressionStream(uint8ArrayStream: ReadableStream<Uint8Array>): Promise<{ body: ArrayBuffer; size: number }> { |
| 211 | + const compressionStream = new CompressionStream("gzip"); |
| 212 | + |
| 213 | + const compressedStream = uint8ArrayStream.pipeThrough<Uint8Array>(compressionStream); |
| 214 | + |
| 215 | + const reader = compressedStream.getReader(); |
| 216 | + const compressedChunks: Uint8Array[] = []; |
| 217 | + let totalLength = 0; |
| 218 | + |
| 219 | + let result = await reader.read(); |
| 220 | + while (!result.done) { |
| 221 | + const chunk = result.value; |
| 222 | + compressedChunks.push(chunk); |
| 223 | + totalLength += chunk.length; |
| 224 | + result = await reader.read(); |
| 225 | + } |
| 226 | + |
| 227 | + const compressedArray = new Uint8Array(totalLength); |
| 228 | + let offset = 0; |
| 229 | + for (const chunk of compressedChunks) { |
| 230 | + compressedArray.set(chunk, offset); |
| 231 | + offset += chunk.length; |
| 232 | + } |
| 233 | + |
| 234 | + return { |
| 235 | + body: compressedArray.buffer, |
| 236 | + size: compressedArray.length, |
| 237 | + }; |
| 238 | + } |
| 239 | +} |
0 commit comments