fix(webkit): avoid UnhandledPromiseRejection (#986)

* fix(webkit): avoid UnhandledPromiseRejection

If we close a browser before sending first websocket message,
the `this._connect` promise will reject and will not be handled.

This surfaced at https://github.com/microsoft/playwright/pull/976/checks?check_run_id=444445350

* fix
This commit is contained in:
Andrey Lushnikov 2020-02-13 12:19:41 -08:00 committed by GitHub
parent 991b89fb11
commit 8006b2c01f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -316,16 +316,16 @@ export class WebSocketTransport implements ConnectionTransport {
onmessage?: (message: string) => void;
onclose?: () => void;
private _connect: Promise<void>;
private _connectPromise: Promise<(Error|null)>;
constructor(url: string) {
this._ws = (isNode ? new NodeWebSocket(url, [], {
perMessageDeflate: false,
maxPayload: 256 * 1024 * 1024, // 256Mb
}) : new WebSocket(url)) as WebSocket;
this._connect = new Promise((fulfill, reject) => {
this._ws.addEventListener('open', () => fulfill());
this._ws.addEventListener('error', event => reject(new Error('WebSocket error: ' + (event as ErrorEvent).message)));
this._connectPromise = new Promise(fulfill => {
this._ws.addEventListener('open', () => fulfill(null));
this._ws.addEventListener('error', event => fulfill(new Error('WebSocket error: ' + (event as ErrorEvent).message)));
});
// The 'ws' module in node sometimes sends us multiple messages in a single task.
// In Web, all IO callbacks (e.g. WebSocket callbacks)
@ -349,7 +349,9 @@ export class WebSocketTransport implements ConnectionTransport {
}
async send(message: string) {
await this._connect;
const error = await this._connectPromise;
if (error)
throw error;
this._ws.send(message);
}