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