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

Adding our own partial versions of node's 'net' Server and Socket classes #1756

Closed
wants to merge 2 commits into from
Closed
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
17 changes: 17 additions & 0 deletions libraries/botframework-streaming/src/interfaces/INodeServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @module botframework-streaming
*/
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/

/**
* Represents a Server from the `net` module in Node.js.
*
* This interface supports the framework and is not intended to be called directly for your code.
*/
export interface INodeServer {
listen(path: string, backlog?: number, listeningListener?: Function): this
close(callback?: (err?: Error) => void): this;
}
1 change: 1 addition & 0 deletions libraries/botframework-streaming/src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export * from './IBrowserFileReader';
export * from './IBrowserWebSocket';
export * from './INodeBuffer';
export * from './INodeIncomingMessage';
export * from './INodeServer';
export * from './INodeSocket';
export * from './IReceiveRequest';
export * from './IReceiveResponse';
Expand Down
24 changes: 21 additions & 3 deletions libraries/botframework-streaming/src/namedPipe/namedPipeServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,25 @@ import {
PayloadSender
} from '../payloadTransport';
import { NamedPipeTransport } from './namedPipeTransport';
import { IStreamingTransportServer, IReceiveResponse } from '../interfaces';
import { IStreamingTransportServer, IReceiveResponse, INodeServer, INodeSocket } from '../interfaces';
import { doesGlobalServerExist } from '../utilities';

// function(callback): INodeServer <-- type hint
// says I'm "missing the following properties from type 'Server': address, getConnections, ref, unref, and 18 more"
// when I try adding type hint
const createNodeServer = function(callback: (socket: INodeSocket) => void) {
if (!callback) {
throw new TypeError('Unable to create NodeNetServer without callback.');
}

if (doesGlobalServerExist()) {
return new Function(`return new Server(${ callback });`)();
}

throw new ReferenceError('Unable to find global.Server. Unable to create Server for NamedPipeServer.');
}

// const getNodeNetServerConstructor = new Function(`const { Server } = require('net'); return Server;`);

/**
* Streaming transport server implementation that uses named pipes for inter-process communication.
Expand Down Expand Up @@ -71,9 +89,9 @@ export class NamedPipeServer implements IStreamingTransportServer {
if (this._receiver.isConnected || this._sender.isConnected || this._incomingServer || this._outgoingServer) {
this.disconnect();
}

// const Server = getNodeNetServer();
const incoming = new Promise(resolve => {
this._incomingServer = new Server((socket: Socket): void => {
this._incomingServer = createNodeServer((socket: Socket): void => {
this._receiver.connect(new NamedPipeTransport(socket));
resolve();
});
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 doesGlobalServerExist = new Function('try {return typeof Server !== "undefined" && Server !== null;}catch(e){ return false;}');
1 change: 1 addition & 0 deletions libraries/botframework-streaming/src/utilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@

export * from './doesGlobalFileReaderExist';
export * from './doesGlobalWebSocketExist';
export * from './doesGlobalServerExist';
export * from './protocol-base';