botframework-streaming
Advanced tools
Comparing version 4.19.2 to 4.19.3-rc
@@ -46,9 +46,9 @@ /** | ||
* | ||
* @param serverAddress The address the server is listening on. | ||
* @param port The port the server is listening on, defaults to 8082. | ||
* @param serverAddressOrHostName The host name or URL the server is listening on. | ||
* @param port If `serverAddressOrHostName` is a host name, the port the server is listening on, defaults to 8082. Otherwise, this argument is ignored. | ||
* @returns A Promise that resolves when the websocket connection is closed, or rejects on an error. | ||
*/ | ||
connect(serverAddress: string, port?: number): Promise<void>; | ||
connect(serverAddressOrHostName: string, port?: number): Promise<void>; | ||
/** | ||
* Set the handler for `'data'` and `'message'` events received on the socket. | ||
* Set the handler for `'message'` events received on the socket. | ||
* | ||
@@ -55,0 +55,0 @@ * @param handler The callback to handle the "message" event. |
@@ -46,9 +46,9 @@ /** | ||
* | ||
* @param serverAddress The address the server is listening on. | ||
* @param port The port the server is listening on, defaults to 8082. | ||
* @param serverAddressOrHostName The host name or URL the server is listening on. | ||
* @param port If `serverAddressOrHostName` is a host name, the port the server is listening on, defaults to 8082. Otherwise, this argument is ignored. | ||
* @returns A Promise that resolves when the websocket connection is closed, or rejects on an error. | ||
*/ | ||
connect(serverAddress: string, port?: number): Promise<void>; | ||
connect(serverAddressOrHostName: string, port?: number): Promise<void>; | ||
/** | ||
* Set the handler for `'data'` and `'message'` events received on the socket. | ||
* Set the handler for `'message'` events received on the socket. | ||
* | ||
@@ -55,0 +55,0 @@ * @param handler The callback to handle the "message" event. |
@@ -20,4 +20,5 @@ "use strict"; | ||
exports.NodeWebSocket = void 0; | ||
const http_1 = require("http"); | ||
const url_1 = require("url"); | ||
const crypto = require("crypto"); | ||
const http_1 = require("http"); | ||
const WebSocket = require("ws"); | ||
@@ -80,8 +81,31 @@ const NONCE_LENGTH = 16; | ||
* | ||
* @param serverAddress The address the server is listening on. | ||
* @param port The port the server is listening on, defaults to 8082. | ||
* @param serverAddressOrHostName The host name or URL the server is listening on. | ||
* @param port If `serverAddressOrHostName` is a host name, the port the server is listening on, defaults to 8082. Otherwise, this argument is ignored. | ||
* @returns A Promise that resolves when the websocket connection is closed, or rejects on an error. | ||
*/ | ||
connect(serverAddress, port = 8082) { | ||
connect(serverAddressOrHostName, port = 8082) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let url; | ||
try { | ||
url = new url_1.URL(serverAddressOrHostName); | ||
// eslint-disable-next-line no-empty | ||
} | ||
catch (_error) { } | ||
if (url === null || url === void 0 ? void 0 : url.hostname) { | ||
return new Promise((resolve, reject) => { | ||
const ws = (this.wsSocket = new WebSocket(url)); | ||
ws.once('error', ({ message }) => reject(new Error(message))); | ||
ws.once('open', () => resolve()); | ||
}); | ||
} | ||
// [hawo]: The following logics are kept here for backward compatibility. | ||
// | ||
// However, there are no tests to prove the following code works. | ||
// We tried our best to write a test and figure out how the code would work. | ||
// | ||
// However, there are obvious mistakes in the code that made it very unlikely to work: | ||
// - `options.headers.upgrade` must set to `'websocket'` | ||
// - Second argument of `WebSocket.server.completeUpgrade` should be `{}`, instead of `undefined` | ||
// | ||
// More readings at https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers#client_handshake_request. | ||
this.wsServer = new WebSocket.Server({ noServer: true }); | ||
@@ -92,3 +116,3 @@ // Key generation per https://tools.ietf.org/html/rfc6455#section-1.3 (pg. 7) | ||
port: port, | ||
hostname: serverAddress, | ||
hostname: serverAddressOrHostName, | ||
headers: { | ||
@@ -117,3 +141,3 @@ connection: 'upgrade', | ||
/** | ||
* Set the handler for `'data'` and `'message'` events received on the socket. | ||
* Set the handler for `'message'` events received on the socket. | ||
* | ||
@@ -124,3 +148,2 @@ * @param handler The callback to handle the "message" event. | ||
setOnMessageHandler(handler) { | ||
this.wsSocket.on('data', handler); | ||
this.wsSocket.on('message', handler); | ||
@@ -127,0 +150,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"description": "Streaming library for the Microsoft Bot Framework", | ||
"version": "4.19.2", | ||
"version": "4.19.3-rc", | ||
"license": "MIT", | ||
@@ -8,0 +8,0 @@ "keywords": [ |
@@ -9,7 +9,9 @@ /** | ||
import { IncomingMessage, request } from 'http'; | ||
import { URL } from 'url'; | ||
import * as crypto from 'crypto'; | ||
import { IncomingMessage, request } from 'http'; | ||
import * as WebSocket from 'ws'; | ||
import { INodeIncomingMessage, INodeBuffer, INodeSocket, ISocket } from '../interfaces'; | ||
const NONCE_LENGTH = 16; | ||
@@ -78,7 +80,33 @@ | ||
* | ||
* @param serverAddress The address the server is listening on. | ||
* @param port The port the server is listening on, defaults to 8082. | ||
* @param serverAddressOrHostName The host name or URL the server is listening on. | ||
* @param port If `serverAddressOrHostName` is a host name, the port the server is listening on, defaults to 8082. Otherwise, this argument is ignored. | ||
* @returns A Promise that resolves when the websocket connection is closed, or rejects on an error. | ||
*/ | ||
async connect(serverAddress: string, port = 8082): Promise<void> { | ||
async connect(serverAddressOrHostName: string, port = 8082): Promise<void> { | ||
let url: URL; | ||
try { | ||
url = new URL(serverAddressOrHostName); | ||
// eslint-disable-next-line no-empty | ||
} catch (_error) {} | ||
if (url?.hostname) { | ||
return new Promise<void>((resolve, reject) => { | ||
const ws = (this.wsSocket = new WebSocket(url)); | ||
ws.once('error', ({ message }) => reject(new Error(message))); | ||
ws.once('open', () => resolve()); | ||
}); | ||
} | ||
// [hawo]: The following logics are kept here for backward compatibility. | ||
// | ||
// However, there are no tests to prove the following code works. | ||
// We tried our best to write a test and figure out how the code would work. | ||
// | ||
// However, there are obvious mistakes in the code that made it very unlikely to work: | ||
// - `options.headers.upgrade` must set to `'websocket'` | ||
// - Second argument of `WebSocket.server.completeUpgrade` should be `{}`, instead of `undefined` | ||
// | ||
// More readings at https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers#client_handshake_request. | ||
this.wsServer = new WebSocket.Server({ noServer: true }); | ||
@@ -89,3 +117,3 @@ // Key generation per https://tools.ietf.org/html/rfc6455#section-1.3 (pg. 7) | ||
port: port, | ||
hostname: serverAddress, | ||
hostname: serverAddressOrHostName, | ||
headers: { | ||
@@ -97,2 +125,3 @@ connection: 'upgrade', | ||
}; | ||
const req = request(options); | ||
@@ -116,3 +145,3 @@ req.end(); | ||
/** | ||
* Set the handler for `'data'` and `'message'` events received on the socket. | ||
* Set the handler for `'message'` events received on the socket. | ||
* | ||
@@ -123,3 +152,2 @@ * @param handler The callback to handle the "message" event. | ||
setOnMessageHandler(handler: (x: any) => void): void { | ||
this.wsSocket.on('data', handler); | ||
this.wsSocket.on('message', handler); | ||
@@ -126,0 +154,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1308647
19622
2