@middy/input-output-logger 4.6.5 → 5.0.0-alpha.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.
Files changed (3) hide show
  1. package/index.js +116 -99
  2. package/package.json +4 -13
  3. package/index.cjs +0 -117
package/index.js CHANGED
@@ -1,107 +1,124 @@
1
- import { jsonSafeParse, jsonSafeStringify } from '@middy/util';
2
1
  const defaults = {
3
- logger: console.log,
4
- awsContext: false,
5
- omitPaths: [],
6
- mask: undefined,
7
- replacer: undefined
8
- };
9
- const inputOutputLoggerMiddleware = (opts = {})=>{
10
- const { logger, awsContext, omitPaths, mask, replacer } = {
11
- ...defaults,
12
- ...opts
13
- };
14
- if (typeof logger !== 'function') {
15
- throw new Error('[input-output-logger-middleware]: logger must be a function');
2
+ logger: console.log,
3
+ awsContext: false,
4
+ omitPaths: [],
5
+ mask: undefined,
6
+ replacer: undefined
7
+ }
8
+
9
+ const inputOutputLoggerMiddleware = (opts = {}) => {
10
+ const { logger, awsContext, omitPaths, mask, replacer } = {
11
+ ...defaults,
12
+ ...opts
13
+ }
14
+
15
+ if (typeof logger !== 'function') {
16
+ throw new Error('logger must be a function', {
17
+ cause: {
18
+ pacakge: '@middy/input-output-logger'
19
+ }
20
+ })
21
+ }
22
+
23
+ const omitPathTree = buildPathTree(omitPaths)
24
+ const omitAndLog = (param, request) => {
25
+ const message = { [param]: request[param] }
26
+
27
+ if (awsContext) {
28
+ message.context = pick(request.context, awsContextKeys)
16
29
  }
17
- const omitPathTree = buildPathTree(omitPaths);
18
- const omitAndLog = (param, request)=>{
19
- const message = {
20
- [param]: request[param]
21
- };
22
- if (awsContext) {
23
- message.context = pick(request.context, awsContextKeys);
24
- }
25
- const cloneMessage = jsonSafeParse(jsonSafeStringify(message, replacer)) // Full clone to prevent nested mutations
26
- ;
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
- }
30
+
31
+ let cloneMessage = message
32
+ if (omitPaths.length) {
33
+ cloneMessage = structuredClone(message, replacer) // Full clone to prevent nested mutations
34
+ omit(cloneMessage, { [param]: omitPathTree[param] })
35
+ }
36
+ logger(cloneMessage)
37
+ }
38
+
39
+ const omit = (obj, pathTree = {}) => {
40
+ if (Array.isArray(obj) && pathTree['[]']) {
41
+ for (let i = 0, l = obj.length; i < l; i++) {
42
+ omit(obj[i], pathTree['[]'])
43
+ }
44
+ } else if (isObject(obj)) {
45
+ for (const key in pathTree) {
46
+ if (pathTree[key] === true) {
47
+ if (mask) {
48
+ obj[key] = mask
49
+ } else {
50
+ delete obj[key]
51
+ }
52
+ } else {
53
+ omit(obj[key], pathTree[key])
49
54
  }
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
- omitAndLog('response', request);
56
- };
57
- return {
58
- before: inputOutputLoggerMiddlewareBefore,
59
- after: inputOutputLoggerMiddlewareAfter,
60
- onError: inputOutputLoggerMiddlewareOnError
61
- };
62
- };
55
+ }
56
+ }
57
+ }
58
+
59
+ const inputOutputLoggerMiddlewareBefore = async (request) =>
60
+ omitAndLog('event', request)
61
+ const inputOutputLoggerMiddlewareAfter = async (request) =>
62
+ omitAndLog('response', request)
63
+ const inputOutputLoggerMiddlewareOnError = async (request) => {
64
+ if (request.response === undefined) return
65
+ omitAndLog('response', request)
66
+ }
67
+
68
+ return {
69
+ before: inputOutputLoggerMiddlewareBefore,
70
+ after: inputOutputLoggerMiddlewareAfter,
71
+ onError: inputOutputLoggerMiddlewareOnError
72
+ }
73
+ }
74
+
63
75
  // https://docs.aws.amazon.com/lambda/latest/dg/nodejs-context.html
