chore: migrate src/WebSocketTransport to TypeScript (#5696)

This commit is contained in:
Jack Franklin 2020-04-21 07:45:52 +01:00 committed by GitHub
parent 5c839f5e48
commit 376d234ed1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 22 deletions

View File

@ -28,7 +28,7 @@ const fs = require('fs');
const {helper, assert, debugError} = require('./helper');
const debugLauncher = require('debug')(`puppeteer:launcher`);
const {TimeoutError} = require('./Errors');
const WebSocketTransport = require('./WebSocketTransport');
const {WebSocketTransport} = require('./WebSocketTransport');
const {PipeTransport} = require('./PipeTransport');
const mkdtempAsync = helper.promisify(fs.mkdtemp);

View File

@ -13,17 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const NodeWebSocket = require('ws');
import * as NodeWebSocket from 'ws';
/**
* @implements {!Puppeteer.ConnectionTransport}
*/
class WebSocketTransport {
/**
* @param {string} url
* @return {!Promise<!WebSocketTransport>}
*/
static create(url) {
export class WebSocketTransport implements Puppeteer.ConnectionTransport {
static create(url: string): Promise<WebSocketTransport> {
return new Promise((resolve, reject) => {
const ws = new NodeWebSocket(url, [], {
perMessageDeflate: false,
@ -35,16 +28,17 @@ class WebSocketTransport {
});
}
/**
* @param {!NodeWebSocket} ws
*/
constructor(ws) {
_ws: NodeWebSocket;
onmessage?: (message: string) => void;
onclose?: () => void;
constructor(ws: NodeWebSocket) {
this._ws = ws;
this._ws.addEventListener('message', event => {
if (this.onmessage)
this.onmessage.call(null, event.data);
});
this._ws.addEventListener('close', event => {
this._ws.addEventListener('close', () => {
if (this.onclose)
this.onclose.call(null);
});
@ -54,16 +48,12 @@ class WebSocketTransport {
this.onclose = null;
}
/**
* @param {string} message
*/
send(message) {
send(message): void {
this._ws.send(message);
}
close() {
close(): void {
this._ws.close();
}
}
module.exports = WebSocketTransport;