fix(rrweb-plugin-console-record): bind wrapped console methods to the logger#1904
Open
yashrao2607 wants to merge 2 commits into
Open
fix(rrweb-plugin-console-record): bind wrapped console methods to the logger#1904yashrao2607 wants to merge 2 commits into
yashrao2607 wants to merge 2 commits into
Conversation
The wrapped console method was an arrow function, so `original.apply(this, args)` never used the logger as `this` — arrow functions ignore the call-site receiver entirely and use the enclosing (non-method) call's `this`, which is undefined in this module's strict mode. Native console implementations can throw "Illegal invocation" when called with the wrong receiver, which is what crashed the reported Chrome extension content script. Bind explicitly to `_logger` instead, in both the normal call path and the internal-error fallback that has the same issue. Fixes rrweb-io#1772
🦋 Changeset detectedLatest commit: 45c77df The changes in this PR will be included in the next version bump. This PR includes changesets to release 22 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
getRecordConsolePluginpatches each console method with an arrow function that callsoriginal.apply(this, args). Arrow functions ignore the call-site receiver entirely and use their lexically enclosingthis— here that's thereplace()function, which is invoked as a plain function call, sothisisundefinedin this module's strict mode. In other wordsthiswas never the logger, regardless of how the wrapped method was actually called.Native console implementations can throw
TypeError: Illegal invocationwhen a method is applied with the wrong receiver. That's consistent with the reported behavior: importing this plugin silently crashed a Chrome extension's content script (isolated-world consoles appear to enforce this more strictly than a page's main-world console, which is likely why this wasn't caught earlier).Fixed by binding explicitly to
_logger(the actual logger object passed intoreplace()), both in the normal call path and in the internal-error fallback (catchblock), which had the identical bug (original('rrweb logger error:', ...), an unbound plain call).Test plan
test/this-binding.test.ts: patches a fake logger whose method recordsthis, calls it, and assertsthisis the logger instance. Verified this test fails against the pre-fix code (this === undefined) and passes after the fix.yarn vitest runfor this package: all existing tests (including the puppeteer-basedtest/index.test.tsintegration tests) still pass.tsc -noEmitclean.Fixes #1772