@middy/input-output-logger 6.1.6 → 6.2.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/index.d.ts +7 -7
- package/index.js +141 -141
- package/package.json +67 -70
package/index.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import middy from
|
|
1
|
+
import type middy from "@middy/core";
|
|
2
2
|
|
|
3
3
|
interface Options {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
logger?: (message: any) => undefined;
|
|
5
|
+
awsContext?: boolean;
|
|
6
|
+
omitPaths?: string[];
|
|
7
|
+
mask?: string;
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
declare function inputOutputLogger
|
|
10
|
+
declare function inputOutputLogger(options?: Options): middy.MiddlewareObj;
|
|
11
11
|
|
|
12
|
-
export default inputOutputLogger
|
|
12
|
+
export default inputOutputLogger;
|
package/index.js
CHANGED
|
@@ -1,165 +1,165 @@
|
|
|
1
|
-
import { Transform } from
|
|
1
|
+
import { Transform } from "node:stream";
|
|
2
2
|
|
|
3
3
|
const defaults = {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
4
|
+
logger: (message) => {
|
|
5
|
+
console.log(JSON.stringify(message));
|
|
6
|
+
},
|
|
7
|
+
awsContext: false,
|
|
8
|
+
omitPaths: [],
|
|
9
|
+
mask: undefined,
|
|
10
|
+
};
|
|
11
11
|
|
|
12
12
|
const inputOutputLoggerMiddleware = (opts = {}) => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
const { logger, awsContext, omitPaths, mask } = {
|
|
14
|
+
...defaults,
|
|
15
|
+
...opts,
|
|
16
|
+
};
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
18
|
+
if (typeof logger !== "function") {
|
|
19
|
+
throw new Error("logger must be a function", {
|
|
20
|
+
cause: {
|
|
21
|
+
package: "@middy/input-output-logger",
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
26
|
+
const omitPathTree = buildPathTree(omitPaths);
|
|
27
|
+
// needs `omitPathTree`, `logger`
|
|
28
|
+
const omitAndLog = (param, request) => {
|
|
29
|
+
const message = { [param]: request[param] };
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
if (awsContext) {
|
|
32
|
+
message.context = pick(request.context, awsContextKeys);
|
|
33
|
+
}
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
35
|
+
let cloneMessage = message;
|
|
36
|
+
if (omitPaths.length) {
|
|
37
|
+
cloneMessage = structuredClone(message); // Full clone to prevent nested mutations
|
|
38
|
+
omit(cloneMessage, { [param]: omitPathTree[param] });
|
|
39
|
+
}
|
|
40
|
+
logger(cloneMessage);
|
|
41
|
+
};
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
43
|
+
// needs `mask`
|
|
44
|
+
const omit = (obj, pathTree = {}) => {
|
|
45
|
+
if (Array.isArray(obj) && pathTree["[]"]) {
|
|
46
|
+
for (let i = 0, l = obj.length; i < l; i++) {
|
|
47
|
+
omit(obj[i], pathTree["[]"]);
|
|
48
|
+
}
|
|
49
|
+
} else if (isObject(obj)) {
|
|
50
|
+
for (const key in pathTree) {
|
|
51
|
+
if (pathTree[key] === true) {
|
|
52
|
+
if (mask) {
|
|
53
|
+
obj[key] = mask;
|
|
54
|
+
} else {
|
|
55
|
+
delete obj[key];
|
|
56
|
+
}
|
|
57
|
+
} else {
|
|
58
|
+
omit(obj[key], pathTree[key]);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
63
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
64
|
+
const inputOutputLoggerMiddlewareBefore = async (request) => {
|
|
65
|
+
omitAndLog("event", request);
|
|
66
|
+
};
|
|
67
|
+
const inputOutputLoggerMiddlewareAfter = async (request) => {
|
|
68
|
+
if (
|
|
69
|
+
request.response?._readableState ??
|
|
70
|
+
request.response?.body?._readableState
|
|
71
|
+
) {
|
|
72
|
+
passThrough(request, omitAndLog);
|
|
73
|
+
} else {
|
|
74
|
+
omitAndLog("response", request);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
const inputOutputLoggerMiddlewareOnError = async (request) => {
|
|
78
|
+
if (request.response === undefined) return;
|
|
79
|
+
await inputOutputLoggerMiddlewareAfter(request);
|
|
80
|
+
};
|
|
81
81
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
82
|
+
return {
|
|
83
|
+
before: inputOutputLoggerMiddlewareBefore,
|
|
84
|
+
after: inputOutputLoggerMiddlewareAfter,
|
|
85
|
+
onError: inputOutputLoggerMiddlewareOnError,
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
88
|
|
|
89
89
|
// https://docs.aws.amazon.com/lambda/latest/dg/nodejs-context.html
|
|
90
90
|
const awsContextKeys = [
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
]
|
|
91
|
+
"functionName",
|
|
92
|
+
"functionVersion",
|
|
93
|
+
"invokedFunctionArn",
|
|
94
|
+
"memoryLimitInMB",
|
|
95
|
+
"awsRequestId",
|
|
96
|
+
"logGroupName",
|
|
97
|
+
"logStreamName",
|
|
98
|
+
"identity",
|
|
99
|
+
"clientContext",
|
|
100
|
+
"callbackWaitsForEmptyEventLoop",
|
|
101
|
+
];
|
|
102
102
|
|
|
103
103
|
// move to util, if ever used elsewhere
|
|
104
104
|
const pick = (originalObject = {}, keysToPick = []) => {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
}
|
|
105
|
+
const newObject = {};
|
|
106
|
+
for (const path of keysToPick) {
|
|
107
|
+
// only supports first level
|
|
108
|
+
if (originalObject[path] !== undefined) {
|
|
109
|
+
newObject[path] = originalObject[path];
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return newObject;
|
|
113
|
+
};
|
|
114
114
|
|
|
115
115
|
const isObject = (value) =>
|
|
116
|
-
|
|
116
|
+
value && typeof value === "object" && value.constructor === Object;
|
|
117
117
|
|
|
118
118
|
const buildPathTree = (paths) => {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
}
|
|
119
|
+
const tree = {};
|
|
120
|
+
for (let path of paths.sort().reverse()) {
|
|
121
|
+
// reverse to ensure conflicting paths don't cause issues
|
|
122
|
+
if (!Array.isArray(path)) path = path.split(".");
|
|
123
|
+
if (path.includes("__proto__")) continue;
|
|
124
|
+
path
|
|
125
|
+
.slice(0) // clone
|
|
126
|
+
.reduce((a, b, idx) => {
|
|
127
|
+
if (idx < path.length - 1) {
|
|
128
|
+
a[b] ??= {};
|
|
129
|
+
return a[b];
|
|
130
|
+
}
|
|
131
|
+
a[b] = true;
|
|
132
|
+
return true;
|
|
133
|
+
}, tree);
|
|
134
|
+
}
|
|
135
|
+
return tree;
|
|
136
|
+
};
|
|
137
137
|
|
|
138
138
|
const passThrough = (request, omitAndLog) => {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
}
|
|
139
|
+
// required because `core` remove body before `flush` is triggered
|
|
140
|
+
const hasBody = request.response?.body;
|
|
141
|
+
let body = "";
|
|
142
|
+
const listen = new Transform({
|
|
143
|
+
objectMode: false,
|
|
144
|
+
transform(chunk, encoding, callback) {
|
|
145
|
+
body += chunk;
|
|
146
|
+
this.push(chunk, encoding);
|
|
147
|
+
callback();
|
|
148
|
+
},
|
|
149
|
+
flush(callback) {
|
|
150
|
+
if (hasBody) {
|
|
151
|
+
omitAndLog("response", { response: { ...request.response, body } });
|
|
152
|
+
} else {
|
|
153
|
+
omitAndLog("response", { response: body });
|
|
154
|
+
}
|
|
155
|
+
callback();
|
|
156
|
+
},
|
|
157
|
+
});
|
|
158
|
+
if (hasBody) {
|
|
159
|
+
request.response.body = request.response.body.pipe(listen);
|
|
160
|
+
} else {
|
|
161
|
+
request.response = request.response.pipe(listen);
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
164
|
|
|
165
|
-
export default inputOutputLoggerMiddleware
|
|
165
|
+
export default inputOutputLoggerMiddleware;
|
package/package.json
CHANGED
|
@@ -1,72 +1,69 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
"@types/node": "^20.0.0"
|
|
70
|
-
},
|
|
71
|
-
"gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"
|
|
2
|
+
"name": "@middy/input-output-logger",
|
|
3
|
+
"version": "6.2.1",
|
|
4
|
+
"description": "Input and output logger middleware for the middy framework",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=20"
|
|
8
|
+
},
|
|
9
|
+
"engineStrict": true,
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"module": "./index.js",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"import": {
|
|
17
|
+
"types": "./index.d.ts",
|
|
18
|
+
"default": "./index.js"
|
|
19
|
+
},
|
|
20
|
+
"require": {
|
|
21
|
+
"default": "./index.js"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"types": "index.d.ts",
|
|
26
|
+
"files": ["index.js", "index.d.ts"],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"test": "npm run test:unit && npm run test:fuzz",
|
|
29
|
+
"test:unit": "node --test",
|
|
30
|
+
"test:fuzz": "node --test index.fuzz.js",
|
|
31
|
+
"test:perf": "node --test index.perf.js"
|
|
32
|
+
},
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"keywords": [
|
|
35
|
+
"Lambda",
|
|
36
|
+
"Middleware",
|
|
37
|
+
"Serverless",
|
|
38
|
+
"Framework",
|
|
39
|
+
"AWS",
|
|
40
|
+
"AWS Lambda",
|
|
41
|
+
"Middy",
|
|
42
|
+
"Input",
|
|
43
|
+
"Output",
|
|
44
|
+
"Logger"
|
|
45
|
+
],
|
|
46
|
+
"author": {
|
|
47
|
+
"name": "Middy contributors",
|
|
48
|
+
"url": "https://github.com/middyjs/middy/graphs/contributors"
|
|
49
|
+
},
|
|
50
|
+
"repository": {
|
|
51
|
+
"type": "git",
|
|
52
|
+
"url": "git+https://github.com/middyjs/middy.git",
|
|
53
|
+
"directory": "packages/input-output-logger"
|
|
54
|
+
},
|
|
55
|
+
"bugs": {
|
|
56
|
+
"url": "https://github.com/middyjs/middy/issues"
|
|
57
|
+
},
|
|
58
|
+
"homepage": "https://middy.js.org",
|
|
59
|
+
"funding": {
|
|
60
|
+
"type": "github",
|
|
61
|
+
"url": "https://github.com/sponsors/willfarrell"
|
|
62
|
+
},
|
|
63
|
+
"devDependencies": {
|
|
64
|
+
"@datastream/core": "0.0.40",
|
|
65
|
+
"@middy/core": "6.2.1",
|
|
66
|
+
"@types/node": "^20.0.0"
|
|
67
|
+
},
|
|
68
|
+
"gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"
|
|
72
69
|
}
|