* Initialize a WebSocket client. * * @param {WebSocket} websocket The client to initialize * @param {(String|URL)} address The URL to which to connect * @param {Array} protocols The subprotocols * @param {Object} [options] Connection options * @param {Boolean} [options.allowSynchronousEvents=t
(websocket, address, protocols, options)
| 665 | * @private |
| 666 | */ |
| 667 | function initAsClient(websocket, address, protocols, options) { |
| 668 | const opts = { |
| 669 | allowSynchronousEvents: true, |
| 670 | autoPong: true, |
| 671 | closeTimeout: CLOSE_TIMEOUT, |
| 672 | protocolVersion: protocolVersions[1], |
| 673 | maxBufferedChunks: 1024 * 1024, |
| 674 | maxFragments: 128 * 1024, |
| 675 | maxPayload: 100 * 1024 * 1024, |
| 676 | skipUTF8Validation: false, |
| 677 | perMessageDeflate: true, |
| 678 | followRedirects: false, |
| 679 | maxRedirects: 10, |
| 680 | ...options, |
| 681 | socketPath: undefined, |
| 682 | hostname: undefined, |
| 683 | protocol: undefined, |
| 684 | timeout: undefined, |
| 685 | method: 'GET', |
| 686 | host: undefined, |
| 687 | path: undefined, |
| 688 | port: undefined |
| 689 | }; |
| 690 | |
| 691 | websocket._autoPong = opts.autoPong; |
| 692 | websocket._closeTimeout = opts.closeTimeout; |
| 693 | |
| 694 | if (!protocolVersions.includes(opts.protocolVersion)) { |
| 695 | throw new RangeError( |
| 696 | `Unsupported protocol version: ${opts.protocolVersion} ` + |
| 697 | `(supported versions: ${protocolVersions.join(', ')})` |
| 698 | ); |
| 699 | } |
| 700 | |
| 701 | let parsedUrl; |
| 702 | |
| 703 | if (address instanceof URL) { |
| 704 | parsedUrl = address; |
| 705 | } else { |
| 706 | try { |
| 707 | parsedUrl = new URL(address); |
| 708 | } catch { |
| 709 | throw new SyntaxError(`Invalid URL: ${address}`); |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | if (parsedUrl.protocol === 'http:') { |
| 714 | parsedUrl.protocol = 'ws:'; |
| 715 | } else if (parsedUrl.protocol === 'https:') { |
| 716 | parsedUrl.protocol = 'wss:'; |
| 717 | } |
| 718 | |
| 719 | websocket._url = parsedUrl.href; |
| 720 | |
| 721 | const isSecure = parsedUrl.protocol === 'wss:'; |
| 722 | const isIpcUrl = parsedUrl.protocol === 'ws+unix:'; |
| 723 | let invalidUrlMessage; |
| 724 |
no test coverage detected