@middy/input-output-logger 6.2.4 → 6.3.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 +44 -0
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Transform } from "node:stream";
2
+ import { TransformStream } from "node:stream/web";
2
3
 
3
4
  const defaults = {
4
5
  logger: (message) => {
@@ -65,11 +66,19 @@ const inputOutputLoggerMiddleware = (opts = {}) => {
65
66
  omitAndLog("event", request);
66
67
  };
67
68
  const inputOutputLoggerMiddlewareAfter = async (request) => {
69
+ // Check for Node.js stream
68
70
  if (
69
71
  request.response?._readableState ??
70
72
  request.response?.body?._readableState
71
73
  ) {
72
74
  passThrough(request, omitAndLog);
75
+ }
76
+ // Check for Web stream
77
+ else if (
78
+ request.response instanceof ReadableStream ||
79
+ request.response?.body instanceof ReadableStream
80
+ ) {
81
+ passThroughWebStream(request, omitAndLog);
73
82
  } else {
74
83
  omitAndLog("response", request);
75
84
  }
@@ -162,4 +171,39 @@ const passThrough = (request, omitAndLog) => {
162
171
  }
163
172
  };
164
173
 
174
+ // Handler for Web Streams API
175
+ const passThroughWebStream = (request, omitAndLog) => {
176
+ const hasBody = request.response?.body;
177
+ let body = "";
178
+
179
+ const transformer = new TransformStream({
180
+ transform(chunk, controller) {
181
+ // For web streams, chunks could be various types
182
+ const textChunk =
183
+ typeof chunk === "string"
184
+ ? chunk
185
+ : chunk instanceof Uint8Array
186
+ ? new TextDecoder().decode(chunk)
187
+ : String(chunk);
188
+ body += textChunk;
189
+ controller.enqueue(chunk);
190
+ },
191
+ flush(controller) {
192
+ if (hasBody) {
193
+ omitAndLog("response", { response: { ...request.response, body } });
194
+ } else {
195
+ omitAndLog("response", { response: body });
196
+ }
197
+ },
198
+ });
199
+
200
+ if (hasBody) {
201
+ // Handle response with body property that's a ReadableStream
202
+ request.response.body = request.response.body.pipeThrough(transformer);
203
+ } else {
204
+ // Handle response that's directly a ReadableStream
205
+ request.response = request.response.pipeThrough(transformer);
206
+ }
207
+ };
208
+
165
209
  export default inputOutputLoggerMiddleware;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/input-output-logger",
3
- "version": "6.2.4",
3
+ "version": "6.3.0",
4
4
  "description": "Input and output logger middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -62,7 +62,7 @@
62
62
  },
63
63
  "devDependencies": {
64
64
  "@datastream/core": "0.0.40",
65
- "@middy/core": "6.2.4",
65
+ "@middy/core": "6.3.0",
66
66
  "@types/node": "^20.0.0"
67
67
  },
68
68
  "gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"