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 {helper, assert, debugError} = require('./helper');
const debugLauncher = require('debug')(`puppeteer:launcher`); const debugLauncher = require('debug')(`puppeteer:launcher`);
const {TimeoutError} = require('./Errors'); const {TimeoutError} = require('./Errors');
const WebSocketTransport = require('./WebSocketTransport'); const {WebSocketTransport} = require('./WebSocketTransport');
const {PipeTransport} = require('./PipeTransport'); const {PipeTransport} = require('./PipeTransport');
const mkdtempAsync = helper.promisify(fs.mkdtemp); const mkdtempAsync = helper.promisify(fs.mkdtemp);

View File

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