@middy/input-output-logger 3.0.0-alpha.6 → 3.0.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.
- package/README.md +25 -14
- package/index.cjs +3 -0
- package/index.js +2 -104
- package/package.json +13 -6
package/README.md
CHANGED
|
@@ -1,26 +1,36 @@
|
|
|
1
|
-
# Middy input-output-logger middleware
|
|
2
|
-
|
|
3
|
-
<div align="center">
|
|
4
|
-
<img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.png"/>
|
|
5
|
-
</div>
|
|
6
|
-
|
|
7
1
|
<div align="center">
|
|
2
|
+
<h1>Middy input-output-logger middleware</h1>
|
|
3
|
+
<img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.svg"/>
|
|
8
4
|
<p><strong>Input output logger middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda</strong></p>
|
|
9
|
-
</div>
|
|
10
|
-
|
|
11
|
-
<div align="center">
|
|
12
5
|
<p>
|
|
13
|
-
<a href="
|
|
6
|
+
<a href="https://www.npmjs.com/package/@middy/input-output-logger?activeTab=versions">
|
|
14
7
|
<img src="https://badge.fury.io/js/%40middy%2Finput-output-logger.svg" alt="npm version" style="max-width:100%;">
|
|
15
8
|
</a>
|
|
9
|
+
<a href="https://packagephobia.com/result?p=@middy/input-output-logger">
|
|
10
|
+
<img src="https://packagephobia.com/badge?p=@middy/input-output-logger" alt="npm install size" style="max-width:100%;">
|
|
11
|
+
</a>
|
|
12
|
+
<a href="https://github.com/middyjs/middy/actions">
|
|
13
|
+
<img src="https://github.com/middyjs/middy/workflows/Tests/badge.svg" alt="GitHub Actions test status badge" style="max-width:100%;">
|
|
14
|
+
</a>
|
|
15
|
+
<br/>
|
|
16
|
+
<a href="https://standardjs.com/">
|
|
17
|
+
<img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
|
|
18
|
+
</a>
|
|
16
19
|
<a href="https://snyk.io/test/github/middyjs/middy">
|
|
17
20
|
<img src="https://snyk.io/test/github/middyjs/middy/badge.svg" alt="Known Vulnerabilities" data-canonical-src="https://snyk.io/test/github/middyjs/middy" style="max-width:100%;">
|
|
18
21
|
</a>
|
|
19
|
-
<a href="https://
|
|
20
|
-
<img src="https://img.shields.io/
|
|
22
|
+
<a href="https://lgtm.com/projects/g/middyjs/middy/context:javascript">
|
|
23
|
+
<img src="https://img.shields.io/lgtm/grade/javascript/g/middyjs/middy.svg?logo=lgtm&logoWidth=18" alt="Language grade: JavaScript" style="max-width:100%;">
|
|
24
|
+
</a>
|
|
25
|
+
<a href="https://bestpractices.coreinfrastructure.org/projects/5280">
|
|
26
|
+
<img src="https://bestpractices.coreinfrastructure.org/projects/5280/badge" alt="Core Infrastructure Initiative (CII) Best Practices" style="max-width:100%;">
|
|
21
27
|
</a>
|
|
28
|
+
<br/>
|
|
22
29
|
<a href="https://gitter.im/middyjs/Lobby">
|
|
23
|
-
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter"
|
|
30
|
+
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
|
|
31
|
+
</a>
|
|
32
|
+
<a href="https://stackoverflow.com/questions/tagged/middy?sort=Newest&uqlId=35052">
|
|
33
|
+
<img src="https://img.shields.io/badge/StackOverflow-[middy]-yellow" alt="Ask questions on StackOverflow" style="max-width:100%;">
|
|
24
34
|
</a>
|
|
25
35
|
</p>
|
|
26
36
|
</div>
|
|
@@ -44,7 +54,8 @@ npm install --save @middy/input-output-logger
|
|
|
44
54
|
- `logger` (function) (default `console.log`): logging function that accepts an object
|
|
45
55
|
- `awsContext` (boolean) (default `false`): Include [AWS Lambda context object](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-context.html) to the logger
|
|
46
56
|
- `omitPaths` (string[]) (default `[]`): property accepts an array of paths that will be used to remove particular fields import the logged objects. This could serve as a simple way to redact sensitive data from logs (default []).
|
|
47
|
-
- `
|
|
57
|
+
- `mask` (string) (default `undefined`): When set to a truthy value (i.e. `*** redacted ***`) a path is replaced instead of ommitted.
|
|
58
|
+
- `replacer` (function) (default `undefined`): A [replacer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Parameters) parameter may be passed which will be used `JSON.stringify`ing the request during cloning.
|
|
48
59
|
|
|
49
60
|
## Sample usage
|
|
50
61
|
|
package/index.cjs
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.default=void 0;var _util=require("@middy/util");var _a,_b;const defaults={logger:console.log,awsContext:false,omitPaths:[],mask:undefined,replacer:undefined};const inputOutputLoggerMiddleware=(opts={})=>{const{logger,awsContext,omitPaths,mask,replacer}={...defaults,...opts};if(typeof logger!=="function"){throw new Error("[input-output-logger-middleware]: logger must be a function")}const omitPathTree=buildPathOmitTree(omitPaths);const omitAndLog=(param,request)=>{const message={[param]:request[param]};if(awsContext){message.context=pick(request.context,awsContextKeys)}const cloneMessage=(0,_util).jsonSafeParse((0,_util).jsonSafeStringify(message,replacer));omit(cloneMessage,{[param]:omitPathTree[param]});logger(cloneMessage)};const omit=(obj,pathTree={})=>{if(Array.isArray(obj)&&pathTree["[]"]){for(const value of obj){omit(value,pathTree["[]"])}}else if(isObject(obj)){for(const key in pathTree){if(pathTree[key]===true){if(mask){obj[key]=mask}else{delete obj[key]}}else{omit(obj[key],pathTree[key])}}}};const inputOutputLoggerMiddlewareBefore=async request=>omitAndLog("event",request);const inputOutputLoggerMiddlewareAfter=async request=>omitAndLog("response",request);const inputOutputLoggerMiddlewareOnError=async request=>{if(request.response===undefined)return;return omitAndLog("response",request)};return{before:inputOutputLoggerMiddlewareBefore,after:inputOutputLoggerMiddlewareAfter,onError:inputOutputLoggerMiddlewareOnError}};const awsContextKeys=["functionName","functionVersion","invokedFunctionArn","memoryLimitInMB","awsRequestId","logGroupName","logStreamName","identity","clientContext","callbackWaitsForEmptyEventLoop"];const pick=(originalObject={},keysToPick=[])=>{const newObject={};for(const path of keysToPick){if(originalObject[path]!==undefined){newObject[path]=originalObject[path]}}return newObject};const buildPathOmitTree=paths=>{const tree={};for(let path of paths.sort().reverse()){if(!Array.isArray(path))path=path.split(".");if(path.includes("__proto__"))continue;path.slice(0).reduce((a,b,idx)=>{if(idx<path.length-1){(_a=a)[_b=b]??(_a[_b]={});return a[b]}a[b]=true;return true},tree)}return tree};const isObject=value=>value&& typeof value==="object"&&value.constructor===Object;var _default=inputOutputLoggerMiddleware;exports.default=_default
|
|
2
|
+
|
|
3
|
+
//# sourceMappingURL=index.cjs.map
|
package/index.js
CHANGED
|
@@ -1,105 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
const defaults = {
|
|
3
|
-
logger: console.log,
|
|
4
|
-
awsContext: false,
|
|
5
|
-
omitPaths: [],
|
|
6
|
-
replacer: undefined
|
|
7
|
-
};
|
|
1
|
+
var _a,_b;import{jsonSafeParse,jsonSafeStringify}from"@middy/util";const defaults={logger:console.log,awsContext:false,omitPaths:[],mask:undefined,replacer:undefined};const inputOutputLoggerMiddleware=(opts={})=>{const{logger,awsContext,omitPaths,mask,replacer}={...defaults,...opts};if(typeof logger!=="function"){throw new Error("[input-output-logger-middleware]: logger must be a function")}const omitPathTree=buildPathOmitTree(omitPaths);const omitAndLog=(param,request)=>{const message={[param]:request[param]};if(awsContext){message.context=pick(request.context,awsContextKeys)}const cloneMessage=jsonSafeParse(jsonSafeStringify(message,replacer));omit(cloneMessage,{[param]:omitPathTree[param]});logger(cloneMessage)};const omit=(obj,pathTree={})=>{if(Array.isArray(obj)&&pathTree["[]"]){for(const value of obj){omit(value,pathTree["[]"])}}else if(isObject(obj)){for(const key in pathTree){if(pathTree[key]===true){if(mask){obj[key]=mask}else{delete obj[key]}}else{omit(obj[key],pathTree[key])}}}};const inputOutputLoggerMiddlewareBefore=async request=>omitAndLog("event",request);const inputOutputLoggerMiddlewareAfter=async request=>omitAndLog("response",request);const inputOutputLoggerMiddlewareOnError=async request=>{if(request.response===undefined)return;return omitAndLog("response",request)};return{before:inputOutputLoggerMiddlewareBefore,after:inputOutputLoggerMiddlewareAfter,onError:inputOutputLoggerMiddlewareOnError}};const awsContextKeys=["functionName","functionVersion","invokedFunctionArn","memoryLimitInMB","awsRequestId","logGroupName","logStreamName","identity","clientContext","callbackWaitsForEmptyEventLoop"];const pick=(originalObject={},keysToPick=[])=>{const newObject={};for(const path of keysToPick){if(originalObject[path]!==undefined){newObject[path]=originalObject[path]}}return newObject};const buildPathOmitTree=paths=>{const tree={};for(let path of paths.sort().reverse()){if(!Array.isArray(path))path=path.split(".");if(path.includes("__proto__"))continue;path.slice(0).reduce((a,b,idx)=>{if(idx<path.length-1){(_a=a)[_b=b]??(_a[_b]={});return a[b]}a[b]=true;return true},tree)}return tree};const isObject=value=>value&& typeof value==="object"&&value.constructor===Object;export default inputOutputLoggerMiddleware
|
|
8
2
|
|
|
9
|
-
|
|
10
|
-
let {
|
|
11
|
-
logger,
|
|
12
|
-
awsContext,
|
|
13
|
-
omitPaths,
|
|
14
|
-
replacer
|
|
15
|
-
} = { ...defaults,
|
|
16
|
-
...opts
|
|
17
|
-
};
|
|
18
|
-
if (typeof logger !== 'function') logger = null;
|
|
19
|
-
const omitPathTree = buildPathOmitTree(omitPaths);
|
|
20
|
-
|
|
21
|
-
const omitAndLog = (param, request) => {
|
|
22
|
-
const message = {
|
|
23
|
-
[param]: request[param]
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
if (awsContext) {
|
|
27
|
-
message.context = pick(request.context, awsContextKeys);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const cloneMessage = jsonSafeParse(jsonSafeStringify(message, replacer));
|
|
31
|
-
omit(cloneMessage, {
|
|
32
|
-
[param]: omitPathTree[param]
|
|
33
|
-
});
|
|
34
|
-
logger(cloneMessage);
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
const inputOutputLoggerMiddlewareBefore = async request => omitAndLog('event', request);
|
|
38
|
-
|
|
39
|
-
const inputOutputLoggerMiddlewareAfter = async request => omitAndLog('response', request);
|
|
40
|
-
|
|
41
|
-
const inputOutputLoggerMiddlewareOnError = async request => {
|
|
42
|
-
if (request.response === undefined) return;
|
|
43
|
-
return omitAndLog('response', request);
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
return {
|
|
47
|
-
before: logger ? inputOutputLoggerMiddlewareBefore : undefined,
|
|
48
|
-
after: logger ? inputOutputLoggerMiddlewareAfter : undefined,
|
|
49
|
-
onError: logger ? inputOutputLoggerMiddlewareOnError : undefined
|
|
50
|
-
};
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
const awsContextKeys = ['functionName', 'functionVersion', 'invokedFunctionArn', 'memoryLimitInMB', 'awsRequestId', 'logGroupName', 'logStreamName', 'identity', 'clientContext', 'callbackWaitsForEmptyEventLoop'];
|
|
54
|
-
|
|
55
|
-
const pick = (originalObject = {}, keysToPick = []) => {
|
|
56
|
-
const newObject = {};
|
|
57
|
-
|
|
58
|
-
for (const path of keysToPick) {
|
|
59
|
-
if (originalObject[path] !== undefined) {
|
|
60
|
-
newObject[path] = originalObject[path];
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return newObject;
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
const buildPathOmitTree = paths => {
|
|
68
|
-
const tree = {};
|
|
69
|
-
|
|
70
|
-
for (let path of paths.sort().reverse()) {
|
|
71
|
-
if (!Array.isArray(path)) path = path.split('.');
|
|
72
|
-
if (path.includes('__proto__')) continue;
|
|
73
|
-
path.slice(0).reduce((a, b, idx) => {
|
|
74
|
-
if (idx < path.length - 1) {
|
|
75
|
-
a[b] ?? (a[b] = {});
|
|
76
|
-
return a[b];
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
a[b] = true;
|
|
80
|
-
return true;
|
|
81
|
-
}, tree);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
return tree;
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
const omit = (obj, pathTree = {}) => {
|
|
88
|
-
if (Array.isArray(obj) && pathTree['[]']) {
|
|
89
|
-
for (const value of obj) {
|
|
90
|
-
omit(value, pathTree['[]']);
|
|
91
|
-
}
|
|
92
|
-
} else if (isObject(obj)) {
|
|
93
|
-
for (const key in pathTree) {
|
|
94
|
-
if (pathTree[key] === true) {
|
|
95
|
-
delete obj[key];
|
|
96
|
-
} else {
|
|
97
|
-
omit(obj[key], pathTree[key]);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
const isObject = value => value && typeof value === 'object' && value.constructor === Object;
|
|
104
|
-
|
|
105
|
-
export default inputOutputLoggerMiddleware;
|
|
3
|
+
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/input-output-logger",
|
|
3
|
-
"version": "3.0.0
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Input and output logger middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -10,10 +10,17 @@
|
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
|
-
"exports":
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"import": "./index.js",
|
|
16
|
+
"require": "./index.cjs",
|
|
17
|
+
"types": "./index.d.ts"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
14
20
|
"types": "index.d.ts",
|
|
15
21
|
"files": [
|
|
16
22
|
"index.js",
|
|
23
|
+
"index.cjs",
|
|
17
24
|
"index.d.ts"
|
|
18
25
|
],
|
|
19
26
|
"scripts": {
|
|
@@ -46,13 +53,13 @@
|
|
|
46
53
|
"bugs": {
|
|
47
54
|
"url": "https://github.com/middyjs/middy/issues"
|
|
48
55
|
},
|
|
49
|
-
"homepage": "https://
|
|
56
|
+
"homepage": "https://middy.js.org",
|
|
50
57
|
"dependencies": {
|
|
51
|
-
"@middy/util": "^3.0.0
|
|
58
|
+
"@middy/util": "^3.0.0"
|
|
52
59
|
},
|
|
53
60
|
"devDependencies": {
|
|
54
|
-
"@middy/core": "^3.0.0
|
|
61
|
+
"@middy/core": "^3.0.0",
|
|
55
62
|
"@types/node": "^17.0.0"
|
|
56
63
|
},
|
|
57
|
-
"gitHead": "
|
|
64
|
+
"gitHead": "01520fa8628a36c2f89e126cad656a16547ea0d6"
|
|
58
65
|
}
|