fallback to json dance if structureClone fails
[squeep-logger-json-console] / lib / logger.js
index 3f336ab4c7121d17fbeaca6eacfee45bc678c043..6cc4a2b4f30065b6d51e41de253d2267a780ee95 100644 (file)
@@ -3,7 +3,7 @@
 const jsonReplacers = require('./json-replacers');
 const dataSanitizers = require('./data-sanitizers');
 
-const nop = () => { /**/ };
+const nop = () => undefined;
 
 class Logger {
   /**
@@ -94,7 +94,11 @@ class Logger {
 
     if (this.sanitizationNeeded(data)) {
       // Create copy of data so we are not changing anything important.
-      data = structuredClone(data);
+      try {
+        data = structuredClone(data);
+      } catch (e) {
+        data = JSON.parse(JSON.stringify(data, replacer));
+      }
       this.sanitize(data);
     }
 
@@ -150,7 +154,7 @@ class Logger {
         while (ancestors.length > 0 && ancestors.at(-1) !== this) {
           ancestors.pop();
         }
-        if (ancestors.includes(value)) { // eslint-disable-line security/detect-object-injection
+        if (ancestors.includes(value)) {
           return '[Circular]';
         } else {
           ancestors.push(value);
@@ -158,7 +162,7 @@ class Logger {
       }
 
       return value;
-    }
+    };
   }
 
 }