-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Added timeout for browserWS connection
- Loading branch information
1 parent
c6ae6eb
commit 3b31c1f
Showing
4 changed files
with
92 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { ConnectionTransport } from 'puppeteer'; | ||
import * as WebSocket from 'ws'; | ||
|
||
export class WebSocketTransport implements ConnectionTransport { | ||
static create(url: string, timeout?: number): Promise<WebSocketTransport> { | ||
return new Promise((resolve, reject) => { | ||
const ws = new WebSocket(url, [], { | ||
perMessageDeflate: false, | ||
maxPayload: 256 * 1024 * 1024, // 256Mb | ||
handshakeTimeout: timeout, | ||
}); | ||
|
||
ws.addEventListener('open', () => resolve(new WebSocketTransport(ws))); | ||
ws.addEventListener('error', reject); | ||
}); | ||
} | ||
|
||
private _ws: WebSocket; | ||
onmessage?: (message: string) => void; | ||
onclose?: () => void; | ||
|
||
constructor(ws: WebSocket) { | ||
this._ws = ws; | ||
this._ws.addEventListener('message', (event) => { | ||
if (this.onmessage) this.onmessage.call(null, event.data); | ||
}); | ||
this._ws.addEventListener('close', () => { | ||
if (this.onclose) this.onclose.call(null); | ||
}); | ||
// Silently ignore all errors - we don't know what to do with them. | ||
this._ws.addEventListener('error', () => {}); | ||
this.onmessage = null; | ||
this.onclose = null; | ||
} | ||
|
||
send(message: string): void { | ||
this._ws.send(message); | ||
} | ||
|
||
close(): void { | ||
this._ws.close(); | ||
} | ||
} |