Skip to content

Commit

Permalink
remove isBrowser, look for global members instead
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengum committed Jan 14, 2020
1 parent 28d195c commit f643429
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @module botframework-streaming
*/
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/

export const doesGlobalFileReaderExist = new Function('try {return typeof FileReader !== "undefined" && FileReader !== null;}catch(e){ return false;}');
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @module botframework-streaming
*/
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/

export const doesGlobalWebSocketExist = new Function('try {return typeof WebSocket !== "undefined" && WebSocket !== null;}catch(e){ return false;}');
3 changes: 2 additions & 1 deletion libraries/botframework-streaming/src/utilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
* Licensed under the MIT License.
*/

export * from './isBrowser';
export * from './doesGlobalFileReaderExist';
export * from './doesGlobalWebSocketExist';
export * from './protocol-base';
9 changes: 0 additions & 9 deletions libraries/botframework-streaming/src/utilities/isBrowser.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@
* Licensed under the MIT License.
*/
import { IBrowserFileReader, IBrowserWebSocket, ISocket } from '../interfaces';
import { isBrowser } from '../utilities';
import { doesGlobalFileReaderExist, doesGlobalWebSocketExist } from '../utilities';

const createWebSocket = function(url: string): IBrowserWebSocket {
if (!url) {
throw new TypeError('Unable to create WebSocket without url.');
}
if (isBrowser()) {
if (doesGlobalWebSocketExist()) {
return new Function(`return new WebSocket('${ url }');`)();
}
throw new ReferenceError('Incorrect environment detected for BrowserWebSocket. Unable to create backing WebSocket for BrowserWebSocket.');
throw new ReferenceError('Unable to find global.WebSocket which is required for constructing a BrowserWebSocket.');
};

const createFileReader = function(): IBrowserFileReader {
if (isBrowser()) {
if (doesGlobalFileReaderExist()) {
return new Function(`return new FileReader();`)();
}
throw new ReferenceError('Incorrect environment detected for BrowserWebSocket. Unable to create backing FileReader for BrowserWebSocket.');
throw new ReferenceError('Unable to find global.FileReader. Unable to create FileReader for BrowserWebSocket.');
};

export class BrowserWebSocket implements ISocket {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from '../payloadTransport';
import { BrowserWebSocket } from './browserWebSocket';
import { NodeWebSocket } from './nodeWebSocket';
import { isBrowser } from '../utilities';
import { doesGlobalWebSocketExist } from '../utilities';
import { WebSocketTransport } from './webSocketTransport';
import { IStreamingTransportClient, IReceiveResponse } from '../interfaces';

Expand Down Expand Up @@ -60,7 +60,7 @@ export class WebSocketClient implements IStreamingTransportClient {
* @returns A promise that will not resolve until the client stops listening for incoming messages.
*/
public async connect(): Promise<void> {
if (isBrowser()) {
if (doesGlobalWebSocketExist()) {
const ws = new BrowserWebSocket();
await ws.connect(this._url);
const transport = new WebSocketTransport(ws);
Expand Down
43 changes: 39 additions & 4 deletions libraries/botframework-streaming/tests/InternalUtilities.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,45 @@
const { isBrowser } = require('../lib/utilities');
const { doesGlobalFileReaderExist, doesGlobalWebSocketExist } = require('../lib/utilities');
const chai = require('chai');
const expect = chai.expect;

describe('Internal Utilities', () => {
it('isBrowser() should return false when being run via Node.js', () => {
// More specifically, it should return false when the global object is not window.
expect(isBrowser()).to.be.false;
it('doesGlobalWebSocketExist() should return true if global.WebSocket is truthy', () => {
global.WebSocket = {};
try {
expect(doesGlobalWebSocketExist()).to.be.true;
} finally {
global.WebSocket = undefined;
}
});

it('doesGlobalWebSocketExist() should return false if global.WebSocket is null or undefined', () => {
expect(doesGlobalWebSocketExist()).to.be.false;

global.WebSocket = null;
try {
expect(doesGlobalWebSocketExist()).to.be.false;
} finally {
global.WebSocket = undefined;
}
});

it('doesGlobalFileReaderExist() should return true if global.FileReader is truthy', () => {
global.FileReader = {};
try {
expect(doesGlobalFileReaderExist()).to.be.true;
} finally {
global.FileReader = undefined;
}
});

it('doesGlobalFileReaderExist() should return false if global.FileReader is null or undefined', () => {
expect(doesGlobalFileReaderExist()).to.be.false;

global.FileReader = null;
try {
expect(doesGlobalFileReaderExist()).to.be.false;
} finally {
global.FileReader = undefined;
}
});
});

0 comments on commit f643429

Please sign in to comment.