From 87484fb9e4a9e7ec96780c3ebcb31dc4eb8bb37b Mon Sep 17 00:00:00 2001 From: Walt Jones Date: Tue, 13 Sep 2022 11:16:39 -0400 Subject: [PATCH] fix: allow captureIp without a window object (#1008) --- src/browser/transforms.js | 19 ++++++++++++------- test/browser.transforms.test.js | 16 +++++++++++++++- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/browser/transforms.js b/src/browser/transforms.js index cab7b99c..a9d45e39 100644 --- a/src/browser/transforms.js +++ b/src/browser/transforms.js @@ -79,20 +79,25 @@ function addBaseInfo(item, options, callback) { function addRequestInfo(window) { return function(item, options, callback) { - if (!window || !window.location) { - return callback(null, item); + var requestInfo = {}; + + if (window && window.location) { + requestInfo.url = window.location.href; + requestInfo.query_string = window.location.search; } + var remoteString = '$remote_ip'; if (!options.captureIp) { remoteString = null; } else if (options.captureIp !== true) { remoteString += '_anonymize'; } - _.set(item, 'data.request', { - url: window.location.href, - query_string: window.location.search, - user_ip: remoteString - }); + if (remoteString) requestInfo.user_ip = remoteString; + + if (Object.keys(requestInfo).length > 0) { + _.set(item, 'data.request', requestInfo); + } + callback(null, item); }; } diff --git a/test/browser.transforms.test.js b/test/browser.transforms.test.js index 2e5f4761..69656a63 100644 --- a/test/browser.transforms.test.js +++ b/test/browser.transforms.test.js @@ -226,9 +226,10 @@ describe('addRequestInfo', function() { it('should use window info to set request properties', function(done) { var args = ['a message']; var item = itemFromArgs(args); - var options = {}; + var options = { captureIp: 'anonymize' }; t.addRequestInfo(window)(item, options, function(e, i) { expect(i.data.request).to.be.ok(); + expect(i.data.request.user_ip).to.eql('$remote_ip_anonymize'); done(e); }); }); @@ -243,6 +244,19 @@ describe('addRequestInfo', function() { done(e); }); }); + it('should honor captureIp without window', function(done) { + var args = ['a message']; + var item = itemFromArgs(args); + item.data = {}; + var options = { captureIp: true }; + var w = null; + t.addRequestInfo(w)(item, options, function(e, i) { + expect(i.data.request.url).to.not.be.ok(); + expect(i.data.request.query_string).to.not.be.ok(); + expect(i.data.request.user_ip).to.eql('$remote_ip'); + done(e); + }); + }); }); describe('addClientInfo', function() {