Chrome sendrequest error: TypeError: Converting circular structure to JSON

后端 未结 11 1237
醉梦人生
醉梦人生 2020-11-22 04:16

I\'ve got the following...

chrome.extension.sendRequest({
  req: \"getDocument\",
  docu: pagedoc,
  name: \'name\'
}, function(response){
  var efjs = respo         


        
11条回答
  •  一个人的身影
    2020-11-22 05:14

    This works and tells you which properties are circular. It also allows for reconstructing the object with the references

      JSON.stringifyWithCircularRefs = (function() {
        const refs = new Map();
        const parents = [];
        const path = ["this"];
    
        function clear() {
          refs.clear();
          parents.length = 0;
          path.length = 1;
        }
    
        function updateParents(key, value) {
          var idx = parents.length - 1;
          var prev = parents[idx];
          if (prev[key] === value || idx === 0) {
            path.push(key);
            parents.push(value);
          } else {
            while (idx-- >= 0) {
              prev = parents[idx];
              if (prev[key] === value) {
                idx += 2;
                parents.length = idx;
                path.length = idx;
                --idx;
                parents[idx] = value;
                path[idx] = key;
                break;
              }
            }
          }
        }
    
        function checkCircular(key, value) {
          if (value != null) {
            if (typeof value === "object") {
              if (key) { updateParents(key, value); }
    
              let other = refs.get(value);
              if (other) {
                return '[Circular Reference]' + other;
              } else {
                refs.set(value, path.join('.'));
              }
            }
          }
          return value;
        }
    
        return function stringifyWithCircularRefs(obj, space) {
          try {
            parents.push(obj);
            return JSON.stringify(obj, checkCircular, space);
          } finally {
            clear();
          }
        }
      })();
    

    Example with a lot of the noise removed:

    {
        "requestStartTime": "2020-05-22...",
        "ws": {
            "_events": {},
            "readyState": 2,
            "_closeTimer": {
                "_idleTimeout": 30000,
                "_idlePrev": {
                    "_idleNext": "[Circular Reference]this.ws._closeTimer",
                    "_idlePrev": "[Circular Reference]this.ws._closeTimer",
                    "expiry": 33764,
                    "id": -9007199254740987,
                    "msecs": 30000,
                    "priorityQueuePosition": 2
                },
                "_idleNext": "[Circular Reference]this.ws._closeTimer._idlePrev",
                "_idleStart": 3764,
                "_destroyed": false
            },
            "_closeCode": 1006,
            "_extensions": {},
            "_receiver": {
                "_binaryType": "nodebuffer",
                "_extensions": "[Circular Reference]this.ws._extensions",
            },
            "_sender": {
                "_extensions": "[Circular Reference]this.ws._extensions",
                "_socket": {
                    "_tlsOptions": {
                        "pipe": false,
                        "secureContext": {
                            "context": {},
                            "singleUse": true
                        },
                    },
                    "ssl": {
                        "_parent": {
                            "reading": true
                        },
                        "_secureContext": "[Circular Reference]this.ws._sender._socket._tlsOptions.secureContext",
                        "reading": true
                    }
                },
                "_firstFragment": true,
                "_compress": false,
                "_bufferedBytes": 0,
                "_deflating": false,
                "_queue": []
            },
            "_socket": "[Circular Reference]this.ws._sender._socket"
        }
    }
    

    To reconstruct call JSON.parse() then loop through the properties looking for the [Circular Reference] tag. Then chop that off and... eval... it with this set to the root object.

    Don't eval anything that can be hacked. Better practice would be to do string.split('.') then lookup the properties by name to set the reference.

提交回复
热议问题