Method console.warn invoked with wrong context
See original GitHub issueHi,
We’re running Autobahn JS on various terminal types, including Samsung and LG Smart TVs (Chromium-based browsers). With these Smart TVs, when the established connection is closed (for example after stopping the Crossbar server), we get JS errors like:
Uncaught TypeError: Illegal invocation
TypeError: Type error
In this case the AB connection’s onclose
handler is not called, and thereby our code is not aware that reconnection must be attempted.
This issue appeared from ABJS v19.10.1.
Connection handler self._transport.onclose
calls:
log.warn("connection closed", reason, details);
https://github.com/crossbario/autobahn-js/blob/v19.10.1/packages/autobahn/lib/connection.js#L343
The log module defines its warn
method as a direct reference to DOM console.warn
:
var warn = console.warn;
exports.warn = warn;
https://github.com/crossbario/autobahn-js/blob/master/packages/autobahn/lib/log.js#L23
Execution of AB’s log.warn
does not cause issue on desktop browsers (Chrome, Firefox, Edge, IE11). However it fails on the affected Smart TVs because the warn
Function
is executed with a this
context which is not the console
object.
The Smart TV browsers may have a specific behaviour requiring this context (like redirecting the browser logs to an internal debugging system…).
To work on such platforms, Autobahn log module code should be something like:
var warn = console.warn.bind(console);
Note: AB’s log.debug
method should not be affected by this issue, as its code uses the console context.
Beyond the specific case of these Smart TV platforms, from a more general OOP point of view, the method of an object’s instance should not be called outside of the context of this instance.
Thanks
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (1 by maintainers)
@vla74 @hmistry-lz I have released autobahn-js 20.4.1, with contains the fix for this bug as well. Kindly try that.
You’re welcome.
log.debug
does not cause any exception on the affected Smart TVs, whilelog.warn
does.debug
function:Function.prototype.bind
normally requires an ECMAScript 5 compatible browser (http://kangax.github.io/compat-table/es5/#test-Function.prototype.bind), however I think that there’s a polyfill embedded in ABJS (https://github.com/crossbario/autobahn-js/blob/master/packages/autobahn/lib/polyfill/function.js).BTW, for the sake of consistency, let me suggest that
debug
andwarn
functions are declared the same way (so, probably using thebind
method for both).