Skip to content

Commit f01c2a8

Browse files
committed
chore: run prettier on WIP code in middleware-compression
1 parent 2e4885f commit f01c2a8

File tree

7 files changed

+77
-67
lines changed

7 files changed

+77
-67
lines changed
Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
11
{
22
"name": "middleware-compression",
33
"version": "1.0.0",
4-
"description": "Package that implements the compression middleware for request bodies. ",
5-
"main": "index.js",
4+
"description": "Package that implements the compression middleware for request bodies.",
65
"scripts": {
76
"test": "jest"
87
},
9-
"repository": {
10-
"type": "git",
11-
"url": "git+https://github.com/aws/aws-sdk-js-v3.git"
8+
"main": "./dist-cjs/index.js",
9+
"types": "./dist-types/index.d.ts",
10+
"module": "./dist-es/index.js",
11+
"dependencies": {
12+
"@smithy/types": "^2.7.0",
13+
"tslib": "^2.5.0"
1214
},
1315
"keywords": [
1416
"middleware",
1517
"compression",
1618
"gzip"
1719
],
18-
"author": "AWS SDK for JavaScript Team",
19-
"license": "Apache-2.0",
20-
"bugs": {
21-
"url": "https://github.com/aws/aws-sdk-js-v3/issues"
20+
"author": {
21+
"name": "AWS SDK for JavaScript Team",
22+
"url": "https://aws.amazon.com/javascript/"
2223
},
23-
"homepage": "https://github.com/aws/aws-sdk-js-v3#readme"
24+
"license": "Apache-2.0",
25+
"homepage": "https://github.com/aws/aws-sdk-js-v3/tree/main/packages/middleware-compression",
26+
"repository": {
27+
"type": "git",
28+
"url": "https://github.com/aws/aws-sdk-js-v3.git",
29+
"directory": "packages/middleware-compression"
30+
}
2431
}
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
import zlib from 'zlib';
2-
import { promisify } from 'util';
3-
import { CompressionAlgorithm } from './constants';
1+
import { promisify } from "util";
2+
import zlib from "zlib";
43

5-
// zlib.gzip( buffer, options, callback )
4+
import { CompressionAlgorithm } from "./constants";
65

7-
// reject stream if it goes into compress function
6+
// zlib.gzip( buffer, options, callback )
7+
8+
// reject stream if it goes into compress function
89
export const compress = async (body: Uint8Array, algorithm: CompressionAlgorithm): Promise<Uint8Array> => {
9-
// switch-case for algorithms: default -> unsupported
10+
// switch-case for algorithms: default -> unsupported
1011
if (algorithm !== "gzip") {
11-
throw new Error('Unsupported compression algorithm');
12+
throw new Error("Unsupported compression algorithm");
1213
}
1314
try {
1415
const compressedData = zlib.gzipSync(body);
1516
return compressedData as Uint8Array; // should it be returned as buffer?
1617
} catch (err) {
17-
throw new Error('Compression failed: ' + err.message);
18+
throw new Error("Compression failed: " + err.message);
1819
}
1920
};
2021

21-
// type should be --> import type readable from stream and use readable as the type
22+
// type should be --> import type readable from stream and use readable as the type
2223
// export const compressStream = (body: NodeJS.ReadableStream, algorithm: string): NodeJS.ReadableStream => {
23-
Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,59 @@
11
import { HttpRequest } from "@smithy/protocol-http";
2-
import { BuildHandler, BuildHandlerArguments, BuildHandlerOptions, BuildHandlerOutput, BuildMiddleware, HandlerExecutionContext, MetadataBearer, } from "@smithy/types";
3-
import { CompressionResolvedConfig } from "./configuration"
4-
import { isStreaming } from "./utils";
5-
import { CLIENT_SUPPORTED_ALGORITHMS as supportedAlgorithms } from "./types";
2+
import {
3+
BuildHandler,
4+
BuildHandlerArguments,
5+
BuildHandlerOptions,
6+
BuildHandlerOutput,
7+
BuildMiddleware,
8+
HandlerExecutionContext,
9+
MetadataBearer,
10+
} from "@smithy/types";
11+
612
import { compress, compressStream } from "./compress";
13+
import { CompressionResolvedConfig } from "./configuration";
14+
import { CLIENT_SUPPORTED_ALGORITHMS as supportedAlgorithms } from "./types";
15+
import { isStreaming } from "./utils";
716

817
/**
918
* @internal
1019
*/
11-
export const compressionMiddleware = (config: CompressionResolvedConfig):
12-
BuildMiddleware<any, any> => {
13-
return <Output extends MetadataBearer>(next: BuildHandler<any, Output>,
14-
context: HandlerExecutionContext):
15-
BuildHandler<any, Output> =>
16-
async (args: BuildHandlerArguments<any>):
17-
Promise<BuildHandlerOutput<Output>> => {
18-
19-
if (!HttpRequest.isInstance(args.request) || config.disableRequestCompression) {
20-
return next(args);
20+
export const compressionMiddleware = (config: CompressionResolvedConfig): BuildMiddleware<any, any> => {
21+
return <Output extends MetadataBearer>(
22+
next: BuildHandler<any, Output>,
23+
context: HandlerExecutionContext
24+
): BuildHandler<any, Output> =>
25+
async (args: BuildHandlerArguments<any>): Promise<BuildHandlerOutput<Output>> => {
26+
if (!HttpRequest.isInstance(args.request) || config.disableRequestCompression) {
27+
return next(args);
28+
}
29+
30+
const { request } = args;
31+
const { body, headers } = request;
32+
33+
for (const algorithm of supportedAlgorithms) {
34+
// have to check for streaming length trait and @requiredLength trait; probably to be done in codegen part and not check in middleware
35+
if (isStreaming(body) && !isStreamingLengthRequired(body)) {
36+
// if isStreaming results in Transfer-Encoding: Chunked
37+
request.body = compressStream(body, algorithm);
38+
// body.length checks --> check for util body length in smithy pkg
39+
} else if (!isStreaming(body) && body.length >= config.minCompressionSizeInBytes) {
40+
request.body = await compress(body, algorithm);
2141
}
22-
23-
const { request } = args;
24-
const { body, headers } = request;
25-
26-
for (const algorithm of supportedAlgorithms) {
27-
// have to check for streaming length trait and @requiredLength trait; probably to be done in codegen part and not check in middleware
28-
if (isStreaming(body) && !isStreamingLengthRequired(body)) {
29-
// if isStreaming results in Transfer-Encoding: Chunked
30-
request.body = compressStream(body, algorithm);
31-
// body.length checks --> check for util body length in smithy pkg
32-
} else if (!isStreaming(body) && body.length >= config.minCompressionSizeInBytes) {
33-
request.body = await compress(body, algorithm);
34-
}
35-
if (headers["Content-Encoding"]) {
36-
headers["Content-Encoding"] += `,${algorithm}`;
37-
} else {
38-
headers["Content-Encoding"] = algorithm;
39-
}
40-
break;
42+
if (headers["Content-Encoding"]) {
43+
headers["Content-Encoding"] += `,${algorithm}`;
44+
} else {
45+
headers["Content-Encoding"] = algorithm;
4146
}
42-
43-
return next({ ...args, request });
44-
};
47+
break;
48+
}
4549

46-
}
47-
50+
return next({ ...args, request });
51+
};
52+
};
4853

4954
export const compressionMiddlewareOptions: BuildHandlerOptions = {
5055
name: "compressionMiddleware",
5156
step: "build",
5257
tags: ["REQUEST_BODY_COMPRESSION", "GZIP"],
5358
override: true,
54-
};
59+
};

packages/middleware-compression/src/configuration.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/**
32
* @public
43
*
@@ -19,13 +18,12 @@ export interface CompressionResolvedConfig {
1918
minCompressionSizeInBytes: number;
2019
}
2120

22-
2321
export const resolveCompressionConfig = <T>(input: T & CompressionInputConfig): T & CompressionResolvedConfig => {
2422
const minCompressionSizeInBytes = input.minCompressionSizeInBytes ?? 10240;
2523
const maxCompressionSizeInBytes = 10485760;
2624
// minCompressionSizeInBytes explanation
2725
if (minCompressionSizeInBytes < 0 || minCompressionSizeInBytes > maxCompressionSizeInBytes) {
28-
throw new Error('minCompressionSizeInBytes must be between 0 and 10485760 bytes inclusive');
26+
throw new Error("minCompressionSizeInBytes must be between 0 and 10485760 bytes inclusive");
2927
}
3028
return {
3129
...input,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/**
22
* Compression algorithms supported by the SDK.
33
*/
4-
export type CompressionAlgorithm = "gzip"
4+
export type CompressionAlgorithm = "gzip";
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from "./compressionMiddleware";
1+
export * from "./compressionMiddleware";
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { CompressionAlgorithm } from "./constants";
22

33
export const CLIENT_SUPPORTED_ALGORITHMS: CompressionAlgorithm[] = [
4-
'gzip',
4+
"gzip",
55
// add more supported compression algorithms here
66
];
77

88
export const PRIORITY_ORDER_ALGORITHMS: CompressionAlgorithm[] = [
9-
'gzip',
9+
"gzip",
1010
// add more supported compression algorithms here in the order of their priority
11-
];
11+
];

0 commit comments

Comments
 (0)