Skip to content

Commit 362ae20

Browse files
committed
Vendor in json-stringify-safe to avoid circular JSON references
1 parent d781478 commit 362ae20

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/raven.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
var TraceKit = require('../vendor/TraceKit/tracekit');
55
var RavenConfigError = require('./configError');
66
var utils = require('./utils');
7+
var stringify = require('../vendor/json-stringify-safe/stringify');
78

89
var isFunction = utils.isFunction;
910
var isUndefined = utils.isUndefined;
@@ -428,7 +429,7 @@ Raven.prototype = {
428429
*/
429430
getContext: function() {
430431
// lol javascript
431-
return JSON.parse(JSON.stringify(this._globalContext));
432+
return JSON.parse(stringify(this._globalContext));
432433
},
433434

434435
/*
@@ -1285,7 +1286,7 @@ Raven.prototype = {
12851286
// NOTE: auth is intentionally sent as part of query string (NOT as custom
12861287
// HTTP header) so as to avoid preflight CORS requests
12871288
request.open('POST', url + '?' + urlencode(opts.auth));
1288-
request.send(JSON.stringify(opts.data));
1289+
request.send(stringify(opts.data));
12891290
},
12901291

12911292
// Note: this is shitty, but I can't figure out how to get
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
exports = module.exports = stringify
2+
exports.getSerialize = serializer
3+
4+
function stringify(obj, replacer, spaces, cycleReplacer) {
5+
return JSON.stringify(obj, serializer(replacer, cycleReplacer), spaces)
6+
}
7+
8+
function serializer(replacer, cycleReplacer) {
9+
var stack = [], keys = []
10+
11+
if (cycleReplacer == null) cycleReplacer = function(key, value) {
12+
if (stack[0] === value) return "[Circular ~]"
13+
return "[Circular ~." + keys.slice(0, stack.indexOf(value)).join(".") + "]"
14+
}
15+
16+
return function(key, value) {
17+
if (stack.length > 0) {
18+
var thisPos = stack.indexOf(this)
19+
~thisPos ? stack.splice(thisPos + 1) : stack.push(this)
20+
~thisPos ? keys.splice(thisPos, Infinity, key) : keys.push(key)
21+
if (~stack.indexOf(value)) value = cycleReplacer.call(this, key, value)
22+
}
23+
else stack.push(value)
24+
25+
return replacer == null ? value : replacer.call(this, key, value)
26+
}
27+
}

0 commit comments

Comments
 (0)