Skip to content

Commit

Permalink
Merge pull request #164 from newrelic/handle-data-urls
Browse files Browse the repository at this point in the history
Add handling for data URLs
  • Loading branch information
aubreymasten authored Jan 4, 2022
2 parents ed32de6 + 1031a8a commit b4a8e50
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
17 changes: 17 additions & 0 deletions feature/xhr/instrument/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var ffVersion = require('../../../loader/firefox-version')
var dataSize = require('ds')
var responseSizeFromXhr = require('./response-size')
var eventListenerOpts = require('event-listener-opts')
var recordSupportability = require('metrics').recordSupportability

var origRequest = NREUM.o.REQ
var origXHR = window.XMLHttpRequest
Expand Down Expand Up @@ -264,6 +265,9 @@ ee.on('fetch-start', function (fetchArguments, dtPayload) {
}
addUrl(this, url)

// Do not generate telemetry for Data URL requests because they don't behave like other network requests
if (this.params.protocol === 'data') return

var method = ('' + ((target && target instanceof origRequest && target.method) ||
opts.method || 'GET')).toUpperCase()
this.params.method = method
Expand All @@ -278,6 +282,13 @@ ee.on('fetch-done', function (err, res) {
if (!this.params) {
this.params = {}
}

// Do not generate telemetry for Data URL requests because they don't behave like other network requests
if (this.params.protocol === 'data') {
recordSupportability('Ajax/DataUrl/Excluded')
return
}

this.params.status = res ? res.status : 0

// convert rxSize to a number
Expand Down Expand Up @@ -307,6 +318,12 @@ function end (xhr) {
xhr.removeEventListener(handlers[i], this.listener, false)
}

// Do not generate telemetry for Data URL requests because they don't behave like other network requests
if (params.protocol && params.protocol === 'data') {
recordSupportability('Ajax/DataUrl/Excluded')
return
}

if (params.aborted) return
metrics.duration = loader.now() - this.startTime
if (!this.loadCaptureCalled && xhr.readyState === 4) {
Expand Down
7 changes: 7 additions & 0 deletions feature/xhr/instrument/parse-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ module.exports = function parseUrl (url) {
return stringsToParsedUrls[url]
}

// Return if URL is a data URL, parseUrl assumes urls are http/https
if ((url || '').indexOf('data:') === 0) {
return {
protocol: 'data'
}
}

var urlEl = document.createElement('a')
var location = window.location
var ret = {}
Expand Down
88 changes: 88 additions & 0 deletions tests/browser/xhr/data-url.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

var test = require('../../../tools/jil/browser-test')
var ee = require('ee')
var handleEE = ee.get('handle')

var hasXhr = window.XMLHttpRequest && XMLHttpRequest.prototype && XMLHttpRequest.prototype.addEventListener

require('../../../feature/xhr/instrument')

test('XHR request for Data URL does not generate telemetry', function(t) {
if (!hasXhr) {
t.skip('XHR is not supported in this browser')
t.end()
return
}

ee.addEventListener('send-xhr-start', validate)
handleEE.addEventListener('xhr', failCase)

try {
var xhr = new XMLHttpRequest()
xhr.open('GET', 'data:,dataUrl')
xhr.send()
} catch (e) {
ee.removeEventListener('send-xhr-start', validate)
handleEE.removeEventListener('xhr', failCase)

t.skip('XHR with data URL not supported in this browser')
t.end()
return
}

t.plan(2)

function validate (args, xhr) {
t.equals(this.params.protocol, 'data', 'XHR Data URL request recorded')
setTimeout(() => {
ee.removeEventListener('send-xhr-start', validate)
handleEE.removeEventListener('xhr', failCase)

t.pass('XHR Data URL request did not generate telemetry')
t.end()
}, 100)
}

function failCase (params, metrics, start) {
ee.removeEventListener('send-xhr-start', validate)
handleEE.removeEventListener('xhr', failCase)

t.fail('XHR Data URL request should not generate telemetry')
}
})

test('Data URL Fetch requests do not generate telemetry', function(t) {
if (!window.fetch) {
t.pass('fetch is not supported in this browser')
t.end()
return
}

handleEE.addEventListener('xhr', failCase)

ee.addEventListener('fetch-done', validate)

fetch('data:,dataUrl')

function validate () {
t.equals(this.params.protocol, 'data', 'Fetch data URL request recorded')

setTimeout(() => {
handleEE.removeEventListener('xhr', failCase)
ee.removeEventListener('fetch-done', validate)

t.pass('Fetch data URL request did not generate telemetry')
t.end()
}, 100)
}

function failCase(params, metrics, start) {
t.fail('Data URL Fetch requests should not generate telemetry')
handleEE.removeEventListener('xhr', failCase)
ee.removeEventListener('fetch-done', validate)
}
})
6 changes: 6 additions & 0 deletions tests/browser/xhr/parse-url.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ function xhr_tests () {
port: location.port,
sameOrigin: true
}
},
{
url: 'data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==',
results: {
protocol: 'data'
}
}
]

Expand Down

0 comments on commit b4a8e50

Please sign in to comment.