@middy/http-content-encoding 7.0.3 → 7.1.0

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 (2) hide show
  1. package/index.js +16 -4
  2. package/package.json +3 -3
package/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  // Copyright 2017 - 2026 will Farrell, Luciano Mammino, and Middy contributors.
2
2
  // SPDX-License-Identifier: MIT
3
3
  import { Readable } from "node:stream";
4
+ import { ReadableStream } from "node:stream/web";
4
5
 
5
6
  import {
6
7
  createBrotliCompress as brotliCompressStream,
@@ -51,6 +52,8 @@ const httpContentEncodingMiddleware = (opts) => {
51
52
  }
52
53
  const responseCacheControl =
53
54
  response.headers["Cache-Control"] ?? response.headers["cache-control"];
55
+ const isNodeStream = response.body?._readableState;
56
+ const isWebStream = response.body instanceof ReadableStream;
54
57
  if (
55
58
  response.isBase64Encoded ||
56
59
  !preferredEncoding ||
@@ -58,7 +61,8 @@ const httpContentEncodingMiddleware = (opts) => {
58
61
  !response.body ||
59
62
  (typeof response.body !== "string" &&
60
63
  !Buffer.isBuffer(response.body) &&
61
- !response.body?._readableState) ||
64
+ !isNodeStream &&
65
+ !isWebStream) ||
62
66
  responseCacheControl?.includes("no-transform")
63
67
  ) {
64
68
  return;
@@ -78,13 +82,21 @@ const httpContentEncodingMiddleware = (opts) => {
78
82
  }
79
83
 
80
84
  // Support streamifyResponse
81
- if (response.body?._readableState) {
85
+ if (isNodeStream || isWebStream) {
82
86
  request.response.headers["Content-Encoding"] = contentEncoding;
83
- request.response.body = request.response.body.pipe(contentEncodingStream);
87
+ if (isNodeStream) {
88
+ request.response.body = request.response.body.pipe(
89
+ contentEncodingStream,
90
+ );
91
+ } else if (isWebStream) {
92
+ request.response.body = Readable.toWeb(
93
+ Readable.fromWeb(response.body).pipe(contentEncodingStream),
94
+ );
95
+ }
84
96
  addHeaderPart(response, "Vary", "Accept-Encoding");
85
97
  return;
86
98
  }
87
-
99
+ // isString
88
100
  const stream = Readable.from(response.body).pipe(contentEncodingStream);
89
101
 
90
102
  const chunks = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/http-content-encoding",
3
- "version": "7.0.3",
3
+ "version": "7.1.0",
4
4
  "description": "Http content encoding middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -68,11 +68,11 @@
68
68
  "url": "https://github.com/sponsors/willfarrell"
69
69
  },
70
70
  "dependencies": {
71
- "@middy/util": "7.0.3"
71
+ "@middy/util": "7.1.0"
72
72
  },
73
73
  "devDependencies": {
74
74
  "@datastream/core": "0.0.42",
75
- "@middy/core": "7.0.3"
75
+ "@middy/core": "7.1.0"
76
76
  },
77
77
  "gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"
78
78
  }