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

[Cherry-pick to master] remove "dom" from bf-streaming tsconfig and refactor code #1579

Merged
merged 1 commit into from
Jan 15, 2020
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @module botframework-streaming
*/
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/

/**
* Partially represents a FileReader from the W3C FileAPI Working Draft.
* For more information, see https://w3c.github.io/FileAPI/#APIASynch.
*
* This interface supports the framework and is not intended to be called directly for your code.
*/
export interface IBrowserFileReader {
result: any;
onload: (event: any) => void;
readAsArrayBuffer: (blobOrFile: any) => void;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @module botframework-streaming
*/
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/

/**
* Partially represents a WebSocket from the HTML Living Standard.
* For more information, see https://html.spec.whatwg.org/multipage/web-sockets.html.
*
* This interface supports the framework and is not intended to be called directly for your code.
*/
export interface IBrowserWebSocket {
onclose: (event: any) => void;
onerror: (event: any) => void;
onmessage: (event: any) => void;
onopen: (event: any) => void;
readyState: number;

close(): void;
send(buffer: any): void;
}
2 changes: 2 additions & 0 deletions libraries/botframework-streaming/src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* Licensed under the MIT License.
*/

export * from './IBrowserFileReader';
export * from './IBrowserWebSocket';
export * from './INodeBuffer';
export * from './INodeIncomingMessage';
export * from './INodeSocket';
Expand Down
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;}');
2 changes: 2 additions & 0 deletions libraries/botframework-streaming/src/utilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
* Licensed under the MIT License.
*/

export * from './doesGlobalFileReaderExist';
export * from './doesGlobalWebSocketExist';
export * from './protocol-base';
30 changes: 24 additions & 6 deletions libraries/botframework-streaming/src/webSocket/browserWebSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,35 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { ISocket } from '../interfaces/ISocket';
import { IBrowserFileReader, IBrowserWebSocket, ISocket } from '../interfaces';
import { doesGlobalFileReaderExist, doesGlobalWebSocketExist } from '../utilities';

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

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

export class BrowserWebSocket implements ISocket {
private webSocket: WebSocket;
private webSocket: IBrowserWebSocket;

/**
* Creates a new instance of the [BrowserWebSocket](xref:botframework-streaming.BrowserWebSocket) class.
*
* @param socket The socket object to build this connection on.
*/
public constructor(socket?: WebSocket) {
public constructor(socket?: IBrowserWebSocket) {
if (socket) {
this.webSocket = socket;
}
Expand All @@ -31,7 +49,7 @@ export class BrowserWebSocket implements ISocket {
let rejector;

if (!this.webSocket) {
this.webSocket = new WebSocket(serverAddress);
this.webSocket = createWebSocket(serverAddress);
}

this.webSocket.onerror = (e): void => {
Expand Down Expand Up @@ -79,11 +97,11 @@ export class BrowserWebSocket implements ISocket {
const bufferKey: string = 'buffer';
let packets = [];
this.webSocket.onmessage = (evt): void => {
let fileReader = new FileReader();
let fileReader = createFileReader();
let queueEntry = {buffer: null};
packets.push(queueEntry);
fileReader.onload = (e): void => {
let t: FileReader = e.target as FileReader;
let t = e.target as IBrowserFileReader;
queueEntry[bufferKey] = t.result;
if (packets[0] === queueEntry) {
while(0 < packets.length && packets[0][bufferKey]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from '../payloadTransport';
import { BrowserWebSocket } from './browserWebSocket';
import { NodeWebSocket } from './nodeWebSocket';
import { doesGlobalWebSocketExist } from '../utilities';
import { WebSocketTransport } from './webSocketTransport';
import { IStreamingTransportClient, IReceiveResponse } from '../interfaces';

Expand Down Expand Up @@ -59,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 (typeof WebSocket !== 'undefined') {
if (doesGlobalWebSocketExist()) {
const ws = new BrowserWebSocket();
await ws.connect(this._url);
const transport = new WebSocketTransport(ws);
Expand Down
45 changes: 45 additions & 0 deletions libraries/botframework-streaming/tests/InternalUtilities.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const { doesGlobalFileReaderExist, doesGlobalWebSocketExist } = require('../lib/utilities');
const chai = require('chai');
const expect = chai.expect;

describe('Internal Utilities', () => {
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;
}
});
});
2 changes: 1 addition & 1 deletion libraries/botframework-streaming/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "es6",
"lib": ["es2015", "dom"],
"lib": ["es2015"],
"module": "commonjs",
"declaration": true,
"sourceMap": true,
Expand Down