Skip to content
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

Salesforce lightning compat #993

Merged
merged 3 commits into from
Jun 9, 2022
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
7 changes: 5 additions & 2 deletions src/common/lib/util/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -44,7 +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 */ ||
(global.console && global.console.log && typeof global.console.log.apply === 'function') /* sensible browsers */
typeof globalOrWindow?.console?.log?.apply === 'function' /* sensible browsers */
) {
consoleLogger = function (...args: unknown[]) {
console.log.apply(console, args);
Expand All @@ -54,7 +57,7 @@ const getDefaultLoggers = (): [Function, Function] => {
console.warn.apply(console, args);
}
: consoleLogger;
} else if (global.console && (global.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 () {
Expand Down
7 changes: 4 additions & 3 deletions src/platform/web/lib/transport/jsonptransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>;
JSONPTransport: typeof JSONPTransport;
};

const noop = function () {};
/* Can't just use window.Ably, as that won't exist if using the commonjs version. */
const _: Record<string, unknown> = (global._ablyjs_jsonp = {});
const _: Record<string, unknown> = (globalOrWindow._ablyjs_jsonp = {});

/* express strips out parantheses from the callback!
* Kludge to still alow its responses to work, while not keeping the
Expand Down Expand Up @@ -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;
}
Expand Down
37 changes: 20 additions & 17 deletions src/platform/web/platform.ts
Original file line number Diff line number Diff line change
@@ -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') {
Expand All @@ -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',
Expand All @@ -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;
Expand All @@ -67,7 +70,7 @@ const Platform: IPlatform = {
callback(null);
}
};
})(global.crypto || msCrypto),
})(globalOrWindow.crypto || msCrypto),
};

export default Platform;