@middy/http-content-encoding 7.1.1 → 7.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +17 -0
  2. package/index.d.ts +17 -6
  3. package/index.js +23 -17
  4. package/package.json +4 -5
package/README.md CHANGED
@@ -30,6 +30,23 @@
30
30
  <p>You can read the documentation at: <a href="https://middy.js.org/docs/middlewares/http-content-encoding">https://middy.js.org/docs/middlewares/http-content-encoding</a></p>
31
31
  </div>
32
32
 
33
+ ## Install
34
+
35
+ ```bash
36
+ npm install --save @middy/http-content-encoding
37
+ ```
38
+
39
+
40
+ ## Documentation and examples
41
+
42
+ For documentation and examples, refer to the main [Middy monorepo on GitHub](https://github.com/middyjs/middy) or [Middy official website](https://middy.js.org/docs/middlewares/http-content-encoding).
43
+
44
+
45
+ ## Contributing
46
+
47
+ Everyone is very welcome to contribute to this repository. Feel free to [raise issues](https://github.com/middyjs/middy/issues) or to [submit Pull Requests](https://github.com/middyjs/middy/pulls).
48
+
49
+
33
50
  ## License
34
51
 
35
52
  Licensed under [MIT License](LICENSE). Copyright (c) 2017-2026 [will Farrell](https://github.com/willfarrell), [Luciano Mammino](https://github.com/lmammino), and [Middy contributors](https://github.com/middyjs/middy/graphs/contributors).
package/index.d.ts CHANGED
@@ -1,21 +1,32 @@
1
1
  // Copyright 2017 - 2026 will Farrell, Luciano Mammino, and Middy contributors.
2
2
  // SPDX-License-Identifier: MIT
3
3
  import type middy from "@middy/core";
4
+ import type {
5
+ BrotliCompress,
6
+ BrotliOptions,
7
+ Deflate,
8
+ Gzip,
9
+ ZlibOptions,
10
+ ZstdCompress,
11
+ ZstdOptions,
12
+ } from "node:zlib";
4
13
 
5
14
  export type ContentEncoding = "br" | "deflate" | "gzip" | "zstd";
6
15
 
7
16
  export interface Options {
8
- br?: any;
9
- gzip?: any;
10
- deflate?: any;
11
- zstd?: any;
17
+ br?: BrotliOptions;
18
+ gzip?: ZlibOptions;
19
+ deflate?: ZlibOptions;
20
+ zstd?: ZstdOptions;
12
21
  overridePreferredEncoding?: string[];
13
22
  }
14
23
 
15
24
  export declare function getContentEncodingStream(
16
25
  preferredEncoding: ContentEncoding,
17
- ): ReturnType<typeof import("node:zlib")["createGzip"]>;
26
+ ): BrotliCompress | Deflate | Gzip | ZstdCompress;
18
27
 
19
- declare function httpContentEncoding(options?: Options): middy.MiddlewareObj;
28
+ declare function httpContentEncoding(
29
+ options?: Options,
30
+ ): middy.MiddlewareObj<unknown, unknown, Error>;
20
31
 
21
32
  export default httpContentEncoding;
package/index.js CHANGED
@@ -5,9 +5,13 @@ import { Readable } from "node:stream";
5
5
  import { ReadableStream } from "node:stream/web";
6
6
  import {
7
7
  createBrotliCompress as brotliCompressStream,
8
+ brotliCompressSync,
8
9
  createDeflate as deflateCompressStream,
10
+ deflateSync,
9
11
  createGzip as gzipCompressStream,
12
+ gzipSync,
10
13
  createZstdCompress as zstdCompressStream,
14
+ zstdCompressSync,
11
15
  } from "node:zlib";
12
16
  import { normalizeHttpResponse } from "@middy/util";
13
17
 
@@ -18,6 +22,13 @@ const contentEncodingStreams = {
18
22
  zstd: zstdCompressStream,
19
23
  };
20
24
 
25
+ const contentEncodingSync = {
26
+ br: brotliCompressSync,
27
+ deflate: deflateSync,
28
+ gzip: gzipSync,
29
+ zstd: zstdCompressSync,
30
+ };
31
+
21
32
  const defaults = {
22
33
  br: undefined,
23
34
  deflate: undefined,
@@ -30,7 +41,7 @@ export const getContentEncodingStream = (preferredEncoding) => {
30
41
  return contentEncodingStreams[preferredEncoding]();
31
42
  };
32
43
 
33
- const httpContentEncodingMiddleware = (opts) => {
44
+ const httpContentEncodingMiddleware = (opts = {}) => {
34
45
  const options = { ...defaults, ...opts };
35
46
 
36
47
  const supportedContentEncodings = Object.keys(contentEncodingStreams);
@@ -42,7 +53,7 @@ const httpContentEncodingMiddleware = (opts) => {
42
53
  response,
43
54
  } = request;
44
55
 
45
- // Encoding not supported, already encoded, or doesn't need to'
56
+ // Encoding not supported, already encoded, or doesn't need to
46
57
  const eventCacheControl =
47
58
  request.event?.headers?.["cache-control"] ??
48
59
  request.event?.headers?.["Cache-Control"];
@@ -67,21 +78,19 @@ const httpContentEncodingMiddleware = (opts) => {
67
78
  return;
68
79
  }
69
80
 
70
- let contentEncodingStream = contentEncodingStreams[preferredEncoding](
71
- options[preferredEncoding],
72
- );
81
+ // Resolve encoding choice before creating any stream
73
82
  let contentEncoding = preferredEncoding;
74
83
  for (const encoding of options.overridePreferredEncoding) {
75
84
  if (!preferredEncodings.includes(encoding)) continue;
76
- contentEncodingStream = contentEncodingStreams[encoding](
77
- options[encoding],
78
- );
79
85
  contentEncoding = encoding;
80
86
  break;
81
87
  }
82
88
 
83
89
  // Support streamifyResponse
84
90
  if (isNodeStream || isWebStream) {
91
+ const contentEncodingStream = contentEncodingStreams[contentEncoding](
92
+ options[contentEncoding],
93
+ );
85
94
  request.response.headers["Content-Encoding"] = contentEncoding;
86
95
  if (isNodeStream) {
87
96
  request.response.body = request.response.body.pipe(
@@ -95,15 +104,12 @@ const httpContentEncodingMiddleware = (opts) => {
95
104
  addHeaderPart(response, "Vary", "Accept-Encoding");
96
105
  return;
97
106
  }
98
- // isString
99
- const stream = Readable.from(response.body).pipe(contentEncodingStream);
100
-
101
- const chunks = [];
102
- for await (const chunk of stream) {
103
- chunks.push(chunk);
104
- }
105
-
106
- const body = Buffer.concat(chunks).toString("base64");
107
+ // isString/isBuffer — use sync compression (avoids stream overhead)
108
+ const inputBuffer = Buffer.isBuffer(response.body)
109
+ ? response.body
110
+ : Buffer.from(response.body);
111
+ const compressed = contentEncodingSync[contentEncoding](inputBuffer);
112
+ const body = compressed.toString("base64");
107
113
 
108
114
  // Only apply encoding if it's smaller
109
115
  if (body.length < response.body.length) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/http-content-encoding",
3
- "version": "7.1.1",
3
+ "version": "7.1.3",
4
4
  "description": "HTTP content encoding middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -68,13 +68,12 @@
68
68
  "url": "https://github.com/sponsors/willfarrell"
69
69
  },
70
70
  "dependencies": {
71
- "@middy/util": "7.1.1"
71
+ "@middy/util": "7.1.3"
72
72
  },
73
73
  "devDependencies": {
74
74
  "@datastream/core": "0.0.42",
75
- "@middy/core": "7.1.1",
75
+ "@middy/core": "7.1.3",
76
76
  "@types/aws-lambda": "^8.0.0",
77
77
  "@types/node": "^22.0.0"
78
- },
79
- "gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"
78
+ }
80
79
  }