From 40d6df9c7ee3e122578b097dd2a42d7bf498ef7c Mon Sep 17 00:00:00 2001 From: Owen Pearson Date: Fri, 27 May 2022 13:29:52 +0100 Subject: [PATCH 1/3] Make web platform use window as `global` when undefined --- src/platform/web/platform.ts | 37 +++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/platform/web/platform.ts b/src/platform/web/platform.ts index a37070c292..825544d95f 100644 --- a/src/platform/web/platform.ts +++ b/src/platform/web/platform.ts @@ -1,6 +1,9 @@ import msgpack from './lib/util/msgpack'; import { IPlatform, TypedArray } from '../../common/types/IPlatform'; +// Workaround for salesforce lightning locker compat +const globalOrWindow = global || window; + declare var msCrypto: typeof crypto; // for IE11 if (typeof Window === 'undefined' && typeof WorkerGlobalScope === 'undefined') { @@ -13,12 +16,12 @@ function allowComet() { /* xhr requests from local files are unreliable in some browsers, such as Chrome 65 and higher -- see eg * https://stackoverflow.com/questions/49256429/chrome-65-unable-to-make-post-requests-from-local-files-to-flask * So if websockets are supported, then just forget about comet transports and use that */ - const loc = global.location; - return !global.WebSocket || !loc || !loc.origin || loc.origin.indexOf('http') > -1; + const loc = globalOrWindow.location; + return !globalOrWindow.WebSocket || !loc || !loc.origin || loc.origin.indexOf('http') > -1; } -const userAgent = global.navigator && global.navigator.userAgent.toString(); -const currentUrl = global.location && global.location.href; +const userAgent = globalOrWindow.navigator && globalOrWindow.navigator.userAgent.toString(); +const currentUrl = globalOrWindow.location && globalOrWindow.location.href; const Platform: IPlatform = { agent: 'browser', @@ -27,36 +30,36 @@ const Platform: IPlatform = { currentUrl: currentUrl, noUpgrade: userAgent && !!userAgent.match(/MSIE\s8\.0/), binaryType: 'arraybuffer', - WebSocket: global.WebSocket, - xhrSupported: global.XMLHttpRequest && 'withCredentials' in new XMLHttpRequest(), + WebSocket: globalOrWindow.WebSocket, + xhrSupported: globalOrWindow.XMLHttpRequest && 'withCredentials' in new XMLHttpRequest(), jsonpSupported: typeof document !== 'undefined', allowComet: allowComet(), streamingSupported: true, useProtocolHeartbeats: true, createHmac: null, msgpack: msgpack, - supportsBinary: !!global.TextDecoder, + supportsBinary: !!globalOrWindow.TextDecoder, preferBinary: false, - ArrayBuffer: global.ArrayBuffer, - atob: global.atob, + ArrayBuffer: globalOrWindow.ArrayBuffer, + atob: globalOrWindow.atob, nextTick: - typeof global.setImmediate !== 'undefined' - ? global.setImmediate.bind(global) + typeof globalOrWindow.setImmediate !== 'undefined' + ? globalOrWindow.setImmediate.bind(globalOrWindow) : function (f: () => void) { setTimeout(f, 0); }, - addEventListener: global.addEventListener, + addEventListener: globalOrWindow.addEventListener, inspect: JSON.stringify, stringByteSize: function (str: string) { /* str.length will be an underestimate for non-ascii strings. But if we're * in a browser too old to support TextDecoder, not much we can do. Better * to underestimate, so if we do go over-size, the server will reject the * message */ - return (global.TextDecoder && new global.TextEncoder().encode(str).length) || str.length; + return (globalOrWindow.TextDecoder && new globalOrWindow.TextEncoder().encode(str).length) || str.length; }, - TextEncoder: global.TextEncoder, - TextDecoder: global.TextDecoder, - Promise: global.Promise, + TextEncoder: globalOrWindow.TextEncoder, + TextDecoder: globalOrWindow.TextDecoder, + Promise: globalOrWindow.Promise, getRandomValues: (function (crypto) { if (crypto === undefined) { return undefined; @@ -67,7 +70,7 @@ const Platform: IPlatform = { callback(null); } }; - })(global.crypto || msCrypto), + })(globalOrWindow.crypto || msCrypto), }; export default Platform; From 4f179a85ead91cbc98ebc7e32c9e510a5f550cfe Mon Sep 17 00:00:00 2001 From: Owen Pearson Date: Fri, 27 May 2022 13:37:44 +0100 Subject: [PATCH 2/3] Make logger and jsonptransport use window when `global` is undefined --- src/common/lib/util/logger.ts | 9 +++++++-- src/platform/web/lib/transport/jsonptransport.ts | 7 ++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/common/lib/util/logger.ts b/src/common/lib/util/logger.ts index 6c53d0103e..61e974f257 100644 --- a/src/common/lib/util/logger.ts +++ b/src/common/lib/util/logger.ts @@ -6,6 +6,9 @@ export type LoggerOptions = { }; type LoggerFunction = (...args: string[]) => void; +// Workaround for salesforce lightning locker compatibility +let globalOrWindow = global || window; + enum LogLevels { None = 0, Error = 1, @@ -44,7 +47,9 @@ const getDefaultLoggers = (): [Function, Function] => { /* Can't just check for console && console.log; fails in IE <=9 */ if ( (typeof Window === 'undefined' && typeof WorkerGlobalScope === 'undefined') /* node */ || - (global.console && global.console.log && typeof global.console.log.apply === 'function') /* sensible browsers */ + (globalOrWindow.console && + globalOrWindow.console.log && + typeof globalOrWindow.console.log.apply === 'function') /* sensible browsers */ ) { consoleLogger = function (...args: unknown[]) { console.log.apply(console, args); @@ -54,7 +59,7 @@ const getDefaultLoggers = (): [Function, Function] => { console.warn.apply(console, args); } : consoleLogger; - } else if (global.console && (global.console.log as unknown)) { + } else if (globalOrWindow.console && (globalOrWindow.console.log as unknown)) { /* IE <= 9 with the console open -- console.log does not * inherit from Function, so has no apply method */ consoleLogger = errorLogger = function () { diff --git a/src/platform/web/lib/transport/jsonptransport.ts b/src/platform/web/lib/transport/jsonptransport.ts index 5473aeca13..233c8a634d 100644 --- a/src/platform/web/lib/transport/jsonptransport.ts +++ b/src/platform/web/lib/transport/jsonptransport.ts @@ -12,14 +12,15 @@ import ConnectionManager, { TransportParams } from 'common/lib/transport/connect import { TryConnectCallback } from 'common/lib/transport/transport'; import XHRStates from 'common/constants/XHRStates'; -declare const global: { +// Workaround for salesforce lightning locker compatibility +let globalOrWindow = (global || window) as unknown as { _ablyjs_jsonp: Record; JSONPTransport: typeof JSONPTransport; }; const noop = function () {}; /* Can't just use window.Ably, as that won't exist if using the commonjs version. */ -const _: Record = (global._ablyjs_jsonp = {}); +const _: Record = (globalOrWindow._ablyjs_jsonp = {}); /* express strips out parantheses from the callback! * Kludge to still alow its responses to work, while not keeping the @@ -263,7 +264,7 @@ export class Request extends EventEmitter { } export default function (connectionManager: typeof ConnectionManager): typeof JSONPTransport { - global.JSONPTransport = JSONPTransport; + globalOrWindow.JSONPTransport = JSONPTransport; if (JSONPTransport.isAvailable()) { connectionManager.supportedTransports[shortName] = JSONPTransport; } From 8e9ffac1cbfc09d0797d8e0ad9575536bd2ea09d Mon Sep 17 00:00:00 2001 From: Owen Pearson Date: Thu, 9 Jun 2022 16:20:16 +0100 Subject: [PATCH 3/3] Use optional chaining to check for `console.log` existence --- src/common/lib/util/logger.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/common/lib/util/logger.ts b/src/common/lib/util/logger.ts index 61e974f257..6edcff7fbd 100644 --- a/src/common/lib/util/logger.ts +++ b/src/common/lib/util/logger.ts @@ -47,9 +47,7 @@ const getDefaultLoggers = (): [Function, Function] => { /* Can't just check for console && console.log; fails in IE <=9 */ if ( (typeof Window === 'undefined' && typeof WorkerGlobalScope === 'undefined') /* node */ || - (globalOrWindow.console && - globalOrWindow.console.log && - typeof globalOrWindow.console.log.apply === 'function') /* sensible browsers */ + typeof globalOrWindow?.console?.log?.apply === 'function' /* sensible browsers */ ) { consoleLogger = function (...args: unknown[]) { console.log.apply(console, args); @@ -59,7 +57,7 @@ const getDefaultLoggers = (): [Function, Function] => { console.warn.apply(console, args); } : consoleLogger; - } else if (globalOrWindow.console && (globalOrWindow.console.log as unknown)) { + } else if (globalOrWindow?.console.log as unknown) { /* IE <= 9 with the console open -- console.log does not * inherit from Function, so has no apply method */ consoleLogger = errorLogger = function () {