64
76
  const awsContextKeys = [
65
- 'functionName',
66
- 'functionVersion',
67
- 'invokedFunctionArn',
68
- 'memoryLimitInMB',
69
- 'awsRequestId',
70
- 'logGroupName',
71
- 'logStreamName',
72
- 'identity',
73
- 'clientContext',
74
- 'callbackWaitsForEmptyEventLoop'
75
- ];
77
+ 'functionName',
78
+ 'functionVersion',
79
+ 'invokedFunctionArn',
80
+ 'memoryLimitInMB',
81
+ 'awsRequestId',
82
+ 'logGroupName',
83
+ 'logStreamName',
84
+ 'identity',
85
+ 'clientContext',
86
+ 'callbackWaitsForEmptyEventLoop'
87
+ ]
88
+
76
89
  // move to util, if ever used elsewhere
77
- const pick = (originalObject = {}, keysToPick = [])=>{
78
- const newObject = {};
79
- for (const path of keysToPick){
80
- // only supports first level
81
- if (originalObject[path] !== undefined) {
82
- newObject[path] = originalObject[path];
83
- }
90
+ const pick = (originalObject = {}, keysToPick = []) => {
91
+ const newObject = {}
92
+ for (const path of keysToPick) {
93
+ // only supports first level
94
+ if (originalObject[path] !== undefined) {
95
+ newObject[path] = originalObject[path]
84
96
  }
85
- return newObject;
86
- };
87
- const buildPathTree = (paths)=>{
88
- const tree = {};
89
- for (let path of paths.sort().reverse()){
90
- // reverse to ensure conflicting paths don't cause issues
91
- if (!Array.isArray(path)) path = path.split('.');
92
- if (path.includes('__proto__')) continue;
93
- path.slice(0) // clone
94
- .reduce((a, b, idx)=>{
95
- if (idx < path.length - 1) {
96
- 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
- export default inputOutputLoggerMiddleware;
97
+ }
98
+ return newObject
99
+ }
100
+
101
+ const buildPathTree = (paths) => {
102
+ const tree = {}
103
+ for (let path of paths.sort().reverse()) {
104
+ // reverse to ensure conflicting paths don't cause issues
105
+ if (!Array.isArray(path)) path = path.split('.')
106
+ if (path.includes('__proto__')) continue
107
+ path
108
+ .slice(0) // clone
109
+ .reduce((a, b, idx) => {
110
+ if (idx < path.length - 1) {
111
+ a[b] ??= {}
112
+ return a[b]
113
+ }
114
+ a[b] = true
115
+ return true
116
+ }, tree)
117
+ }
118
+ return tree
119
+ }
120
+
121
+ const isObject = (value) =>
122
+ value && typeof value === 'object' && value.constructor === Object
107
123
 
124
+ export default inputOutputLoggerMiddleware
package/package.json CHANGED
@@ -1,33 +1,27 @@
1
1
  {
2
2
  "name": "@middy/input-output-logger",
3
- "version": "4.6.5",
3
+ "version": "5.0.0-alpha.1",
4
4
  "description": "Input and output logger middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
7
- "node": ">=16"
7
+ "node": ">=18"
8
8
  },
9
9
  "engineStrict": true,
10
10
  "publishConfig": {
11
11
  "access": "public"
12
12
  },
13
- "main": "./index.cjs",
14
13
  "module": "./index.js",
15
14
  "exports": {
16
15
  ".": {
17
16
  "import": {
18
17
  "types": "./index.d.ts",
19
18
  "default": "./index.js"
20
- },
21
- "require": {
22
- "types": "./index.d.ts",
23
- "default": "./index.cjs"
24
19
  }
25
20
  }
26
21
  },
27
22
  "types": "index.d.ts",
28
23
  "files": [
29
24
  "index.js",
30
- "index.cjs",
31
25
  "index.d.ts"
32
26
  ],
33
27
  "scripts": {
@@ -65,12 +59,9 @@
65
59
  "type": "github",
66
60
  "url": "https://github.com/sponsors/willfarrell"
67
61
  },
68
- "dependencies": {
69
- "@middy/util": "4.6.5"
70
- },
71
62
  "devDependencies": {
72
- "@middy/core": "4.6.5",
63
+ "@middy/core": "5.0.0-alpha.1",
73
64
  "@types/node": "^20.0.0"
74
65
  },
75
- "gitHead": "573d7b0bb243d8c5a9bcb00cf29d031aa7a0c606"
66
+ "gitHead": "ebce8d5df8783077fa49ba62ee9be20e8486a7f1"
76
67
  }
package/index.cjs DELETED
@@ -1,117 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(module, "exports", {
6
- enumerable: true,
7
- get: function() {
8
- return _default;
9
- }
10
- });
11
- const _util = require("@middy/util");
12
- const defaults = {
13
- logger: console.log,
14
- awsContext: false,
15
- omitPaths: [],
16
- mask: undefined,
17
- replacer: undefined
18
- };
19
- const inputOutputLoggerMiddleware = (opts = {})=>{
20
- const { logger, awsContext, omitPaths, mask, replacer } = {
21
- ...defaults,
22
- ...opts
23
- };
24
- if (typeof logger !== 'function') {
25
- throw new Error('[input-output-logger-middleware]: logger must be a function');
26
- }
27
- const omitPathTree = buildPathTree(omitPaths);
28
- const omitAndLog = (param, request)=>{
29
- const message = {
30
- [param]: request[param]
31
- };
32
- if (awsContext) {
33
- message.context = pick(request.context, awsContextKeys);
34
- }
35
- const cloneMessage = (0, _util.jsonSafeParse)((0, _util.jsonSafeStringify)(message, replacer)) // Full clone to prevent nested mutations
36
- ;
37
- omit(cloneMessage, {
38
- [param]: omitPathTree[param]
39
- });
40
- logger(cloneMessage);
41
- };
42
- const omit = (obj, pathTree = {})=>{
43
- if (Array.isArray(obj) && pathTree['[]']) {
44
- for (const value of obj){
45
- omit(value, pathTree['[]']);
46
- }
47
- } else if (isObject(obj)) {
48
- for(const key in pathTree){
49
- if (pathTree[key] === true) {
50
- if (mask) {
51
- obj[key] = mask;
52
- } else {
53
- delete obj[key];
54
- }
55
- } else {
56
- omit(obj[key], pathTree[key]);
57
- }
58
- }
59
- }
60
- };
61
- const inputOutputLoggerMiddlewareBefore = async (request)=>omitAndLog('event', request);
62
- const inputOutputLoggerMiddlewareAfter = async (request)=>omitAndLog('response', request);
63
- const inputOutputLoggerMiddlewareOnError = async (request)=>{
64
- if (request.response === undefined) return;
65
- omitAndLog('response', request);
66
- };
67
- return {
68
- before: inputOutputLoggerMiddlewareBefore,
69
- after: inputOutputLoggerMiddlewareAfter,
70
- onError: inputOutputLoggerMiddlewareOnError
71
- };
72
- };
73
- // https://docs.aws.amazon.com/lambda/latest/dg/nodejs-context.html
74
- const awsContextKeys = [
75
- 'functionName',
76
- 'functionVersion',
77
- 'invokedFunctionArn',
78
- 'memoryLimitInMB',
79
- 'awsRequestId',
80
- 'logGroupName',
81
- 'logStreamName',
82
- 'identity',
83
- 'clientContext',
84
- 'callbackWaitsForEmptyEventLoop'
85
- ];
86
- // move to util, if ever used elsewhere
87
- const pick = (originalObject = {}, keysToPick = [])=>{
88
- const newObject = {};
89
- for (const path of keysToPick){
90
- // only supports first level
91
- if (originalObject[path] !== undefined) {
92
- newObject[path] = originalObject[path];
93
- }
94
- }
95
- return newObject;
96
- };
97
- const buildPathTree = (paths)=>{
98
- const tree = {};
99
- for (let path of paths.sort().reverse()){
100
- // reverse to ensure conflicting paths don't cause issues
101
- if (!Array.isArray(path)) path = path.split('.');
102
- if (path.includes('__proto__')) continue;
103
- path.slice(0) // clone
104
- .reduce((a, b, idx)=>{
105
- if (idx < path.length - 1) {
106
- a[b] ??= {};
107
- return a[b];
108
- }
109
- a[b] = true;
110
- return true;
111
- }, tree);
112
- }
113
- return tree;
114
- };
115
- const isObject = (value)=>value && typeof value === 'object' && value.constructor === Object;
116
- const _default = inputOutputLoggerMiddleware;
117
-