Skip to content

fix: Multi fill + test #2048

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- [browser] fix: Fixed a bug if Sentry was initialized multiple times: Fix #2043
- [browser] ref: Mangle more stuff, reduce bundle size
- [node] fix: Expose lastEventId method

Expand Down
6 changes: 6 additions & 0 deletions packages/browser/test/manual/npm-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,10 @@ function runTests() {
const scriptEl = window.document.createElement('script');
scriptEl.textContent = myLibrary;
window.document.body.appendChild(scriptEl);

// Testing https://github.com/getsentry/sentry-javascript/issues/2043
const scriptEl2 = window.document.createElement('script');
scriptEl2.textContent = myLibrary;
window.document.body.appendChild(scriptEl2);
// ------------------------------------------------------------------
}
6 changes: 6 additions & 0 deletions packages/browser/test/manual/test-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,9 @@ Sentry.withScope(scope => {
Sentry.captureException(new TypeError('bar'));
Sentry.captureMessage('baz');
});

var xhr = new XMLHttpRequest();
xhr.onload = () => console.log('loaded'); // This throws error
// xhr.addEventListener("load", () => console.log('loaded')); This does not throw error
xhr.open('GET', 'https://httpbin.org/get');
xhr.send();
35 changes: 20 additions & 15 deletions packages/utils/src/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,26 @@ export function fill(source: { [key: string]: any }, name: string, replacement:
// otherwise it'll throw "TypeError: Object.defineProperties called on non-object"
// tslint:disable-next-line:strict-type-predicates
if (typeof wrapped === 'function') {
wrapped.prototype = wrapped.prototype || {};
Object.defineProperties(wrapped, {
__sentry__: {
enumerable: false,
value: true,
},
__sentry_original__: {
enumerable: false,
value: original,
},
__sentry_wrapped__: {
enumerable: false,
value: wrapped,
},
});
try {
wrapped.prototype = wrapped.prototype || {};
Object.defineProperties(wrapped, {
__sentry__: {
enumerable: false,
value: true,
},
__sentry_original__: {
enumerable: false,
value: original,
},
__sentry_wrapped__: {
enumerable: false,
value: wrapped,
},
});
} catch (_Oo) {
// This can throw if multiple fill happens on a global object like XMLHttpRequest
// Fixes https://github.com/getsentry/sentry-javascript/issues/2043
}
}

source[name] = wrapped;
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/test/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('fill()', () => {
expect.assertions(3);
});

test('mulitple fills calls all functions', () => {
test('multiple fills calls all functions', () => {
const source = {
foo: (): number => 42,
};
Expand Down