@middy/input-output-logger 3.0.2 → 3.1.0-rc.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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2017-2022 Luciano Mammino, will Farrell and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
3
+ Copyright (c) 2017-2022 [Luciano Mammino](https://github.com/lmammino), [will Farrell](https://github.com/willfarrell) and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -9,8 +9,8 @@
9
9
  <a href="https://packagephobia.com/result?p=@middy/input-output-logger">
10
10
  <img src="https://packagephobia.com/badge?p=@middy/input-output-logger" alt="npm install size" style="max-width:100%;">
11
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%;">
12
+ <a href="https://github.com/middyjs/middy/actions/workflows/tests.yml">
13
+ <img src="https://github.com/middyjs/middy/actions/workflows/tests.yml/badge.svg?branch=main&event=push" alt="GitHub Actions CI status badge" style="max-width:100%;">
14
14
  </a>
15
15
  <br/>
16
16
  <a href="https://standardjs.com/">
@@ -33,6 +33,7 @@
33
33
  <img src="https://img.shields.io/badge/StackOverflow-[middy]-yellow" alt="Ask questions on StackOverflow" style="max-width:100%;">
34
34
  </a>
35
35
  </p>
36
+ <p>You can read the documentation at: <a href="https://middy.js.org/docs/middlewares/input-output-logger">https://middy.js.org/docs/middlewares/input-output-logger</a></p>
36
37
  </div>
37
38
 
38
39
  Logs the incoming request (input) and the response (output).
@@ -109,7 +110,7 @@ Everyone is very welcome to contribute to this repository. Feel free to [raise i
109
110
 
110
111
  ## License
111
112
 
112
- Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
113
+ Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 [Luciano Mammino](https://github.com/lmammino), [will Farrell](https://github.com/willfarrell), and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
113
114
 
114
115
  <a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
115
116
  <img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
package/index.cjs CHANGED
@@ -1,3 +1,110 @@
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
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ module.exports = void 0;
6
+ var _util = require("@middy/util");
7
+ var _a, _b;
8
+ const defaults = {
9
+ logger: console.log,
10
+ awsContext: false,
11
+ omitPaths: [],
12
+ mask: undefined,
13
+ replacer: undefined
14
+ };
15
+ const inputOutputLoggerMiddleware = (opts = {})=>{
16
+ const { logger , awsContext , omitPaths , mask , replacer } = {
17
+ ...defaults,
18
+ ...opts
19
+ };
20
+ if (typeof logger !== 'function') {
21
+ throw new Error('[input-output-logger-middleware]: logger must be a function');
22
+ }
23
+ const omitPathTree = buildPathOmitTree(omitPaths);
24
+ const omitAndLog = (param, request)=>{
25
+ const message = {
26
+ [param]: request[param]
27
+ };
28
+ if (awsContext) {
29
+ message.context = pick(request.context, awsContextKeys);
30
+ }
31
+ const cloneMessage = (0, _util).jsonSafeParse((0, _util).jsonSafeStringify(message, replacer));
32
+ omit(cloneMessage, {
33
+ [param]: omitPathTree[param]
34
+ });
35
+ logger(cloneMessage);
36
+ };
37
+ const omit = (obj, pathTree = {})=>{
38
+ if (Array.isArray(obj) && pathTree['[]']) {
39
+ for (const value of obj){
40
+ omit(value, pathTree['[]']);
41
+ }
42
+ } else if (isObject(obj)) {
43
+ for(const key in pathTree){
44
+ if (pathTree[key] === true) {
45
+ if (mask) {
46
+ obj[key] = mask;
47
+ } else {
48
+ delete obj[key];
49
+ }
50
+ } else {
51
+ omit(obj[key], pathTree[key]);
52
+ }
53
+ }
54
+ }
55
+ };
56
+ const inputOutputLoggerMiddlewareBefore = async (request)=>omitAndLog('event', request);
57
+ const inputOutputLoggerMiddlewareAfter = async (request)=>omitAndLog('response', request);
58
+ const inputOutputLoggerMiddlewareOnError = async (request)=>{
59
+ if (request.response === undefined) return;
60
+ return omitAndLog('response', request);
61
+ };
62
+ return {
63
+ before: inputOutputLoggerMiddlewareBefore,
64
+ after: inputOutputLoggerMiddlewareAfter,
65
+ onError: inputOutputLoggerMiddlewareOnError
66
+ };
67
+ };
68
+ const awsContextKeys = [
69
+ 'functionName',
70
+ 'functionVersion',
71
+ 'invokedFunctionArn',
72
+ 'memoryLimitInMB',
73
+ 'awsRequestId',
74
+ 'logGroupName',
75
+ 'logStreamName',
76
+ 'identity',
77
+ 'clientContext',
78
+ 'callbackWaitsForEmptyEventLoop'
79
+ ];
80
+ const pick = (originalObject = {}, keysToPick = [])=>{
81
+ const newObject = {};
82
+ for (const path of keysToPick){
83
+ if (originalObject[path] !== undefined) {
84
+ newObject[path] = originalObject[path];
85
+ }
86
+ }
87
+ return newObject;
88
+ };
89
+ const buildPathOmitTree = (paths)=>{
90
+ const tree = {};
91
+ for (let path of paths.sort().reverse()){
92
+ if (!Array.isArray(path)) path = path.split('.');
93
+ if (path.includes('__proto__')) continue;
94
+ path.slice(0).reduce((a, b, idx)=>{
95
+ if (idx < path.length - 1) {
96
+ (_a = a)[_b = b] ?? (_a[_b] = {});
97
+ return a[b];
98
+ }
99
+ a[b] = true;
100
+ return true;
101
+ }, tree);
102
+ }
103
+ return tree;
104
+ };
105
+ const isObject = (value)=>value && typeof value === 'object' && value.constructor === Object;
106
+ var _default = inputOutputLoggerMiddleware;
107
+ module.exports = _default;
108
+
2
109
 
3
110
  //# sourceMappingURL=index.cjs.map
package/index.js CHANGED
@@ -1,3 +1,104 @@
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
1
+ var _a, _b;
2
+ import { jsonSafeParse, jsonSafeStringify } from '@middy/util';
3
+ const defaults = {
4
+ logger: console.log,
5
+ awsContext: false,
6
+ omitPaths: [],
7
+ mask: undefined,
8
+ replacer: undefined
9
+ };
10
+ const inputOutputLoggerMiddleware = (opts = {})=>{
11
+ const { logger , awsContext , omitPaths , mask , replacer } = {
12
+ ...defaults,
13
+ ...opts
14
+ };
15
+ if (typeof logger !== 'function') {
16
+ throw new Error('[input-output-logger-middleware]: logger must be a function');
17
+ }
18
+ const omitPathTree = buildPathOmitTree(omitPaths);
19
+ const omitAndLog = (param, request)=>{
20
+ const message = {
21
+ [param]: request[param]
22
+ };
23
+ if (awsContext) {
24
+ message.context = pick(request.context, awsContextKeys);
25
+ }
26
+ const cloneMessage = jsonSafeParse(jsonSafeStringify(message, replacer));
27
+ omit(cloneMessage, {
28
+ [param]: omitPathTree[param]
29
+ });
30
+ logger(cloneMessage);
31
+ };
32
+ const omit = (obj, pathTree = {})=>{
33
+ if (Array.isArray(obj) && pathTree['[]']) {
34
+ for (const value of obj){
35
+ omit(value, pathTree['[]']);
36
+ }
37
+ } else if (isObject(obj)) {
38
+ for(const key in pathTree){
39
+ if (pathTree[key] === true) {
40
+ if (mask) {
41
+ obj[key] = mask;
42
+ } else {
43
+ delete obj[key];
44
+ }
45
+ } else {
46
+ omit(obj[key], pathTree[key]);
47
+ }
48
+ }
49
+ }
50
+ };
51
+ const inputOutputLoggerMiddlewareBefore = async (request)=>omitAndLog('event', request);
52
+ const inputOutputLoggerMiddlewareAfter = async (request)=>omitAndLog('response', request);
53
+ const inputOutputLoggerMiddlewareOnError = async (request)=>{
54
+ if (request.response === undefined) return;
55
+ return omitAndLog('response', request);
56
+ };
57
+ return {
58
+ before: inputOutputLoggerMiddlewareBefore,
59
+ after: inputOutputLoggerMiddlewareAfter,
60
+ onError: inputOutputLoggerMiddlewareOnError
61
+ };
62
+ };
63
+ const awsContextKeys = [
64
+ 'functionName',
65
+ 'functionVersion',
66
+ 'invokedFunctionArn',
67
+ 'memoryLimitInMB',
68
+ 'awsRequestId',
69
+ 'logGroupName',
70
+ 'logStreamName',
71
+ 'identity',
72
+ 'clientContext',
73
+ 'callbackWaitsForEmptyEventLoop'
74
+ ];
75
+ const pick = (originalObject = {}, keysToPick = [])=>{
76
+ const newObject = {};
77
+ for (const path of keysToPick){
78
+ if (originalObject[path] !== undefined) {
79
+ newObject[path] = originalObject[path];
80
+ }
81
+ }
82
+ return newObject;
83
+ };
84
+ const buildPathOmitTree = (paths)=>{
85
+ const tree = {};
86
+ for (let path of paths.sort().reverse()){
87
+ if (!Array.isArray(path)) path = path.split('.');
88
+ if (path.includes('__proto__')) continue;
89
+ path.slice(0).reduce((a, b, idx)=>{
90
+ if (idx < path.length - 1) {
91
+ (_a = a)[_b = b] ?? (_a[_b] = {});
92
+ return a[b];
93
+ }
94
+ a[b] = true;
95
+ return true;
96
+ }, tree);
97
+ }
98
+ return tree;
99
+ };
100
+ const isObject = (value)=>value && typeof value === 'object' && value.constructor === Object;
101
+ export default inputOutputLoggerMiddleware;
102
+
2
103
 
3
104
  //# 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.2",
3
+ "version": "3.1.0-rc.0",
4
4
  "description": "Input and output logger middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -10,11 +10,17 @@
10
10
  "publishConfig": {
11
11
  "access": "public"
12
12
  },
13
+ "main": "./index.cjs",
13
14
  "exports": {
14
15
  ".": {
15
- "import": "./index.js",
16
- "require": "./index.cjs",
17
- "types": "./index.d.ts"
16
+ "import": {
17
+ "types": "./index.d.ts",
18
+ "default": "./index.js"
19
+ },
20
+ "require": {
21
+ "types": "./index.d.ts",
22
+ "default": "./index.cjs"
23
+ }
18
24
  }
19
25
  },
20
26
  "types": "index.d.ts",
@@ -55,11 +61,11 @@
55
61
  },
56
62
  "homepage": "https://middy.js.org",
57
63
  "dependencies": {
58
- "@middy/util": "^3.0.2"
64
+ "@middy/util": "3.1.0-rc.0"
59
65
  },
60
66
  "devDependencies": {
61
- "@middy/core": "^3.0.2",
67
+ "@middy/core": "3.1.0-rc.0",
62
68
  "@types/node": "^17.0.0"
63
69
  },
64
- "gitHead": "983649b8359ea32a786e75dfc2953aeee8ec6052"
70
+ "gitHead": "03a8794d3cdb4319eca49ba4c55420bea5d66430"
65
71
  }