vtlab-generic-functions 1.0.9 → 1.0.11

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.
@@ -6,6 +6,21 @@ if (!global.LOG_STREAM_NAME) {
6
6
  global.LOG_STREAM_NAME = `${process.env.AWS_LAMBDA_FUNCTION_NAME}-${process.env.AWS_LAMBDA_LOG_STREAM_NAME}-${randomUUID()}`;
7
7
  }
8
8
 
9
+ function safeStringify(obj) {
10
+ //this avoids circular references
11
+ const cache = new Set();
12
+ return JSON.stringify(obj, (key, value) => {
13
+ if (typeof value === 'object' && value !== null) {
14
+ if (cache.has(value)) {
15
+ // duplicate reference found, skip it
16
+ return;
17
+ }
18
+ cache.add(value);
19
+ }
20
+ return value;
21
+ });
22
+ }
23
+
9
24
  const putLogEvents = async (logEvents = [], logGroupName, logLevel = 2, logStreamName = null) => {
10
25
  if (logLevel === 0) {
11
26
  return;
@@ -24,7 +39,23 @@ const putLogEvents = async (logEvents = [], logGroupName, logLevel = 2, logStrea
24
39
  }
25
40
  });
26
41
 
27
- Array.isArray(logEvents) || (logEvents = [String(logEvents)]);
42
+ if (!Array.isArray(logEvents)) {
43
+ logEvents = [logEvents];
44
+ }
45
+
46
+ //safe stringify to avoid circular references and [object Object]
47
+ logEvents = logEvents.map(event => {
48
+ if (typeof event === 'string') {
49
+ return event;
50
+ }
51
+ try {
52
+ return safeStringify(event);
53
+ } catch (error) {
54
+ // Handle or log the error as needed
55
+ console.error('Error stringifying event:', error);
56
+ return 'Error in stringifying event';
57
+ }
58
+ });
28
59
 
29
60
  if (logEvents.length === 0) {
30
61
  throw new Error('Empty log event');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vtlab-generic-functions",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
4
4
  "scripts": {
5
5
  "test": "jest"
6
6
  },