Skip to content

Commit b0ea98b

Browse files
committed
test: Tests for blob handling
1 parent de0db41 commit b0ea98b

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

test/vendor/tracekit.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
var TraceKit = require('../../vendor/TraceKit/tracekit');
55
var utils = require('../../src/utils');
66
var supportsErrorEvent = utils.supportsErrorEvent;
7+
var sinon = require('sinon');
78

89
describe('TraceKit', function() {
910
describe('stacktrace info', function() {
@@ -81,6 +82,52 @@ describe('TraceKit', function() {
8182
assert.equal(trace.stack[2].line, 26);
8283
assert.equal(trace.stack[2].column, 5);
8384
});
85+
86+
it('should update url based on sourcemap suffix in blob: based frames if full url available', function() {
87+
var server = sinon.createFakeServer();
88+
server.respondImmediately = true;
89+
server.respondWith('GET', 'blob:http://localhost:8080/some-blob', [
90+
200,
91+
{'Content-Type': 'application/javascript'},
92+
'just a random stream of bytes, as we care only about the sourcemaps suffix there\n' +
93+
'oh, here it comes! //# sourceMappingURL=http://awesome.com/file.js.map'
94+
]);
95+
96+
var stack_str =
97+
'Error: test\n' +
98+
' at Error (native)\n' +
99+
' at s (blob:http://localhost:8080/some-blob:31:29146)';
100+
101+
var mock_err = {stack: stack_str};
102+
var trace = TraceKit.computeStackTrace.computeStackTraceFromStackProp(mock_err);
103+
104+
assert.equal(trace.stack[1].url, 'http://awesome.com/file.js');
105+
106+
server.restore();
107+
});
108+
109+
it('should update url based on sourcemap suffix in blob: based frames if relative url available, by adding location.origin to it', function() {
110+
var server = sinon.createFakeServer();
111+
server.respondImmediately = true;
112+
server.respondWith('GET', 'blob:http://localhost:8080/some-blob', [
113+
200,
114+
{'Content-Type': 'application/javascript'},
115+
'just a random stream of bytes, as we care only about the sourcemaps suffix there\n' +
116+
'oh, here it comes! //# sourceMappingURL=~/awesome.com/file.js.map'
117+
]);
118+
119+
var stack_str =
120+
'Error: test\n' +
121+
' at Error (native)\n' +
122+
' at s (blob:http://localhost:8080/some-blob:31:29146)';
123+
124+
var mock_err = {stack: stack_str};
125+
var trace = TraceKit.computeStackTrace.computeStackTraceFromStackProp(mock_err);
126+
127+
assert.equal(trace.stack[1].url, 'http://localhost:9876/awesome.com/file.js');
128+
129+
server.restore();
130+
});
84131
});
85132

86133
describe('.computeStackTrace', function() {

vendor/TraceKit/tracekit.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@ var ERROR_TYPES_RE = /^(?:[Uu]ncaught (?:exception: )?)?(?:((?:Eval|Internal|Ran
3030

3131
function getLocationHref() {
3232
if (typeof document === 'undefined' || document.location == null) return '';
33-
3433
return document.location.href;
3534
}
3635

36+
function getLocationOrigin() {
37+
if (typeof document === 'undefined' || document.location == null) return '';
38+
return document.location.origin;
39+
}
40+
3741
/**
3842
* TraceKit.report: cross-browser processing of unhandled exceptions
3943
*
@@ -451,9 +455,9 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
451455
xhr.open('GET', element.url, false);
452456
xhr.send(null);
453457

454-
// If we failed to download the source, skip the element.
458+
// If we failed to download the source, skip this patch
455459
if (xhr.status === 200) {
456-
var source = xhr.responseText;
460+
var source = xhr.responseText || '';
457461

458462
// We trim the source down to the last 300 characters as sourceMappingURL is always at the end of the file.
459463
// Why 300? To be in line with: https://github.com/getsentry/sentry/blob/4af29e8f2350e20c28a6933354e4f42437b4ba42/src/sentry/lang/javascript/processor.py#L164-L175
@@ -463,13 +467,13 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
463467
var sourceMaps = source.match(/\/\/# sourceMappingURL=(.*)$/);
464468

465469
// If we don't find a source map comment or we find more than one, continue on to the next element.
466-
if (!sourceMaps) {
470+
if (sourceMaps) {
467471
var sourceMapAddress = sourceMaps[1];
468472

469473
// Now we check to see if it's a relative URL.
470474
// If it is, convert it to an absolute one.
471475
if (sourceMapAddress.charAt(0) === '~') {
472-
sourceMapAddress = window.location.origin + sourceMapAddress.slice(1);
476+
sourceMapAddress = getLocationOrigin() + sourceMapAddress.slice(1);
473477
}
474478

475479
// Now we strip the '.map' off of the end of the URL and update the

0 commit comments

Comments
 (0)