@middy/http-content-encoding 7.0.3 → 7.1.1

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.
package/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  <div align="center">
2
- <h1>Middy http-content-encoding middleware</h1>
2
+ <h1>Middy `http-content-encoding` middleware</h1>
3
3
  <img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.svg"/>
4
4
  <p><strong>HTTP content encoding middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda</strong></p>
5
5
  <p>
package/index.d.ts CHANGED
@@ -2,7 +2,9 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
  import type middy from "@middy/core";
4
4
 
5
- interface Options {
5
+ export type ContentEncoding = "br" | "deflate" | "gzip" | "zstd";
6
+
7
+ export interface Options {
6
8
  br?: any;
7
9
  gzip?: any;
8
10
  deflate?: any;
@@ -10,6 +12,10 @@ interface Options {
10
12
  overridePreferredEncoding?: string[];
11
13
  }
12
14
 
15
+ export declare function getContentEncodingStream(
16
+ preferredEncoding: ContentEncoding,
17
+ ): ReturnType<typeof import("node:zlib")["createGzip"]>;
18
+
13
19
  declare function httpContentEncoding(options?: Options): middy.MiddlewareObj;
14
20
 
15
21
  export default httpContentEncoding;
package/index.js CHANGED
@@ -1,14 +1,14 @@
1
1
  // Copyright 2017 - 2026 will Farrell, Luciano Mammino, and Middy contributors.
2
2
  // SPDX-License-Identifier: MIT
3
- import { Readable } from "node:stream";
4
3
 
4
+ import { Readable } from "node:stream";
5
+ import { ReadableStream } from "node:stream/web";
5
6
  import {
6
7
  createBrotliCompress as brotliCompressStream,
7
8
  createDeflate as deflateCompressStream,
8
9
  createGzip as gzipCompressStream,
9
10
  createZstdCompress as zstdCompressStream,
10
11
  } from "node:zlib";
11
-
12
12
  import { normalizeHttpResponse } from "@middy/util";
13
13
 
14
14
  const contentEncodingStreams = {
@@ -51,6 +51,8 @@ const httpContentEncodingMiddleware = (opts) => {
51
51
  }
52
52
  const responseCacheControl =
53
53
  response.headers["Cache-Control"] ?? response.headers["cache-control"];
54
+ const isNodeStream = response.body?._readableState;
55
+ const isWebStream = response.body instanceof ReadableStream;
54
56
  if (
55
57
  response.isBase64Encoded ||
56
58
  !preferredEncoding ||
@@ -58,7 +60,8 @@ const httpContentEncodingMiddleware = (opts) => {
58
60
  !response.body ||
59
61
  (typeof response.body !== "string" &&
60
62
  !Buffer.isBuffer(response.body) &&
61
- !response.body?._readableState) ||
63
+ !isNodeStream &&
64
+ !isWebStream) ||
62
65
  responseCacheControl?.includes("no-transform")
63
66
  ) {
64
67
  return;
@@ -78,13 +81,21 @@ const httpContentEncodingMiddleware = (opts) => {
78
81
  }
79
82
 
80
83
  // Support streamifyResponse
81
- if (response.body?._readableState) {
84
+ if (isNodeStream || isWebStream) {
82
85
  request.response.headers["Content-Encoding"] = contentEncoding;
83
- request.response.body = request.response.body.pipe(contentEncodingStream);
86
+ if (isNodeStream) {
87
+ request.response.body = request.response.body.pipe(
88
+ contentEncodingStream,
89
+ );
90
+ } else if (isWebStream) {
91
+ request.response.body = Readable.toWeb(
92
+ Readable.fromWeb(response.body).pipe(contentEncodingStream),
93
+ );
94
+ }
84
95
  addHeaderPart(response, "Vary", "Accept-Encoding");
85
96
  return;
86
97
  }
87
-
98
+ // isString
88
99
  const stream = Readable.from(response.body).pipe(contentEncodingStream);
89
100
 
90
101
  const chunks = [];
@@ -116,7 +127,7 @@ const httpContentEncodingMiddleware = (opts) => {
116
127
  };
117
128
  };
118
129
 
119
- // header in offical name, lowercase varient handeled
130
+ // header in official name, lowercase variant handled
120
131
  const addHeaderPart = (response, header, value) => {
121
132
  const headerLower = header.toLowerCase();
122
133
  const sanitizedHeader = response.headers[headerLower] ? headerLower : header;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@middy/http-content-encoding",
3
- "version": "7.0.3",
4
- "description": "Http content encoding middleware for the middy framework",
3
+ "version": "7.1.1",
4
+ "description": "HTTP content encoding middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
7
7
  "node": ">=22"
@@ -68,11 +68,13 @@
68
68
  "url": "https://github.com/sponsors/willfarrell"
69
69
  },
70
70
  "dependencies": {
71
- "@middy/util": "7.0.3"
71
+ "@middy/util": "7.1.1"
72
72
  },
73
73
  "devDependencies": {
74
74
  "@datastream/core": "0.0.42",
75
- "@middy/core": "7.0.3"
75
+ "@middy/core": "7.1.1",
76
+ "@types/aws-lambda": "^8.0.0",
77
+ "@types/node": "^22.0.0"
76
78
  },
77
79
  "gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"
78
80
  }