Skip to content
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Fixed

- (plugin-inline-script-content) Fix strict mode compatibility by replacing `arguments` usage with rest parameters [#2711](https://github.com/bugsnag/bugsnag-js/pull/2711)

### Added

- (delivery-react-native) Handle request and response parameters [#2667](https://github.com/bugsnag/bugsnag-js/pull/2667)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,9 @@ module.exports = (doc = document, win = window) => ({
})

function __traceOriginalScript (fn, callbackAccessor, alsoCallOriginal = false) {
return function () {
return function (...args) {
// this is required for removeEventListener to remove anything added with
// addEventListener before the functions started being wrapped by Bugsnag
const args = [].slice.call(arguments)
try {
const cba = callbackAccessor(args)
const cb = cba.get()
Expand All @@ -142,14 +141,14 @@ module.exports = (doc = document, win = window) => ({
// this function mustn't be annonymous due to a bug in the stack
// generation logic, meaning it gets tripped up
// see: https://github.com/stacktracejs/stack-generator/issues/6
cb.__trace__ = function __trace__ () {
cb.__trace__ = function __trace__ (...cbArgs) {
// set the script that called this function
updateLastScript(script)
// immediately unset the currentScript synchronously below, however
// if this cb throws an error the line after will not get run so schedule
// an almost-immediate aysnc update too
_setTimeout(function () { updateLastScript(null) }, 0)
const ret = cb.apply(this, arguments)
const ret = cb.apply(this, cbArgs)
updateLastScript(null)
return ret
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,42 @@ Lorem ipsum dolor sit amet.
expect(payloads.length).toEqual(1)
expect(payloads[0].events[0]._metadata.script).not.toBeDefined()
})

it('supports rest parameters correctly in wrapped callbacks', () => {
const scriptContent = 'console.log("test")'
const document = {
scripts: [{ innerHTML: scriptContent }],
currentScript: { innerHTML: scriptContent },
documentElement: {
outerHTML: `<script>${scriptContent}</script>`
}
} as unknown as Document
function Window () {}
Window.prototype = {
addEventListener: function () {},
removeEventListener: function () {}
}
const window = {
location: { href: 'https://app.bugsnag.com/errors' }
} as unknown as Window &typeof globalThis

Object.setPrototypeOf(window, Window.prototype)
// @ts-ignore
window.Window = Window

const testCallback = function () {
console.log('Event received')
}

const client = new Client({ apiKey: 'API_KEY_YEAH' }, undefined, [plugin(document, window)])

// Add event listener with wrapped callback
// @ts-ignore
window.addEventListener('custom', testCallback)

// Verify the callback was wrapped and can handle arguments correctly
// @ts-ignore
expect(testCallback.__trace__).toBeDefined()
expect(client).toBe(client)
})
})
Loading