* Creates a WebSocket transport by connecting to the given URL.
(
url: string,
cancellationToken: CancellationToken,
remoteHostHeader?: string,
)
| 25 | * Creates a WebSocket transport by connecting to the given URL. |
| 26 | */ |
| 27 | static async create( |
| 28 | url: string, |
| 29 | cancellationToken: CancellationToken, |
| 30 | remoteHostHeader?: string, |
| 31 | ): Promise<WebSocketTransport> { |
| 32 | const isSecure = !url.startsWith('ws://'); |
| 33 | const targetAddressIsLoopback = await isLoopback(url); |
| 34 | |
| 35 | while (true) { |
| 36 | const dontRetryBefore = Date.now() + maxRetryInterval; |
| 37 | try { |
| 38 | const options = { |
| 39 | headers: { host: remoteHostHeader ?? 'localhost' }, |
| 40 | perMessageDeflate: false, |
| 41 | maxPayload: 256 * 1024 * 1024, // 256Mb |
| 42 | rejectUnauthorized: !(isSecure && targetAddressIsLoopback), |
| 43 | followRedirects: true, |
| 44 | }; |
| 45 | |
| 46 | const ws = new WebSocket(url, [], options); |
| 47 | return await timeoutPromise( |
| 48 | new Promise<WebSocketTransport>((resolve, reject) => { |
| 49 | ws.addEventListener('open', () => resolve(new WebSocketTransport(ws))); |
| 50 | ws.addEventListener('error', errorEvent => { |
| 51 | // Check for invalid http redirects for compatibility with old cdp proxies |
| 52 | const redirectUrl = url === ws.url ? url : ws.url.replace(/^http(s?):/, 'ws$1:'); |
| 53 | |
| 54 | if (redirectUrl === url) { |
| 55 | reject(errorEvent.error); // Parameter is an ErrorEvent. See https://github.com/websockets/ws/blob/master/doc/ws.md#websocketonerror |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | this.create(redirectUrl, cancellationToken, remoteHostHeader).then(resolve, reject); |
| 60 | }); |
| 61 | }), |
| 62 | CancellationTokenSource.withTimeout(2000, cancellationToken).token, |
| 63 | `Could not open ${url}`, |
| 64 | ).catch(err => { |
| 65 | ws.close(); |
| 66 | throw err; |
| 67 | }); |
| 68 | } catch (err) { |
| 69 | if (cancellationToken.isCancellationRequested) { |
| 70 | throw err; |
| 71 | } |
| 72 | |
| 73 | const retryIn = dontRetryBefore - Date.now(); |
| 74 | if (retryIn > 0) { |
| 75 | await delay(retryIn); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | constructor(ws: WebSocket) { |
| 82 | this._ws = ws; |
nothing calls this directly
no test coverage detected