Skip to content

Commit c5eec04

Browse files
committed
feat: Preserve some non-enumerable properties from request
1 parent 9219b55 commit c5eec04

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
lines changed

lib/client.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,14 +230,29 @@ extend(Raven.prototype, {
230230
so we only parse a `req` property if the `request` property is absent/empty (and hence we won't clobber)
231231
parseUser returns a partial kwargs object with a `request` property and possibly a `user` property
232232
*/
233+
var initialRequest = utils.prepareInitialRequest(
234+
this._globalContext.request,
235+
domainContext.request,
236+
kwargs.request
237+
);
233238
kwargs.request = extend(
234-
{},
239+
initialRequest,
235240
this._globalContext.request,
236241
domainContext.request,
237242
kwargs.request
238243
);
239244
if (Object.keys(kwargs.request).length === 0) {
240-
var req = extend({}, this._globalContext.req, domainContext.req, kwargs.req);
245+
var initialReq = utils.prepareInitialRequest(
246+
this._globalContext.req,
247+
domainContext.req,
248+
kwargs.req
249+
);
250+
var req = extend(
251+
initialReq,
252+
this._globalContext.req,
253+
domainContext.req,
254+
kwargs.req
255+
);
241256
if (Object.keys(req).length > 0) {
242257
var parseUser = Object.keys(kwargs.user).length === 0 ? this.parseUser : false;
243258
extend(kwargs, parsers.parseRequest(req, parseUser));

lib/utils.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,22 @@ module.exports.getAuthHeader = function getAuthHeader(timestamp, apiKey, apiSecr
5656
return header.join(', ');
5757
};
5858

59+
module.exports.prepareInitialRequest = function prepareInitialRequest() {
60+
var request = {};
61+
var nonEnumberables = ['ip'];
62+
var sources = Array.from(arguments).filter(function(source) {
63+
return Object.prototype.toString.call(source) === '[object Object]';
64+
});
65+
66+
nonEnumberables.forEach(function(key) {
67+
sources.forEach(function(source) {
68+
if (source[key]) request[key] = source[key];
69+
});
70+
});
71+
72+
return request;
73+
};
74+
5975
module.exports.parseDSN = function parseDSN(dsn) {
6076
if (!dsn) {
6177
// Let a falsey value return false explicitly

test/raven.utils.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,66 @@ describe('raven.utils', function() {
9898
});
9999
});
100100

101+
describe('#prepareInitialRequest', function() {
102+
it('should extract specific non-enumerables and return an object containing them', function() {
103+
var req = raven.utils.prepareInitialRequest({
104+
ip: '127.0.0.1'
105+
});
106+
var expected = {
107+
ip: '127.0.0.1'
108+
};
109+
req.should.eql(expected);
110+
});
111+
112+
it('should skip non-listed properties', function() {
113+
var req = raven.utils.prepareInitialRequest({
114+
ip: '127.0.0.1',
115+
pickle: 'Rick',
116+
evil: 'Morty'
117+
});
118+
var expected = {
119+
ip: '127.0.0.1'
120+
};
121+
req.should.eql(expected);
122+
});
123+
124+
it('should preserve extend-like order', function() {
125+
var req = raven.utils.prepareInitialRequest(
126+
{
127+
ip: '127.0.0.1'
128+
},
129+
{
130+
ip: '123.123.123.123'
131+
},
132+
{
133+
ip: '42.42.42.42'
134+
}
135+
);
136+
var expected = {
137+
ip: '42.42.42.42'
138+
};
139+
req.should.eql(expected);
140+
});
141+
142+
it('should filter incorrect sources', function() {
143+
var req = raven.utils.prepareInitialRequest(
144+
{
145+
ip: '127.0.0.1'
146+
},
147+
[42],
148+
null,
149+
'hello',
150+
{
151+
ip: '123.123.123.123'
152+
}
153+
);
154+
var expected = {
155+
ip: '123.123.123.123'
156+
};
157+
req.should.eql(expected);
158+
});
159+
});
160+
101161
describe('#parseAuthHeader()', function() {
102162
it('should parse all parameters', function() {
103163
var timestamp = 12345,

0 commit comments

Comments
 (0)