diff --git a/packages/middleware-compression/LICENSE b/packages/middleware-compression/LICENSE new file mode 100644 index 0000000000000..7b6491ba78762 --- /dev/null +++ b/packages/middleware-compression/LICENSE @@ -0,0 +1,201 @@ +Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2018-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. \ No newline at end of file diff --git a/packages/middleware-compression/README.md b/packages/middleware-compression/README.md new file mode 100644 index 0000000000000..936b088b0ba8f --- /dev/null +++ b/packages/middleware-compression/README.md @@ -0,0 +1,12 @@ +# @aws-sdk/middleware-compression + +[![NPM version](https://img.shields.io/npm/v/@aws-sdk/middleware-token/latest.svg)](https://www.npmjs.com/package/@aws-sdk/middleware-compression) +[![NPM downloads](https://img.shields.io/npm/dm/@aws-sdk/middleware-token.svg)](https://www.npmjs.com/package/@aws-sdk/middleware-compression) + +Middleware and Plugin for request compression. + +> An internal package + +## Usage + +You probably shouldn't, at least directly. diff --git a/packages/middleware-compression/jest.config.js b/packages/middleware-compression/jest.config.js new file mode 100644 index 0000000000000..95d8863b22a18 --- /dev/null +++ b/packages/middleware-compression/jest.config.js @@ -0,0 +1,6 @@ +const base = require("../../jest.config.base.js"); + +module.exports = { + ...base, + testMatch: ["**/*.spec.ts"], +}; diff --git a/packages/middleware-compression/package.json b/packages/middleware-compression/package.json new file mode 100644 index 0000000000000..6dfeb3d765330 --- /dev/null +++ b/packages/middleware-compression/package.json @@ -0,0 +1,68 @@ +{ + "name": "middleware-compression", + "version": "1.0.0", + "description": "Middleware and Plugin for request compression.", + "scripts": { + "build": "concurrently 'yarn:build:cjs' 'yarn:build:es' 'yarn:build:types'", + "build:cjs": "tsc -p tsconfig.cjs.json", + "build:es": "tsc -p tsconfig.es.json", + "build:include:deps": "lerna run --scope $npm_package_name --include-dependencies build", + "build:types": "tsc -p tsconfig.types.json", + "build:types:downlevel": "downlevel-dts dist-types dist-types/ts3.4", + "clean": "rimraf ./dist-* && rimraf *.tsbuildinfo", + "test": "jest" + }, + "main": "./dist-cjs/index.js", + "module": "./dist-es/index.js", + "dependencies": { + "@smithy/node-config-provider": "^2.1.8", + "@smithy/types": "^2.7.0", + "@smithy/util-config-provider": "^2.1.0", + "fflate": "0.8.1", + "tslib": "^2.5.0" + }, + "devDependencies": { + "@tsconfig/recommended": "1.0.1", + "@types/node": "^14.14.31", + "concurrently": "7.0.0", + "downlevel-dts": "0.10.1", + "rimraf": "3.0.2", + "typescript": "~4.9.5", + "web-streams-polyfill": "3.2.1" + }, + "types": "./dist-types/index.d.ts", + "engines": { + "node": ">=14.0.0" + }, + "typesVersions": { + "<4.0": { + "dist-types/*": [ + "dist-types/ts3.4/*" + ] + } + }, + "files": [ + "dist-*/**" + ], + "keywords": [ + "middleware", + "compression", + "gzip" + ], + "author": { + "name": "AWS SDK for JavaScript Team", + "url": "https://aws.amazon.com/javascript/" + }, + "license": "Apache-2.0", + "browser": { + "./dist-es/compressString": "./dist-es/compressString.browser", + "./dist-es/compressStream": "./dist-es/compressStream.browser" + }, + "react-native": {}, + "homepage": "https://github.com/aws/aws-sdk-js-v3/tree/main/packages/middleware-compression", + "repository": { + "type": "git", + "url": "https://github.com/aws/aws-sdk-js-v3.git", + "directory": "packages/middleware-compression" + } +} diff --git a/packages/middleware-compression/src/NODE_DISABLE_REQUEST_COMPRESSION_CONFIG_OPTIONS.spec.ts b/packages/middleware-compression/src/NODE_DISABLE_REQUEST_COMPRESSION_CONFIG_OPTIONS.spec.ts new file mode 100644 index 0000000000000..5d6699bd7d05d --- /dev/null +++ b/packages/middleware-compression/src/NODE_DISABLE_REQUEST_COMPRESSION_CONFIG_OPTIONS.spec.ts @@ -0,0 +1,50 @@ +import { booleanSelector, SelectorType } from "@smithy/util-config-provider"; + +import { + NODE_DISABLE_REQUEST_COMPRESSION_CONFIG_OPTIONS, + NODE_DISABLE_REQUEST_COMPRESSION_ENV_NAME, + NODE_DISABLE_REQUEST_COMPRESSION_INI_NAME, +} from "./NODE_DISABLE_REQUEST_COMPRESSION_CONFIG_OPTIONS"; + +jest.mock("@smithy/util-config-provider"); + +describe("NODE_DISABLE_REQUEST_COMPRESSION_CONFIG_OPTIONS", () => { + afterEach(() => { + jest.clearAllMocks(); + }); + + const test = (func: Function, obj: Record, key: string, type: SelectorType) => { + it.each([true, false, undefined])("returns %s", (output) => { + (booleanSelector as jest.Mock).mockReturnValueOnce(output); + expect(func(obj)).toEqual(output); + expect(booleanSelector).toBeCalledWith(obj, key, type); + }); + + it("throws error", () => { + const mockError = new Error("error"); + (booleanSelector as jest.Mock).mockImplementationOnce(() => { + throw mockError; + }); + expect(() => { + func(obj); + }).toThrow(mockError); + }); + }; + + describe("calls booleanSelector for environmentVariableSelector", () => { + const env: { [NODE_DISABLE_REQUEST_COMPRESSION_ENV_NAME]: any } = {} as any; + const { environmentVariableSelector } = NODE_DISABLE_REQUEST_COMPRESSION_CONFIG_OPTIONS; + test(environmentVariableSelector, env, NODE_DISABLE_REQUEST_COMPRESSION_ENV_NAME, SelectorType.ENV); + }); + + describe("calls booleanSelector for configFileSelector", () => { + const profileContent: { [NODE_DISABLE_REQUEST_COMPRESSION_INI_NAME]: any } = {} as any; + const { configFileSelector } = NODE_DISABLE_REQUEST_COMPRESSION_CONFIG_OPTIONS; + test(configFileSelector, profileContent, NODE_DISABLE_REQUEST_COMPRESSION_INI_NAME, SelectorType.CONFIG); + }); + + it("returns false for default", () => { + const { default: defaultValue } = NODE_DISABLE_REQUEST_COMPRESSION_CONFIG_OPTIONS; + expect(defaultValue).toEqual(false); + }); +}); diff --git a/packages/middleware-compression/src/NODE_DISABLE_REQUEST_COMPRESSION_CONFIG_OPTIONS.ts b/packages/middleware-compression/src/NODE_DISABLE_REQUEST_COMPRESSION_CONFIG_OPTIONS.ts new file mode 100644 index 0000000000000..5e63696f98f97 --- /dev/null +++ b/packages/middleware-compression/src/NODE_DISABLE_REQUEST_COMPRESSION_CONFIG_OPTIONS.ts @@ -0,0 +1,23 @@ +import { LoadedConfigSelectors } from "@smithy/node-config-provider"; +import { booleanSelector, SelectorType } from "@smithy/util-config-provider"; + +/** + * @internal + */ +export const NODE_DISABLE_REQUEST_COMPRESSION_ENV_NAME = "AWS_DISABLE_REQUEST_COMPRESSION"; + +/** + * @internal + */ +export const NODE_DISABLE_REQUEST_COMPRESSION_INI_NAME = "disable_request_compression"; + +/** + * @internal + */ +export const NODE_DISABLE_REQUEST_COMPRESSION_CONFIG_OPTIONS: LoadedConfigSelectors = { + environmentVariableSelector: (env: NodeJS.ProcessEnv) => + booleanSelector(env, NODE_DISABLE_REQUEST_COMPRESSION_ENV_NAME, SelectorType.ENV), + configFileSelector: (profile) => + booleanSelector(profile, NODE_DISABLE_REQUEST_COMPRESSION_INI_NAME, SelectorType.CONFIG), + default: false, +}; diff --git a/packages/middleware-compression/src/NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_CONFIG_OPTIONS.spec.ts b/packages/middleware-compression/src/NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_CONFIG_OPTIONS.spec.ts new file mode 100644 index 0000000000000..3286b7fc61405 --- /dev/null +++ b/packages/middleware-compression/src/NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_CONFIG_OPTIONS.spec.ts @@ -0,0 +1,50 @@ +import { numberSelector, SelectorType } from "@smithy/util-config-provider"; + +import { + NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_CONFIG_OPTIONS, + NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_ENV_NAME, + NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_INI_NAME, +} from "./NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_CONFIG_OPTIONS"; + +jest.mock("@smithy/util-config-provider"); + +describe("NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_CONFIG_OPTIONS", () => { + afterEach(() => { + jest.clearAllMocks(); + }); + + const test = (func: Function, obj: Record, key: string, type: SelectorType) => { + it.each([0, 1, undefined])("returns %s", (output) => { + (numberSelector as jest.Mock).mockReturnValueOnce(output); + expect(func(obj)).toEqual(output); + expect(numberSelector).toBeCalledWith(obj, key, type); + }); + + it("throws error", () => { + const mockError = new Error("error"); + (numberSelector as jest.Mock).mockImplementationOnce(() => { + throw mockError; + }); + expect(() => { + func(obj); + }).toThrow(mockError); + }); + }; + + describe("calls numberSelector for environmentVariableSelector", () => { + const env: { [NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_ENV_NAME]: any } = {} as any; + const { environmentVariableSelector } = NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_CONFIG_OPTIONS; + test(environmentVariableSelector, env, NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_ENV_NAME, SelectorType.ENV); + }); + + describe("calls numberSelector for configFileSelector", () => { + const profileContent: { [NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_INI_NAME]: any } = {} as any; + const { configFileSelector } = NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_CONFIG_OPTIONS; + test(configFileSelector, profileContent, NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_INI_NAME, SelectorType.CONFIG); + }); + + it("returns 10240 for default", () => { + const { default: defaultValue } = NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_CONFIG_OPTIONS; + expect(defaultValue).toEqual(10240); + }); +}); diff --git a/packages/middleware-compression/src/NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_CONFIG_OPTIONS.ts b/packages/middleware-compression/src/NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_CONFIG_OPTIONS.ts new file mode 100644 index 0000000000000..fbff3aea0d243 --- /dev/null +++ b/packages/middleware-compression/src/NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_CONFIG_OPTIONS.ts @@ -0,0 +1,23 @@ +import { LoadedConfigSelectors } from "@smithy/node-config-provider"; +import { numberSelector, SelectorType } from "@smithy/util-config-provider"; + +/** + * @internal + */ +export const NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_ENV_NAME = "AWS_REQUEST_MIN_COMPRESSION_SIZE_BYTES"; + +/** + * @internal + */ +export const NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_INI_NAME = "request_min_compression_size_bytes"; + +/** + * @internal + */ +export const NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_CONFIG_OPTIONS: LoadedConfigSelectors = { + environmentVariableSelector: (env: NodeJS.ProcessEnv) => + numberSelector(env, NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_ENV_NAME, SelectorType.ENV), + configFileSelector: (profile) => + numberSelector(profile, NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_INI_NAME, SelectorType.CONFIG), + default: 10240, +}; diff --git a/packages/middleware-compression/src/compressStream.browser.spec.ts b/packages/middleware-compression/src/compressStream.browser.spec.ts new file mode 100644 index 0000000000000..bca22275e4c05 --- /dev/null +++ b/packages/middleware-compression/src/compressStream.browser.spec.ts @@ -0,0 +1,73 @@ +// @jest-environment jsdom +import { AsyncGzip } from "fflate"; +import { ReadableStream } from "web-streams-polyfill"; + +import { compressStream } from "./compressStream.browser"; + +jest.mock("fflate"); + +describe(compressStream.name, () => { + const compressionSuffix = "compressed"; + const compressionSeparator = "."; + const asyncGzip = { + ondata: jest.fn(), + push: jest.fn().mockImplementation((chunk, final) => { + const data = typeof chunk === "string" ? [chunk, compressionSuffix].join(compressionSeparator) : null; + asyncGzip.ondata(undefined, data, final); + }), + }; + + beforeEach(() => { + (AsyncGzip as jest.Mock).mockImplementation(() => asyncGzip); + }); + + afterEach(() => { + jest.resetAllMocks(); + }); + + it("compresses a stream", async () => { + const inputChunks = ["hello", "world"]; + const inputStream = new ReadableStream({ + start(controller) { + for (const inputChunk of inputChunks) { + controller.enqueue(inputChunk); + } + controller.close(); + }, + }); + + const compressionStream = await compressStream(inputStream); + const reader = compressionStream.getReader(); + for (const inputChunk of inputChunks) { + const { value, done } = await reader.read(); + expect(value).toEqual([inputChunk, compressionSuffix].join(compressionSeparator)); + expect(done).toEqual(false); + } + + // Mock for last push. + const { value, done } = await reader.read(); + expect(value).toEqual(null); + expect(done).toEqual(false); + + // Mock for stream ending. + const { value: valueFinal, done: doneFinal } = await reader.read(); + expect(valueFinal).toEqual(undefined); + expect(doneFinal).toEqual(true); + }); + + it("should throw an error if compression fails", async () => { + const compressionErrorMsg = "compression error message"; + const compressionError = new Error(compressionErrorMsg); + (AsyncGzip as jest.Mock).mockImplementationOnce(() => { + throw compressionError; + }); + + const inputStream = new ReadableStream({ + start(controller) { + controller.close(); + }, + }); + + await expect(compressStream(inputStream)).rejects.toThrow(compressionError); + }); +}); diff --git a/packages/middleware-compression/src/compressStream.browser.ts b/packages/middleware-compression/src/compressStream.browser.ts new file mode 100644 index 0000000000000..266744511db8f --- /dev/null +++ b/packages/middleware-compression/src/compressStream.browser.ts @@ -0,0 +1,35 @@ +import { AsyncGzip } from "fflate"; + +export const compressStream = async (body: ReadableStream): Promise => { + let endCallback: () => void; + const asyncGzip = new AsyncGzip(); + + // Replace with Compression Streams API once supported in all browsers. + // https://developer.mozilla.org/en-US/docs/Web/API/Compression_Streams_API + const compressionStream = new TransformStream({ + start(controller) { + asyncGzip.ondata = (err, data, final) => { + if (err) { + controller.error(err); + } else { + controller.enqueue(data); + if (final) { + if (endCallback) endCallback(); + else controller.terminate(); + } + } + }; + }, + transform(chunk) { + asyncGzip.push(chunk); + }, + flush() { + return new Promise((resolve) => { + endCallback = resolve; + asyncGzip.push(new Uint8Array(0), true); + }); + }, + }); + + return body.pipeThrough(compressionStream); +}; diff --git a/packages/middleware-compression/src/compressStream.spec.ts b/packages/middleware-compression/src/compressStream.spec.ts new file mode 100644 index 0000000000000..96ee325ca2859 --- /dev/null +++ b/packages/middleware-compression/src/compressStream.spec.ts @@ -0,0 +1,52 @@ +import { Readable } from "stream"; +import { createGzip } from "zlib"; + +import { compressStream } from "./compressStream"; + +jest.mock("zlib"); + +describe(compressStream.name, () => { + const getGenerator = (chunks: string[]) => + async function* generator() { + for (const chunk of chunks) { + yield chunk; + } + }; + + const testInputStream = Readable.from(getGenerator(["input"])()); + const mockGzipFn = jest.fn(); + const testOutputStream = Readable.from(getGenerator(["input", "gzipped"])()); + + beforeEach(() => { + (createGzip as jest.Mock).mockReturnValue(mockGzipFn); + testInputStream.pipe = jest.fn().mockReturnValue(testOutputStream); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + it("should compress a readable stream using gzip", async () => { + const outputStream = await compressStream(testInputStream); + + expect(outputStream).toBeInstanceOf(Readable); + expect(outputStream).toBe(testOutputStream); + + expect(testInputStream.pipe).toHaveBeenCalledTimes(1); + expect(testInputStream.pipe).toHaveBeenCalledWith(mockGzipFn); + expect(createGzip).toHaveBeenCalledTimes(1); + }); + + it("should throw an error if compression fails", async () => { + const compressionErrorMsg = "compression error message"; + const compressionError = new Error(compressionErrorMsg); + (createGzip as jest.Mock).mockImplementationOnce(() => { + throw compressionError; + }); + + await expect(compressStream(testInputStream)).rejects.toThrow(compressionError); + + expect(createGzip).toHaveBeenCalledTimes(1); + expect(testInputStream.pipe).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/middleware-compression/src/compressStream.ts b/packages/middleware-compression/src/compressStream.ts new file mode 100644 index 0000000000000..44e0bacad4b34 --- /dev/null +++ b/packages/middleware-compression/src/compressStream.ts @@ -0,0 +1,4 @@ +import { Readable } from "stream"; +import { createGzip } from "zlib"; + +export const compressStream = async (body: Readable): Promise => body.pipe(createGzip()); diff --git a/packages/middleware-compression/src/compressString.browser.spec.ts b/packages/middleware-compression/src/compressString.browser.spec.ts new file mode 100644 index 0000000000000..0a03de318647f --- /dev/null +++ b/packages/middleware-compression/src/compressString.browser.spec.ts @@ -0,0 +1,53 @@ +// @jest-environment jsdom +import { toUint8Array } from "@smithy/util-utf8"; +import { gzip } from "fflate"; + +import { compressString } from "./compressString.browser"; + +jest.mock("@smithy/util-utf8"); +jest.mock("fflate"); + +describe(compressString.name, () => { + const testData = "test"; + const compressionSuffix = "compressed"; + const compressionSeparator = "."; + + beforeEach(() => { + (toUint8Array as jest.Mock).mockImplementation((data) => data); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + it("should compress data with gzip", async () => { + (gzip as jest.Mock).mockImplementation((data, callback) => { + callback(null, [data, compressionSuffix].join(compressionSeparator)); + }); + const receivedOutput = await compressString(testData); + const expectedOutput = [testData, compressionSuffix].join(compressionSeparator); + + expect(receivedOutput).toEqual(expectedOutput); + expect(gzip).toHaveBeenCalledTimes(1); + expect(gzip).toHaveBeenCalledWith(testData, expect.any(Function)); + expect(toUint8Array).toHaveBeenCalledTimes(1); + expect(toUint8Array).toHaveBeenCalledWith(testData); + }); + + it("should throw an error if compression fails", async () => { + const compressionErrorMsg = "compression error message"; + const compressionError = new Error(compressionErrorMsg); + (gzip as jest.Mock).mockImplementation((data, callback) => { + callback(compressionError); + }); + + await expect(compressString(testData)).rejects.toThrow( + new Error("Failure during compression: " + compressionErrorMsg) + ); + + expect(gzip).toHaveBeenCalledTimes(1); + expect(gzip).toHaveBeenCalledWith(testData, expect.any(Function)); + expect(toUint8Array).toHaveBeenCalledTimes(1); + expect(toUint8Array).toHaveBeenCalledWith(testData); + }); +}); diff --git a/packages/middleware-compression/src/compressString.browser.ts b/packages/middleware-compression/src/compressString.browser.ts new file mode 100644 index 0000000000000..a24c92adae5ee --- /dev/null +++ b/packages/middleware-compression/src/compressString.browser.ts @@ -0,0 +1,13 @@ +import { toUint8Array } from "@smithy/util-utf8"; +import { gzip } from "fflate"; + +export const compressString = async (body: any): Promise => + new Promise((resolve, reject) => { + gzip(toUint8Array(body || ""), (err, data) => { + if (err) { + reject(new Error("Failure during compression: " + err.message)); + } else { + resolve(data); + } + }); + }); diff --git a/packages/middleware-compression/src/compressString.spec.ts b/packages/middleware-compression/src/compressString.spec.ts new file mode 100644 index 0000000000000..d77d4814c328c --- /dev/null +++ b/packages/middleware-compression/src/compressString.spec.ts @@ -0,0 +1,54 @@ +import { toUint8Array } from "@smithy/util-utf8"; +import { gzip } from "zlib"; + +import { compressString } from "./compressString"; + +const compressionSuffix = "compressed"; +const compressionSeparator = "."; + +jest.mock("@smithy/util-utf8"); +jest.mock("util", () => ({ promisify: jest.fn().mockImplementation((fn) => fn) })); +jest.mock("zlib", () => ({ + gzip: jest.fn().mockImplementation((data) => [data, compressionSuffix].join(compressionSeparator)), +})); + +describe(compressString.name, () => { + const testData = "test"; + + beforeEach(() => { + (toUint8Array as jest.Mock).mockImplementation((data) => data); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + it("should compress data with gzip", async () => { + const receivedOutput = await compressString(testData); + const expectedOutput = [testData, compressionSuffix].join(compressionSeparator); + + expect(receivedOutput).toEqual(expectedOutput); + expect(gzip).toHaveBeenCalledTimes(1); + expect(gzip).toHaveBeenCalledWith(testData); + expect(toUint8Array).toHaveBeenCalledTimes(2); + expect(toUint8Array).toHaveBeenNthCalledWith(1, testData); + expect(toUint8Array).toHaveBeenNthCalledWith(2, expectedOutput); + }); + + it("should throw an error if compression fails", async () => { + const compressionErrorMsg = "compression error message"; + const compressionError = new Error(compressionErrorMsg); + (gzip as unknown as jest.Mock).mockImplementationOnce(() => { + throw compressionError; + }); + + await expect(compressString(testData)).rejects.toThrow( + new Error("Failure during compression: " + compressionErrorMsg) + ); + + expect(gzip).toHaveBeenCalledTimes(1); + expect(gzip).toHaveBeenCalledWith(testData); + expect(toUint8Array).toHaveBeenCalledTimes(1); + expect(toUint8Array).toHaveBeenCalledWith(testData); + }); +}); diff --git a/packages/middleware-compression/src/compressString.ts b/packages/middleware-compression/src/compressString.ts new file mode 100644 index 0000000000000..287f88552593f --- /dev/null +++ b/packages/middleware-compression/src/compressString.ts @@ -0,0 +1,15 @@ +import { toUint8Array } from "@smithy/util-utf8"; +import { promisify } from "util"; +import { gzip } from "zlib"; + +const gzipAsync = promisify(gzip); + +export const compressString = async (body: any): Promise => { + // Only gzip shall be supported initial release. + try { + const compressedBuffer = await gzipAsync(toUint8Array(body || "")); + return toUint8Array(compressedBuffer); + } catch (err) { + throw new Error("Failure during compression: " + err.message); + } +}; diff --git a/packages/middleware-compression/src/compressionMiddleware.spec.ts b/packages/middleware-compression/src/compressionMiddleware.spec.ts new file mode 100644 index 0000000000000..1691d977c206c --- /dev/null +++ b/packages/middleware-compression/src/compressionMiddleware.spec.ts @@ -0,0 +1,172 @@ +import { HttpRequest } from "@smithy/protocol-http"; + +import { compressionMiddleware } from "./compressionMiddleware"; +import { compressStream } from "./compressStream"; +import { compressString } from "./compressString"; +import { CompressionAlgorithm } from "./constants"; +import { isStreaming } from "./isStreaming"; + +jest.mock("@smithy/protocol-http"); +jest.mock("./compressString"); +jest.mock("./compressStream"); +jest.mock("./isStreaming"); + +describe(compressionMiddleware.name, () => { + const mockBody = "body"; + const mockConfig = { + bodyLengthChecker: jest.fn().mockReturnValue(mockBody.length), + disableRequestCompression: false, + requestMinCompressionSizeBytes: 0, + }; + const mockMiddlewareConfig = { + encodings: [CompressionAlgorithm.GZIP], + }; + + const mockNext = jest.fn(); + const mockContext = {}; + const mockArgs = { request: { headers: {}, body: mockBody } }; + + afterEach(() => { + jest.clearAllMocks(); + }); + + it("skips compression if it's not an HttpRequest", async () => { + const { isInstance } = HttpRequest; + (isInstance as unknown as jest.Mock).mockReturnValue(false); + await compressionMiddleware(mockConfig, mockMiddlewareConfig)(mockNext, mockContext)({ ...mockArgs } as any); + expect(mockNext).toHaveBeenCalledWith(mockArgs); + }); + + describe("HttpRequest", () => { + beforeEach(() => { + const { isInstance } = HttpRequest; + (isInstance as unknown as jest.Mock).mockReturnValue(true); + (isStreaming as jest.Mock).mockReturnValue(false); + }); + + it("skips compression if disabled", async () => { + await compressionMiddleware({ ...mockConfig, disableRequestCompression: true }, mockMiddlewareConfig)( + mockNext, + mockContext + )({ ...mockArgs } as any); + expect(mockNext).toHaveBeenCalledWith(mockArgs); + }); + + it("skips compression if encodings are not provided", async () => { + await compressionMiddleware(mockConfig, { encodings: [] })(mockNext, mockContext)({ ...mockArgs } as any); + expect(mockNext).toHaveBeenCalledWith(mockArgs); + }); + + it("skips compression if encodings are not supported", async () => { + await compressionMiddleware(mockConfig, { encodings: ["brotli"] })(mockNext, mockContext)({ ...mockArgs } as any); + expect(mockNext).toHaveBeenCalledWith(mockArgs); + }); + + describe("streaming", () => { + beforeEach(() => { + (isStreaming as jest.Mock).mockReturnValue(true); + }); + + it("throws error if streaming blob requires length", async () => { + await expect( + compressionMiddleware(mockConfig, { ...mockMiddlewareConfig, streamRequiresLength: true })( + mockNext, + mockContext + )({ ...mockArgs } as any) + ).rejects.toThrow("Compression is not supported for streaming blobs that require a length."); + + expect(isStreaming).toHaveBeenCalledTimes(1); + expect(isStreaming).toHaveBeenCalledWith(mockBody); + expect(mockNext).not.toHaveBeenCalled(); + }); + + it("compresses streaming blob", async () => { + const mockCompressedStream = "compressed-stream"; + (compressStream as jest.Mock).mockResolvedValueOnce(mockCompressedStream); + + await compressionMiddleware(mockConfig, mockMiddlewareConfig)(mockNext, mockContext)({ ...mockArgs } as any); + + expect(isStreaming).toHaveBeenCalledTimes(1); + expect(isStreaming).toHaveBeenCalledWith(mockBody); + expect(mockNext).toHaveBeenCalledWith({ + ...mockArgs, + request: { + ...mockArgs.request, + body: mockCompressedStream, + headers: { + ...mockArgs.request.headers, + "Content-Encoding": "gzip", + }, + }, + }); + expect(compressStream).toHaveBeenCalledTimes(1); + expect(compressStream).toHaveBeenCalledWith(mockBody); + }); + }); + + describe("not streaming", () => { + it("skips compression if body is smaller than min size", async () => { + await compressionMiddleware( + { ...mockConfig, requestMinCompressionSizeBytes: mockBody.length + 1 }, + mockMiddlewareConfig + )( + mockNext, + mockContext + )({ ...mockArgs } as any); + + expect(mockNext).toHaveBeenCalledWith(mockArgs); + }); + + it("compresses body", async () => { + const mockCompressedBody = "compressed-body"; + (compressString as jest.Mock).mockResolvedValueOnce(mockCompressedBody); + + await compressionMiddleware(mockConfig, mockMiddlewareConfig)(mockNext, mockContext)({ ...mockArgs } as any); + + expect(mockNext).toHaveBeenCalledWith({ + ...mockArgs, + request: { + ...mockArgs.request, + body: mockCompressedBody, + headers: { + ...mockArgs.request.headers, + "Content-Encoding": "gzip", + }, + }, + }); + expect(compressString).toHaveBeenCalledTimes(1); + expect(compressString).toHaveBeenCalledWith(mockBody); + }); + + it("appends algorithm to existing Content-Encoding header", async () => { + const mockCompressedBody = "compressed-body"; + (compressString as jest.Mock).mockResolvedValueOnce(mockCompressedBody); + + const mockExistingContentEncoding = "deflate"; + await compressionMiddleware(mockConfig, mockMiddlewareConfig)(mockNext, mockContext)({ + ...mockArgs, + request: { + ...mockArgs.request, + headers: { + "Content-Encoding": mockExistingContentEncoding, + }, + }, + } as any); + + expect(mockNext).toHaveBeenCalledWith({ + ...mockArgs, + request: { + ...mockArgs.request, + body: mockCompressedBody, + headers: { + ...mockArgs.request.headers, + "Content-Encoding": [mockExistingContentEncoding, "gzip"].join(","), + }, + }, + }); + expect(compressString).toHaveBeenCalledTimes(1); + expect(compressString).toHaveBeenCalledWith(mockBody); + }); + }); + }); +}); diff --git a/packages/middleware-compression/src/compressionMiddleware.ts b/packages/middleware-compression/src/compressionMiddleware.ts new file mode 100644 index 0000000000000..2b41a7d5052d0 --- /dev/null +++ b/packages/middleware-compression/src/compressionMiddleware.ts @@ -0,0 +1,105 @@ +import { HttpRequest } from "@smithy/protocol-http"; +import { + AbsoluteLocation, + BuildHandler, + BuildHandlerArguments, + BuildHandlerOptions, + BuildHandlerOutput, + BuildMiddleware, + MetadataBearer, +} from "@smithy/types"; + +import { compressStream } from "./compressStream"; +import { compressString } from "./compressString"; +import { CompressionResolvedConfig } from "./configurations"; +import { CLIENT_SUPPORTED_ALGORITHMS, CompressionAlgorithm } from "./constants"; +import { isStreaming } from "./isStreaming"; + +/** + * @internal + */ +export interface CompressionMiddlewareConfig { + /** + * Defines the priority-ordered list of compression algorithms supported by the service operation. + */ + encodings: string[]; + + /** + * Indicates that the streaming blob MUST be finite and have a known size when sending data from a client to a server. + * Populated if smithy requiresLength is set https://smithy.io/2.0/spec/streaming.html#requireslength-trait + */ + streamRequiresLength?: boolean; +} + +/** + * @internal + */ +export const compressionMiddleware = + (config: CompressionResolvedConfig, middlewareConfig: CompressionMiddlewareConfig): BuildMiddleware => + (next: BuildHandler): BuildHandler => + async (args: BuildHandlerArguments): Promise> => { + if (!HttpRequest.isInstance(args.request) || config.disableRequestCompression) { + return next(args); + } + + const { request } = args; + const { body, headers } = request; + const { encodings, streamRequiresLength } = middlewareConfig; + + let updatedBody = body; + let updatedHeaders = headers; + + for (const algorithm of encodings) { + if (CLIENT_SUPPORTED_ALGORITHMS.includes(algorithm as CompressionAlgorithm)) { + let isRequestCompressed = false; + if (isStreaming(body)) { + if (!streamRequiresLength) { + updatedBody = await compressStream(body); + isRequestCompressed = true; + } else { + // Invalid case. We should never get here. + throw new Error("Compression is not supported for streaming blobs that require a length."); + } + } else { + const bodyLength = config.bodyLengthChecker(body); + if (bodyLength && bodyLength >= config.requestMinCompressionSizeBytes) { + updatedBody = await compressString(body); + isRequestCompressed = true; + } + } + + if (isRequestCompressed) { + // Either append to the header if it already exists, else set it + if (headers["Content-Encoding"]) { + updatedHeaders = { + ...headers, + "Content-Encoding": `${headers["Content-Encoding"]},${algorithm}`, + }; + } else { + updatedHeaders = { ...headers, "Content-Encoding": algorithm }; + } + + // We've matched on one supported algorithm in the + // priority-ordered list, so we're finished. + break; + } + } + } + + return next({ + ...args, + request: { + ...request, + body: updatedBody, + headers: updatedHeaders, + }, + }); + }; + +export const compressionMiddlewareOptions: BuildHandlerOptions & AbsoluteLocation = { + name: "compressionMiddleware", + step: "build", + tags: ["REQUEST_BODY_COMPRESSION", "GZIP"], + override: true, + priority: "high", +}; diff --git a/packages/middleware-compression/src/configurations.ts b/packages/middleware-compression/src/configurations.ts new file mode 100644 index 0000000000000..e25c1e23bc337 --- /dev/null +++ b/packages/middleware-compression/src/configurations.ts @@ -0,0 +1,27 @@ +import { BodyLengthCalculator } from "@smithy/types"; + +/** + * @public + */ +export interface CompressionInputConfig { + /** + * A function that can calculate the length of a body. + */ + bodyLengthChecker: BodyLengthCalculator; + + /** + * Whether to disable request compression. + */ + disableRequestCompression: boolean; + + /** + * The minimum size in bytes that a request body should be to trigger compression. + * The value must be a non-negative integer value between 0 and 10485760 bytes inclusive. + */ + requestMinCompressionSizeBytes: number; +} + +/** + * @internal + */ +export interface CompressionResolvedConfig extends CompressionInputConfig {} diff --git a/packages/middleware-compression/src/constants.ts b/packages/middleware-compression/src/constants.ts new file mode 100644 index 0000000000000..889932c718a0f --- /dev/null +++ b/packages/middleware-compression/src/constants.ts @@ -0,0 +1,8 @@ +/** + * Compression Algorithms supported by the SDK. + */ +export enum CompressionAlgorithm { + GZIP = "gzip", +} + +export const CLIENT_SUPPORTED_ALGORITHMS: CompressionAlgorithm[] = [CompressionAlgorithm.GZIP]; diff --git a/packages/middleware-compression/src/getCompressionPlugin.spec.ts b/packages/middleware-compression/src/getCompressionPlugin.spec.ts new file mode 100644 index 0000000000000..7752d980bdeac --- /dev/null +++ b/packages/middleware-compression/src/getCompressionPlugin.spec.ts @@ -0,0 +1,27 @@ +import { compressionMiddleware, compressionMiddlewareOptions } from "./compressionMiddleware"; +import { getCompressionPlugin } from "./getCompressionPlugin"; + +jest.mock("./compressionMiddleware"); + +describe(getCompressionPlugin.name, () => { + const config = { + bodyLengthChecker: jest.fn(), + disableRequestCompression: false, + requestMinCompressionSizeBytes: 0, + }; + const middlewareConfig = { encodings: [] }; + + it("applyToStack adds compressionMiddleware", () => { + const middlewareReturn = {}; + (compressionMiddleware as jest.Mock).mockReturnValueOnce(middlewareReturn); + + const plugin = getCompressionPlugin(config, middlewareConfig); + const commandStack = { add: jest.fn() }; + + // @ts-ignore + plugin.applyToStack(commandStack); + expect(commandStack.add).toHaveBeenCalledWith(middlewareReturn, compressionMiddlewareOptions); + expect(compressionMiddleware).toHaveBeenCalled(); + expect(compressionMiddleware).toHaveBeenCalledWith(config, middlewareConfig); + }); +}); diff --git a/packages/middleware-compression/src/getCompressionPlugin.ts b/packages/middleware-compression/src/getCompressionPlugin.ts new file mode 100644 index 0000000000000..9ebb3babc00b8 --- /dev/null +++ b/packages/middleware-compression/src/getCompressionPlugin.ts @@ -0,0 +1,20 @@ +import { Pluggable } from "@smithy/types"; + +import { + compressionMiddleware, + CompressionMiddlewareConfig, + compressionMiddlewareOptions, +} from "./compressionMiddleware"; +import { CompressionResolvedConfig } from "./configurations"; + +/** + * @internal + */ +export const getCompressionPlugin = ( + config: CompressionResolvedConfig, + middlewareConfig: CompressionMiddlewareConfig +): Pluggable => ({ + applyToStack: (clientStack) => { + clientStack.add(compressionMiddleware(config, middlewareConfig), compressionMiddlewareOptions); + }, +}); diff --git a/packages/middleware-compression/src/index.ts b/packages/middleware-compression/src/index.ts new file mode 100644 index 0000000000000..237037a980ae3 --- /dev/null +++ b/packages/middleware-compression/src/index.ts @@ -0,0 +1,6 @@ +export * from "./NODE_DISABLE_REQUEST_COMPRESSION_CONFIG_OPTIONS"; +export * from "./NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_CONFIG_OPTIONS"; +export * from "./compressionMiddleware"; +export * from "./configurations"; +export * from "./getCompressionPlugin"; +export * from "./resolveCompressionConfig"; diff --git a/packages/middleware-compression/src/isStreaming.spec.ts b/packages/middleware-compression/src/isStreaming.spec.ts new file mode 100644 index 0000000000000..339774dcacf48 --- /dev/null +++ b/packages/middleware-compression/src/isStreaming.spec.ts @@ -0,0 +1,59 @@ +import { isArrayBuffer } from "@smithy/is-array-buffer"; + +import { isStreaming } from "./isStreaming"; + +jest.mock("@smithy/is-array-buffer"); + +describe(isStreaming.name, () => { + beforeEach(() => { + (isArrayBuffer as unknown as jest.Mock).mockReturnValue(true); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + it("returns true when body is a stream", () => { + (isArrayBuffer as unknown as jest.Mock).mockReturnValue(false); + // Mocking {} as a stream + const mockStream = {}; + expect(isStreaming(mockStream)).toBe(true); + expect(isArrayBuffer).toHaveBeenCalledTimes(1); + expect(isArrayBuffer).toHaveBeenCalledWith(mockStream); + }); + + describe("returns false when body is", () => { + it.each([undefined, "str"])("special case: %s", (val) => { + expect(isStreaming(val)).toBe(false); + expect(isArrayBuffer).not.toHaveBeenCalled(); + }); + + it.each([null, true, 1])("primitive data type: %s", (val) => { + expect(isStreaming(val)).toBe(false); + expect(isArrayBuffer).toHaveBeenCalledTimes(1); + expect(isArrayBuffer).toHaveBeenCalledWith(val); + }); + + const buffer = new ArrayBuffer(4); + const arr = [...Array(4).keys()]; + const addPointOne = (num: number) => num + 0.1; + it.each([ + Buffer.from(buffer), + new DataView(buffer), + new Int8Array(arr), + new Uint8Array(arr), + new Uint8ClampedArray(arr), + new Int16Array(arr), + new Uint16Array(arr), + new Int32Array(arr), + new Uint32Array(arr), + new Float32Array(arr.map(addPointOne)), + new Float64Array(arr.map(addPointOne)), + new BigInt64Array(arr.map(BigInt)), + new BigUint64Array(arr.map(BigInt)), + ])("ArrayBuffer View: %s", (arrayBufferView) => { + expect(isStreaming(arrayBufferView)).toBe(false); + expect(isArrayBuffer).not.toHaveBeenCalled(); + }); + }); +}); diff --git a/packages/middleware-compression/src/isStreaming.ts b/packages/middleware-compression/src/isStreaming.ts new file mode 100644 index 0000000000000..cb407d574ea9d --- /dev/null +++ b/packages/middleware-compression/src/isStreaming.ts @@ -0,0 +1,7 @@ +import { isArrayBuffer } from "@smithy/is-array-buffer"; + +/** + * Returns true if the given value is a streaming response. + */ +export const isStreaming = (body: unknown) => + body !== undefined && typeof body !== "string" && !ArrayBuffer.isView(body) && !isArrayBuffer(body); diff --git a/packages/middleware-compression/src/resolveCompressionConfig.spec.ts b/packages/middleware-compression/src/resolveCompressionConfig.spec.ts new file mode 100644 index 0000000000000..e94a789a2629d --- /dev/null +++ b/packages/middleware-compression/src/resolveCompressionConfig.spec.ts @@ -0,0 +1,44 @@ +import { resolveCompressionConfig } from "./resolveCompressionConfig"; + +describe(resolveCompressionConfig.name, () => { + const mockConfig = { + bodyLengthChecker: jest.fn(), + disableRequestCompression: false, + requestMinCompressionSizeBytes: 0, + }; + it("should throw an error if requestMinCompressionSizeBytes is less than 0", () => { + const requestMinCompressionSizeBytes = -1; + expect(() => { + resolveCompressionConfig({ ...mockConfig, requestMinCompressionSizeBytes }); + }).toThrow( + new RangeError( + "The value for requestMinCompressionSizeBytes must be between 0 and 10485760 inclusive. " + + `The provided value ${requestMinCompressionSizeBytes} is outside this range."` + ) + ); + }); + + it("should throw an error if requestMinCompressionSizeBytes is greater than 10485760", () => { + const requestMinCompressionSizeBytes = 10485761; + expect(() => { + resolveCompressionConfig({ ...mockConfig, requestMinCompressionSizeBytes }); + }).toThrow( + new RangeError( + "The value for requestMinCompressionSizeBytes must be between 0 and 10485760 inclusive. " + + `The provided value ${requestMinCompressionSizeBytes} is outside this range."` + ) + ); + }); + + it.each([0, 10240, 10485760])("returns requestMinCompressionSizeBytes value %s", (requestMinCompressionSizeBytes) => { + const inputConfig = { ...mockConfig, requestMinCompressionSizeBytes }; + const resolvedConfig = resolveCompressionConfig(inputConfig); + expect(inputConfig).toEqual(resolvedConfig); + }); + + it.each([false, true])("returns disableRequestCompression value %s", (disableRequestCompression) => { + const inputConfig = { ...mockConfig, disableRequestCompression }; + const resolvedConfig = resolveCompressionConfig(inputConfig); + expect(inputConfig).toEqual(resolvedConfig); + }); +}); diff --git a/packages/middleware-compression/src/resolveCompressionConfig.ts b/packages/middleware-compression/src/resolveCompressionConfig.ts new file mode 100644 index 0000000000000..94eac993c6846 --- /dev/null +++ b/packages/middleware-compression/src/resolveCompressionConfig.ts @@ -0,0 +1,19 @@ +import { CompressionInputConfig, CompressionResolvedConfig } from "./configurations"; + +/** + * @internal + */ +export const resolveCompressionConfig = (input: T & CompressionInputConfig): T & CompressionResolvedConfig => { + const { requestMinCompressionSizeBytes } = input; + + // The requestMinCompressionSizeBytes should be less than the upper limit for API Gateway + // https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-openapi-minimum-compression-size.html + if (requestMinCompressionSizeBytes < 0 || requestMinCompressionSizeBytes > 10485760) { + throw new RangeError( + "The value for requestMinCompressionSizeBytes must be between 0 and 10485760 inclusive. " + + `The provided value ${requestMinCompressionSizeBytes} is outside this range."` + ); + } + + return input; +}; diff --git a/packages/middleware-compression/tsconfig.cjs.json b/packages/middleware-compression/tsconfig.cjs.json new file mode 100644 index 0000000000000..96198be81644a --- /dev/null +++ b/packages/middleware-compression/tsconfig.cjs.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "baseUrl": ".", + "outDir": "dist-cjs", + "rootDir": "src" + }, + "extends": "../../tsconfig.cjs.json", + "include": ["src/"] +} diff --git a/packages/middleware-compression/tsconfig.es.json b/packages/middleware-compression/tsconfig.es.json new file mode 100644 index 0000000000000..7f162b266e26c --- /dev/null +++ b/packages/middleware-compression/tsconfig.es.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "baseUrl": ".", + "lib": [], + "outDir": "dist-es", + "rootDir": "src" + }, + "extends": "../../tsconfig.es.json", + "include": ["src/"] +} diff --git a/packages/middleware-compression/tsconfig.types.json b/packages/middleware-compression/tsconfig.types.json new file mode 100644 index 0000000000000..6cdf9f52ea065 --- /dev/null +++ b/packages/middleware-compression/tsconfig.types.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "baseUrl": ".", + "declarationDir": "dist-types", + "rootDir": "src" + }, + "extends": "../../tsconfig.types.json", + "include": ["src/"] +} diff --git a/yarn.lock b/yarn.lock index 09b9799d3c1ad..3bb3efe3f7f21 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6882,6 +6882,11 @@ fb-watchman@^2.0.0, fb-watchman@^2.0.1: dependencies: bser "2.1.1" +fflate@0.8.1: + version "0.8.1" + resolved "https://registry.yarnpkg.com/fflate/-/fflate-0.8.1.tgz#1ed92270674d2ad3c73f077cd0acf26486dae6c9" + integrity sha512-/exOvEuc+/iaUm105QIiOt4LpBdMTWsXxqR0HDF35vx3fmaKzw7354gTilCh5rkzEt8WYyG//ku3h3nRmd7CHQ== + figlet@^1.5.0: version "1.6.0" resolved "https://registry.yarnpkg.com/figlet/-/figlet-1.6.0.tgz#812050fa9f01043b4d44ddeb11f20fb268fa4b93"