[
  {
    "path": ".gitattributes",
    "content": "* text=auto eol=lf\n"
  },
  {
    "path": ".github/FUNDING.yml",
    "content": "github:\n  - lpinca\n"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/bug_report.yml",
    "content": "name: Bug report\ndescription: Create a bug report\nbody:\n  - type: markdown\n    attributes:\n      value: |\n        Thank you for reporting an issue.\n\n        This issue tracker is for bugs and issues found in ws.\n        General support questions should be raised on a channel like Stack Overflow.\n\n        Please fill in as much of the template below as you're able.\n  - type: checkboxes\n    attributes:\n      label: Is there an existing issue for this?\n      description:\n        Please search to see if an issue already exists for the bug you\n        encountered.\n      options:\n        - label:\n            I've searched for any related issues and avoided creating a\n            duplicate issue.\n          required: true\n  - type: textarea\n    attributes:\n      label: Description\n      description:\n        Description of the bug or feature, preferably a simple code snippet that\n        can be run directly without installing third-party dependencies.\n  - type: input\n    attributes:\n      label: ws version\n  - type: input\n    attributes:\n      label: Node.js Version\n      description: Output of `node -v`.\n  - type: textarea\n    attributes:\n      label: System\n      description: Output of `npx envinfo --system`.\n  - type: textarea\n    attributes:\n      label: Expected result\n      description: What you expected to happen.\n  - type: textarea\n    attributes:\n      label: Actual result\n      description: What actually happened.\n  - type: textarea\n    attributes:\n      label: Attachments\n      description: Logs, screenshots, screencast, etc.\n"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/config.yml",
    "content": "blank_issues_enabled: false\n"
  },
  {
    "path": ".github/workflows/ci.yml",
    "content": "name: CI\n\non:\n  - push\n  - pull_request\n\npermissions: {}\n\njobs:\n  test:\n    runs-on: ${{ matrix.os }}\n    strategy:\n      matrix:\n        arch:\n          - x64\n        node:\n          - 10\n          - 12\n          - 14\n          - 16\n          - 18\n          - 20\n          - 22\n          - 24\n          - 25\n        os:\n          - macOS-latest\n          - ubuntu-latest\n          - windows-latest\n        include:\n          - arch: x86\n            node: 10\n            os: windows-latest\n          - arch: x86\n            node: 12\n            os: windows-latest\n          - arch: x86\n            node: 14\n            os: windows-latest\n          - arch: x86\n            node: 16\n            os: windows-latest\n          - arch: x86\n            node: 20\n            os: windows-latest\n          - arch: x86\n            node: 22\n            os: windows-latest\n    steps:\n      - uses: actions/checkout@v6\n      - uses: actions/setup-node@v6\n        with:\n          node-version: ${{ matrix.node }}\n          architecture: ${{ matrix.arch }}\n          cache: npm\n          cache-dependency-path: ./package.json\n      - run: npm install\n      - run: npm run lint\n        if:\n          matrix.os == 'ubuntu-latest' && matrix.node == 22 && matrix.arch ==\n          'x64'\n      - run: npm test\n      - run: |\n          id=$(node -e \"console.log(crypto.randomBytes(16).toString('hex'))\")\n\n          echo \"job_id=$id\" >> $GITHUB_OUTPUT\n        id: get_job_id\n        shell: bash\n      - uses: coverallsapp/github-action@v2\n        with:\n          flag-name:\n            ${{ steps.get_job_id.outputs.job_id }} (Node.js ${{ matrix.node }}\n            ${{ matrix.arch }} on ${{ matrix.os }})\n          github-token: ${{ secrets.GITHUB_TOKEN }}\n          parallel: true\n  coverage:\n    needs: test\n    runs-on: ubuntu-latest\n    steps:\n      - uses: coverallsapp/github-action@v2\n        with:\n          github-token: ${{ secrets.GITHUB_TOKEN }}\n          parallel-finished: true\n"
  },
  {
    "path": ".gitignore",
    "content": "node_modules/\n.nyc_output/\ncoverage/\n.vscode/\n"
  },
  {
    "path": ".npmrc",
    "content": "package-lock=false\n"
  },
  {
    "path": ".prettierrc.yaml",
    "content": "arrowParens: always\nendOfLine: lf\nproseWrap: always\nsingleQuote: true\ntrailingComma: none\n"
  },
  {
    "path": "FUNDING.json",
    "content": "{\n  \"drips\": {\n    \"ethereum\": {\n      \"ownedBy\": \"0x3D4f997A071d2BA735AC767E68052679423c3dBe\"\n    }\n  }\n}\n"
  },
  {
    "path": "LICENSE",
    "content": "Copyright (c) 2011 Einar Otto Stangvik <einaros@gmail.com>\nCopyright (c) 2013 Arnout Kazemier and contributors\nCopyright (c) 2016 Luigi Pinca and contributors\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of\nthis software and associated documentation files (the \"Software\"), to deal in\nthe Software without restriction, including without limitation the rights to\nuse, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\nthe Software, and to permit persons to whom the Software is furnished to do so,\nsubject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\nFOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\nCOPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\nIN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\nCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n"
  },
  {
    "path": "README.md",
    "content": "# ws: a Node.js WebSocket library\n\n[![Version npm](https://img.shields.io/npm/v/ws.svg?logo=npm)](https://www.npmjs.com/package/ws)\n[![CI](https://img.shields.io/github/actions/workflow/status/websockets/ws/ci.yml?branch=master&label=CI&logo=github)](https://github.com/websockets/ws/actions?query=workflow%3ACI+branch%3Amaster)\n[![Coverage Status](https://img.shields.io/coveralls/websockets/ws/master.svg?logo=coveralls)](https://coveralls.io/github/websockets/ws)\n\nws is a simple to use, blazing fast, and thoroughly tested WebSocket client and\nserver implementation.\n\nPasses the quite extensive Autobahn test suite: [server][server-report],\n[client][client-report].\n\n**Note**: This module does not work in the browser. The client in the docs is a\nreference to a backend with the role of a client in the WebSocket communication.\nBrowser clients must use the native\n[`WebSocket`](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket)\nobject. To make the same code work seamlessly on Node.js and the browser, you\ncan use one of the many wrappers available on npm, like\n[isomorphic-ws](https://github.com/heineiuo/isomorphic-ws).\n\n## Table of Contents\n\n- [Protocol support](#protocol-support)\n- [Installing](#installing)\n  - [Opt-in for performance](#opt-in-for-performance)\n    - [Legacy opt-in for performance](#legacy-opt-in-for-performance)\n- [API docs](#api-docs)\n- [WebSocket compression](#websocket-compression)\n- [Usage examples](#usage-examples)\n  - [Sending and receiving text data](#sending-and-receiving-text-data)\n  - [Sending binary data](#sending-binary-data)\n  - [Simple server](#simple-server)\n  - [External HTTP/S server](#external-https-server)\n  - [Multiple servers sharing a single HTTP/S server](#multiple-servers-sharing-a-single-https-server)\n  - [Client authentication](#client-authentication)\n  - [Server broadcast](#server-broadcast)\n  - [Round-trip time](#round-trip-time)\n  - [Use the Node.js streams API](#use-the-nodejs-streams-api)\n  - [Other examples](#other-examples)\n- [FAQ](#faq)\n  - [How to get the IP address of the client?](#how-to-get-the-ip-address-of-the-client)\n  - [How to detect and close broken connections?](#how-to-detect-and-close-broken-connections)\n  - [How to connect via a proxy?](#how-to-connect-via-a-proxy)\n- [Changelog](#changelog)\n- [License](#license)\n\n## Protocol support\n\n- **HyBi drafts 07-12** (Use the option `protocolVersion: 8`)\n- **HyBi drafts 13-17** (Current default, alternatively option\n  `protocolVersion: 13`)\n\n## Installing\n\n```\nnpm install ws\n```\n\n### Opt-in for performance\n\n[bufferutil][] is an optional module that can be installed alongside the ws\nmodule:\n\n```\nnpm install --save-optional bufferutil\n```\n\nThis is a binary addon that improves the performance of certain operations such\nas masking and unmasking the data payload of the WebSocket frames. Prebuilt\nbinaries are available for the most popular platforms, so you don't necessarily\nneed to have a C++ compiler installed on your machine.\n\nTo force ws to not use bufferutil, use the\n[`WS_NO_BUFFER_UTIL`](./doc/ws.md#ws_no_buffer_util) environment variable. This\ncan be useful to enhance security in systems where a user can put a package in\nthe package search path of an application of another user, due to how the\nNode.js resolver algorithm works.\n\n#### Legacy opt-in for performance\n\nIf you are running on an old version of Node.js (prior to v18.14.0), ws also\nsupports the [utf-8-validate][] module:\n\n```\nnpm install --save-optional utf-8-validate\n```\n\nThis contains a binary polyfill for [`buffer.isUtf8()`][].\n\nTo force ws not to use utf-8-validate, use the\n[`WS_NO_UTF_8_VALIDATE`](./doc/ws.md#ws_no_utf_8_validate) environment variable.\n\n## API docs\n\nSee [`/doc/ws.md`](./doc/ws.md) for Node.js-like documentation of ws classes and\nutility functions.\n\n## WebSocket compression\n\nws supports the [permessage-deflate extension][permessage-deflate] which enables\nthe client and server to negotiate a compression algorithm and its parameters,\nand then selectively apply it to the data payloads of each WebSocket message.\n\nThe extension is disabled by default on the server and enabled by default on the\nclient. It adds a significant overhead in terms of performance and memory\nconsumption so we suggest to enable it only if it is really needed.\n\nNote that Node.js has a variety of issues with high-performance compression,\nwhere increased concurrency, especially on Linux, can lead to [catastrophic\nmemory fragmentation][node-zlib-bug] and slow performance. If you intend to use\npermessage-deflate in production, it is worthwhile to set up a test\nrepresentative of your workload and ensure Node.js/zlib will handle it with\nacceptable performance and memory usage.\n\nTuning of permessage-deflate can be done via the options defined below. You can\nalso use `zlibDeflateOptions` and `zlibInflateOptions`, which is passed directly\ninto the creation of [raw deflate/inflate streams][node-zlib-deflaterawdocs].\n\nSee [the docs][ws-server-options] for more options.\n\n```js\nimport WebSocket, { WebSocketServer } from 'ws';\n\nconst wss = new WebSocketServer({\n  port: 8080,\n  perMessageDeflate: {\n    zlibDeflateOptions: {\n      // See zlib defaults.\n      chunkSize: 1024,\n      memLevel: 7,\n      level: 3\n    },\n    zlibInflateOptions: {\n      chunkSize: 10 * 1024\n    },\n    // Other options settable:\n    clientNoContextTakeover: true, // Defaults to negotiated value.\n    serverNoContextTakeover: true, // Defaults to negotiated value.\n    serverMaxWindowBits: 10, // Defaults to negotiated value.\n    // Below options specified as default values.\n    concurrencyLimit: 10, // Limits zlib concurrency for perf.\n    threshold: 1024 // Size (in bytes) below which messages\n    // should not be compressed if context takeover is disabled.\n  }\n});\n```\n\nThe client will only use the extension if it is supported and enabled on the\nserver. To always disable the extension on the client, set the\n`perMessageDeflate` option to `false`.\n\n```js\nimport WebSocket from 'ws';\n\nconst ws = new WebSocket('ws://www.host.com/path', {\n  perMessageDeflate: false\n});\n```\n\n## Usage examples\n\n### Sending and receiving text data\n\n```js\nimport WebSocket from 'ws';\n\nconst ws = new WebSocket('ws://www.host.com/path');\n\nws.on('error', console.error);\n\nws.on('open', function open() {\n  ws.send('something');\n});\n\nws.on('message', function message(data) {\n  console.log('received: %s', data);\n});\n```\n\n### Sending binary data\n\n```js\nimport WebSocket from 'ws';\n\nconst ws = new WebSocket('ws://www.host.com/path');\n\nws.on('error', console.error);\n\nws.on('open', function open() {\n  const array = new Float32Array(5);\n\n  for (var i = 0; i < array.length; ++i) {\n    array[i] = i / 2;\n  }\n\n  ws.send(array);\n});\n```\n\n### Simple server\n\n```js\nimport { WebSocketServer } from 'ws';\n\nconst wss = new WebSocketServer({ port: 8080 });\n\nwss.on('connection', function connection(ws) {\n  ws.on('error', console.error);\n\n  ws.on('message', function message(data) {\n    console.log('received: %s', data);\n  });\n\n  ws.send('something');\n});\n```\n\n### External HTTP/S server\n\n```js\nimport { createServer } from 'https';\nimport { readFileSync } from 'fs';\nimport { WebSocketServer } from 'ws';\n\nconst server = createServer({\n  cert: readFileSync('/path/to/cert.pem'),\n  key: readFileSync('/path/to/key.pem')\n});\nconst wss = new WebSocketServer({ server });\n\nwss.on('connection', function connection(ws) {\n  ws.on('error', console.error);\n\n  ws.on('message', function message(data) {\n    console.log('received: %s', data);\n  });\n\n  ws.send('something');\n});\n\nserver.listen(8080);\n```\n\n### Multiple servers sharing a single HTTP/S server\n\n```js\nimport { createServer } from 'http';\nimport { WebSocketServer } from 'ws';\n\nconst server = createServer();\nconst wss1 = new WebSocketServer({ noServer: true });\nconst wss2 = new WebSocketServer({ noServer: true });\n\nwss1.on('connection', function connection(ws) {\n  ws.on('error', console.error);\n\n  // ...\n});\n\nwss2.on('connection', function connection(ws) {\n  ws.on('error', console.error);\n\n  // ...\n});\n\nserver.on('upgrade', function upgrade(request, socket, head) {\n  const { pathname } = new URL(request.url, 'wss://base.url');\n\n  if (pathname === '/foo') {\n    wss1.handleUpgrade(request, socket, head, function done(ws) {\n      wss1.emit('connection', ws, request);\n    });\n  } else if (pathname === '/bar') {\n    wss2.handleUpgrade(request, socket, head, function done(ws) {\n      wss2.emit('connection', ws, request);\n    });\n  } else {\n    socket.destroy();\n  }\n});\n\nserver.listen(8080);\n```\n\n### Client authentication\n\n```js\nimport { createServer } from 'http';\nimport { WebSocketServer } from 'ws';\n\nfunction onSocketError(err) {\n  console.error(err);\n}\n\nconst server = createServer();\nconst wss = new WebSocketServer({ noServer: true });\n\nwss.on('connection', function connection(ws, request, client) {\n  ws.on('error', console.error);\n\n  ws.on('message', function message(data) {\n    console.log(`Received message ${data} from user ${client}`);\n  });\n});\n\nserver.on('upgrade', function upgrade(request, socket, head) {\n  socket.on('error', onSocketError);\n\n  // This function is not defined on purpose. Implement it with your own logic.\n  authenticate(request, function next(err, client) {\n    if (err || !client) {\n      socket.write('HTTP/1.1 401 Unauthorized\\r\\n\\r\\n');\n      socket.destroy();\n      return;\n    }\n\n    socket.removeListener('error', onSocketError);\n\n    wss.handleUpgrade(request, socket, head, function done(ws) {\n      wss.emit('connection', ws, request, client);\n    });\n  });\n});\n\nserver.listen(8080);\n```\n\nAlso see the provided [example][session-parse-example] using `express-session`.\n\n### Server broadcast\n\nA client WebSocket broadcasting to all connected WebSocket clients, including\nitself.\n\n```js\nimport WebSocket, { WebSocketServer } from 'ws';\n\nconst wss = new WebSocketServer({ port: 8080 });\n\nwss.on('connection', function connection(ws) {\n  ws.on('error', console.error);\n\n  ws.on('message', function message(data, isBinary) {\n    wss.clients.forEach(function each(client) {\n      if (client.readyState === WebSocket.OPEN) {\n        client.send(data, { binary: isBinary });\n      }\n    });\n  });\n});\n```\n\nA client WebSocket broadcasting to every other connected WebSocket clients,\nexcluding itself.\n\n```js\nimport WebSocket, { WebSocketServer } from 'ws';\n\nconst wss = new WebSocketServer({ port: 8080 });\n\nwss.on('connection', function connection(ws) {\n  ws.on('error', console.error);\n\n  ws.on('message', function message(data, isBinary) {\n    wss.clients.forEach(function each(client) {\n      if (client !== ws && client.readyState === WebSocket.OPEN) {\n        client.send(data, { binary: isBinary });\n      }\n    });\n  });\n});\n```\n\n### Round-trip time\n\n```js\nimport WebSocket from 'ws';\n\nconst ws = new WebSocket('wss://websocket-echo.com/');\n\nws.on('error', console.error);\n\nws.on('open', function open() {\n  console.log('connected');\n  ws.send(Date.now());\n});\n\nws.on('close', function close() {\n  console.log('disconnected');\n});\n\nws.on('message', function message(data) {\n  console.log(`Round-trip time: ${Date.now() - data} ms`);\n\n  setTimeout(function timeout() {\n    ws.send(Date.now());\n  }, 500);\n});\n```\n\n### Use the Node.js streams API\n\n```js\nimport WebSocket, { createWebSocketStream } from 'ws';\n\nconst ws = new WebSocket('wss://websocket-echo.com/');\n\nconst duplex = createWebSocketStream(ws, { encoding: 'utf8' });\n\nduplex.on('error', console.error);\n\nduplex.pipe(process.stdout);\nprocess.stdin.pipe(duplex);\n```\n\n### Other examples\n\nFor a full example with a browser client communicating with a ws server, see the\nexamples folder.\n\nOtherwise, see the test cases.\n\n## FAQ\n\n### How to get the IP address of the client?\n\nThe remote IP address can be obtained from the raw socket.\n\n```js\nimport { WebSocketServer } from 'ws';\n\nconst wss = new WebSocketServer({ port: 8080 });\n\nwss.on('connection', function connection(ws, req) {\n  const ip = req.socket.remoteAddress;\n\n  ws.on('error', console.error);\n});\n```\n\nWhen the server runs behind a proxy like NGINX, the de-facto standard is to use\nthe `X-Forwarded-For` header.\n\n```js\nwss.on('connection', function connection(ws, req) {\n  const ip = req.headers['x-forwarded-for'].split(',')[0].trim();\n\n  ws.on('error', console.error);\n});\n```\n\n### How to detect and close broken connections?\n\nSometimes, the link between the server and the client can be interrupted in a\nway that keeps both the server and the client unaware of the broken state of the\nconnection (e.g. when pulling the cord).\n\nIn these cases, ping messages can be used as a means to verify that the remote\nendpoint is still responsive.\n\n```js\nimport { WebSocketServer } from 'ws';\n\nfunction heartbeat() {\n  this.isAlive = true;\n}\n\nconst wss = new WebSocketServer({ port: 8080 });\n\nwss.on('connection', function connection(ws) {\n  ws.isAlive = true;\n  ws.on('error', console.error);\n  ws.on('pong', heartbeat);\n});\n\nconst interval = setInterval(function ping() {\n  wss.clients.forEach(function each(ws) {\n    if (ws.isAlive === false) return ws.terminate();\n\n    ws.isAlive = false;\n    ws.ping();\n  });\n}, 30000);\n\nwss.on('close', function close() {\n  clearInterval(interval);\n});\n```\n\nPong messages are automatically sent in response to ping messages as required by\nthe spec.\n\nJust like the server example above, your clients might as well lose connection\nwithout knowing it. You might want to add a ping listener on your clients to\nprevent that. A simple implementation would be:\n\n```js\nimport WebSocket from 'ws';\n\nfunction heartbeat() {\n  clearTimeout(this.pingTimeout);\n\n  // Use `WebSocket#terminate()`, which immediately destroys the connection,\n  // instead of `WebSocket#close()`, which waits for the close timer.\n  // Delay should be equal to the interval at which your server\n  // sends out pings plus a conservative assumption of the latency.\n  this.pingTimeout = setTimeout(() => {\n    this.terminate();\n  }, 30000 + 1000);\n}\n\nconst client = new WebSocket('wss://websocket-echo.com/');\n\nclient.on('error', console.error);\nclient.on('open', heartbeat);\nclient.on('ping', heartbeat);\nclient.on('close', function clear() {\n  clearTimeout(this.pingTimeout);\n});\n```\n\n### How to connect via a proxy?\n\nUse a custom `http.Agent` implementation like [https-proxy-agent][] or\n[socks-proxy-agent][].\n\n## Changelog\n\nWe're using the GitHub [releases][changelog] for changelog entries.\n\n## License\n\n[MIT](LICENSE)\n\n[`buffer.isutf8()`]: https://nodejs.org/api/buffer.html#bufferisutf8input\n[bufferutil]: https://github.com/websockets/bufferutil\n[changelog]: https://github.com/websockets/ws/releases\n[client-report]: http://websockets.github.io/ws/autobahn/clients/\n[https-proxy-agent]: https://github.com/TooTallNate/node-https-proxy-agent\n[node-zlib-bug]: https://github.com/nodejs/node/issues/8871\n[node-zlib-deflaterawdocs]:\n  https://nodejs.org/api/zlib.html#zlib_zlib_createdeflateraw_options\n[permessage-deflate]: https://tools.ietf.org/html/rfc7692\n[server-report]: http://websockets.github.io/ws/autobahn/servers/\n[session-parse-example]: ./examples/express-session-parse\n[socks-proxy-agent]: https://github.com/TooTallNate/node-socks-proxy-agent\n[utf-8-validate]: https://github.com/websockets/utf-8-validate\n[ws-server-options]: ./doc/ws.md#new-websocketserveroptions-callback\n"
  },
  {
    "path": "SECURITY.md",
    "content": "# Security Guidelines\n\nPlease contact us directly at **security@3rd-Eden.com** for any bug that might\nimpact the security of this project. Please prefix the subject of your email\nwith `[security]` in lowercase and square brackets. Our email filters will\nautomatically prevent these messages from being moved to our spam box.\n\nYou will receive an acknowledgement of your report within **24 hours**.\n\nAll emails that do not include security vulnerabilities will be removed and\nblocked instantly.\n\n## Exceptions\n\nIf you do not receive an acknowledgement within the said time frame, please give\nus the benefit of the doubt as it's possible that we haven't seen it yet. In\nthis case, please send us a message **without details** using one of the\nfollowing methods:\n\n- Contact the lead developers of this project on their personal e-mails. You can\n  find the e-mails in the git logs, for example, using the following command:\n  `git --no-pager show -s --format='%an <%ae>' <gitsha>` where `<gitsha>` is the\n  SHA1 of their latest commit in the project.\n- Create a GitHub issue stating contact details and the severity of the issue.\n\nOnce we have acknowledged receipt of your report and confirmed the bug\nourselves, we will work with you to fix the vulnerability and publicly\nacknowledge your responsible disclosure, if you wish. In addition to that, we\nwill create and publish a security advisory to\n[GitHub Security Advisories](https://github.com/websockets/ws/security/advisories?state=published).\n\n## History\n\n- 04 Jan 2016:\n  [Buffer vulnerability](https://github.com/websockets/ws/releases/tag/1.0.1)\n- 08 Nov 2017:\n  [DoS in the `Sec-Websocket-Extensions` header parser](https://github.com/websockets/ws/releases/tag/3.3.1)\n- 25 May 2021:\n  [ReDoS in `Sec-Websocket-Protocol` header](https://github.com/websockets/ws/releases/tag/7.4.6)\n- 16 Jun 2024:\n  [DoS when handling a request with many HTTP headers](https://github.com/websockets/ws/releases/tag/8.17.1)\n"
  },
  {
    "path": "bench/parser.benchmark.js",
    "content": "'use strict';\n\nconst benchmark = require('benchmark');\nconst crypto = require('crypto');\n\nconst WebSocket = require('..');\n\nconst Receiver = WebSocket.Receiver;\nconst Sender = WebSocket.Sender;\n\nconst options = {\n  fin: true,\n  rsv1: false,\n  mask: true,\n  readOnly: false\n};\n\nfunction createBinaryFrame(length) {\n  const list = Sender.frame(crypto.randomBytes(length), {\n    opcode: 0x02,\n    ...options\n  });\n\n  return Buffer.concat(list);\n}\n\nconst pingFrame1 = Buffer.concat(\n  Sender.frame(crypto.randomBytes(5), { opcode: 0x09, ...options })\n);\n\nconst textFrame = Buffer.from('819461616161' + '61'.repeat(20), 'hex');\nconst pingFrame2 = Buffer.from('8980146e915a', 'hex');\nconst binaryFrame1 = createBinaryFrame(125);\nconst binaryFrame2 = createBinaryFrame(65535);\nconst binaryFrame3 = createBinaryFrame(200 * 1024);\nconst binaryFrame4 = createBinaryFrame(1024 * 1024);\n\nconst suite = new benchmark.Suite();\nconst receiver = new Receiver({\n  binaryType: 'nodebuffer',\n  extensions: {},\n  isServer: true,\n  skipUTF8Validation: false\n});\n\nsuite.add('ping frame (5 bytes payload)', {\n  defer: true,\n  fn: (deferred) => {\n    receiver.write(pingFrame1, deferred.resolve.bind(deferred));\n  }\n});\nsuite.add('ping frame (no payload)', {\n  defer: true,\n  fn: (deferred) => {\n    receiver.write(pingFrame2, deferred.resolve.bind(deferred));\n  }\n});\nsuite.add('text frame (20 bytes payload)', {\n  defer: true,\n  fn: (deferred) => {\n    receiver.write(textFrame, deferred.resolve.bind(deferred));\n  }\n});\nsuite.add('binary frame (125 bytes payload)', {\n  defer: true,\n  fn: (deferred) => {\n    receiver.write(binaryFrame1, deferred.resolve.bind(deferred));\n  }\n});\nsuite.add('binary frame (65535 bytes payload)', {\n  defer: true,\n  fn: (deferred) => {\n    receiver.write(binaryFrame2, deferred.resolve.bind(deferred));\n  }\n});\nsuite.add('binary frame (200 KiB payload)', {\n  defer: true,\n  fn: (deferred) => {\n    receiver.write(binaryFrame3, deferred.resolve.bind(deferred));\n  }\n});\nsuite.add('binary frame (1 MiB payload)', {\n  defer: true,\n  fn: (deferred) => {\n    receiver.write(binaryFrame4, deferred.resolve.bind(deferred));\n  }\n});\n\nsuite.on('cycle', (e) => console.log(e.target.toString()));\n\nif (require.main === module) {\n  suite.run({ async: true });\n} else {\n  module.exports = suite;\n}\n"
  },
  {
    "path": "bench/sender.benchmark.js",
    "content": "'use strict';\n\nconst benchmark = require('benchmark');\nconst crypto = require('crypto');\n\nconst Sender = require('../').Sender;\n\nconst data1 = crypto.randomBytes(64);\nconst data2 = crypto.randomBytes(16 * 1024);\nconst data3 = crypto.randomBytes(64 * 1024);\nconst data4 = crypto.randomBytes(200 * 1024);\nconst data5 = crypto.randomBytes(1024 * 1024);\n\nconst opts1 = {\n  readOnly: false,\n  mask: false,\n  rsv1: false,\n  opcode: 2,\n  fin: true\n};\nconst opts2 = {\n  readOnly: true,\n  rsv1: false,\n  mask: true,\n  opcode: 2,\n  fin: true\n};\n\nconst suite = new benchmark.Suite();\n\nsuite.add('frame, unmasked (64 B)', () => Sender.frame(data1, opts1));\nsuite.add('frame, masked (64 B)', () => Sender.frame(data1, opts2));\nsuite.add('frame, unmasked (16 KiB)', () => Sender.frame(data2, opts1));\nsuite.add('frame, masked (16 KiB)', () => Sender.frame(data2, opts2));\nsuite.add('frame, unmasked (64 KiB)', () => Sender.frame(data3, opts1));\nsuite.add('frame, masked (64 KiB)', () => Sender.frame(data3, opts2));\nsuite.add('frame, unmasked (200 KiB)', () => Sender.frame(data4, opts1));\nsuite.add('frame, masked (200 KiB)', () => Sender.frame(data4, opts2));\nsuite.add('frame, unmasked (1 MiB)', () => Sender.frame(data5, opts1));\nsuite.add('frame, masked (1 MiB)', () => Sender.frame(data5, opts2));\n\nsuite.on('cycle', (e) => console.log(e.target.toString()));\n\nif (require.main === module) {\n  suite.run({ async: true });\n} else {\n  module.exports = suite;\n}\n"
  },
  {
    "path": "bench/speed.js",
    "content": "'use strict';\n\nconst cluster = require('cluster');\nconst http = require('http');\n\nconst WebSocket = require('..');\n\nconst port = 8181;\nconst path = '';\n// const path = '/tmp/wss.sock';\n\nif (cluster.isMaster) {\n  const server = http.createServer();\n  const wss = new WebSocket.Server({\n    maxPayload: 600 * 1024 * 1024,\n    perMessageDeflate: false,\n    clientTracking: false,\n    server\n  });\n\n  wss.on('connection', (ws) => {\n    ws.on('message', (data, isBinary) => {\n      ws.send(data, { binary: isBinary });\n    });\n  });\n\n  server.listen(path ? { path } : { port }, () => cluster.fork());\n\n  cluster.on('exit', () => {\n    wss.close();\n    server.close();\n  });\n} else {\n  const configs = [\n    [true, 10000, 64],\n    [true, 5000, 16 * 1024],\n    [true, 1000, 128 * 1024],\n    [true, 100, 1024 * 1024],\n    [true, 1, 500 * 1024 * 1024],\n    [false, 10000, 64],\n    [false, 5000, 16 * 1024],\n    [false, 1000, 128 * 1024],\n    [false, 100, 1024 * 1024]\n  ];\n\n  const roundPrec = (num, prec) => {\n    const mul = Math.pow(10, prec);\n    return Math.round(num * mul) / mul;\n  };\n\n  const humanSize = (bytes) => {\n    if (bytes >= 1073741824) return roundPrec(bytes / 1073741824, 2) + ' GiB';\n    if (bytes >= 1048576) return roundPrec(bytes / 1048576, 2) + ' MiB';\n    if (bytes >= 1024) return roundPrec(bytes / 1024, 2) + ' KiB';\n    return roundPrec(bytes, 2) + ' B';\n  };\n\n  const largest = configs.reduce(\n    (prev, curr) => (curr[2] > prev ? curr[2] : prev),\n    0\n  );\n  console.log('Generating %s of test data...', humanSize(largest));\n  const randomBytes = Buffer.allocUnsafe(largest);\n\n  for (let i = 0; i < largest; ++i) {\n    randomBytes[i] = ~~(Math.random() * 127);\n  }\n\n  console.log(`Testing ws on ${path || '[::]:' + port}`);\n\n  const runConfig = (useBinary, roundtrips, size, cb) => {\n    const data = randomBytes.slice(0, size);\n    const url = path ? `ws+unix://${path}` : `ws://localhost:${port}`;\n    const ws = new WebSocket(url, {\n      maxPayload: 600 * 1024 * 1024\n    });\n    let roundtrip = 0;\n    let time;\n\n    ws.on('error', (err) => {\n      console.error(err.stack);\n      cluster.worker.disconnect();\n    });\n    ws.on('open', () => {\n      time = process.hrtime();\n      ws.send(data, { binary: useBinary });\n    });\n    ws.on('message', () => {\n      if (++roundtrip !== roundtrips)\n        return ws.send(data, { binary: useBinary });\n\n      let elapsed = process.hrtime(time);\n      elapsed = elapsed[0] * 1e9 + elapsed[1];\n\n      console.log(\n        '%d roundtrips of %s %s data:\\t%ss\\t%s',\n        roundtrips,\n        humanSize(size),\n        useBinary ? 'binary' : 'text',\n        roundPrec(elapsed / 1e9, 1),\n        humanSize(((size * 2 * roundtrips) / elapsed) * 1e9) + '/s'\n      );\n\n      ws.close();\n      cb();\n    });\n  };\n\n  (function run() {\n    if (configs.length === 0) return cluster.worker.disconnect();\n    const config = configs.shift();\n    config.push(run);\n    runConfig.apply(null, config);\n  })();\n}\n"
  },
  {
    "path": "browser.js",
    "content": "'use strict';\n\nmodule.exports = function () {\n  throw new Error(\n    'ws does not work in the browser. Browser clients must use the native ' +\n      'WebSocket object'\n  );\n};\n"
  },
  {
    "path": "doc/ws.md",
    "content": "# ws\n\n## Table of Contents\n\n- [Class: WebSocketServer](#class-websocketserver)\n  - [new WebSocketServer(options[, callback])](#new-websocketserveroptions-callback)\n  - [Event: 'close'](#event-close)\n  - [Event: 'connection'](#event-connection)\n  - [Event: 'error'](#event-error)\n  - [Event: 'headers'](#event-headers)\n  - [Event: 'listening'](#event-listening)\n  - [Event: 'wsClientError'](#event-wsclienterror)\n  - [server.address()](#serveraddress)\n  - [server.clients](#serverclients)\n  - [server.close([callback])](#serverclosecallback)\n  - [server.handleUpgrade(request, socket, head, callback)](#serverhandleupgraderequest-socket-head-callback)\n  - [server.shouldHandle(request)](#servershouldhandlerequest)\n- [Class: WebSocket](#class-websocket)\n  - [Ready state constants](#ready-state-constants)\n  - [new WebSocket(address[, protocols][, options])](#new-websocketaddress-protocols-options)\n    - [IPC connections](#ipc-connections)\n  - [Event: 'close'](#event-close-1)\n  - [Event: 'error'](#event-error-1)\n  - [Event: 'message'](#event-message)\n  - [Event: 'open'](#event-open)\n  - [Event: 'ping'](#event-ping)\n  - [Event: 'pong'](#event-pong)\n  - [Event: 'redirect'](#event-redirect)\n  - [Event: 'unexpected-response'](#event-unexpected-response)\n  - [Event: 'upgrade'](#event-upgrade)\n  - [websocket.addEventListener(type, listener[, options])](#websocketaddeventlistenertype-listener-options)\n  - [websocket.binaryType](#websocketbinarytype)\n  - [websocket.bufferedAmount](#websocketbufferedamount)\n  - [websocket.close([code[, reason]])](#websocketclosecode-reason)\n  - [websocket.extensions](#websocketextensions)\n  - [websocket.isPaused](#websocketispaused)\n  - [websocket.onclose](#websocketonclose)\n  - [websocket.onerror](#websocketonerror)\n  - [websocket.onmessage](#websocketonmessage)\n  - [websocket.onopen](#websocketonopen)\n  - [websocket.pause()](#websocketpause)\n  - [websocket.ping([data[, mask]][, callback])](#websocketpingdata-mask-callback)\n  - [websocket.pong([data[, mask]][, callback])](#websocketpongdata-mask-callback)\n  - [websocket.protocol](#websocketprotocol)\n  - [websocket.readyState](#websocketreadystate)\n  - [websocket.removeEventListener(type, listener)](#websocketremoveeventlistenertype-listener)\n  - [websocket.resume()](#websocketresume)\n  - [websocket.send(data[, options][, callback])](#websocketsenddata-options-callback)\n  - [websocket.terminate()](#websocketterminate)\n  - [websocket.url](#websocketurl)\n- [createWebSocketStream(websocket[, options])](#createwebsocketstreamwebsocket-options)\n- [Environment variables](#environment-variables)\n  - [WS_NO_BUFFER_UTIL](#ws_no_buffer_util)\n  - [WS_NO_UTF_8_VALIDATE](#ws_no_utf_8_validate)\n- [Error codes](#error-codes)\n  - [WS_ERR_EXPECTED_FIN](#ws_err_expected_fin)\n  - [WS_ERR_EXPECTED_MASK](#ws_err_expected_mask)\n  - [WS_ERR_INVALID_CLOSE_CODE](#ws_err_invalid_close_code)\n  - [WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH](#ws_err_invalid_control_payload_length)\n  - [WS_ERR_INVALID_OPCODE](#ws_err_invalid_opcode)\n  - [WS_ERR_INVALID_UTF8](#ws_err_invalid_utf8)\n  - [WS_ERR_UNEXPECTED_MASK](#ws_err_unexpected_mask)\n  - [WS_ERR_UNEXPECTED_RSV_1](#ws_err_unexpected_rsv_1)\n  - [WS_ERR_UNEXPECTED_RSV_2_3](#ws_err_unexpected_rsv_2_3)\n  - [WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH](#ws_err_unsupported_data_payload_length)\n  - [WS_ERR_UNSUPPORTED_MESSAGE_LENGTH](#ws_err_unsupported_message_length)\n\n## Class: WebSocketServer\n\nThis class represents a WebSocket server. It extends the `EventEmitter`.\n\n### new WebSocketServer(options[, callback])\n\n- `options` {Object}\n  - `allowSynchronousEvents` {Boolean} Specifies whether any of the `'message'`,\n    `'ping'`, and `'pong'` events can be emitted multiple times in the same\n    tick. Defaults to `true`. Setting it to `false` improves compatibility with\n    the WHATWG standard but may negatively impact performance.\n  - `autoPong` {Boolean} Specifies whether or not to automatically send a pong\n    in response to a ping. Defaults to `true`.\n  - `backlog` {Number} The maximum length of the queue of pending connections.\n  - `clientTracking` {Boolean} Specifies whether or not to track clients.\n  - `closeTimeout` {Number} Duration in milliseconds to wait for a graceful\n    close after [`websocket.close()`][] is called. If the limit is reached, the\n    connection is forcibly terminated. Defaults to 30000.\n  - `handleProtocols` {Function} A function which can be used to handle the\n    WebSocket subprotocols. See description below.\n  - `host` {String} The hostname where to bind the server.\n  - `maxPayload` {Number} The maximum allowed message size in bytes. Defaults to\n    100 MiB (104857600 bytes).\n  - `noServer` {Boolean} Enable no server mode.\n  - `path` {String} Accept only connections matching this path.\n  - `perMessageDeflate` {Boolean|Object} Enable/disable permessage-deflate.\n  - `port` {Number} The port where to bind the server.\n  - `server` {http.Server|https.Server} A pre-created Node.js HTTP/S server.\n  - `skipUTF8Validation` {Boolean} Specifies whether or not to skip UTF-8\n    validation for text and close messages. Defaults to `false`. Set to `true`\n    only if clients are trusted.\n  - `verifyClient` {Function} A function which can be used to validate incoming\n    connections. See description below. (Usage is discouraged: see\n    [Issue #337](https://github.com/websockets/ws/issues/377#issuecomment-462152231))\n  - `WebSocket` {Function} Specifies the `WebSocket` class to be used. It must\n    be extended from the original `WebSocket`. Defaults to `WebSocket`.\n- `callback` {Function}\n\nCreate a new server instance. One and only one of `port`, `server` or `noServer`\nmust be provided or an error is thrown. An HTTP server is automatically created,\nstarted, and used if `port` is set. To use an external HTTP/S server instead,\nspecify only `server` or `noServer`. In this case, the HTTP/S server must be\nstarted manually. The \"noServer\" mode allows the WebSocket server to be\ncompletely detached from the HTTP/S server. This makes it possible, for example,\nto share a single HTTP/S server between multiple WebSocket servers.\n\n> **NOTE:** Use of `verifyClient` is discouraged. Rather handle client\n> authentication in the `'upgrade'` event of the HTTP server. See examples for\n> more details.\n\nIf `verifyClient` is not set, then the handshake is automatically accepted. If\nit has a single parameter, then `ws` will invoke it with the following argument:\n\n- `info` {Object}\n  - `origin` {String} The value in the Origin header indicated by the client.\n  - `req` {http.IncomingMessage} The client HTTP GET request.\n  - `secure` {Boolean} `true` if `req.socket.authorized` or\n    `req.socket.encrypted` is set.\n\nThe return value (`Boolean`) of the function determines whether or not to accept\nthe handshake.\n\nIf `verifyClient` has two parameters, then `ws` will invoke it with the\nfollowing arguments:\n\n- `info` {Object} Same as above.\n- `cb` {Function} A callback that must be called by the user upon inspection of\n  the `info` fields. Arguments in this callback are:\n  - `result` {Boolean} Whether or not to accept the handshake.\n  - `code` {Number} When `result` is `false`, this field determines the HTTP\n    error status code to be sent to the client.\n  - `name` {String} When `result` is `false`, this field determines the HTTP\n    reason phrase.\n  - `headers` {Object} When `result` is `false`, this field determines\n    additional HTTP headers to be sent to the client. For example,\n    `{ 'Retry-After': 120 }`.\n\n`handleProtocols` takes two arguments:\n\n- `protocols` {Set} The list of WebSocket subprotocols indicated by the client\n  in the `Sec-WebSocket-Protocol` header.\n- `request` {http.IncomingMessage} The client HTTP GET request.\n\nThe returned value sets the value of the `Sec-WebSocket-Protocol` header in the\nHTTP 101 response. If returned value is `false`, the header is not added in the\nresponse.\n\nIf `handleProtocols` is not set, then the first of the client's requested\nsubprotocols is used.\n\n`perMessageDeflate` can be used to control the behavior of [permessage-deflate\nextension][permessage-deflate]. The extension is disabled when `false` (default\nvalue). If an object is provided, then that is extension parameters:\n\n- `serverNoContextTakeover` {Boolean} Whether to use context takeover or not.\n- `clientNoContextTakeover` {Boolean} Acknowledge disabling of client context\n  takeover.\n- `serverMaxWindowBits` {Number} The value of `windowBits`.\n- `clientMaxWindowBits` {Number} Request a custom client window size.\n- `zlibDeflateOptions` {Object} [Additional options][zlib-options] to pass to\n  zlib on deflate.\n- `zlibInflateOptions` {Object} [Additional options][zlib-options] to pass to\n  zlib on inflate.\n- `threshold` {Number} Payloads smaller than this will not be compressed if\n  context takeover is disabled. Defaults to 1024 bytes.\n- `concurrencyLimit` {Number} The number of concurrent calls to zlib. Calls\n  above this limit will be queued. Default 10. You usually won't need to touch\n  this option. See [this issue][concurrency-limit] for more details.\n\nIf a property is empty, then either an offered configuration or a default value\nis used. When sending a fragmented message, the length of the first fragment is\ncompared to the threshold. This determines if compression is used for the entire\nmessage.\n\n`callback` will be added as a listener for the `'listening'` event on the HTTP\nserver when the `port` option is set.\n\n### Event: 'close'\n\nEmitted when the server closes. This event depends on the `'close'` event of\nHTTP server only when it is created internally. In all other cases, the event is\nemitted independently.\n\n### Event: 'connection'\n\n- `websocket` {WebSocket}\n- `request` {http.IncomingMessage}\n\nEmitted when the handshake is complete. `request` is the http GET request sent\nby the client. Useful for parsing authority headers, cookie headers, and other\ninformation.\n\n### Event: 'error'\n\n- `error` {Error}\n\nEmitted when an error occurs on the underlying server.\n\n### Event: 'headers'\n\n- `headers` {Array}\n- `request` {http.IncomingMessage}\n\nEmitted before the response headers are written to the socket as part of the\nhandshake. This allows you to inspect/modify the headers before they are sent.\n\n### Event: 'listening'\n\nEmitted when the underlying server has been bound.\n\n### Event: 'wsClientError'\n\n- `error` {Error}\n- `socket` {net.Socket|tls.Socket}\n- `request` {http.IncomingMessage}\n\nEmitted when an error occurs before the WebSocket connection is established.\n`socket` and `request` are respectively the socket and the HTTP request from\nwhich the error originated. The listener of this event is responsible for\nclosing the socket. When the `'wsClientError'` event is emitted there is no\n`http.ServerResponse` object, so any HTTP response, including the response\nheaders and body, must be written directly to the `socket`. If there is no\nlistener for this event, the socket is closed with a default 4xx response\ncontaining a descriptive error message.\n\n### server.address()\n\nReturns an object with `port`, `family`, and `address` properties specifying the\nbound address, the address family name, and port of the server as reported by\nthe operating system if listening on an IP socket. If the server is listening on\na pipe or UNIX domain socket, the name is returned as a string.\n\n### server.clients\n\n- {Set}\n\nA set that stores all connected clients. This property is only added when the\n`clientTracking` is truthy.\n\n### server.close([callback])\n\nPrevent the server from accepting new connections and close the HTTP server if\ncreated internally. If an external HTTP server is used via the `server` or\n`noServer` constructor options, it must be closed manually. Existing connections\nare not closed automatically. The server emits a `'close'` event when all\nconnections are closed unless an external HTTP server is used and client\ntracking is disabled. In this case, the `'close'` event is emitted in the next\ntick. The optional callback is called when the `'close'` event occurs and\nreceives an `Error` if the server is already closed.\n\n### server.handleUpgrade(request, socket, head, callback)\n\n- `request` {http.IncomingMessage} The client HTTP GET request.\n- `socket` {stream.Duplex} The network socket between the server and client.\n- `head` {Buffer} The first packet of the upgraded stream.\n- `callback` {Function}\n\nHandle a HTTP upgrade request. When the HTTP server is created internally or\nwhen the HTTP server is passed via the `server` option, this method is called\nautomatically. When operating in \"noServer\" mode, this method must be called\nmanually.\n\nIf the upgrade is successful, the `callback` is called with two arguments:\n\n- `websocket` {WebSocket} A `WebSocket` object.\n- `request` {http.IncomingMessage} The client HTTP GET request.\n\n### server.shouldHandle(request)\n\n- `request` {http.IncomingMessage} The client HTTP GET request.\n\nSee if a given request should be handled by this server. By default, this method\nvalidates the pathname of the request, matching it against the `path` option if\nprovided. The return value, `true` or `false`, determines whether or not to\naccept the handshake.\n\nThis method can be overridden when a custom handling logic is required.\n\n## Class: WebSocket\n\nThis class represents a WebSocket. It extends the `EventEmitter`.\n\n### Ready state constants\n\n| Constant   | Value | Description                                      |\n| ---------- | ----- | ------------------------------------------------ |\n| CONNECTING | 0     | The connection is not yet open.                  |\n| OPEN       | 1     | The connection is open and ready to communicate. |\n| CLOSING    | 2     | The connection is in the process of closing.     |\n| CLOSED     | 3     | The connection is closed.                        |\n\n### new WebSocket(address[, protocols][, options])\n\n- `address` {String|url.URL} The URL to which to connect.\n- `protocols` {String|Array} The list of subprotocols.\n- `options` {Object}\n  - `allowSynchronousEvents` {Boolean} Specifies whether any of the `'message'`,\n    `'ping'`, and `'pong'` events can be emitted multiple times in the same\n    tick. Defaults to `true`. Setting it to `false` improves compatibility with\n    the WHATWG standard but may negatively impact performance.\n  - `autoPong` {Boolean} Specifies whether or not to automatically send a pong\n    in response to a ping. Defaults to `true`.\n  - `closeTimeout` {Number} Duration in milliseconds to wait for a graceful\n    close after [`websocket.close()`][] is called. If the limit is reached, the\n    connection is forcibly terminated. Defaults to 30000.\n  - `finishRequest` {Function} A function which can be used to customize the\n    headers of each HTTP request before it is sent. See description below.\n  - `followRedirects` {Boolean} Whether or not to follow redirects. Defaults to\n    `false`.\n  - `generateMask` {Function} The function used to generate the masking key. It\n    takes a `Buffer` that must be filled synchronously and is called before a\n    message is sent, for each message. By default, the buffer is filled with\n    cryptographically strong random bytes.\n  - `handshakeTimeout` {Number} Timeout in milliseconds for the handshake\n    request. This is reset after every redirection.\n  - `maxPayload` {Number} The maximum allowed message size in bytes. Defaults to\n    100 MiB (104857600 bytes).\n  - `maxRedirects` {Number} The maximum number of redirects allowed. Defaults\n    to 10.\n  - `origin` {String} Value of the `Origin` or `Sec-WebSocket-Origin` header\n    depending on the `protocolVersion`.\n  - `perMessageDeflate` {Boolean|Object} Enable/disable permessage-deflate.\n  - `protocolVersion` {Number} Value of the `Sec-WebSocket-Version` header.\n  - `skipUTF8Validation` {Boolean} Specifies whether or not to skip UTF-8\n    validation for text and close messages. Defaults to `false`. Set to `true`\n    only if the server is trusted.\n  - Any other option allowed in [`http.request()`][] or [`https.request()`][].\n    Options given do not have any effect if parsed from the URL given with the\n    `address` parameter.\n\nCreate a new WebSocket instance.\n\n`perMessageDeflate` default value is `true`. When using an object, parameters\nare the same of the server. The only difference is the direction of requests.\nFor example, `serverNoContextTakeover` can be used to ask the server to disable\ncontext takeover.\n\n`finishRequest` is called with arguments\n\n- `request` {http.ClientRequest}\n- `websocket` {WebSocket}\n\nfor each HTTP GET request (the initial one and any caused by redirects) when it\nis ready to be sent, to allow for last minute customization of the headers. If\n`finishRequest` is set, then it has the responsibility to call `request.end()`\nonce it is done setting request headers. This is intended for niche use-cases\nwhere some headers can't be provided in advance e.g. because they depend on the\nunderlying socket.\n\n#### IPC connections\n\n`ws` supports IPC connections. To connect to an IPC endpoint, use the following\nURL form:\n\n- On Unices\n\n  ```\n  ws+unix:/absolute/path/to/uds_socket:/pathname?search_params\n  ```\n\n- On Windows\n\n  ```\n  ws+unix:\\\\.\\pipe\\pipe_name:/pathname?search_params\n  ```\n\nThe character `:` is the separator between the IPC path (the UNIX domain socket\npath or the Windows named pipe) and the URL path. The IPC path must not include\nthe characters `:` and `?`, otherwise the URL is incorrectly parsed. If the URL\npath is omitted\n\n```\nws+unix:/absolute/path/to/uds_socket\n```\n\nit defaults to `/`.\n\n### Event: 'close'\n\n- `code` {Number}\n- `reason` {Buffer}\n\nEmitted when the connection is closed. `code` is a numeric value indicating the\nstatus code explaining why the connection has been closed. `reason` is a\n`Buffer` containing a human-readable string explaining why the connection has\nbeen closed.\n\n### Event: 'error'\n\n- `error` {Error}\n\nEmitted when an error occurs. Errors may have a `.code` property, matching one\nof the string values defined below under [Error codes](#error-codes).\n\n### Event: 'message'\n\n- `data` {ArrayBuffer|Blob|Buffer|Buffer[]}\n- `isBinary` {Boolean}\n\nEmitted when a message is received. `data` is the message content. `isBinary`\nspecifies whether the message is binary or not.\n\n### Event: 'open'\n\nEmitted when the connection is established.\n\n### Event: 'ping'\n\n- `data` {Buffer}\n\nEmitted when a ping is received.\n\n### Event: 'pong'\n\n- `data` {Buffer}\n\nEmitted when a pong is received.\n\n### Event: 'redirect'\n\n- `url` {String}\n- `request` {http.ClientRequest}\n\nEmitted before a redirect is followed. `url` is the redirect URL. `request` is\nthe HTTP GET request with the headers queued. This event gives the ability to\ninspect confidential headers and remove them on a per-redirect basis using the\n[`request.getHeader()`][] and [`request.removeHeader()`][] API. The `request`\nobject should be used only for this purpose. When there is at least one listener\nfor this event, no header is removed by default, even if the redirect is to a\ndifferent domain.\n\n### Event: 'unexpected-response'\n\n- `request` {http.ClientRequest}\n- `response` {http.IncomingMessage}\n\nEmitted when the server response is not the expected one, for example a 401\nresponse. This event gives the ability to read the response in order to extract\nuseful information. If the server sends an invalid response and there isn't a\nlistener for this event, an error is emitted.\n\n### Event: 'upgrade'\n\n- `response` {http.IncomingMessage}\n\nEmitted when response headers are received from the server as part of the\nhandshake. This allows you to read headers from the server, for example\n'set-cookie' headers.\n\n### websocket.addEventListener(type, listener[, options])\n\n- `type` {String} A string representing the event type to listen for.\n- `listener` {Function|Object} The listener to add.\n- `options` {Object}\n  - `once` {Boolean} A `Boolean` indicating that the listener should be invoked\n    at most once after being added. If `true`, the listener would be\n    automatically removed when invoked.\n\nRegister an event listener emulating the `EventTarget` interface. This method\ndoes nothing if `type` is not one of `'close'`, `'error'`, `'message'`, or\n`'open'`.\n\n### websocket.binaryType\n\n- {String}\n\nA string indicating the type of binary data being transmitted by the connection.\nThis should be one of \"nodebuffer\", \"arraybuffer\", \"blob\", or \"fragments\".\nDefaults to \"nodebuffer\". Type \"fragments\" will emit the array of fragments as\nreceived from the sender, without copyfull concatenation, which is useful for\nthe performance of binary protocols transferring large messages with multiple\nfragments.\n\n### websocket.bufferedAmount\n\n- {Number}\n\nThe number of bytes of data that have been queued using calls to `send()` but\nnot yet transmitted to the network. This deviates from the HTML standard in the\nfollowing ways:\n\n1. If the data is immediately sent, the value is `0`.\n1. All framing bytes are included.\n\n### websocket.close([code[, reason]])\n\n- `code` {Number} A numeric value indicating the status code explaining why the\n  connection is being closed.\n- `reason` {String|Buffer} The reason why the connection is closing.\n\nInitiate a closing handshake.\n\n### websocket.isPaused\n\n- {Boolean}\n\nIndicates whether the websocket is paused.\n\n### websocket.extensions\n\n- {Object}\n\nAn object containing the negotiated extensions.\n\n### websocket.onclose\n\n- {Function}\n\nAn event listener to be called when connection is closed. The listener receives\na `CloseEvent` named \"close\".\n\n### websocket.onerror\n\n- {Function}\n\nAn event listener to be called when an error occurs. The listener receives an\n`ErrorEvent` named \"error\".\n\n### websocket.onmessage\n\n- {Function}\n\nAn event listener to be called when a message is received. The listener receives\na `MessageEvent` named \"message\".\n\n### websocket.onopen\n\n- {Function}\n\nAn event listener to be called when the connection is established. The listener\nreceives an `OpenEvent` named \"open\".\n\n### websocket.pause()\n\nPause the websocket causing it to stop emitting events. Some events can still be\nemitted after this is called, until all buffered data is consumed. This method\nis a noop if the ready state is `CONNECTING` or `CLOSED`.\n\n### websocket.ping([data[, mask]][, callback])\n\n- `data`\n  {Array|Number|Object|String|ArrayBuffer|Buffer|DataView|TypedArray|Blob} The\n  data to send in the ping frame.\n- `mask` {Boolean} Specifies whether `data` should be masked or not. Defaults to\n  `true` when `websocket` is not a server client.\n- `callback` {Function} An optional callback which is invoked when the ping\n  frame is written out. If an error occurs, the callback is called with the\n  error as its first argument.\n\nSend a ping. This method throws an error if the ready state is `CONNECTING`.\n\n### websocket.pong([data[, mask]][, callback])\n\n- `data`\n  {Array|Number|Object|String|ArrayBuffer|Buffer|DataView|TypedArray|Blob} The\n  data to send in the pong frame.\n- `mask` {Boolean} Specifies whether `data` should be masked or not. Defaults to\n  `true` when `websocket` is not a server client.\n- `callback` {Function} An optional callback which is invoked when the pong\n  frame is written out. If an error occurs, the callback is called with the\n  error as its first argument.\n\nSend a pong. This method throws an error if the ready state is `CONNECTING`.\n\n### websocket.protocol\n\n- {String}\n\nThe subprotocol selected by the server.\n\n### websocket.resume()\n\nMake a paused socket resume emitting events. This method is a noop if the ready\nstate is `CONNECTING` or `CLOSED`.\n\n### websocket.readyState\n\n- {Number}\n\nThe current state of the connection. This is one of the ready state constants.\n\n### websocket.removeEventListener(type, listener)\n\n- `type` {String} A string representing the event type to remove.\n- `listener` {Function|Object} The listener to remove.\n\nRemoves an event listener emulating the `EventTarget` interface. This method\nonly removes listeners added with\n[`websocket.addEventListener()`](#websocketaddeventlistenertype-listener-options).\n\n### websocket.send(data[, options][, callback])\n\n- `data`\n  {Array|Number|Object|String|ArrayBuffer|Buffer|DataView|TypedArray|Blob} The\n  data to send. `Object` values are only supported if they conform to the\n  requirements of [`Buffer.from()`][]. If those constraints are not met, a\n  `TypeError` is thrown.\n- `options` {Object}\n  - `binary` {Boolean} Specifies whether `data` should be sent as a binary or\n    not. Default is autodetected.\n  - `compress` {Boolean} Specifies whether `data` should be compressed or not.\n    Defaults to `true` when permessage-deflate is enabled.\n  - `fin` {Boolean} Specifies whether `data` is the last fragment of a message\n    or not. Defaults to `true`.\n  - `mask` {Boolean} Specifies whether `data` should be masked or not. Defaults\n    to `true` when `websocket` is not a server client.\n- `callback` {Function} An optional callback which is invoked when `data` is\n  written out. If an error occurs, the callback is called with the error as its\n  first argument.\n\nSend `data` through the connection. This method throws an error if the ready\nstate is `CONNECTING`.\n\n### websocket.terminate()\n\nForcibly close the connection. Internally, this calls [`socket.destroy()`][].\n\n### websocket.url\n\n- {String}\n\nThe URL of the WebSocket server. Server clients don't have this attribute.\n\n## createWebSocketStream(websocket[, options])\n\n- `websocket` {WebSocket} A `WebSocket` object.\n- `options` {Object} [Options][duplex-options] to pass to the `Duplex`\n  constructor.\n\nReturns a `Duplex` stream that allows to use the Node.js streams API on top of a\ngiven `WebSocket`.\n\n## Environment variables\n\n### WS_NO_BUFFER_UTIL\n\nWhen set to a non-empty value, prevents the optional `bufferutil` dependency\nfrom being required.\n\n### WS_NO_UTF_8_VALIDATE\n\nWhen set to a non-empty value, prevents the optional `utf-8-validate` dependency\nfrom being required.\n\n## Error codes\n\nErrors emitted by the websocket may have a `.code` property, describing the\nspecific type of error that has occurred:\n\n### WS_ERR_EXPECTED_FIN\n\nA WebSocket frame was received with the FIN bit not set when it was expected.\n\n### WS_ERR_EXPECTED_MASK\n\nAn unmasked WebSocket frame was received by a WebSocket server.\n\n### WS_ERR_INVALID_CLOSE_CODE\n\nA WebSocket close frame was received with an invalid close code.\n\n### WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH\n\nA control frame with an invalid payload length was received.\n\n### WS_ERR_INVALID_OPCODE\n\nA WebSocket frame was received with an invalid opcode.\n\n### WS_ERR_INVALID_UTF8\n\nA text or close frame was received containing invalid UTF-8 data.\n\n### WS_ERR_UNEXPECTED_MASK\n\nA masked WebSocket frame was received by a WebSocket client.\n\n### WS_ERR_UNEXPECTED_RSV_1\n\nA WebSocket frame was received with the RSV1 bit set unexpectedly.\n\n### WS_ERR_UNEXPECTED_RSV_2_3\n\nA WebSocket frame was received with the RSV2 or RSV3 bit set unexpectedly.\n\n### WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH\n\nA data frame was received with a length longer than the max supported length\n(2^53 - 1, due to JavaScript language limitations).\n\n### WS_ERR_UNSUPPORTED_MESSAGE_LENGTH\n\nA message was received with a length longer than the maximum supported length,\nas configured by the `maxPayload` option.\n\n[concurrency-limit]: https://github.com/websockets/ws/issues/1202\n[duplex-options]:\n  https://nodejs.org/api/stream.html#stream_new_stream_duplex_options\n[`buffer.from()`]:\n  https://nodejs.org/api/buffer.html#static-method-bufferfromobject-offsetorencoding-length\n[`http.request()`]:\n  https://nodejs.org/api/http.html#http_http_request_options_callback\n[`https.request()`]:\n  https://nodejs.org/api/https.html#https_https_request_options_callback\n[permessage-deflate]:\n  https://tools.ietf.org/html/draft-ietf-hybi-permessage-compression-19\n[`request.getheader()`]: https://nodejs.org/api/http.html#requestgetheadername\n[`request.removeheader()`]:\n  https://nodejs.org/api/http.html#requestremoveheadername\n[`socket.destroy()`]: https://nodejs.org/api/net.html#net_socket_destroy_error\n[`websocket.close()`]: #websocketclosecode-reason\n[zlib-options]: https://nodejs.org/api/zlib.html#zlib_class_options\n"
  },
  {
    "path": "eslint.config.js",
    "content": "'use strict';\n\nconst pluginPrettierRecommended = require('eslint-plugin-prettier/recommended');\nconst globals = require('globals');\nconst js = require('@eslint/js');\n\nmodule.exports = [\n  js.configs.recommended,\n  {\n    ignores: ['.nyc_output/', '.vscode/', 'coverage/', 'node_modules/'],\n    languageOptions: {\n      ecmaVersion: 'latest',\n      globals: {\n        ...globals.browser,\n        ...globals.mocha,\n        ...globals.node\n      },\n      sourceType: 'module'\n    },\n    rules: {\n      'no-console': 'off',\n      'no-unused-vars': ['error', { caughtErrors: 'none' }],\n      'no-var': 'error',\n      'prefer-const': 'error'\n    }\n  },\n  pluginPrettierRecommended\n];\n"
  },
  {
    "path": "examples/express-session-parse/index.js",
    "content": "'use strict';\n\nconst session = require('express-session');\nconst express = require('express');\nconst http = require('http');\nconst uuid = require('uuid');\n\nconst { WebSocketServer } = require('../..');\n\nfunction onSocketError(err) {\n  console.error(err);\n}\n\nconst app = express();\nconst map = new Map();\n\n//\n// We need the same instance of the session parser in express and\n// WebSocket server.\n//\nconst sessionParser = session({\n  saveUninitialized: false,\n  secret: '$eCuRiTy',\n  resave: false\n});\n\n//\n// Serve static files from the 'public' folder.\n//\napp.use(express.static('public'));\napp.use(sessionParser);\n\napp.post('/login', function (req, res) {\n  //\n  // \"Log in\" user and set userId to session.\n  //\n  const id = uuid.v4();\n\n  console.log(`Updating session for user ${id}`);\n  req.session.userId = id;\n  res.send({ result: 'OK', message: 'Session updated' });\n});\n\napp.delete('/logout', function (request, response) {\n  const ws = map.get(request.session.userId);\n\n  console.log('Destroying session');\n  request.session.destroy(function () {\n    if (ws) ws.close();\n\n    response.send({ result: 'OK', message: 'Session destroyed' });\n  });\n});\n\n//\n// Create an HTTP server.\n//\nconst server = http.createServer(app);\n\n//\n// Create a WebSocket server completely detached from the HTTP server.\n//\nconst wss = new WebSocketServer({ clientTracking: false, noServer: true });\n\nserver.on('upgrade', function (request, socket, head) {\n  socket.on('error', onSocketError);\n\n  console.log('Parsing session from request...');\n\n  sessionParser(request, {}, () => {\n    if (!request.session.userId) {\n      socket.write('HTTP/1.1 401 Unauthorized\\r\\n\\r\\n');\n      socket.destroy();\n      return;\n    }\n\n    console.log('Session is parsed!');\n\n    socket.removeListener('error', onSocketError);\n\n    wss.handleUpgrade(request, socket, head, function (ws) {\n      wss.emit('connection', ws, request);\n    });\n  });\n});\n\nwss.on('connection', function (ws, request) {\n  const userId = request.session.userId;\n\n  map.set(userId, ws);\n\n  ws.on('error', console.error);\n\n  ws.on('message', function (message) {\n    //\n    // Here we can now use session parameters.\n    //\n    console.log(`Received message ${message} from user ${userId}`);\n  });\n\n  ws.on('close', function () {\n    map.delete(userId);\n  });\n});\n\n//\n// Start the server.\n//\nserver.listen(8080, function () {\n  console.log('Listening on http://localhost:8080');\n});\n"
  },
  {
    "path": "examples/express-session-parse/package.json",
    "content": "{\n  \"author\": \"\",\n  \"name\": \"express-session-parse\",\n  \"version\": \"0.0.0\",\n  \"repository\": \"websockets/ws\",\n  \"dependencies\": {\n    \"express\": \"^4.16.4\",\n    \"express-session\": \"^1.16.1\",\n    \"uuid\": \"^8.3.2\"\n  }\n}\n"
  },
  {
    "path": "examples/express-session-parse/public/app.js",
    "content": "(function () {\n  const messages = document.querySelector('#messages');\n  const wsButton = document.querySelector('#wsButton');\n  const wsSendButton = document.querySelector('#wsSendButton');\n  const logout = document.querySelector('#logout');\n  const login = document.querySelector('#login');\n\n  function showMessage(message) {\n    messages.textContent += `\\n${message}`;\n    messages.scrollTop = messages.scrollHeight;\n  }\n\n  function handleResponse(response) {\n    return response.ok\n      ? response.json().then((data) => JSON.stringify(data, null, 2))\n      : Promise.reject(new Error('Unexpected response'));\n  }\n\n  login.onclick = function () {\n    fetch('/login', { method: 'POST', credentials: 'same-origin' })\n      .then(handleResponse)\n      .then(showMessage)\n      .catch(function (err) {\n        showMessage(err.message);\n      });\n  };\n\n  logout.onclick = function () {\n    fetch('/logout', { method: 'DELETE', credentials: 'same-origin' })\n      .then(handleResponse)\n      .then(showMessage)\n      .catch(function (err) {\n        showMessage(err.message);\n      });\n  };\n\n  let ws;\n\n  wsButton.onclick = function () {\n    if (ws) {\n      ws.onerror = ws.onopen = ws.onclose = null;\n      ws.close();\n    }\n\n    ws = new WebSocket(`ws://${location.host}`);\n    ws.onerror = function () {\n      showMessage('WebSocket error');\n    };\n    ws.onopen = function () {\n      showMessage('WebSocket connection established');\n    };\n    ws.onclose = function () {\n      showMessage('WebSocket connection closed');\n      ws = null;\n    };\n  };\n\n  wsSendButton.onclick = function () {\n    if (!ws) {\n      showMessage('No WebSocket connection');\n      return;\n    }\n\n    ws.send('Hello World!');\n    showMessage('Sent \"Hello World!\"');\n  };\n})();\n"
  },
  {
    "path": "examples/express-session-parse/public/index.html",
    "content": "<!DOCTYPE html>\n<html lang=\"en\">\n  <head>\n      <meta charset=\"utf-8\">\n      <title>Express session demo</title>\n  </head>\n  <body>\n    <h1>Choose an action.</h1>\n    <button id=\"login\" type=\"button\" title=\"Simulate login\">\n      Simulate login\n    </button>\n    <button id=\"logout\" type=\"button\" title=\"Simulate logout\">\n      Simulate logout\n    </button>\n    <button id=\"wsButton\" type=\"button\" title=\"Open WebSocket connection\">\n      Open WebSocket connection\n    </button>\n    <button id=\"wsSendButton\" type=\"button\" title=\"Send WebSocket message\">\n      Send WebSocket message\n    </button>\n    <pre id=\"messages\" style=\"height: 400px; overflow: scroll\"></pre>\n    <script src=\"app.js\"></script>\n  </body>\n</html>\n"
  },
  {
    "path": "examples/server-stats/index.js",
    "content": "'use strict';\n\nconst express = require('express');\nconst path = require('path');\nconst { createServer } = require('http');\n\nconst { WebSocketServer } = require('../..');\n\nconst app = express();\napp.use(express.static(path.join(__dirname, '/public')));\n\nconst server = createServer(app);\nconst wss = new WebSocketServer({ server });\n\nwss.on('connection', function (ws) {\n  const id = setInterval(function () {\n    ws.send(JSON.stringify(process.memoryUsage()), function () {\n      //\n      // Ignore errors.\n      //\n    });\n  }, 100);\n  console.log('started client interval');\n\n  ws.on('error', console.error);\n\n  ws.on('close', function () {\n    console.log('stopping client interval');\n    clearInterval(id);\n  });\n});\n\nserver.listen(8080, function () {\n  console.log('Listening on http://localhost:8080');\n});\n"
  },
  {
    "path": "examples/server-stats/package.json",
    "content": "{\n  \"author\": \"\",\n  \"name\": \"serverstats\",\n  \"version\": \"0.0.0\",\n  \"repository\": \"websockets/ws\",\n  \"dependencies\": {\n    \"express\": \"^4.16.4\"\n  }\n}\n"
  },
  {
    "path": "examples/server-stats/public/index.html",
    "content": "<!DOCTYPE html>\n<html lang=\"en\">\n  <head>\n    <meta charset=\"utf-8\">\n    <title>Server stats</title>\n    <style>\n      table, td {\n        border: 1px solid #333;\n      }\n\n      thead {\n        background-color: #333;\n        color: #fff;\n      }\n    </style>\n  </head>\n  <body>\n    <h1>Server stats</h1>\n    <table>\n      <thead>\n        <tr>\n          <th colspan=\"2\">Memory usage</th>\n        </tr>\n      </thead>\n      <tbody>\n        <tr>\n          <td>RSS</td>\n          <td id=\"rss\"></td>\n        </tr>\n        <tr>\n          <td>Heap total</td>\n          <td id=\"heapTotal\"></td>\n        </tr>\n        <tr>\n          <td>Heap used</td>\n          <td id=\"heapUsed\"></td>\n        </tr>\n        <tr>\n          <td>External</td>\n          <td id=\"external\"></td>\n        </tr>\n      </tbody>\n    </table>\n    <script>\n      (function() {\n        const rss = document.getElementById('rss');\n        const heapTotal = document.getElementById('heapTotal');\n        const heapUsed = document.getElementById('heapUsed');\n        const external = document.getElementById('external');\n        const ws = new WebSocket(`ws://${location.host}`);\n\n        ws.onmessage = function(event) {\n          const data = JSON.parse(event.data);\n\n          rss.textContent = data.rss;\n          heapTotal.textContent = data.heapTotal;\n          heapUsed.textContent = data.heapUsed;\n          external.textContent = data.external;\n        };\n      })();\n    </script>\n  </body>\n</html>\n"
  },
  {
    "path": "examples/ssl.js",
    "content": "'use strict';\n\nconst https = require('https');\nconst fs = require('fs');\n\nconst { WebSocket, WebSocketServer } = require('..');\n\nconst server = https.createServer({\n  cert: fs.readFileSync('../test/fixtures/certificate.pem'),\n  key: fs.readFileSync('../test/fixtures/key.pem')\n});\n\nconst wss = new WebSocketServer({ server });\n\nwss.on('connection', function connection(ws) {\n  ws.on('error', console.error);\n\n  ws.on('message', function message(msg) {\n    console.log(msg.toString());\n  });\n});\n\nserver.listen(function listening() {\n  //\n  // If the `rejectUnauthorized` option is not `false`, the server certificate\n  // is verified against a list of well-known CAs. An 'error' event is emitted\n  // if verification fails.\n  //\n  // The certificate used in this example is self-signed so `rejectUnauthorized`\n  // is set to `false`.\n  //\n  const ws = new WebSocket(`wss://localhost:${server.address().port}`, {\n    rejectUnauthorized: false\n  });\n\n  ws.on('error', console.error);\n\n  ws.on('open', function open() {\n    ws.send('All glory to WebSockets!');\n  });\n});\n"
  },
  {
    "path": "index.js",
    "content": "'use strict';\n\nconst createWebSocketStream = require('./lib/stream');\nconst extension = require('./lib/extension');\nconst PerMessageDeflate = require('./lib/permessage-deflate');\nconst Receiver = require('./lib/receiver');\nconst Sender = require('./lib/sender');\nconst subprotocol = require('./lib/subprotocol');\nconst WebSocket = require('./lib/websocket');\nconst WebSocketServer = require('./lib/websocket-server');\n\nWebSocket.createWebSocketStream = createWebSocketStream;\nWebSocket.extension = extension;\nWebSocket.PerMessageDeflate = PerMessageDeflate;\nWebSocket.Receiver = Receiver;\nWebSocket.Sender = Sender;\nWebSocket.Server = WebSocketServer;\nWebSocket.subprotocol = subprotocol;\nWebSocket.WebSocket = WebSocket;\nWebSocket.WebSocketServer = WebSocketServer;\n\nmodule.exports = WebSocket;\n"
  },
  {
    "path": "lib/buffer-util.js",
    "content": "'use strict';\n\nconst { EMPTY_BUFFER } = require('./constants');\n\nconst FastBuffer = Buffer[Symbol.species];\n\n/**\n * Merges an array of buffers into a new buffer.\n *\n * @param {Buffer[]} list The array of buffers to concat\n * @param {Number} totalLength The total length of buffers in the list\n * @return {Buffer} The resulting buffer\n * @public\n */\nfunction concat(list, totalLength) {\n  if (list.length === 0) return EMPTY_BUFFER;\n  if (list.length === 1) return list[0];\n\n  const target = Buffer.allocUnsafe(totalLength);\n  let offset = 0;\n\n  for (let i = 0; i < list.length; i++) {\n    const buf = list[i];\n    target.set(buf, offset);\n    offset += buf.length;\n  }\n\n  if (offset < totalLength) {\n    return new FastBuffer(target.buffer, target.byteOffset, offset);\n  }\n\n  return target;\n}\n\n/**\n * Masks a buffer using the given mask.\n *\n * @param {Buffer} source The buffer to mask\n * @param {Buffer} mask The mask to use\n * @param {Buffer} output The buffer where to store the result\n * @param {Number} offset The offset at which to start writing\n * @param {Number} length The number of bytes to mask.\n * @public\n */\nfunction _mask(source, mask, output, offset, length) {\n  for (let i = 0; i < length; i++) {\n    output[offset + i] = source[i] ^ mask[i & 3];\n  }\n}\n\n/**\n * Unmasks a buffer using the given mask.\n *\n * @param {Buffer} buffer The buffer to unmask\n * @param {Buffer} mask The mask to use\n * @public\n */\nfunction _unmask(buffer, mask) {\n  for (let i = 0; i < buffer.length; i++) {\n    buffer[i] ^= mask[i & 3];\n  }\n}\n\n/**\n * Converts a buffer to an `ArrayBuffer`.\n *\n * @param {Buffer} buf The buffer to convert\n * @return {ArrayBuffer} Converted buffer\n * @public\n */\nfunction toArrayBuffer(buf) {\n  if (buf.length === buf.buffer.byteLength) {\n    return buf.buffer;\n  }\n\n  return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.length);\n}\n\n/**\n * Converts `data` to a `Buffer`.\n *\n * @param {*} data The data to convert\n * @return {Buffer} The buffer\n * @throws {TypeError}\n * @public\n */\nfunction toBuffer(data) {\n  toBuffer.readOnly = true;\n\n  if (Buffer.isBuffer(data)) return data;\n\n  let buf;\n\n  if (data instanceof ArrayBuffer) {\n    buf = new FastBuffer(data);\n  } else if (ArrayBuffer.isView(data)) {\n    buf = new FastBuffer(data.buffer, data.byteOffset, data.byteLength);\n  } else {\n    buf = Buffer.from(data);\n    toBuffer.readOnly = false;\n  }\n\n  return buf;\n}\n\nmodule.exports = {\n  concat,\n  mask: _mask,\n  toArrayBuffer,\n  toBuffer,\n  unmask: _unmask\n};\n\n/* istanbul ignore else  */\nif (!process.env.WS_NO_BUFFER_UTIL) {\n  try {\n    const bufferUtil = require('bufferutil');\n\n    module.exports.mask = function (source, mask, output, offset, length) {\n      if (length < 48) _mask(source, mask, output, offset, length);\n      else bufferUtil.mask(source, mask, output, offset, length);\n    };\n\n    module.exports.unmask = function (buffer, mask) {\n      if (buffer.length < 32) _unmask(buffer, mask);\n      else bufferUtil.unmask(buffer, mask);\n    };\n  } catch (e) {\n    // Continue regardless of the error.\n  }\n}\n"
  },
  {
    "path": "lib/constants.js",
    "content": "'use strict';\n\nconst BINARY_TYPES = ['nodebuffer', 'arraybuffer', 'fragments'];\nconst hasBlob = typeof Blob !== 'undefined';\n\nif (hasBlob) BINARY_TYPES.push('blob');\n\nmodule.exports = {\n  BINARY_TYPES,\n  CLOSE_TIMEOUT: 30000,\n  EMPTY_BUFFER: Buffer.alloc(0),\n  GUID: '258EAFA5-E914-47DA-95CA-C5AB0DC85B11',\n  hasBlob,\n  kForOnEventAttribute: Symbol('kIsForOnEventAttribute'),\n  kListener: Symbol('kListener'),\n  kStatusCode: Symbol('status-code'),\n  kWebSocket: Symbol('websocket'),\n  NOOP: () => {}\n};\n"
  },
  {
    "path": "lib/event-target.js",
    "content": "'use strict';\n\nconst { kForOnEventAttribute, kListener } = require('./constants');\n\nconst kCode = Symbol('kCode');\nconst kData = Symbol('kData');\nconst kError = Symbol('kError');\nconst kMessage = Symbol('kMessage');\nconst kReason = Symbol('kReason');\nconst kTarget = Symbol('kTarget');\nconst kType = Symbol('kType');\nconst kWasClean = Symbol('kWasClean');\n\n/**\n * Class representing an event.\n */\nclass Event {\n  /**\n   * Create a new `Event`.\n   *\n   * @param {String} type The name of the event\n   * @throws {TypeError} If the `type` argument is not specified\n   */\n  constructor(type) {\n    this[kTarget] = null;\n    this[kType] = type;\n  }\n\n  /**\n   * @type {*}\n   */\n  get target() {\n    return this[kTarget];\n  }\n\n  /**\n   * @type {String}\n   */\n  get type() {\n    return this[kType];\n  }\n}\n\nObject.defineProperty(Event.prototype, 'target', { enumerable: true });\nObject.defineProperty(Event.prototype, 'type', { enumerable: true });\n\n/**\n * Class representing a close event.\n *\n * @extends Event\n */\nclass CloseEvent extends Event {\n  /**\n   * Create a new `CloseEvent`.\n   *\n   * @param {String} type The name of the event\n   * @param {Object} [options] A dictionary object that allows for setting\n   *     attributes via object members of the same name\n   * @param {Number} [options.code=0] The status code explaining why the\n   *     connection was closed\n   * @param {String} [options.reason=''] A human-readable string explaining why\n   *     the connection was closed\n   * @param {Boolean} [options.wasClean=false] Indicates whether or not the\n   *     connection was cleanly closed\n   */\n  constructor(type, options = {}) {\n    super(type);\n\n    this[kCode] = options.code === undefined ? 0 : options.code;\n    this[kReason] = options.reason === undefined ? '' : options.reason;\n    this[kWasClean] = options.wasClean === undefined ? false : options.wasClean;\n  }\n\n  /**\n   * @type {Number}\n   */\n  get code() {\n    return this[kCode];\n  }\n\n  /**\n   * @type {String}\n   */\n  get reason() {\n    return this[kReason];\n  }\n\n  /**\n   * @type {Boolean}\n   */\n  get wasClean() {\n    return this[kWasClean];\n  }\n}\n\nObject.defineProperty(CloseEvent.prototype, 'code', { enumerable: true });\nObject.defineProperty(CloseEvent.prototype, 'reason', { enumerable: true });\nObject.defineProperty(CloseEvent.prototype, 'wasClean', { enumerable: true });\n\n/**\n * Class representing an error event.\n *\n * @extends Event\n */\nclass ErrorEvent extends Event {\n  /**\n   * Create a new `ErrorEvent`.\n   *\n   * @param {String} type The name of the event\n   * @param {Object} [options] A dictionary object that allows for setting\n   *     attributes via object members of the same name\n   * @param {*} [options.error=null] The error that generated this event\n   * @param {String} [options.message=''] The error message\n   */\n  constructor(type, options = {}) {\n    super(type);\n\n    this[kError] = options.error === undefined ? null : options.error;\n    this[kMessage] = options.message === undefined ? '' : options.message;\n  }\n\n  /**\n   * @type {*}\n   */\n  get error() {\n    return this[kError];\n  }\n\n  /**\n   * @type {String}\n   */\n  get message() {\n    return this[kMessage];\n  }\n}\n\nObject.defineProperty(ErrorEvent.prototype, 'error', { enumerable: true });\nObject.defineProperty(ErrorEvent.prototype, 'message', { enumerable: true });\n\n/**\n * Class representing a message event.\n *\n * @extends Event\n */\nclass MessageEvent extends Event {\n  /**\n   * Create a new `MessageEvent`.\n   *\n   * @param {String} type The name of the event\n   * @param {Object} [options] A dictionary object that allows for setting\n   *     attributes via object members of the same name\n   * @param {*} [options.data=null] The message content\n   */\n  constructor(type, options = {}) {\n    super(type);\n\n    this[kData] = options.data === undefined ? null : options.data;\n  }\n\n  /**\n   * @type {*}\n   */\n  get data() {\n    return this[kData];\n  }\n}\n\nObject.defineProperty(MessageEvent.prototype, 'data', { enumerable: true });\n\n/**\n * This provides methods for emulating the `EventTarget` interface. It's not\n * meant to be used directly.\n *\n * @mixin\n */\nconst EventTarget = {\n  /**\n   * Register an event listener.\n   *\n   * @param {String} type A string representing the event type to listen for\n   * @param {(Function|Object)} handler The listener to add\n   * @param {Object} [options] An options object specifies characteristics about\n   *     the event listener\n   * @param {Boolean} [options.once=false] A `Boolean` indicating that the\n   *     listener should be invoked at most once after being added. If `true`,\n   *     the listener would be automatically removed when invoked.\n   * @public\n   */\n  addEventListener(type, handler, options = {}) {\n    for (const listener of this.listeners(type)) {\n      if (\n        !options[kForOnEventAttribute] &&\n        listener[kListener] === handler &&\n        !listener[kForOnEventAttribute]\n      ) {\n        return;\n      }\n    }\n\n    let wrapper;\n\n    if (type === 'message') {\n      wrapper = function onMessage(data, isBinary) {\n        const event = new MessageEvent('message', {\n          data: isBinary ? data : data.toString()\n        });\n\n        event[kTarget] = this;\n        callListener(handler, this, event);\n      };\n    } else if (type === 'close') {\n      wrapper = function onClose(code, message) {\n        const event = new CloseEvent('close', {\n          code,\n          reason: message.toString(),\n          wasClean: this._closeFrameReceived && this._closeFrameSent\n        });\n\n        event[kTarget] = this;\n        callListener(handler, this, event);\n      };\n    } else if (type === 'error') {\n      wrapper = function onError(error) {\n        const event = new ErrorEvent('error', {\n          error,\n          message: error.message\n        });\n\n        event[kTarget] = this;\n        callListener(handler, this, event);\n      };\n    } else if (type === 'open') {\n      wrapper = function onOpen() {\n        const event = new Event('open');\n\n        event[kTarget] = this;\n        callListener(handler, this, event);\n      };\n    } else {\n      return;\n    }\n\n    wrapper[kForOnEventAttribute] = !!options[kForOnEventAttribute];\n    wrapper[kListener] = handler;\n\n    if (options.once) {\n      this.once(type, wrapper);\n    } else {\n      this.on(type, wrapper);\n    }\n  },\n\n  /**\n   * Remove an event listener.\n   *\n   * @param {String} type A string representing the event type to remove\n   * @param {(Function|Object)} handler The listener to remove\n   * @public\n   */\n  removeEventListener(type, handler) {\n    for (const listener of this.listeners(type)) {\n      if (listener[kListener] === handler && !listener[kForOnEventAttribute]) {\n        this.removeListener(type, listener);\n        break;\n      }\n    }\n  }\n};\n\nmodule.exports = {\n  CloseEvent,\n  ErrorEvent,\n  Event,\n  EventTarget,\n  MessageEvent\n};\n\n/**\n * Call an event listener\n *\n * @param {(Function|Object)} listener The listener to call\n * @param {*} thisArg The value to use as `this`` when calling the listener\n * @param {Event} event The event to pass to the listener\n * @private\n */\nfunction callListener(listener, thisArg, event) {\n  if (typeof listener === 'object' && listener.handleEvent) {\n    listener.handleEvent.call(listener, event);\n  } else {\n    listener.call(thisArg, event);\n  }\n}\n"
  },
  {
    "path": "lib/extension.js",
    "content": "'use strict';\n\nconst { tokenChars } = require('./validation');\n\n/**\n * Adds an offer to the map of extension offers or a parameter to the map of\n * parameters.\n *\n * @param {Object} dest The map of extension offers or parameters\n * @param {String} name The extension or parameter name\n * @param {(Object|Boolean|String)} elem The extension parameters or the\n *     parameter value\n * @private\n */\nfunction push(dest, name, elem) {\n  if (dest[name] === undefined) dest[name] = [elem];\n  else dest[name].push(elem);\n}\n\n/**\n * Parses the `Sec-WebSocket-Extensions` header into an object.\n *\n * @param {String} header The field value of the header\n * @return {Object} The parsed object\n * @public\n */\nfunction parse(header) {\n  const offers = Object.create(null);\n  let params = Object.create(null);\n  let mustUnescape = false;\n  let isEscaping = false;\n  let inQuotes = false;\n  let extensionName;\n  let paramName;\n  let start = -1;\n  let code = -1;\n  let end = -1;\n  let i = 0;\n\n  for (; i < header.length; i++) {\n    code = header.charCodeAt(i);\n\n    if (extensionName === undefined) {\n      if (end === -1 && tokenChars[code] === 1) {\n        if (start === -1) start = i;\n      } else if (\n        i !== 0 &&\n        (code === 0x20 /* ' ' */ || code === 0x09) /* '\\t' */\n      ) {\n        if (end === -1 && start !== -1) end = i;\n      } else if (code === 0x3b /* ';' */ || code === 0x2c /* ',' */) {\n        if (start === -1) {\n          throw new SyntaxError(`Unexpected character at index ${i}`);\n        }\n\n        if (end === -1) end = i;\n        const name = header.slice(start, end);\n        if (code === 0x2c) {\n          push(offers, name, params);\n          params = Object.create(null);\n        } else {\n          extensionName = name;\n        }\n\n        start = end = -1;\n      } else {\n        throw new SyntaxError(`Unexpected character at index ${i}`);\n      }\n    } else if (paramName === undefined) {\n      if (end === -1 && tokenChars[code] === 1) {\n        if (start === -1) start = i;\n      } else if (code === 0x20 || code === 0x09) {\n        if (end === -1 && start !== -1) end = i;\n      } else if (code === 0x3b || code === 0x2c) {\n        if (start === -1) {\n          throw new SyntaxError(`Unexpected character at index ${i}`);\n        }\n\n        if (end === -1) end = i;\n        push(params, header.slice(start, end), true);\n        if (code === 0x2c) {\n          push(offers, extensionName, params);\n          params = Object.create(null);\n          extensionName = undefined;\n        }\n\n        start = end = -1;\n      } else if (code === 0x3d /* '=' */ && start !== -1 && end === -1) {\n        paramName = header.slice(start, i);\n        start = end = -1;\n      } else {\n        throw new SyntaxError(`Unexpected character at index ${i}`);\n      }\n    } else {\n      //\n      // The value of a quoted-string after unescaping must conform to the\n      // token ABNF, so only token characters are valid.\n      // Ref: https://tools.ietf.org/html/rfc6455#section-9.1\n      //\n      if (isEscaping) {\n        if (tokenChars[code] !== 1) {\n          throw new SyntaxError(`Unexpected character at index ${i}`);\n        }\n        if (start === -1) start = i;\n        else if (!mustUnescape) mustUnescape = true;\n        isEscaping = false;\n      } else if (inQuotes) {\n        if (tokenChars[code] === 1) {\n          if (start === -1) start = i;\n        } else if (code === 0x22 /* '\"' */ && start !== -1) {\n          inQuotes = false;\n          end = i;\n        } else if (code === 0x5c /* '\\' */) {\n          isEscaping = true;\n        } else {\n          throw new SyntaxError(`Unexpected character at index ${i}`);\n        }\n      } else if (code === 0x22 && header.charCodeAt(i - 1) === 0x3d) {\n        inQuotes = true;\n      } else if (end === -1 && tokenChars[code] === 1) {\n        if (start === -1) start = i;\n      } else if (start !== -1 && (code === 0x20 || code === 0x09)) {\n        if (end === -1) end = i;\n      } else if (code === 0x3b || code === 0x2c) {\n        if (start === -1) {\n          throw new SyntaxError(`Unexpected character at index ${i}`);\n        }\n\n        if (end === -1) end = i;\n        let value = header.slice(start, end);\n        if (mustUnescape) {\n          value = value.replace(/\\\\/g, '');\n          mustUnescape = false;\n        }\n        push(params, paramName, value);\n        if (code === 0x2c) {\n          push(offers, extensionName, params);\n          params = Object.create(null);\n          extensionName = undefined;\n        }\n\n        paramName = undefined;\n        start = end = -1;\n      } else {\n        throw new SyntaxError(`Unexpected character at index ${i}`);\n      }\n    }\n  }\n\n  if (start === -1 || inQuotes || code === 0x20 || code === 0x09) {\n    throw new SyntaxError('Unexpected end of input');\n  }\n\n  if (end === -1) end = i;\n  const token = header.slice(start, end);\n  if (extensionName === undefined) {\n    push(offers, token, params);\n  } else {\n    if (paramName === undefined) {\n      push(params, token, true);\n    } else if (mustUnescape) {\n      push(params, paramName, token.replace(/\\\\/g, ''));\n    } else {\n      push(params, paramName, token);\n    }\n    push(offers, extensionName, params);\n  }\n\n  return offers;\n}\n\n/**\n * Builds the `Sec-WebSocket-Extensions` header field value.\n *\n * @param {Object} extensions The map of extensions and parameters to format\n * @return {String} A string representing the given object\n * @public\n */\nfunction format(extensions) {\n  return Object.keys(extensions)\n    .map((extension) => {\n      let configurations = extensions[extension];\n      if (!Array.isArray(configurations)) configurations = [configurations];\n      return configurations\n        .map((params) => {\n          return [extension]\n            .concat(\n              Object.keys(params).map((k) => {\n                let values = params[k];\n                if (!Array.isArray(values)) values = [values];\n                return values\n                  .map((v) => (v === true ? k : `${k}=${v}`))\n                  .join('; ');\n              })\n            )\n            .join('; ');\n        })\n        .join(', ');\n    })\n    .join(', ');\n}\n\nmodule.exports = { format, parse };\n"
  },
  {
    "path": "lib/limiter.js",
    "content": "'use strict';\n\nconst kDone = Symbol('kDone');\nconst kRun = Symbol('kRun');\n\n/**\n * A very simple job queue with adjustable concurrency. Adapted from\n * https://github.com/STRML/async-limiter\n */\nclass Limiter {\n  /**\n   * Creates a new `Limiter`.\n   *\n   * @param {Number} [concurrency=Infinity] The maximum number of jobs allowed\n   *     to run concurrently\n   */\n  constructor(concurrency) {\n    this[kDone] = () => {\n      this.pending--;\n      this[kRun]();\n    };\n    this.concurrency = concurrency || Infinity;\n    this.jobs = [];\n    this.pending = 0;\n  }\n\n  /**\n   * Adds a job to the queue.\n   *\n   * @param {Function} job The job to run\n   * @public\n   */\n  add(job) {\n    this.jobs.push(job);\n    this[kRun]();\n  }\n\n  /**\n   * Removes a job from the queue and runs it if possible.\n   *\n   * @private\n   */\n  [kRun]() {\n    if (this.pending === this.concurrency) return;\n\n    if (this.jobs.length) {\n      const job = this.jobs.shift();\n\n      this.pending++;\n      job(this[kDone]);\n    }\n  }\n}\n\nmodule.exports = Limiter;\n"
  },
  {
    "path": "lib/permessage-deflate.js",
    "content": "'use strict';\n\nconst zlib = require('zlib');\n\nconst bufferUtil = require('./buffer-util');\nconst Limiter = require('./limiter');\nconst { kStatusCode } = require('./constants');\n\nconst FastBuffer = Buffer[Symbol.species];\nconst TRAILER = Buffer.from([0x00, 0x00, 0xff, 0xff]);\nconst kPerMessageDeflate = Symbol('permessage-deflate');\nconst kTotalLength = Symbol('total-length');\nconst kCallback = Symbol('callback');\nconst kBuffers = Symbol('buffers');\nconst kError = Symbol('error');\n\n//\n// We limit zlib concurrency, which prevents severe memory fragmentation\n// as documented in https://github.com/nodejs/node/issues/8871#issuecomment-250915913\n// and https://github.com/websockets/ws/issues/1202\n//\n// Intentionally global; it's the global thread pool that's an issue.\n//\nlet zlibLimiter;\n\n/**\n * permessage-deflate implementation.\n */\nclass PerMessageDeflate {\n  /**\n   * Creates a PerMessageDeflate instance.\n   *\n   * @param {Object} [options] Configuration options\n   * @param {(Boolean|Number)} [options.clientMaxWindowBits] Advertise support\n   *     for, or request, a custom client window size\n   * @param {Boolean} [options.clientNoContextTakeover=false] Advertise/\n   *     acknowledge disabling of client context takeover\n   * @param {Number} [options.concurrencyLimit=10] The number of concurrent\n   *     calls to zlib\n   * @param {Boolean} [options.isServer=false] Create the instance in either\n   *     server or client mode\n   * @param {Number} [options.maxPayload=0] The maximum allowed message length\n   * @param {(Boolean|Number)} [options.serverMaxWindowBits] Request/confirm the\n   *     use of a custom server window size\n   * @param {Boolean} [options.serverNoContextTakeover=false] Request/accept\n   *     disabling of server context takeover\n   * @param {Number} [options.threshold=1024] Size (in bytes) below which\n   *     messages should not be compressed if context takeover is disabled\n   * @param {Object} [options.zlibDeflateOptions] Options to pass to zlib on\n   *     deflate\n   * @param {Object} [options.zlibInflateOptions] Options to pass to zlib on\n   *     inflate\n   */\n  constructor(options) {\n    this._options = options || {};\n    this._threshold =\n      this._options.threshold !== undefined ? this._options.threshold : 1024;\n    this._maxPayload = this._options.maxPayload | 0;\n    this._isServer = !!this._options.isServer;\n    this._deflate = null;\n    this._inflate = null;\n\n    this.params = null;\n\n    if (!zlibLimiter) {\n      const concurrency =\n        this._options.concurrencyLimit !== undefined\n          ? this._options.concurrencyLimit\n          : 10;\n      zlibLimiter = new Limiter(concurrency);\n    }\n  }\n\n  /**\n   * @type {String}\n   */\n  static get extensionName() {\n    return 'permessage-deflate';\n  }\n\n  /**\n   * Create an extension negotiation offer.\n   *\n   * @return {Object} Extension parameters\n   * @public\n   */\n  offer() {\n    const params = {};\n\n    if (this._options.serverNoContextTakeover) {\n      params.server_no_context_takeover = true;\n    }\n    if (this._options.clientNoContextTakeover) {\n      params.client_no_context_takeover = true;\n    }\n    if (this._options.serverMaxWindowBits) {\n      params.server_max_window_bits = this._options.serverMaxWindowBits;\n    }\n    if (this._options.clientMaxWindowBits) {\n      params.client_max_window_bits = this._options.clientMaxWindowBits;\n    } else if (this._options.clientMaxWindowBits == null) {\n      params.client_max_window_bits = true;\n    }\n\n    return params;\n  }\n\n  /**\n   * Accept an extension negotiation offer/response.\n   *\n   * @param {Array} configurations The extension negotiation offers/reponse\n   * @return {Object} Accepted configuration\n   * @public\n   */\n  accept(configurations) {\n    configurations = this.normalizeParams(configurations);\n\n    this.params = this._isServer\n      ? this.acceptAsServer(configurations)\n      : this.acceptAsClient(configurations);\n\n    return this.params;\n  }\n\n  /**\n   * Releases all resources used by the extension.\n   *\n   * @public\n   */\n  cleanup() {\n    if (this._inflate) {\n      this._inflate.close();\n      this._inflate = null;\n    }\n\n    if (this._deflate) {\n      const callback = this._deflate[kCallback];\n\n      this._deflate.close();\n      this._deflate = null;\n\n      if (callback) {\n        callback(\n          new Error(\n            'The deflate stream was closed while data was being processed'\n          )\n        );\n      }\n    }\n  }\n\n  /**\n   *  Accept an extension negotiation offer.\n   *\n   * @param {Array} offers The extension negotiation offers\n   * @return {Object} Accepted configuration\n   * @private\n   */\n  acceptAsServer(offers) {\n    const opts = this._options;\n    const accepted = offers.find((params) => {\n      if (\n        (opts.serverNoContextTakeover === false &&\n          params.server_no_context_takeover) ||\n        (params.server_max_window_bits &&\n          (opts.serverMaxWindowBits === false ||\n            (typeof opts.serverMaxWindowBits === 'number' &&\n              opts.serverMaxWindowBits > params.server_max_window_bits))) ||\n        (typeof opts.clientMaxWindowBits === 'number' &&\n          !params.client_max_window_bits)\n      ) {\n        return false;\n      }\n\n      return true;\n    });\n\n    if (!accepted) {\n      throw new Error('None of the extension offers can be accepted');\n    }\n\n    if (opts.serverNoContextTakeover) {\n      accepted.server_no_context_takeover = true;\n    }\n    if (opts.clientNoContextTakeover) {\n      accepted.client_no_context_takeover = true;\n    }\n    if (typeof opts.serverMaxWindowBits === 'number') {\n      accepted.server_max_window_bits = opts.serverMaxWindowBits;\n    }\n    if (typeof opts.clientMaxWindowBits === 'number') {\n      accepted.client_max_window_bits = opts.clientMaxWindowBits;\n    } else if (\n      accepted.client_max_window_bits === true ||\n      opts.clientMaxWindowBits === false\n    ) {\n      delete accepted.client_max_window_bits;\n    }\n\n    return accepted;\n  }\n\n  /**\n   * Accept the extension negotiation response.\n   *\n   * @param {Array} response The extension negotiation response\n   * @return {Object} Accepted configuration\n   * @private\n   */\n  acceptAsClient(response) {\n    const params = response[0];\n\n    if (\n      this._options.clientNoContextTakeover === false &&\n      params.client_no_context_takeover\n    ) {\n      throw new Error('Unexpected parameter \"client_no_context_takeover\"');\n    }\n\n    if (!params.client_max_window_bits) {\n      if (typeof this._options.clientMaxWindowBits === 'number') {\n        params.client_max_window_bits = this._options.clientMaxWindowBits;\n      }\n    } else if (\n      this._options.clientMaxWindowBits === false ||\n      (typeof this._options.clientMaxWindowBits === 'number' &&\n        params.client_max_window_bits > this._options.clientMaxWindowBits)\n    ) {\n      throw new Error(\n        'Unexpected or invalid parameter \"client_max_window_bits\"'\n      );\n    }\n\n    return params;\n  }\n\n  /**\n   * Normalize parameters.\n   *\n   * @param {Array} configurations The extension negotiation offers/reponse\n   * @return {Array} The offers/response with normalized parameters\n   * @private\n   */\n  normalizeParams(configurations) {\n    configurations.forEach((params) => {\n      Object.keys(params).forEach((key) => {\n        let value = params[key];\n\n        if (value.length > 1) {\n          throw new Error(`Parameter \"${key}\" must have only a single value`);\n        }\n\n        value = value[0];\n\n        if (key === 'client_max_window_bits') {\n          if (value !== true) {\n            const num = +value;\n            if (!Number.isInteger(num) || num < 8 || num > 15) {\n              throw new TypeError(\n                `Invalid value for parameter \"${key}\": ${value}`\n              );\n            }\n            value = num;\n          } else if (!this._isServer) {\n            throw new TypeError(\n              `Invalid value for parameter \"${key}\": ${value}`\n            );\n          }\n        } else if (key === 'server_max_window_bits') {\n          const num = +value;\n          if (!Number.isInteger(num) || num < 8 || num > 15) {\n            throw new TypeError(\n              `Invalid value for parameter \"${key}\": ${value}`\n            );\n          }\n          value = num;\n        } else if (\n          key === 'client_no_context_takeover' ||\n          key === 'server_no_context_takeover'\n        ) {\n          if (value !== true) {\n            throw new TypeError(\n              `Invalid value for parameter \"${key}\": ${value}`\n            );\n          }\n        } else {\n          throw new Error(`Unknown parameter \"${key}\"`);\n        }\n\n        params[key] = value;\n      });\n    });\n\n    return configurations;\n  }\n\n  /**\n   * Decompress data. Concurrency limited.\n   *\n   * @param {Buffer} data Compressed data\n   * @param {Boolean} fin Specifies whether or not this is the last fragment\n   * @param {Function} callback Callback\n   * @public\n   */\n  decompress(data, fin, callback) {\n    zlibLimiter.add((done) => {\n      this._decompress(data, fin, (err, result) => {\n        done();\n        callback(err, result);\n      });\n    });\n  }\n\n  /**\n   * Compress data. Concurrency limited.\n   *\n   * @param {(Buffer|String)} data Data to compress\n   * @param {Boolean} fin Specifies whether or not this is the last fragment\n   * @param {Function} callback Callback\n   * @public\n   */\n  compress(data, fin, callback) {\n    zlibLimiter.add((done) => {\n      this._compress(data, fin, (err, result) => {\n        done();\n        callback(err, result);\n      });\n    });\n  }\n\n  /**\n   * Decompress data.\n   *\n   * @param {Buffer} data Compressed data\n   * @param {Boolean} fin Specifies whether or not this is the last fragment\n   * @param {Function} callback Callback\n   * @private\n   */\n  _decompress(data, fin, callback) {\n    const endpoint = this._isServer ? 'client' : 'server';\n\n    if (!this._inflate) {\n      const key = `${endpoint}_max_window_bits`;\n      const windowBits =\n        typeof this.params[key] !== 'number'\n          ? zlib.Z_DEFAULT_WINDOWBITS\n          : this.params[key];\n\n      this._inflate = zlib.createInflateRaw({\n        ...this._options.zlibInflateOptions,\n        windowBits\n      });\n      this._inflate[kPerMessageDeflate] = this;\n      this._inflate[kTotalLength] = 0;\n      this._inflate[kBuffers] = [];\n      this._inflate.on('error', inflateOnError);\n      this._inflate.on('data', inflateOnData);\n    }\n\n    this._inflate[kCallback] = callback;\n\n    this._inflate.write(data);\n    if (fin) this._inflate.write(TRAILER);\n\n    this._inflate.flush(() => {\n      const err = this._inflate[kError];\n\n      if (err) {\n        this._inflate.close();\n        this._inflate = null;\n        callback(err);\n        return;\n      }\n\n      const data = bufferUtil.concat(\n        this._inflate[kBuffers],\n        this._inflate[kTotalLength]\n      );\n\n      if (this._inflate._readableState.endEmitted) {\n        this._inflate.close();\n        this._inflate = null;\n      } else {\n        this._inflate[kTotalLength] = 0;\n        this._inflate[kBuffers] = [];\n\n        if (fin && this.params[`${endpoint}_no_context_takeover`]) {\n          this._inflate.reset();\n        }\n      }\n\n      callback(null, data);\n    });\n  }\n\n  /**\n   * Compress data.\n   *\n   * @param {(Buffer|String)} data Data to compress\n   * @param {Boolean} fin Specifies whether or not this is the last fragment\n   * @param {Function} callback Callback\n   * @private\n   */\n  _compress(data, fin, callback) {\n    const endpoint = this._isServer ? 'server' : 'client';\n\n    if (!this._deflate) {\n      const key = `${endpoint}_max_window_bits`;\n      const windowBits =\n        typeof this.params[key] !== 'number'\n          ? zlib.Z_DEFAULT_WINDOWBITS\n          : this.params[key];\n\n      this._deflate = zlib.createDeflateRaw({\n        ...this._options.zlibDeflateOptions,\n        windowBits\n      });\n\n      this._deflate[kTotalLength] = 0;\n      this._deflate[kBuffers] = [];\n\n      this._deflate.on('data', deflateOnData);\n    }\n\n    this._deflate[kCallback] = callback;\n\n    this._deflate.write(data);\n    this._deflate.flush(zlib.Z_SYNC_FLUSH, () => {\n      if (!this._deflate) {\n        //\n        // The deflate stream was closed while data was being processed.\n        //\n        return;\n      }\n\n      let data = bufferUtil.concat(\n        this._deflate[kBuffers],\n        this._deflate[kTotalLength]\n      );\n\n      if (fin) {\n        data = new FastBuffer(data.buffer, data.byteOffset, data.length - 4);\n      }\n\n      //\n      // Ensure that the callback will not be called again in\n      // `PerMessageDeflate#cleanup()`.\n      //\n      this._deflate[kCallback] = null;\n\n      this._deflate[kTotalLength] = 0;\n      this._deflate[kBuffers] = [];\n\n      if (fin && this.params[`${endpoint}_no_context_takeover`]) {\n        this._deflate.reset();\n      }\n\n      callback(null, data);\n    });\n  }\n}\n\nmodule.exports = PerMessageDeflate;\n\n/**\n * The listener of the `zlib.DeflateRaw` stream `'data'` event.\n *\n * @param {Buffer} chunk A chunk of data\n * @private\n */\nfunction deflateOnData(chunk) {\n  this[kBuffers].push(chunk);\n  this[kTotalLength] += chunk.length;\n}\n\n/**\n * The listener of the `zlib.InflateRaw` stream `'data'` event.\n *\n * @param {Buffer} chunk A chunk of data\n * @private\n */\nfunction inflateOnData(chunk) {\n  this[kTotalLength] += chunk.length;\n\n  if (\n    this[kPerMessageDeflate]._maxPayload < 1 ||\n    this[kTotalLength] <= this[kPerMessageDeflate]._maxPayload\n  ) {\n    this[kBuffers].push(chunk);\n    return;\n  }\n\n  this[kError] = new RangeError('Max payload size exceeded');\n  this[kError].code = 'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH';\n  this[kError][kStatusCode] = 1009;\n  this.removeListener('data', inflateOnData);\n\n  //\n  // The choice to employ `zlib.reset()` over `zlib.close()` is dictated by the\n  // fact that in Node.js versions prior to 13.10.0, the callback for\n  // `zlib.flush()` is not called if `zlib.close()` is used. Utilizing\n  // `zlib.reset()` ensures that either the callback is invoked or an error is\n  // emitted.\n  //\n  this.reset();\n}\n\n/**\n * The listener of the `zlib.InflateRaw` stream `'error'` event.\n *\n * @param {Error} err The emitted error\n * @private\n */\nfunction inflateOnError(err) {\n  //\n  // There is no need to call `Zlib#close()` as the handle is automatically\n  // closed when an error is emitted.\n  //\n  this[kPerMessageDeflate]._inflate = null;\n\n  if (this[kError]) {\n    this[kCallback](this[kError]);\n    return;\n  }\n\n  err[kStatusCode] = 1007;\n  this[kCallback](err);\n}\n"
  },
  {
    "path": "lib/receiver.js",
    "content": "'use strict';\n\nconst { Writable } = require('stream');\n\nconst PerMessageDeflate = require('./permessage-deflate');\nconst {\n  BINARY_TYPES,\n  EMPTY_BUFFER,\n  kStatusCode,\n  kWebSocket\n} = require('./constants');\nconst { concat, toArrayBuffer, unmask } = require('./buffer-util');\nconst { isValidStatusCode, isValidUTF8 } = require('./validation');\n\nconst FastBuffer = Buffer[Symbol.species];\n\nconst GET_INFO = 0;\nconst GET_PAYLOAD_LENGTH_16 = 1;\nconst GET_PAYLOAD_LENGTH_64 = 2;\nconst GET_MASK = 3;\nconst GET_DATA = 4;\nconst INFLATING = 5;\nconst DEFER_EVENT = 6;\n\n/**\n * HyBi Receiver implementation.\n *\n * @extends Writable\n */\nclass Receiver extends Writable {\n  /**\n   * Creates a Receiver instance.\n   *\n   * @param {Object} [options] Options object\n   * @param {Boolean} [options.allowSynchronousEvents=true] Specifies whether\n   *     any of the `'message'`, `'ping'`, and `'pong'` events can be emitted\n   *     multiple times in the same tick\n   * @param {String} [options.binaryType=nodebuffer] The type for binary data\n   * @param {Object} [options.extensions] An object containing the negotiated\n   *     extensions\n   * @param {Boolean} [options.isServer=false] Specifies whether to operate in\n   *     client or server mode\n   * @param {Number} [options.maxPayload=0] The maximum allowed message length\n   * @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or\n   *     not to skip UTF-8 validation for text and close messages\n   */\n  constructor(options = {}) {\n    super();\n\n    this._allowSynchronousEvents =\n      options.allowSynchronousEvents !== undefined\n        ? options.allowSynchronousEvents\n        : true;\n    this._binaryType = options.binaryType || BINARY_TYPES[0];\n    this._extensions = options.extensions || {};\n    this._isServer = !!options.isServer;\n    this._maxPayload = options.maxPayload | 0;\n    this._skipUTF8Validation = !!options.skipUTF8Validation;\n    this[kWebSocket] = undefined;\n\n    this._bufferedBytes = 0;\n    this._buffers = [];\n\n    this._compressed = false;\n    this._payloadLength = 0;\n    this._mask = undefined;\n    this._fragmented = 0;\n    this._masked = false;\n    this._fin = false;\n    this._opcode = 0;\n\n    this._totalPayloadLength = 0;\n    this._messageLength = 0;\n    this._fragments = [];\n\n    this._errored = false;\n    this._loop = false;\n    this._state = GET_INFO;\n  }\n\n  /**\n   * Implements `Writable.prototype._write()`.\n   *\n   * @param {Buffer} chunk The chunk of data to write\n   * @param {String} encoding The character encoding of `chunk`\n   * @param {Function} cb Callback\n   * @private\n   */\n  _write(chunk, encoding, cb) {\n    if (this._opcode === 0x08 && this._state == GET_INFO) return cb();\n\n    this._bufferedBytes += chunk.length;\n    this._buffers.push(chunk);\n    this.startLoop(cb);\n  }\n\n  /**\n   * Consumes `n` bytes from the buffered data.\n   *\n   * @param {Number} n The number of bytes to consume\n   * @return {Buffer} The consumed bytes\n   * @private\n   */\n  consume(n) {\n    this._bufferedBytes -= n;\n\n    if (n === this._buffers[0].length) return this._buffers.shift();\n\n    if (n < this._buffers[0].length) {\n      const buf = this._buffers[0];\n      this._buffers[0] = new FastBuffer(\n        buf.buffer,\n        buf.byteOffset + n,\n        buf.length - n\n      );\n\n      return new FastBuffer(buf.buffer, buf.byteOffset, n);\n    }\n\n    const dst = Buffer.allocUnsafe(n);\n\n    do {\n      const buf = this._buffers[0];\n      const offset = dst.length - n;\n\n      if (n >= buf.length) {\n        dst.set(this._buffers.shift(), offset);\n      } else {\n        dst.set(new Uint8Array(buf.buffer, buf.byteOffset, n), offset);\n        this._buffers[0] = new FastBuffer(\n          buf.buffer,\n          buf.byteOffset + n,\n          buf.length - n\n        );\n      }\n\n      n -= buf.length;\n    } while (n > 0);\n\n    return dst;\n  }\n\n  /**\n   * Starts the parsing loop.\n   *\n   * @param {Function} cb Callback\n   * @private\n   */\n  startLoop(cb) {\n    this._loop = true;\n\n    do {\n      switch (this._state) {\n        case GET_INFO:\n          this.getInfo(cb);\n          break;\n        case GET_PAYLOAD_LENGTH_16:\n          this.getPayloadLength16(cb);\n          break;\n        case GET_PAYLOAD_LENGTH_64:\n          this.getPayloadLength64(cb);\n          break;\n        case GET_MASK:\n          this.getMask();\n          break;\n        case GET_DATA:\n          this.getData(cb);\n          break;\n        case INFLATING:\n        case DEFER_EVENT:\n          this._loop = false;\n          return;\n      }\n    } while (this._loop);\n\n    if (!this._errored) cb();\n  }\n\n  /**\n   * Reads the first two bytes of a frame.\n   *\n   * @param {Function} cb Callback\n   * @private\n   */\n  getInfo(cb) {\n    if (this._bufferedBytes < 2) {\n      this._loop = false;\n      return;\n    }\n\n    const buf = this.consume(2);\n\n    if ((buf[0] & 0x30) !== 0x00) {\n      const error = this.createError(\n        RangeError,\n        'RSV2 and RSV3 must be clear',\n        true,\n        1002,\n        'WS_ERR_UNEXPECTED_RSV_2_3'\n      );\n\n      cb(error);\n      return;\n    }\n\n    const compressed = (buf[0] & 0x40) === 0x40;\n\n    if (compressed && !this._extensions[PerMessageDeflate.extensionName]) {\n      const error = this.createError(\n        RangeError,\n        'RSV1 must be clear',\n        true,\n        1002,\n        'WS_ERR_UNEXPECTED_RSV_1'\n      );\n\n      cb(error);\n      return;\n    }\n\n    this._fin = (buf[0] & 0x80) === 0x80;\n    this._opcode = buf[0] & 0x0f;\n    this._payloadLength = buf[1] & 0x7f;\n\n    if (this._opcode === 0x00) {\n      if (compressed) {\n        const error = this.createError(\n          RangeError,\n          'RSV1 must be clear',\n          true,\n          1002,\n          'WS_ERR_UNEXPECTED_RSV_1'\n        );\n\n        cb(error);\n        return;\n      }\n\n      if (!this._fragmented) {\n        const error = this.createError(\n          RangeError,\n          'invalid opcode 0',\n          true,\n          1002,\n          'WS_ERR_INVALID_OPCODE'\n        );\n\n        cb(error);\n        return;\n      }\n\n      this._opcode = this._fragmented;\n    } else if (this._opcode === 0x01 || this._opcode === 0x02) {\n      if (this._fragmented) {\n        const error = this.createError(\n          RangeError,\n          `invalid opcode ${this._opcode}`,\n          true,\n          1002,\n          'WS_ERR_INVALID_OPCODE'\n        );\n\n        cb(error);\n        return;\n      }\n\n      this._compressed = compressed;\n    } else if (this._opcode > 0x07 && this._opcode < 0x0b) {\n      if (!this._fin) {\n        const error = this.createError(\n          RangeError,\n          'FIN must be set',\n          true,\n          1002,\n          'WS_ERR_EXPECTED_FIN'\n        );\n\n        cb(error);\n        return;\n      }\n\n      if (compressed) {\n        const error = this.createError(\n          RangeError,\n          'RSV1 must be clear',\n          true,\n          1002,\n          'WS_ERR_UNEXPECTED_RSV_1'\n        );\n\n        cb(error);\n        return;\n      }\n\n      if (\n        this._payloadLength > 0x7d ||\n        (this._opcode === 0x08 && this._payloadLength === 1)\n      ) {\n        const error = this.createError(\n          RangeError,\n          `invalid payload length ${this._payloadLength}`,\n          true,\n          1002,\n          'WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH'\n        );\n\n        cb(error);\n        return;\n      }\n    } else {\n      const error = this.createError(\n        RangeError,\n        `invalid opcode ${this._opcode}`,\n        true,\n        1002,\n        'WS_ERR_INVALID_OPCODE'\n      );\n\n      cb(error);\n      return;\n    }\n\n    if (!this._fin && !this._fragmented) this._fragmented = this._opcode;\n    this._masked = (buf[1] & 0x80) === 0x80;\n\n    if (this._isServer) {\n      if (!this._masked) {\n        const error = this.createError(\n          RangeError,\n          'MASK must be set',\n          true,\n          1002,\n          'WS_ERR_EXPECTED_MASK'\n        );\n\n        cb(error);\n        return;\n      }\n    } else if (this._masked) {\n      const error = this.createError(\n        RangeError,\n        'MASK must be clear',\n        true,\n        1002,\n        'WS_ERR_UNEXPECTED_MASK'\n      );\n\n      cb(error);\n      return;\n    }\n\n    if (this._payloadLength === 126) this._state = GET_PAYLOAD_LENGTH_16;\n    else if (this._payloadLength === 127) this._state = GET_PAYLOAD_LENGTH_64;\n    else this.haveLength(cb);\n  }\n\n  /**\n   * Gets extended payload length (7+16).\n   *\n   * @param {Function} cb Callback\n   * @private\n   */\n  getPayloadLength16(cb) {\n    if (this._bufferedBytes < 2) {\n      this._loop = false;\n      return;\n    }\n\n    this._payloadLength = this.consume(2).readUInt16BE(0);\n    this.haveLength(cb);\n  }\n\n  /**\n   * Gets extended payload length (7+64).\n   *\n   * @param {Function} cb Callback\n   * @private\n   */\n  getPayloadLength64(cb) {\n    if (this._bufferedBytes < 8) {\n      this._loop = false;\n      return;\n    }\n\n    const buf = this.consume(8);\n    const num = buf.readUInt32BE(0);\n\n    //\n    // The maximum safe integer in JavaScript is 2^53 - 1. An error is returned\n    // if payload length is greater than this number.\n    //\n    if (num > Math.pow(2, 53 - 32) - 1) {\n      const error = this.createError(\n        RangeError,\n        'Unsupported WebSocket frame: payload length > 2^53 - 1',\n        false,\n        1009,\n        'WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH'\n      );\n\n      cb(error);\n      return;\n    }\n\n    this._payloadLength = num * Math.pow(2, 32) + buf.readUInt32BE(4);\n    this.haveLength(cb);\n  }\n\n  /**\n   * Payload length has been read.\n   *\n   * @param {Function} cb Callback\n   * @private\n   */\n  haveLength(cb) {\n    if (this._payloadLength && this._opcode < 0x08) {\n      this._totalPayloadLength += this._payloadLength;\n      if (this._totalPayloadLength > this._maxPayload && this._maxPayload > 0) {\n        const error = this.createError(\n          RangeError,\n          'Max payload size exceeded',\n          false,\n          1009,\n          'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH'\n        );\n\n        cb(error);\n        return;\n      }\n    }\n\n    if (this._masked) this._state = GET_MASK;\n    else this._state = GET_DATA;\n  }\n\n  /**\n   * Reads mask bytes.\n   *\n   * @private\n   */\n  getMask() {\n    if (this._bufferedBytes < 4) {\n      this._loop = false;\n      return;\n    }\n\n    this._mask = this.consume(4);\n    this._state = GET_DATA;\n  }\n\n  /**\n   * Reads data bytes.\n   *\n   * @param {Function} cb Callback\n   * @private\n   */\n  getData(cb) {\n    let data = EMPTY_BUFFER;\n\n    if (this._payloadLength) {\n      if (this._bufferedBytes < this._payloadLength) {\n        this._loop = false;\n        return;\n      }\n\n      data = this.consume(this._payloadLength);\n\n      if (\n        this._masked &&\n        (this._mask[0] | this._mask[1] | this._mask[2] | this._mask[3]) !== 0\n      ) {\n        unmask(data, this._mask);\n      }\n    }\n\n    if (this._opcode > 0x07) {\n      this.controlMessage(data, cb);\n      return;\n    }\n\n    if (this._compressed) {\n      this._state = INFLATING;\n      this.decompress(data, cb);\n      return;\n    }\n\n    if (data.length) {\n      //\n      // This message is not compressed so its length is the sum of the payload\n      // length of all fragments.\n      //\n      this._messageLength = this._totalPayloadLength;\n      this._fragments.push(data);\n    }\n\n    this.dataMessage(cb);\n  }\n\n  /**\n   * Decompresses data.\n   *\n   * @param {Buffer} data Compressed data\n   * @param {Function} cb Callback\n   * @private\n   */\n  decompress(data, cb) {\n    const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];\n\n    perMessageDeflate.decompress(data, this._fin, (err, buf) => {\n      if (err) return cb(err);\n\n      if (buf.length) {\n        this._messageLength += buf.length;\n        if (this._messageLength > this._maxPayload && this._maxPayload > 0) {\n          const error = this.createError(\n            RangeError,\n            'Max payload size exceeded',\n            false,\n            1009,\n            'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH'\n          );\n\n          cb(error);\n          return;\n        }\n\n        this._fragments.push(buf);\n      }\n\n      this.dataMessage(cb);\n      if (this._state === GET_INFO) this.startLoop(cb);\n    });\n  }\n\n  /**\n   * Handles a data message.\n   *\n   * @param {Function} cb Callback\n   * @private\n   */\n  dataMessage(cb) {\n    if (!this._fin) {\n      this._state = GET_INFO;\n      return;\n    }\n\n    const messageLength = this._messageLength;\n    const fragments = this._fragments;\n\n    this._totalPayloadLength = 0;\n    this._messageLength = 0;\n    this._fragmented = 0;\n    this._fragments = [];\n\n    if (this._opcode === 2) {\n      let data;\n\n      if (this._binaryType === 'nodebuffer') {\n        data = concat(fragments, messageLength);\n      } else if (this._binaryType === 'arraybuffer') {\n        data = toArrayBuffer(concat(fragments, messageLength));\n      } else if (this._binaryType === 'blob') {\n        data = new Blob(fragments);\n      } else {\n        data = fragments;\n      }\n\n      if (this._allowSynchronousEvents) {\n        this.emit('message', data, true);\n        this._state = GET_INFO;\n      } else {\n        this._state = DEFER_EVENT;\n        setImmediate(() => {\n          this.emit('message', data, true);\n          this._state = GET_INFO;\n          this.startLoop(cb);\n        });\n      }\n    } else {\n      const buf = concat(fragments, messageLength);\n\n      if (!this._skipUTF8Validation && !isValidUTF8(buf)) {\n        const error = this.createError(\n          Error,\n          'invalid UTF-8 sequence',\n          true,\n          1007,\n          'WS_ERR_INVALID_UTF8'\n        );\n\n        cb(error);\n        return;\n      }\n\n      if (this._state === INFLATING || this._allowSynchronousEvents) {\n        this.emit('message', buf, false);\n        this._state = GET_INFO;\n      } else {\n        this._state = DEFER_EVENT;\n        setImmediate(() => {\n          this.emit('message', buf, false);\n          this._state = GET_INFO;\n          this.startLoop(cb);\n        });\n      }\n    }\n  }\n\n  /**\n   * Handles a control message.\n   *\n   * @param {Buffer} data Data to handle\n   * @return {(Error|RangeError|undefined)} A possible error\n   * @private\n   */\n  controlMessage(data, cb) {\n    if (this._opcode === 0x08) {\n      if (data.length === 0) {\n        this._loop = false;\n        this.emit('conclude', 1005, EMPTY_BUFFER);\n        this.end();\n      } else {\n        const code = data.readUInt16BE(0);\n\n        if (!isValidStatusCode(code)) {\n          const error = this.createError(\n            RangeError,\n            `invalid status code ${code}`,\n            true,\n            1002,\n            'WS_ERR_INVALID_CLOSE_CODE'\n          );\n\n          cb(error);\n          return;\n        }\n\n        const buf = new FastBuffer(\n          data.buffer,\n          data.byteOffset + 2,\n          data.length - 2\n        );\n\n        if (!this._skipUTF8Validation && !isValidUTF8(buf)) {\n          const error = this.createError(\n            Error,\n            'invalid UTF-8 sequence',\n            true,\n            1007,\n            'WS_ERR_INVALID_UTF8'\n          );\n\n          cb(error);\n          return;\n        }\n\n        this._loop = false;\n        this.emit('conclude', code, buf);\n        this.end();\n      }\n\n      this._state = GET_INFO;\n      return;\n    }\n\n    if (this._allowSynchronousEvents) {\n      this.emit(this._opcode === 0x09 ? 'ping' : 'pong', data);\n      this._state = GET_INFO;\n    } else {\n      this._state = DEFER_EVENT;\n      setImmediate(() => {\n        this.emit(this._opcode === 0x09 ? 'ping' : 'pong', data);\n        this._state = GET_INFO;\n        this.startLoop(cb);\n      });\n    }\n  }\n\n  /**\n   * Builds an error object.\n   *\n   * @param {function(new:Error|RangeError)} ErrorCtor The error constructor\n   * @param {String} message The error message\n   * @param {Boolean} prefix Specifies whether or not to add a default prefix to\n   *     `message`\n   * @param {Number} statusCode The status code\n   * @param {String} errorCode The exposed error code\n   * @return {(Error|RangeError)} The error\n   * @private\n   */\n  createError(ErrorCtor, message, prefix, statusCode, errorCode) {\n    this._loop = false;\n    this._errored = true;\n\n    const err = new ErrorCtor(\n      prefix ? `Invalid WebSocket frame: ${message}` : message\n    );\n\n    Error.captureStackTrace(err, this.createError);\n    err.code = errorCode;\n    err[kStatusCode] = statusCode;\n    return err;\n  }\n}\n\nmodule.exports = Receiver;\n"
  },
  {
    "path": "lib/sender.js",
    "content": "/* eslint no-unused-vars: [\"error\", { \"varsIgnorePattern\": \"^Duplex\" }] */\n\n'use strict';\n\nconst { Duplex } = require('stream');\nconst { randomFillSync } = require('crypto');\n\nconst PerMessageDeflate = require('./permessage-deflate');\nconst { EMPTY_BUFFER, kWebSocket, NOOP } = require('./constants');\nconst { isBlob, isValidStatusCode } = require('./validation');\nconst { mask: applyMask, toBuffer } = require('./buffer-util');\n\nconst kByteLength = Symbol('kByteLength');\nconst maskBuffer = Buffer.alloc(4);\nconst RANDOM_POOL_SIZE = 8 * 1024;\nlet randomPool;\nlet randomPoolPointer = RANDOM_POOL_SIZE;\n\nconst DEFAULT = 0;\nconst DEFLATING = 1;\nconst GET_BLOB_DATA = 2;\n\n/**\n * HyBi Sender implementation.\n */\nclass Sender {\n  /**\n   * Creates a Sender instance.\n   *\n   * @param {Duplex} socket The connection socket\n   * @param {Object} [extensions] An object containing the negotiated extensions\n   * @param {Function} [generateMask] The function used to generate the masking\n   *     key\n   */\n  constructor(socket, extensions, generateMask) {\n    this._extensions = extensions || {};\n\n    if (generateMask) {\n      this._generateMask = generateMask;\n      this._maskBuffer = Buffer.alloc(4);\n    }\n\n    this._socket = socket;\n\n    this._firstFragment = true;\n    this._compress = false;\n\n    this._bufferedBytes = 0;\n    this._queue = [];\n    this._state = DEFAULT;\n    this.onerror = NOOP;\n    this[kWebSocket] = undefined;\n  }\n\n  /**\n   * Frames a piece of data according to the HyBi WebSocket protocol.\n   *\n   * @param {(Buffer|String)} data The data to frame\n   * @param {Object} options Options object\n   * @param {Boolean} [options.fin=false] Specifies whether or not to set the\n   *     FIN bit\n   * @param {Function} [options.generateMask] The function used to generate the\n   *     masking key\n   * @param {Boolean} [options.mask=false] Specifies whether or not to mask\n   *     `data`\n   * @param {Buffer} [options.maskBuffer] The buffer used to store the masking\n   *     key\n   * @param {Number} options.opcode The opcode\n   * @param {Boolean} [options.readOnly=false] Specifies whether `data` can be\n   *     modified\n   * @param {Boolean} [options.rsv1=false] Specifies whether or not to set the\n   *     RSV1 bit\n   * @return {(Buffer|String)[]} The framed data\n   * @public\n   */\n  static frame(data, options) {\n    let mask;\n    let merge = false;\n    let offset = 2;\n    let skipMasking = false;\n\n    if (options.mask) {\n      mask = options.maskBuffer || maskBuffer;\n\n      if (options.generateMask) {\n        options.generateMask(mask);\n      } else {\n        if (randomPoolPointer === RANDOM_POOL_SIZE) {\n          /* istanbul ignore else  */\n          if (randomPool === undefined) {\n            //\n            // This is lazily initialized because server-sent frames must not\n            // be masked so it may never be used.\n            //\n            randomPool = Buffer.alloc(RANDOM_POOL_SIZE);\n          }\n\n          randomFillSync(randomPool, 0, RANDOM_POOL_SIZE);\n          randomPoolPointer = 0;\n        }\n\n        mask[0] = randomPool[randomPoolPointer++];\n        mask[1] = randomPool[randomPoolPointer++];\n        mask[2] = randomPool[randomPoolPointer++];\n        mask[3] = randomPool[randomPoolPointer++];\n      }\n\n      skipMasking = (mask[0] | mask[1] | mask[2] | mask[3]) === 0;\n      offset = 6;\n    }\n\n    let dataLength;\n\n    if (typeof data === 'string') {\n      if (\n        (!options.mask || skipMasking) &&\n        options[kByteLength] !== undefined\n      ) {\n        dataLength = options[kByteLength];\n      } else {\n        data = Buffer.from(data);\n        dataLength = data.length;\n      }\n    } else {\n      dataLength = data.length;\n      merge = options.mask && options.readOnly && !skipMasking;\n    }\n\n    let payloadLength = dataLength;\n\n    if (dataLength >= 65536) {\n      offset += 8;\n      payloadLength = 127;\n    } else if (dataLength > 125) {\n      offset += 2;\n      payloadLength = 126;\n    }\n\n    const target = Buffer.allocUnsafe(merge ? dataLength + offset : offset);\n\n    target[0] = options.fin ? options.opcode | 0x80 : options.opcode;\n    if (options.rsv1) target[0] |= 0x40;\n\n    target[1] = payloadLength;\n\n    if (payloadLength === 126) {\n      target.writeUInt16BE(dataLength, 2);\n    } else if (payloadLength === 127) {\n      target[2] = target[3] = 0;\n      target.writeUIntBE(dataLength, 4, 6);\n    }\n\n    if (!options.mask) return [target, data];\n\n    target[1] |= 0x80;\n    target[offset - 4] = mask[0];\n    target[offset - 3] = mask[1];\n    target[offset - 2] = mask[2];\n    target[offset - 1] = mask[3];\n\n    if (skipMasking) return [target, data];\n\n    if (merge) {\n      applyMask(data, mask, target, offset, dataLength);\n      return [target];\n    }\n\n    applyMask(data, mask, data, 0, dataLength);\n    return [target, data];\n  }\n\n  /**\n   * Sends a close message to the other peer.\n   *\n   * @param {Number} [code] The status code component of the body\n   * @param {(String|Buffer)} [data] The message component of the body\n   * @param {Boolean} [mask=false] Specifies whether or not to mask the message\n   * @param {Function} [cb] Callback\n   * @public\n   */\n  close(code, data, mask, cb) {\n    let buf;\n\n    if (code === undefined) {\n      buf = EMPTY_BUFFER;\n    } else if (typeof code !== 'number' || !isValidStatusCode(code)) {\n      throw new TypeError('First argument must be a valid error code number');\n    } else if (data === undefined || !data.length) {\n      buf = Buffer.allocUnsafe(2);\n      buf.writeUInt16BE(code, 0);\n    } else {\n      const length = Buffer.byteLength(data);\n\n      if (length > 123) {\n        throw new RangeError('The message must not be greater than 123 bytes');\n      }\n\n      buf = Buffer.allocUnsafe(2 + length);\n      buf.writeUInt16BE(code, 0);\n\n      if (typeof data === 'string') {\n        buf.write(data, 2);\n      } else {\n        buf.set(data, 2);\n      }\n    }\n\n    const options = {\n      [kByteLength]: buf.length,\n      fin: true,\n      generateMask: this._generateMask,\n      mask,\n      maskBuffer: this._maskBuffer,\n      opcode: 0x08,\n      readOnly: false,\n      rsv1: false\n    };\n\n    if (this._state !== DEFAULT) {\n      this.enqueue([this.dispatch, buf, false, options, cb]);\n    } else {\n      this.sendFrame(Sender.frame(buf, options), cb);\n    }\n  }\n\n  /**\n   * Sends a ping message to the other peer.\n   *\n   * @param {*} data The message to send\n   * @param {Boolean} [mask=false] Specifies whether or not to mask `data`\n   * @param {Function} [cb] Callback\n   * @public\n   */\n  ping(data, mask, cb) {\n    let byteLength;\n    let readOnly;\n\n    if (typeof data === 'string') {\n      byteLength = Buffer.byteLength(data);\n      readOnly = false;\n    } else if (isBlob(data)) {\n      byteLength = data.size;\n      readOnly = false;\n    } else {\n      data = toBuffer(data);\n      byteLength = data.length;\n      readOnly = toBuffer.readOnly;\n    }\n\n    if (byteLength > 125) {\n      throw new RangeError('The data size must not be greater than 125 bytes');\n    }\n\n    const options = {\n      [kByteLength]: byteLength,\n      fin: true,\n      generateMask: this._generateMask,\n      mask,\n      maskBuffer: this._maskBuffer,\n      opcode: 0x09,\n      readOnly,\n      rsv1: false\n    };\n\n    if (isBlob(data)) {\n      if (this._state !== DEFAULT) {\n        this.enqueue([this.getBlobData, data, false, options, cb]);\n      } else {\n        this.getBlobData(data, false, options, cb);\n      }\n    } else if (this._state !== DEFAULT) {\n      this.enqueue([this.dispatch, data, false, options, cb]);\n    } else {\n      this.sendFrame(Sender.frame(data, options), cb);\n    }\n  }\n\n  /**\n   * Sends a pong message to the other peer.\n   *\n   * @param {*} data The message to send\n   * @param {Boolean} [mask=false] Specifies whether or not to mask `data`\n   * @param {Function} [cb] Callback\n   * @public\n   */\n  pong(data, mask, cb) {\n    let byteLength;\n    let readOnly;\n\n    if (typeof data === 'string') {\n      byteLength = Buffer.byteLength(data);\n      readOnly = false;\n    } else if (isBlob(data)) {\n      byteLength = data.size;\n      readOnly = false;\n    } else {\n      data = toBuffer(data);\n      byteLength = data.length;\n      readOnly = toBuffer.readOnly;\n    }\n\n    if (byteLength > 125) {\n      throw new RangeError('The data size must not be greater than 125 bytes');\n    }\n\n    const options = {\n      [kByteLength]: byteLength,\n      fin: true,\n      generateMask: this._generateMask,\n      mask,\n      maskBuffer: this._maskBuffer,\n      opcode: 0x0a,\n      readOnly,\n      rsv1: false\n    };\n\n    if (isBlob(data)) {\n      if (this._state !== DEFAULT) {\n        this.enqueue([this.getBlobData, data, false, options, cb]);\n      } else {\n        this.getBlobData(data, false, options, cb);\n      }\n    } else if (this._state !== DEFAULT) {\n      this.enqueue([this.dispatch, data, false, options, cb]);\n    } else {\n      this.sendFrame(Sender.frame(data, options), cb);\n    }\n  }\n\n  /**\n   * Sends a data message to the other peer.\n   *\n   * @param {*} data The message to send\n   * @param {Object} options Options object\n   * @param {Boolean} [options.binary=false] Specifies whether `data` is binary\n   *     or text\n   * @param {Boolean} [options.compress=false] Specifies whether or not to\n   *     compress `data`\n   * @param {Boolean} [options.fin=false] Specifies whether the fragment is the\n   *     last one\n   * @param {Boolean} [options.mask=false] Specifies whether or not to mask\n   *     `data`\n   * @param {Function} [cb] Callback\n   * @public\n   */\n  send(data, options, cb) {\n    const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];\n    let opcode = options.binary ? 2 : 1;\n    let rsv1 = options.compress;\n\n    let byteLength;\n    let readOnly;\n\n    if (typeof data === 'string') {\n      byteLength = Buffer.byteLength(data);\n      readOnly = false;\n    } else if (isBlob(data)) {\n      byteLength = data.size;\n      readOnly = false;\n    } else {\n      data = toBuffer(data);\n      byteLength = data.length;\n      readOnly = toBuffer.readOnly;\n    }\n\n    if (this._firstFragment) {\n      this._firstFragment = false;\n      if (\n        rsv1 &&\n        perMessageDeflate &&\n        perMessageDeflate.params[\n          perMessageDeflate._isServer\n            ? 'server_no_context_takeover'\n            : 'client_no_context_takeover'\n        ]\n      ) {\n        rsv1 = byteLength >= perMessageDeflate._threshold;\n      }\n      this._compress = rsv1;\n    } else {\n      rsv1 = false;\n      opcode = 0;\n    }\n\n    if (options.fin) this._firstFragment = true;\n\n    const opts = {\n      [kByteLength]: byteLength,\n      fin: options.fin,\n      generateMask: this._generateMask,\n      mask: options.mask,\n      maskBuffer: this._maskBuffer,\n      opcode,\n      readOnly,\n      rsv1\n    };\n\n    if (isBlob(data)) {\n      if (this._state !== DEFAULT) {\n        this.enqueue([this.getBlobData, data, this._compress, opts, cb]);\n      } else {\n        this.getBlobData(data, this._compress, opts, cb);\n      }\n    } else if (this._state !== DEFAULT) {\n      this.enqueue([this.dispatch, data, this._compress, opts, cb]);\n    } else {\n      this.dispatch(data, this._compress, opts, cb);\n    }\n  }\n\n  /**\n   * Gets the contents of a blob as binary data.\n   *\n   * @param {Blob} blob The blob\n   * @param {Boolean} [compress=false] Specifies whether or not to compress\n   *     the data\n   * @param {Object} options Options object\n   * @param {Boolean} [options.fin=false] Specifies whether or not to set the\n   *     FIN bit\n   * @param {Function} [options.generateMask] The function used to generate the\n   *     masking key\n   * @param {Boolean} [options.mask=false] Specifies whether or not to mask\n   *     `data`\n   * @param {Buffer} [options.maskBuffer] The buffer used to store the masking\n   *     key\n   * @param {Number} options.opcode The opcode\n   * @param {Boolean} [options.readOnly=false] Specifies whether `data` can be\n   *     modified\n   * @param {Boolean} [options.rsv1=false] Specifies whether or not to set the\n   *     RSV1 bit\n   * @param {Function} [cb] Callback\n   * @private\n   */\n  getBlobData(blob, compress, options, cb) {\n    this._bufferedBytes += options[kByteLength];\n    this._state = GET_BLOB_DATA;\n\n    blob\n      .arrayBuffer()\n      .then((arrayBuffer) => {\n        if (this._socket.destroyed) {\n          const err = new Error(\n            'The socket was closed while the blob was being read'\n          );\n\n          //\n          // `callCallbacks` is called in the next tick to ensure that errors\n          // that might be thrown in the callbacks behave like errors thrown\n          // outside the promise chain.\n          //\n          process.nextTick(callCallbacks, this, err, cb);\n          return;\n        }\n\n        this._bufferedBytes -= options[kByteLength];\n        const data = toBuffer(arrayBuffer);\n\n        if (!compress) {\n          this._state = DEFAULT;\n          this.sendFrame(Sender.frame(data, options), cb);\n          this.dequeue();\n        } else {\n          this.dispatch(data, compress, options, cb);\n        }\n      })\n      .catch((err) => {\n        //\n        // `onError` is called in the next tick for the same reason that\n        // `callCallbacks` above is.\n        //\n        process.nextTick(onError, this, err, cb);\n      });\n  }\n\n  /**\n   * Dispatches a message.\n   *\n   * @param {(Buffer|String)} data The message to send\n   * @param {Boolean} [compress=false] Specifies whether or not to compress\n   *     `data`\n   * @param {Object} options Options object\n   * @param {Boolean} [options.fin=false] Specifies whether or not to set the\n   *     FIN bit\n   * @param {Function} [options.generateMask] The function used to generate the\n   *     masking key\n   * @param {Boolean} [options.mask=false] Specifies whether or not to mask\n   *     `data`\n   * @param {Buffer} [options.maskBuffer] The buffer used to store the masking\n   *     key\n   * @param {Number} options.opcode The opcode\n   * @param {Boolean} [options.readOnly=false] Specifies whether `data` can be\n   *     modified\n   * @param {Boolean} [options.rsv1=false] Specifies whether or not to set the\n   *     RSV1 bit\n   * @param {Function} [cb] Callback\n   * @private\n   */\n  dispatch(data, compress, options, cb) {\n    if (!compress) {\n      this.sendFrame(Sender.frame(data, options), cb);\n      return;\n    }\n\n    const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];\n\n    this._bufferedBytes += options[kByteLength];\n    this._state = DEFLATING;\n    perMessageDeflate.compress(data, options.fin, (_, buf) => {\n      if (this._socket.destroyed) {\n        const err = new Error(\n          'The socket was closed while data was being compressed'\n        );\n\n        callCallbacks(this, err, cb);\n        return;\n      }\n\n      this._bufferedBytes -= options[kByteLength];\n      this._state = DEFAULT;\n      options.readOnly = false;\n      this.sendFrame(Sender.frame(buf, options), cb);\n      this.dequeue();\n    });\n  }\n\n  /**\n   * Executes queued send operations.\n   *\n   * @private\n   */\n  dequeue() {\n    while (this._state === DEFAULT && this._queue.length) {\n      const params = this._queue.shift();\n\n      this._bufferedBytes -= params[3][kByteLength];\n      Reflect.apply(params[0], this, params.slice(1));\n    }\n  }\n\n  /**\n   * Enqueues a send operation.\n   *\n   * @param {Array} params Send operation parameters.\n   * @private\n   */\n  enqueue(params) {\n    this._bufferedBytes += params[3][kByteLength];\n    this._queue.push(params);\n  }\n\n  /**\n   * Sends a frame.\n   *\n   * @param {(Buffer | String)[]} list The frame to send\n   * @param {Function} [cb] Callback\n   * @private\n   */\n  sendFrame(list, cb) {\n    if (list.length === 2) {\n      this._socket.cork();\n      this._socket.write(list[0]);\n      this._socket.write(list[1], cb);\n      this._socket.uncork();\n    } else {\n      this._socket.write(list[0], cb);\n    }\n  }\n}\n\nmodule.exports = Sender;\n\n/**\n * Calls queued callbacks with an error.\n *\n * @param {Sender} sender The `Sender` instance\n * @param {Error} err The error to call the callbacks with\n * @param {Function} [cb] The first callback\n * @private\n */\nfunction callCallbacks(sender, err, cb) {\n  if (typeof cb === 'function') cb(err);\n\n  for (let i = 0; i < sender._queue.length; i++) {\n    const params = sender._queue[i];\n    const callback = params[params.length - 1];\n\n    if (typeof callback === 'function') callback(err);\n  }\n}\n\n/**\n * Handles a `Sender` error.\n *\n * @param {Sender} sender The `Sender` instance\n * @param {Error} err The error\n * @param {Function} [cb] The first pending callback\n * @private\n */\nfunction onError(sender, err, cb) {\n  callCallbacks(sender, err, cb);\n  sender.onerror(err);\n}\n"
  },
  {
    "path": "lib/stream.js",
    "content": "/* eslint no-unused-vars: [\"error\", { \"varsIgnorePattern\": \"^WebSocket$\" }] */\n'use strict';\n\nconst WebSocket = require('./websocket');\nconst { Duplex } = require('stream');\n\n/**\n * Emits the `'close'` event on a stream.\n *\n * @param {Duplex} stream The stream.\n * @private\n */\nfunction emitClose(stream) {\n  stream.emit('close');\n}\n\n/**\n * The listener of the `'end'` event.\n *\n * @private\n */\nfunction duplexOnEnd() {\n  if (!this.destroyed && this._writableState.finished) {\n    this.destroy();\n  }\n}\n\n/**\n * The listener of the `'error'` event.\n *\n * @param {Error} err The error\n * @private\n */\nfunction duplexOnError(err) {\n  this.removeListener('error', duplexOnError);\n  this.destroy();\n  if (this.listenerCount('error') === 0) {\n    // Do not suppress the throwing behavior.\n    this.emit('error', err);\n  }\n}\n\n/**\n * Wraps a `WebSocket` in a duplex stream.\n *\n * @param {WebSocket} ws The `WebSocket` to wrap\n * @param {Object} [options] The options for the `Duplex` constructor\n * @return {Duplex} The duplex stream\n * @public\n */\nfunction createWebSocketStream(ws, options) {\n  let terminateOnDestroy = true;\n\n  const duplex = new Duplex({\n    ...options,\n    autoDestroy: false,\n    emitClose: false,\n    objectMode: false,\n    writableObjectMode: false\n  });\n\n  ws.on('message', function message(msg, isBinary) {\n    const data =\n      !isBinary && duplex._readableState.objectMode ? msg.toString() : msg;\n\n    if (!duplex.push(data)) ws.pause();\n  });\n\n  ws.once('error', function error(err) {\n    if (duplex.destroyed) return;\n\n    // Prevent `ws.terminate()` from being called by `duplex._destroy()`.\n    //\n    // - If the `'error'` event is emitted before the `'open'` event, then\n    //   `ws.terminate()` is a noop as no socket is assigned.\n    // - Otherwise, the error is re-emitted by the listener of the `'error'`\n    //   event of the `Receiver` object. The listener already closes the\n    //   connection by calling `ws.close()`. This allows a close frame to be\n    //   sent to the other peer. If `ws.terminate()` is called right after this,\n    //   then the close frame might not be sent.\n    terminateOnDestroy = false;\n    duplex.destroy(err);\n  });\n\n  ws.once('close', function close() {\n    if (duplex.destroyed) return;\n\n    duplex.push(null);\n  });\n\n  duplex._destroy = function (err, callback) {\n    if (ws.readyState === ws.CLOSED) {\n      callback(err);\n      process.nextTick(emitClose, duplex);\n      return;\n    }\n\n    let called = false;\n\n    ws.once('error', function error(err) {\n      called = true;\n      callback(err);\n    });\n\n    ws.once('close', function close() {\n      if (!called) callback(err);\n      process.nextTick(emitClose, duplex);\n    });\n\n    if (terminateOnDestroy) ws.terminate();\n  };\n\n  duplex._final = function (callback) {\n    if (ws.readyState === ws.CONNECTING) {\n      ws.once('open', function open() {\n        duplex._final(callback);\n      });\n      return;\n    }\n\n    // If the value of the `_socket` property is `null` it means that `ws` is a\n    // client websocket and the handshake failed. In fact, when this happens, a\n    // socket is never assigned to the websocket. Wait for the `'error'` event\n    // that will be emitted by the websocket.\n    if (ws._socket === null) return;\n\n    if (ws._socket._writableState.finished) {\n      callback();\n      if (duplex._readableState.endEmitted) duplex.destroy();\n    } else {\n      ws._socket.once('finish', function finish() {\n        // `duplex` is not destroyed here because the `'end'` event will be\n        // emitted on `duplex` after this `'finish'` event. The EOF signaling\n        // `null` chunk is, in fact, pushed when the websocket emits `'close'`.\n        callback();\n      });\n      ws.close();\n    }\n  };\n\n  duplex._read = function () {\n    if (ws.isPaused) ws.resume();\n  };\n\n  duplex._write = function (chunk, encoding, callback) {\n    if (ws.readyState === ws.CONNECTING) {\n      ws.once('open', function open() {\n        duplex._write(chunk, encoding, callback);\n      });\n      return;\n    }\n\n    ws.send(chunk, callback);\n  };\n\n  duplex.on('end', duplexOnEnd);\n  duplex.on('error', duplexOnError);\n  return duplex;\n}\n\nmodule.exports = createWebSocketStream;\n"
  },
  {
    "path": "lib/subprotocol.js",
    "content": "'use strict';\n\nconst { tokenChars } = require('./validation');\n\n/**\n * Parses the `Sec-WebSocket-Protocol` header into a set of subprotocol names.\n *\n * @param {String} header The field value of the header\n * @return {Set} The subprotocol names\n * @public\n */\nfunction parse(header) {\n  const protocols = new Set();\n  let start = -1;\n  let end = -1;\n  let i = 0;\n\n  for (i; i < header.length; i++) {\n    const code = header.charCodeAt(i);\n\n    if (end === -1 && tokenChars[code] === 1) {\n      if (start === -1) start = i;\n    } else if (\n      i !== 0 &&\n      (code === 0x20 /* ' ' */ || code === 0x09) /* '\\t' */\n    ) {\n      if (end === -1 && start !== -1) end = i;\n    } else if (code === 0x2c /* ',' */) {\n      if (start === -1) {\n        throw new SyntaxError(`Unexpected character at index ${i}`);\n      }\n\n      if (end === -1) end = i;\n\n      const protocol = header.slice(start, end);\n\n      if (protocols.has(protocol)) {\n        throw new SyntaxError(`The \"${protocol}\" subprotocol is duplicated`);\n      }\n\n      protocols.add(protocol);\n      start = end = -1;\n    } else {\n      throw new SyntaxError(`Unexpected character at index ${i}`);\n    }\n  }\n\n  if (start === -1 || end !== -1) {\n    throw new SyntaxError('Unexpected end of input');\n  }\n\n  const protocol = header.slice(start, i);\n\n  if (protocols.has(protocol)) {\n    throw new SyntaxError(`The \"${protocol}\" subprotocol is duplicated`);\n  }\n\n  protocols.add(protocol);\n  return protocols;\n}\n\nmodule.exports = { parse };\n"
  },
  {
    "path": "lib/validation.js",
    "content": "'use strict';\n\nconst { isUtf8 } = require('buffer');\n\nconst { hasBlob } = require('./constants');\n\n//\n// Allowed token characters:\n//\n// '!', '#', '$', '%', '&', ''', '*', '+', '-',\n// '.', 0-9, A-Z, '^', '_', '`', a-z, '|', '~'\n//\n// tokenChars[32] === 0 // ' '\n// tokenChars[33] === 1 // '!'\n// tokenChars[34] === 0 // '\"'\n// ...\n//\n// prettier-ignore\nconst tokenChars = [\n  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 15\n  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16 - 31\n  0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, // 32 - 47\n  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 48 - 63\n  0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 64 - 79\n  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, // 80 - 95\n  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 96 - 111\n  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0 // 112 - 127\n];\n\n/**\n * Checks if a status code is allowed in a close frame.\n *\n * @param {Number} code The status code\n * @return {Boolean} `true` if the status code is valid, else `false`\n * @public\n */\nfunction isValidStatusCode(code) {\n  return (\n    (code >= 1000 &&\n      code <= 1014 &&\n      code !== 1004 &&\n      code !== 1005 &&\n      code !== 1006) ||\n    (code >= 3000 && code <= 4999)\n  );\n}\n\n/**\n * Checks if a given buffer contains only correct UTF-8.\n * Ported from https://www.cl.cam.ac.uk/%7Emgk25/ucs/utf8_check.c by\n * Markus Kuhn.\n *\n * @param {Buffer} buf The buffer to check\n * @return {Boolean} `true` if `buf` contains only correct UTF-8, else `false`\n * @public\n */\nfunction _isValidUTF8(buf) {\n  const len = buf.length;\n  let i = 0;\n\n  while (i < len) {\n    if ((buf[i] & 0x80) === 0) {\n      // 0xxxxxxx\n      i++;\n    } else if ((buf[i] & 0xe0) === 0xc0) {\n      // 110xxxxx 10xxxxxx\n      if (\n        i + 1 === len ||\n        (buf[i + 1] & 0xc0) !== 0x80 ||\n        (buf[i] & 0xfe) === 0xc0 // Overlong\n      ) {\n        return false;\n      }\n\n      i += 2;\n    } else if ((buf[i] & 0xf0) === 0xe0) {\n      // 1110xxxx 10xxxxxx 10xxxxxx\n      if (\n        i + 2 >= len ||\n        (buf[i + 1] & 0xc0) !== 0x80 ||\n        (buf[i + 2] & 0xc0) !== 0x80 ||\n        (buf[i] === 0xe0 && (buf[i + 1] & 0xe0) === 0x80) || // Overlong\n        (buf[i] === 0xed && (buf[i + 1] & 0xe0) === 0xa0) // Surrogate (U+D800 - U+DFFF)\n      ) {\n        return false;\n      }\n\n      i += 3;\n    } else if ((buf[i] & 0xf8) === 0xf0) {\n      // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx\n      if (\n        i + 3 >= len ||\n        (buf[i + 1] & 0xc0) !== 0x80 ||\n        (buf[i + 2] & 0xc0) !== 0x80 ||\n        (buf[i + 3] & 0xc0) !== 0x80 ||\n        (buf[i] === 0xf0 && (buf[i + 1] & 0xf0) === 0x80) || // Overlong\n        (buf[i] === 0xf4 && buf[i + 1] > 0x8f) ||\n        buf[i] > 0xf4 // > U+10FFFF\n      ) {\n        return false;\n      }\n\n      i += 4;\n    } else {\n      return false;\n    }\n  }\n\n  return true;\n}\n\n/**\n * Determines whether a value is a `Blob`.\n *\n * @param {*} value The value to be tested\n * @return {Boolean} `true` if `value` is a `Blob`, else `false`\n * @private\n */\nfunction isBlob(value) {\n  return (\n    hasBlob &&\n    typeof value === 'object' &&\n    typeof value.arrayBuffer === 'function' &&\n    typeof value.type === 'string' &&\n    typeof value.stream === 'function' &&\n    (value[Symbol.toStringTag] === 'Blob' ||\n      value[Symbol.toStringTag] === 'File')\n  );\n}\n\nmodule.exports = {\n  isBlob,\n  isValidStatusCode,\n  isValidUTF8: _isValidUTF8,\n  tokenChars\n};\n\nif (isUtf8) {\n  module.exports.isValidUTF8 = function (buf) {\n    return buf.length < 24 ? _isValidUTF8(buf) : isUtf8(buf);\n  };\n} /* istanbul ignore else  */ else if (!process.env.WS_NO_UTF_8_VALIDATE) {\n  try {\n    const isValidUTF8 = require('utf-8-validate');\n\n    module.exports.isValidUTF8 = function (buf) {\n      return buf.length < 32 ? _isValidUTF8(buf) : isValidUTF8(buf);\n    };\n  } catch (e) {\n    // Continue regardless of the error.\n  }\n}\n"
  },
  {
    "path": "lib/websocket-server.js",
    "content": "/* eslint no-unused-vars: [\"error\", { \"varsIgnorePattern\": \"^Duplex$\", \"caughtErrors\": \"none\" }] */\n\n'use strict';\n\nconst EventEmitter = require('events');\nconst http = require('http');\nconst { Duplex } = require('stream');\nconst { createHash } = require('crypto');\n\nconst extension = require('./extension');\nconst PerMessageDeflate = require('./permessage-deflate');\nconst subprotocol = require('./subprotocol');\nconst WebSocket = require('./websocket');\nconst { CLOSE_TIMEOUT, GUID, kWebSocket } = require('./constants');\n\nconst keyRegex = /^[+/0-9A-Za-z]{22}==$/;\n\nconst RUNNING = 0;\nconst CLOSING = 1;\nconst CLOSED = 2;\n\n/**\n * Class representing a WebSocket server.\n *\n * @extends EventEmitter\n */\nclass WebSocketServer extends EventEmitter {\n  /**\n   * Create a `WebSocketServer` instance.\n   *\n   * @param {Object} options Configuration options\n   * @param {Boolean} [options.allowSynchronousEvents=true] Specifies whether\n   *     any of the `'message'`, `'ping'`, and `'pong'` events can be emitted\n   *     multiple times in the same tick\n   * @param {Boolean} [options.autoPong=true] Specifies whether or not to\n   *     automatically send a pong in response to a ping\n   * @param {Number} [options.backlog=511] The maximum length of the queue of\n   *     pending connections\n   * @param {Boolean} [options.clientTracking=true] Specifies whether or not to\n   *     track clients\n   * @param {Number} [options.closeTimeout=30000] Duration in milliseconds to\n   *     wait for the closing handshake to finish after `websocket.close()` is\n   *     called\n   * @param {Function} [options.handleProtocols] A hook to handle protocols\n   * @param {String} [options.host] The hostname where to bind the server\n   * @param {Number} [options.maxPayload=104857600] The maximum allowed message\n   *     size\n   * @param {Boolean} [options.noServer=false] Enable no server mode\n   * @param {String} [options.path] Accept only connections matching this path\n   * @param {(Boolean|Object)} [options.perMessageDeflate=false] Enable/disable\n   *     permessage-deflate\n   * @param {Number} [options.port] The port where to bind the server\n   * @param {(http.Server|https.Server)} [options.server] A pre-created HTTP/S\n   *     server to use\n   * @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or\n   *     not to skip UTF-8 validation for text and close messages\n   * @param {Function} [options.verifyClient] A hook to reject connections\n   * @param {Function} [options.WebSocket=WebSocket] Specifies the `WebSocket`\n   *     class to use. It must be the `WebSocket` class or class that extends it\n   * @param {Function} [callback] A listener for the `listening` event\n   */\n  constructor(options, callback) {\n    super();\n\n    options = {\n      allowSynchronousEvents: true,\n      autoPong: true,\n      maxPayload: 100 * 1024 * 1024,\n      skipUTF8Validation: false,\n      perMessageDeflate: false,\n      handleProtocols: null,\n      clientTracking: true,\n      closeTimeout: CLOSE_TIMEOUT,\n      verifyClient: null,\n      noServer: false,\n      backlog: null, // use default (511 as implemented in net.js)\n      server: null,\n      host: null,\n      path: null,\n      port: null,\n      WebSocket,\n      ...options\n    };\n\n    if (\n      (options.port == null && !options.server && !options.noServer) ||\n      (options.port != null && (options.server || options.noServer)) ||\n      (options.server && options.noServer)\n    ) {\n      throw new TypeError(\n        'One and only one of the \"port\", \"server\", or \"noServer\" options ' +\n          'must be specified'\n      );\n    }\n\n    if (options.port != null) {\n      this._server = http.createServer((req, res) => {\n        const body = http.STATUS_CODES[426];\n\n        res.writeHead(426, {\n          'Content-Length': body.length,\n          'Content-Type': 'text/plain'\n        });\n        res.end(body);\n      });\n      this._server.listen(\n        options.port,\n        options.host,\n        options.backlog,\n        callback\n      );\n    } else if (options.server) {\n      this._server = options.server;\n    }\n\n    if (this._server) {\n      const emitConnection = this.emit.bind(this, 'connection');\n\n      this._removeListeners = addListeners(this._server, {\n        listening: this.emit.bind(this, 'listening'),\n        error: this.emit.bind(this, 'error'),\n        upgrade: (req, socket, head) => {\n          this.handleUpgrade(req, socket, head, emitConnection);\n        }\n      });\n    }\n\n    if (options.perMessageDeflate === true) options.perMessageDeflate = {};\n    if (options.clientTracking) {\n      this.clients = new Set();\n      this._shouldEmitClose = false;\n    }\n\n    this.options = options;\n    this._state = RUNNING;\n  }\n\n  /**\n   * Returns the bound address, the address family name, and port of the server\n   * as reported by the operating system if listening on an IP socket.\n   * If the server is listening on a pipe or UNIX domain socket, the name is\n   * returned as a string.\n   *\n   * @return {(Object|String|null)} The address of the server\n   * @public\n   */\n  address() {\n    if (this.options.noServer) {\n      throw new Error('The server is operating in \"noServer\" mode');\n    }\n\n    if (!this._server) return null;\n    return this._server.address();\n  }\n\n  /**\n   * Stop the server from accepting new connections and emit the `'close'` event\n   * when all existing connections are closed.\n   *\n   * @param {Function} [cb] A one-time listener for the `'close'` event\n   * @public\n   */\n  close(cb) {\n    if (this._state === CLOSED) {\n      if (cb) {\n        this.once('close', () => {\n          cb(new Error('The server is not running'));\n        });\n      }\n\n      process.nextTick(emitClose, this);\n      return;\n    }\n\n    if (cb) this.once('close', cb);\n\n    if (this._state === CLOSING) return;\n    this._state = CLOSING;\n\n    if (this.options.noServer || this.options.server) {\n      if (this._server) {\n        this._removeListeners();\n        this._removeListeners = this._server = null;\n      }\n\n      if (this.clients) {\n        if (!this.clients.size) {\n          process.nextTick(emitClose, this);\n        } else {\n          this._shouldEmitClose = true;\n        }\n      } else {\n        process.nextTick(emitClose, this);\n      }\n    } else {\n      const server = this._server;\n\n      this._removeListeners();\n      this._removeListeners = this._server = null;\n\n      //\n      // The HTTP/S server was created internally. Close it, and rely on its\n      // `'close'` event.\n      //\n      server.close(() => {\n        emitClose(this);\n      });\n    }\n  }\n\n  /**\n   * See if a given request should be handled by this server instance.\n   *\n   * @param {http.IncomingMessage} req Request object to inspect\n   * @return {Boolean} `true` if the request is valid, else `false`\n   * @public\n   */\n  shouldHandle(req) {\n    if (this.options.path) {\n      const index = req.url.indexOf('?');\n      const pathname = index !== -1 ? req.url.slice(0, index) : req.url;\n\n      if (pathname !== this.options.path) return false;\n    }\n\n    return true;\n  }\n\n  /**\n   * Handle a HTTP Upgrade request.\n   *\n   * @param {http.IncomingMessage} req The request object\n   * @param {Duplex} socket The network socket between the server and client\n   * @param {Buffer} head The first packet of the upgraded stream\n   * @param {Function} cb Callback\n   * @public\n   */\n  handleUpgrade(req, socket, head, cb) {\n    socket.on('error', socketOnError);\n\n    const key = req.headers['sec-websocket-key'];\n    const upgrade = req.headers.upgrade;\n    const version = +req.headers['sec-websocket-version'];\n\n    if (req.method !== 'GET') {\n      const message = 'Invalid HTTP method';\n      abortHandshakeOrEmitwsClientError(this, req, socket, 405, message);\n      return;\n    }\n\n    if (upgrade === undefined || upgrade.toLowerCase() !== 'websocket') {\n      const message = 'Invalid Upgrade header';\n      abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);\n      return;\n    }\n\n    if (key === undefined || !keyRegex.test(key)) {\n      const message = 'Missing or invalid Sec-WebSocket-Key header';\n      abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);\n      return;\n    }\n\n    if (version !== 13 && version !== 8) {\n      const message = 'Missing or invalid Sec-WebSocket-Version header';\n      abortHandshakeOrEmitwsClientError(this, req, socket, 400, message, {\n        'Sec-WebSocket-Version': '13, 8'\n      });\n      return;\n    }\n\n    if (!this.shouldHandle(req)) {\n      abortHandshake(socket, 400);\n      return;\n    }\n\n    const secWebSocketProtocol = req.headers['sec-websocket-protocol'];\n    let protocols = new Set();\n\n    if (secWebSocketProtocol !== undefined) {\n      try {\n        protocols = subprotocol.parse(secWebSocketProtocol);\n      } catch (err) {\n        const message = 'Invalid Sec-WebSocket-Protocol header';\n        abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);\n        return;\n      }\n    }\n\n    const secWebSocketExtensions = req.headers['sec-websocket-extensions'];\n    const extensions = {};\n\n    if (\n      this.options.perMessageDeflate &&\n      secWebSocketExtensions !== undefined\n    ) {\n      const perMessageDeflate = new PerMessageDeflate({\n        ...this.options.perMessageDeflate,\n        isServer: true,\n        maxPayload: this.options.maxPayload\n      });\n\n      try {\n        const offers = extension.parse(secWebSocketExtensions);\n\n        if (offers[PerMessageDeflate.extensionName]) {\n          perMessageDeflate.accept(offers[PerMessageDeflate.extensionName]);\n          extensions[PerMessageDeflate.extensionName] = perMessageDeflate;\n        }\n      } catch (err) {\n        const message =\n          'Invalid or unacceptable Sec-WebSocket-Extensions header';\n        abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);\n        return;\n      }\n    }\n\n    //\n    // Optionally call external client verification handler.\n    //\n    if (this.options.verifyClient) {\n      const info = {\n        origin:\n          req.headers[`${version === 8 ? 'sec-websocket-origin' : 'origin'}`],\n        secure: !!(req.socket.authorized || req.socket.encrypted),\n        req\n      };\n\n      if (this.options.verifyClient.length === 2) {\n        this.options.verifyClient(info, (verified, code, message, headers) => {\n          if (!verified) {\n            return abortHandshake(socket, code || 401, message, headers);\n          }\n\n          this.completeUpgrade(\n            extensions,\n            key,\n            protocols,\n            req,\n            socket,\n            head,\n            cb\n          );\n        });\n        return;\n      }\n\n      if (!this.options.verifyClient(info)) return abortHandshake(socket, 401);\n    }\n\n    this.completeUpgrade(extensions, key, protocols, req, socket, head, cb);\n  }\n\n  /**\n   * Upgrade the connection to WebSocket.\n   *\n   * @param {Object} extensions The accepted extensions\n   * @param {String} key The value of the `Sec-WebSocket-Key` header\n   * @param {Set} protocols The subprotocols\n   * @param {http.IncomingMessage} req The request object\n   * @param {Duplex} socket The network socket between the server and client\n   * @param {Buffer} head The first packet of the upgraded stream\n   * @param {Function} cb Callback\n   * @throws {Error} If called more than once with the same socket\n   * @private\n   */\n  completeUpgrade(extensions, key, protocols, req, socket, head, cb) {\n    //\n    // Destroy the socket if the client has already sent a FIN packet.\n    //\n    if (!socket.readable || !socket.writable) return socket.destroy();\n\n    if (socket[kWebSocket]) {\n      throw new Error(\n        'server.handleUpgrade() was called more than once with the same ' +\n          'socket, possibly due to a misconfiguration'\n      );\n    }\n\n    if (this._state > RUNNING) return abortHandshake(socket, 503);\n\n    const digest = createHash('sha1')\n      .update(key + GUID)\n      .digest('base64');\n\n    const headers = [\n      'HTTP/1.1 101 Switching Protocols',\n      'Upgrade: websocket',\n      'Connection: Upgrade',\n      `Sec-WebSocket-Accept: ${digest}`\n    ];\n\n    const ws = new this.options.WebSocket(null, undefined, this.options);\n\n    if (protocols.size) {\n      //\n      // Optionally call external protocol selection handler.\n      //\n      const protocol = this.options.handleProtocols\n        ? this.options.handleProtocols(protocols, req)\n        : protocols.values().next().value;\n\n      if (protocol) {\n        headers.push(`Sec-WebSocket-Protocol: ${protocol}`);\n        ws._protocol = protocol;\n      }\n    }\n\n    if (extensions[PerMessageDeflate.extensionName]) {\n      const params = extensions[PerMessageDeflate.extensionName].params;\n      const value = extension.format({\n        [PerMessageDeflate.extensionName]: [params]\n      });\n      headers.push(`Sec-WebSocket-Extensions: ${value}`);\n      ws._extensions = extensions;\n    }\n\n    //\n    // Allow external modification/inspection of handshake headers.\n    //\n    this.emit('headers', headers, req);\n\n    socket.write(headers.concat('\\r\\n').join('\\r\\n'));\n    socket.removeListener('error', socketOnError);\n\n    ws.setSocket(socket, head, {\n      allowSynchronousEvents: this.options.allowSynchronousEvents,\n      maxPayload: this.options.maxPayload,\n      skipUTF8Validation: this.options.skipUTF8Validation\n    });\n\n    if (this.clients) {\n      this.clients.add(ws);\n      ws.on('close', () => {\n        this.clients.delete(ws);\n\n        if (this._shouldEmitClose && !this.clients.size) {\n          process.nextTick(emitClose, this);\n        }\n      });\n    }\n\n    cb(ws, req);\n  }\n}\n\nmodule.exports = WebSocketServer;\n\n/**\n * Add event listeners on an `EventEmitter` using a map of <event, listener>\n * pairs.\n *\n * @param {EventEmitter} server The event emitter\n * @param {Object.<String, Function>} map The listeners to add\n * @return {Function} A function that will remove the added listeners when\n *     called\n * @private\n */\nfunction addListeners(server, map) {\n  for (const event of Object.keys(map)) server.on(event, map[event]);\n\n  return function removeListeners() {\n    for (const event of Object.keys(map)) {\n      server.removeListener(event, map[event]);\n    }\n  };\n}\n\n/**\n * Emit a `'close'` event on an `EventEmitter`.\n *\n * @param {EventEmitter} server The event emitter\n * @private\n */\nfunction emitClose(server) {\n  server._state = CLOSED;\n  server.emit('close');\n}\n\n/**\n * Handle socket errors.\n *\n * @private\n */\nfunction socketOnError() {\n  this.destroy();\n}\n\n/**\n * Close the connection when preconditions are not fulfilled.\n *\n * @param {Duplex} socket The socket of the upgrade request\n * @param {Number} code The HTTP response status code\n * @param {String} [message] The HTTP response body\n * @param {Object} [headers] Additional HTTP response headers\n * @private\n */\nfunction abortHandshake(socket, code, message, headers) {\n  //\n  // The socket is writable unless the user destroyed or ended it before calling\n  // `server.handleUpgrade()` or in the `verifyClient` function, which is a user\n  // error. Handling this does not make much sense as the worst that can happen\n  // is that some of the data written by the user might be discarded due to the\n  // call to `socket.end()` below, which triggers an `'error'` event that in\n  // turn causes the socket to be destroyed.\n  //\n  message = message || http.STATUS_CODES[code];\n  headers = {\n    Connection: 'close',\n    'Content-Type': 'text/html',\n    'Content-Length': Buffer.byteLength(message),\n    ...headers\n  };\n\n  socket.once('finish', socket.destroy);\n\n  socket.end(\n    `HTTP/1.1 ${code} ${http.STATUS_CODES[code]}\\r\\n` +\n      Object.keys(headers)\n        .map((h) => `${h}: ${headers[h]}`)\n        .join('\\r\\n') +\n      '\\r\\n\\r\\n' +\n      message\n  );\n}\n\n/**\n * Emit a `'wsClientError'` event on a `WebSocketServer` if there is at least\n * one listener for it, otherwise call `abortHandshake()`.\n *\n * @param {WebSocketServer} server The WebSocket server\n * @param {http.IncomingMessage} req The request object\n * @param {Duplex} socket The socket of the upgrade request\n * @param {Number} code The HTTP response status code\n * @param {String} message The HTTP response body\n * @param {Object} [headers] The HTTP response headers\n * @private\n */\nfunction abortHandshakeOrEmitwsClientError(\n  server,\n  req,\n  socket,\n  code,\n  message,\n  headers\n) {\n  if (server.listenerCount('wsClientError')) {\n    const err = new Error(message);\n    Error.captureStackTrace(err, abortHandshakeOrEmitwsClientError);\n\n    server.emit('wsClientError', err, socket, req);\n  } else {\n    abortHandshake(socket, code, message, headers);\n  }\n}\n"
  },
  {
    "path": "lib/websocket.js",
    "content": "/* eslint no-unused-vars: [\"error\", { \"varsIgnorePattern\": \"^Duplex|Readable$\", \"caughtErrors\": \"none\" }] */\n\n'use strict';\n\nconst EventEmitter = require('events');\nconst https = require('https');\nconst http = require('http');\nconst net = require('net');\nconst tls = require('tls');\nconst { randomBytes, createHash } = require('crypto');\nconst { Duplex, Readable } = require('stream');\nconst { URL } = require('url');\n\nconst PerMessageDeflate = require('./permessage-deflate');\nconst Receiver = require('./receiver');\nconst Sender = require('./sender');\nconst { isBlob } = require('./validation');\n\nconst {\n  BINARY_TYPES,\n  CLOSE_TIMEOUT,\n  EMPTY_BUFFER,\n  GUID,\n  kForOnEventAttribute,\n  kListener,\n  kStatusCode,\n  kWebSocket,\n  NOOP\n} = require('./constants');\nconst {\n  EventTarget: { addEventListener, removeEventListener }\n} = require('./event-target');\nconst { format, parse } = require('./extension');\nconst { toBuffer } = require('./buffer-util');\n\nconst kAborted = Symbol('kAborted');\nconst protocolVersions = [8, 13];\nconst readyStates = ['CONNECTING', 'OPEN', 'CLOSING', 'CLOSED'];\nconst subprotocolRegex = /^[!#$%&'*+\\-.0-9A-Z^_`|a-z~]+$/;\n\n/**\n * Class representing a WebSocket.\n *\n * @extends EventEmitter\n */\nclass WebSocket extends EventEmitter {\n  /**\n   * Create a new `WebSocket`.\n   *\n   * @param {(String|URL)} address The URL to which to connect\n   * @param {(String|String[])} [protocols] The subprotocols\n   * @param {Object} [options] Connection options\n   */\n  constructor(address, protocols, options) {\n    super();\n\n    this._binaryType = BINARY_TYPES[0];\n    this._closeCode = 1006;\n    this._closeFrameReceived = false;\n    this._closeFrameSent = false;\n    this._closeMessage = EMPTY_BUFFER;\n    this._closeTimer = null;\n    this._errorEmitted = false;\n    this._extensions = {};\n    this._paused = false;\n    this._protocol = '';\n    this._readyState = WebSocket.CONNECTING;\n    this._receiver = null;\n    this._sender = null;\n    this._socket = null;\n\n    if (address !== null) {\n      this._bufferedAmount = 0;\n      this._isServer = false;\n      this._redirects = 0;\n\n      if (protocols === undefined) {\n        protocols = [];\n      } else if (!Array.isArray(protocols)) {\n        if (typeof protocols === 'object' && protocols !== null) {\n          options = protocols;\n          protocols = [];\n        } else {\n          protocols = [protocols];\n        }\n      }\n\n      initAsClient(this, address, protocols, options);\n    } else {\n      this._autoPong = options.autoPong;\n      this._closeTimeout = options.closeTimeout;\n      this._isServer = true;\n    }\n  }\n\n  /**\n   * For historical reasons, the custom \"nodebuffer\" type is used by the default\n   * instead of \"blob\".\n   *\n   * @type {String}\n   */\n  get binaryType() {\n    return this._binaryType;\n  }\n\n  set binaryType(type) {\n    if (!BINARY_TYPES.includes(type)) return;\n\n    this._binaryType = type;\n\n    //\n    // Allow to change `binaryType` on the fly.\n    //\n    if (this._receiver) this._receiver._binaryType = type;\n  }\n\n  /**\n   * @type {Number}\n   */\n  get bufferedAmount() {\n    if (!this._socket) return this._bufferedAmount;\n\n    return this._socket._writableState.length + this._sender._bufferedBytes;\n  }\n\n  /**\n   * @type {String}\n   */\n  get extensions() {\n    return Object.keys(this._extensions).join();\n  }\n\n  /**\n   * @type {Boolean}\n   */\n  get isPaused() {\n    return this._paused;\n  }\n\n  /**\n   * @type {Function}\n   */\n  /* istanbul ignore next */\n  get onclose() {\n    return null;\n  }\n\n  /**\n   * @type {Function}\n   */\n  /* istanbul ignore next */\n  get onerror() {\n    return null;\n  }\n\n  /**\n   * @type {Function}\n   */\n  /* istanbul ignore next */\n  get onopen() {\n    return null;\n  }\n\n  /**\n   * @type {Function}\n   */\n  /* istanbul ignore next */\n  get onmessage() {\n    return null;\n  }\n\n  /**\n   * @type {String}\n   */\n  get protocol() {\n    return this._protocol;\n  }\n\n  /**\n   * @type {Number}\n   */\n  get readyState() {\n    return this._readyState;\n  }\n\n  /**\n   * @type {String}\n   */\n  get url() {\n    return this._url;\n  }\n\n  /**\n   * Set up the socket and the internal resources.\n   *\n   * @param {Duplex} socket The network socket between the server and client\n   * @param {Buffer} head The first packet of the upgraded stream\n   * @param {Object} options Options object\n   * @param {Boolean} [options.allowSynchronousEvents=false] Specifies whether\n   *     any of the `'message'`, `'ping'`, and `'pong'` events can be emitted\n   *     multiple times in the same tick\n   * @param {Function} [options.generateMask] The function used to generate the\n   *     masking key\n   * @param {Number} [options.maxPayload=0] The maximum allowed message size\n   * @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or\n   *     not to skip UTF-8 validation for text and close messages\n   * @private\n   */\n  setSocket(socket, head, options) {\n    const receiver = new Receiver({\n      allowSynchronousEvents: options.allowSynchronousEvents,\n      binaryType: this.binaryType,\n      extensions: this._extensions,\n      isServer: this._isServer,\n      maxPayload: options.maxPayload,\n      skipUTF8Validation: options.skipUTF8Validation\n    });\n\n    const sender = new Sender(socket, this._extensions, options.generateMask);\n\n    this._receiver = receiver;\n    this._sender = sender;\n    this._socket = socket;\n\n    receiver[kWebSocket] = this;\n    sender[kWebSocket] = this;\n    socket[kWebSocket] = this;\n\n    receiver.on('conclude', receiverOnConclude);\n    receiver.on('drain', receiverOnDrain);\n    receiver.on('error', receiverOnError);\n    receiver.on('message', receiverOnMessage);\n    receiver.on('ping', receiverOnPing);\n    receiver.on('pong', receiverOnPong);\n\n    sender.onerror = senderOnError;\n\n    //\n    // These methods may not be available if `socket` is just a `Duplex`.\n    //\n    if (socket.setTimeout) socket.setTimeout(0);\n    if (socket.setNoDelay) socket.setNoDelay();\n\n    if (head.length > 0) socket.unshift(head);\n\n    socket.on('close', socketOnClose);\n    socket.on('data', socketOnData);\n    socket.on('end', socketOnEnd);\n    socket.on('error', socketOnError);\n\n    this._readyState = WebSocket.OPEN;\n    this.emit('open');\n  }\n\n  /**\n   * Emit the `'close'` event.\n   *\n   * @private\n   */\n  emitClose() {\n    if (!this._socket) {\n      this._readyState = WebSocket.CLOSED;\n      this.emit('close', this._closeCode, this._closeMessage);\n      return;\n    }\n\n    if (this._extensions[PerMessageDeflate.extensionName]) {\n      this._extensions[PerMessageDeflate.extensionName].cleanup();\n    }\n\n    this._receiver.removeAllListeners();\n    this._readyState = WebSocket.CLOSED;\n    this.emit('close', this._closeCode, this._closeMessage);\n  }\n\n  /**\n   * Start a closing handshake.\n   *\n   *          +----------+   +-----------+   +----------+\n   *     - - -|ws.close()|-->|close frame|-->|ws.close()|- - -\n   *    |     +----------+   +-----------+   +----------+     |\n   *          +----------+   +-----------+         |\n   * CLOSING  |ws.close()|<--|close frame|<--+-----+       CLOSING\n   *          +----------+   +-----------+   |\n   *    |           |                        |   +---+        |\n   *                +------------------------+-->|fin| - - - -\n   *    |         +---+                      |   +---+\n   *     - - - - -|fin|<---------------------+\n   *              +---+\n   *\n   * @param {Number} [code] Status code explaining why the connection is closing\n   * @param {(String|Buffer)} [data] The reason why the connection is\n   *     closing\n   * @public\n   */\n  close(code, data) {\n    if (this.readyState === WebSocket.CLOSED) return;\n    if (this.readyState === WebSocket.CONNECTING) {\n      const msg = 'WebSocket was closed before the connection was established';\n      abortHandshake(this, this._req, msg);\n      return;\n    }\n\n    if (this.readyState === WebSocket.CLOSING) {\n      if (\n        this._closeFrameSent &&\n        (this._closeFrameReceived || this._receiver._writableState.errorEmitted)\n      ) {\n        this._socket.end();\n      }\n\n      return;\n    }\n\n    this._readyState = WebSocket.CLOSING;\n    this._sender.close(code, data, !this._isServer, (err) => {\n      //\n      // This error is handled by the `'error'` listener on the socket. We only\n      // want to know if the close frame has been sent here.\n      //\n      if (err) return;\n\n      this._closeFrameSent = true;\n\n      if (\n        this._closeFrameReceived ||\n        this._receiver._writableState.errorEmitted\n      ) {\n        this._socket.end();\n      }\n    });\n\n    setCloseTimer(this);\n  }\n\n  /**\n   * Pause the socket.\n   *\n   * @public\n   */\n  pause() {\n    if (\n      this.readyState === WebSocket.CONNECTING ||\n      this.readyState === WebSocket.CLOSED\n    ) {\n      return;\n    }\n\n    this._paused = true;\n    this._socket.pause();\n  }\n\n  /**\n   * Send a ping.\n   *\n   * @param {*} [data] The data to send\n   * @param {Boolean} [mask] Indicates whether or not to mask `data`\n   * @param {Function} [cb] Callback which is executed when the ping is sent\n   * @public\n   */\n  ping(data, mask, cb) {\n    if (this.readyState === WebSocket.CONNECTING) {\n      throw new Error('WebSocket is not open: readyState 0 (CONNECTING)');\n    }\n\n    if (typeof data === 'function') {\n      cb = data;\n      data = mask = undefined;\n    } else if (typeof mask === 'function') {\n      cb = mask;\n      mask = undefined;\n    }\n\n    if (typeof data === 'number') data = data.toString();\n\n    if (this.readyState !== WebSocket.OPEN) {\n      sendAfterClose(this, data, cb);\n      return;\n    }\n\n    if (mask === undefined) mask = !this._isServer;\n    this._sender.ping(data || EMPTY_BUFFER, mask, cb);\n  }\n\n  /**\n   * Send a pong.\n   *\n   * @param {*} [data] The data to send\n   * @param {Boolean} [mask] Indicates whether or not to mask `data`\n   * @param {Function} [cb] Callback which is executed when the pong is sent\n   * @public\n   */\n  pong(data, mask, cb) {\n    if (this.readyState === WebSocket.CONNECTING) {\n      throw new Error('WebSocket is not open: readyState 0 (CONNECTING)');\n    }\n\n    if (typeof data === 'function') {\n      cb = data;\n      data = mask = undefined;\n    } else if (typeof mask === 'function') {\n      cb = mask;\n      mask = undefined;\n    }\n\n    if (typeof data === 'number') data = data.toString();\n\n    if (this.readyState !== WebSocket.OPEN) {\n      sendAfterClose(this, data, cb);\n      return;\n    }\n\n    if (mask === undefined) mask = !this._isServer;\n    this._sender.pong(data || EMPTY_BUFFER, mask, cb);\n  }\n\n  /**\n   * Resume the socket.\n   *\n   * @public\n   */\n  resume() {\n    if (\n      this.readyState === WebSocket.CONNECTING ||\n      this.readyState === WebSocket.CLOSED\n    ) {\n      return;\n    }\n\n    this._paused = false;\n    if (!this._receiver._writableState.needDrain) this._socket.resume();\n  }\n\n  /**\n   * Send a data message.\n   *\n   * @param {*} data The message to send\n   * @param {Object} [options] Options object\n   * @param {Boolean} [options.binary] Specifies whether `data` is binary or\n   *     text\n   * @param {Boolean} [options.compress] Specifies whether or not to compress\n   *     `data`\n   * @param {Boolean} [options.fin=true] Specifies whether the fragment is the\n   *     last one\n   * @param {Boolean} [options.mask] Specifies whether or not to mask `data`\n   * @param {Function} [cb] Callback which is executed when data is written out\n   * @public\n   */\n  send(data, options, cb) {\n    if (this.readyState === WebSocket.CONNECTING) {\n      throw new Error('WebSocket is not open: readyState 0 (CONNECTING)');\n    }\n\n    if (typeof options === 'function') {\n      cb = options;\n      options = {};\n    }\n\n    if (typeof data === 'number') data = data.toString();\n\n    if (this.readyState !== WebSocket.OPEN) {\n      sendAfterClose(this, data, cb);\n      return;\n    }\n\n    const opts = {\n      binary: typeof data !== 'string',\n      mask: !this._isServer,\n      compress: true,\n      fin: true,\n      ...options\n    };\n\n    if (!this._extensions[PerMessageDeflate.extensionName]) {\n      opts.compress = false;\n    }\n\n    this._sender.send(data || EMPTY_BUFFER, opts, cb);\n  }\n\n  /**\n   * Forcibly close the connection.\n   *\n   * @public\n   */\n  terminate() {\n    if (this.readyState === WebSocket.CLOSED) return;\n    if (this.readyState === WebSocket.CONNECTING) {\n      const msg = 'WebSocket was closed before the connection was established';\n      abortHandshake(this, this._req, msg);\n      return;\n    }\n\n    if (this._socket) {\n      this._readyState = WebSocket.CLOSING;\n      this._socket.destroy();\n    }\n  }\n}\n\n/**\n * @constant {Number} CONNECTING\n * @memberof WebSocket\n */\nObject.defineProperty(WebSocket, 'CONNECTING', {\n  enumerable: true,\n  value: readyStates.indexOf('CONNECTING')\n});\n\n/**\n * @constant {Number} CONNECTING\n * @memberof WebSocket.prototype\n */\nObject.defineProperty(WebSocket.prototype, 'CONNECTING', {\n  enumerable: true,\n  value: readyStates.indexOf('CONNECTING')\n});\n\n/**\n * @constant {Number} OPEN\n * @memberof WebSocket\n */\nObject.defineProperty(WebSocket, 'OPEN', {\n  enumerable: true,\n  value: readyStates.indexOf('OPEN')\n});\n\n/**\n * @constant {Number} OPEN\n * @memberof WebSocket.prototype\n */\nObject.defineProperty(WebSocket.prototype, 'OPEN', {\n  enumerable: true,\n  value: readyStates.indexOf('OPEN')\n});\n\n/**\n * @constant {Number} CLOSING\n * @memberof WebSocket\n */\nObject.defineProperty(WebSocket, 'CLOSING', {\n  enumerable: true,\n  value: readyStates.indexOf('CLOSING')\n});\n\n/**\n * @constant {Number} CLOSING\n * @memberof WebSocket.prototype\n */\nObject.defineProperty(WebSocket.prototype, 'CLOSING', {\n  enumerable: true,\n  value: readyStates.indexOf('CLOSING')\n});\n\n/**\n * @constant {Number} CLOSED\n * @memberof WebSocket\n */\nObject.defineProperty(WebSocket, 'CLOSED', {\n  enumerable: true,\n  value: readyStates.indexOf('CLOSED')\n});\n\n/**\n * @constant {Number} CLOSED\n * @memberof WebSocket.prototype\n */\nObject.defineProperty(WebSocket.prototype, 'CLOSED', {\n  enumerable: true,\n  value: readyStates.indexOf('CLOSED')\n});\n\n[\n  'binaryType',\n  'bufferedAmount',\n  'extensions',\n  'isPaused',\n  'protocol',\n  'readyState',\n  'url'\n].forEach((property) => {\n  Object.defineProperty(WebSocket.prototype, property, { enumerable: true });\n});\n\n//\n// Add the `onopen`, `onerror`, `onclose`, and `onmessage` attributes.\n// See https://html.spec.whatwg.org/multipage/comms.html#the-websocket-interface\n//\n['open', 'error', 'close', 'message'].forEach((method) => {\n  Object.defineProperty(WebSocket.prototype, `on${method}`, {\n    enumerable: true,\n    get() {\n      for (const listener of this.listeners(method)) {\n        if (listener[kForOnEventAttribute]) return listener[kListener];\n      }\n\n      return null;\n    },\n    set(handler) {\n      for (const listener of this.listeners(method)) {\n        if (listener[kForOnEventAttribute]) {\n          this.removeListener(method, listener);\n          break;\n        }\n      }\n\n      if (typeof handler !== 'function') return;\n\n      this.addEventListener(method, handler, {\n        [kForOnEventAttribute]: true\n      });\n    }\n  });\n});\n\nWebSocket.prototype.addEventListener = addEventListener;\nWebSocket.prototype.removeEventListener = removeEventListener;\n\nmodule.exports = WebSocket;\n\n/**\n * Initialize a WebSocket client.\n *\n * @param {WebSocket} websocket The client to initialize\n * @param {(String|URL)} address The URL to which to connect\n * @param {Array} protocols The subprotocols\n * @param {Object} [options] Connection options\n * @param {Boolean} [options.allowSynchronousEvents=true] Specifies whether any\n *     of the `'message'`, `'ping'`, and `'pong'` events can be emitted multiple\n *     times in the same tick\n * @param {Boolean} [options.autoPong=true] Specifies whether or not to\n *     automatically send a pong in response to a ping\n * @param {Number} [options.closeTimeout=30000] Duration in milliseconds to wait\n *     for the closing handshake to finish after `websocket.close()` is called\n * @param {Function} [options.finishRequest] A function which can be used to\n *     customize the headers of each http request before it is sent\n * @param {Boolean} [options.followRedirects=false] Whether or not to follow\n *     redirects\n * @param {Function} [options.generateMask] The function used to generate the\n *     masking key\n * @param {Number} [options.handshakeTimeout] Timeout in milliseconds for the\n *     handshake request\n * @param {Number} [options.maxPayload=104857600] The maximum allowed message\n *     size\n * @param {Number} [options.maxRedirects=10] The maximum number of redirects\n *     allowed\n * @param {String} [options.origin] Value of the `Origin` or\n *     `Sec-WebSocket-Origin` header\n * @param {(Boolean|Object)} [options.perMessageDeflate=true] Enable/disable\n *     permessage-deflate\n * @param {Number} [options.protocolVersion=13] Value of the\n *     `Sec-WebSocket-Version` header\n * @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or\n *     not to skip UTF-8 validation for text and close messages\n * @private\n */\nfunction initAsClient(websocket, address, protocols, options) {\n  const opts = {\n    allowSynchronousEvents: true,\n    autoPong: true,\n    closeTimeout: CLOSE_TIMEOUT,\n    protocolVersion: protocolVersions[1],\n    maxPayload: 100 * 1024 * 1024,\n    skipUTF8Validation: false,\n    perMessageDeflate: true,\n    followRedirects: false,\n    maxRedirects: 10,\n    ...options,\n    socketPath: undefined,\n    hostname: undefined,\n    protocol: undefined,\n    timeout: undefined,\n    method: 'GET',\n    host: undefined,\n    path: undefined,\n    port: undefined\n  };\n\n  websocket._autoPong = opts.autoPong;\n  websocket._closeTimeout = opts.closeTimeout;\n\n  if (!protocolVersions.includes(opts.protocolVersion)) {\n    throw new RangeError(\n      `Unsupported protocol version: ${opts.protocolVersion} ` +\n        `(supported versions: ${protocolVersions.join(', ')})`\n    );\n  }\n\n  let parsedUrl;\n\n  if (address instanceof URL) {\n    parsedUrl = address;\n  } else {\n    try {\n      parsedUrl = new URL(address);\n    } catch {\n      throw new SyntaxError(`Invalid URL: ${address}`);\n    }\n  }\n\n  if (parsedUrl.protocol === 'http:') {\n    parsedUrl.protocol = 'ws:';\n  } else if (parsedUrl.protocol === 'https:') {\n    parsedUrl.protocol = 'wss:';\n  }\n\n  websocket._url = parsedUrl.href;\n\n  const isSecure = parsedUrl.protocol === 'wss:';\n  const isIpcUrl = parsedUrl.protocol === 'ws+unix:';\n  let invalidUrlMessage;\n\n  if (parsedUrl.protocol !== 'ws:' && !isSecure && !isIpcUrl) {\n    invalidUrlMessage =\n      'The URL\\'s protocol must be one of \"ws:\", \"wss:\", ' +\n      '\"http:\", \"https:\", or \"ws+unix:\"';\n  } else if (isIpcUrl && !parsedUrl.pathname) {\n    invalidUrlMessage = \"The URL's pathname is empty\";\n  } else if (parsedUrl.hash) {\n    invalidUrlMessage = 'The URL contains a fragment identifier';\n  }\n\n  if (invalidUrlMessage) {\n    const err = new SyntaxError(invalidUrlMessage);\n\n    if (websocket._redirects === 0) {\n      throw err;\n    } else {\n      emitErrorAndClose(websocket, err);\n      return;\n    }\n  }\n\n  const defaultPort = isSecure ? 443 : 80;\n  const key = randomBytes(16).toString('base64');\n  const request = isSecure ? https.request : http.request;\n  const protocolSet = new Set();\n  let perMessageDeflate;\n\n  opts.createConnection =\n    opts.createConnection || (isSecure ? tlsConnect : netConnect);\n  opts.defaultPort = opts.defaultPort || defaultPort;\n  opts.port = parsedUrl.port || defaultPort;\n  opts.host = parsedUrl.hostname.startsWith('[')\n    ? parsedUrl.hostname.slice(1, -1)\n    : parsedUrl.hostname;\n  opts.headers = {\n    ...opts.headers,\n    'Sec-WebSocket-Version': opts.protocolVersion,\n    'Sec-WebSocket-Key': key,\n    Connection: 'Upgrade',\n    Upgrade: 'websocket'\n  };\n  opts.path = parsedUrl.pathname + parsedUrl.search;\n  opts.timeout = opts.handshakeTimeout;\n\n  if (opts.perMessageDeflate) {\n    perMessageDeflate = new PerMessageDeflate({\n      ...opts.perMessageDeflate,\n      isServer: false,\n      maxPayload: opts.maxPayload\n    });\n    opts.headers['Sec-WebSocket-Extensions'] = format({\n      [PerMessageDeflate.extensionName]: perMessageDeflate.offer()\n    });\n  }\n  if (protocols.length) {\n    for (const protocol of protocols) {\n      if (\n        typeof protocol !== 'string' ||\n        !subprotocolRegex.test(protocol) ||\n        protocolSet.has(protocol)\n      ) {\n        throw new SyntaxError(\n          'An invalid or duplicated subprotocol was specified'\n        );\n      }\n\n      protocolSet.add(protocol);\n    }\n\n    opts.headers['Sec-WebSocket-Protocol'] = protocols.join(',');\n  }\n  if (opts.origin) {\n    if (opts.protocolVersion < 13) {\n      opts.headers['Sec-WebSocket-Origin'] = opts.origin;\n    } else {\n      opts.headers.Origin = opts.origin;\n    }\n  }\n  if (parsedUrl.username || parsedUrl.password) {\n    opts.auth = `${parsedUrl.username}:${parsedUrl.password}`;\n  }\n\n  if (isIpcUrl) {\n    const parts = opts.path.split(':');\n\n    opts.socketPath = parts[0];\n    opts.path = parts[1];\n  }\n\n  let req;\n\n  if (opts.followRedirects) {\n    if (websocket._redirects === 0) {\n      websocket._originalIpc = isIpcUrl;\n      websocket._originalSecure = isSecure;\n      websocket._originalHostOrSocketPath = isIpcUrl\n        ? opts.socketPath\n        : parsedUrl.host;\n\n      const headers = options && options.headers;\n\n      //\n      // Shallow copy the user provided options so that headers can be changed\n      // without mutating the original object.\n      //\n      options = { ...options, headers: {} };\n\n      if (headers) {\n        for (const [key, value] of Object.entries(headers)) {\n          options.headers[key.toLowerCase()] = value;\n        }\n      }\n    } else if (websocket.listenerCount('redirect') === 0) {\n      const isSameHost = isIpcUrl\n        ? websocket._originalIpc\n          ? opts.socketPath === websocket._originalHostOrSocketPath\n          : false\n        : websocket._originalIpc\n          ? false\n          : parsedUrl.host === websocket._originalHostOrSocketPath;\n\n      if (!isSameHost || (websocket._originalSecure && !isSecure)) {\n        //\n        // Match curl 7.77.0 behavior and drop the following headers. These\n        // headers are also dropped when following a redirect to a subdomain.\n        //\n        delete opts.headers.authorization;\n        delete opts.headers.cookie;\n\n        if (!isSameHost) delete opts.headers.host;\n\n        opts.auth = undefined;\n      }\n    }\n\n    //\n    // Match curl 7.77.0 behavior and make the first `Authorization` header win.\n    // If the `Authorization` header is set, then there is nothing to do as it\n    // will take precedence.\n    //\n    if (opts.auth && !options.headers.authorization) {\n      options.headers.authorization =\n        'Basic ' + Buffer.from(opts.auth).toString('base64');\n    }\n\n    req = websocket._req = request(opts);\n\n    if (websocket._redirects) {\n      //\n      // Unlike what is done for the `'upgrade'` event, no early exit is\n      // triggered here if the user calls `websocket.close()` or\n      // `websocket.terminate()` from a listener of the `'redirect'` event. This\n      // is because the user can also call `request.destroy()` with an error\n      // before calling `websocket.close()` or `websocket.terminate()` and this\n      // would result in an error being emitted on the `request` object with no\n      // `'error'` event listeners attached.\n      //\n      websocket.emit('redirect', websocket.url, req);\n    }\n  } else {\n    req = websocket._req = request(opts);\n  }\n\n  if (opts.timeout) {\n    req.on('timeout', () => {\n      abortHandshake(websocket, req, 'Opening handshake has timed out');\n    });\n  }\n\n  req.on('error', (err) => {\n    if (req === null || req[kAborted]) return;\n\n    req = websocket._req = null;\n    emitErrorAndClose(websocket, err);\n  });\n\n  req.on('response', (res) => {\n    const location = res.headers.location;\n    const statusCode = res.statusCode;\n\n    if (\n      location &&\n      opts.followRedirects &&\n      statusCode >= 300 &&\n      statusCode < 400\n    ) {\n      if (++websocket._redirects > opts.maxRedirects) {\n        abortHandshake(websocket, req, 'Maximum redirects exceeded');\n        return;\n      }\n\n      req.abort();\n\n      let addr;\n\n      try {\n        addr = new URL(location, address);\n      } catch (e) {\n        const err = new SyntaxError(`Invalid URL: ${location}`);\n        emitErrorAndClose(websocket, err);\n        return;\n      }\n\n      initAsClient(websocket, addr, protocols, options);\n    } else if (!websocket.emit('unexpected-response', req, res)) {\n      abortHandshake(\n        websocket,\n        req,\n        `Unexpected server response: ${res.statusCode}`\n      );\n    }\n  });\n\n  req.on('upgrade', (res, socket, head) => {\n    websocket.emit('upgrade', res);\n\n    //\n    // The user may have closed the connection from a listener of the\n    // `'upgrade'` event.\n    //\n    if (websocket.readyState !== WebSocket.CONNECTING) return;\n\n    req = websocket._req = null;\n\n    const upgrade = res.headers.upgrade;\n\n    if (upgrade === undefined || upgrade.toLowerCase() !== 'websocket') {\n      abortHandshake(websocket, socket, 'Invalid Upgrade header');\n      return;\n    }\n\n    const digest = createHash('sha1')\n      .update(key + GUID)\n      .digest('base64');\n\n    if (res.headers['sec-websocket-accept'] !== digest) {\n      abortHandshake(websocket, socket, 'Invalid Sec-WebSocket-Accept header');\n      return;\n    }\n\n    const serverProt = res.headers['sec-websocket-protocol'];\n    let protError;\n\n    if (serverProt !== undefined) {\n      if (!protocolSet.size) {\n        protError = 'Server sent a subprotocol but none was requested';\n      } else if (!protocolSet.has(serverProt)) {\n        protError = 'Server sent an invalid subprotocol';\n      }\n    } else if (protocolSet.size) {\n      protError = 'Server sent no subprotocol';\n    }\n\n    if (protError) {\n      abortHandshake(websocket, socket, protError);\n      return;\n    }\n\n    if (serverProt) websocket._protocol = serverProt;\n\n    const secWebSocketExtensions = res.headers['sec-websocket-extensions'];\n\n    if (secWebSocketExtensions !== undefined) {\n      if (!perMessageDeflate) {\n        const message =\n          'Server sent a Sec-WebSocket-Extensions header but no extension ' +\n          'was requested';\n        abortHandshake(websocket, socket, message);\n        return;\n      }\n\n      let extensions;\n\n      try {\n        extensions = parse(secWebSocketExtensions);\n      } catch (err) {\n        const message = 'Invalid Sec-WebSocket-Extensions header';\n        abortHandshake(websocket, socket, message);\n        return;\n      }\n\n      const extensionNames = Object.keys(extensions);\n\n      if (\n        extensionNames.length !== 1 ||\n        extensionNames[0] !== PerMessageDeflate.extensionName\n      ) {\n        const message = 'Server indicated an extension that was not requested';\n        abortHandshake(websocket, socket, message);\n        return;\n      }\n\n      try {\n        perMessageDeflate.accept(extensions[PerMessageDeflate.extensionName]);\n      } catch (err) {\n        const message = 'Invalid Sec-WebSocket-Extensions header';\n        abortHandshake(websocket, socket, message);\n        return;\n      }\n\n      websocket._extensions[PerMessageDeflate.extensionName] =\n        perMessageDeflate;\n    }\n\n    websocket.setSocket(socket, head, {\n      allowSynchronousEvents: opts.allowSynchronousEvents,\n      generateMask: opts.generateMask,\n      maxPayload: opts.maxPayload,\n      skipUTF8Validation: opts.skipUTF8Validation\n    });\n  });\n\n  if (opts.finishRequest) {\n    opts.finishRequest(req, websocket);\n  } else {\n    req.end();\n  }\n}\n\n/**\n * Emit the `'error'` and `'close'` events.\n *\n * @param {WebSocket} websocket The WebSocket instance\n * @param {Error} The error to emit\n * @private\n */\nfunction emitErrorAndClose(websocket, err) {\n  websocket._readyState = WebSocket.CLOSING;\n  //\n  // The following assignment is practically useless and is done only for\n  // consistency.\n  //\n  websocket._errorEmitted = true;\n  websocket.emit('error', err);\n  websocket.emitClose();\n}\n\n/**\n * Create a `net.Socket` and initiate a connection.\n *\n * @param {Object} options Connection options\n * @return {net.Socket} The newly created socket used to start the connection\n * @private\n */\nfunction netConnect(options) {\n  options.path = options.socketPath;\n  return net.connect(options);\n}\n\n/**\n * Create a `tls.TLSSocket` and initiate a connection.\n *\n * @param {Object} options Connection options\n * @return {tls.TLSSocket} The newly created socket used to start the connection\n * @private\n */\nfunction tlsConnect(options) {\n  options.path = undefined;\n\n  if (!options.servername && options.servername !== '') {\n    options.servername = net.isIP(options.host) ? '' : options.host;\n  }\n\n  return tls.connect(options);\n}\n\n/**\n * Abort the handshake and emit an error.\n *\n * @param {WebSocket} websocket The WebSocket instance\n * @param {(http.ClientRequest|net.Socket|tls.Socket)} stream The request to\n *     abort or the socket to destroy\n * @param {String} message The error message\n * @private\n */\nfunction abortHandshake(websocket, stream, message) {\n  websocket._readyState = WebSocket.CLOSING;\n\n  const err = new Error(message);\n  Error.captureStackTrace(err, abortHandshake);\n\n  if (stream.setHeader) {\n    stream[kAborted] = true;\n    stream.abort();\n\n    if (stream.socket && !stream.socket.destroyed) {\n      //\n      // On Node.js >= 14.3.0 `request.abort()` does not destroy the socket if\n      // called after the request completed. See\n      // https://github.com/websockets/ws/issues/1869.\n      //\n      stream.socket.destroy();\n    }\n\n    process.nextTick(emitErrorAndClose, websocket, err);\n  } else {\n    stream.destroy(err);\n    stream.once('error', websocket.emit.bind(websocket, 'error'));\n    stream.once('close', websocket.emitClose.bind(websocket));\n  }\n}\n\n/**\n * Handle cases where the `ping()`, `pong()`, or `send()` methods are called\n * when the `readyState` attribute is `CLOSING` or `CLOSED`.\n *\n * @param {WebSocket} websocket The WebSocket instance\n * @param {*} [data] The data to send\n * @param {Function} [cb] Callback\n * @private\n */\nfunction sendAfterClose(websocket, data, cb) {\n  if (data) {\n    const length = isBlob(data) ? data.size : toBuffer(data).length;\n\n    //\n    // The `_bufferedAmount` property is used only when the peer is a client and\n    // the opening handshake fails. Under these circumstances, in fact, the\n    // `setSocket()` method is not called, so the `_socket` and `_sender`\n    // properties are set to `null`.\n    //\n    if (websocket._socket) websocket._sender._bufferedBytes += length;\n    else websocket._bufferedAmount += length;\n  }\n\n  if (cb) {\n    const err = new Error(\n      `WebSocket is not open: readyState ${websocket.readyState} ` +\n        `(${readyStates[websocket.readyState]})`\n    );\n    process.nextTick(cb, err);\n  }\n}\n\n/**\n * The listener of the `Receiver` `'conclude'` event.\n *\n * @param {Number} code The status code\n * @param {Buffer} reason The reason for closing\n * @private\n */\nfunction receiverOnConclude(code, reason) {\n  const websocket = this[kWebSocket];\n\n  websocket._closeFrameReceived = true;\n  websocket._closeMessage = reason;\n  websocket._closeCode = code;\n\n  if (websocket._socket[kWebSocket] === undefined) return;\n\n  websocket._socket.removeListener('data', socketOnData);\n  process.nextTick(resume, websocket._socket);\n\n  if (code === 1005) websocket.close();\n  else websocket.close(code, reason);\n}\n\n/**\n * The listener of the `Receiver` `'drain'` event.\n *\n * @private\n */\nfunction receiverOnDrain() {\n  const websocket = this[kWebSocket];\n\n  if (!websocket.isPaused) websocket._socket.resume();\n}\n\n/**\n * The listener of the `Receiver` `'error'` event.\n *\n * @param {(RangeError|Error)} err The emitted error\n * @private\n */\nfunction receiverOnError(err) {\n  const websocket = this[kWebSocket];\n\n  if (websocket._socket[kWebSocket] !== undefined) {\n    websocket._socket.removeListener('data', socketOnData);\n\n    //\n    // On Node.js < 14.0.0 the `'error'` event is emitted synchronously. See\n    // https://github.com/websockets/ws/issues/1940.\n    //\n    process.nextTick(resume, websocket._socket);\n\n    websocket.close(err[kStatusCode]);\n  }\n\n  if (!websocket._errorEmitted) {\n    websocket._errorEmitted = true;\n    websocket.emit('error', err);\n  }\n}\n\n/**\n * The listener of the `Receiver` `'finish'` event.\n *\n * @private\n */\nfunction receiverOnFinish() {\n  this[kWebSocket].emitClose();\n}\n\n/**\n * The listener of the `Receiver` `'message'` event.\n *\n * @param {Buffer|ArrayBuffer|Buffer[])} data The message\n * @param {Boolean} isBinary Specifies whether the message is binary or not\n * @private\n */\nfunction receiverOnMessage(data, isBinary) {\n  this[kWebSocket].emit('message', data, isBinary);\n}\n\n/**\n * The listener of the `Receiver` `'ping'` event.\n *\n * @param {Buffer} data The data included in the ping frame\n * @private\n */\nfunction receiverOnPing(data) {\n  const websocket = this[kWebSocket];\n\n  if (websocket._autoPong) websocket.pong(data, !this._isServer, NOOP);\n  websocket.emit('ping', data);\n}\n\n/**\n * The listener of the `Receiver` `'pong'` event.\n *\n * @param {Buffer} data The data included in the pong frame\n * @private\n */\nfunction receiverOnPong(data) {\n  this[kWebSocket].emit('pong', data);\n}\n\n/**\n * Resume a readable stream\n *\n * @param {Readable} stream The readable stream\n * @private\n */\nfunction resume(stream) {\n  stream.resume();\n}\n\n/**\n * The `Sender` error event handler.\n *\n * @param {Error} The error\n * @private\n */\nfunction senderOnError(err) {\n  const websocket = this[kWebSocket];\n\n  if (websocket.readyState === WebSocket.CLOSED) return;\n  if (websocket.readyState === WebSocket.OPEN) {\n    websocket._readyState = WebSocket.CLOSING;\n    setCloseTimer(websocket);\n  }\n\n  //\n  // `socket.end()` is used instead of `socket.destroy()` to allow the other\n  // peer to finish sending queued data. There is no need to set a timer here\n  // because `CLOSING` means that it is already set or not needed.\n  //\n  this._socket.end();\n\n  if (!websocket._errorEmitted) {\n    websocket._errorEmitted = true;\n    websocket.emit('error', err);\n  }\n}\n\n/**\n * Set a timer to destroy the underlying raw socket of a WebSocket.\n *\n * @param {WebSocket} websocket The WebSocket instance\n * @private\n */\nfunction setCloseTimer(websocket) {\n  websocket._closeTimer = setTimeout(\n    websocket._socket.destroy.bind(websocket._socket),\n    websocket._closeTimeout\n  );\n}\n\n/**\n * The listener of the socket `'close'` event.\n *\n * @private\n */\nfunction socketOnClose() {\n  const websocket = this[kWebSocket];\n\n  this.removeListener('close', socketOnClose);\n  this.removeListener('data', socketOnData);\n  this.removeListener('end', socketOnEnd);\n\n  websocket._readyState = WebSocket.CLOSING;\n\n  //\n  // The close frame might not have been received or the `'end'` event emitted,\n  // for example, if the socket was destroyed due to an error. Ensure that the\n  // `receiver` stream is closed after writing any remaining buffered data to\n  // it. If the readable side of the socket is in flowing mode then there is no\n  // buffered data as everything has been already written. If instead, the\n  // socket is paused, any possible buffered data will be read as a single\n  // chunk.\n  //\n  if (\n    !this._readableState.endEmitted &&\n    !websocket._closeFrameReceived &&\n    !websocket._receiver._writableState.errorEmitted &&\n    this._readableState.length !== 0\n  ) {\n    const chunk = this.read(this._readableState.length);\n\n    websocket._receiver.write(chunk);\n  }\n\n  websocket._receiver.end();\n\n  this[kWebSocket] = undefined;\n\n  clearTimeout(websocket._closeTimer);\n\n  if (\n    websocket._receiver._writableState.finished ||\n    websocket._receiver._writableState.errorEmitted\n  ) {\n    websocket.emitClose();\n  } else {\n    websocket._receiver.on('error', receiverOnFinish);\n    websocket._receiver.on('finish', receiverOnFinish);\n  }\n}\n\n/**\n * The listener of the socket `'data'` event.\n *\n * @param {Buffer} chunk A chunk of data\n * @private\n */\nfunction socketOnData(chunk) {\n  if (!this[kWebSocket]._receiver.write(chunk)) {\n    this.pause();\n  }\n}\n\n/**\n * The listener of the socket `'end'` event.\n *\n * @private\n */\nfunction socketOnEnd() {\n  const websocket = this[kWebSocket];\n\n  websocket._readyState = WebSocket.CLOSING;\n  websocket._receiver.end();\n  this.end();\n}\n\n/**\n * The listener of the socket `'error'` event.\n *\n * @private\n */\nfunction socketOnError() {\n  const websocket = this[kWebSocket];\n\n  this.removeListener('error', socketOnError);\n  this.on('error', NOOP);\n\n  if (websocket) {\n    websocket._readyState = WebSocket.CLOSING;\n    this.destroy();\n  }\n}\n"
  },
  {
    "path": "package.json",
    "content": "{\n  \"name\": \"ws\",\n  \"version\": \"8.19.0\",\n  \"description\": \"Simple to use, blazing fast and thoroughly tested websocket client and server for Node.js\",\n  \"keywords\": [\n    \"HyBi\",\n    \"Push\",\n    \"RFC-6455\",\n    \"WebSocket\",\n    \"WebSockets\",\n    \"real-time\"\n  ],\n  \"homepage\": \"https://github.com/websockets/ws\",\n  \"bugs\": \"https://github.com/websockets/ws/issues\",\n  \"repository\": {\n    \"type\": \"git\",\n    \"url\": \"git+https://github.com/websockets/ws.git\"\n  },\n  \"author\": \"Einar Otto Stangvik <einaros@gmail.com> (http://2x.io)\",\n  \"license\": \"MIT\",\n  \"main\": \"index.js\",\n  \"exports\": {\n    \".\": {\n      \"browser\": \"./browser.js\",\n      \"import\": \"./wrapper.mjs\",\n      \"require\": \"./index.js\"\n    },\n    \"./package.json\": \"./package.json\"\n  },\n  \"browser\": \"browser.js\",\n  \"engines\": {\n    \"node\": \">=10.0.0\"\n  },\n  \"files\": [\n    \"browser.js\",\n    \"index.js\",\n    \"lib/*.js\",\n    \"wrapper.mjs\"\n  ],\n  \"scripts\": {\n    \"test\": \"nyc --reporter=lcov --reporter=text mocha --throw-deprecation test/*.test.js\",\n    \"integration\": \"mocha --throw-deprecation test/*.integration.js\",\n    \"lint\": \"eslint . && prettier --check --ignore-path .gitignore \\\"**/*.{json,md,yaml,yml}\\\"\"\n  },\n  \"peerDependencies\": {\n    \"bufferutil\": \"^4.0.1\",\n    \"utf-8-validate\": \">=5.0.2\"\n  },\n  \"peerDependenciesMeta\": {\n    \"bufferutil\": {\n      \"optional\": true\n    },\n    \"utf-8-validate\": {\n      \"optional\": true\n    }\n  },\n  \"devDependencies\": {\n    \"@eslint/js\": \"^10.0.1\",\n    \"benchmark\": \"^2.1.4\",\n    \"bufferutil\": \"^4.0.1\",\n    \"eslint\": \"^10.0.1\",\n    \"eslint-config-prettier\": \"^10.0.1\",\n    \"eslint-plugin-prettier\": \"^5.0.0\",\n    \"globals\": \"^17.0.0\",\n    \"mocha\": \"^8.4.0\",\n    \"nyc\": \"^15.0.0\",\n    \"prettier\": \"^3.0.0\",\n    \"utf-8-validate\": \"^6.0.0\"\n  }\n}\n"
  },
  {
    "path": "test/autobahn-server.js",
    "content": "'use strict';\n\nconst WebSocket = require('../');\n\nconst port = process.argv.length > 2 ? parseInt(process.argv[2]) : 9001;\nconst wss = new WebSocket.Server({ port }, () => {\n  console.log(\n    `Listening to port ${port}. Use extra argument to define the port`\n  );\n});\n\nwss.on('connection', (ws) => {\n  ws.on('message', (data, isBinary) => {\n    ws.send(data, { binary: isBinary });\n  });\n  ws.on('error', (e) => console.error(e));\n});\n"
  },
  {
    "path": "test/autobahn.js",
    "content": "'use strict';\n\nconst WebSocket = require('../');\n\nlet currentTest = 1;\nlet testCount;\n\nfunction nextTest() {\n  let ws;\n\n  if (currentTest > testCount) {\n    ws = new WebSocket('ws://localhost:9001/updateReports?agent=ws');\n    return;\n  }\n\n  console.log(`Running test case ${currentTest}/${testCount}`);\n\n  ws = new WebSocket(\n    `ws://localhost:9001/runCase?case=${currentTest}&agent=ws`\n  );\n  ws.on('message', (data, isBinary) => {\n    ws.send(data, { binary: isBinary });\n  });\n  ws.on('close', () => {\n    currentTest++;\n    process.nextTick(nextTest);\n  });\n  ws.on('error', (e) => console.error(e));\n}\n\nconst ws = new WebSocket('ws://localhost:9001/getCaseCount');\nws.on('message', (data) => {\n  testCount = parseInt(data);\n});\nws.on('close', () => {\n  if (testCount > 0) {\n    nextTest();\n  }\n});\n"
  },
  {
    "path": "test/buffer-util.test.js",
    "content": "'use strict';\n\nconst assert = require('assert');\n\nconst { concat } = require('../lib/buffer-util');\n\ndescribe('bufferUtil', () => {\n  describe('concat', () => {\n    it('never returns uninitialized data', () => {\n      const buf = concat([Buffer.from([1, 2]), Buffer.from([3, 4])], 6);\n\n      assert.ok(buf.equals(Buffer.from([1, 2, 3, 4])));\n    });\n  });\n});\n"
  },
  {
    "path": "test/create-websocket-stream.test.js",
    "content": "'use strict';\n\nconst assert = require('assert');\nconst EventEmitter = require('events');\nconst { createServer } = require('http');\nconst { Duplex, getDefaultHighWaterMark } = require('stream');\nconst { randomBytes } = require('crypto');\n\nconst createWebSocketStream = require('../lib/stream');\nconst Sender = require('../lib/sender');\nconst WebSocket = require('..');\nconst { EMPTY_BUFFER } = require('../lib/constants');\n\nconst highWaterMark = getDefaultHighWaterMark\n  ? getDefaultHighWaterMark(false)\n  : 16 * 1024;\n\ndescribe('createWebSocketStream', () => {\n  it('is exposed as a property of the `WebSocket` class', () => {\n    assert.strictEqual(WebSocket.createWebSocketStream, createWebSocketStream);\n  });\n\n  it('returns a `Duplex` stream', () => {\n    const duplex = createWebSocketStream(new EventEmitter());\n\n    assert.ok(duplex instanceof Duplex);\n  });\n\n  it('passes the options object to the `Duplex` constructor', (done) => {\n    const wss = new WebSocket.Server({ port: 0 }, () => {\n      const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n      const duplex = createWebSocketStream(ws, {\n        allowHalfOpen: false,\n        encoding: 'utf8'\n      });\n\n      duplex.on('data', (chunk) => {\n        assert.strictEqual(chunk, 'hi');\n\n        duplex.on('close', () => {\n          wss.close(done);\n        });\n      });\n    });\n\n    wss.on('connection', (ws) => {\n      ws.send(Buffer.from('hi'));\n      ws.close();\n    });\n  });\n\n  describe('The returned stream', () => {\n    it('buffers writes if `readyState` is `CONNECTING`', (done) => {\n      const chunk = randomBytes(1024);\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        assert.strictEqual(ws.readyState, WebSocket.CONNECTING);\n\n        const duplex = createWebSocketStream(ws);\n\n        duplex.write(chunk);\n      });\n\n      wss.on('connection', (ws) => {\n        ws.on('message', (message, isBinary) => {\n          ws.on('close', (code, reason) => {\n            assert.deepStrictEqual(message, chunk);\n            assert.ok(isBinary);\n            assert.strictEqual(code, 1005);\n            assert.strictEqual(reason, EMPTY_BUFFER);\n            wss.close(done);\n          });\n        });\n\n        ws.close();\n      });\n    });\n\n    it('errors if a write occurs when `readyState` is `CLOSING`', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n        const duplex = createWebSocketStream(ws);\n\n        duplex.on('error', (err) => {\n          assert.ok(duplex.destroyed);\n          assert.ok(err instanceof Error);\n          assert.strictEqual(\n            err.message,\n            'WebSocket is not open: readyState 2 (CLOSING)'\n          );\n\n          duplex.on('close', () => {\n            wss.close(done);\n          });\n        });\n\n        ws.on('open', () => {\n          ws._receiver.on('conclude', () => {\n            duplex.write('hi');\n          });\n        });\n      });\n\n      wss.on('connection', (ws) => {\n        ws.close();\n      });\n    });\n\n    it('errors if a write occurs when `readyState` is `CLOSED`', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n        const duplex = createWebSocketStream(ws);\n\n        duplex.on('error', (err) => {\n          assert.ok(duplex.destroyed);\n          assert.ok(err instanceof Error);\n          assert.strictEqual(\n            err.message,\n            'WebSocket is not open: readyState 3 (CLOSED)'\n          );\n\n          duplex.on('close', () => {\n            wss.close(done);\n          });\n        });\n\n        ws.on('close', () => {\n          duplex.write('hi');\n        });\n      });\n\n      wss.on('connection', (ws) => {\n        ws.close();\n      });\n    });\n\n    it('does not error if `_final()` is called while connecting', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        assert.strictEqual(ws.readyState, WebSocket.CONNECTING);\n\n        const duplex = createWebSocketStream(ws);\n\n        duplex.on('close', () => {\n          wss.close(done);\n        });\n\n        duplex.resume();\n        duplex.end();\n      });\n    });\n\n    it('makes `_final()` a noop if no socket is assigned', (done) => {\n      const server = createServer();\n\n      server.on('upgrade', (request, socket) => {\n        socket.on('end', socket.end);\n\n        const headers = [\n          'HTTP/1.1 101 Switching Protocols',\n          'Upgrade: websocket',\n          'Connection: Upgrade',\n          'Sec-WebSocket-Accept: foo'\n        ];\n\n        socket.write(headers.concat('\\r\\n').join('\\r\\n'));\n      });\n\n      server.listen(() => {\n        const called = [];\n        const ws = new WebSocket(`ws://localhost:${server.address().port}`);\n        const duplex = WebSocket.createWebSocketStream(ws);\n        const final = duplex._final;\n\n        duplex._final = (callback) => {\n          called.push('final');\n          assert.strictEqual(ws.readyState, WebSocket.CLOSING);\n          assert.strictEqual(ws._socket, null);\n\n          final(callback);\n        };\n\n        duplex.on('error', (err) => {\n          called.push('error');\n          assert.ok(err instanceof Error);\n          assert.strictEqual(\n            err.message,\n            'Invalid Sec-WebSocket-Accept header'\n          );\n        });\n\n        duplex.on('finish', () => {\n          called.push('finish');\n        });\n\n        duplex.on('close', () => {\n          assert.deepStrictEqual(called, ['final', 'error']);\n          server.close(done);\n        });\n\n        ws.on('upgrade', () => {\n          process.nextTick(() => {\n            duplex.end();\n          });\n        });\n      });\n    });\n\n    it('reemits errors', (done) => {\n      let duplexCloseEventEmitted = false;\n      let serverClientCloseEventEmitted = false;\n\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n        const duplex = createWebSocketStream(ws);\n\n        duplex.on('error', (err) => {\n          assert.ok(err instanceof RangeError);\n          assert.strictEqual(err.code, 'WS_ERR_INVALID_OPCODE');\n          assert.strictEqual(\n            err.message,\n            'Invalid WebSocket frame: invalid opcode 5'\n          );\n\n          duplex.on('close', () => {\n            duplexCloseEventEmitted = true;\n            if (serverClientCloseEventEmitted) wss.close(done);\n          });\n        });\n      });\n\n      wss.on('connection', (ws) => {\n        ws._socket.write(Buffer.from([0x85, 0x00]));\n        ws.on('close', (code, reason) => {\n          assert.strictEqual(code, 1002);\n          assert.deepStrictEqual(reason, EMPTY_BUFFER);\n\n          serverClientCloseEventEmitted = true;\n          if (duplexCloseEventEmitted) wss.close(done);\n        });\n      });\n    });\n\n    it('does not swallow errors that may occur while destroying', (done) => {\n      const frame = Buffer.concat(\n        Sender.frame(Buffer.from([0x22, 0xfa, 0xec, 0x78]), {\n          fin: true,\n          rsv1: true,\n          opcode: 0x02,\n          mask: false,\n          readOnly: false\n        })\n      );\n\n      const wss = new WebSocket.Server(\n        {\n          perMessageDeflate: true,\n          port: 0\n        },\n        () => {\n          const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n          const duplex = createWebSocketStream(ws);\n\n          duplex.on('error', (err) => {\n            assert.ok(err instanceof Error);\n            assert.strictEqual(err.code, 'Z_DATA_ERROR');\n            assert.strictEqual(err.errno, -3);\n\n            duplex.on('close', () => {\n              wss.close(done);\n            });\n          });\n\n          let bytesRead = 0;\n\n          ws.on('open', () => {\n            ws._socket.on('data', (chunk) => {\n              bytesRead += chunk.length;\n              if (bytesRead === frame.length) duplex.destroy();\n            });\n          });\n        }\n      );\n\n      wss.on('connection', (ws) => {\n        ws._socket.write(frame);\n      });\n    });\n\n    it(\"does not suppress the throwing behavior of 'error' events\", (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n        createWebSocketStream(ws);\n      });\n\n      wss.on('connection', (ws) => {\n        ws._socket.write(Buffer.from([0x85, 0x00]));\n      });\n\n      assert.strictEqual(\n        process.listenerCount('uncaughtException'),\n        EventEmitter.usingDomains ? 2 : 1\n      );\n\n      const listener = process.listeners('uncaughtException').pop();\n\n      process.removeListener('uncaughtException', listener);\n      process.once('uncaughtException', (err) => {\n        assert.ok(err instanceof Error);\n        assert.strictEqual(\n          err.message,\n          'Invalid WebSocket frame: invalid opcode 5'\n        );\n\n        process.on('uncaughtException', listener);\n        wss.close(done);\n      });\n    });\n\n    it(\"is destroyed after 'end' and 'finish' are emitted (1/2)\", (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const events = [];\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n        const duplex = createWebSocketStream(ws);\n\n        duplex.on('end', () => {\n          events.push('end');\n          assert.ok(duplex.destroyed);\n        });\n\n        duplex.on('close', () => {\n          assert.deepStrictEqual(events, ['finish', 'end']);\n          wss.close(done);\n        });\n\n        duplex.on('finish', () => {\n          events.push('finish');\n          assert.ok(!duplex.destroyed);\n          assert.ok(duplex.readable);\n\n          duplex.resume();\n        });\n\n        ws.on('close', () => {\n          duplex.end();\n        });\n      });\n\n      wss.on('connection', (ws) => {\n        ws.send('foo');\n        ws.close();\n      });\n    });\n\n    it(\"is destroyed after 'end' and 'finish' are emitted (2/2)\", (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const events = [];\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n        const duplex = createWebSocketStream(ws);\n\n        duplex.on('end', () => {\n          events.push('end');\n          assert.ok(!duplex.destroyed);\n          assert.ok(duplex.writable);\n\n          duplex.end();\n        });\n\n        duplex.on('close', () => {\n          assert.deepStrictEqual(events, ['end', 'finish']);\n          wss.close(done);\n        });\n\n        duplex.on('finish', () => {\n          events.push('finish');\n        });\n\n        duplex.resume();\n      });\n\n      wss.on('connection', (ws) => {\n        ws.close();\n      });\n    });\n\n    it('handles backpressure (1/3)', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        // eslint-disable-next-line no-unused-vars\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n      });\n\n      wss.on('connection', (ws) => {\n        const duplex = createWebSocketStream(ws);\n\n        duplex.resume();\n\n        duplex.on('drain', () => {\n          duplex.on('close', () => {\n            wss.close(done);\n          });\n\n          duplex.end();\n        });\n\n        const chunk = randomBytes(1024);\n        let ret;\n\n        do {\n          ret = duplex.write(chunk);\n        } while (ret !== false);\n      });\n    });\n\n    it('handles backpressure (2/3)', (done) => {\n      const wss = new WebSocket.Server(\n        { port: 0, perMessageDeflate: true },\n        () => {\n          const called = [];\n          const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n          const duplex = createWebSocketStream(ws);\n          const read = duplex._read;\n\n          duplex._read = () => {\n            duplex._read = read;\n            called.push('read');\n            assert.ok(ws._receiver._writableState.needDrain);\n            read();\n            assert.ok(ws._socket.isPaused());\n          };\n\n          ws.on('open', () => {\n            ws._socket.on('pause', () => {\n              duplex.resume();\n            });\n\n            ws._receiver.on('drain', () => {\n              called.push('drain');\n              assert.ok(!ws._socket.isPaused());\n              duplex.end();\n            });\n\n            const opts = {\n              fin: true,\n              opcode: 0x02,\n              mask: false,\n              readOnly: false\n            };\n\n            const list = [\n              ...Sender.frame(randomBytes(highWaterMark), {\n                rsv1: false,\n                ...opts\n              }),\n              ...Sender.frame(Buffer.alloc(1), { rsv1: true, ...opts })\n            ];\n\n            // This hack is used because there is no guarantee that more than\n            // `highWaterMark` bytes will be sent as a single TCP packet.\n            ws._socket.push(Buffer.concat(list));\n          });\n\n          duplex.on('close', () => {\n            assert.deepStrictEqual(called, ['read', 'drain']);\n            wss.close(done);\n          });\n        }\n      );\n    });\n\n    it('handles backpressure (3/3)', (done) => {\n      const wss = new WebSocket.Server(\n        { port: 0, perMessageDeflate: true },\n        () => {\n          const called = [];\n          const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n          const duplex = createWebSocketStream(ws);\n          const read = duplex._read;\n\n          duplex._read = () => {\n            called.push('read');\n            assert.ok(!ws._receiver._writableState.needDrain);\n            read();\n            assert.ok(!ws._socket.isPaused());\n            duplex.end();\n          };\n\n          ws.on('open', () => {\n            ws._receiver.on('drain', () => {\n              called.push('drain');\n              assert.ok(ws._socket.isPaused());\n              duplex.resume();\n            });\n\n            const opts = {\n              fin: true,\n              opcode: 0x02,\n              mask: false,\n              readOnly: false\n            };\n\n            const list = [\n              ...Sender.frame(randomBytes(highWaterMark), {\n                rsv1: false,\n                ...opts\n              }),\n              ...Sender.frame(Buffer.alloc(1), { rsv1: true, ...opts })\n            ];\n\n            ws._socket.push(Buffer.concat(list));\n          });\n\n          duplex.on('close', () => {\n            assert.deepStrictEqual(called, ['drain', 'read']);\n            wss.close(done);\n          });\n        }\n      );\n    });\n\n    it('can be destroyed (1/2)', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const error = new Error('Oops');\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n        const duplex = createWebSocketStream(ws);\n\n        duplex.on('error', (err) => {\n          assert.strictEqual(err, error);\n\n          duplex.on('close', () => {\n            wss.close(done);\n          });\n        });\n\n        ws.on('open', () => {\n          duplex.destroy(error);\n        });\n      });\n    });\n\n    it('can be destroyed (2/2)', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n        const duplex = createWebSocketStream(ws);\n\n        duplex.on('close', () => {\n          wss.close(done);\n        });\n\n        ws.on('open', () => {\n          duplex.destroy();\n        });\n      });\n    });\n\n    it('converts text messages to strings in readable object mode', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const events = [];\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n        const duplex = createWebSocketStream(ws, { readableObjectMode: true });\n\n        duplex.on('data', (data) => {\n          events.push('data');\n          assert.strictEqual(data, 'foo');\n        });\n\n        duplex.on('end', () => {\n          events.push('end');\n          duplex.end();\n        });\n\n        duplex.on('close', () => {\n          assert.deepStrictEqual(events, ['data', 'end']);\n          wss.close(done);\n        });\n      });\n\n      wss.on('connection', (ws) => {\n        ws.send('foo');\n        ws.close();\n      });\n    });\n\n    it('resumes the socket if `readyState` is `CLOSING`', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n        const duplex = createWebSocketStream(ws);\n\n        ws.on('message', () => {\n          assert.ok(ws._socket.isPaused());\n\n          duplex.on('close', () => {\n            wss.close(done);\n          });\n\n          duplex.end();\n\n          process.nextTick(() => {\n            assert.strictEqual(ws.readyState, WebSocket.CLOSING);\n            duplex.resume();\n          });\n        });\n      });\n\n      wss.on('connection', (ws) => {\n        ws.send(randomBytes(highWaterMark));\n      });\n    });\n  });\n});\n"
  },
  {
    "path": "test/duplex-pair.js",
    "content": "//\n// This code was copied from\n// https://github.com/nodejs/node/blob/c506660f3267/test/common/duplexpair.js\n//\n// Copyright Node.js contributors. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to\n// deal in the Software without restriction, including without limitation the\n// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or\n// sell copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\n// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS\n// IN THE SOFTWARE.\n//\n'use strict';\n\nconst assert = require('assert');\nconst { Duplex } = require('stream');\n\nconst kCallback = Symbol('Callback');\nconst kOtherSide = Symbol('Other');\n\nclass DuplexSocket extends Duplex {\n  constructor() {\n    super();\n    this[kCallback] = null;\n    this[kOtherSide] = null;\n  }\n\n  _read() {\n    const callback = this[kCallback];\n    if (callback) {\n      this[kCallback] = null;\n      callback();\n    }\n  }\n\n  _write(chunk, encoding, callback) {\n    assert.notStrictEqual(this[kOtherSide], null);\n    assert.strictEqual(this[kOtherSide][kCallback], null);\n    if (chunk.length === 0) {\n      process.nextTick(callback);\n    } else {\n      this[kOtherSide].push(chunk);\n      this[kOtherSide][kCallback] = callback;\n    }\n  }\n\n  _final(callback) {\n    this[kOtherSide].on('end', callback);\n    this[kOtherSide].push(null);\n  }\n}\n\nfunction makeDuplexPair() {\n  const clientSide = new DuplexSocket();\n  const serverSide = new DuplexSocket();\n  clientSide[kOtherSide] = serverSide;\n  serverSide[kOtherSide] = clientSide;\n  return { clientSide, serverSide };\n}\n\nmodule.exports = makeDuplexPair;\n"
  },
  {
    "path": "test/event-target.test.js",
    "content": "'use strict';\n\nconst assert = require('assert');\n\nconst {\n  CloseEvent,\n  ErrorEvent,\n  Event,\n  MessageEvent\n} = require('../lib/event-target');\n\ndescribe('Event', () => {\n  describe('#ctor', () => {\n    it('takes a `type` argument', () => {\n      const event = new Event('foo');\n\n      assert.strictEqual(event.type, 'foo');\n    });\n  });\n\n  describe('Properties', () => {\n    describe('`target`', () => {\n      it('is enumerable and configurable', () => {\n        const descriptor = Object.getOwnPropertyDescriptor(\n          Event.prototype,\n          'target'\n        );\n\n        assert.strictEqual(descriptor.configurable, true);\n        assert.strictEqual(descriptor.enumerable, true);\n        assert.ok(descriptor.get !== undefined);\n        assert.ok(descriptor.set === undefined);\n      });\n\n      it('defaults to `null`', () => {\n        const event = new Event('foo');\n\n        assert.strictEqual(event.target, null);\n      });\n    });\n\n    describe('`type`', () => {\n      it('is enumerable and configurable', () => {\n        const descriptor = Object.getOwnPropertyDescriptor(\n          Event.prototype,\n          'type'\n        );\n\n        assert.strictEqual(descriptor.configurable, true);\n        assert.strictEqual(descriptor.enumerable, true);\n        assert.ok(descriptor.get !== undefined);\n        assert.ok(descriptor.set === undefined);\n      });\n    });\n  });\n});\n\ndescribe('CloseEvent', () => {\n  it('inherits from `Event`', () => {\n    assert.ok(CloseEvent.prototype instanceof Event);\n  });\n\n  describe('#ctor', () => {\n    it('takes a `type` argument', () => {\n      const event = new CloseEvent('foo');\n\n      assert.strictEqual(event.type, 'foo');\n    });\n\n    it('takes an optional `options` argument', () => {\n      const event = new CloseEvent('close', {\n        code: 1000,\n        reason: 'foo',\n        wasClean: true\n      });\n\n      assert.strictEqual(event.type, 'close');\n      assert.strictEqual(event.code, 1000);\n      assert.strictEqual(event.reason, 'foo');\n      assert.strictEqual(event.wasClean, true);\n    });\n  });\n\n  describe('Properties', () => {\n    describe('`code`', () => {\n      it('is enumerable and configurable', () => {\n        const descriptor = Object.getOwnPropertyDescriptor(\n          CloseEvent.prototype,\n          'code'\n        );\n\n        assert.strictEqual(descriptor.configurable, true);\n        assert.strictEqual(descriptor.enumerable, true);\n        assert.ok(descriptor.get !== undefined);\n        assert.ok(descriptor.set === undefined);\n      });\n\n      it('defaults to 0', () => {\n        const event = new CloseEvent('close');\n\n        assert.strictEqual(event.code, 0);\n      });\n    });\n\n    describe('`reason`', () => {\n      it('is enumerable and configurable', () => {\n        const descriptor = Object.getOwnPropertyDescriptor(\n          CloseEvent.prototype,\n          'reason'\n        );\n\n        assert.strictEqual(descriptor.configurable, true);\n        assert.strictEqual(descriptor.enumerable, true);\n        assert.ok(descriptor.get !== undefined);\n        assert.ok(descriptor.set === undefined);\n      });\n\n      it('defaults to an empty string', () => {\n        const event = new CloseEvent('close');\n\n        assert.strictEqual(event.reason, '');\n      });\n    });\n\n    describe('`wasClean`', () => {\n      it('is enumerable and configurable', () => {\n        const descriptor = Object.getOwnPropertyDescriptor(\n          CloseEvent.prototype,\n          'wasClean'\n        );\n\n        assert.strictEqual(descriptor.configurable, true);\n        assert.strictEqual(descriptor.enumerable, true);\n        assert.ok(descriptor.get !== undefined);\n        assert.ok(descriptor.set === undefined);\n      });\n\n      it('defaults to false', () => {\n        const event = new CloseEvent('close');\n\n        assert.strictEqual(event.wasClean, false);\n      });\n    });\n  });\n});\n\ndescribe('ErrorEvent', () => {\n  it('inherits from `Event`', () => {\n    assert.ok(ErrorEvent.prototype instanceof Event);\n  });\n\n  describe('#ctor', () => {\n    it('takes a `type` argument', () => {\n      const event = new ErrorEvent('foo');\n\n      assert.strictEqual(event.type, 'foo');\n    });\n\n    it('takes an optional `options` argument', () => {\n      const error = new Error('Oops');\n      const event = new ErrorEvent('error', { error, message: error.message });\n\n      assert.strictEqual(event.type, 'error');\n      assert.strictEqual(event.error, error);\n      assert.strictEqual(event.message, error.message);\n    });\n  });\n\n  describe('Properties', () => {\n    describe('`error`', () => {\n      it('is enumerable and configurable', () => {\n        const descriptor = Object.getOwnPropertyDescriptor(\n          ErrorEvent.prototype,\n          'error'\n        );\n\n        assert.strictEqual(descriptor.configurable, true);\n        assert.strictEqual(descriptor.enumerable, true);\n        assert.ok(descriptor.get !== undefined);\n        assert.ok(descriptor.set === undefined);\n      });\n\n      it('defaults to `null`', () => {\n        const event = new ErrorEvent('error');\n\n        assert.strictEqual(event.error, null);\n      });\n    });\n\n    describe('`message`', () => {\n      it('is enumerable and configurable', () => {\n        const descriptor = Object.getOwnPropertyDescriptor(\n          ErrorEvent.prototype,\n          'message'\n        );\n\n        assert.strictEqual(descriptor.configurable, true);\n        assert.strictEqual(descriptor.enumerable, true);\n        assert.ok(descriptor.get !== undefined);\n        assert.ok(descriptor.set === undefined);\n      });\n\n      it('defaults to an empty string', () => {\n        const event = new ErrorEvent('error');\n\n        assert.strictEqual(event.message, '');\n      });\n    });\n  });\n});\n\ndescribe('MessageEvent', () => {\n  it('inherits from `Event`', () => {\n    assert.ok(MessageEvent.prototype instanceof Event);\n  });\n\n  describe('#ctor', () => {\n    it('takes a `type` argument', () => {\n      const event = new MessageEvent('foo');\n\n      assert.strictEqual(event.type, 'foo');\n    });\n\n    it('takes an optional `options` argument', () => {\n      const event = new MessageEvent('message', { data: 'bar' });\n\n      assert.strictEqual(event.type, 'message');\n      assert.strictEqual(event.data, 'bar');\n    });\n  });\n\n  describe('Properties', () => {\n    describe('`data`', () => {\n      it('is enumerable and configurable', () => {\n        const descriptor = Object.getOwnPropertyDescriptor(\n          MessageEvent.prototype,\n          'data'\n        );\n\n        assert.strictEqual(descriptor.configurable, true);\n        assert.strictEqual(descriptor.enumerable, true);\n        assert.ok(descriptor.get !== undefined);\n        assert.ok(descriptor.set === undefined);\n      });\n\n      it('defaults to `null`', () => {\n        const event = new MessageEvent('message');\n\n        assert.strictEqual(event.data, null);\n      });\n    });\n  });\n});\n"
  },
  {
    "path": "test/extension.test.js",
    "content": "'use strict';\n\nconst assert = require('assert');\n\nconst { format, parse } = require('../lib/extension');\n\ndescribe('extension', () => {\n  describe('parse', () => {\n    it('parses a single extension', () => {\n      assert.deepStrictEqual(parse('foo'), {\n        foo: [{ __proto__: null }],\n        __proto__: null\n      });\n    });\n\n    it('parses params', () => {\n      assert.deepStrictEqual(parse('foo;bar;baz=1;bar=2'), {\n        foo: [{ bar: [true, '2'], baz: ['1'], __proto__: null }],\n        __proto__: null\n      });\n    });\n\n    it('parses multiple extensions', () => {\n      assert.deepStrictEqual(parse('foo,bar;baz,foo;baz'), {\n        foo: [{ __proto__: null }, { baz: [true], __proto__: null }],\n        bar: [{ baz: [true], __proto__: null }],\n        __proto__: null\n      });\n    });\n\n    it('parses quoted params', () => {\n      assert.deepStrictEqual(parse('foo;bar=\"hi\"'), {\n        foo: [{ bar: ['hi'], __proto__: null }],\n        __proto__: null\n      });\n      assert.deepStrictEqual(parse('foo;bar=\"\\\\0\"'), {\n        foo: [{ bar: ['0'], __proto__: null }],\n        __proto__: null\n      });\n      assert.deepStrictEqual(parse('foo;bar=\"b\\\\a\\\\z\"'), {\n        foo: [{ bar: ['baz'], __proto__: null }],\n        __proto__: null\n      });\n      assert.deepStrictEqual(parse('foo;bar=\"b\\\\az\";bar'), {\n        foo: [{ bar: ['baz', true], __proto__: null }],\n        __proto__: null\n      });\n      assert.throws(\n        () => parse('foo;bar=\"baz\"qux'),\n        /^SyntaxError: Unexpected character at index 13$/\n      );\n      assert.throws(\n        () => parse('foo;bar=\"baz\" qux'),\n        /^SyntaxError: Unexpected character at index 14$/\n      );\n    });\n\n    it('works with names that match `Object.prototype` property names', () => {\n      assert.deepStrictEqual(parse('hasOwnProperty, toString'), {\n        hasOwnProperty: [{ __proto__: null }],\n        toString: [{ __proto__: null }],\n        __proto__: null\n      });\n      assert.deepStrictEqual(parse('foo;constructor'), {\n        foo: [{ constructor: [true], __proto__: null }],\n        __proto__: null\n      });\n    });\n\n    it('ignores the optional white spaces', () => {\n      const header = 'foo; bar\\t; \\tbaz=1\\t ;  bar=\"1\"\\t\\t, \\tqux\\t ;norf';\n\n      assert.deepStrictEqual(parse(header), {\n        foo: [{ bar: [true, '1'], baz: ['1'], __proto__: null }],\n        qux: [{ norf: [true], __proto__: null }],\n        __proto__: null\n      });\n    });\n\n    it('throws an error if a name is empty', () => {\n      [\n        [',', 0],\n        ['foo,,', 4],\n        ['foo,  ,', 6],\n        ['foo;=', 4],\n        ['foo; =', 5],\n        ['foo;;', 4],\n        ['foo; ;', 5],\n        ['foo;bar=,', 8],\n        ['foo;bar=\"\"', 9]\n      ].forEach((element) => {\n        assert.throws(\n          () => parse(element[0]),\n          new RegExp(\n            `^SyntaxError: Unexpected character at index ${element[1]}$`\n          )\n        );\n      });\n    });\n\n    it('throws an error if a white space is misplaced', () => {\n      [\n        [' foo', 0],\n        ['f oo', 2],\n        ['foo;ba r', 7],\n        ['foo;bar =', 8],\n        ['foo;bar= ', 8],\n        ['foo;bar=ba z', 11]\n      ].forEach((element) => {\n        assert.throws(\n          () => parse(element[0]),\n          new RegExp(\n            `^SyntaxError: Unexpected character at index ${element[1]}$`\n          )\n        );\n      });\n    });\n\n    it('throws an error if a token contains invalid characters', () => {\n      [\n        ['f@o', 1],\n        ['f\\\\oo', 1],\n        ['\"foo\"', 0],\n        ['f\"oo\"', 1],\n        ['foo;b@r', 5],\n        ['foo;b\\\\ar', 5],\n        ['foo;\"bar\"', 4],\n        ['foo;b\"ar\"', 5],\n        ['foo;bar=b@z', 9],\n        ['foo;bar=b\\\\az ', 9],\n        ['foo;bar=\"b@z\"', 10],\n        ['foo;bar=\"baz;\"', 12],\n        ['foo;bar=b\"az\"', 9],\n        ['foo;bar=\"\\\\\\\\\"', 10]\n      ].forEach((element) => {\n        assert.throws(\n          () => parse(element[0]),\n          new RegExp(\n            `^SyntaxError: Unexpected character at index ${element[1]}$`\n          )\n        );\n      });\n    });\n\n    it('throws an error if the header value ends prematurely', () => {\n      [\n        '',\n        'foo ',\n        'foo\\t',\n        'foo, ',\n        'foo;',\n        'foo;bar ',\n        'foo;bar,',\n        'foo;bar; ',\n        'foo;bar=',\n        'foo;bar=\"baz',\n        'foo;bar=\"1\\\\',\n        'foo;bar=\"baz\" '\n      ].forEach((header) => {\n        assert.throws(\n          () => parse(header),\n          /^SyntaxError: Unexpected end of input$/\n        );\n      });\n    });\n  });\n\n  describe('format', () => {\n    it('formats a single extension', () => {\n      const extensions = format({ foo: {} });\n\n      assert.strictEqual(extensions, 'foo');\n    });\n\n    it('formats params', () => {\n      const extensions = format({ foo: { bar: [true, 2], baz: 1 } });\n\n      assert.strictEqual(extensions, 'foo; bar; bar=2; baz=1');\n    });\n\n    it('formats multiple extensions', () => {\n      const extensions = format({\n        foo: [{}, { baz: true }],\n        bar: { baz: true }\n      });\n\n      assert.strictEqual(extensions, 'foo, foo; baz, bar; baz');\n    });\n  });\n});\n"
  },
  {
    "path": "test/fixtures/ca-certificate.pem",
    "content": "-----BEGIN CERTIFICATE-----\nMIIBtTCCAVoCCQCXqK2FegDgiDAKBggqhkjOPQQDAjBhMQswCQYDVQQGEwJJVDEQ\nMA4GA1UECAwHUGVydWdpYTEQMA4GA1UEBwwHRm9saWdubzETMBEGA1UECgwKd2Vi\nc29ja2V0czELMAkGA1UECwwCd3MxDDAKBgNVBAMMA2NhMTAgFw0yMTA1MjYxOTA1\nMjdaGA8yMTIxMDUwMjE5MDUyN1owYTELMAkGA1UEBhMCSVQxEDAOBgNVBAgMB1Bl\ncnVnaWExEDAOBgNVBAcMB0ZvbGlnbm8xEzARBgNVBAoMCndlYnNvY2tldHMxCzAJ\nBgNVBAsMAndzMQwwCgYDVQQDDANjYTEwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNC\nAASHE75QDQN6XNo/711YSbckaa8r4lt0hGkgtADaBFT9Qn9gcm5omapePZT76Ff9\nrwjMcS+YPXS7J7bk+QHLihJMMAoGCCqGSM49BAMCA0kAMEYCIQCUMdUih+sE0ZTu\nORlcKiM8DKyiKkGU4Ty+dslz6nVJjAIhAMcSy0SBsBDgsai1s9aCmAGJXCijNb6g\nvfWaatgq+ma2\n-----END CERTIFICATE-----\n"
  },
  {
    "path": "test/fixtures/ca-key.pem",
    "content": "-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIAa/Onpk27cLkqzje69Bac8yG+LTBXIPWT8yGlyjEFbboAoGCCqGSM49\nAwEHoUQDQgAEhxO+UA0DelzaP+9dWEm3JGmvK+JbdIRpILQA2gRU/UJ/YHJuaJmq\nXj2U++hX/a8IzHEvmD10uye25PkBy4oSTA==\n-----END EC PRIVATE KEY-----\n"
  },
  {
    "path": "test/fixtures/certificate.pem",
    "content": "-----BEGIN CERTIFICATE-----\nMIIBujCCAWACCQDjKdAMt3mZhDAKBggqhkjOPQQDAjBkMQswCQYDVQQGEwJJVDEQ\nMA4GA1UECAwHUGVydWdpYTEQMA4GA1UEBwwHRm9saWdubzETMBEGA1UECgwKd2Vi\nc29ja2V0czELMAkGA1UECwwCd3MxDzANBgNVBAMMBnNlcnZlcjAgFw0yMTA1MjYx\nOTEwMjlaGA8yMTIxMDUwMjE5MTAyOVowZDELMAkGA1UEBhMCSVQxEDAOBgNVBAgM\nB1BlcnVnaWExEDAOBgNVBAcMB0ZvbGlnbm8xEzARBgNVBAoMCndlYnNvY2tldHMx\nCzAJBgNVBAsMAndzMQ8wDQYDVQQDDAZzZXJ2ZXIwWTATBgcqhkjOPQIBBggqhkjO\nPQMBBwNCAAQKhyRhdSVOecbJU4O5XkB/iGodbnCOqmchs4TXmE3Prv5SrNDhODDv\nrOWTXwR3/HrrdNfOzPdb54amu8POwpohMAoGCCqGSM49BAMCA0gAMEUCIHMRUSPl\n8FGkDLl8KF1A+SbT2ds3zUOLdYvj30Z2SKSVAiEA84U/R1ly9wf5Rzv93sTHI99o\nKScsr/PHN8rT2pop5pk=\n-----END CERTIFICATE-----\n"
  },
  {
    "path": "test/fixtures/client-certificate.pem",
    "content": "-----BEGIN CERTIFICATE-----\nMIIBtzCCAV0CCQDDIX2dKuKP0zAKBggqhkjOPQQDAjBhMQswCQYDVQQGEwJJVDEQ\nMA4GA1UECAwHUGVydWdpYTEQMA4GA1UEBwwHRm9saWdubzETMBEGA1UECgwKd2Vi\nc29ja2V0czELMAkGA1UECwwCd3MxDDAKBgNVBAMMA2NhMTAgFw0yMTA1MjYxOTE3\nNDJaGA8yMTIxMDUwMjE5MTc0MlowZDELMAkGA1UEBhMCSVQxEDAOBgNVBAgMB1Bl\ncnVnaWExEDAOBgNVBAcMB0ZvbGlnbm8xEzARBgNVBAoMCndlYnNvY2tldHMxCzAJ\nBgNVBAsMAndzMQ8wDQYDVQQDDAZhZ2VudDEwWTATBgcqhkjOPQIBBggqhkjOPQMB\nBwNCAATwHlNS2b13TMhBTSWBXAn6TEPxrsvG93ZZyUlmrEMOXSMX2hI7sv660YNj\n+eGyE2CV33XsQxV3TUqi51fUjIu8MAoGCCqGSM49BAMCA0gAMEUCIQCxsqBre+Do\njnfg6XmCaB0fywNzcDlvdoVNuNAWfVNrSAIgDQmbM0mXZaSAkf4sgtKdXnpE3vrb\nMElb457Bi3B+rkE=\n-----END CERTIFICATE-----\n"
  },
  {
    "path": "test/fixtures/client-key.pem",
    "content": "-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIKVGskK0UR86WwMo5H0+hNAFGRBYsEevK3ye4y1YberVoAoGCCqGSM49\nAwEHoUQDQgAE8B5TUtm9d0zIQU0lgVwJ+kxD8a7Lxvd2WclJZqxDDl0jF9oSO7L+\nutGDY/nhshNgld917EMVd01KoudX1IyLvA==\n-----END EC PRIVATE KEY-----\n"
  },
  {
    "path": "test/fixtures/key.pem",
    "content": "-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIIjLz7YEWIrsGem2+YV8eJhHhetsjYIrjuqJLbdG7B3zoAoGCCqGSM49\nAwEHoUQDQgAECockYXUlTnnGyVODuV5Af4hqHW5wjqpnIbOE15hNz67+UqzQ4Tgw\n76zlk18Ed/x663TXzsz3W+eGprvDzsKaIQ==\n-----END EC PRIVATE KEY-----\n"
  },
  {
    "path": "test/limiter.test.js",
    "content": "'use strict';\n\nconst assert = require('assert');\n\nconst Limiter = require('../lib/limiter');\n\ndescribe('Limiter', () => {\n  describe('#ctor', () => {\n    it('takes a `concurrency` argument', () => {\n      const limiter = new Limiter(0);\n\n      assert.strictEqual(limiter.concurrency, Infinity);\n    });\n  });\n\n  describe('#kRun', () => {\n    it('limits the number of jobs allowed to run concurrently', (done) => {\n      const limiter = new Limiter(1);\n\n      limiter.add((callback) => {\n        setImmediate(() => {\n          callback();\n\n          assert.strictEqual(limiter.jobs.length, 0);\n          assert.strictEqual(limiter.pending, 1);\n        });\n      });\n\n      limiter.add((callback) => {\n        setImmediate(() => {\n          callback();\n\n          assert.strictEqual(limiter.pending, 0);\n          done();\n        });\n      });\n\n      assert.strictEqual(limiter.jobs.length, 1);\n    });\n  });\n});\n"
  },
  {
    "path": "test/permessage-deflate.test.js",
    "content": "'use strict';\n\nconst assert = require('assert');\n\nconst PerMessageDeflate = require('../lib/permessage-deflate');\nconst extension = require('../lib/extension');\n\ndescribe('PerMessageDeflate', () => {\n  describe('#offer', () => {\n    it('creates an offer', () => {\n      const perMessageDeflate = new PerMessageDeflate();\n\n      assert.deepStrictEqual(perMessageDeflate.offer(), {\n        client_max_window_bits: true\n      });\n    });\n\n    it('uses the configuration options', () => {\n      const perMessageDeflate = new PerMessageDeflate({\n        serverNoContextTakeover: true,\n        clientNoContextTakeover: true,\n        serverMaxWindowBits: 10,\n        clientMaxWindowBits: 11\n      });\n\n      assert.deepStrictEqual(perMessageDeflate.offer(), {\n        server_no_context_takeover: true,\n        client_no_context_takeover: true,\n        server_max_window_bits: 10,\n        client_max_window_bits: 11\n      });\n    });\n  });\n\n  describe('#accept', () => {\n    it('throws an error if a parameter has multiple values', () => {\n      const perMessageDeflate = new PerMessageDeflate();\n      const extensions = extension.parse(\n        'permessage-deflate; server_no_context_takeover; server_no_context_takeover'\n      );\n\n      assert.throws(\n        () => perMessageDeflate.accept(extensions['permessage-deflate']),\n        /^Error: Parameter \"server_no_context_takeover\" must have only a single value$/\n      );\n    });\n\n    it('throws an error if a parameter has an invalid name', () => {\n      const perMessageDeflate = new PerMessageDeflate();\n      const extensions = extension.parse('permessage-deflate;foo');\n\n      assert.throws(\n        () => perMessageDeflate.accept(extensions['permessage-deflate']),\n        /^Error: Unknown parameter \"foo\"$/\n      );\n    });\n\n    it('throws an error if client_no_context_takeover has a value', () => {\n      const perMessageDeflate = new PerMessageDeflate();\n      const extensions = extension.parse(\n        'permessage-deflate; client_no_context_takeover=10'\n      );\n\n      assert.throws(\n        () => perMessageDeflate.accept(extensions['permessage-deflate']),\n        /^TypeError: Invalid value for parameter \"client_no_context_takeover\": 10$/\n      );\n    });\n\n    it('throws an error if server_no_context_takeover has a value', () => {\n      const perMessageDeflate = new PerMessageDeflate();\n      const extensions = extension.parse(\n        'permessage-deflate; server_no_context_takeover=10'\n      );\n\n      assert.throws(\n        () => perMessageDeflate.accept(extensions['permessage-deflate']),\n        /^TypeError: Invalid value for parameter \"server_no_context_takeover\": 10$/\n      );\n    });\n\n    it('throws an error if server_max_window_bits has an invalid value', () => {\n      const perMessageDeflate = new PerMessageDeflate();\n\n      let extensions = extension.parse(\n        'permessage-deflate; server_max_window_bits=7'\n      );\n      assert.throws(\n        () => perMessageDeflate.accept(extensions['permessage-deflate']),\n        /^TypeError: Invalid value for parameter \"server_max_window_bits\": 7$/\n      );\n\n      extensions = extension.parse(\n        'permessage-deflate; server_max_window_bits'\n      );\n      assert.throws(\n        () => perMessageDeflate.accept(extensions['permessage-deflate']),\n        /^TypeError: Invalid value for parameter \"server_max_window_bits\": true$/\n      );\n    });\n\n    describe('As server', () => {\n      it('accepts an offer with no parameters', () => {\n        const perMessageDeflate = new PerMessageDeflate({ isServer: true });\n\n        assert.deepStrictEqual(perMessageDeflate.accept([{}]), {});\n      });\n\n      it('accepts an offer with parameters', () => {\n        const perMessageDeflate = new PerMessageDeflate({ isServer: true });\n        const extensions = extension.parse(\n          'permessage-deflate; server_no_context_takeover; ' +\n            'client_no_context_takeover; server_max_window_bits=10; ' +\n            'client_max_window_bits=11'\n        );\n\n        assert.deepStrictEqual(\n          perMessageDeflate.accept(extensions['permessage-deflate']),\n          {\n            server_no_context_takeover: true,\n            client_no_context_takeover: true,\n            server_max_window_bits: 10,\n            client_max_window_bits: 11,\n            __proto__: null\n          }\n        );\n      });\n\n      it('prefers the configuration options', () => {\n        const perMessageDeflate = new PerMessageDeflate({\n          serverNoContextTakeover: true,\n          clientNoContextTakeover: true,\n          serverMaxWindowBits: 12,\n          clientMaxWindowBits: 11,\n          isServer: true\n        });\n        const extensions = extension.parse(\n          'permessage-deflate; server_max_window_bits=14; client_max_window_bits=13'\n        );\n\n        assert.deepStrictEqual(\n          perMessageDeflate.accept(extensions['permessage-deflate']),\n          {\n            server_no_context_takeover: true,\n            client_no_context_takeover: true,\n            server_max_window_bits: 12,\n            client_max_window_bits: 11,\n            __proto__: null\n          }\n        );\n      });\n\n      it('accepts the first supported offer', () => {\n        const perMessageDeflate = new PerMessageDeflate({\n          isServer: true,\n          serverMaxWindowBits: 11\n        });\n        const extensions = extension.parse(\n          'permessage-deflate; server_max_window_bits=10, permessage-deflate'\n        );\n\n        assert.deepStrictEqual(\n          perMessageDeflate.accept(extensions['permessage-deflate']),\n          {\n            server_max_window_bits: 11,\n            __proto__: null\n          }\n        );\n      });\n\n      it('throws an error if server_no_context_takeover is unsupported', () => {\n        const perMessageDeflate = new PerMessageDeflate({\n          isServer: true,\n          serverNoContextTakeover: false\n        });\n        const extensions = extension.parse(\n          'permessage-deflate; server_no_context_takeover'\n        );\n\n        assert.throws(\n          () => perMessageDeflate.accept(extensions['permessage-deflate']),\n          /^Error: None of the extension offers can be accepted$/\n        );\n      });\n\n      it('throws an error if server_max_window_bits is unsupported', () => {\n        const perMessageDeflate = new PerMessageDeflate({\n          isServer: true,\n          serverMaxWindowBits: false\n        });\n        const extensions = extension.parse(\n          'permessage-deflate; server_max_window_bits=10'\n        );\n\n        assert.throws(\n          () => perMessageDeflate.accept(extensions['permessage-deflate']),\n          /^Error: None of the extension offers can be accepted$/\n        );\n      });\n\n      it('throws an error if server_max_window_bits is less than configuration', () => {\n        const perMessageDeflate = new PerMessageDeflate({\n          isServer: true,\n          serverMaxWindowBits: 11\n        });\n        const extensions = extension.parse(\n          'permessage-deflate; server_max_window_bits=10'\n        );\n\n        assert.throws(\n          () => perMessageDeflate.accept(extensions['permessage-deflate']),\n          /^Error: None of the extension offers can be accepted$/\n        );\n      });\n\n      it('throws an error if client_max_window_bits is unsupported on client', () => {\n        const perMessageDeflate = new PerMessageDeflate({\n          isServer: true,\n          clientMaxWindowBits: 10\n        });\n        const extensions = extension.parse('permessage-deflate');\n\n        assert.throws(\n          () => perMessageDeflate.accept(extensions['permessage-deflate']),\n          /^Error: None of the extension offers can be accepted$/\n        );\n      });\n\n      it('throws an error if client_max_window_bits has an invalid value', () => {\n        const perMessageDeflate = new PerMessageDeflate({ isServer: true });\n\n        const extensions = extension.parse(\n          'permessage-deflate; client_max_window_bits=16'\n        );\n        assert.throws(\n          () => perMessageDeflate.accept(extensions['permessage-deflate']),\n          /^TypeError: Invalid value for parameter \"client_max_window_bits\": 16$/\n        );\n      });\n    });\n\n    describe('As client', () => {\n      it('accepts a response with no parameters', () => {\n        const perMessageDeflate = new PerMessageDeflate({});\n\n        assert.deepStrictEqual(perMessageDeflate.accept([{}]), {});\n      });\n\n      it('accepts a response with parameters', () => {\n        const perMessageDeflate = new PerMessageDeflate({});\n        const extensions = extension.parse(\n          'permessage-deflate; server_no_context_takeover; ' +\n            'client_no_context_takeover; server_max_window_bits=10; ' +\n            'client_max_window_bits=11'\n        );\n\n        assert.deepStrictEqual(\n          perMessageDeflate.accept(extensions['permessage-deflate']),\n          {\n            server_no_context_takeover: true,\n            client_no_context_takeover: true,\n            server_max_window_bits: 10,\n            client_max_window_bits: 11,\n            __proto__: null\n          }\n        );\n      });\n\n      it('throws an error if client_no_context_takeover is unsupported', () => {\n        const perMessageDeflate = new PerMessageDeflate({\n          clientNoContextTakeover: false\n        });\n        const extensions = extension.parse(\n          'permessage-deflate; client_no_context_takeover'\n        );\n\n        assert.throws(\n          () => perMessageDeflate.accept(extensions['permessage-deflate']),\n          /^Error: Unexpected parameter \"client_no_context_takeover\"$/\n        );\n      });\n\n      it('throws an error if client_max_window_bits is unsupported', () => {\n        const perMessageDeflate = new PerMessageDeflate({\n          clientMaxWindowBits: false\n        });\n        const extensions = extension.parse(\n          'permessage-deflate; client_max_window_bits=10'\n        );\n\n        assert.throws(\n          () => perMessageDeflate.accept(extensions['permessage-deflate']),\n          /^Error: Unexpected or invalid parameter \"client_max_window_bits\"$/\n        );\n      });\n\n      it('throws an error if client_max_window_bits is greater than configuration', () => {\n        const perMessageDeflate = new PerMessageDeflate({\n          clientMaxWindowBits: 10\n        });\n        const extensions = extension.parse(\n          'permessage-deflate; client_max_window_bits=11'\n        );\n\n        assert.throws(\n          () => perMessageDeflate.accept(extensions['permessage-deflate']),\n          /^Error: Unexpected or invalid parameter \"client_max_window_bits\"$/\n        );\n      });\n\n      it('throws an error if client_max_window_bits has an invalid value', () => {\n        const perMessageDeflate = new PerMessageDeflate();\n\n        let extensions = extension.parse(\n          'permessage-deflate; client_max_window_bits=16'\n        );\n        assert.throws(\n          () => perMessageDeflate.accept(extensions['permessage-deflate']),\n          /^TypeError: Invalid value for parameter \"client_max_window_bits\": 16$/\n        );\n\n        extensions = extension.parse(\n          'permessage-deflate; client_max_window_bits'\n        );\n        assert.throws(\n          () => perMessageDeflate.accept(extensions['permessage-deflate']),\n          /^TypeError: Invalid value for parameter \"client_max_window_bits\": true$/\n        );\n      });\n\n      it('uses the config value if client_max_window_bits is not specified', () => {\n        const perMessageDeflate = new PerMessageDeflate({\n          clientMaxWindowBits: 10\n        });\n\n        assert.deepStrictEqual(perMessageDeflate.accept([{}]), {\n          client_max_window_bits: 10\n        });\n      });\n    });\n  });\n\n  describe('#compress and #decompress', () => {\n    it('works with unfragmented messages', (done) => {\n      const perMessageDeflate = new PerMessageDeflate();\n      const buf = Buffer.from([1, 2, 3]);\n\n      perMessageDeflate.accept([{}]);\n      perMessageDeflate.compress(buf, true, (err, data) => {\n        if (err) return done(err);\n\n        perMessageDeflate.decompress(data, true, (err, data) => {\n          if (err) return done(err);\n\n          assert.ok(data.equals(buf));\n          done();\n        });\n      });\n    });\n\n    it('works with fragmented messages', (done) => {\n      const perMessageDeflate = new PerMessageDeflate();\n      const buf = Buffer.from([1, 2, 3, 4]);\n\n      perMessageDeflate.accept([{}]);\n\n      perMessageDeflate.compress(buf.slice(0, 2), false, (err, compressed1) => {\n        if (err) return done(err);\n\n        perMessageDeflate.compress(buf.slice(2), true, (err, compressed2) => {\n          if (err) return done(err);\n\n          perMessageDeflate.decompress(compressed1, false, (err, data1) => {\n            if (err) return done(err);\n\n            perMessageDeflate.decompress(compressed2, true, (err, data2) => {\n              if (err) return done(err);\n\n              assert.ok(Buffer.concat([data1, data2]).equals(buf));\n              done();\n            });\n          });\n        });\n      });\n    });\n\n    it('works with the negotiated parameters', (done) => {\n      const perMessageDeflate = new PerMessageDeflate({\n        zlibDeflateOptions: {\n          memLevel: 5,\n          level: 9\n        }\n      });\n      const extensions = extension.parse(\n        'permessage-deflate; server_no_context_takeover; ' +\n          'client_no_context_takeover; server_max_window_bits=10; ' +\n          'client_max_window_bits=11'\n      );\n      const buf = Buffer.from(\"Some compressible data, it's compressible.\");\n\n      perMessageDeflate.accept(extensions['permessage-deflate']);\n\n      perMessageDeflate.compress(buf, true, (err, data) => {\n        if (err) return done(err);\n\n        perMessageDeflate.decompress(data, true, (err, data) => {\n          if (err) return done(err);\n\n          assert.ok(data.equals(buf));\n          done();\n        });\n      });\n    });\n\n    it('honors the `level` option', (done) => {\n      const lev0 = new PerMessageDeflate({\n        zlibDeflateOptions: { level: 0 }\n      });\n      const lev9 = new PerMessageDeflate({\n        zlibDeflateOptions: { level: 9 }\n      });\n      const extensionStr =\n        'permessage-deflate; server_no_context_takeover; ' +\n        'client_no_context_takeover; server_max_window_bits=10; ' +\n        'client_max_window_bits=11';\n      const buf = Buffer.from(\"Some compressible data, it's compressible.\");\n\n      lev0.accept(extension.parse(extensionStr)['permessage-deflate']);\n      lev9.accept(extension.parse(extensionStr)['permessage-deflate']);\n\n      lev0.compress(buf, true, (err, compressed1) => {\n        if (err) return done(err);\n\n        lev0.decompress(compressed1, true, (err, decompressed1) => {\n          if (err) return done(err);\n\n          lev9.compress(buf, true, (err, compressed2) => {\n            if (err) return done(err);\n\n            lev9.decompress(compressed2, true, (err, decompressed2) => {\n              if (err) return done(err);\n\n              // Level 0 compression actually adds a few bytes due to headers.\n              assert.ok(compressed1.length > buf.length);\n              // Level 9 should not, of course.\n              assert.ok(compressed2.length < buf.length);\n              // Ensure they both decompress back properly.\n              assert.ok(decompressed1.equals(buf));\n              assert.ok(decompressed2.equals(buf));\n              done();\n            });\n          });\n        });\n      });\n    });\n\n    it('honors the `zlib{Deflate,Inflate}Options` option', (done) => {\n      const lev0 = new PerMessageDeflate({\n        zlibDeflateOptions: {\n          level: 0,\n          chunkSize: 256\n        },\n        zlibInflateOptions: {\n          chunkSize: 2048\n        }\n      });\n      const lev9 = new PerMessageDeflate({\n        zlibDeflateOptions: {\n          level: 9,\n          chunkSize: 128\n        },\n        zlibInflateOptions: {\n          chunkSize: 1024\n        }\n      });\n\n      // Note no context takeover so we can get a hold of the raw streams after\n      // we do the dance.\n      const extensionStr =\n        'permessage-deflate; server_max_window_bits=10; ' +\n        'client_max_window_bits=11';\n      const buf = Buffer.from(\"Some compressible data, it's compressible.\");\n\n      lev0.accept(extension.parse(extensionStr)['permessage-deflate']);\n      lev9.accept(extension.parse(extensionStr)['permessage-deflate']);\n\n      lev0.compress(buf, true, (err, compressed1) => {\n        if (err) return done(err);\n\n        lev0.decompress(compressed1, true, (err, decompressed1) => {\n          if (err) return done(err);\n\n          lev9.compress(buf, true, (err, compressed2) => {\n            if (err) return done(err);\n\n            lev9.decompress(compressed2, true, (err, decompressed2) => {\n              if (err) return done(err);\n              // Level 0 compression actually adds a few bytes due to headers.\n              assert.ok(compressed1.length > buf.length);\n              // Level 9 should not, of course.\n              assert.ok(compressed2.length < buf.length);\n              // Ensure they both decompress back properly.\n              assert.ok(decompressed1.equals(buf));\n              assert.ok(decompressed2.equals(buf));\n\n              // Assert options were set.\n              assert.ok(lev0._deflate._level === 0);\n              assert.ok(lev9._deflate._level === 9);\n              assert.ok(lev0._deflate._chunkSize === 256);\n              assert.ok(lev9._deflate._chunkSize === 128);\n              assert.ok(lev0._inflate._chunkSize === 2048);\n              assert.ok(lev9._inflate._chunkSize === 1024);\n              done();\n            });\n          });\n        });\n      });\n    });\n\n    it(\"doesn't use contex takeover if not allowed\", (done) => {\n      const perMessageDeflate = new PerMessageDeflate({ isServer: true });\n      const extensions = extension.parse(\n        'permessage-deflate;server_no_context_takeover'\n      );\n      const buf = Buffer.from('foofoo');\n\n      perMessageDeflate.accept(extensions['permessage-deflate']);\n\n      perMessageDeflate.compress(buf, true, (err, compressed1) => {\n        if (err) return done(err);\n\n        perMessageDeflate.decompress(compressed1, true, (err, data) => {\n          if (err) return done(err);\n\n          assert.ok(data.equals(buf));\n          perMessageDeflate.compress(data, true, (err, compressed2) => {\n            if (err) return done(err);\n\n            assert.strictEqual(compressed2.length, compressed1.length);\n            perMessageDeflate.decompress(compressed2, true, (err, data) => {\n              if (err) return done(err);\n\n              assert.ok(data.equals(buf));\n              done();\n            });\n          });\n        });\n      });\n    });\n\n    it('uses contex takeover if allowed', (done) => {\n      const perMessageDeflate = new PerMessageDeflate({ isServer: true });\n      const extensions = extension.parse('permessage-deflate');\n      const buf = Buffer.from('foofoo');\n\n      perMessageDeflate.accept(extensions['permessage-deflate']);\n\n      perMessageDeflate.compress(buf, true, (err, compressed1) => {\n        if (err) return done(err);\n\n        perMessageDeflate.decompress(compressed1, true, (err, data) => {\n          if (err) return done(err);\n\n          assert.ok(data.equals(buf));\n          perMessageDeflate.compress(data, true, (err, compressed2) => {\n            if (err) return done(err);\n\n            assert.ok(compressed2.length < compressed1.length);\n            perMessageDeflate.decompress(compressed2, true, (err, data) => {\n              if (err) return done(err);\n\n              assert.ok(data.equals(buf));\n              done();\n            });\n          });\n        });\n      });\n    });\n\n    it('calls the callback when an error occurs (inflate)', (done) => {\n      const perMessageDeflate = new PerMessageDeflate();\n      const data = Buffer.from('something invalid');\n\n      perMessageDeflate.accept([{}]);\n      perMessageDeflate.decompress(data, true, (err) => {\n        assert.ok(err instanceof Error);\n        assert.strictEqual(err.code, 'Z_DATA_ERROR');\n        assert.strictEqual(err.errno, -3);\n        done();\n      });\n    });\n\n    it('calls the callback when `maxPayload` is exceeded (1/2)', (done) => {\n      const perMessageDeflate = new PerMessageDeflate({\n        isServer: false,\n        maxPayload: 25\n      });\n      const buf = Buffer.alloc(50, 'A');\n\n      perMessageDeflate.accept([{}]);\n      perMessageDeflate.compress(buf, true, (err, data) => {\n        if (err) return done(err);\n\n        perMessageDeflate.decompress(data, true, (err) => {\n          assert.ok(err instanceof RangeError);\n          assert.strictEqual(err.message, 'Max payload size exceeded');\n          done();\n        });\n      });\n    });\n\n    it('calls the callback when `maxPayload` is exceeded (2/2)', (done) => {\n      // A copy of the previous test but with a larger input. See\n      // https://github.com/websockets/ws/pull/2285.\n      const perMessageDeflate = new PerMessageDeflate({\n        isServer: false,\n        maxPayload: 25\n      });\n      const buf = Buffer.alloc(1024 * 1024, 'A');\n\n      perMessageDeflate.accept([{}]);\n      perMessageDeflate.compress(buf, true, (err, data) => {\n        if (err) return done(err);\n\n        perMessageDeflate.decompress(data, true, (err) => {\n          assert.ok(err instanceof RangeError);\n          assert.strictEqual(err.message, 'Max payload size exceeded');\n          done();\n        });\n      });\n    });\n\n    it('calls the callback if the deflate stream is closed prematurely', (done) => {\n      const perMessageDeflate = new PerMessageDeflate();\n      const buf = Buffer.from('A'.repeat(50));\n\n      perMessageDeflate.accept([{}]);\n      perMessageDeflate.compress(buf, true, (err) => {\n        assert.ok(err instanceof Error);\n        assert.strictEqual(\n          err.message,\n          'The deflate stream was closed while data was being processed'\n        );\n        done();\n      });\n\n      process.nextTick(() => perMessageDeflate.cleanup());\n    });\n\n    it('recreates the inflate stream if it ends', (done) => {\n      const perMessageDeflate = new PerMessageDeflate();\n      const extensions = extension.parse(\n        'permessage-deflate; client_no_context_takeover; ' +\n          'server_no_context_takeover'\n      );\n      const buf = Buffer.from('33343236313533b7000000', 'hex');\n      const expected = Buffer.from('12345678');\n\n      perMessageDeflate.accept(extensions['permessage-deflate']);\n\n      perMessageDeflate.decompress(buf, true, (err, data) => {\n        assert.ok(data.equals(expected));\n\n        perMessageDeflate.decompress(buf, true, (err, data) => {\n          assert.ok(data.equals(expected));\n          done();\n        });\n      });\n    });\n  });\n});\n"
  },
  {
    "path": "test/receiver.test.js",
    "content": "'use strict';\n\nconst assert = require('assert');\nconst crypto = require('crypto');\nconst EventEmitter = require('events');\n\nconst PerMessageDeflate = require('../lib/permessage-deflate');\nconst Receiver = require('../lib/receiver');\nconst Sender = require('../lib/sender');\nconst { EMPTY_BUFFER, hasBlob, kStatusCode } = require('../lib/constants');\n\ndescribe('Receiver', () => {\n  it('parses an unmasked text message', (done) => {\n    const receiver = new Receiver();\n\n    receiver.on('message', (data, isBinary) => {\n      assert.deepStrictEqual(data, Buffer.from('Hello'));\n      assert.ok(!isBinary);\n      done();\n    });\n\n    receiver.write(Buffer.from('810548656c6c6f', 'hex'));\n  });\n\n  it('parses a close message', (done) => {\n    const receiver = new Receiver();\n\n    receiver.on('conclude', (code, data) => {\n      assert.strictEqual(code, 1005);\n      assert.strictEqual(data, EMPTY_BUFFER);\n      done();\n    });\n\n    receiver.write(Buffer.from('8800', 'hex'));\n  });\n\n  it('parses a close message spanning multiple writes', (done) => {\n    const receiver = new Receiver();\n\n    receiver.on('conclude', (code, data) => {\n      assert.strictEqual(code, 1000);\n      assert.deepStrictEqual(data, Buffer.from('DONE'));\n      done();\n    });\n\n    receiver.write(Buffer.from('8806', 'hex'));\n    receiver.write(Buffer.from('03e8444F4E45', 'hex'));\n  });\n\n  it('parses a masked text message', (done) => {\n    const receiver = new Receiver({ isServer: true });\n\n    receiver.on('message', (data, isBinary) => {\n      assert.deepStrictEqual(data, Buffer.from('5:::{\"name\":\"echo\"}'));\n      assert.ok(!isBinary);\n      done();\n    });\n\n    receiver.write(\n      Buffer.from('81933483a86801b992524fa1c60959e68a5216e6cb005ba1d5', 'hex')\n    );\n  });\n\n  it('parses a masked text message longer than 125 B', (done) => {\n    const receiver = new Receiver({ isServer: true });\n    const msg = Buffer.from('A'.repeat(200));\n\n    const list = Sender.frame(msg, {\n      fin: true,\n      rsv1: false,\n      opcode: 0x01,\n      mask: true,\n      readOnly: true\n    });\n\n    const frame = Buffer.concat(list);\n\n    receiver.on('message', (data, isBinary) => {\n      assert.deepStrictEqual(data, msg);\n      assert.ok(!isBinary);\n      done();\n    });\n\n    receiver.write(frame.slice(0, 2));\n    setImmediate(() => receiver.write(frame.slice(2)));\n  });\n\n  it('parses a really long masked text message', (done) => {\n    const receiver = new Receiver({ isServer: true });\n    const msg = Buffer.from('A'.repeat(64 * 1024));\n\n    const list = Sender.frame(msg, {\n      fin: true,\n      rsv1: false,\n      opcode: 0x01,\n      mask: true,\n      readOnly: true\n    });\n\n    const frame = Buffer.concat(list);\n\n    receiver.on('message', (data, isBinary) => {\n      assert.deepStrictEqual(data, msg);\n      assert.ok(!isBinary);\n      done();\n    });\n\n    receiver.write(frame);\n  });\n\n  it('parses a 300 B fragmented masked text message', (done) => {\n    const receiver = new Receiver({ isServer: true });\n    const msg = Buffer.from('A'.repeat(300));\n\n    const fragment1 = msg.slice(0, 150);\n    const fragment2 = msg.slice(150);\n\n    const options = { rsv1: false, mask: true, readOnly: true };\n\n    const frame1 = Buffer.concat(\n      Sender.frame(fragment1, {\n        fin: false,\n        opcode: 0x01,\n        ...options\n      })\n    );\n    const frame2 = Buffer.concat(\n      Sender.frame(fragment2, {\n        fin: true,\n        opcode: 0x00,\n        ...options\n      })\n    );\n\n    receiver.on('message', (data, isBinary) => {\n      assert.deepStrictEqual(data, msg);\n      assert.ok(!isBinary);\n      done();\n    });\n\n    receiver.write(frame1);\n    receiver.write(frame2);\n  });\n\n  it('parses a ping message', (done) => {\n    const receiver = new Receiver({ isServer: true });\n    const msg = Buffer.from('Hello');\n\n    const list = Sender.frame(msg, {\n      fin: true,\n      rsv1: false,\n      opcode: 0x09,\n      mask: true,\n      readOnly: true\n    });\n\n    const frame = Buffer.concat(list);\n\n    receiver.on('ping', (data) => {\n      assert.deepStrictEqual(data, msg);\n      done();\n    });\n\n    receiver.write(frame);\n  });\n\n  it('parses a ping message with no data', (done) => {\n    const receiver = new Receiver();\n\n    receiver.on('ping', (data) => {\n      assert.strictEqual(data, EMPTY_BUFFER);\n      done();\n    });\n\n    receiver.write(Buffer.from('8900', 'hex'));\n  });\n\n  it('parses a 300 B fragmented masked text message with a ping in the middle (1/2)', (done) => {\n    const receiver = new Receiver({ isServer: true });\n    const msg = Buffer.from('A'.repeat(300));\n    const pingMessage = Buffer.from('Hello');\n\n    const fragment1 = msg.slice(0, 150);\n    const fragment2 = msg.slice(150);\n\n    const options = { rsv1: false, mask: true, readOnly: true };\n\n    const frame1 = Buffer.concat(\n      Sender.frame(fragment1, {\n        fin: false,\n        opcode: 0x01,\n        ...options\n      })\n    );\n    const frame2 = Buffer.concat(\n      Sender.frame(pingMessage, {\n        fin: true,\n        opcode: 0x09,\n        ...options\n      })\n    );\n    const frame3 = Buffer.concat(\n      Sender.frame(fragment2, {\n        fin: true,\n        opcode: 0x00,\n        ...options\n      })\n    );\n\n    let gotPing = false;\n\n    receiver.on('message', (data, isBinary) => {\n      assert.deepStrictEqual(data, msg);\n      assert.ok(!isBinary);\n      assert.ok(gotPing);\n      done();\n    });\n    receiver.on('ping', (data) => {\n      gotPing = true;\n      assert.ok(data.equals(pingMessage));\n    });\n\n    receiver.write(frame1);\n    receiver.write(frame2);\n    receiver.write(frame3);\n  });\n\n  it('parses a 300 B fragmented masked text message with a ping in the middle (2/2)', (done) => {\n    const receiver = new Receiver({ isServer: true });\n    const msg = Buffer.from('A'.repeat(300));\n    const pingMessage = Buffer.from('Hello');\n\n    const fragment1 = msg.slice(0, 150);\n    const fragment2 = msg.slice(150);\n\n    const options = { rsv1: false, mask: true, readOnly: false };\n\n    const frame1 = Buffer.concat(\n      Sender.frame(Buffer.from(fragment1), {\n        fin: false,\n        opcode: 0x01,\n        ...options\n      })\n    );\n    const frame2 = Buffer.concat(\n      Sender.frame(Buffer.from(pingMessage), {\n        fin: true,\n        opcode: 0x09,\n        ...options\n      })\n    );\n    const frame3 = Buffer.concat(\n      Sender.frame(Buffer.from(fragment2), {\n        fin: true,\n        opcode: 0x00,\n        ...options\n      })\n    );\n\n    let chunks = [];\n    const splitBuffer = (buf) => {\n      const i = Math.floor(buf.length / 2);\n      return [buf.slice(0, i), buf.slice(i)];\n    };\n\n    chunks = chunks.concat(splitBuffer(frame1));\n    chunks = chunks.concat(splitBuffer(frame2));\n    chunks = chunks.concat(splitBuffer(frame3));\n\n    let gotPing = false;\n\n    receiver.on('message', (data, isBinary) => {\n      assert.deepStrictEqual(data, msg);\n      assert.ok(!isBinary);\n      assert.ok(gotPing);\n      done();\n    });\n    receiver.on('ping', (data) => {\n      gotPing = true;\n      assert.ok(data.equals(pingMessage));\n    });\n\n    for (let i = 0; i < chunks.length; ++i) {\n      receiver.write(chunks[i]);\n    }\n  });\n\n  it('parses a 100 B masked binary message', (done) => {\n    const receiver = new Receiver({ isServer: true });\n    const msg = crypto.randomBytes(100);\n\n    const list = Sender.frame(msg, {\n      fin: true,\n      rsv1: false,\n      opcode: 0x02,\n      mask: true,\n      readOnly: true\n    });\n\n    const frame = Buffer.concat(list);\n\n    receiver.on('message', (data, isBinary) => {\n      assert.deepStrictEqual(data, msg);\n      assert.ok(isBinary);\n      done();\n    });\n\n    receiver.write(frame);\n  });\n\n  it('parses a 256 B masked binary message', (done) => {\n    const receiver = new Receiver({ isServer: true });\n    const msg = crypto.randomBytes(256);\n\n    const list = Sender.frame(msg, {\n      fin: true,\n      rsv1: false,\n      opcode: 0x02,\n      mask: true,\n      readOnly: true\n    });\n\n    const frame = Buffer.concat(list);\n\n    receiver.on('message', (data, isBinary) => {\n      assert.deepStrictEqual(data, msg);\n      assert.ok(isBinary);\n      done();\n    });\n\n    receiver.write(frame);\n  });\n\n  it('parses a 200 KiB masked binary message', (done) => {\n    const receiver = new Receiver({ isServer: true });\n    const msg = crypto.randomBytes(200 * 1024);\n\n    const list = Sender.frame(msg, {\n      fin: true,\n      rsv1: false,\n      opcode: 0x02,\n      mask: true,\n      readOnly: true\n    });\n\n    const frame = Buffer.concat(list);\n\n    receiver.on('message', (data, isBinary) => {\n      assert.deepStrictEqual(data, msg);\n      assert.ok(isBinary);\n      done();\n    });\n\n    receiver.write(frame);\n  });\n\n  it('parses a 200 KiB unmasked binary message', (done) => {\n    const receiver = new Receiver();\n    const msg = crypto.randomBytes(200 * 1024);\n\n    const list = Sender.frame(msg, {\n      fin: true,\n      rsv1: false,\n      opcode: 0x02,\n      mask: false,\n      readOnly: true\n    });\n\n    const frame = Buffer.concat(list);\n\n    receiver.on('message', (data, isBinary) => {\n      assert.deepStrictEqual(data, msg);\n      assert.ok(isBinary);\n      done();\n    });\n\n    receiver.write(frame);\n  });\n\n  it('parses a compressed message', (done) => {\n    const perMessageDeflate = new PerMessageDeflate();\n    perMessageDeflate.accept([{}]);\n\n    const receiver = new Receiver({\n      extensions: {\n        'permessage-deflate': perMessageDeflate\n      }\n    });\n    const buf = Buffer.from('Hello');\n\n    receiver.on('message', (data, isBinary) => {\n      assert.deepStrictEqual(data, buf);\n      assert.ok(!isBinary);\n      done();\n    });\n\n    perMessageDeflate.compress(buf, true, (err, data) => {\n      if (err) return done(err);\n\n      receiver.write(Buffer.from([0xc1, data.length]));\n      receiver.write(data);\n    });\n  });\n\n  it('parses a compressed and fragmented message', (done) => {\n    const perMessageDeflate = new PerMessageDeflate();\n    perMessageDeflate.accept([{}]);\n\n    const receiver = new Receiver({\n      extensions: {\n        'permessage-deflate': perMessageDeflate\n      }\n    });\n    const buf1 = Buffer.from('foo');\n    const buf2 = Buffer.from('bar');\n\n    receiver.on('message', (data, isBinary) => {\n      assert.deepStrictEqual(data, Buffer.concat([buf1, buf2]));\n      assert.ok(!isBinary);\n      done();\n    });\n\n    perMessageDeflate.compress(buf1, false, (err, fragment1) => {\n      if (err) return done(err);\n\n      receiver.write(Buffer.from([0x41, fragment1.length]));\n      receiver.write(fragment1);\n\n      perMessageDeflate.compress(buf2, true, (err, fragment2) => {\n        if (err) return done(err);\n\n        receiver.write(Buffer.from([0x80, fragment2.length]));\n        receiver.write(fragment2);\n      });\n    });\n  });\n\n  it('parses a buffer with thousands of frames', (done) => {\n    const buf = Buffer.allocUnsafe(40000);\n\n    for (let i = 0; i < buf.length; i += 2) {\n      buf[i] = 0x81;\n      buf[i + 1] = 0x00;\n    }\n\n    const receiver = new Receiver();\n    let counter = 0;\n\n    receiver.on('message', (data, isBinary) => {\n      assert.strictEqual(data, EMPTY_BUFFER);\n      assert.ok(!isBinary);\n      if (++counter === 20000) done();\n    });\n\n    receiver.write(buf);\n  });\n\n  it('resets `totalPayloadLength` only on final frame (unfragmented)', (done) => {\n    const receiver = new Receiver({ maxPayload: 10 });\n\n    receiver.on('message', (data, isBinary) => {\n      assert.strictEqual(receiver._totalPayloadLength, 0);\n      assert.deepStrictEqual(data, Buffer.from('Hello'));\n      assert.ok(!isBinary);\n      done();\n    });\n\n    assert.strictEqual(receiver._totalPayloadLength, 0);\n    receiver.write(Buffer.from('810548656c6c6f', 'hex'));\n  });\n\n  it('resets `totalPayloadLength` only on final frame (fragmented)', (done) => {\n    const receiver = new Receiver({ maxPayload: 10 });\n\n    receiver.on('message', (data, isBinary) => {\n      assert.strictEqual(receiver._totalPayloadLength, 0);\n      assert.deepStrictEqual(data, Buffer.from('Hello'));\n      assert.ok(!isBinary);\n      done();\n    });\n\n    assert.strictEqual(receiver._totalPayloadLength, 0);\n    receiver.write(Buffer.from('01024865', 'hex'));\n    assert.strictEqual(receiver._totalPayloadLength, 2);\n    receiver.write(Buffer.from('80036c6c6f', 'hex'));\n  });\n\n  it('resets `totalPayloadLength` only on final frame (fragmented + ping)', (done) => {\n    const receiver = new Receiver({ maxPayload: 10 });\n    let data;\n\n    receiver.on('ping', (buf) => {\n      assert.strictEqual(receiver._totalPayloadLength, 2);\n      data = buf;\n    });\n    receiver.on('message', (buf, isBinary) => {\n      assert.strictEqual(receiver._totalPayloadLength, 0);\n      assert.deepStrictEqual(data, EMPTY_BUFFER);\n      assert.deepStrictEqual(buf, Buffer.from('Hello'));\n      assert.ok(isBinary);\n      done();\n    });\n\n    assert.strictEqual(receiver._totalPayloadLength, 0);\n    receiver.write(Buffer.from('02024865', 'hex'));\n    receiver.write(Buffer.from('8900', 'hex'));\n    receiver.write(Buffer.from('80036c6c6f', 'hex'));\n  });\n\n  it('ignores any data after a close frame', (done) => {\n    const perMessageDeflate = new PerMessageDeflate();\n    perMessageDeflate.accept([{}]);\n\n    const receiver = new Receiver({\n      extensions: {\n        'permessage-deflate': perMessageDeflate\n      }\n    });\n    const results = [];\n    const push = results.push.bind(results);\n\n    receiver.on('conclude', push).on('message', push);\n    receiver.on('finish', () => {\n      assert.deepStrictEqual(results, [\n        EMPTY_BUFFER,\n        false,\n        1005,\n        EMPTY_BUFFER\n      ]);\n      done();\n    });\n\n    receiver.write(Buffer.from([0xc1, 0x01, 0x00]));\n    receiver.write(Buffer.from([0x88, 0x00]));\n    receiver.write(Buffer.from([0x81, 0x00]));\n  });\n\n  it('emits an error if RSV1 is on and permessage-deflate is disabled', (done) => {\n    const receiver = new Receiver();\n\n    receiver.on('error', (err) => {\n      assert.ok(err instanceof RangeError);\n      assert.strictEqual(err.code, 'WS_ERR_UNEXPECTED_RSV_1');\n      assert.strictEqual(\n        err.message,\n        'Invalid WebSocket frame: RSV1 must be clear'\n      );\n      assert.strictEqual(err[kStatusCode], 1002);\n      done();\n    });\n\n    receiver.write(Buffer.from([0xc2, 0x80, 0x00, 0x00, 0x00, 0x00]));\n  });\n\n  it('emits an error if RSV1 is on and opcode is 0', (done) => {\n    const perMessageDeflate = new PerMessageDeflate();\n    perMessageDeflate.accept([{}]);\n\n    const receiver = new Receiver({\n      extensions: {\n        'permessage-deflate': perMessageDeflate\n      }\n    });\n\n    receiver.on('error', (err) => {\n      assert.ok(err instanceof RangeError);\n      assert.strictEqual(err.code, 'WS_ERR_UNEXPECTED_RSV_1');\n      assert.strictEqual(\n        err.message,\n        'Invalid WebSocket frame: RSV1 must be clear'\n      );\n      assert.strictEqual(err[kStatusCode], 1002);\n      done();\n    });\n\n    receiver.write(Buffer.from([0x40, 0x00]));\n  });\n\n  it('emits an error if RSV2 is on', (done) => {\n    const receiver = new Receiver();\n\n    receiver.on('error', (err) => {\n      assert.ok(err instanceof RangeError);\n      assert.strictEqual(err.code, 'WS_ERR_UNEXPECTED_RSV_2_3');\n      assert.strictEqual(\n        err.message,\n        'Invalid WebSocket frame: RSV2 and RSV3 must be clear'\n      );\n      assert.strictEqual(err[kStatusCode], 1002);\n      done();\n    });\n\n    receiver.write(Buffer.from([0xa2, 0x00]));\n  });\n\n  it('emits an error if RSV3 is on', (done) => {\n    const receiver = new Receiver();\n\n    receiver.on('error', (err) => {\n      assert.ok(err instanceof RangeError);\n      assert.strictEqual(err.code, 'WS_ERR_UNEXPECTED_RSV_2_3');\n      assert.strictEqual(\n        err.message,\n        'Invalid WebSocket frame: RSV2 and RSV3 must be clear'\n      );\n      assert.strictEqual(err[kStatusCode], 1002);\n      done();\n    });\n\n    receiver.write(Buffer.from([0x92, 0x00]));\n  });\n\n  it('emits an error if the first frame in a fragmented message has opcode 0', (done) => {\n    const receiver = new Receiver();\n\n    receiver.on('error', (err) => {\n      assert.ok(err instanceof RangeError);\n      assert.strictEqual(err.code, 'WS_ERR_INVALID_OPCODE');\n      assert.strictEqual(\n        err.message,\n        'Invalid WebSocket frame: invalid opcode 0'\n      );\n      assert.strictEqual(err[kStatusCode], 1002);\n      done();\n    });\n\n    receiver.write(Buffer.from([0x00, 0x00]));\n  });\n\n  it('emits an error if a frame has opcode 1 in the middle of a fragmented message', (done) => {\n    const receiver = new Receiver();\n\n    receiver.on('error', (err) => {\n      assert.ok(err instanceof RangeError);\n      assert.strictEqual(err.code, 'WS_ERR_INVALID_OPCODE');\n      assert.strictEqual(\n        err.message,\n        'Invalid WebSocket frame: invalid opcode 1'\n      );\n      assert.strictEqual(err[kStatusCode], 1002);\n      done();\n    });\n\n    receiver.write(Buffer.from([0x01, 0x00]));\n    receiver.write(Buffer.from([0x01, 0x00]));\n  });\n\n  it('emits an error if a frame has opcode 2 in the middle of a fragmented message', (done) => {\n    const receiver = new Receiver();\n\n    receiver.on('error', (err) => {\n      assert.ok(err instanceof RangeError);\n      assert.strictEqual(err.code, 'WS_ERR_INVALID_OPCODE');\n      assert.strictEqual(\n        err.message,\n        'Invalid WebSocket frame: invalid opcode 2'\n      );\n      assert.strictEqual(err[kStatusCode], 1002);\n      done();\n    });\n\n    receiver.write(Buffer.from([0x01, 0x00]));\n    receiver.write(Buffer.from([0x02, 0x00]));\n  });\n\n  it('emits an error if a control frame has the FIN bit off', (done) => {\n    const receiver = new Receiver();\n\n    receiver.on('error', (err) => {\n      assert.ok(err instanceof RangeError);\n      assert.strictEqual(err.code, 'WS_ERR_EXPECTED_FIN');\n      assert.strictEqual(\n        err.message,\n        'Invalid WebSocket frame: FIN must be set'\n      );\n      assert.strictEqual(err[kStatusCode], 1002);\n      done();\n    });\n\n    receiver.write(Buffer.from([0x09, 0x00]));\n  });\n\n  it('emits an error if a control frame has the RSV1 bit on', (done) => {\n    const perMessageDeflate = new PerMessageDeflate();\n    perMessageDeflate.accept([{}]);\n\n    const receiver = new Receiver({\n      extensions: {\n        'permessage-deflate': perMessageDeflate\n      }\n    });\n\n    receiver.on('error', (err) => {\n      assert.ok(err instanceof RangeError);\n      assert.strictEqual(err.code, 'WS_ERR_UNEXPECTED_RSV_1');\n      assert.strictEqual(\n        err.message,\n        'Invalid WebSocket frame: RSV1 must be clear'\n      );\n      assert.strictEqual(err[kStatusCode], 1002);\n      done();\n    });\n\n    receiver.write(Buffer.from([0xc9, 0x00]));\n  });\n\n  it('emits an error if a control frame has the FIN bit off', (done) => {\n    const receiver = new Receiver();\n\n    receiver.on('error', (err) => {\n      assert.ok(err instanceof RangeError);\n      assert.strictEqual(err.code, 'WS_ERR_EXPECTED_FIN');\n      assert.strictEqual(\n        err.message,\n        'Invalid WebSocket frame: FIN must be set'\n      );\n      assert.strictEqual(err[kStatusCode], 1002);\n      done();\n    });\n\n    receiver.write(Buffer.from([0x09, 0x00]));\n  });\n\n  it('emits an error if a frame has the MASK bit off (server mode)', (done) => {\n    const receiver = new Receiver({ isServer: true });\n\n    receiver.on('error', (err) => {\n      assert.ok(err instanceof RangeError);\n      assert.strictEqual(err.code, 'WS_ERR_EXPECTED_MASK');\n      assert.strictEqual(\n        err.message,\n        'Invalid WebSocket frame: MASK must be set'\n      );\n      assert.strictEqual(err[kStatusCode], 1002);\n      done();\n    });\n\n    receiver.write(Buffer.from([0x81, 0x02, 0x68, 0x69]));\n  });\n\n  it('emits an error if a frame has the MASK bit on (client mode)', (done) => {\n    const receiver = new Receiver();\n\n    receiver.on('error', (err) => {\n      assert.ok(err instanceof RangeError);\n      assert.strictEqual(err.code, 'WS_ERR_UNEXPECTED_MASK');\n      assert.strictEqual(\n        err.message,\n        'Invalid WebSocket frame: MASK must be clear'\n      );\n      assert.strictEqual(err[kStatusCode], 1002);\n      done();\n    });\n\n    receiver.write(\n      Buffer.from([0x81, 0x82, 0x56, 0x3a, 0xac, 0x80, 0x3e, 0x53])\n    );\n  });\n\n  it('emits an error if a control frame has a payload bigger than 125 B', (done) => {\n    const receiver = new Receiver();\n\n    receiver.on('error', (err) => {\n      assert.ok(err instanceof RangeError);\n      assert.strictEqual(err.code, 'WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH');\n      assert.strictEqual(\n        err.message,\n        'Invalid WebSocket frame: invalid payload length 126'\n      );\n      assert.strictEqual(err[kStatusCode], 1002);\n      done();\n    });\n\n    receiver.write(Buffer.from([0x89, 0x7e]));\n  });\n\n  it('emits an error if a data frame has a payload bigger than 2^53 - 1 B', (done) => {\n    const receiver = new Receiver();\n\n    receiver.on('error', (err) => {\n      assert.ok(err instanceof RangeError);\n      assert.strictEqual(err.code, 'WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH');\n      assert.strictEqual(\n        err.message,\n        'Unsupported WebSocket frame: payload length > 2^53 - 1'\n      );\n      assert.strictEqual(err[kStatusCode], 1009);\n      done();\n    });\n\n    receiver.write(Buffer.from([0x82, 0x7f]));\n    setImmediate(() =>\n      receiver.write(\n        Buffer.from([0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])\n      )\n    );\n  });\n\n  it('emits an error if a text frame contains invalid UTF-8 data (1/2)', (done) => {\n    const receiver = new Receiver();\n\n    receiver.on('error', (err) => {\n      assert.ok(err instanceof Error);\n      assert.strictEqual(err.code, 'WS_ERR_INVALID_UTF8');\n      assert.strictEqual(\n        err.message,\n        'Invalid WebSocket frame: invalid UTF-8 sequence'\n      );\n      assert.strictEqual(err[kStatusCode], 1007);\n      done();\n    });\n\n    receiver.write(Buffer.from([0x81, 0x04, 0xce, 0xba, 0xe1, 0xbd]));\n  });\n\n  it('emits an error if a text frame contains invalid UTF-8 data (2/2)', (done) => {\n    const perMessageDeflate = new PerMessageDeflate();\n    perMessageDeflate.accept([{}]);\n\n    const receiver = new Receiver({\n      extensions: {\n        'permessage-deflate': perMessageDeflate\n      }\n    });\n    const buf = Buffer.from([0xce, 0xba, 0xe1, 0xbd]);\n\n    receiver.on('error', (err) => {\n      assert.ok(err instanceof Error);\n      assert.strictEqual(err.code, 'WS_ERR_INVALID_UTF8');\n      assert.strictEqual(\n        err.message,\n        'Invalid WebSocket frame: invalid UTF-8 sequence'\n      );\n      assert.strictEqual(err[kStatusCode], 1007);\n      done();\n    });\n\n    perMessageDeflate.compress(buf, true, (err, data) => {\n      if (err) return done(err);\n\n      receiver.write(Buffer.from([0xc1, data.length]));\n      receiver.write(data);\n    });\n  });\n\n  it('emits an error if a close frame has a payload of 1 B', (done) => {\n    const receiver = new Receiver();\n\n    receiver.on('error', (err) => {\n      assert.ok(err instanceof RangeError);\n      assert.strictEqual(err.code, 'WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH');\n      assert.strictEqual(\n        err.message,\n        'Invalid WebSocket frame: invalid payload length 1'\n      );\n      assert.strictEqual(err[kStatusCode], 1002);\n      done();\n    });\n\n    receiver.write(Buffer.from([0x88, 0x01]));\n  });\n\n  it('emits an error if a close frame contains an invalid close code', (done) => {\n    const receiver = new Receiver();\n\n    receiver.on('error', (err) => {\n      assert.ok(err instanceof RangeError);\n      assert.strictEqual(err.code, 'WS_ERR_INVALID_CLOSE_CODE');\n      assert.strictEqual(\n        err.message,\n        'Invalid WebSocket frame: invalid status code 0'\n      );\n      assert.strictEqual(err[kStatusCode], 1002);\n      done();\n    });\n\n    receiver.write(Buffer.from([0x88, 0x02, 0x00, 0x00]));\n  });\n\n  it('emits an error if a close frame contains invalid UTF-8 data', (done) => {\n    const receiver = new Receiver();\n\n    receiver.on('error', (err) => {\n      assert.ok(err instanceof Error);\n      assert.strictEqual(err.code, 'WS_ERR_INVALID_UTF8');\n      assert.strictEqual(\n        err.message,\n        'Invalid WebSocket frame: invalid UTF-8 sequence'\n      );\n      assert.strictEqual(err[kStatusCode], 1007);\n      done();\n    });\n\n    receiver.write(\n      Buffer.from([0x88, 0x06, 0x03, 0xef, 0xce, 0xba, 0xe1, 0xbd])\n    );\n  });\n\n  it('emits an error if a frame payload length is bigger than `maxPayload`', (done) => {\n    const receiver = new Receiver({ isServer: true, maxPayload: 20 * 1024 });\n    const msg = crypto.randomBytes(200 * 1024);\n\n    const list = Sender.frame(msg, {\n      fin: true,\n      rsv1: false,\n      opcode: 0x02,\n      mask: true,\n      readOnly: true\n    });\n\n    const frame = Buffer.concat(list);\n\n    receiver.on('error', (err) => {\n      assert.ok(err instanceof RangeError);\n      assert.strictEqual(err.code, 'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH');\n      assert.strictEqual(err.message, 'Max payload size exceeded');\n      assert.strictEqual(err[kStatusCode], 1009);\n      done();\n    });\n\n    receiver.write(frame);\n  });\n\n  it('emits an error if the message length exceeds `maxPayload`', (done) => {\n    const perMessageDeflate = new PerMessageDeflate({\n      isServer: false,\n      maxPayload: 25\n    });\n    perMessageDeflate.accept([{}]);\n\n    const receiver = new Receiver({\n      extensions: { 'permessage-deflate': perMessageDeflate },\n      isServer: false,\n      maxPayload: 25\n    });\n    const buf = Buffer.from('A'.repeat(50));\n\n    receiver.on('error', (err) => {\n      assert.ok(err instanceof RangeError);\n      assert.strictEqual(err.code, 'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH');\n      assert.strictEqual(err.message, 'Max payload size exceeded');\n      assert.strictEqual(err[kStatusCode], 1009);\n      done();\n    });\n\n    perMessageDeflate.compress(buf, true, (err, data) => {\n      if (err) return done(err);\n\n      receiver.write(Buffer.from([0xc1, data.length]));\n      receiver.write(data);\n    });\n  });\n\n  it('emits an error if the sum of fragment lengths exceeds `maxPayload`', (done) => {\n    const perMessageDeflate = new PerMessageDeflate({\n      isServer: false,\n      maxPayload: 25\n    });\n    perMessageDeflate.accept([{}]);\n\n    const receiver = new Receiver({\n      extensions: { 'permessage-deflate': perMessageDeflate },\n      isServer: false,\n      maxPayload: 25\n    });\n    const buf = Buffer.from('A'.repeat(15));\n\n    receiver.on('error', (err) => {\n      assert.ok(err instanceof RangeError);\n      assert.strictEqual(err.code, 'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH');\n      assert.strictEqual(err.message, 'Max payload size exceeded');\n      assert.strictEqual(err[kStatusCode], 1009);\n      done();\n    });\n\n    perMessageDeflate.compress(buf, false, (err, fragment1) => {\n      if (err) return done(err);\n\n      receiver.write(Buffer.from([0x41, fragment1.length]));\n      receiver.write(fragment1);\n\n      perMessageDeflate.compress(buf, true, (err, fragment2) => {\n        if (err) return done(err);\n\n        receiver.write(Buffer.from([0x80, fragment2.length]));\n        receiver.write(fragment2);\n      });\n    });\n  });\n\n  it(\"honors the 'nodebuffer' binary type\", (done) => {\n    const receiver = new Receiver();\n    const frags = [\n      crypto.randomBytes(7321),\n      crypto.randomBytes(137),\n      crypto.randomBytes(285787),\n      crypto.randomBytes(3)\n    ];\n\n    receiver.on('message', (data, isBinary) => {\n      assert.deepStrictEqual(data, Buffer.concat(frags));\n      assert.ok(isBinary);\n      done();\n    });\n\n    frags.forEach((frag, i) => {\n      Sender.frame(frag, {\n        fin: i === frags.length - 1,\n        opcode: i === 0 ? 2 : 0,\n        readOnly: true,\n        mask: false,\n        rsv1: false\n      }).forEach((buf) => receiver.write(buf));\n    });\n  });\n\n  it(\"honors the 'arraybuffer' binary type\", (done) => {\n    const receiver = new Receiver({ binaryType: 'arraybuffer' });\n    const frags = [\n      crypto.randomBytes(19221),\n      crypto.randomBytes(954),\n      crypto.randomBytes(623987)\n    ];\n\n    receiver.on('message', (data, isBinary) => {\n      assert.ok(data instanceof ArrayBuffer);\n      assert.deepStrictEqual(Buffer.from(data), Buffer.concat(frags));\n      assert.ok(isBinary);\n      done();\n    });\n\n    frags.forEach((frag, i) => {\n      Sender.frame(frag, {\n        fin: i === frags.length - 1,\n        opcode: i === 0 ? 2 : 0,\n        readOnly: true,\n        mask: false,\n        rsv1: false\n      }).forEach((buf) => receiver.write(buf));\n    });\n  });\n\n  it(\"honors the 'fragments' binary type\", (done) => {\n    const receiver = new Receiver({ binaryType: 'fragments' });\n    const frags = [\n      crypto.randomBytes(17),\n      crypto.randomBytes(419872),\n      crypto.randomBytes(83),\n      crypto.randomBytes(9928),\n      crypto.randomBytes(1)\n    ];\n\n    receiver.on('message', (data, isBinary) => {\n      assert.deepStrictEqual(data, frags);\n      assert.ok(isBinary);\n      done();\n    });\n\n    frags.forEach((frag, i) => {\n      Sender.frame(frag, {\n        fin: i === frags.length - 1,\n        opcode: i === 0 ? 2 : 0,\n        readOnly: true,\n        mask: false,\n        rsv1: false\n      }).forEach((buf) => receiver.write(buf));\n    });\n  });\n\n  it(\"honors the 'blob' binary type\", function (done) {\n    if (!hasBlob) return this.skip();\n\n    const receiver = new Receiver({ binaryType: 'blob' });\n    const frags = [\n      crypto.randomBytes(75688),\n      crypto.randomBytes(2688),\n      crypto.randomBytes(46753)\n    ];\n\n    receiver.on('message', (data, isBinary) => {\n      assert.ok(data instanceof Blob);\n      assert.ok(isBinary);\n\n      data\n        .arrayBuffer()\n        .then((arrayBuffer) => {\n          assert.deepStrictEqual(\n            Buffer.from(arrayBuffer),\n            Buffer.concat(frags)\n          );\n\n          done();\n        })\n        .catch(done);\n    });\n\n    frags.forEach((frag, i) => {\n      Sender.frame(frag, {\n        fin: i === frags.length - 1,\n        opcode: i === 0 ? 2 : 0,\n        readOnly: true,\n        mask: false,\n        rsv1: false\n      }).forEach((buf) => receiver.write(buf));\n    });\n  });\n\n  it('honors the `skipUTF8Validation` option (1/2)', (done) => {\n    const receiver = new Receiver({ skipUTF8Validation: true });\n\n    receiver.on('message', (data, isBinary) => {\n      assert.deepStrictEqual(data, Buffer.from([0xf8]));\n      assert.ok(!isBinary);\n      done();\n    });\n\n    receiver.write(Buffer.from([0x81, 0x01, 0xf8]));\n  });\n\n  it('honors the `skipUTF8Validation` option (2/2)', (done) => {\n    const receiver = new Receiver({ skipUTF8Validation: true });\n\n    receiver.on('conclude', (code, data) => {\n      assert.strictEqual(code, 1000);\n      assert.deepStrictEqual(data, Buffer.from([0xf8]));\n      done();\n    });\n\n    receiver.write(Buffer.from([0x88, 0x03, 0x03, 0xe8, 0xf8]));\n  });\n\n  it('honors the `allowSynchronousEvents` option', (done) => {\n    const actual = [];\n    const expected = [\n      '1',\n      '- 1',\n      '-- 1',\n      '2',\n      '- 2',\n      '-- 2',\n      '3',\n      '- 3',\n      '-- 3',\n      '4',\n      '- 4',\n      '-- 4'\n    ];\n\n    function listener(data) {\n      const message = data.toString();\n      actual.push(message);\n\n      // `queueMicrotask()` is not available in Node.js < 11.\n      Promise.resolve().then(() => {\n        actual.push(`- ${message}`);\n\n        Promise.resolve().then(() => {\n          actual.push(`-- ${message}`);\n\n          if (actual.length === 12) {\n            assert.deepStrictEqual(actual, expected);\n            done();\n          }\n        });\n      });\n    }\n\n    const receiver = new Receiver({ allowSynchronousEvents: false });\n\n    receiver.on('message', listener);\n    receiver.on('ping', listener);\n    receiver.on('pong', listener);\n\n    receiver.write(Buffer.from('8101318901328a0133820134', 'hex'));\n  });\n\n  it('does not swallow errors thrown from event handlers', (done) => {\n    const receiver = new Receiver();\n    let count = 0;\n\n    receiver.on('message', () => {\n      if (++count === 2) {\n        throw new Error('Oops');\n      }\n    });\n\n    assert.strictEqual(\n      process.listenerCount('uncaughtException'),\n      EventEmitter.usingDomains ? 2 : 1\n    );\n\n    const listener = process.listeners('uncaughtException').pop();\n\n    process.removeListener('uncaughtException', listener);\n    process.once('uncaughtException', (err) => {\n      assert.ok(err instanceof Error);\n      assert.strictEqual(err.message, 'Oops');\n\n      process.on('uncaughtException', listener);\n      done();\n    });\n\n    setImmediate(() => {\n      receiver.write(Buffer.from('82008200', 'hex'));\n    });\n  });\n});\n"
  },
  {
    "path": "test/sender.test.js",
    "content": "'use strict';\n\nconst assert = require('assert');\n\nconst extension = require('../lib/extension');\nconst PerMessageDeflate = require('../lib/permessage-deflate');\nconst Sender = require('../lib/sender');\nconst { EMPTY_BUFFER, hasBlob } = require('../lib/constants');\n\nclass MockSocket {\n  constructor({ write } = {}) {\n    this.readable = true;\n    this.writable = true;\n\n    if (write) this.write = write;\n  }\n\n  cork() {}\n  write() {}\n  uncork() {}\n}\n\ndescribe('Sender', () => {\n  describe('.frame', () => {\n    it('does not mutate the input buffer if data is `readOnly`', () => {\n      const buf = Buffer.from([1, 2, 3, 4, 5]);\n\n      Sender.frame(buf, {\n        readOnly: true,\n        rsv1: false,\n        mask: true,\n        opcode: 2,\n        fin: true\n      });\n\n      assert.ok(buf.equals(Buffer.from([1, 2, 3, 4, 5])));\n    });\n\n    it('honors the `rsv1` option', () => {\n      const list = Sender.frame(EMPTY_BUFFER, {\n        readOnly: false,\n        mask: false,\n        rsv1: true,\n        opcode: 1,\n        fin: true\n      });\n\n      assert.strictEqual(list[0][0] & 0x40, 0x40);\n    });\n\n    it('accepts a string as first argument', () => {\n      const list = Sender.frame('€', {\n        readOnly: false,\n        rsv1: false,\n        mask: false,\n        opcode: 1,\n        fin: true\n      });\n\n      assert.deepStrictEqual(list[0], Buffer.from('8103', 'hex'));\n      assert.deepStrictEqual(list[1], Buffer.from('e282ac', 'hex'));\n    });\n  });\n\n  describe('#send', () => {\n    it('compresses data if compress option is enabled', (done) => {\n      const chunks = [];\n      const perMessageDeflate = new PerMessageDeflate();\n      const mockSocket = new MockSocket({\n        write: (chunk) => {\n          chunks.push(chunk);\n          if (chunks.length !== 6) return;\n\n          assert.strictEqual(chunks[0].length, 2);\n          assert.strictEqual(chunks[0][0] & 0x40, 0x40);\n\n          assert.strictEqual(chunks[2].length, 2);\n          assert.strictEqual(chunks[2][0] & 0x40, 0x40);\n\n          assert.strictEqual(chunks[4].length, 2);\n          assert.strictEqual(chunks[4][0] & 0x40, 0x40);\n          done();\n        }\n      });\n      const sender = new Sender(mockSocket, {\n        'permessage-deflate': perMessageDeflate\n      });\n\n      perMessageDeflate.accept([{}]);\n\n      const options = { compress: true, fin: true };\n      const array = new Uint8Array([0x68, 0x69]);\n\n      sender.send(array.buffer, options);\n      sender.send(array, options);\n      sender.send('hi', options);\n    });\n\n    describe('when context takeover is disabled', () => {\n      it('honors the compression threshold', (done) => {\n        const chunks = [];\n        const perMessageDeflate = new PerMessageDeflate();\n        const mockSocket = new MockSocket({\n          write: (chunk) => {\n            chunks.push(chunk);\n            if (chunks.length !== 2) return;\n\n            assert.strictEqual(chunks[0].length, 2);\n            assert.notStrictEqual(chunk[0][0] & 0x40, 0x40);\n            assert.strictEqual(chunks[1], 'hi');\n            done();\n          }\n        });\n        const sender = new Sender(mockSocket, {\n          'permessage-deflate': perMessageDeflate\n        });\n        const extensions = extension.parse(\n          'permessage-deflate; client_no_context_takeover'\n        );\n\n        perMessageDeflate.accept(extensions['permessage-deflate']);\n\n        sender.send('hi', { compress: true, fin: true });\n      });\n\n      it('compresses all fragments of a fragmented message', (done) => {\n        const chunks = [];\n        const perMessageDeflate = new PerMessageDeflate({ threshold: 3 });\n        const mockSocket = new MockSocket({\n          write: (chunk) => {\n            chunks.push(chunk);\n            if (chunks.length !== 4) return;\n\n            assert.strictEqual(chunks[0].length, 2);\n            assert.strictEqual(chunks[0][0] & 0x40, 0x40);\n            assert.strictEqual(chunks[1].length, 9);\n\n            assert.strictEqual(chunks[2].length, 2);\n            assert.strictEqual(chunks[2][0] & 0x40, 0x00);\n            assert.strictEqual(chunks[3].length, 4);\n            done();\n          }\n        });\n        const sender = new Sender(mockSocket, {\n          'permessage-deflate': perMessageDeflate\n        });\n        const extensions = extension.parse(\n          'permessage-deflate; client_no_context_takeover'\n        );\n\n        perMessageDeflate.accept(extensions['permessage-deflate']);\n\n        sender.send('123', { compress: true, fin: false });\n        sender.send('12', { compress: true, fin: true });\n      });\n\n      it('does not compress any fragments of a fragmented message', (done) => {\n        const chunks = [];\n        const perMessageDeflate = new PerMessageDeflate({ threshold: 3 });\n        const mockSocket = new MockSocket({\n          write: (chunk) => {\n            chunks.push(chunk);\n            if (chunks.length !== 4) return;\n\n            assert.strictEqual(chunks[0].length, 2);\n            assert.strictEqual(chunks[0][0] & 0x40, 0x00);\n            assert.strictEqual(chunks[1].length, 2);\n\n            assert.strictEqual(chunks[2].length, 2);\n            assert.strictEqual(chunks[2][0] & 0x40, 0x00);\n            assert.strictEqual(chunks[3].length, 3);\n            done();\n          }\n        });\n        const sender = new Sender(mockSocket, {\n          'permessage-deflate': perMessageDeflate\n        });\n        const extensions = extension.parse(\n          'permessage-deflate; client_no_context_takeover'\n        );\n\n        perMessageDeflate.accept(extensions['permessage-deflate']);\n\n        sender.send('12', { compress: true, fin: false });\n        sender.send('123', { compress: true, fin: true });\n      });\n\n      it('compresses empty buffer as first fragment', (done) => {\n        const chunks = [];\n        const perMessageDeflate = new PerMessageDeflate({ threshold: 0 });\n        const mockSocket = new MockSocket({\n          write: (chunk) => {\n            chunks.push(chunk);\n            if (chunks.length !== 4) return;\n\n            assert.strictEqual(chunks[0].length, 2);\n            assert.strictEqual(chunks[0][0] & 0x40, 0x40);\n            assert.strictEqual(chunks[1].length, 5);\n\n            assert.strictEqual(chunks[2].length, 2);\n            assert.strictEqual(chunks[2][0] & 0x40, 0x00);\n            assert.strictEqual(chunks[3].length, 6);\n            done();\n          }\n        });\n        const sender = new Sender(mockSocket, {\n          'permessage-deflate': perMessageDeflate\n        });\n        const extensions = extension.parse(\n          'permessage-deflate; client_no_context_takeover'\n        );\n\n        perMessageDeflate.accept(extensions['permessage-deflate']);\n\n        sender.send(Buffer.alloc(0), { compress: true, fin: false });\n        sender.send('data', { compress: true, fin: true });\n      });\n\n      it('compresses empty buffer as last fragment', (done) => {\n        const chunks = [];\n        const perMessageDeflate = new PerMessageDeflate({ threshold: 0 });\n        const mockSocket = new MockSocket({\n          write: (chunk) => {\n            chunks.push(chunk);\n            if (chunks.length !== 4) return;\n\n            assert.strictEqual(chunks[0].length, 2);\n            assert.strictEqual(chunks[0][0] & 0x40, 0x40);\n            assert.strictEqual(chunks[1].length, 10);\n\n            assert.strictEqual(chunks[2].length, 2);\n            assert.strictEqual(chunks[2][0] & 0x40, 0x00);\n            assert.strictEqual(chunks[3].length, 1);\n            done();\n          }\n        });\n        const sender = new Sender(mockSocket, {\n          'permessage-deflate': perMessageDeflate\n        });\n        const extensions = extension.parse(\n          'permessage-deflate; client_no_context_takeover'\n        );\n\n        perMessageDeflate.accept(extensions['permessage-deflate']);\n\n        sender.send('data', { compress: true, fin: false });\n        sender.send(Buffer.alloc(0), { compress: true, fin: true });\n      });\n    });\n  });\n\n  describe('#ping', () => {\n    it('can send a string as ping payload', (done) => {\n      const perMessageDeflate = new PerMessageDeflate();\n      let count = 0;\n      const mockSocket = new MockSocket({\n        write: (data) => {\n          if (++count < 3) return;\n\n          if (count === 3) {\n            assert.deepStrictEqual(data, Buffer.from([0x89, 0x02]));\n          } else {\n            assert.strictEqual(data, 'hi');\n            done();\n          }\n        }\n      });\n      const sender = new Sender(mockSocket, {\n        'permessage-deflate': perMessageDeflate\n      });\n\n      perMessageDeflate.accept([{}]);\n\n      sender.send('foo', { compress: true, fin: true });\n      sender.ping('hi', false);\n    });\n\n    it('can send a `TypedArray` as ping payload', (done) => {\n      let count = 0;\n      const mockSocket = new MockSocket({\n        write: (data) => {\n          if (++count === 1) {\n            assert.deepStrictEqual(data, Buffer.from([0x89, 0x02]));\n          } else {\n            assert.deepStrictEqual(data, Buffer.from([0x68, 0x69]));\n            done();\n          }\n        }\n      });\n\n      const sender = new Sender(mockSocket);\n      const array = new Uint8Array([0x68, 0x69]);\n\n      sender.ping(array, false);\n    });\n\n    it('can send an `ArrayBuffer` as ping payload', (done) => {\n      let count = 0;\n      const mockSocket = new MockSocket({\n        write: (data) => {\n          if (++count === 1) {\n            assert.deepStrictEqual(data, Buffer.from([0x89, 0x02]));\n          } else {\n            assert.deepStrictEqual(data, Buffer.from([0x68, 0x69]));\n            done();\n          }\n        }\n      });\n\n      const sender = new Sender(mockSocket);\n      const array = new Uint8Array([0x68, 0x69]);\n\n      sender.ping(array.buffer, false);\n    });\n\n    it('can send a `Blob` as ping payload', function (done) {\n      if (!hasBlob) return this.skip();\n\n      let count = 0;\n      const mockSocket = new MockSocket({\n        write: (data) => {\n          if (++count % 2) {\n            assert.deepStrictEqual(data, Buffer.from([0x89, 0x02]));\n          } else {\n            assert.deepStrictEqual(data, Buffer.from([0x68, 0x69]));\n            if (count === 4) done();\n          }\n        }\n      });\n\n      const sender = new Sender(mockSocket);\n      const blob = new Blob(['hi']);\n\n      sender.ping(blob, false);\n      sender.ping(blob, false);\n    });\n  });\n\n  describe('#pong', () => {\n    it('can send a string as ping payload', (done) => {\n      const perMessageDeflate = new PerMessageDeflate();\n      let count = 0;\n      const mockSocket = new MockSocket({\n        write: (data) => {\n          if (++count < 3) return;\n\n          if (count === 3) {\n            assert.deepStrictEqual(data, Buffer.from([0x8a, 0x02]));\n          } else {\n            assert.strictEqual(data, 'hi');\n            done();\n          }\n        }\n      });\n      const sender = new Sender(mockSocket, {\n        'permessage-deflate': perMessageDeflate\n      });\n\n      perMessageDeflate.accept([{}]);\n\n      sender.send('foo', { compress: true, fin: true });\n      sender.pong('hi', false);\n    });\n\n    it('can send a `TypedArray` as ping payload', (done) => {\n      let count = 0;\n      const mockSocket = new MockSocket({\n        write: (data) => {\n          if (++count === 1) {\n            assert.deepStrictEqual(data, Buffer.from([0x8a, 0x02]));\n          } else {\n            assert.deepStrictEqual(data, Buffer.from([0x68, 0x69]));\n            done();\n          }\n        }\n      });\n\n      const sender = new Sender(mockSocket);\n      const array = new Uint8Array([0x68, 0x69]);\n\n      sender.pong(array, false);\n    });\n\n    it('can send an `ArrayBuffer` as ping payload', (done) => {\n      let count = 0;\n      const mockSocket = new MockSocket({\n        write: (data) => {\n          if (++count === 1) {\n            assert.deepStrictEqual(data, Buffer.from([0x8a, 0x02]));\n          } else {\n            assert.deepStrictEqual(data, Buffer.from([0x68, 0x69]));\n            done();\n          }\n        }\n      });\n\n      const sender = new Sender(mockSocket);\n      const array = new Uint8Array([0x68, 0x69]);\n\n      sender.pong(array.buffer, false);\n    });\n\n    it('can send a `Blob` as ping payload', function (done) {\n      if (!hasBlob) return this.skip();\n\n      let count = 0;\n      const mockSocket = new MockSocket({\n        write: (data) => {\n          if (++count % 2) {\n            assert.deepStrictEqual(data, Buffer.from([0x8a, 0x02]));\n          } else {\n            assert.deepStrictEqual(data, Buffer.from([0x68, 0x69]));\n            if (count === 4) done();\n          }\n        }\n      });\n\n      const sender = new Sender(mockSocket);\n      const blob = new Blob(['hi']);\n\n      sender.pong(blob, false);\n      sender.pong(blob, false);\n    });\n  });\n\n  describe('#close', () => {\n    it('throws an error if the first argument is invalid', () => {\n      const mockSocket = new MockSocket();\n      const sender = new Sender(mockSocket);\n\n      assert.throws(\n        () => sender.close('error'),\n        /^TypeError: First argument must be a valid error code number$/\n      );\n\n      assert.throws(\n        () => sender.close(1004),\n        /^TypeError: First argument must be a valid error code number$/\n      );\n    });\n\n    it('throws an error if the message is greater than 123 bytes', () => {\n      const mockSocket = new MockSocket();\n      const sender = new Sender(mockSocket);\n\n      assert.throws(\n        () => sender.close(1000, 'a'.repeat(124)),\n        /^RangeError: The message must not be greater than 123 bytes$/\n      );\n    });\n\n    it('should consume all data before closing', (done) => {\n      const perMessageDeflate = new PerMessageDeflate();\n\n      let count = 0;\n      const mockSocket = new MockSocket({\n        write: (data, cb) => {\n          count++;\n          if (cb) cb();\n        }\n      });\n      const sender = new Sender(mockSocket, {\n        'permessage-deflate': perMessageDeflate\n      });\n\n      perMessageDeflate.accept([{}]);\n\n      sender.send('foo', { compress: true, fin: true });\n      sender.send('bar', { compress: true, fin: true });\n      sender.send('baz', { compress: true, fin: true });\n\n      sender.close(1000, undefined, false, () => {\n        assert.strictEqual(count, 8);\n        done();\n      });\n    });\n  });\n});\n"
  },
  {
    "path": "test/subprotocol.test.js",
    "content": "'use strict';\n\nconst assert = require('assert');\n\nconst { parse } = require('../lib/subprotocol');\n\ndescribe('subprotocol', () => {\n  describe('parse', () => {\n    it('parses a single subprotocol', () => {\n      assert.deepStrictEqual(parse('foo'), new Set(['foo']));\n    });\n\n    it('parses multiple subprotocols', () => {\n      assert.deepStrictEqual(\n        parse('foo,bar,baz'),\n        new Set(['foo', 'bar', 'baz'])\n      );\n    });\n\n    it('ignores the optional white spaces', () => {\n      const header = 'foo , bar\\t, \\tbaz\\t ,  qux\\t\\t,norf';\n\n      assert.deepStrictEqual(\n        parse(header),\n        new Set(['foo', 'bar', 'baz', 'qux', 'norf'])\n      );\n    });\n\n    it('throws an error if a subprotocol is empty', () => {\n      [\n        [',', 0],\n        ['foo,,', 4],\n        ['foo,  ,', 6]\n      ].forEach((element) => {\n        assert.throws(\n          () => parse(element[0]),\n          new RegExp(\n            `^SyntaxError: Unexpected character at index ${element[1]}$`\n          )\n        );\n      });\n    });\n\n    it('throws an error if a subprotocol is duplicated', () => {\n      ['foo,foo,bar', 'foo,bar,foo'].forEach((header) => {\n        assert.throws(\n          () => parse(header),\n          /^SyntaxError: The \"foo\" subprotocol is duplicated$/\n        );\n      });\n    });\n\n    it('throws an error if a white space is misplaced', () => {\n      [\n        ['f oo', 2],\n        [' foo', 0]\n      ].forEach((element) => {\n        assert.throws(\n          () => parse(element[0]),\n          new RegExp(\n            `^SyntaxError: Unexpected character at index ${element[1]}$`\n          )\n        );\n      });\n    });\n\n    it('throws an error if a subprotocol contains invalid characters', () => {\n      [\n        ['f@o', 1],\n        ['f\\\\oo', 1],\n        ['foo,b@r', 5]\n      ].forEach((element) => {\n        assert.throws(\n          () => parse(element[0]),\n          new RegExp(\n            `^SyntaxError: Unexpected character at index ${element[1]}$`\n          )\n        );\n      });\n    });\n\n    it('throws an error if the header value ends prematurely', () => {\n      ['foo ', 'foo, ', 'foo,bar ', 'foo,bar,'].forEach((header) => {\n        assert.throws(\n          () => parse(header),\n          /^SyntaxError: Unexpected end of input$/\n        );\n      });\n    });\n  });\n});\n"
  },
  {
    "path": "test/validation.test.js",
    "content": "'use strict';\n\nconst assert = require('assert');\n\nconst { isValidUTF8 } = require('../lib/validation');\n\ndescribe('extension', () => {\n  describe('isValidUTF8', () => {\n    it('returns false if it finds invalid bytes', () => {\n      assert.strictEqual(isValidUTF8(Buffer.from([0xf8])), false);\n    });\n\n    it('returns false for overlong encodings', () => {\n      assert.strictEqual(isValidUTF8(Buffer.from([0xc0, 0xa0])), false);\n      assert.strictEqual(isValidUTF8(Buffer.from([0xe0, 0x80, 0xa0])), false);\n      assert.strictEqual(\n        isValidUTF8(Buffer.from([0xf0, 0x80, 0x80, 0xa0])),\n        false\n      );\n    });\n\n    it('returns false for code points in the range U+D800 - U+DFFF', () => {\n      for (let i = 0xa0; i < 0xc0; i++) {\n        for (let j = 0x80; j < 0xc0; j++) {\n          assert.strictEqual(isValidUTF8(Buffer.from([0xed, i, j])), false);\n        }\n      }\n    });\n\n    it('returns false for code points greater than U+10FFFF', () => {\n      assert.strictEqual(\n        isValidUTF8(Buffer.from([0xf4, 0x90, 0x80, 0x80])),\n        false\n      );\n      assert.strictEqual(\n        isValidUTF8(Buffer.from([0xf5, 0x80, 0x80, 0x80])),\n        false\n      );\n    });\n\n    it('returns true for a well-formed UTF-8 byte sequence', () => {\n      // prettier-ignore\n      const buf = Buffer.from([\n        0xe2, 0x82, 0xAC, // €\n        0xf0, 0x90, 0x8c, 0x88, // 𐍈\n        0x24 // $\n      ]);\n\n      assert.strictEqual(isValidUTF8(buf), true);\n    });\n  });\n});\n"
  },
  {
    "path": "test/websocket-server.test.js",
    "content": "/* eslint no-unused-vars: [\"error\", { \"varsIgnorePattern\": \"^ws$\" }] */\n\n'use strict';\n\nconst assert = require('assert');\nconst crypto = require('crypto');\nconst https = require('https');\nconst http = require('http');\nconst path = require('path');\nconst net = require('net');\nconst fs = require('fs');\nconst os = require('os');\n\nconst makeDuplexPair = require('./duplex-pair');\nconst Sender = require('../lib/sender');\nconst WebSocket = require('..');\nconst { NOOP } = require('../lib/constants');\n\ndescribe('WebSocketServer', () => {\n  describe('#ctor', () => {\n    it('throws an error if no option object is passed', () => {\n      assert.throws(\n        () => new WebSocket.Server(),\n        new RegExp(\n          '^TypeError: One and only one of the \"port\", \"server\", or ' +\n            '\"noServer\" options must be specified$'\n        )\n      );\n    });\n\n    describe('options', () => {\n      it('throws an error if required options are not specified', () => {\n        assert.throws(\n          () => new WebSocket.Server({}),\n          new RegExp(\n            '^TypeError: One and only one of the \"port\", \"server\", or ' +\n              '\"noServer\" options must be specified$'\n          )\n        );\n      });\n\n      it('throws an error if mutually exclusive options are specified', () => {\n        const server = http.createServer();\n        const variants = [\n          { port: 0, noServer: true, server },\n          { port: 0, noServer: true },\n          { port: 0, server },\n          { noServer: true, server }\n        ];\n\n        for (const options of variants) {\n          assert.throws(\n            () => new WebSocket.Server(options),\n            new RegExp(\n              '^TypeError: One and only one of the \"port\", \"server\", or ' +\n                '\"noServer\" options must be specified$'\n            )\n          );\n        }\n      });\n\n      it('exposes options passed to constructor', (done) => {\n        const wss = new WebSocket.Server({ port: 0 }, () => {\n          assert.strictEqual(wss.options.port, 0);\n          wss.close(done);\n        });\n      });\n\n      it('accepts the `maxPayload` option', (done) => {\n        const maxPayload = 20480;\n        const wss = new WebSocket.Server(\n          {\n            perMessageDeflate: true,\n            maxPayload,\n            port: 0\n          },\n          () => {\n            const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n            ws.on('open', ws.close);\n          }\n        );\n\n        wss.on('connection', (ws) => {\n          assert.strictEqual(ws._receiver._maxPayload, maxPayload);\n          assert.strictEqual(\n            ws._receiver._extensions['permessage-deflate']._maxPayload,\n            maxPayload\n          );\n          wss.close(done);\n        });\n      });\n\n      it('honors the `WebSocket` option', (done) => {\n        class CustomWebSocket extends WebSocket.WebSocket {\n          get foo() {\n            return 'foo';\n          }\n        }\n\n        const wss = new WebSocket.Server(\n          {\n            port: 0,\n            WebSocket: CustomWebSocket\n          },\n          () => {\n            const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n            ws.on('open', ws.close);\n          }\n        );\n\n        wss.on('connection', (ws) => {\n          assert.ok(ws instanceof CustomWebSocket);\n          assert.strictEqual(ws.foo, 'foo');\n          wss.close(done);\n        });\n      });\n\n      it('honors the `autoPong` option', (done) => {\n        const wss = new WebSocket.Server({ autoPong: false, port: 0 }, () => {\n          const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n          ws.on('open', () => {\n            ws.ping();\n          });\n\n          ws.on('pong', () => {\n            done(new Error(\"Unexpected 'pong' event\"));\n          });\n        });\n\n        wss.on('connection', (ws) => {\n          ws.on('ping', () => {\n            ws.close();\n          });\n\n          ws.on('close', () => {\n            wss.close(done);\n          });\n        });\n      });\n\n      it('honors the `closeTimeout` option', (done) => {\n        const closeTimeout = 1000;\n        const wss = new WebSocket.Server({ closeTimeout, port: 0 }, () => {\n          const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n        });\n\n        wss.on('connection', (ws) => {\n          ws.on('close', () => {\n            wss.close(done);\n          });\n\n          ws.close();\n          assert.strictEqual(ws._closeTimer._idleTimeout, closeTimeout);\n        });\n      });\n    });\n\n    it('emits an error if http server bind fails', (done) => {\n      const wss1 = new WebSocket.Server({ port: 0 }, () => {\n        const wss2 = new WebSocket.Server({\n          port: wss1.address().port\n        });\n\n        wss2.on('error', () => wss1.close(done));\n      });\n    });\n\n    it('starts a server on a given port', (done) => {\n      const port = 1337;\n      const wss = new WebSocket.Server({ port }, () => {\n        const ws = new WebSocket(`ws://localhost:${port}`);\n\n        ws.on('open', ws.close);\n      });\n\n      wss.on('connection', () => wss.close(done));\n    });\n\n    it('binds the server on any IPv6 address when available', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        assert.strictEqual(wss._server.address().address, '::');\n        wss.close(done);\n      });\n    });\n\n    it('uses a precreated http server', (done) => {\n      const server = http.createServer();\n\n      server.listen(0, () => {\n        const wss = new WebSocket.Server({ server });\n\n        wss.on('connection', () => {\n          server.close(done);\n        });\n\n        const ws = new WebSocket(`ws://localhost:${server.address().port}`);\n\n        ws.on('open', ws.close);\n      });\n    });\n\n    it('426s for non-Upgrade requests', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        http.get(`http://localhost:${wss.address().port}`, (res) => {\n          let body = '';\n\n          assert.strictEqual(res.statusCode, 426);\n          res.on('data', (chunk) => {\n            body += chunk;\n          });\n          res.on('end', () => {\n            assert.strictEqual(body, http.STATUS_CODES[426]);\n            wss.close(done);\n          });\n        });\n      });\n    });\n\n    it('uses a precreated http server listening on IPC', (done) => {\n      const randomString = crypto.randomBytes(4).toString('hex');\n      const ipcPath =\n        process.platform === 'win32'\n          ? `\\\\\\\\.\\\\pipe\\\\ws-pipe-${randomString}`\n          : path.join(os.tmpdir(), `ws-${randomString}.sock`);\n\n      const server = http.createServer();\n\n      server.listen(ipcPath, () => {\n        const wss = new WebSocket.Server({ server });\n\n        wss.on('connection', (ws, req) => {\n          if (wss.clients.size === 1) {\n            assert.strictEqual(req.url, '/foo?bar=bar');\n          } else {\n            assert.strictEqual(req.url, '/');\n\n            for (const client of wss.clients) {\n              client.close();\n            }\n\n            server.close(done);\n          }\n        });\n\n        const ws = new WebSocket(`ws+unix:${ipcPath}:/foo?bar=bar`);\n        ws.on('open', () => new WebSocket(`ws+unix:${ipcPath}`));\n      });\n    });\n  });\n\n  describe('#address', () => {\n    it('returns the address of the server', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const addr = wss.address();\n\n        assert.deepStrictEqual(addr, wss._server.address());\n        wss.close(done);\n      });\n    });\n\n    it('throws an error when operating in \"noServer\" mode', () => {\n      const wss = new WebSocket.Server({ noServer: true });\n\n      assert.throws(() => {\n        wss.address();\n      }, /^Error: The server is operating in \"noServer\" mode$/);\n    });\n\n    it('returns `null` if called after close', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        wss.close(() => {\n          assert.strictEqual(wss.address(), null);\n          done();\n        });\n      });\n    });\n  });\n\n  describe('#close', () => {\n    it('does not throw if called multiple times', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        wss.on('close', done);\n\n        wss.close();\n        wss.close();\n        wss.close();\n      });\n    });\n\n    it(\"doesn't close a precreated server\", (done) => {\n      const server = http.createServer();\n      const realClose = server.close;\n\n      server.close = () => {\n        done(new Error('Must not close pre-created server'));\n      };\n\n      const wss = new WebSocket.Server({ server });\n\n      wss.on('connection', () => {\n        wss.close();\n        server.close = realClose;\n        server.close(done);\n      });\n\n      server.listen(0, () => {\n        const ws = new WebSocket(`ws://localhost:${server.address().port}`);\n\n        ws.on('open', ws.close);\n      });\n    });\n\n    it('invokes the callback in noServer mode', (done) => {\n      const wss = new WebSocket.Server({ noServer: true });\n\n      wss.close(done);\n    });\n\n    it('cleans event handlers on precreated server', (done) => {\n      const server = http.createServer();\n      const listeningListenerCount = server.listenerCount('listening');\n      const wss = new WebSocket.Server({ server });\n\n      server.listen(0, () => {\n        wss.close(() => {\n          assert.strictEqual(\n            server.listenerCount('listening'),\n            listeningListenerCount\n          );\n          assert.strictEqual(server.listenerCount('upgrade'), 0);\n          assert.strictEqual(server.listenerCount('error'), 0);\n\n          server.close(done);\n        });\n      });\n    });\n\n    it(\"emits the 'close' event after the server closes\", (done) => {\n      let serverCloseEventEmitted = false;\n\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        net.createConnection({ port: wss.address().port });\n      });\n\n      wss._server.on('connection', (socket) => {\n        wss.close();\n\n        //\n        // The server is closing. Ensure this does not emit a `'close'`\n        // event before the server is actually closed.\n        //\n        wss.close();\n\n        process.nextTick(() => {\n          socket.end();\n        });\n      });\n\n      wss._server.on('close', () => {\n        serverCloseEventEmitted = true;\n      });\n\n      wss.on('close', () => {\n        assert.ok(serverCloseEventEmitted);\n        done();\n      });\n    });\n\n    it(\"emits the 'close' event if client tracking is disabled\", (done) => {\n      const wss = new WebSocket.Server({\n        noServer: true,\n        clientTracking: false\n      });\n\n      wss.on('close', done);\n      wss.close();\n    });\n\n    it('calls the callback if the server is already closed', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        wss.close(() => {\n          assert.strictEqual(wss._state, 2);\n\n          wss.close((err) => {\n            assert.ok(err instanceof Error);\n            assert.strictEqual(err.message, 'The server is not running');\n            done();\n          });\n        });\n      });\n    });\n\n    it(\"emits the 'close' event if the server is already closed\", (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        wss.close(() => {\n          assert.strictEqual(wss._state, 2);\n\n          wss.on('close', done);\n          wss.close();\n        });\n      });\n    });\n  });\n\n  describe('#clients', () => {\n    it('returns a list of connected clients', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        assert.strictEqual(wss.clients.size, 0);\n\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('open', ws.close);\n      });\n\n      wss.on('connection', () => {\n        assert.strictEqual(wss.clients.size, 1);\n        wss.close(done);\n      });\n    });\n\n    it('can be disabled', (done) => {\n      const wss = new WebSocket.Server(\n        { port: 0, clientTracking: false },\n        () => {\n          assert.strictEqual(wss.clients, undefined);\n          const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n          ws.on('open', () => ws.close());\n        }\n      );\n\n      wss.on('connection', (ws) => {\n        assert.strictEqual(wss.clients, undefined);\n        ws.on('close', () => wss.close(done));\n      });\n    });\n\n    it('is updated when client terminates the connection', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('open', () => ws.terminate());\n      });\n\n      wss.on('connection', (ws) => {\n        ws.on('close', () => {\n          assert.strictEqual(wss.clients.size, 0);\n          wss.close(done);\n        });\n      });\n    });\n\n    it('is updated when client closes the connection', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('open', () => ws.close());\n      });\n\n      wss.on('connection', (ws) => {\n        ws.on('close', () => {\n          assert.strictEqual(wss.clients.size, 0);\n          wss.close(done);\n        });\n      });\n    });\n  });\n\n  describe('#shouldHandle', () => {\n    it('returns true when the path matches', () => {\n      const wss = new WebSocket.Server({ noServer: true, path: '/foo' });\n\n      assert.strictEqual(wss.shouldHandle({ url: '/foo' }), true);\n      assert.strictEqual(wss.shouldHandle({ url: '/foo?bar=baz' }), true);\n    });\n\n    it(\"returns false when the path doesn't match\", () => {\n      const wss = new WebSocket.Server({ noServer: true, path: '/foo' });\n\n      assert.strictEqual(wss.shouldHandle({ url: '/bar' }), false);\n    });\n  });\n\n  describe('#handleUpgrade', () => {\n    it('can be used for a pre-existing server', (done) => {\n      const server = http.createServer();\n\n      server.listen(0, () => {\n        const wss = new WebSocket.Server({ noServer: true });\n\n        server.on('upgrade', (req, socket, head) => {\n          wss.handleUpgrade(req, socket, head, (ws) => {\n            ws.send('hello');\n            ws.close();\n          });\n        });\n\n        const ws = new WebSocket(`ws://localhost:${server.address().port}`);\n\n        ws.on('message', (message, isBinary) => {\n          assert.deepStrictEqual(message, Buffer.from('hello'));\n          assert.ok(!isBinary);\n          server.close(done);\n        });\n      });\n    });\n\n    it(\"closes the connection when path doesn't match\", (done) => {\n      const wss = new WebSocket.Server({ port: 0, path: '/ws' }, () => {\n        const req = http.get({\n          port: wss.address().port,\n          headers: {\n            Connection: 'Upgrade',\n            Upgrade: 'websocket',\n            'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ==',\n            'Sec-WebSocket-Version': 13\n          }\n        });\n\n        req.on('response', (res) => {\n          assert.strictEqual(res.statusCode, 400);\n          wss.close(done);\n        });\n      });\n    });\n\n    it('closes the connection when protocol version is Hixie-76', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const req = http.get({\n          port: wss.address().port,\n          headers: {\n            Connection: 'Upgrade',\n            Upgrade: 'WebSocket',\n            'Sec-WebSocket-Key1': '4 @1  46546xW%0l 1 5',\n            'Sec-WebSocket-Key2': '12998 5 Y3 1  .P00',\n            'Sec-WebSocket-Protocol': 'sample'\n          }\n        });\n\n        req.on('response', (res) => {\n          assert.strictEqual(res.statusCode, 400);\n\n          const chunks = [];\n\n          res.on('data', (chunk) => {\n            chunks.push(chunk);\n          });\n\n          res.on('end', () => {\n            assert.strictEqual(\n              Buffer.concat(chunks).toString(),\n              'Missing or invalid Sec-WebSocket-Key header'\n            );\n            wss.close(done);\n          });\n        });\n      });\n    });\n\n    it('completes a WebSocket upgrade over any duplex stream', (done) => {\n      const server = http.createServer();\n\n      server.listen(0, () => {\n        const wss = new WebSocket.Server({ noServer: true });\n\n        server.on('upgrade', (req, socket, head) => {\n          //\n          // Put a stream between the raw socket and our websocket processing.\n          //\n          const { clientSide, serverSide } = makeDuplexPair();\n\n          socket.pipe(clientSide);\n          clientSide.pipe(socket);\n\n          //\n          // Pass the other side of the stream as the socket to upgrade.\n          //\n          wss.handleUpgrade(req, serverSide, head, (ws) => {\n            ws.send('hello');\n            ws.close();\n          });\n        });\n\n        const ws = new WebSocket(`ws://localhost:${server.address().port}`);\n\n        ws.on('message', (message, isBinary) => {\n          assert.deepStrictEqual(message, Buffer.from('hello'));\n          assert.ok(!isBinary);\n          server.close(done);\n        });\n      });\n    });\n  });\n\n  describe('#completeUpgrade', () => {\n    it('throws an error if called twice with the same socket', (done) => {\n      const server = http.createServer();\n\n      server.listen(0, () => {\n        const wss = new WebSocket.Server({ noServer: true });\n\n        server.on('upgrade', (req, socket, head) => {\n          wss.handleUpgrade(req, socket, head, (ws) => {\n            ws.close();\n          });\n          assert.throws(\n            () => wss.handleUpgrade(req, socket, head, NOOP),\n            (err) => {\n              assert.ok(err instanceof Error);\n              assert.strictEqual(\n                err.message,\n                'server.handleUpgrade() was called more than once with the ' +\n                  'same socket, possibly due to a misconfiguration'\n              );\n              return true;\n            }\n          );\n        });\n\n        const ws = new WebSocket(`ws://localhost:${server.address().port}`);\n\n        ws.on('open', () => {\n          ws.on('close', () => {\n            server.close(done);\n          });\n        });\n      });\n    });\n  });\n\n  describe('Connection establishing', () => {\n    it('fails if the HTTP method is not GET', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const req = http.request({\n          method: 'POST',\n          port: wss.address().port,\n          headers: {\n            Connection: 'Upgrade',\n            Upgrade: 'websocket'\n          }\n        });\n\n        req.on('response', (res) => {\n          assert.strictEqual(res.statusCode, 405);\n\n          const chunks = [];\n\n          res.on('data', (chunk) => {\n            chunks.push(chunk);\n          });\n\n          res.on('end', () => {\n            assert.strictEqual(\n              Buffer.concat(chunks).toString(),\n              'Invalid HTTP method'\n            );\n            wss.close(done);\n          });\n        });\n\n        req.end();\n      });\n\n      wss.on('connection', () => {\n        done(new Error(\"Unexpected 'connection' event\"));\n      });\n    });\n\n    it('fails if the Upgrade header field value cannot be read', (done) => {\n      const server = http.createServer();\n      const wss = new WebSocket.Server({ noServer: true });\n\n      server.maxHeadersCount = 1;\n\n      server.on('upgrade', (req, socket, head) => {\n        assert.deepStrictEqual(req.headers, { foo: 'bar' });\n        wss.handleUpgrade(req, socket, head, () => {\n          done(new Error('Unexpected callback invocation'));\n        });\n      });\n\n      server.listen(() => {\n        const req = http.get({\n          port: server.address().port,\n          headers: {\n            foo: 'bar',\n            bar: 'baz',\n            Connection: 'Upgrade',\n            Upgrade: 'websocket'\n          }\n        });\n\n        req.on('response', (res) => {\n          assert.strictEqual(res.statusCode, 400);\n\n          const chunks = [];\n\n          res.on('data', (chunk) => {\n            chunks.push(chunk);\n          });\n\n          res.on('end', () => {\n            assert.strictEqual(\n              Buffer.concat(chunks).toString(),\n              'Invalid Upgrade header'\n            );\n            server.close(done);\n          });\n        });\n      });\n    });\n\n    it('fails if the Upgrade header field value is not \"websocket\"', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const req = http.get({\n          port: wss.address().port,\n          headers: {\n            Connection: 'Upgrade',\n            Upgrade: 'foo'\n          }\n        });\n\n        req.on('response', (res) => {\n          assert.strictEqual(res.statusCode, 400);\n\n          const chunks = [];\n\n          res.on('data', (chunk) => {\n            chunks.push(chunk);\n          });\n\n          res.on('end', () => {\n            assert.strictEqual(\n              Buffer.concat(chunks).toString(),\n              'Invalid Upgrade header'\n            );\n            wss.close(done);\n          });\n        });\n      });\n\n      wss.on('connection', () => {\n        done(new Error(\"Unexpected 'connection' event\"));\n      });\n    });\n\n    it('fails if the Sec-WebSocket-Key header is invalid (1/2)', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const req = http.get({\n          port: wss.address().port,\n          headers: {\n            Connection: 'Upgrade',\n            Upgrade: 'websocket'\n          }\n        });\n\n        req.on('response', (res) => {\n          assert.strictEqual(res.statusCode, 400);\n\n          const chunks = [];\n\n          res.on('data', (chunk) => {\n            chunks.push(chunk);\n          });\n\n          res.on('end', () => {\n            assert.strictEqual(\n              Buffer.concat(chunks).toString(),\n              'Missing or invalid Sec-WebSocket-Key header'\n            );\n            wss.close(done);\n          });\n        });\n      });\n\n      wss.on('connection', () => {\n        done(new Error(\"Unexpected 'connection' event\"));\n      });\n    });\n\n    it('fails if the Sec-WebSocket-Key header is invalid (2/2)', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const req = http.get({\n          port: wss.address().port,\n          headers: {\n            Connection: 'Upgrade',\n            Upgrade: 'websocket',\n            'Sec-WebSocket-Key': 'P5l8BJcZwRc='\n          }\n        });\n\n        req.on('response', (res) => {\n          assert.strictEqual(res.statusCode, 400);\n\n          const chunks = [];\n\n          res.on('data', (chunk) => {\n            chunks.push(chunk);\n          });\n\n          res.on('end', () => {\n            assert.strictEqual(\n              Buffer.concat(chunks).toString(),\n              'Missing or invalid Sec-WebSocket-Key header'\n            );\n            wss.close(done);\n          });\n        });\n      });\n\n      wss.on('connection', () => {\n        done(new Error(\"Unexpected 'connection' event\"));\n      });\n    });\n\n    it('fails if the Sec-WebSocket-Version header is invalid (1/2)', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const req = http.get({\n          port: wss.address().port,\n          headers: {\n            Connection: 'Upgrade',\n            Upgrade: 'websocket',\n            'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ=='\n          }\n        });\n\n        req.on('response', (res) => {\n          assert.strictEqual(res.statusCode, 400);\n          assert.strictEqual(res.headers['sec-websocket-version'], '13, 8');\n\n          const chunks = [];\n\n          res.on('data', (chunk) => {\n            chunks.push(chunk);\n          });\n\n          res.on('end', () => {\n            assert.strictEqual(\n              Buffer.concat(chunks).toString(),\n              'Missing or invalid Sec-WebSocket-Version header'\n            );\n            wss.close(done);\n          });\n        });\n      });\n\n      wss.on('connection', () => {\n        done(new Error(\"Unexpected 'connection' event\"));\n      });\n    });\n\n    it('fails if the Sec-WebSocket-Version header is invalid (2/2)', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const req = http.get({\n          port: wss.address().port,\n          headers: {\n            Connection: 'Upgrade',\n            Upgrade: 'websocket',\n            'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ==',\n            'Sec-WebSocket-Version': 12\n          }\n        });\n\n        req.on('response', (res) => {\n          assert.strictEqual(res.statusCode, 400);\n          assert.strictEqual(res.headers['sec-websocket-version'], '13, 8');\n\n          const chunks = [];\n\n          res.on('data', (chunk) => {\n            chunks.push(chunk);\n          });\n\n          res.on('end', () => {\n            assert.strictEqual(\n              Buffer.concat(chunks).toString(),\n              'Missing or invalid Sec-WebSocket-Version header'\n            );\n            wss.close(done);\n          });\n        });\n      });\n\n      wss.on('connection', () => {\n        done(new Error(\"Unexpected 'connection' event\"));\n      });\n    });\n\n    it('fails is the Sec-WebSocket-Protocol header is invalid', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const req = http.get({\n          port: wss.address().port,\n          headers: {\n            Connection: 'Upgrade',\n            Upgrade: 'websocket',\n            'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ==',\n            'Sec-WebSocket-Version': 13,\n            'Sec-WebSocket-Protocol': 'foo;bar'\n          }\n        });\n\n        req.on('response', (res) => {\n          assert.strictEqual(res.statusCode, 400);\n\n          const chunks = [];\n\n          res.on('data', (chunk) => {\n            chunks.push(chunk);\n          });\n\n          res.on('end', () => {\n            assert.strictEqual(\n              Buffer.concat(chunks).toString(),\n              'Invalid Sec-WebSocket-Protocol header'\n            );\n            wss.close(done);\n          });\n        });\n      });\n\n      wss.on('connection', () => {\n        done(new Error(\"Unexpected 'connection' event\"));\n      });\n    });\n\n    it('fails if the Sec-WebSocket-Extensions header is invalid', (done) => {\n      const wss = new WebSocket.Server(\n        {\n          perMessageDeflate: true,\n          port: 0\n        },\n        () => {\n          const req = http.get({\n            port: wss.address().port,\n            headers: {\n              Connection: 'Upgrade',\n              Upgrade: 'websocket',\n              'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ==',\n              'Sec-WebSocket-Version': 13,\n              'Sec-WebSocket-Extensions':\n                'permessage-deflate; server_max_window_bits=foo'\n            }\n          });\n\n          req.on('response', (res) => {\n            assert.strictEqual(res.statusCode, 400);\n\n            const chunks = [];\n\n            res.on('data', (chunk) => {\n              chunks.push(chunk);\n            });\n\n            res.on('end', () => {\n              assert.strictEqual(\n                Buffer.concat(chunks).toString(),\n                'Invalid or unacceptable Sec-WebSocket-Extensions header'\n              );\n              wss.close(done);\n            });\n          });\n        }\n      );\n\n      wss.on('connection', () => {\n        done(new Error(\"Unexpected 'connection' event\"));\n      });\n    });\n\n    it(\"emits the 'wsClientError' event\", (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const req = http.request({\n          method: 'POST',\n          port: wss.address().port,\n          headers: {\n            Connection: 'Upgrade',\n            Upgrade: 'websocket'\n          }\n        });\n\n        req.on('response', (res) => {\n          assert.strictEqual(res.statusCode, 400);\n          wss.close(done);\n        });\n\n        req.end();\n      });\n\n      wss.on('wsClientError', (err, socket, request) => {\n        assert.ok(err instanceof Error);\n        assert.strictEqual(err.message, 'Invalid HTTP method');\n\n        assert.ok(request instanceof http.IncomingMessage);\n        assert.strictEqual(request.method, 'POST');\n\n        socket.end('HTTP/1.1 400 Bad Request\\r\\n\\r\\n');\n      });\n\n      wss.on('connection', () => {\n        done(new Error(\"Unexpected 'connection' event\"));\n      });\n    });\n\n    it('fails if the WebSocket server is closing or closed', (done) => {\n      const server = http.createServer();\n      const wss = new WebSocket.Server({ noServer: true });\n\n      server.on('upgrade', (req, socket, head) => {\n        wss.close();\n        wss.handleUpgrade(req, socket, head, () => {\n          done(new Error('Unexpected callback invocation'));\n        });\n      });\n\n      server.listen(0, () => {\n        const ws = new WebSocket(`ws://localhost:${server.address().port}`);\n\n        ws.on('unexpected-response', (req, res) => {\n          assert.strictEqual(res.statusCode, 503);\n          res.resume();\n          server.close(done);\n        });\n      });\n    });\n\n    it('handles unsupported extensions', (done) => {\n      const wss = new WebSocket.Server(\n        {\n          perMessageDeflate: true,\n          port: 0\n        },\n        () => {\n          const req = http.get({\n            port: wss.address().port,\n            headers: {\n              Connection: 'Upgrade',\n              Upgrade: 'websocket',\n              'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ==',\n              'Sec-WebSocket-Version': 13,\n              'Sec-WebSocket-Extensions': 'foo; bar'\n            }\n          });\n\n          req.on('upgrade', (res, socket, head) => {\n            if (head.length) socket.unshift(head);\n\n            socket.once('data', (chunk) => {\n              assert.strictEqual(chunk[0], 0x88);\n              socket.destroy();\n              wss.close(done);\n            });\n          });\n        }\n      );\n\n      wss.on('connection', (ws) => {\n        assert.strictEqual(ws.extensions, '');\n        ws.close();\n      });\n    });\n\n    describe('`verifyClient`', () => {\n      it('can reject client synchronously', (done) => {\n        const wss = new WebSocket.Server(\n          {\n            verifyClient: () => false,\n            port: 0\n          },\n          () => {\n            const req = http.get({\n              port: wss.address().port,\n              headers: {\n                Connection: 'Upgrade',\n                Upgrade: 'websocket',\n                'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ==',\n                'Sec-WebSocket-Version': 8\n              }\n            });\n\n            req.on('response', (res) => {\n              assert.strictEqual(res.statusCode, 401);\n              wss.close(done);\n            });\n          }\n        );\n\n        wss.on('connection', () => {\n          done(new Error(\"Unexpected 'connection' event\"));\n        });\n      });\n\n      it('can accept client synchronously', (done) => {\n        const server = https.createServer({\n          cert: fs.readFileSync('test/fixtures/certificate.pem'),\n          key: fs.readFileSync('test/fixtures/key.pem')\n        });\n\n        const wss = new WebSocket.Server({\n          verifyClient: (info) => {\n            assert.strictEqual(info.origin, 'https://example.com');\n            assert.strictEqual(info.req.headers.foo, 'bar');\n            assert.ok(info.secure, true);\n            return true;\n          },\n          server\n        });\n\n        wss.on('connection', () => {\n          server.close(done);\n        });\n\n        server.listen(0, () => {\n          const ws = new WebSocket(`wss://localhost:${server.address().port}`, {\n            headers: { Origin: 'https://example.com', foo: 'bar' },\n            rejectUnauthorized: false\n          });\n\n          ws.on('open', ws.close);\n        });\n      });\n\n      it('can accept client asynchronously', (done) => {\n        const wss = new WebSocket.Server(\n          {\n            verifyClient: (o, cb) => process.nextTick(cb, true),\n            port: 0\n          },\n          () => {\n            const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n            ws.on('open', ws.close);\n          }\n        );\n\n        wss.on('connection', () => wss.close(done));\n      });\n\n      it('can reject client asynchronously', (done) => {\n        const wss = new WebSocket.Server(\n          {\n            verifyClient: (info, cb) => process.nextTick(cb, false),\n            port: 0\n          },\n          () => {\n            const req = http.get({\n              port: wss.address().port,\n              headers: {\n                Connection: 'Upgrade',\n                Upgrade: 'websocket',\n                'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ==',\n                'Sec-WebSocket-Version': 8\n              }\n            });\n\n            req.on('response', (res) => {\n              assert.strictEqual(res.statusCode, 401);\n              wss.close(done);\n            });\n          }\n        );\n\n        wss.on('connection', () => {\n          done(new Error(\"Unexpected 'connection' event\"));\n        });\n      });\n\n      it('can reject client asynchronously w/ status code', (done) => {\n        const wss = new WebSocket.Server(\n          {\n            verifyClient: (info, cb) => process.nextTick(cb, false, 404),\n            port: 0\n          },\n          () => {\n            const req = http.get({\n              port: wss.address().port,\n              headers: {\n                Connection: 'Upgrade',\n                Upgrade: 'websocket',\n                'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ==',\n                'Sec-WebSocket-Version': 8\n              }\n            });\n\n            req.on('response', (res) => {\n              assert.strictEqual(res.statusCode, 404);\n              wss.close(done);\n            });\n          }\n        );\n\n        wss.on('connection', () => {\n          done(new Error(\"Unexpected 'connection' event\"));\n        });\n      });\n\n      it('can reject client asynchronously w/ custom headers', (done) => {\n        const wss = new WebSocket.Server(\n          {\n            verifyClient: (info, cb) => {\n              process.nextTick(cb, false, 503, '', { 'Retry-After': 120 });\n            },\n            port: 0\n          },\n          () => {\n            const req = http.get({\n              port: wss.address().port,\n              headers: {\n                Connection: 'Upgrade',\n                Upgrade: 'websocket',\n                'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ==',\n                'Sec-WebSocket-Version': 8\n              }\n            });\n\n            req.on('response', (res) => {\n              assert.strictEqual(res.statusCode, 503);\n              assert.strictEqual(res.headers['retry-after'], '120');\n              wss.close(done);\n            });\n          }\n        );\n\n        wss.on('connection', () => {\n          done(new Error(\"Unexpected 'connection' event\"));\n        });\n      });\n    });\n\n    it(\"doesn't emit the 'connection' event if socket is closed prematurely\", (done) => {\n      const server = http.createServer();\n\n      server.listen(0, () => {\n        const wss = new WebSocket.Server({\n          verifyClient: ({ req: { socket } }, cb) => {\n            assert.strictEqual(socket.readable, true);\n            assert.strictEqual(socket.writable, true);\n\n            socket.on('end', () => {\n              assert.strictEqual(socket.readable, false);\n              assert.strictEqual(socket.writable, true);\n              cb(true);\n            });\n          },\n          server\n        });\n\n        wss.on('connection', () => {\n          done(new Error(\"Unexpected 'connection' event\"));\n        });\n\n        const socket = net.connect(\n          {\n            port: server.address().port,\n            allowHalfOpen: true\n          },\n          () => {\n            socket.end(\n              [\n                'GET / HTTP/1.1',\n                'Host: localhost',\n                'Upgrade: websocket',\n                'Connection: Upgrade',\n                'Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==',\n                'Sec-WebSocket-Version: 13',\n                '\\r\\n'\n              ].join('\\r\\n')\n            );\n          }\n        );\n\n        socket.on('end', () => {\n          wss.close();\n          server.close(done);\n        });\n      });\n    });\n\n    it('handles data passed along with the upgrade request', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const req = http.request({\n          port: wss.address().port,\n          headers: {\n            Connection: 'Upgrade',\n            Upgrade: 'websocket',\n            'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ==',\n            'Sec-WebSocket-Version': 13\n          }\n        });\n\n        const list = Sender.frame(Buffer.from('Hello'), {\n          fin: true,\n          rsv1: false,\n          opcode: 0x01,\n          mask: true,\n          readOnly: false\n        });\n\n        req.write(Buffer.concat(list));\n        req.end();\n      });\n\n      wss.on('connection', (ws) => {\n        ws.on('message', (data, isBinary) => {\n          assert.deepStrictEqual(data, Buffer.from('Hello'));\n          assert.ok(!isBinary);\n          wss.close(done);\n        });\n      });\n    });\n\n    describe('`handleProtocols`', () => {\n      it('allows to select a subprotocol', (done) => {\n        const handleProtocols = (protocols, request) => {\n          assert.ok(request instanceof http.IncomingMessage);\n          assert.strictEqual(request.url, '/');\n          return Array.from(protocols).pop();\n        };\n        const wss = new WebSocket.Server({ handleProtocols, port: 0 }, () => {\n          const ws = new WebSocket(`ws://localhost:${wss.address().port}`, [\n            'foo',\n            'bar'\n          ]);\n\n          ws.on('open', () => {\n            assert.strictEqual(ws.protocol, 'bar');\n            wss.close(done);\n          });\n        });\n\n        wss.on('connection', (ws) => {\n          ws.close();\n        });\n      });\n    });\n\n    it(\"emits the 'headers' event\", (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(\n          `ws://localhost:${wss.address().port}?foo=bar`\n        );\n\n        ws.on('open', ws.close);\n      });\n\n      wss.on('headers', (headers, request) => {\n        assert.deepStrictEqual(headers.slice(0, 3), [\n          'HTTP/1.1 101 Switching Protocols',\n          'Upgrade: websocket',\n          'Connection: Upgrade'\n        ]);\n        assert.ok(request instanceof http.IncomingMessage);\n        assert.strictEqual(request.url, '/?foo=bar');\n\n        wss.on('connection', () => wss.close(done));\n      });\n    });\n  });\n\n  describe('permessage-deflate', () => {\n    it('is disabled by default', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('open', ws.close);\n      });\n\n      wss.on('connection', (ws, req) => {\n        assert.strictEqual(\n          req.headers['sec-websocket-extensions'],\n          'permessage-deflate; client_max_window_bits'\n        );\n        assert.strictEqual(ws.extensions, '');\n        wss.close(done);\n      });\n    });\n\n    it('uses configuration options', (done) => {\n      const wss = new WebSocket.Server(\n        {\n          perMessageDeflate: { clientMaxWindowBits: 8 },\n          port: 0\n        },\n        () => {\n          const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n          ws.on('upgrade', (res) => {\n            assert.strictEqual(\n              res.headers['sec-websocket-extensions'],\n              'permessage-deflate; client_max_window_bits=8'\n            );\n\n            wss.close(done);\n          });\n        }\n      );\n\n      wss.on('connection', (ws) => {\n        ws.close();\n      });\n    });\n  });\n});\n"
  },
  {
    "path": "test/websocket.integration.js",
    "content": "'use strict';\n\nconst assert = require('assert');\n\nconst WebSocket = require('..');\n\ndescribe('WebSocket', () => {\n  it('communicates successfully with echo service (ws)', (done) => {\n    const ws = new WebSocket('ws://websocket-echo.com/', {\n      protocolVersion: 13\n    });\n\n    let dataReceived = false;\n\n    ws.on('open', () => {\n      ws.send('hello');\n    });\n\n    ws.on('close', () => {\n      assert.ok(dataReceived);\n      done();\n    });\n\n    ws.on('message', (message, isBinary) => {\n      dataReceived = true;\n      assert.ok(!isBinary);\n      assert.strictEqual(message.toString(), 'hello');\n      ws.close();\n    });\n  });\n\n  it('communicates successfully with echo service (wss)', (done) => {\n    const ws = new WebSocket('wss://websocket-echo.com/', {\n      protocolVersion: 13\n    });\n\n    let dataReceived = false;\n\n    ws.on('open', () => {\n      ws.send('hello');\n    });\n\n    ws.on('close', () => {\n      assert.ok(dataReceived);\n      done();\n    });\n\n    ws.on('message', (message, isBinary) => {\n      dataReceived = true;\n      assert.ok(!isBinary);\n      assert.strictEqual(message.toString(), 'hello');\n      ws.close();\n    });\n  });\n});\n"
  },
  {
    "path": "test/websocket.test.js",
    "content": "/* eslint no-unused-vars: [\"error\", { \"varsIgnorePattern\": \"^ws$\" }] */\n\n'use strict';\n\nconst assert = require('assert');\nconst crypto = require('crypto');\nconst https = require('https');\nconst http = require('http');\nconst path = require('path');\nconst net = require('net');\nconst tls = require('tls');\nconst os = require('os');\nconst fs = require('fs');\nconst { getDefaultHighWaterMark } = require('stream');\nconst { URL } = require('url');\n\nconst Sender = require('../lib/sender');\nconst WebSocket = require('..');\nconst {\n  CloseEvent,\n  ErrorEvent,\n  Event,\n  MessageEvent\n} = require('../lib/event-target');\nconst {\n  EMPTY_BUFFER,\n  GUID,\n  hasBlob,\n  kListener,\n  NOOP\n} = require('../lib/constants');\n\nconst highWaterMark = getDefaultHighWaterMark\n  ? getDefaultHighWaterMark(false)\n  : 16 * 1024;\n\nclass CustomAgent extends http.Agent {\n  addRequest() {}\n}\n\ndescribe('WebSocket', () => {\n  describe('#ctor', () => {\n    it('throws an error when using an invalid url', () => {\n      assert.throws(\n        () => new WebSocket('foo'),\n        /^SyntaxError: Invalid URL: foo$/\n      );\n\n      assert.throws(\n        () => new WebSocket('bad-scheme://websocket-echo.com'),\n        (err) => {\n          assert.strictEqual(\n            err.message,\n            'The URL\\'s protocol must be one of \"ws:\", \"wss:\", ' +\n              '\"http:\", \"https:\", or \"ws+unix:\"'\n          );\n\n          return true;\n        }\n      );\n\n      assert.throws(\n        () => new WebSocket('ws+unix:'),\n        /^SyntaxError: The URL's pathname is empty$/\n      );\n\n      assert.throws(\n        () => new WebSocket('wss://websocket-echo.com#foo'),\n        /^SyntaxError: The URL contains a fragment identifier$/\n      );\n    });\n\n    it('throws an error if a subprotocol is invalid or duplicated', () => {\n      for (const subprotocol of [null, '', 'a,b', ['a', 'a']]) {\n        assert.throws(\n          () => new WebSocket('ws://localhost', subprotocol),\n          /^SyntaxError: An invalid or duplicated subprotocol was specified$/\n        );\n      }\n    });\n\n    it('accepts `url.URL` objects as url', (done) => {\n      const agent = new http.Agent();\n\n      agent.addRequest = (req, opts) => {\n        assert.strictEqual(opts.host, '::1');\n        assert.strictEqual(req.path, '/');\n        done();\n      };\n\n      const ws = new WebSocket(new URL('ws://[::1]'), { agent });\n    });\n\n    it('allows the http scheme', (done) => {\n      const agent = new CustomAgent();\n\n      agent.addRequest = (req, opts) => {\n        assert.strictEqual(opts.host, 'localhost');\n        assert.strictEqual(opts.port, 80);\n        done();\n      };\n\n      const ws = new WebSocket('http://localhost', { agent });\n    });\n\n    it('allows the https scheme', (done) => {\n      const agent = new https.Agent();\n\n      agent.addRequest = (req, opts) => {\n        assert.strictEqual(opts.host, 'localhost');\n        assert.strictEqual(opts.port, 443);\n        done();\n      };\n\n      const ws = new WebSocket('https://localhost', { agent });\n    });\n\n    describe('options', () => {\n      it('accepts the `options` object as 3rd argument', () => {\n        const agent = new http.Agent();\n        let count = 0;\n        let ws;\n\n        agent.addRequest = (req) => {\n          assert.strictEqual(\n            req.getHeader('sec-websocket-protocol'),\n            undefined\n          );\n          count++;\n        };\n\n        ws = new WebSocket('ws://localhost', undefined, { agent });\n        ws = new WebSocket('ws://localhost', [], { agent });\n\n        assert.strictEqual(count, 2);\n      });\n\n      it('accepts the `maxPayload` option', (done) => {\n        const maxPayload = 20480;\n        const wss = new WebSocket.Server(\n          {\n            perMessageDeflate: true,\n            port: 0\n          },\n          () => {\n            const ws = new WebSocket(`ws://localhost:${wss.address().port}`, {\n              perMessageDeflate: true,\n              maxPayload\n            });\n\n            ws.on('open', () => {\n              assert.strictEqual(ws._receiver._maxPayload, maxPayload);\n              assert.strictEqual(\n                ws._receiver._extensions['permessage-deflate']._maxPayload,\n                maxPayload\n              );\n              wss.close(done);\n            });\n          }\n        );\n\n        wss.on('connection', (ws) => {\n          ws.close();\n        });\n      });\n\n      it('throws an error when using an invalid `protocolVersion`', () => {\n        assert.throws(\n          () => new WebSocket('ws://localhost', { protocolVersion: 1000 }),\n          /^RangeError: Unsupported protocol version: 1000 \\(supported versions: 8, 13\\)$/\n        );\n      });\n\n      it('honors the `generateMask` option', (done) => {\n        const data = Buffer.from('foo');\n        const wss = new WebSocket.Server({ port: 0 }, () => {\n          const ws = new WebSocket(`ws://localhost:${wss.address().port}`, {\n            generateMask() {}\n          });\n\n          ws.on('open', () => {\n            ws.send(data);\n          });\n\n          ws.on('close', (code, reason) => {\n            assert.strictEqual(code, 1005);\n            assert.deepStrictEqual(reason, EMPTY_BUFFER);\n\n            wss.close(done);\n          });\n        });\n\n        wss.on('connection', (ws) => {\n          const chunks = [];\n\n          ws._socket.prependListener('data', (chunk) => {\n            chunks.push(chunk);\n          });\n\n          ws.on('message', (message) => {\n            assert.deepStrictEqual(message, data);\n            assert.deepStrictEqual(\n              Buffer.concat(chunks).slice(2, 6),\n              Buffer.alloc(4)\n            );\n\n            ws.close();\n          });\n        });\n      });\n\n      it('honors the `autoPong` option', (done) => {\n        const wss = new WebSocket.Server({ port: 0 }, () => {\n          const ws = new WebSocket(`ws://localhost:${wss.address().port}`, {\n            autoPong: false\n          });\n\n          ws.on('ping', () => {\n            ws.close();\n          });\n\n          ws.on('close', () => {\n            wss.close(done);\n          });\n        });\n\n        wss.on('connection', (ws) => {\n          ws.on('pong', () => {\n            done(new Error(\"Unexpected 'pong' event\"));\n          });\n\n          ws.ping();\n        });\n      });\n\n      it('honors the `closeTimeout` option', (done) => {\n        const wss = new WebSocket.Server({ port: 0 }, () => {\n          const closeTimeout = 1000;\n          const ws = new WebSocket(`ws://localhost:${wss.address().port}`, {\n            closeTimeout\n          });\n\n          ws.on('open', () => {\n            ws.close();\n            assert.strictEqual(ws._closeTimer._idleTimeout, closeTimeout);\n          });\n\n          ws.on('close', () => {\n            wss.close(done);\n          });\n        });\n      });\n    });\n  });\n\n  describe('Constants', () => {\n    const readyStates = {\n      CONNECTING: 0,\n      OPEN: 1,\n      CLOSING: 2,\n      CLOSED: 3\n    };\n\n    Object.keys(readyStates).forEach((state) => {\n      describe(`\\`${state}\\``, () => {\n        it('is enumerable property of class', () => {\n          const descriptor = Object.getOwnPropertyDescriptor(WebSocket, state);\n\n          assert.deepStrictEqual(descriptor, {\n            configurable: false,\n            enumerable: true,\n            value: readyStates[state],\n            writable: false\n          });\n        });\n\n        it('is enumerable property of prototype', () => {\n          const descriptor = Object.getOwnPropertyDescriptor(\n            WebSocket.prototype,\n            state\n          );\n\n          assert.deepStrictEqual(descriptor, {\n            configurable: false,\n            enumerable: true,\n            value: readyStates[state],\n            writable: false\n          });\n        });\n      });\n    });\n  });\n\n  describe('Attributes', () => {\n    describe('`binaryType`', () => {\n      it('is enumerable and configurable', () => {\n        const descriptor = Object.getOwnPropertyDescriptor(\n          WebSocket.prototype,\n          'binaryType'\n        );\n\n        assert.strictEqual(descriptor.configurable, true);\n        assert.strictEqual(descriptor.enumerable, true);\n        assert.ok(descriptor.get !== undefined);\n        assert.ok(descriptor.set !== undefined);\n      });\n\n      it(\"defaults to 'nodebuffer'\", () => {\n        const ws = new WebSocket('ws://localhost', {\n          agent: new CustomAgent()\n        });\n\n        assert.strictEqual(ws.binaryType, 'nodebuffer');\n      });\n\n      it(\"can be changed to 'arraybuffer' or 'fragments'\", () => {\n        const ws = new WebSocket('ws://localhost', {\n          agent: new CustomAgent()\n        });\n\n        ws.binaryType = 'arraybuffer';\n        assert.strictEqual(ws.binaryType, 'arraybuffer');\n\n        ws.binaryType = 'foo';\n        assert.strictEqual(ws.binaryType, 'arraybuffer');\n\n        ws.binaryType = 'fragments';\n        assert.strictEqual(ws.binaryType, 'fragments');\n\n        ws.binaryType = '';\n        assert.strictEqual(ws.binaryType, 'fragments');\n\n        ws.binaryType = 'nodebuffer';\n        assert.strictEqual(ws.binaryType, 'nodebuffer');\n      });\n    });\n\n    describe('`bufferedAmount`', () => {\n      it('is enumerable and configurable', () => {\n        const descriptor = Object.getOwnPropertyDescriptor(\n          WebSocket.prototype,\n          'bufferedAmount'\n        );\n\n        assert.strictEqual(descriptor.configurable, true);\n        assert.strictEqual(descriptor.enumerable, true);\n        assert.ok(descriptor.get !== undefined);\n        assert.ok(descriptor.set === undefined);\n      });\n\n      it('defaults to zero', () => {\n        const ws = new WebSocket('ws://localhost', {\n          agent: new CustomAgent()\n        });\n\n        assert.strictEqual(ws.bufferedAmount, 0);\n      });\n\n      it('defaults to zero upon \"open\"', (done) => {\n        const wss = new WebSocket.Server({ port: 0 }, () => {\n          const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n          ws.onopen = () => {\n            assert.strictEqual(ws.bufferedAmount, 0);\n            wss.close(done);\n          };\n        });\n\n        wss.on('connection', (ws) => {\n          ws.close();\n        });\n      });\n\n      it('takes into account the data in the sender queue', (done) => {\n        const wss = new WebSocket.Server(\n          {\n            perMessageDeflate: true,\n            port: 0\n          },\n          () => {\n            const ws = new WebSocket(`ws://localhost:${wss.address().port}`, {\n              perMessageDeflate: { threshold: 0 }\n            });\n\n            ws.on('open', () => {\n              ws.send('foo');\n\n              assert.strictEqual(ws.bufferedAmount, 3);\n\n              ws.send('bar', (err) => {\n                assert.ifError(err);\n                assert.strictEqual(ws.bufferedAmount, 0);\n                wss.close(done);\n              });\n\n              assert.strictEqual(ws.bufferedAmount, 6);\n            });\n          }\n        );\n\n        wss.on('connection', (ws) => {\n          ws.close();\n        });\n      });\n\n      it('takes into account the data in the socket queue', (done) => {\n        const wss = new WebSocket.Server({ port: 0 }, () => {\n          const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n        });\n\n        wss.on('connection', (ws) => {\n          const data = Buffer.alloc(1024, 61);\n\n          while (ws.bufferedAmount === 0) {\n            ws.send(data);\n          }\n\n          assert.ok(ws.bufferedAmount > 0);\n          assert.strictEqual(\n            ws.bufferedAmount,\n            ws._socket._writableState.length\n          );\n\n          ws.on('close', () => wss.close(done));\n          ws.close();\n        });\n      });\n    });\n\n    describe('`extensions`', () => {\n      it('is enumerable and configurable', () => {\n        const descriptor = Object.getOwnPropertyDescriptor(\n          WebSocket.prototype,\n          'bufferedAmount'\n        );\n\n        assert.strictEqual(descriptor.configurable, true);\n        assert.strictEqual(descriptor.enumerable, true);\n        assert.ok(descriptor.get !== undefined);\n        assert.ok(descriptor.set === undefined);\n      });\n\n      it('exposes the negotiated extensions names (1/2)', (done) => {\n        const wss = new WebSocket.Server({ port: 0 }, () => {\n          const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n          assert.strictEqual(ws.extensions, '');\n\n          ws.on('open', () => {\n            assert.strictEqual(ws.extensions, '');\n            ws.on('close', () => wss.close(done));\n          });\n        });\n\n        wss.on('connection', (ws) => {\n          assert.strictEqual(ws.extensions, '');\n          ws.close();\n        });\n      });\n\n      it('exposes the negotiated extensions names (2/2)', (done) => {\n        const wss = new WebSocket.Server(\n          {\n            perMessageDeflate: true,\n            port: 0\n          },\n          () => {\n            const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n            assert.strictEqual(ws.extensions, '');\n\n            ws.on('open', () => {\n              assert.strictEqual(ws.extensions, 'permessage-deflate');\n              ws.on('close', () => wss.close(done));\n            });\n          }\n        );\n\n        wss.on('connection', (ws) => {\n          assert.strictEqual(ws.extensions, 'permessage-deflate');\n          ws.close();\n        });\n      });\n    });\n\n    describe('`isPaused`', () => {\n      it('is enumerable and configurable', () => {\n        const descriptor = Object.getOwnPropertyDescriptor(\n          WebSocket.prototype,\n          'isPaused'\n        );\n\n        assert.strictEqual(descriptor.configurable, true);\n        assert.strictEqual(descriptor.enumerable, true);\n        assert.ok(descriptor.get !== undefined);\n        assert.ok(descriptor.set === undefined);\n      });\n\n      it('indicates whether the websocket is paused', (done) => {\n        const wss = new WebSocket.Server({ port: 0 }, () => {\n          const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n          ws.on('open', () => {\n            ws.pause();\n            assert.ok(ws.isPaused);\n\n            ws.resume();\n            assert.ok(!ws.isPaused);\n\n            ws.close();\n            wss.close(done);\n          });\n\n          assert.ok(!ws.isPaused);\n        });\n      });\n    });\n\n    describe('`protocol`', () => {\n      it('is enumerable and configurable', () => {\n        const descriptor = Object.getOwnPropertyDescriptor(\n          WebSocket.prototype,\n          'protocol'\n        );\n\n        assert.strictEqual(descriptor.configurable, true);\n        assert.strictEqual(descriptor.enumerable, true);\n        assert.ok(descriptor.get !== undefined);\n        assert.ok(descriptor.set === undefined);\n      });\n\n      it('exposes the subprotocol selected by the server', (done) => {\n        const wss = new WebSocket.Server({ port: 0 }, () => {\n          const port = wss.address().port;\n          const ws = new WebSocket(`ws://localhost:${port}`, 'foo');\n\n          assert.strictEqual(ws.extensions, '');\n\n          ws.on('open', () => {\n            assert.strictEqual(ws.protocol, 'foo');\n            ws.on('close', () => wss.close(done));\n          });\n        });\n\n        wss.on('connection', (ws) => {\n          assert.strictEqual(ws.protocol, 'foo');\n          ws.close();\n        });\n      });\n    });\n\n    describe('`readyState`', () => {\n      it('is enumerable and configurable', () => {\n        const descriptor = Object.getOwnPropertyDescriptor(\n          WebSocket.prototype,\n          'readyState'\n        );\n\n        assert.strictEqual(descriptor.configurable, true);\n        assert.strictEqual(descriptor.enumerable, true);\n        assert.ok(descriptor.get !== undefined);\n        assert.ok(descriptor.set === undefined);\n      });\n\n      it('defaults to `CONNECTING`', () => {\n        const ws = new WebSocket('ws://localhost', {\n          agent: new CustomAgent()\n        });\n\n        assert.strictEqual(ws.readyState, WebSocket.CONNECTING);\n      });\n\n      it('is set to `OPEN` once connection is established', (done) => {\n        const wss = new WebSocket.Server({ port: 0 }, () => {\n          const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n          ws.on('open', () => {\n            assert.strictEqual(ws.readyState, WebSocket.OPEN);\n            ws.close();\n          });\n\n          ws.on('close', () => wss.close(done));\n        });\n      });\n\n      it('is set to `CLOSED` once connection is closed', (done) => {\n        const wss = new WebSocket.Server({ port: 0 }, () => {\n          const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n          ws.on('close', () => {\n            assert.strictEqual(ws.readyState, WebSocket.CLOSED);\n            wss.close(done);\n          });\n\n          ws.on('open', () => ws.close(1001));\n        });\n      });\n\n      it('is set to `CLOSED` once connection is terminated', (done) => {\n        const wss = new WebSocket.Server({ port: 0 }, () => {\n          const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n          ws.on('close', () => {\n            assert.strictEqual(ws.readyState, WebSocket.CLOSED);\n            wss.close(done);\n          });\n\n          ws.on('open', () => ws.terminate());\n        });\n      });\n    });\n\n    describe('`url`', () => {\n      it('is enumerable and configurable', () => {\n        const descriptor = Object.getOwnPropertyDescriptor(\n          WebSocket.prototype,\n          'url'\n        );\n\n        assert.strictEqual(descriptor.configurable, true);\n        assert.strictEqual(descriptor.enumerable, true);\n        assert.ok(descriptor.get !== undefined);\n        assert.ok(descriptor.set === undefined);\n      });\n\n      it('exposes the server url', () => {\n        const schemes = new Map([\n          ['ws', 'ws'],\n          ['wss', 'wss'],\n          ['http', 'ws'],\n          ['https', 'wss']\n        ]);\n\n        for (const [key, value] of schemes) {\n          const ws = new WebSocket(`${key}://localhost/`, { lookup() {} });\n\n          assert.strictEqual(ws.url, `${value}://localhost/`);\n        }\n      });\n    });\n  });\n\n  describe('Events', () => {\n    it(\"emits an 'error' event if an error occurs (1/2)\", (done) => {\n      let clientCloseEventEmitted = false;\n      let serverClientCloseEventEmitted = false;\n\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('error', (err) => {\n          assert.ok(err instanceof RangeError);\n          assert.strictEqual(err.code, 'WS_ERR_INVALID_OPCODE');\n          assert.strictEqual(\n            err.message,\n            'Invalid WebSocket frame: invalid opcode 5'\n          );\n\n          ws.on('close', (code, reason) => {\n            assert.strictEqual(code, 1006);\n            assert.strictEqual(reason, EMPTY_BUFFER);\n\n            clientCloseEventEmitted = true;\n            if (serverClientCloseEventEmitted) wss.close(done);\n          });\n        });\n      });\n\n      wss.on('connection', (ws) => {\n        ws.on('close', (code, reason) => {\n          assert.strictEqual(code, 1002);\n          assert.deepStrictEqual(reason, EMPTY_BUFFER);\n\n          serverClientCloseEventEmitted = true;\n          if (clientCloseEventEmitted) wss.close(done);\n        });\n\n        ws._socket.write(Buffer.from([0x85, 0x00]));\n      });\n    });\n\n    it(\"emits an 'error' event if an error occurs (2/2)\", function (done) {\n      if (!fs.openAsBlob) return this.skip();\n\n      const randomString = crypto.randomBytes(4).toString('hex');\n      const file = path.join(os.tmpdir(), `ws-${randomString}.txt`);\n\n      fs.writeFileSync(file, 'x'.repeat(64));\n\n      fs.openAsBlob(file)\n        .then((blob) => {\n          fs.writeFileSync(file, 'x'.repeat(32));\n          runTest(blob);\n        })\n        .catch(done);\n\n      function runTest(blob) {\n        const wss = new WebSocket.Server({ port: 0 }, () => {\n          const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n        });\n\n        wss.on('connection', (ws) => {\n          ws.send(blob);\n\n          ws.on('error', (err) => {\n            try {\n              assert.ok(err instanceof DOMException);\n              assert.strictEqual(err.name, 'NotReadableError');\n              assert.strictEqual(err.message, 'The blob could not be read');\n            } finally {\n              fs.unlinkSync(file);\n            }\n\n            ws.on('close', () => {\n              wss.close(done);\n            });\n          });\n        });\n      }\n    });\n\n    it(\"emits the 'error' event only once (1/2)\", function (done) {\n      if (!fs.openAsBlob) return this.skip();\n\n      const randomString = crypto.randomBytes(4).toString('hex');\n      const file = path.join(os.tmpdir(), `ws-${randomString}.txt`);\n\n      fs.writeFileSync(file, 'x'.repeat(64));\n\n      fs.openAsBlob(file)\n        .then((blob) => {\n          fs.writeFileSync(file, 'x'.repeat(32));\n          runTest(blob);\n        })\n        .catch(done);\n\n      function runTest(blob) {\n        const wss = new WebSocket.Server(\n          {\n            perMessageDeflate: true,\n            port: 0\n          },\n          () => {\n            const ws = new WebSocket(`ws://localhost:${wss.address().port}`, {\n              perMessageDeflate: { threshold: 0 }\n            });\n\n            ws.on('open', () => {\n              ws.send('foo');\n              ws.send(blob);\n            });\n\n            ws.on('error', (err) => {\n              try {\n                assert.ok(err instanceof RangeError);\n                assert.strictEqual(err.code, 'WS_ERR_INVALID_OPCODE');\n                assert.strictEqual(\n                  err.message,\n                  'Invalid WebSocket frame: invalid opcode 5'\n                );\n              } finally {\n                fs.unlinkSync(file);\n              }\n\n              ws.on('close', () => {\n                wss.close(done);\n              });\n            });\n          }\n        );\n\n        wss.on('connection', (ws) => {\n          ws._socket.write(Buffer.from([0x85, 0x00]));\n        });\n      }\n    });\n\n    it(\"emits the 'error' event only once (2/2)\", function (done) {\n      if (!fs.openAsBlob) return this.skip();\n\n      const randomString = crypto.randomBytes(4).toString('hex');\n      const file = path.join(os.tmpdir(), `ws-${randomString}.txt`);\n\n      fs.writeFileSync(file, 'x'.repeat(64));\n\n      fs.openAsBlob(file)\n        .then((blob) => {\n          fs.writeFileSync(file, 'x'.repeat(32));\n          runTest(blob);\n        })\n        .catch(done);\n\n      function runTest(blob) {\n        const wss = new WebSocket.Server(\n          {\n            perMessageDeflate: true,\n            port: 0\n          },\n          () => {\n            const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n            ws.on('open', () => {\n              ws.send(blob);\n            });\n\n            ws.on('error', (err) => {\n              try {\n                assert.ok(err instanceof DOMException);\n                assert.strictEqual(err.name, 'NotReadableError');\n                assert.strictEqual(err.message, 'The blob could not be read');\n              } finally {\n                fs.unlinkSync(file);\n              }\n\n              ws.on('close', () => {\n                wss.close(done);\n              });\n            });\n          }\n        );\n\n        wss.on('connection', (ws) => {\n          const buf = Buffer.from('c10100'.repeat(5) + '8500', 'hex');\n\n          ws._socket.write(buf);\n        });\n      }\n    });\n\n    it(\"does not emit 'error' after 'close'\", function (done) {\n      if (!fs.openAsBlob) return this.skip();\n\n      const randomString = crypto.randomBytes(4).toString('hex');\n      const file = path.join(os.tmpdir(), `ws-${randomString}.bin`);\n\n      fs.writeFileSync(file, crypto.randomBytes(1024 * 1024));\n      fs.openAsBlob(file).then(runTest).catch(done);\n\n      function runTest(blob) {\n        const wss = new WebSocket.Server({ port: 0 }, () => {\n          const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n          ws.on('open', () => {\n            ws.send(blob, (err) => {\n              try {\n                assert.ok(err instanceof DOMException);\n                assert.strictEqual(err.name, 'NotReadableError');\n                assert.strictEqual(err.message, 'The blob could not be read');\n              } catch (e) {\n                ws.removeListener(onClose);\n                throw e;\n              } finally {\n                fs.unlinkSync(file);\n              }\n\n              wss.close(done);\n            });\n          });\n\n          ws.on('error', () => {\n            done(new Error(\"Unexpected 'error' event\"));\n          });\n          ws.on('close', onClose);\n\n          function onClose() {\n            fs.writeFileSync(file, crypto.randomBytes(32));\n          }\n        });\n\n        wss.on('connection', (ws) => {\n          ws._socket.end();\n        });\n      }\n    });\n\n    it('does not re-emit `net.Socket` errors', function (done) {\n      //\n      // `socket.resetAndDestroy()` is not available in Node.js < 16.17.0.\n      //\n      if (process.versions.modules < 93) return this.skip();\n\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('open', () => {\n          ws._socket.on('error', (err) => {\n            assert.ok(err instanceof Error);\n            assert.strictEqual(err.code, 'ECONNRESET');\n            ws.on('close', (code, message) => {\n              assert.strictEqual(code, 1006);\n              assert.strictEqual(message, EMPTY_BUFFER);\n              wss.close(done);\n            });\n          });\n\n          wss.clients.values().next().value._socket.resetAndDestroy();\n        });\n      });\n    });\n\n    it(\"emits an 'upgrade' event\", (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n        ws.on('upgrade', (res) => {\n          assert.ok(res instanceof http.IncomingMessage);\n          wss.close(done);\n        });\n      });\n\n      wss.on('connection', (ws) => {\n        ws.close();\n      });\n    });\n\n    it(\"emits a 'ping' event\", (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n        ws.on('ping', () => wss.close(done));\n      });\n\n      wss.on('connection', (ws) => {\n        ws.ping();\n        ws.close();\n      });\n    });\n\n    it(\"emits a 'pong' event\", (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n        ws.on('pong', () => wss.close(done));\n      });\n\n      wss.on('connection', (ws) => {\n        ws.pong();\n        ws.close();\n      });\n    });\n\n    it(\"emits a 'redirect' event\", (done) => {\n      const server = http.createServer();\n      const wss = new WebSocket.Server({ noServer: true, path: '/foo' });\n\n      server.once('upgrade', (req, socket) => {\n        socket.end('HTTP/1.1 302 Found\\r\\nLocation: /foo\\r\\n\\r\\n');\n        server.once('upgrade', (req, socket, head) => {\n          wss.handleUpgrade(req, socket, head, (ws) => {\n            ws.close();\n          });\n        });\n      });\n\n      server.listen(() => {\n        const port = server.address().port;\n        const ws = new WebSocket(`ws://localhost:${port}`, {\n          followRedirects: true\n        });\n\n        ws.on('redirect', (url, req) => {\n          assert.strictEqual(ws._redirects, 1);\n          assert.strictEqual(url, `ws://localhost:${port}/foo`);\n          assert.ok(req instanceof http.ClientRequest);\n\n          ws.on('close', (code) => {\n            assert.strictEqual(code, 1005);\n            server.close(done);\n          });\n        });\n      });\n    });\n  });\n\n  describe('Connection establishing', () => {\n    const server = http.createServer();\n\n    beforeEach((done) => server.listen(0, done));\n    afterEach((done) => server.close(done));\n\n    it('fails if the Upgrade header field value cannot be read', (done) => {\n      server.once('upgrade', (req, socket) => {\n        socket.on('end', socket.end);\n        socket.write(\n          'HTTP/1.1 101 Switching Protocols\\r\\n' +\n            'Connection: Upgrade\\r\\n' +\n            'Upgrade: websocket\\r\\n' +\n            '\\r\\n'\n        );\n      });\n\n      const ws = new WebSocket(`ws://localhost:${server.address().port}`);\n\n      ws._req.maxHeadersCount = 1;\n\n      ws.on('upgrade', (res) => {\n        assert.deepStrictEqual(res.headers, { connection: 'Upgrade' });\n\n        ws.on('error', (err) => {\n          assert.ok(err instanceof Error);\n          assert.strictEqual(err.message, 'Invalid Upgrade header');\n          done();\n        });\n      });\n    });\n\n    it('fails if the Upgrade header field value is not \"websocket\"', (done) => {\n      server.once('upgrade', (req, socket) => {\n        socket.on('end', socket.end);\n        socket.write(\n          'HTTP/1.1 101 Switching Protocols\\r\\n' +\n            'Connection: Upgrade\\r\\n' +\n            'Upgrade: foo\\r\\n' +\n            '\\r\\n'\n        );\n      });\n\n      const ws = new WebSocket(`ws://localhost:${server.address().port}`);\n\n      ws.on('error', (err) => {\n        assert.ok(err instanceof Error);\n        assert.strictEqual(err.message, 'Invalid Upgrade header');\n        done();\n      });\n    });\n\n    it('fails if the Sec-WebSocket-Accept header is invalid', (done) => {\n      server.once('upgrade', (req, socket) => {\n        socket.on('end', socket.end);\n        socket.write(\n          'HTTP/1.1 101 Switching Protocols\\r\\n' +\n            'Upgrade: websocket\\r\\n' +\n            'Connection: Upgrade\\r\\n' +\n            'Sec-WebSocket-Accept: CxYS6+NgJSBG74mdgLvGscRvpns=\\r\\n' +\n            '\\r\\n'\n        );\n      });\n\n      const ws = new WebSocket(`ws://localhost:${server.address().port}`);\n\n      ws.on('error', (err) => {\n        assert.ok(err instanceof Error);\n        assert.strictEqual(err.message, 'Invalid Sec-WebSocket-Accept header');\n        done();\n      });\n    });\n\n    it('close event is raised when server closes connection', (done) => {\n      server.once('upgrade', (req, socket) => {\n        const key = crypto\n          .createHash('sha1')\n          .update(req.headers['sec-websocket-key'] + GUID)\n          .digest('base64');\n\n        socket.end(\n          'HTTP/1.1 101 Switching Protocols\\r\\n' +\n            'Upgrade: websocket\\r\\n' +\n            'Connection: Upgrade\\r\\n' +\n            `Sec-WebSocket-Accept: ${key}\\r\\n` +\n            '\\r\\n'\n        );\n      });\n\n      const ws = new WebSocket(`ws://localhost:${server.address().port}`);\n\n      ws.on('close', (code, reason) => {\n        assert.strictEqual(code, 1006);\n        assert.strictEqual(reason, EMPTY_BUFFER);\n        done();\n      });\n    });\n\n    it('error is emitted if server aborts connection', (done) => {\n      server.once('upgrade', (req, socket) => {\n        socket.end(\n          `HTTP/1.1 401 ${http.STATUS_CODES[401]}\\r\\n` +\n            'Connection: close\\r\\n' +\n            'Content-type: text/html\\r\\n' +\n            `Content-Length: ${http.STATUS_CODES[401].length}\\r\\n` +\n            '\\r\\n'\n        );\n      });\n\n      const ws = new WebSocket(`ws://localhost:${server.address().port}`);\n\n      ws.on('open', () => done(new Error(\"Unexpected 'open' event\")));\n      ws.on('error', (err) => {\n        assert.ok(err instanceof Error);\n        assert.strictEqual(err.message, 'Unexpected server response: 401');\n        done();\n      });\n    });\n\n    it('unexpected response can be read when sent by server', (done) => {\n      server.once('upgrade', (req, socket) => {\n        socket.end(\n          `HTTP/1.1 401 ${http.STATUS_CODES[401]}\\r\\n` +\n            'Connection: close\\r\\n' +\n            'Content-type: text/html\\r\\n' +\n            'Content-Length: 3\\r\\n' +\n            '\\r\\n' +\n            'foo'\n        );\n      });\n\n      const ws = new WebSocket(`ws://localhost:${server.address().port}`);\n\n      ws.on('open', () => done(new Error(\"Unexpected 'open' event\")));\n      ws.on('error', () => done(new Error(\"Unexpected 'error' event\")));\n      ws.on('unexpected-response', (req, res) => {\n        assert.strictEqual(res.statusCode, 401);\n\n        let data = '';\n\n        res.on('data', (v) => {\n          data += v;\n        });\n\n        res.on('end', () => {\n          assert.strictEqual(data, 'foo');\n          done();\n        });\n      });\n    });\n\n    it('request can be aborted when unexpected response is sent by server', (done) => {\n      server.once('upgrade', (req, socket) => {\n        socket.end(\n          `HTTP/1.1 401 ${http.STATUS_CODES[401]}\\r\\n` +\n            'Connection: close\\r\\n' +\n            'Content-type: text/html\\r\\n' +\n            'Content-Length: 3\\r\\n' +\n            '\\r\\n' +\n            'foo'\n        );\n      });\n\n      const ws = new WebSocket(`ws://localhost:${server.address().port}`);\n\n      ws.on('open', () => done(new Error(\"Unexpected 'open' event\")));\n      ws.on('error', () => done(new Error(\"Unexpected 'error' event\")));\n      ws.on('unexpected-response', (req, res) => {\n        assert.strictEqual(res.statusCode, 401);\n\n        res.on('end', done);\n        req.abort();\n      });\n    });\n\n    it('fails if the opening handshake timeout expires', (done) => {\n      server.once('upgrade', (req, socket) => socket.on('end', socket.end));\n\n      const port = server.address().port;\n      const ws = new WebSocket(`ws://localhost:${port}`, {\n        handshakeTimeout: 100\n      });\n\n      ws.on('open', () => done(new Error(\"Unexpected 'open' event\")));\n      ws.on('error', (err) => {\n        assert.ok(err instanceof Error);\n        assert.strictEqual(err.message, 'Opening handshake has timed out');\n        done();\n      });\n    });\n\n    it('fails if an unexpected Sec-WebSocket-Extensions header is received', (done) => {\n      server.once('upgrade', (req, socket) => {\n        const key = crypto\n          .createHash('sha1')\n          .update(req.headers['sec-websocket-key'] + GUID)\n          .digest('base64');\n\n        socket.end(\n          'HTTP/1.1 101 Switching Protocols\\r\\n' +\n            'Upgrade: websocket\\r\\n' +\n            'Connection: Upgrade\\r\\n' +\n            `Sec-WebSocket-Accept: ${key}\\r\\n` +\n            'Sec-WebSocket-Extensions: foo\\r\\n' +\n            '\\r\\n'\n        );\n      });\n\n      const ws = new WebSocket(`ws://localhost:${server.address().port}`, {\n        perMessageDeflate: false\n      });\n\n      ws.on('open', () => done(new Error(\"Unexpected 'open' event\")));\n      ws.on('error', (err) => {\n        assert.ok(err instanceof Error);\n        assert.strictEqual(\n          err.message,\n          'Server sent a Sec-WebSocket-Extensions header but no extension ' +\n            'was requested'\n        );\n        ws.on('close', () => done());\n      });\n    });\n\n    it('fails if the Sec-WebSocket-Extensions header is invalid (1/2)', (done) => {\n      server.once('upgrade', (req, socket) => {\n        const key = crypto\n          .createHash('sha1')\n          .update(req.headers['sec-websocket-key'] + GUID)\n          .digest('base64');\n\n        socket.end(\n          'HTTP/1.1 101 Switching Protocols\\r\\n' +\n            'Upgrade: websocket\\r\\n' +\n            'Connection: Upgrade\\r\\n' +\n            `Sec-WebSocket-Accept: ${key}\\r\\n` +\n            'Sec-WebSocket-Extensions: foo;=\\r\\n' +\n            '\\r\\n'\n        );\n      });\n\n      const ws = new WebSocket(`ws://localhost:${server.address().port}`);\n\n      ws.on('open', () => done(new Error(\"Unexpected 'open' event\")));\n      ws.on('error', (err) => {\n        assert.ok(err instanceof Error);\n        assert.strictEqual(\n          err.message,\n          'Invalid Sec-WebSocket-Extensions header'\n        );\n        ws.on('close', () => done());\n      });\n    });\n\n    it('fails if the Sec-WebSocket-Extensions header is invalid (2/2)', (done) => {\n      server.once('upgrade', (req, socket) => {\n        const key = crypto\n          .createHash('sha1')\n          .update(req.headers['sec-websocket-key'] + GUID)\n          .digest('base64');\n\n        socket.end(\n          'HTTP/1.1 101 Switching Protocols\\r\\n' +\n            'Upgrade: websocket\\r\\n' +\n            'Connection: Upgrade\\r\\n' +\n            `Sec-WebSocket-Accept: ${key}\\r\\n` +\n            'Sec-WebSocket-Extensions: ' +\n            'permessage-deflate; client_max_window_bits=7\\r\\n' +\n            '\\r\\n'\n        );\n      });\n\n      const ws = new WebSocket(`ws://localhost:${server.address().port}`);\n\n      ws.on('open', () => done(new Error(\"Unexpected 'open' event\")));\n      ws.on('error', (err) => {\n        assert.ok(err instanceof Error);\n        assert.strictEqual(\n          err.message,\n          'Invalid Sec-WebSocket-Extensions header'\n        );\n        ws.on('close', () => done());\n      });\n    });\n\n    it('fails if an unexpected extension is received (1/2)', (done) => {\n      server.once('upgrade', (req, socket) => {\n        const key = crypto\n          .createHash('sha1')\n          .update(req.headers['sec-websocket-key'] + GUID)\n          .digest('base64');\n\n        socket.end(\n          'HTTP/1.1 101 Switching Protocols\\r\\n' +\n            'Upgrade: websocket\\r\\n' +\n            'Connection: Upgrade\\r\\n' +\n            `Sec-WebSocket-Accept: ${key}\\r\\n` +\n            'Sec-WebSocket-Extensions: foo\\r\\n' +\n            '\\r\\n'\n        );\n      });\n\n      const ws = new WebSocket(`ws://localhost:${server.address().port}`);\n\n      ws.on('open', () => done(new Error(\"Unexpected 'open' event\")));\n      ws.on('error', (err) => {\n        assert.ok(err instanceof Error);\n        assert.strictEqual(\n          err.message,\n          'Server indicated an extension that was not requested'\n        );\n        ws.on('close', () => done());\n      });\n    });\n\n    it('fails if an unexpected extension is received (2/2)', (done) => {\n      server.once('upgrade', (req, socket) => {\n        const key = crypto\n          .createHash('sha1')\n          .update(req.headers['sec-websocket-key'] + GUID)\n          .digest('base64');\n\n        socket.end(\n          'HTTP/1.1 101 Switching Protocols\\r\\n' +\n            'Upgrade: websocket\\r\\n' +\n            'Connection: Upgrade\\r\\n' +\n            `Sec-WebSocket-Accept: ${key}\\r\\n` +\n            'Sec-WebSocket-Extensions: permessage-deflate,foo\\r\\n' +\n            '\\r\\n'\n        );\n      });\n\n      const ws = new WebSocket(`ws://localhost:${server.address().port}`);\n\n      ws.on('open', () => done(new Error(\"Unexpected 'open' event\")));\n      ws.on('error', (err) => {\n        assert.ok(err instanceof Error);\n        assert.strictEqual(\n          err.message,\n          'Server indicated an extension that was not requested'\n        );\n        ws.on('close', () => done());\n      });\n    });\n\n    it('fails if server sends a subprotocol when none was requested', (done) => {\n      const wss = new WebSocket.Server({ server });\n\n      wss.on('headers', (headers) => {\n        headers.push('Sec-WebSocket-Protocol: foo');\n      });\n\n      const ws = new WebSocket(`ws://localhost:${server.address().port}`);\n\n      ws.on('open', () => done(new Error(\"Unexpected 'open' event\")));\n      ws.on('error', (err) => {\n        assert.ok(err instanceof Error);\n        assert.strictEqual(\n          err.message,\n          'Server sent a subprotocol but none was requested'\n        );\n        ws.on('close', () => wss.close(done));\n      });\n    });\n\n    it('fails if server sends an invalid subprotocol (1/2)', (done) => {\n      const wss = new WebSocket.Server({\n        handleProtocols: () => 'baz',\n        server\n      });\n\n      const ws = new WebSocket(`ws://localhost:${server.address().port}`, [\n        'foo',\n        'bar'\n      ]);\n\n      ws.on('open', () => done(new Error(\"Unexpected 'open' event\")));\n      ws.on('error', (err) => {\n        assert.ok(err instanceof Error);\n        assert.strictEqual(err.message, 'Server sent an invalid subprotocol');\n        ws.on('close', () => wss.close(done));\n      });\n    });\n\n    it('fails if server sends an invalid subprotocol (2/2)', (done) => {\n      server.once('upgrade', (req, socket) => {\n        const key = crypto\n          .createHash('sha1')\n          .update(req.headers['sec-websocket-key'] + GUID)\n          .digest('base64');\n\n        socket.end(\n          'HTTP/1.1 101 Switching Protocols\\r\\n' +\n            'Upgrade: websocket\\r\\n' +\n            'Connection: Upgrade\\r\\n' +\n            `Sec-WebSocket-Accept: ${key}\\r\\n` +\n            'Sec-WebSocket-Protocol:\\r\\n' +\n            '\\r\\n'\n        );\n      });\n\n      const ws = new WebSocket(`ws://localhost:${server.address().port}`, [\n        'foo',\n        'bar'\n      ]);\n\n      ws.on('open', () => done(new Error(\"Unexpected 'open' event\")));\n      ws.on('error', (err) => {\n        assert.ok(err instanceof Error);\n        assert.strictEqual(err.message, 'Server sent an invalid subprotocol');\n        ws.on('close', () => done());\n      });\n    });\n\n    it('fails if server sends no subprotocol', (done) => {\n      const wss = new WebSocket.Server({\n        handleProtocols() {},\n        server\n      });\n\n      const ws = new WebSocket(`ws://localhost:${server.address().port}`, [\n        'foo',\n        'bar'\n      ]);\n\n      ws.on('open', () => done(new Error(\"Unexpected 'open' event\")));\n      ws.on('error', (err) => {\n        assert.ok(err instanceof Error);\n        assert.strictEqual(err.message, 'Server sent no subprotocol');\n        ws.on('close', () => wss.close(done));\n      });\n    });\n\n    it('honors the `createConnection` option', (done) => {\n      const wss = new WebSocket.Server({ noServer: true, path: '/foo' });\n\n      server.once('upgrade', (req, socket, head) => {\n        assert.strictEqual(req.headers.host, 'google.com:22');\n        wss.handleUpgrade(req, socket, head, NOOP);\n      });\n\n      const ws = new WebSocket('ws://google.com:22/foo', {\n        createConnection: (options) => {\n          assert.strictEqual(options.host, 'google.com');\n          assert.strictEqual(options.port, '22');\n\n          // Ignore the `options` argument, and use the correct hostname and\n          // port to connect to the server.\n          return net.createConnection({\n            host: 'localhost',\n            port: server.address().port\n          });\n        }\n      });\n\n      ws.on('open', () => {\n        assert.strictEqual(ws.url, 'ws://google.com:22/foo');\n        ws.on('close', () => done());\n        ws.close();\n      });\n    });\n\n    it('does not follow redirects by default', (done) => {\n      server.once('upgrade', (req, socket) => {\n        socket.end(\n          'HTTP/1.1 301 Moved Permanently\\r\\n' +\n            'Location: ws://localhost:8080\\r\\n' +\n            '\\r\\n'\n        );\n      });\n\n      const ws = new WebSocket(`ws://localhost:${server.address().port}`);\n\n      ws.on('open', () => done(new Error(\"Unexpected 'open' event\")));\n      ws.on('error', (err) => {\n        assert.ok(err instanceof Error);\n        assert.strictEqual(err.message, 'Unexpected server response: 301');\n        assert.strictEqual(ws._redirects, 0);\n        ws.on('close', () => done());\n      });\n    });\n\n    it('honors the `followRedirects` option', (done) => {\n      const wss = new WebSocket.Server({ noServer: true, path: '/foo' });\n\n      server.once('upgrade', (req, socket) => {\n        socket.end('HTTP/1.1 302 Found\\r\\nLocation: /foo\\r\\n\\r\\n');\n        server.once('upgrade', (req, socket, head) => {\n          wss.handleUpgrade(req, socket, head, NOOP);\n        });\n      });\n\n      const port = server.address().port;\n      const ws = new WebSocket(`ws://localhost:${port}`, {\n        followRedirects: true\n      });\n\n      ws.on('open', () => {\n        assert.strictEqual(ws.url, `ws://localhost:${port}/foo`);\n        assert.strictEqual(ws._redirects, 1);\n        ws.on('close', () => done());\n        ws.close();\n      });\n    });\n\n    it('honors the `maxRedirects` option', (done) => {\n      const onUpgrade = (req, socket) => {\n        socket.end('HTTP/1.1 302 Found\\r\\nLocation: /\\r\\n\\r\\n');\n      };\n\n      server.on('upgrade', onUpgrade);\n\n      const ws = new WebSocket(`ws://localhost:${server.address().port}`, {\n        followRedirects: true,\n        maxRedirects: 1\n      });\n\n      ws.on('open', () => done(new Error(\"Unexpected 'open' event\")));\n      ws.on('error', (err) => {\n        assert.ok(err instanceof Error);\n        assert.strictEqual(err.message, 'Maximum redirects exceeded');\n        assert.strictEqual(ws._redirects, 2);\n\n        server.removeListener('upgrade', onUpgrade);\n        ws.on('close', () => done());\n      });\n    });\n\n    it('emits an error if the redirect URL is invalid (1/2)', (done) => {\n      server.once('upgrade', (req, socket) => {\n        socket.end('HTTP/1.1 302 Found\\r\\nLocation: ws://\\r\\n\\r\\n');\n      });\n\n      const ws = new WebSocket(`ws://localhost:${server.address().port}`, {\n        followRedirects: true\n      });\n\n      ws.on('open', () => done(new Error(\"Unexpected 'open' event\")));\n      ws.on('error', (err) => {\n        assert.ok(err instanceof SyntaxError);\n        assert.strictEqual(err.message, 'Invalid URL: ws://');\n        assert.strictEqual(ws._redirects, 1);\n\n        ws.on('close', () => done());\n      });\n    });\n\n    it('emits an error if the redirect URL is invalid (2/2)', (done) => {\n      server.once('upgrade', (req, socket) => {\n        socket.end(\n          'HTTP/1.1 302 Found\\r\\nLocation: bad-scheme://localhost\\r\\n\\r\\n'\n        );\n      });\n\n      const ws = new WebSocket(`ws://localhost:${server.address().port}`, {\n        followRedirects: true\n      });\n\n      ws.on('open', () => done(new Error(\"Unexpected 'open' event\")));\n      ws.on('error', (err) => {\n        assert.ok(err instanceof SyntaxError);\n        assert.strictEqual(\n          err.message,\n          'The URL\\'s protocol must be one of \"ws:\", \"wss:\", ' +\n            '\"http:\", \"https:\", or \"ws+unix:\"'\n        );\n        assert.strictEqual(ws._redirects, 1);\n\n        ws.on('close', () => done());\n      });\n    });\n\n    it('uses the first url userinfo when following redirects', (done) => {\n      const wss = new WebSocket.Server({ noServer: true, path: '/foo' });\n      const authorization = 'Basic Zm9vOmJhcg==';\n\n      server.once('upgrade', (req, socket) => {\n        socket.end(\n          'HTTP/1.1 302 Found\\r\\n' +\n            `Location: ws://baz:qux@localhost:${port}/foo\\r\\n\\r\\n`\n        );\n        server.once('upgrade', (req, socket, head) => {\n          wss.handleUpgrade(req, socket, head, (ws, req) => {\n            assert.strictEqual(req.headers.authorization, authorization);\n            ws.close();\n          });\n        });\n      });\n\n      const port = server.address().port;\n      const ws = new WebSocket(`ws://foo:bar@localhost:${port}`, {\n        followRedirects: true\n      });\n\n      assert.strictEqual(ws._req.getHeader('Authorization'), authorization);\n\n      ws.on('close', (code) => {\n        assert.strictEqual(code, 1005);\n        assert.strictEqual(ws.url, `ws://baz:qux@localhost:${port}/foo`);\n        assert.strictEqual(ws._redirects, 1);\n\n        wss.close(done);\n      });\n    });\n\n    describe('When moving away from a secure context', () => {\n      function proxy(httpServer, httpsServer) {\n        const server = net.createServer({ allowHalfOpen: true });\n\n        server.on('connection', (socket) => {\n          socket.on('readable', function read() {\n            socket.removeListener('readable', read);\n\n            const buf = socket.read(1);\n            const target = buf[0] === 22 ? httpsServer : httpServer;\n\n            socket.unshift(buf);\n            target.emit('connection', socket);\n          });\n        });\n\n        return server;\n      }\n\n      describe(\"If there is no 'redirect' event listener\", () => {\n        it('drops the `auth` option', (done) => {\n          const httpServer = http.createServer();\n          const httpsServer = https.createServer({\n            cert: fs.readFileSync('test/fixtures/certificate.pem'),\n            key: fs.readFileSync('test/fixtures/key.pem')\n          });\n          const server = proxy(httpServer, httpsServer);\n\n          server.listen(() => {\n            const port = server.address().port;\n\n            httpsServer.on('upgrade', (req, socket) => {\n              socket.on('error', NOOP);\n              socket.end(\n                'HTTP/1.1 302 Found\\r\\n' +\n                  `Location: ws://localhost:${port}/\\r\\n\\r\\n`\n              );\n            });\n\n            const wss = new WebSocket.Server({ server: httpServer });\n\n            wss.on('connection', (ws, req) => {\n              assert.strictEqual(req.headers.authorization, undefined);\n              ws.close();\n            });\n\n            const ws = new WebSocket(`wss://localhost:${port}`, {\n              auth: 'foo:bar',\n              followRedirects: true,\n              rejectUnauthorized: false\n            });\n\n            assert.strictEqual(\n              ws._req.getHeader('Authorization'),\n              'Basic Zm9vOmJhcg=='\n            );\n\n            ws.on('close', (code) => {\n              assert.strictEqual(code, 1005);\n              assert.strictEqual(ws.url, `ws://localhost:${port}/`);\n              assert.strictEqual(ws._redirects, 1);\n\n              server.close(done);\n            });\n          });\n        });\n\n        it('drops the Authorization and Cookie headers', (done) => {\n          const httpServer = http.createServer();\n          const httpsServer = https.createServer({\n            cert: fs.readFileSync('test/fixtures/certificate.pem'),\n            key: fs.readFileSync('test/fixtures/key.pem')\n          });\n          const server = proxy(httpServer, httpsServer);\n\n          server.listen(() => {\n            const port = server.address().port;\n\n            httpsServer.on('upgrade', (req, socket) => {\n              socket.on('error', NOOP);\n              socket.end(\n                'HTTP/1.1 302 Found\\r\\n' +\n                  `Location: ws://localhost:${port}/\\r\\n\\r\\n`\n              );\n            });\n\n            const headers = {\n              authorization: 'Basic Zm9vOmJhcg==',\n              cookie: 'foo=bar',\n              host: 'foo'\n            };\n\n            const wss = new WebSocket.Server({ server: httpServer });\n\n            wss.on('connection', (ws, req) => {\n              assert.strictEqual(req.headers.authorization, undefined);\n              assert.strictEqual(req.headers.cookie, undefined);\n              assert.strictEqual(req.headers.host, headers.host);\n\n              ws.close();\n            });\n\n            const ws = new WebSocket(`wss://localhost:${port}`, {\n              followRedirects: true,\n              headers,\n              rejectUnauthorized: false\n            });\n\n            const firstRequest = ws._req;\n\n            assert.strictEqual(\n              firstRequest.getHeader('Authorization'),\n              headers.authorization\n            );\n            assert.strictEqual(\n              firstRequest.getHeader('Cookie'),\n              headers.cookie\n            );\n            assert.strictEqual(firstRequest.getHeader('Host'), headers.host);\n\n            ws.on('close', (code) => {\n              assert.strictEqual(code, 1005);\n              assert.strictEqual(ws.url, `ws://localhost:${port}/`);\n              assert.strictEqual(ws._redirects, 1);\n\n              server.close(done);\n            });\n          });\n        });\n      });\n\n      describe(\"If there is at least one 'redirect' event listener\", () => {\n        it('does not drop any headers by default', (done) => {\n          const httpServer = http.createServer();\n          const httpsServer = https.createServer({\n            cert: fs.readFileSync('test/fixtures/certificate.pem'),\n            key: fs.readFileSync('test/fixtures/key.pem')\n          });\n          const server = proxy(httpServer, httpsServer);\n\n          server.listen(() => {\n            const port = server.address().port;\n\n            httpsServer.on('upgrade', (req, socket) => {\n              socket.on('error', NOOP);\n              socket.end(\n                'HTTP/1.1 302 Found\\r\\n' +\n                  `Location: ws://localhost:${port}/\\r\\n\\r\\n`\n              );\n            });\n\n            const headers = {\n              authorization: 'Basic Zm9vOmJhcg==',\n              cookie: 'foo=bar',\n              host: 'foo'\n            };\n\n            const wss = new WebSocket.Server({ server: httpServer });\n\n            wss.on('connection', (ws, req) => {\n              assert.strictEqual(\n                req.headers.authorization,\n                headers.authorization\n              );\n              assert.strictEqual(req.headers.cookie, headers.cookie);\n              assert.strictEqual(req.headers.host, headers.host);\n\n              ws.close();\n            });\n\n            const ws = new WebSocket(`wss://localhost:${port}`, {\n              followRedirects: true,\n              headers,\n              rejectUnauthorized: false\n            });\n\n            const firstRequest = ws._req;\n\n            assert.strictEqual(\n              firstRequest.getHeader('Authorization'),\n              headers.authorization\n            );\n            assert.strictEqual(\n              firstRequest.getHeader('Cookie'),\n              headers.cookie\n            );\n            assert.strictEqual(firstRequest.getHeader('Host'), headers.host);\n\n            ws.on('redirect', (url, req) => {\n              assert.strictEqual(ws._redirects, 1);\n              assert.strictEqual(url, `ws://localhost:${port}/`);\n              assert.notStrictEqual(firstRequest, req);\n              assert.strictEqual(\n                req.getHeader('Authorization'),\n                headers.authorization\n              );\n              assert.strictEqual(req.getHeader('Cookie'), headers.cookie);\n              assert.strictEqual(req.getHeader('Host'), headers.host);\n\n              ws.on('close', (code) => {\n                assert.strictEqual(code, 1005);\n                server.close(done);\n              });\n            });\n          });\n        });\n      });\n    });\n\n    describe('When the redirect host is different', () => {\n      describe(\"If there is no 'redirect' event listener\", () => {\n        it('drops the `auth` option', (done) => {\n          const wss = new WebSocket.Server({ port: 0 }, () => {\n            const port = wss.address().port;\n\n            server.once('upgrade', (req, socket) => {\n              socket.end(\n                'HTTP/1.1 302 Found\\r\\n' +\n                  `Location: ws://localhost:${port}/\\r\\n\\r\\n`\n              );\n            });\n\n            const ws = new WebSocket(\n              `ws://localhost:${server.address().port}`,\n              {\n                auth: 'foo:bar',\n                followRedirects: true\n              }\n            );\n\n            assert.strictEqual(\n              ws._req.getHeader('Authorization'),\n              'Basic Zm9vOmJhcg=='\n            );\n\n            ws.on('close', (code) => {\n              assert.strictEqual(code, 1005);\n              assert.strictEqual(ws.url, `ws://localhost:${port}/`);\n              assert.strictEqual(ws._redirects, 1);\n\n              wss.close(done);\n            });\n          });\n\n          wss.on('connection', (ws, req) => {\n            assert.strictEqual(req.headers.authorization, undefined);\n            ws.close();\n          });\n        });\n\n        it('drops the Authorization, Cookie and Host headers (1/4)', (done) => {\n          // Test the `ws:` to `ws:` case.\n\n          const wss = new WebSocket.Server({ port: 0 }, () => {\n            const port = wss.address().port;\n\n            server.once('upgrade', (req, socket) => {\n              socket.end(\n                'HTTP/1.1 302 Found\\r\\n' +\n                  `Location: ws://localhost:${port}/\\r\\n\\r\\n`\n              );\n            });\n\n            const headers = {\n              authorization: 'Basic Zm9vOmJhcg==',\n              cookie: 'foo=bar',\n              host: 'foo'\n            };\n\n            const ws = new WebSocket(\n              `ws://localhost:${server.address().port}`,\n              { followRedirects: true, headers }\n            );\n\n            const firstRequest = ws._req;\n\n            assert.strictEqual(\n              firstRequest.getHeader('Authorization'),\n              headers.authorization\n            );\n            assert.strictEqual(\n              firstRequest.getHeader('Cookie'),\n              headers.cookie\n            );\n            assert.strictEqual(firstRequest.getHeader('Host'), headers.host);\n\n            ws.on('close', (code) => {\n              assert.strictEqual(code, 1005);\n              assert.strictEqual(ws.url, `ws://localhost:${port}/`);\n              assert.strictEqual(ws._redirects, 1);\n\n              wss.close(done);\n            });\n          });\n\n          wss.on('connection', (ws, req) => {\n            assert.strictEqual(req.headers.authorization, undefined);\n            assert.strictEqual(req.headers.cookie, undefined);\n            assert.strictEqual(\n              req.headers.host,\n              `localhost:${wss.address().port}`\n            );\n\n            ws.close();\n          });\n        });\n\n        it('drops the Authorization, Cookie and Host headers (2/4)', (done) => {\n          // Test the `ws:` to `ws+unix:` case.\n\n          const randomString = crypto.randomBytes(4).toString('hex');\n          const ipcPath =\n            process.platform === 'win32'\n              ? `\\\\\\\\.\\\\pipe\\\\ws-pipe-${randomString}`\n              : path.join(os.tmpdir(), `ws-${randomString}.sock`);\n\n          server.once('upgrade', (req, socket) => {\n            socket.end(\n              `HTTP/1.1 302 Found\\r\\nLocation: ws+unix:${ipcPath}\\r\\n\\r\\n`\n            );\n          });\n\n          const redirectedServer = http.createServer();\n          const wss = new WebSocket.Server({ server: redirectedServer });\n\n          wss.on('connection', (ws, req) => {\n            assert.strictEqual(req.headers.authorization, undefined);\n            assert.strictEqual(req.headers.cookie, undefined);\n            assert.strictEqual(req.headers.host, 'localhost');\n\n            ws.close();\n          });\n\n          redirectedServer.listen(ipcPath, () => {\n            const headers = {\n              authorization: 'Basic Zm9vOmJhcg==',\n              cookie: 'foo=bar',\n              host: 'foo'\n            };\n\n            const ws = new WebSocket(\n              `ws://localhost:${server.address().port}`,\n              { followRedirects: true, headers }\n            );\n\n            const firstRequest = ws._req;\n\n            assert.strictEqual(\n              firstRequest.getHeader('Authorization'),\n              headers.authorization\n            );\n            assert.strictEqual(\n              firstRequest.getHeader('Cookie'),\n              headers.cookie\n            );\n            assert.strictEqual(firstRequest.getHeader('Host'), headers.host);\n\n            ws.on('close', (code) => {\n              assert.strictEqual(code, 1005);\n              assert.strictEqual(ws.url, `ws+unix:${ipcPath}`);\n              assert.strictEqual(ws._redirects, 1);\n\n              redirectedServer.close(done);\n            });\n          });\n        });\n\n        it('drops the Authorization, Cookie and Host headers (3/4)', (done) => {\n          // Test the `ws+unix:` to `ws+unix:` case.\n\n          const randomString1 = crypto.randomBytes(4).toString('hex');\n          const randomString2 = crypto.randomBytes(4).toString('hex');\n          let redirectingServerIpcPath;\n          let redirectedServerIpcPath;\n\n          if (process.platform === 'win32') {\n            redirectingServerIpcPath = `\\\\\\\\.\\\\pipe\\\\ws-pipe-${randomString1}`;\n            redirectedServerIpcPath = `\\\\\\\\.\\\\pipe\\\\ws-pipe-${randomString2}`;\n          } else {\n            redirectingServerIpcPath = path.join(\n              os.tmpdir(),\n              `ws-${randomString1}.sock`\n            );\n            redirectedServerIpcPath = path.join(\n              os.tmpdir(),\n              `ws-${randomString2}.sock`\n            );\n          }\n\n          const redirectingServer = http.createServer();\n\n          redirectingServer.on('upgrade', (req, socket) => {\n            socket.end(\n              'HTTP/1.1 302 Found\\r\\n' +\n                `Location: ws+unix:${redirectedServerIpcPath}\\r\\n\\r\\n`\n            );\n          });\n\n          const redirectedServer = http.createServer();\n          const wss = new WebSocket.Server({ server: redirectedServer });\n\n          wss.on('connection', (ws, req) => {\n            assert.strictEqual(req.headers.authorization, undefined);\n            assert.strictEqual(req.headers.cookie, undefined);\n            assert.strictEqual(req.headers.host, 'localhost');\n\n            ws.close();\n          });\n\n          redirectingServer.listen(redirectingServerIpcPath, listening);\n          redirectedServer.listen(redirectedServerIpcPath, listening);\n\n          let callCount = 0;\n\n          function listening() {\n            if (++callCount !== 2) return;\n\n            const headers = {\n              authorization: 'Basic Zm9vOmJhcg==',\n              cookie: 'foo=bar',\n              host: 'foo'\n            };\n\n            const ws = new WebSocket(`ws+unix:${redirectingServerIpcPath}`, {\n              followRedirects: true,\n              headers\n            });\n\n            const firstRequest = ws._req;\n\n            assert.strictEqual(\n              firstRequest.getHeader('Authorization'),\n              headers.authorization\n            );\n            assert.strictEqual(\n              firstRequest.getHeader('Cookie'),\n              headers.cookie\n            );\n            assert.strictEqual(firstRequest.getHeader('Host'), headers.host);\n\n            ws.on('close', (code) => {\n              assert.strictEqual(code, 1005);\n              assert.strictEqual(ws.url, `ws+unix:${redirectedServerIpcPath}`);\n              assert.strictEqual(ws._redirects, 1);\n\n              redirectingServer.close();\n              redirectedServer.close(done);\n            });\n          }\n        });\n\n        it('drops the Authorization, Cookie and Host headers (4/4)', (done) => {\n          // Test the `ws+unix:` to `ws:` case.\n\n          const redirectingServer = http.createServer();\n          const redirectedServer = http.createServer();\n          const wss = new WebSocket.Server({ server: redirectedServer });\n\n          wss.on('connection', (ws, req) => {\n            assert.strictEqual(req.headers.authorization, undefined);\n            assert.strictEqual(req.headers.cookie, undefined);\n            assert.strictEqual(\n              req.headers.host,\n              `localhost:${redirectedServer.address().port}`\n            );\n\n            ws.close();\n          });\n\n          const randomString = crypto.randomBytes(4).toString('hex');\n          const ipcPath =\n            process.platform === 'win32'\n              ? `\\\\\\\\.\\\\pipe\\\\ws-pipe-${randomString}`\n              : path.join(os.tmpdir(), `ws-${randomString}.sock`);\n\n          redirectingServer.listen(ipcPath, listening);\n          redirectedServer.listen(0, listening);\n\n          let callCount = 0;\n\n          function listening() {\n            if (++callCount !== 2) return;\n\n            const port = redirectedServer.address().port;\n\n            redirectingServer.on('upgrade', (req, socket) => {\n              socket.end(\n                `HTTP/1.1 302 Found\\r\\nLocation: ws://localhost:${port}\\r\\n\\r\\n`\n              );\n            });\n\n            const headers = {\n              authorization: 'Basic Zm9vOmJhcg==',\n              cookie: 'foo=bar',\n              host: 'foo'\n            };\n\n            const ws = new WebSocket(`ws+unix:${ipcPath}`, {\n              followRedirects: true,\n              headers\n            });\n\n            const firstRequest = ws._req;\n\n            assert.strictEqual(\n              firstRequest.getHeader('Authorization'),\n              headers.authorization\n            );\n            assert.strictEqual(\n              firstRequest.getHeader('Cookie'),\n              headers.cookie\n            );\n            assert.strictEqual(firstRequest.getHeader('Host'), headers.host);\n\n            ws.on('close', (code) => {\n              assert.strictEqual(code, 1005);\n              assert.strictEqual(ws.url, `ws://localhost:${port}/`);\n              assert.strictEqual(ws._redirects, 1);\n\n              redirectingServer.close();\n              redirectedServer.close(done);\n            });\n          }\n        });\n      });\n\n      describe(\"If there is at least one 'redirect' event listener\", () => {\n        it('does not drop any headers by default', (done) => {\n          const headers = {\n            authorization: 'Basic Zm9vOmJhcg==',\n            cookie: 'foo=bar',\n            host: 'foo'\n          };\n\n          const wss = new WebSocket.Server({ port: 0 }, () => {\n            const port = wss.address().port;\n\n            server.once('upgrade', (req, socket) => {\n              socket.end(\n                'HTTP/1.1 302 Found\\r\\n' +\n                  `Location: ws://localhost:${port}/\\r\\n\\r\\n`\n              );\n            });\n\n            const ws = new WebSocket(\n              `ws://localhost:${server.address().port}`,\n              { followRedirects: true, headers }\n            );\n\n            const firstRequest = ws._req;\n\n            assert.strictEqual(\n              firstRequest.getHeader('Authorization'),\n              headers.authorization\n            );\n            assert.strictEqual(\n              firstRequest.getHeader('Cookie'),\n              headers.cookie\n            );\n            assert.strictEqual(firstRequest.getHeader('Host'), headers.host);\n\n            ws.on('redirect', (url, req) => {\n              assert.strictEqual(ws._redirects, 1);\n              assert.strictEqual(url, `ws://localhost:${port}/`);\n              assert.notStrictEqual(firstRequest, req);\n              assert.strictEqual(\n                req.getHeader('Authorization'),\n                headers.authorization\n              );\n              assert.strictEqual(req.getHeader('Cookie'), headers.cookie);\n              assert.strictEqual(req.getHeader('Host'), headers.host);\n\n              ws.on('close', (code) => {\n                assert.strictEqual(code, 1005);\n                wss.close(done);\n              });\n            });\n          });\n\n          wss.on('connection', (ws, req) => {\n            assert.strictEqual(\n              req.headers.authorization,\n              headers.authorization\n            );\n            assert.strictEqual(req.headers.cookie, headers.cookie);\n            assert.strictEqual(req.headers.host, headers.host);\n            ws.close();\n          });\n        });\n      });\n    });\n\n    describe(\"In a listener of the 'redirect' event\", () => {\n      it('allows to abort the request without swallowing errors', (done) => {\n        server.once('upgrade', (req, socket) => {\n          socket.end('HTTP/1.1 302 Found\\r\\nLocation: /foo\\r\\n\\r\\n');\n        });\n\n        const port = server.address().port;\n        const ws = new WebSocket(`ws://localhost:${port}`, {\n          followRedirects: true\n        });\n\n        ws.on('redirect', (url, req) => {\n          assert.strictEqual(ws._redirects, 1);\n          assert.strictEqual(url, `ws://localhost:${port}/foo`);\n\n          req.on('socket', () => {\n            req.abort();\n          });\n\n          ws.on('error', (err) => {\n            assert.ok(err instanceof Error);\n            assert.strictEqual(err.message, 'socket hang up');\n\n            ws.on('close', (code) => {\n              assert.strictEqual(code, 1006);\n              done();\n            });\n          });\n        });\n      });\n\n      it('allows to remove headers', (done) => {\n        const wss = new WebSocket.Server({ port: 0 }, () => {\n          const port = wss.address().port;\n\n          server.once('upgrade', (req, socket) => {\n            socket.end(\n              'HTTP/1.1 302 Found\\r\\n' +\n                `Location: ws://localhost:${port}/\\r\\n\\r\\n`\n            );\n          });\n\n          const headers = {\n            authorization: 'Basic Zm9vOmJhcg==',\n            cookie: 'foo=bar'\n          };\n\n          const ws = new WebSocket(`ws://localhost:${server.address().port}`, {\n            followRedirects: true,\n            headers\n          });\n\n          ws.on('redirect', (url, req) => {\n            assert.strictEqual(ws._redirects, 1);\n            assert.strictEqual(url, `ws://localhost:${port}/`);\n            assert.strictEqual(\n              req.getHeader('Authorization'),\n              headers.authorization\n            );\n            assert.strictEqual(req.getHeader('Cookie'), headers.cookie);\n\n            req.removeHeader('authorization');\n            req.removeHeader('cookie');\n\n            ws.on('close', (code) => {\n              assert.strictEqual(code, 1005);\n              wss.close(done);\n            });\n          });\n        });\n\n        wss.on('connection', (ws, req) => {\n          assert.strictEqual(req.headers.authorization, undefined);\n          assert.strictEqual(req.headers.cookie, undefined);\n          ws.close();\n        });\n      });\n    });\n  });\n\n  describe('#pause', () => {\n    it('does nothing if `readyState` is `CONNECTING` or `CLOSED`', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        assert.strictEqual(ws.readyState, WebSocket.CONNECTING);\n        assert.ok(!ws.isPaused);\n\n        ws.pause();\n        assert.ok(!ws.isPaused);\n\n        ws.on('open', () => {\n          ws.on('close', () => {\n            assert.strictEqual(ws.readyState, WebSocket.CLOSED);\n\n            ws.pause();\n            assert.ok(!ws.isPaused);\n\n            wss.close(done);\n          });\n\n          ws.close();\n        });\n      });\n    });\n\n    it('pauses the socket', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n      });\n\n      wss.on('connection', (ws) => {\n        assert.ok(!ws.isPaused);\n        assert.ok(!ws._socket.isPaused());\n\n        ws.pause();\n        assert.ok(ws.isPaused);\n        assert.ok(ws._socket.isPaused());\n\n        ws.terminate();\n        wss.close(done);\n      });\n    });\n  });\n\n  describe('#ping', () => {\n    it('throws an error if `readyState` is `CONNECTING`', () => {\n      const ws = new WebSocket('ws://localhost', {\n        lookup() {}\n      });\n\n      assert.throws(\n        () => ws.ping(),\n        /^Error: WebSocket is not open: readyState 0 \\(CONNECTING\\)$/\n      );\n\n      assert.throws(\n        () => ws.ping(NOOP),\n        /^Error: WebSocket is not open: readyState 0 \\(CONNECTING\\)$/\n      );\n    });\n\n    it('increases `bufferedAmount` if `readyState` is 2 or 3', (done) => {\n      const ws = new WebSocket('ws://localhost', {\n        lookup() {}\n      });\n\n      ws.on('error', (err) => {\n        assert.ok(err instanceof Error);\n        assert.strictEqual(\n          err.message,\n          'WebSocket was closed before the connection was established'\n        );\n\n        assert.strictEqual(ws.readyState, WebSocket.CLOSING);\n        assert.strictEqual(ws.bufferedAmount, 0);\n\n        ws.ping('hi');\n        assert.strictEqual(ws.bufferedAmount, 2);\n\n        ws.ping();\n        assert.strictEqual(ws.bufferedAmount, 2);\n\n        ws.on('close', () => {\n          assert.strictEqual(ws.readyState, WebSocket.CLOSED);\n\n          ws.ping('hi');\n          assert.strictEqual(ws.bufferedAmount, 4);\n\n          ws.ping();\n          assert.strictEqual(ws.bufferedAmount, 4);\n\n          if (hasBlob) {\n            ws.ping(new Blob(['hi']));\n            assert.strictEqual(ws.bufferedAmount, 6);\n          }\n\n          done();\n        });\n      });\n\n      ws.close();\n    });\n\n    it('calls the callback w/ an error if `readyState` is 2 or 3', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n      });\n\n      wss.on('connection', (ws) => {\n        ws.close();\n\n        assert.strictEqual(ws.bufferedAmount, 0);\n\n        ws.ping('hi', (err) => {\n          assert.ok(err instanceof Error);\n          assert.strictEqual(\n            err.message,\n            'WebSocket is not open: readyState 2 (CLOSING)'\n          );\n          assert.strictEqual(ws.bufferedAmount, 2);\n\n          ws.on('close', () => {\n            ws.ping((err) => {\n              assert.ok(err instanceof Error);\n              assert.strictEqual(\n                err.message,\n                'WebSocket is not open: readyState 3 (CLOSED)'\n              );\n              assert.strictEqual(ws.bufferedAmount, 2);\n\n              wss.close(done);\n            });\n          });\n        });\n      });\n    });\n\n    it('can send a ping with no data', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('open', () => {\n          ws.ping(() => {\n            ws.ping();\n            ws.close();\n          });\n        });\n      });\n\n      wss.on('connection', (ws) => {\n        let pings = 0;\n        ws.on('ping', (data) => {\n          assert.ok(Buffer.isBuffer(data));\n          assert.strictEqual(data.length, 0);\n          if (++pings === 2) wss.close(done);\n        });\n      });\n    });\n\n    it('can send a ping with data', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('open', () => {\n          ws.ping('hi', () => {\n            ws.ping('hi', true);\n            ws.close();\n          });\n        });\n      });\n\n      wss.on('connection', (ws) => {\n        let pings = 0;\n        ws.on('ping', (message) => {\n          assert.strictEqual(message.toString(), 'hi');\n          if (++pings === 2) wss.close(done);\n        });\n      });\n    });\n\n    it('can send numbers as ping payload', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('open', () => {\n          ws.ping(0);\n          ws.close();\n        });\n      });\n\n      wss.on('connection', (ws) => {\n        ws.on('ping', (message) => {\n          assert.strictEqual(message.toString(), '0');\n          wss.close(done);\n        });\n      });\n    });\n\n    it('throws an error if the data size is greater than 125 bytes', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('open', () => {\n          assert.throws(\n            () => ws.ping(Buffer.alloc(126)),\n            /^RangeError: The data size must not be greater than 125 bytes$/\n          );\n\n          wss.close(done);\n        });\n      });\n\n      wss.on('connection', (ws) => {\n        ws.close();\n      });\n    });\n  });\n\n  describe('#pong', () => {\n    it('throws an error if `readyState` is `CONNECTING`', () => {\n      const ws = new WebSocket('ws://localhost', {\n        lookup() {}\n      });\n\n      assert.throws(\n        () => ws.pong(),\n        /^Error: WebSocket is not open: readyState 0 \\(CONNECTING\\)$/\n      );\n\n      assert.throws(\n        () => ws.pong(NOOP),\n        /^Error: WebSocket is not open: readyState 0 \\(CONNECTING\\)$/\n      );\n    });\n\n    it('increases `bufferedAmount` if `readyState` is 2 or 3', (done) => {\n      const ws = new WebSocket('ws://localhost', {\n        lookup() {}\n      });\n\n      ws.on('error', (err) => {\n        assert.ok(err instanceof Error);\n        assert.strictEqual(\n          err.message,\n          'WebSocket was closed before the connection was established'\n        );\n\n        assert.strictEqual(ws.readyState, WebSocket.CLOSING);\n        assert.strictEqual(ws.bufferedAmount, 0);\n\n        ws.pong('hi');\n        assert.strictEqual(ws.bufferedAmount, 2);\n\n        ws.pong();\n        assert.strictEqual(ws.bufferedAmount, 2);\n\n        ws.on('close', () => {\n          assert.strictEqual(ws.readyState, WebSocket.CLOSED);\n\n          ws.pong('hi');\n          assert.strictEqual(ws.bufferedAmount, 4);\n\n          ws.pong();\n          assert.strictEqual(ws.bufferedAmount, 4);\n\n          if (hasBlob) {\n            ws.pong(new Blob(['hi']));\n            assert.strictEqual(ws.bufferedAmount, 6);\n          }\n\n          done();\n        });\n      });\n\n      ws.close();\n    });\n\n    it('calls the callback w/ an error if `readyState` is 2 or 3', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n      });\n\n      wss.on('connection', (ws) => {\n        ws.close();\n\n        assert.strictEqual(ws.bufferedAmount, 0);\n\n        ws.pong('hi', (err) => {\n          assert.ok(err instanceof Error);\n          assert.strictEqual(\n            err.message,\n            'WebSocket is not open: readyState 2 (CLOSING)'\n          );\n          assert.strictEqual(ws.bufferedAmount, 2);\n\n          ws.on('close', () => {\n            ws.pong((err) => {\n              assert.ok(err instanceof Error);\n              assert.strictEqual(\n                err.message,\n                'WebSocket is not open: readyState 3 (CLOSED)'\n              );\n              assert.strictEqual(ws.bufferedAmount, 2);\n\n              wss.close(done);\n            });\n          });\n        });\n      });\n    });\n\n    it('can send a pong with no data', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('open', () => {\n          ws.pong(() => {\n            ws.pong();\n            ws.close();\n          });\n        });\n      });\n\n      wss.on('connection', (ws) => {\n        let pongs = 0;\n        ws.on('pong', (data) => {\n          assert.ok(Buffer.isBuffer(data));\n          assert.strictEqual(data.length, 0);\n          if (++pongs === 2) wss.close(done);\n        });\n      });\n    });\n\n    it('can send a pong with data', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('open', () => {\n          ws.pong('hi', () => {\n            ws.pong('hi', true);\n            ws.close();\n          });\n        });\n      });\n\n      wss.on('connection', (ws) => {\n        let pongs = 0;\n        ws.on('pong', (message) => {\n          assert.strictEqual(message.toString(), 'hi');\n          if (++pongs === 2) wss.close(done);\n        });\n      });\n    });\n\n    it('can send numbers as pong payload', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('open', () => {\n          ws.pong(0);\n          ws.close();\n        });\n      });\n\n      wss.on('connection', (ws) => {\n        ws.on('pong', (message) => {\n          assert.strictEqual(message.toString(), '0');\n          wss.close(done);\n        });\n      });\n    });\n\n    it('throws an error if the data size is greater than 125 bytes', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('open', () => {\n          assert.throws(\n            () => ws.pong(Buffer.alloc(126)),\n            /^RangeError: The data size must not be greater than 125 bytes$/\n          );\n\n          wss.close(done);\n        });\n      });\n\n      wss.on('connection', (ws) => {\n        ws.close();\n      });\n    });\n\n    it('is called automatically when a ping is received', (done) => {\n      const buf = Buffer.from('hi');\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('open', () => {\n          ws.ping(buf);\n        });\n\n        ws.on('pong', (data) => {\n          assert.deepStrictEqual(data, buf);\n          wss.close(done);\n        });\n      });\n\n      wss.on('connection', (ws) => {\n        ws.on('ping', (data) => {\n          assert.deepStrictEqual(data, buf);\n          ws.close();\n        });\n      });\n    });\n  });\n\n  describe('#resume', () => {\n    it('does nothing if `readyState` is `CONNECTING` or `CLOSED`', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        assert.strictEqual(ws.readyState, WebSocket.CONNECTING);\n        assert.ok(!ws.isPaused);\n\n        // Verify that no exception is thrown.\n        ws.resume();\n\n        ws.on('open', () => {\n          ws.pause();\n          assert.ok(ws.isPaused);\n\n          ws.on('close', () => {\n            assert.strictEqual(ws.readyState, WebSocket.CLOSED);\n\n            ws.resume();\n            assert.ok(ws.isPaused);\n\n            wss.close(done);\n          });\n\n          ws.terminate();\n        });\n      });\n    });\n\n    it('resumes the socket', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n      });\n\n      wss.on('connection', (ws) => {\n        assert.ok(!ws.isPaused);\n        assert.ok(!ws._socket.isPaused());\n\n        ws.pause();\n        assert.ok(ws.isPaused);\n        assert.ok(ws._socket.isPaused());\n\n        ws.resume();\n        assert.ok(!ws.isPaused);\n        assert.ok(!ws._socket.isPaused());\n\n        ws.close();\n        wss.close(done);\n      });\n    });\n  });\n\n  describe('#send', () => {\n    it('throws an error if `readyState` is `CONNECTING`', () => {\n      const ws = new WebSocket('ws://localhost', {\n        lookup() {}\n      });\n\n      assert.throws(\n        () => ws.send('hi'),\n        /^Error: WebSocket is not open: readyState 0 \\(CONNECTING\\)$/\n      );\n\n      assert.throws(\n        () => ws.send('hi', NOOP),\n        /^Error: WebSocket is not open: readyState 0 \\(CONNECTING\\)$/\n      );\n    });\n\n    it('increases `bufferedAmount` if `readyState` is 2 or 3', (done) => {\n      const ws = new WebSocket('ws://localhost', {\n        lookup() {}\n      });\n\n      ws.on('error', (err) => {\n        assert.ok(err instanceof Error);\n        assert.strictEqual(\n          err.message,\n          'WebSocket was closed before the connection was established'\n        );\n\n        assert.strictEqual(ws.readyState, WebSocket.CLOSING);\n        assert.strictEqual(ws.bufferedAmount, 0);\n\n        ws.send('hi');\n        assert.strictEqual(ws.bufferedAmount, 2);\n\n        ws.send();\n        assert.strictEqual(ws.bufferedAmount, 2);\n\n        ws.on('close', () => {\n          assert.strictEqual(ws.readyState, WebSocket.CLOSED);\n\n          ws.send('hi');\n          assert.strictEqual(ws.bufferedAmount, 4);\n\n          ws.send();\n          assert.strictEqual(ws.bufferedAmount, 4);\n\n          if (hasBlob) {\n            ws.send(new Blob(['hi']));\n            assert.strictEqual(ws.bufferedAmount, 6);\n          }\n\n          done();\n        });\n      });\n\n      ws.close();\n    });\n\n    it('calls the callback w/ an error if `readyState` is 2 or 3', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n      });\n\n      wss.on('connection', (ws) => {\n        ws.close();\n\n        assert.strictEqual(ws.bufferedAmount, 0);\n\n        ws.send('hi', (err) => {\n          assert.ok(err instanceof Error);\n          assert.strictEqual(\n            err.message,\n            'WebSocket is not open: readyState 2 (CLOSING)'\n          );\n          assert.strictEqual(ws.bufferedAmount, 2);\n\n          ws.on('close', () => {\n            ws.send('hi', (err) => {\n              assert.ok(err instanceof Error);\n              assert.strictEqual(\n                err.message,\n                'WebSocket is not open: readyState 3 (CLOSED)'\n              );\n              assert.strictEqual(ws.bufferedAmount, 4);\n\n              wss.close(done);\n            });\n          });\n        });\n      });\n    });\n\n    it('can send a big binary message', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const array = new Float32Array(1024 * 1024);\n\n        for (let i = 0; i < array.length; i++) {\n          array[i] = i / 5;\n        }\n\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('open', () => ws.send(array));\n        ws.on('message', (msg, isBinary) => {\n          assert.deepStrictEqual(msg, Buffer.from(array.buffer));\n          assert.ok(isBinary);\n          wss.close(done);\n        });\n      });\n\n      wss.on('connection', (ws) => {\n        ws.on('message', (msg, isBinary) => {\n          assert.ok(isBinary);\n          ws.send(msg);\n          ws.close();\n        });\n      });\n    });\n\n    it('can send text data', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('open', () => ws.send('hi'));\n        ws.on('message', (message, isBinary) => {\n          assert.deepStrictEqual(message, Buffer.from('hi'));\n          assert.ok(!isBinary);\n          wss.close(done);\n        });\n      });\n\n      wss.on('connection', (ws) => {\n        ws.on('message', (msg, isBinary) => {\n          ws.send(msg, { binary: isBinary });\n          ws.close();\n        });\n      });\n    });\n\n    it('does not override the `fin` option', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('open', () => {\n          ws.send('fragment', { fin: false });\n          ws.send('fragment', { fin: true });\n          ws.close();\n        });\n      });\n\n      wss.on('connection', (ws) => {\n        ws.on('message', (msg, isBinary) => {\n          assert.deepStrictEqual(msg, Buffer.from('fragmentfragment'));\n          assert.ok(!isBinary);\n          wss.close(done);\n        });\n      });\n    });\n\n    it('sends numbers as strings', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('open', () => {\n          ws.send(0);\n          ws.close();\n        });\n      });\n\n      wss.on('connection', (ws) => {\n        ws.on('message', (msg, isBinary) => {\n          assert.deepStrictEqual(msg, Buffer.from('0'));\n          assert.ok(!isBinary);\n          wss.close(done);\n        });\n      });\n    });\n\n    it('can send a `TypedArray`', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const array = new Float32Array(6);\n\n        for (let i = 0; i < array.length; ++i) {\n          array[i] = i / 2;\n        }\n\n        const partial = array.subarray(2, 5);\n        const buf = Buffer.from(\n          partial.buffer,\n          partial.byteOffset,\n          partial.byteLength\n        );\n\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('open', () => {\n          ws.send(partial);\n          ws.close();\n        });\n\n        ws.on('message', (message, isBinary) => {\n          assert.deepStrictEqual(message, buf);\n          assert.ok(isBinary);\n          wss.close(done);\n        });\n      });\n\n      wss.on('connection', (ws) => {\n        ws.on('message', (msg, isBinary) => {\n          assert.ok(isBinary);\n          ws.send(msg);\n        });\n      });\n    });\n\n    it('can send an `ArrayBuffer`', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const array = new Float32Array(5);\n\n        for (let i = 0; i < array.length; ++i) {\n          array[i] = i / 2;\n        }\n\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('open', () => {\n          ws.send(array.buffer);\n          ws.close();\n        });\n\n        ws.onmessage = (event) => {\n          assert.ok(event.data.equals(Buffer.from(array.buffer)));\n          wss.close(done);\n        };\n      });\n\n      wss.on('connection', (ws) => {\n        ws.on('message', (msg, isBinary) => {\n          assert.ok(isBinary);\n          ws.send(msg);\n        });\n      });\n    });\n\n    it('can send a `Buffer`', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const buf = Buffer.from('foobar');\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('open', () => {\n          ws.send(buf);\n          ws.close();\n        });\n\n        ws.onmessage = (event) => {\n          assert.deepStrictEqual(event.data, buf);\n          wss.close(done);\n        };\n      });\n\n      wss.on('connection', (ws) => {\n        ws.on('message', (msg, isBinary) => {\n          assert.ok(isBinary);\n          ws.send(msg);\n        });\n      });\n    });\n\n    it('can send a `Blob`', function (done) {\n      if (!hasBlob) return this.skip();\n\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        const messages = [];\n\n        ws.on('open', () => {\n          ws.send(new Blob(['foo']));\n          ws.send(new Blob(['bar']));\n          ws.close();\n        });\n\n        ws.on('message', (message, isBinary) => {\n          assert.ok(isBinary);\n          messages.push(message.toString());\n\n          if (messages.length === 2) {\n            assert.deepStrictEqual(messages, ['foo', 'bar']);\n            wss.close(done);\n          }\n        });\n      });\n\n      wss.on('connection', (ws) => {\n        ws.on('message', (message, isBinary) => {\n          assert.ok(isBinary);\n          ws.send(message);\n        });\n      });\n    });\n\n    it('calls the callback when data is written out', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('open', () => {\n          ws.send('hi', (err) => {\n            assert.ifError(err);\n            wss.close(done);\n          });\n        });\n      });\n\n      wss.on('connection', (ws) => {\n        ws.close();\n      });\n    });\n\n    it('calls the callback if the socket is forcibly closed', function (done) {\n      if (!hasBlob) return this.skip();\n\n      const called = [];\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('open', () => {\n          ws.send(new Blob(['foo']), (err) => {\n            called.push(1);\n\n            assert.strictEqual(ws.readyState, WebSocket.CLOSING);\n            assert.ok(err instanceof Error);\n            assert.strictEqual(\n              err.message,\n              'The socket was closed while the blob was being read'\n            );\n          });\n          ws.send('bar');\n          ws.send('baz', (err) => {\n            called.push(2);\n\n            assert.strictEqual(ws.readyState, WebSocket.CLOSING);\n            assert.ok(err instanceof Error);\n            assert.strictEqual(\n              err.message,\n              'The socket was closed while the blob was being read'\n            );\n          });\n\n          ws.terminate();\n        });\n      });\n\n      wss.on('connection', (ws) => {\n        ws.on('close', () => {\n          assert.deepStrictEqual(called, [1, 2]);\n          wss.close(done);\n        });\n      });\n    });\n\n    it('works when the `data` argument is falsy', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('open', () => {\n          ws.send();\n          ws.close();\n        });\n      });\n\n      wss.on('connection', (ws) => {\n        ws.on('message', (message, isBinary) => {\n          assert.strictEqual(message, EMPTY_BUFFER);\n          assert.ok(isBinary);\n          wss.close(done);\n        });\n      });\n    });\n\n    it('honors the `mask` option', (done) => {\n      let clientCloseEventEmitted = false;\n      let serverClientCloseEventEmitted = false;\n\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('open', () => ws.send('hi', { mask: false }));\n        ws.on('close', (code, reason) => {\n          assert.strictEqual(code, 1002);\n          assert.deepStrictEqual(reason, EMPTY_BUFFER);\n\n          clientCloseEventEmitted = true;\n          if (serverClientCloseEventEmitted) wss.close(done);\n        });\n      });\n\n      wss.on('connection', (ws) => {\n        const chunks = [];\n\n        ws._socket.prependListener('data', (chunk) => {\n          chunks.push(chunk);\n        });\n\n        ws.on('error', (err) => {\n          assert.ok(err instanceof RangeError);\n          assert.strictEqual(\n            err.message,\n            'Invalid WebSocket frame: MASK must be set'\n          );\n          assert.ok(\n            Buffer.concat(chunks).slice(0, 2).equals(Buffer.from('8102', 'hex'))\n          );\n\n          ws.on('close', (code, reason) => {\n            assert.strictEqual(code, 1006);\n            assert.strictEqual(reason, EMPTY_BUFFER);\n\n            serverClientCloseEventEmitted = true;\n            if (clientCloseEventEmitted) wss.close(done);\n          });\n        });\n      });\n    });\n  });\n\n  describe('#close', () => {\n    it('closes the connection if called while connecting (1/3)', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('open', () => done(new Error(\"Unexpected 'open' event\")));\n        ws.on('error', (err) => {\n          assert.ok(err instanceof Error);\n          assert.strictEqual(\n            err.message,\n            'WebSocket was closed before the connection was established'\n          );\n          ws.on('close', () => wss.close(done));\n        });\n        ws.close(1001);\n      });\n    });\n\n    it('closes the connection if called while connecting (2/3)', (done) => {\n      const wss = new WebSocket.Server(\n        {\n          verifyClient: (info, cb) => setTimeout(cb, 300, true),\n          port: 0\n        },\n        () => {\n          const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n          ws.on('open', () => done(new Error(\"Unexpected 'open' event\")));\n          ws.on('error', (err) => {\n            assert.ok(err instanceof Error);\n            assert.strictEqual(\n              err.message,\n              'WebSocket was closed before the connection was established'\n            );\n            ws.on('close', () => wss.close(done));\n          });\n          setTimeout(() => ws.close(1001), 150);\n        }\n      );\n    });\n\n    it('closes the connection if called while connecting (3/3)', (done) => {\n      const server = http.createServer();\n\n      server.listen(0, () => {\n        const ws = new WebSocket(`ws://localhost:${server.address().port}`);\n\n        ws.on('open', () => done(new Error(\"Unexpected 'open' event\")));\n        ws.on('error', (err) => {\n          assert.ok(err instanceof Error);\n          assert.strictEqual(\n            err.message,\n            'WebSocket was closed before the connection was established'\n          );\n          ws.on('close', () => {\n            server.close(done);\n          });\n        });\n\n        ws.on('unexpected-response', (req, res) => {\n          assert.strictEqual(res.statusCode, 502);\n\n          const chunks = [];\n\n          res.on('data', (chunk) => {\n            chunks.push(chunk);\n          });\n\n          res.on('end', () => {\n            assert.strictEqual(Buffer.concat(chunks).toString(), 'foo');\n            ws.close();\n          });\n        });\n      });\n\n      server.on('upgrade', (req, socket) => {\n        socket.on('end', socket.end);\n\n        socket.write(\n          `HTTP/1.1 502 ${http.STATUS_CODES[502]}\\r\\n` +\n            'Connection: keep-alive\\r\\n' +\n            'Content-type: text/html\\r\\n' +\n            'Content-Length: 3\\r\\n' +\n            '\\r\\n' +\n            'foo'\n        );\n      });\n    });\n\n    it('can be called from an error listener while connecting', (done) => {\n      const server = net.createServer();\n\n      server.on('connection', (socket) => {\n        socket.on('end', socket.end);\n        socket.resume();\n        socket.write(Buffer.from('foo\\r\\n'));\n      });\n\n      server.listen(0, () => {\n        const ws = new WebSocket(`ws://localhost:${server.address().port}`);\n\n        ws.on('open', () => done(new Error(\"Unexpected 'open' event\")));\n        ws.on('error', (err) => {\n          assert.ok(err instanceof Error);\n          assert.strictEqual(err.code, 'HPE_INVALID_CONSTANT');\n          ws.close();\n          ws.on('close', () => {\n            server.close(done);\n          });\n        });\n      });\n    });\n\n    it(\"can be called from a listener of the 'redirect' event\", (done) => {\n      const server = http.createServer();\n\n      server.once('upgrade', (req, socket) => {\n        socket.end('HTTP/1.1 302 Found\\r\\nLocation: /foo\\r\\n\\r\\n');\n      });\n\n      server.listen(() => {\n        const port = server.address().port;\n        const ws = new WebSocket(`ws://localhost:${port}`, {\n          followRedirects: true\n        });\n\n        ws.on('open', () => {\n          done(new Error(\"Unexpected 'open' event\"));\n        });\n\n        ws.on('error', (err) => {\n          assert.ok(err instanceof Error);\n          assert.strictEqual(\n            err.message,\n            'WebSocket was closed before the connection was established'\n          );\n\n          ws.on('close', (code) => {\n            assert.strictEqual(code, 1006);\n            server.close(done);\n          });\n        });\n\n        ws.on('redirect', () => {\n          ws.close();\n        });\n      });\n    });\n\n    it(\"can be called from a listener of the 'upgrade' event\", (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('open', () => done(new Error(\"Unexpected 'open' event\")));\n        ws.on('error', (err) => {\n          assert.ok(err instanceof Error);\n          assert.strictEqual(\n            err.message,\n            'WebSocket was closed before the connection was established'\n          );\n          ws.on('close', () => wss.close(done));\n        });\n        ws.on('upgrade', () => ws.close());\n      });\n    });\n\n    it('sends the close status code only when necessary', (done) => {\n      let sent;\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('open', () => {\n          ws._socket.once('data', (data) => {\n            sent = data;\n          });\n        });\n      });\n\n      wss.on('connection', (ws) => {\n        ws._socket.once('data', (received) => {\n          assert.deepStrictEqual(\n            received.slice(0, 2),\n            Buffer.from([0x88, 0x80])\n          );\n          assert.deepStrictEqual(sent, Buffer.from([0x88, 0x00]));\n\n          ws.on('close', (code, reason) => {\n            assert.strictEqual(code, 1005);\n            assert.strictEqual(reason, EMPTY_BUFFER);\n            wss.close(done);\n          });\n        });\n        ws.close();\n      });\n    });\n\n    it('works when close reason is not specified', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('open', () => ws.close(1000));\n      });\n\n      wss.on('connection', (ws) => {\n        ws.on('close', (code, message) => {\n          assert.strictEqual(code, 1000);\n          assert.deepStrictEqual(message, EMPTY_BUFFER);\n          wss.close(done);\n        });\n      });\n    });\n\n    it('works when close reason is specified', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('open', () => ws.close(1000, 'some reason'));\n      });\n\n      wss.on('connection', (ws) => {\n        ws.on('close', (code, message) => {\n          assert.strictEqual(code, 1000);\n          assert.deepStrictEqual(message, Buffer.from('some reason'));\n          wss.close(done);\n        });\n      });\n    });\n\n    it('permits all buffered data to be delivered', (done) => {\n      const wss = new WebSocket.Server(\n        {\n          perMessageDeflate: { threshold: 0 },\n          port: 0\n        },\n        () => {\n          const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n          const messages = [];\n\n          ws.on('message', (message, isBinary) => {\n            assert.ok(!isBinary);\n            messages.push(message.toString());\n          });\n          ws.on('close', (code) => {\n            assert.strictEqual(code, 1005);\n            assert.deepStrictEqual(messages, ['foo', 'bar', 'baz']);\n            wss.close(done);\n          });\n        }\n      );\n\n      wss.on('connection', (ws) => {\n        const callback = (err) => assert.ifError(err);\n\n        ws.send('foo', callback);\n        ws.send('bar', callback);\n        ws.send('baz', callback);\n        ws.close();\n        ws.close();\n      });\n    });\n\n    it('allows close code 1013', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('close', (code) => {\n          assert.strictEqual(code, 1013);\n          wss.close(done);\n        });\n      });\n\n      wss.on('connection', (ws) => ws.close(1013));\n    });\n\n    it('allows close code 1014', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('close', (code) => {\n          assert.strictEqual(code, 1014);\n          wss.close(done);\n        });\n      });\n\n      wss.on('connection', (ws) => ws.close(1014));\n    });\n\n    it('does nothing if `readyState` is `CLOSED`', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('close', (code) => {\n          assert.strictEqual(code, 1005);\n          assert.strictEqual(ws.readyState, WebSocket.CLOSED);\n          ws.close();\n          wss.close(done);\n        });\n      });\n\n      wss.on('connection', (ws) => ws.close());\n    });\n\n    it('sets a timer for the closing handshake to complete', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('close', (code, reason) => {\n          assert.strictEqual(code, 1000);\n          assert.deepStrictEqual(reason, Buffer.from('some reason'));\n          wss.close(done);\n        });\n\n        ws.on('open', () => {\n          let callbackCalled = false;\n\n          assert.strictEqual(ws._closeTimer, null);\n\n          ws.send('foo', () => {\n            callbackCalled = true;\n          });\n\n          ws.close(1000, 'some reason');\n\n          //\n          // Check that the close timer is set even if the `Sender.close()`\n          // callback is not called.\n          //\n          assert.strictEqual(callbackCalled, false);\n          assert.strictEqual(ws._closeTimer._idleTimeout, 30000);\n        });\n      });\n    });\n  });\n\n  describe('#terminate', () => {\n    it('closes the connection if called while connecting (1/2)', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('open', () => done(new Error(\"Unexpected 'open' event\")));\n        ws.on('error', (err) => {\n          assert.ok(err instanceof Error);\n          assert.strictEqual(\n            err.message,\n            'WebSocket was closed before the connection was established'\n          );\n          ws.on('close', () => wss.close(done));\n        });\n        ws.terminate();\n      });\n    });\n\n    it('closes the connection if called while connecting (2/2)', (done) => {\n      const wss = new WebSocket.Server(\n        {\n          verifyClient: (info, cb) => setTimeout(cb, 300, true),\n          port: 0\n        },\n        () => {\n          const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n          ws.on('open', () => done(new Error(\"Unexpected 'open' event\")));\n          ws.on('error', (err) => {\n            assert.ok(err instanceof Error);\n            assert.strictEqual(\n              err.message,\n              'WebSocket was closed before the connection was established'\n            );\n            ws.on('close', () => wss.close(done));\n          });\n          setTimeout(() => ws.terminate(), 150);\n        }\n      );\n    });\n\n    it('can be called from an error listener while connecting', (done) => {\n      const server = net.createServer();\n\n      server.on('connection', (socket) => {\n        socket.on('end', socket.end);\n        socket.resume();\n        socket.write(Buffer.from('foo\\r\\n'));\n      });\n\n      server.listen(0, () => {\n        const ws = new WebSocket(`ws://localhost:${server.address().port}`);\n\n        ws.on('open', () => done(new Error(\"Unexpected 'open' event\")));\n        ws.on('error', (err) => {\n          assert.ok(err instanceof Error);\n          assert.strictEqual(err.code, 'HPE_INVALID_CONSTANT');\n          ws.terminate();\n          ws.on('close', () => {\n            server.close(done);\n          });\n        });\n      });\n    });\n\n    it(\"can be called from a listener of the 'redirect' event\", (done) => {\n      const server = http.createServer();\n\n      server.once('upgrade', (req, socket) => {\n        socket.end('HTTP/1.1 302 Found\\r\\nLocation: /foo\\r\\n\\r\\n');\n      });\n\n      server.listen(() => {\n        const port = server.address().port;\n        const ws = new WebSocket(`ws://localhost:${port}`, {\n          followRedirects: true\n        });\n\n        ws.on('open', () => {\n          done(new Error(\"Unexpected 'open' event\"));\n        });\n\n        ws.on('error', (err) => {\n          assert.ok(err instanceof Error);\n          assert.strictEqual(\n            err.message,\n            'WebSocket was closed before the connection was established'\n          );\n\n          ws.on('close', (code) => {\n            assert.strictEqual(code, 1006);\n            server.close(done);\n          });\n        });\n\n        ws.on('redirect', () => {\n          ws.terminate();\n        });\n      });\n    });\n\n    it(\"can be called from a listener of the 'upgrade' event\", (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('open', () => done(new Error(\"Unexpected 'open' event\")));\n        ws.on('error', (err) => {\n          assert.ok(err instanceof Error);\n          assert.strictEqual(\n            err.message,\n            'WebSocket was closed before the connection was established'\n          );\n          ws.on('close', () => wss.close(done));\n        });\n        ws.on('upgrade', () => ws.terminate());\n      });\n    });\n\n    it('does nothing if `readyState` is `CLOSED`', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('close', (code) => {\n          assert.strictEqual(code, 1006);\n          assert.strictEqual(ws.readyState, WebSocket.CLOSED);\n          ws.terminate();\n          wss.close(done);\n        });\n      });\n\n      wss.on('connection', (ws) => ws.terminate());\n    });\n  });\n\n  describe('WHATWG API emulation', () => {\n    it('supports the `on{close,error,message,open}` attributes', () => {\n      for (const property of ['onclose', 'onerror', 'onmessage', 'onopen']) {\n        const descriptor = Object.getOwnPropertyDescriptor(\n          WebSocket.prototype,\n          property\n        );\n\n        assert.strictEqual(descriptor.configurable, true);\n        assert.strictEqual(descriptor.enumerable, true);\n        assert.ok(descriptor.get !== undefined);\n        assert.ok(descriptor.set !== undefined);\n      }\n\n      const ws = new WebSocket('ws://localhost', { agent: new CustomAgent() });\n\n      assert.strictEqual(ws.onmessage, null);\n      assert.strictEqual(ws.onclose, null);\n      assert.strictEqual(ws.onerror, null);\n      assert.strictEqual(ws.onopen, null);\n\n      ws.onmessage = NOOP;\n      ws.onerror = NOOP;\n      ws.onclose = NOOP;\n      ws.onopen = NOOP;\n\n      assert.strictEqual(ws.onmessage, NOOP);\n      assert.strictEqual(ws.onclose, NOOP);\n      assert.strictEqual(ws.onerror, NOOP);\n      assert.strictEqual(ws.onopen, NOOP);\n\n      ws.onmessage = 'foo';\n\n      assert.strictEqual(ws.onmessage, null);\n      assert.strictEqual(ws.listenerCount('message'), 0);\n    });\n\n    it('works like the `EventEmitter` interface', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.onmessage = (messageEvent) => {\n          assert.strictEqual(messageEvent.data, 'foo');\n          ws.onclose = (closeEvent) => {\n            assert.strictEqual(closeEvent.wasClean, true);\n            assert.strictEqual(closeEvent.code, 1005);\n            assert.strictEqual(closeEvent.reason, '');\n            wss.close(done);\n          };\n          ws.close();\n        };\n\n        ws.onopen = () => ws.send('foo');\n      });\n\n      wss.on('connection', (ws) => {\n        ws.on('message', (msg, isBinary) => {\n          ws.send(msg, { binary: isBinary });\n        });\n      });\n    });\n\n    it(\"doesn't return listeners added with `on`\", () => {\n      const ws = new WebSocket('ws://localhost', { agent: new CustomAgent() });\n\n      ws.on('open', NOOP);\n\n      assert.deepStrictEqual(ws.listeners('open'), [NOOP]);\n      assert.strictEqual(ws.onopen, null);\n    });\n\n    it(\"doesn't remove listeners added with `on`\", () => {\n      const ws = new WebSocket('ws://localhost', { agent: new CustomAgent() });\n\n      ws.on('close', NOOP);\n      ws.onclose = NOOP;\n\n      let listeners = ws.listeners('close');\n\n      assert.strictEqual(listeners.length, 2);\n      assert.strictEqual(listeners[0], NOOP);\n      assert.strictEqual(listeners[1][kListener], NOOP);\n\n      ws.onclose = NOOP;\n\n      listeners = ws.listeners('close');\n\n      assert.strictEqual(listeners.length, 2);\n      assert.strictEqual(listeners[0], NOOP);\n      assert.strictEqual(listeners[1][kListener], NOOP);\n    });\n\n    it('supports the `addEventListener` method', () => {\n      const events = [];\n      const ws = new WebSocket('ws://localhost', { agent: new CustomAgent() });\n\n      ws.addEventListener('foo', () => {});\n      assert.strictEqual(ws.listenerCount('foo'), 0);\n\n      function onOpen() {\n        events.push('open');\n        assert.strictEqual(ws.listenerCount('open'), 1);\n      }\n\n      ws.addEventListener('open', onOpen);\n      ws.addEventListener('open', onOpen);\n\n      assert.strictEqual(ws.listenerCount('open'), 1);\n\n      const listener = {\n        handleEvent() {\n          events.push('message');\n          assert.strictEqual(this, listener);\n          assert.strictEqual(ws.listenerCount('message'), 0);\n        }\n      };\n\n      ws.addEventListener('message', listener, { once: true });\n      ws.addEventListener('message', listener);\n\n      assert.strictEqual(ws.listenerCount('message'), 1);\n\n      ws.addEventListener('close', NOOP);\n      ws.onclose = NOOP;\n\n      let listeners = ws.listeners('close');\n\n      assert.strictEqual(listeners.length, 2);\n      assert.strictEqual(listeners[0][kListener], NOOP);\n      assert.strictEqual(listeners[1][kListener], NOOP);\n\n      ws.onerror = NOOP;\n      ws.addEventListener('error', NOOP);\n\n      listeners = ws.listeners('error');\n\n      assert.strictEqual(listeners.length, 2);\n      assert.strictEqual(listeners[0][kListener], NOOP);\n      assert.strictEqual(listeners[1][kListener], NOOP);\n\n      ws.emit('open');\n      ws.emit('message', EMPTY_BUFFER, false);\n\n      assert.deepStrictEqual(events, ['open', 'message']);\n    });\n\n    it(\"doesn't return listeners added with `addEventListener`\", () => {\n      const ws = new WebSocket('ws://localhost', { agent: new CustomAgent() });\n\n      ws.addEventListener('open', NOOP);\n\n      const listeners = ws.listeners('open');\n\n      assert.strictEqual(listeners.length, 1);\n      assert.strictEqual(listeners[0][kListener], NOOP);\n\n      assert.strictEqual(ws.onopen, null);\n    });\n\n    it(\"doesn't remove listeners added with `addEventListener`\", () => {\n      const ws = new WebSocket('ws://localhost', { agent: new CustomAgent() });\n\n      ws.addEventListener('close', NOOP);\n      ws.onclose = NOOP;\n\n      let listeners = ws.listeners('close');\n\n      assert.strictEqual(listeners.length, 2);\n      assert.strictEqual(listeners[0][kListener], NOOP);\n      assert.strictEqual(listeners[1][kListener], NOOP);\n\n      ws.onclose = NOOP;\n\n      listeners = ws.listeners('close');\n\n      assert.strictEqual(listeners.length, 2);\n      assert.strictEqual(listeners[0][kListener], NOOP);\n      assert.strictEqual(listeners[1][kListener], NOOP);\n    });\n\n    it('supports the `removeEventListener` method', () => {\n      const ws = new WebSocket('ws://localhost', { agent: new CustomAgent() });\n\n      const listener = { handleEvent() {} };\n\n      ws.addEventListener('message', listener);\n      ws.addEventListener('open', NOOP);\n\n      assert.strictEqual(ws.listeners('message')[0][kListener], listener);\n      assert.strictEqual(ws.listeners('open')[0][kListener], NOOP);\n\n      ws.removeEventListener('message', () => {});\n\n      assert.strictEqual(ws.listeners('message')[0][kListener], listener);\n\n      ws.removeEventListener('message', listener);\n      ws.removeEventListener('open', NOOP);\n\n      assert.strictEqual(ws.listenerCount('message'), 0);\n      assert.strictEqual(ws.listenerCount('open'), 0);\n\n      ws.addEventListener('message', NOOP, { once: true });\n      ws.addEventListener('open', NOOP, { once: true });\n\n      assert.strictEqual(ws.listeners('message')[0][kListener], NOOP);\n      assert.strictEqual(ws.listeners('open')[0][kListener], NOOP);\n\n      ws.removeEventListener('message', () => {});\n\n      assert.strictEqual(ws.listeners('message')[0][kListener], NOOP);\n\n      ws.removeEventListener('message', NOOP);\n      ws.removeEventListener('open', NOOP);\n\n      assert.strictEqual(ws.listenerCount('message'), 0);\n      assert.strictEqual(ws.listenerCount('open'), 0);\n\n      // Listeners not added with `websocket.addEventListener()`.\n      ws.on('message', NOOP);\n\n      assert.deepStrictEqual(ws.listeners('message'), [NOOP]);\n\n      ws.removeEventListener('message', NOOP);\n\n      assert.deepStrictEqual(ws.listeners('message'), [NOOP]);\n\n      ws.onclose = NOOP;\n\n      assert.strictEqual(ws.listeners('close')[0][kListener], NOOP);\n\n      ws.removeEventListener('close', NOOP);\n\n      assert.strictEqual(ws.listeners('close')[0][kListener], NOOP);\n    });\n\n    it('wraps text data in a `MessageEvent`', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.addEventListener('open', () => {\n          ws.send('hi');\n          ws.close();\n        });\n\n        ws.addEventListener('message', (event) => {\n          assert.ok(event instanceof MessageEvent);\n          assert.strictEqual(event.data, 'hi');\n          wss.close(done);\n        });\n      });\n\n      wss.on('connection', (ws) => {\n        ws.on('message', (msg, isBinary) => {\n          ws.send(msg, { binary: isBinary });\n        });\n      });\n    });\n\n    it('receives a `CloseEvent` when server closes (1000)', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.addEventListener('close', (event) => {\n          assert.ok(event instanceof CloseEvent);\n          assert.ok(event.wasClean);\n          assert.strictEqual(event.reason, '');\n          assert.strictEqual(event.code, 1000);\n          wss.close(done);\n        });\n      });\n\n      wss.on('connection', (ws) => ws.close(1000));\n    });\n\n    it('receives a `CloseEvent` when server closes (4000)', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.addEventListener('close', (event) => {\n          assert.ok(event instanceof CloseEvent);\n          assert.ok(event.wasClean);\n          assert.strictEqual(event.reason, 'some daft reason');\n          assert.strictEqual(event.code, 4000);\n          wss.close(done);\n        });\n      });\n\n      wss.on('connection', (ws) => ws.close(4000, 'some daft reason'));\n    });\n\n    it('sets `target` and `type` on events', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const err = new Error('forced');\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.addEventListener('open', (event) => {\n          assert.ok(event instanceof Event);\n          assert.strictEqual(event.type, 'open');\n          assert.strictEqual(event.target, ws);\n        });\n        ws.addEventListener('message', (event) => {\n          assert.ok(event instanceof MessageEvent);\n          assert.strictEqual(event.type, 'message');\n          assert.strictEqual(event.target, ws);\n          ws.close();\n        });\n        ws.addEventListener('close', (event) => {\n          assert.ok(event instanceof CloseEvent);\n          assert.strictEqual(event.type, 'close');\n          assert.strictEqual(event.target, ws);\n          ws.emit('error', err);\n        });\n        ws.addEventListener('error', (event) => {\n          assert.ok(event instanceof ErrorEvent);\n          assert.strictEqual(event.message, 'forced');\n          assert.strictEqual(event.type, 'error');\n          assert.strictEqual(event.target, ws);\n          assert.strictEqual(event.error, err);\n\n          wss.close(done);\n        });\n      });\n\n      wss.on('connection', (client) => client.send('hi'));\n    });\n\n    it('passes binary data as a Node.js `Buffer` by default', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.onmessage = (evt) => {\n          assert.ok(Buffer.isBuffer(evt.data));\n          wss.close(done);\n        };\n      });\n\n      wss.on('connection', (ws) => {\n        ws.send(new Uint8Array(4096));\n        ws.close();\n      });\n    });\n\n    it('ignores `binaryType` for text messages', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.binaryType = 'arraybuffer';\n\n        ws.onmessage = (evt) => {\n          assert.strictEqual(evt.data, 'foo');\n          wss.close(done);\n        };\n      });\n\n      wss.on('connection', (ws) => {\n        ws.send('foo');\n        ws.close();\n      });\n    });\n\n    it('allows to update `binaryType` on the fly', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        function testType(binaryType, next) {\n          const buf = Buffer.from(binaryType);\n          ws.binaryType = binaryType;\n\n          ws.onmessage = (evt) => {\n            if (binaryType === 'nodebuffer') {\n              assert.ok(Buffer.isBuffer(evt.data));\n              assert.deepStrictEqual(evt.data, buf);\n              next();\n            } else if (binaryType === 'arraybuffer') {\n              assert.ok(evt.data instanceof ArrayBuffer);\n              assert.deepStrictEqual(Buffer.from(evt.data), buf);\n              next();\n            } else if (binaryType === 'fragments') {\n              assert.deepStrictEqual(evt.data, [buf]);\n              next();\n            } else if (binaryType === 'blob') {\n              assert.ok(evt.data instanceof Blob);\n              evt.data\n                .arrayBuffer()\n                .then((arrayBuffer) => {\n                  assert.deepStrictEqual(Buffer.from(arrayBuffer), buf);\n                  next();\n                })\n                .catch(done);\n            }\n          };\n\n          ws.send(buf);\n        }\n\n        function close() {\n          ws.close();\n          wss.close(done);\n        }\n\n        ws.onopen = () => {\n          testType('nodebuffer', () => {\n            testType('arraybuffer', () => {\n              testType('fragments', () => {\n                if (hasBlob) testType('blob', close);\n                else close();\n              });\n            });\n          });\n        };\n      });\n\n      wss.on('connection', (ws) => {\n        ws.on('message', (msg, isBinary) => {\n          assert.ok(isBinary);\n          ws.send(msg);\n        });\n      });\n    });\n  });\n\n  describe('SSL', () => {\n    it('connects to secure websocket server', (done) => {\n      const server = https.createServer({\n        cert: fs.readFileSync('test/fixtures/certificate.pem'),\n        key: fs.readFileSync('test/fixtures/key.pem')\n      });\n      const wss = new WebSocket.Server({ server });\n\n      wss.on('connection', () => {\n        server.close(done);\n      });\n\n      server.listen(0, () => {\n        const ws = new WebSocket(`wss://127.0.0.1:${server.address().port}`, {\n          rejectUnauthorized: false\n        });\n\n        ws.on('open', ws.close);\n      });\n    });\n\n    it('connects to secure websocket server with client side certificate', (done) => {\n      const server = https.createServer({\n        cert: fs.readFileSync('test/fixtures/certificate.pem'),\n        ca: [fs.readFileSync('test/fixtures/ca-certificate.pem')],\n        key: fs.readFileSync('test/fixtures/key.pem'),\n        requestCert: true\n      });\n\n      const wss = new WebSocket.Server({ noServer: true });\n\n      server.on('upgrade', (request, socket, head) => {\n        assert.ok(socket.authorized);\n\n        wss.handleUpgrade(request, socket, head, (ws) => {\n          ws.on('close', (code) => {\n            assert.strictEqual(code, 1005);\n            server.close(done);\n          });\n        });\n      });\n\n      server.listen(0, () => {\n        const ws = new WebSocket(`wss://localhost:${server.address().port}`, {\n          cert: fs.readFileSync('test/fixtures/client-certificate.pem'),\n          key: fs.readFileSync('test/fixtures/client-key.pem'),\n          rejectUnauthorized: false\n        });\n\n        ws.on('open', ws.close);\n      });\n    });\n\n    it('cannot connect to secure websocket server via ws://', (done) => {\n      const server = https.createServer({\n        cert: fs.readFileSync('test/fixtures/certificate.pem'),\n        key: fs.readFileSync('test/fixtures/key.pem')\n      });\n      const wss = new WebSocket.Server({ server });\n\n      server.listen(0, () => {\n        const ws = new WebSocket(`ws://localhost:${server.address().port}`, {\n          rejectUnauthorized: false\n        });\n\n        ws.on('error', () => {\n          server.close(done);\n          wss.close();\n        });\n      });\n    });\n\n    it('can send and receive text data', (done) => {\n      const server = https.createServer({\n        cert: fs.readFileSync('test/fixtures/certificate.pem'),\n        key: fs.readFileSync('test/fixtures/key.pem')\n      });\n      const wss = new WebSocket.Server({ server });\n\n      wss.on('connection', (ws) => {\n        ws.on('message', (message, isBinary) => {\n          assert.deepStrictEqual(message, Buffer.from('foobar'));\n          assert.ok(!isBinary);\n          server.close(done);\n        });\n      });\n\n      server.listen(0, () => {\n        const ws = new WebSocket(`wss://localhost:${server.address().port}`, {\n          rejectUnauthorized: false\n        });\n\n        ws.on('open', () => {\n          ws.send('foobar');\n          ws.close();\n        });\n      });\n    });\n\n    it('can send a big binary message', (done) => {\n      const buf = crypto.randomBytes(5 * 1024 * 1024);\n      const server = https.createServer({\n        cert: fs.readFileSync('test/fixtures/certificate.pem'),\n        key: fs.readFileSync('test/fixtures/key.pem')\n      });\n      const wss = new WebSocket.Server({ server });\n\n      wss.on('connection', (ws) => {\n        ws.on('message', (message, isBinary) => {\n          assert.ok(isBinary);\n          ws.send(message);\n          ws.close();\n        });\n      });\n\n      server.listen(0, () => {\n        const ws = new WebSocket(`wss://localhost:${server.address().port}`, {\n          rejectUnauthorized: false\n        });\n\n        ws.on('open', () => ws.send(buf));\n        ws.on('message', (message, isBinary) => {\n          assert.deepStrictEqual(message, buf);\n          assert.ok(isBinary);\n\n          server.close(done);\n        });\n      });\n    }).timeout(4000);\n\n    it('allows to disable sending the SNI extension', (done) => {\n      const original = tls.connect;\n\n      tls.connect = (options) => {\n        assert.strictEqual(options.servername, '');\n        tls.connect = original;\n        done();\n      };\n\n      const ws = new WebSocket('wss://127.0.0.1', { servername: '' });\n    });\n\n    it(\"works around a double 'error' event bug in Node.js\", function (done) {\n      //\n      // The `minVersion` and `maxVersion` options are not supported in\n      // Node.js < 10.16.0.\n      //\n      if (process.versions.modules < 64) return this.skip();\n\n      //\n      // The `'error'` event can be emitted multiple times by the\n      // `http.ClientRequest` object in Node.js < 13. This test reproduces the\n      // issue in Node.js 12.\n      //\n      const server = https.createServer({\n        cert: fs.readFileSync('test/fixtures/certificate.pem'),\n        key: fs.readFileSync('test/fixtures/key.pem'),\n        minVersion: 'TLSv1.2'\n      });\n      const wss = new WebSocket.Server({ server });\n\n      server.listen(0, () => {\n        const ws = new WebSocket(`wss://localhost:${server.address().port}`, {\n          maxVersion: 'TLSv1.1',\n          rejectUnauthorized: false\n        });\n\n        ws.on('error', (err) => {\n          assert.ok(err instanceof Error);\n          server.close(done);\n          wss.close();\n        });\n      });\n    });\n  });\n\n  describe('Request headers', () => {\n    it('adds the authorization header if the url has userinfo', (done) => {\n      const agent = new http.Agent();\n      const userinfo = 'test:testpass';\n\n      agent.addRequest = (req) => {\n        assert.strictEqual(\n          req.getHeader('authorization'),\n          `Basic ${Buffer.from(userinfo).toString('base64')}`\n        );\n        done();\n      };\n\n      const ws = new WebSocket(`ws://${userinfo}@localhost`, { agent });\n    });\n\n    it('honors the `auth` option', (done) => {\n      const agent = new http.Agent();\n      const auth = 'user:pass';\n\n      agent.addRequest = (req) => {\n        assert.strictEqual(\n          req.getHeader('authorization'),\n          `Basic ${Buffer.from(auth).toString('base64')}`\n        );\n        done();\n      };\n\n      const ws = new WebSocket('ws://localhost', { agent, auth });\n    });\n\n    it('favors the url userinfo over the `auth` option', (done) => {\n      const agent = new http.Agent();\n      const auth = 'foo:bar';\n      const userinfo = 'baz:qux';\n\n      agent.addRequest = (req) => {\n        assert.strictEqual(\n          req.getHeader('authorization'),\n          `Basic ${Buffer.from(userinfo).toString('base64')}`\n        );\n        done();\n      };\n\n      const ws = new WebSocket(`ws://${userinfo}@localhost`, { agent, auth });\n    });\n\n    it('adds custom headers', (done) => {\n      const agent = new http.Agent();\n\n      agent.addRequest = (req) => {\n        assert.strictEqual(req.getHeader('cookie'), 'foo=bar');\n        done();\n      };\n\n      const ws = new WebSocket('ws://localhost', {\n        headers: { Cookie: 'foo=bar' },\n        agent\n      });\n    });\n\n    it('excludes default ports from host header', () => {\n      const options = { lookup() {} };\n      const variants = [\n        ['wss://localhost:8443', 'localhost:8443'],\n        ['wss://localhost:443', 'localhost'],\n        ['ws://localhost:88', 'localhost:88'],\n        ['ws://localhost:80', 'localhost']\n      ];\n\n      for (const [url, host] of variants) {\n        const ws = new WebSocket(url, options);\n        assert.strictEqual(ws._req.getHeader('host'), host);\n      }\n    });\n\n    it(\"doesn't add the origin header by default\", (done) => {\n      const agent = new http.Agent();\n\n      agent.addRequest = (req) => {\n        assert.strictEqual(req.getHeader('origin'), undefined);\n        done();\n      };\n\n      const ws = new WebSocket('ws://localhost', { agent });\n    });\n\n    it('honors the `origin` option (1/2)', (done) => {\n      const agent = new http.Agent();\n\n      agent.addRequest = (req) => {\n        assert.strictEqual(req.getHeader('origin'), 'https://example.com:8000');\n        done();\n      };\n\n      const ws = new WebSocket('ws://localhost', {\n        origin: 'https://example.com:8000',\n        agent\n      });\n    });\n\n    it('honors the `origin` option (2/2)', (done) => {\n      const agent = new http.Agent();\n\n      agent.addRequest = (req) => {\n        assert.strictEqual(\n          req.getHeader('sec-websocket-origin'),\n          'https://example.com:8000'\n        );\n        done();\n      };\n\n      const ws = new WebSocket('ws://localhost', {\n        origin: 'https://example.com:8000',\n        protocolVersion: 8,\n        agent\n      });\n    });\n\n    it('honors the `finishRequest` option', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const host = `localhost:${wss.address().port}`;\n        const ws = new WebSocket(`ws://${host}`, {\n          finishRequest(req, ws) {\n            assert.ok(req instanceof http.ClientRequest);\n            assert.strictEqual(req.getHeader('host'), host);\n            assert.ok(ws instanceof WebSocket);\n            assert.strictEqual(req, ws._req);\n\n            req.on('socket', (socket) => {\n              socket.on('connect', () => {\n                req.setHeader('Cookie', 'foo=bar');\n                req.end();\n              });\n            });\n          }\n        });\n\n        ws.on('close', (code) => {\n          assert.strictEqual(code, 1005);\n          wss.close(done);\n        });\n      });\n\n      wss.on('connection', (ws, req) => {\n        assert.strictEqual(req.headers.cookie, 'foo=bar');\n        ws.close();\n      });\n    });\n  });\n\n  describe('permessage-deflate', () => {\n    it('is enabled by default', (done) => {\n      const agent = new http.Agent();\n\n      agent.addRequest = (req) => {\n        assert.strictEqual(\n          req.getHeader('sec-websocket-extensions'),\n          'permessage-deflate; client_max_window_bits'\n        );\n        done();\n      };\n\n      const ws = new WebSocket('ws://localhost', { agent });\n    });\n\n    it('can be disabled', (done) => {\n      const agent = new http.Agent();\n\n      agent.addRequest = (req) => {\n        assert.strictEqual(\n          req.getHeader('sec-websocket-extensions'),\n          undefined\n        );\n        done();\n      };\n\n      const ws = new WebSocket('ws://localhost', {\n        perMessageDeflate: false,\n        agent\n      });\n    });\n\n    it('can send extension parameters', (done) => {\n      const agent = new http.Agent();\n\n      const value =\n        'permessage-deflate; server_no_context_takeover;' +\n        ' client_no_context_takeover; server_max_window_bits=10;' +\n        ' client_max_window_bits';\n\n      agent.addRequest = (req) => {\n        assert.strictEqual(req.getHeader('sec-websocket-extensions'), value);\n        done();\n      };\n\n      const ws = new WebSocket('ws://localhost', {\n        perMessageDeflate: {\n          clientNoContextTakeover: true,\n          serverNoContextTakeover: true,\n          clientMaxWindowBits: true,\n          serverMaxWindowBits: 10\n        },\n        agent\n      });\n    });\n\n    it('consumes all received data when connection is closed (1/2)', (done) => {\n      const wss = new WebSocket.Server(\n        {\n          perMessageDeflate: { threshold: 0 },\n          port: 0\n        },\n        () => {\n          const messages = [];\n          const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n          ws.on('open', () => {\n            ws._socket.on('close', () => {\n              assert.strictEqual(ws._receiver._state, 5);\n            });\n          });\n\n          ws.on('message', (message, isBinary) => {\n            assert.ok(!isBinary);\n            messages.push(message.toString());\n          });\n\n          ws.on('close', (code) => {\n            assert.strictEqual(code, 1006);\n            assert.deepStrictEqual(messages, ['foo', 'bar', 'baz', 'qux']);\n            wss.close(done);\n          });\n        }\n      );\n\n      wss.on('connection', (ws) => {\n        ws.send('foo');\n        ws.send('bar');\n        ws.send('baz');\n        ws.send('qux', () => ws._socket.end());\n      });\n    });\n\n    it('consumes all received data when connection is closed (2/2)', (done) => {\n      const wss = new WebSocket.Server(\n        {\n          perMessageDeflate: true,\n          port: 0\n        },\n        () => {\n          const messageLengths = [];\n          const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n          ws.on('open', () => {\n            ws._socket.prependListener('close', () => {\n              assert.strictEqual(ws._receiver._state, 5);\n              assert.strictEqual(ws._socket._readableState.length, 3);\n            });\n\n            const push = ws._socket.push;\n\n            // Override `ws._socket.push()` to know exactly when data is\n            // received and call `ws.terminate()` immediately after that without\n            // relying on a timer.\n            ws._socket.push = (data) => {\n              ws._socket.push = push;\n              ws._socket.push(data);\n              ws.terminate();\n            };\n\n            const payload1 = Buffer.alloc(highWaterMark - 1024);\n            const payload2 = Buffer.alloc(1);\n\n            const opts = {\n              fin: true,\n              opcode: 0x02,\n              mask: false,\n              readOnly: false\n            };\n\n            const list = [\n              ...Sender.frame(payload1, { rsv1: false, ...opts }),\n              ...Sender.frame(payload2, { rsv1: true, ...opts })\n            ];\n\n            for (let i = 0; i < 340; i++) {\n              list.push(list[list.length - 2], list[list.length - 1]);\n            }\n\n            const data = Buffer.concat(list);\n\n            assert.ok(data.length > highWaterMark);\n\n            // This hack is used because there is no guarantee that more than\n            // `highWaterMark` bytes will be sent as a single TCP packet.\n            push.call(ws._socket, data);\n\n            wss.clients\n              .values()\n              .next()\n              .value.send(payload2, { compress: false });\n          });\n\n          ws.on('message', (message, isBinary) => {\n            assert.ok(isBinary);\n            messageLengths.push(message.length);\n          });\n\n          ws.on('close', (code) => {\n            assert.strictEqual(code, 1006);\n            assert.strictEqual(messageLengths.length, 343);\n            assert.strictEqual(messageLengths[0], highWaterMark - 1024);\n            assert.strictEqual(messageLengths[messageLengths.length - 1], 1);\n            wss.close(done);\n          });\n        }\n      );\n    });\n\n    it('handles a close frame received while compressing data', (done) => {\n      const wss = new WebSocket.Server(\n        {\n          perMessageDeflate: true,\n          port: 0\n        },\n        () => {\n          const ws = new WebSocket(`ws://localhost:${wss.address().port}`, {\n            perMessageDeflate: { threshold: 0 }\n          });\n\n          ws.on('open', () => {\n            ws._receiver.on('conclude', () => {\n              assert.strictEqual(ws._sender._state, 1);\n            });\n\n            ws.send('foo');\n            ws.send('bar');\n            ws.send('baz');\n            ws.send('qux');\n          });\n        }\n      );\n\n      wss.on('connection', (ws) => {\n        const messages = [];\n\n        ws.on('message', (message, isBinary) => {\n          assert.ok(!isBinary);\n          messages.push(message.toString());\n        });\n\n        ws.on('close', (code, reason) => {\n          assert.deepStrictEqual(messages, ['foo', 'bar', 'baz', 'qux']);\n          assert.strictEqual(code, 1000);\n          assert.deepStrictEqual(reason, EMPTY_BUFFER);\n          wss.close(done);\n        });\n\n        ws.close(1000);\n      });\n    });\n\n    describe('#close', () => {\n      it('can be used while data is being decompressed', (done) => {\n        const wss = new WebSocket.Server(\n          {\n            perMessageDeflate: true,\n            port: 0\n          },\n          () => {\n            const messages = [];\n            const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n            ws.on('message', (message, isBinary) => {\n              assert.ok(!isBinary);\n\n              if (messages.push(message.toString()) > 1) return;\n\n              setImmediate(() => {\n                process.nextTick(() => {\n                  assert.strictEqual(ws._receiver._state, 5);\n                  ws.close(1000);\n                });\n              });\n            });\n\n            ws.on('close', (code, reason) => {\n              assert.deepStrictEqual(messages, ['', '', '', '']);\n              assert.strictEqual(code, 1000);\n              assert.deepStrictEqual(reason, EMPTY_BUFFER);\n              wss.close(done);\n            });\n          }\n        );\n\n        wss.on('connection', (ws) => {\n          const buf = Buffer.from('c10100c10100c10100c10100', 'hex');\n          ws._socket.write(buf);\n        });\n      });\n    });\n\n    describe('#send', () => {\n      it('can send text data', (done) => {\n        const wss = new WebSocket.Server(\n          {\n            perMessageDeflate: { threshold: 0 },\n            port: 0\n          },\n          () => {\n            const ws = new WebSocket(`ws://localhost:${wss.address().port}`, {\n              perMessageDeflate: { threshold: 0 }\n            });\n\n            ws.on('open', () => {\n              ws.send('hi', { compress: true });\n              ws.close();\n            });\n\n            ws.on('message', (message, isBinary) => {\n              assert.deepStrictEqual(message, Buffer.from('hi'));\n              assert.ok(!isBinary);\n              wss.close(done);\n            });\n          }\n        );\n\n        wss.on('connection', (ws) => {\n          ws.on('message', (message, isBinary) => {\n            ws.send(message, { binary: isBinary, compress: true });\n          });\n        });\n      });\n\n      it('can send a `TypedArray`', (done) => {\n        const array = new Float32Array(5);\n\n        for (let i = 0; i < array.length; i++) {\n          array[i] = i / 2;\n        }\n\n        const wss = new WebSocket.Server(\n          {\n            perMessageDeflate: { threshold: 0 },\n            port: 0\n          },\n          () => {\n            const ws = new WebSocket(`ws://localhost:${wss.address().port}`, {\n              perMessageDeflate: { threshold: 0 }\n            });\n\n            ws.on('open', () => {\n              ws.send(array, { compress: true });\n              ws.close();\n            });\n\n            ws.on('message', (message, isBinary) => {\n              assert.deepStrictEqual(message, Buffer.from(array.buffer));\n              assert.ok(isBinary);\n              wss.close(done);\n            });\n          }\n        );\n\n        wss.on('connection', (ws) => {\n          ws.on('message', (message, isBinary) => {\n            assert.ok(isBinary);\n            ws.send(message, { compress: true });\n          });\n        });\n      });\n\n      it('can send an `ArrayBuffer`', (done) => {\n        const array = new Float32Array(5);\n\n        for (let i = 0; i < array.length; i++) {\n          array[i] = i / 2;\n        }\n\n        const wss = new WebSocket.Server(\n          {\n            perMessageDeflate: { threshold: 0 },\n            port: 0\n          },\n          () => {\n            const ws = new WebSocket(`ws://localhost:${wss.address().port}`, {\n              perMessageDeflate: { threshold: 0 }\n            });\n\n            ws.on('open', () => {\n              ws.send(array.buffer, { compress: true });\n              ws.close();\n            });\n\n            ws.on('message', (message, isBinary) => {\n              assert.deepStrictEqual(message, Buffer.from(array.buffer));\n              assert.ok(isBinary);\n              wss.close(done);\n            });\n          }\n        );\n\n        wss.on('connection', (ws) => {\n          ws.on('message', (message, isBinary) => {\n            assert.ok(isBinary);\n            ws.send(message, { compress: true });\n          });\n        });\n      });\n\n      it('can send a `Blob`', function (done) {\n        if (!hasBlob) return this.skip();\n\n        const wss = new WebSocket.Server(\n          {\n            perMessageDeflate: { threshold: 0 },\n            port: 0\n          },\n          () => {\n            const ws = new WebSocket(`ws://localhost:${wss.address().port}`, {\n              perMessageDeflate: { threshold: 0 }\n            });\n\n            const messages = [];\n\n            ws.on('open', () => {\n              ws.send(new Blob(['foo']));\n              ws.send(new Blob(['bar']));\n              ws.close();\n            });\n\n            ws.on('message', (message, isBinary) => {\n              assert.ok(isBinary);\n              messages.push(message.toString());\n\n              if (messages.length === 2) {\n                assert.deepStrictEqual(messages, ['foo', 'bar']);\n                wss.close(done);\n              }\n            });\n          }\n        );\n\n        wss.on('connection', (ws) => {\n          ws.on('message', (message, isBinary) => {\n            assert.ok(isBinary);\n            ws.send(message);\n          });\n        });\n      });\n\n      it('ignores the `compress` option if the extension is disabled', (done) => {\n        const wss = new WebSocket.Server({ port: 0 }, () => {\n          const ws = new WebSocket(`ws://localhost:${wss.address().port}`, {\n            perMessageDeflate: false\n          });\n\n          ws.on('open', () => {\n            ws.send('hi', { compress: true });\n            ws.close();\n          });\n\n          ws.on('message', (message, isBinary) => {\n            assert.deepStrictEqual(message, Buffer.from('hi'));\n            assert.ok(!isBinary);\n            wss.close(done);\n          });\n        });\n\n        wss.on('connection', (ws) => {\n          ws.on('message', (message, isBinary) => {\n            ws.send(message, { binary: isBinary, compress: true });\n          });\n        });\n      });\n\n      it('calls the callback if the socket is closed prematurely', (done) => {\n        const called = [];\n        const wss = new WebSocket.Server(\n          { perMessageDeflate: true, port: 0 },\n          () => {\n            const ws = new WebSocket(`ws://localhost:${wss.address().port}`, {\n              perMessageDeflate: { threshold: 0 }\n            });\n\n            ws.on('open', () => {\n              ws.send('foo');\n              ws.send('bar', (err) => {\n                called.push(1);\n\n                assert.strictEqual(ws.readyState, WebSocket.CLOSING);\n                assert.ok(err instanceof Error);\n                assert.strictEqual(\n                  err.message,\n                  'The socket was closed while data was being compressed'\n                );\n              });\n              ws.send('baz');\n              ws.send('qux', (err) => {\n                called.push(2);\n\n                assert.strictEqual(ws.readyState, WebSocket.CLOSING);\n                assert.ok(err instanceof Error);\n                assert.strictEqual(\n                  err.message,\n                  'The socket was closed while data was being compressed'\n                );\n              });\n              ws.close();\n            });\n          }\n        );\n\n        wss.on('connection', (ws) => {\n          ws.on('close', () => {\n            assert.deepStrictEqual(called, [1, 2]);\n            wss.close(done);\n          });\n\n          ws._socket.end();\n        });\n      });\n    });\n\n    describe('#terminate', () => {\n      it('can be used while data is being compressed', (done) => {\n        const wss = new WebSocket.Server(\n          {\n            perMessageDeflate: { threshold: 0 },\n            port: 0\n          },\n          () => {\n            const ws = new WebSocket(`ws://localhost:${wss.address().port}`, {\n              perMessageDeflate: { threshold: 0 }\n            });\n\n            ws.on('open', () => {\n              ws.send('hi', (err) => {\n                assert.strictEqual(ws.readyState, WebSocket.CLOSING);\n                assert.ok(err instanceof Error);\n                assert.strictEqual(\n                  err.message,\n                  'The socket was closed while data was being compressed'\n                );\n\n                ws.on('close', () => {\n                  wss.close(done);\n                });\n              });\n              ws.terminate();\n            });\n          }\n        );\n      });\n\n      it('can be used while data is being decompressed', (done) => {\n        const wss = new WebSocket.Server(\n          {\n            perMessageDeflate: true,\n            port: 0\n          },\n          () => {\n            const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n            const messages = [];\n\n            ws.on('message', (message, isBinary) => {\n              assert.ok(!isBinary);\n\n              if (messages.push(message.toString()) > 1) return;\n\n              setImmediate(() => {\n                process.nextTick(() => {\n                  assert.strictEqual(ws._receiver._state, 5);\n                  ws.terminate();\n                });\n              });\n            });\n\n            ws.on('close', (code, reason) => {\n              assert.deepStrictEqual(messages, ['', '', '', '']);\n              assert.strictEqual(code, 1006);\n              assert.strictEqual(reason, EMPTY_BUFFER);\n              wss.close(done);\n            });\n          }\n        );\n\n        wss.on('connection', (ws) => {\n          const buf = Buffer.from('c10100c10100c10100c10100', 'hex');\n          ws._socket.write(buf);\n        });\n      });\n    });\n  });\n\n  describe('Connection close', () => {\n    it('closes cleanly after simultaneous errors (1/2)', (done) => {\n      let clientCloseEventEmitted = false;\n      let serverClientCloseEventEmitted = false;\n\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('error', (err) => {\n          assert.ok(err instanceof RangeError);\n          assert.strictEqual(err.code, 'WS_ERR_INVALID_OPCODE');\n          assert.strictEqual(\n            err.message,\n            'Invalid WebSocket frame: invalid opcode 5'\n          );\n\n          ws.on('close', (code, reason) => {\n            assert.strictEqual(code, 1006);\n            assert.strictEqual(reason, EMPTY_BUFFER);\n\n            clientCloseEventEmitted = true;\n            if (serverClientCloseEventEmitted) wss.close(done);\n          });\n        });\n\n        ws.on('open', () => {\n          // Write an invalid frame in both directions to trigger simultaneous\n          // failure.\n          const chunk = Buffer.from([0x85, 0x00]);\n\n          wss.clients.values().next().value._socket.write(chunk);\n          ws._socket.write(chunk);\n        });\n      });\n\n      wss.on('connection', (ws) => {\n        ws.on('error', (err) => {\n          assert.ok(err instanceof RangeError);\n          assert.strictEqual(err.code, 'WS_ERR_INVALID_OPCODE');\n          assert.strictEqual(\n            err.message,\n            'Invalid WebSocket frame: invalid opcode 5'\n          );\n\n          ws.on('close', (code, reason) => {\n            assert.strictEqual(code, 1006);\n            assert.strictEqual(reason, EMPTY_BUFFER);\n\n            serverClientCloseEventEmitted = true;\n            if (clientCloseEventEmitted) wss.close(done);\n          });\n        });\n      });\n    });\n\n    it('closes cleanly after simultaneous errors (2/2)', (done) => {\n      let clientCloseEventEmitted = false;\n      let serverClientCloseEventEmitted = false;\n\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n\n        ws.on('error', (err) => {\n          assert.ok(err instanceof RangeError);\n          assert.strictEqual(err.code, 'WS_ERR_INVALID_OPCODE');\n          assert.strictEqual(\n            err.message,\n            'Invalid WebSocket frame: invalid opcode 5'\n          );\n\n          ws.on('close', (code, reason) => {\n            assert.strictEqual(code, 1006);\n            assert.strictEqual(reason, EMPTY_BUFFER);\n\n            clientCloseEventEmitted = true;\n            if (serverClientCloseEventEmitted) wss.close(done);\n          });\n        });\n\n        ws.on('open', () => {\n          // Write an invalid frame in both directions and change the\n          // `readyState` to `WebSocket.CLOSING`.\n          const chunk = Buffer.from([0x85, 0x00]);\n          const serverWs = wss.clients.values().next().value;\n\n          serverWs._socket.write(chunk);\n          serverWs.close();\n\n          ws._socket.write(chunk);\n          ws.close();\n        });\n      });\n\n      wss.on('connection', (ws) => {\n        ws.on('error', (err) => {\n          assert.ok(err instanceof RangeError);\n          assert.strictEqual(err.code, 'WS_ERR_INVALID_OPCODE');\n          assert.strictEqual(\n            err.message,\n            'Invalid WebSocket frame: invalid opcode 5'\n          );\n\n          ws.on('close', (code, reason) => {\n            assert.strictEqual(code, 1006);\n            assert.strictEqual(reason, EMPTY_BUFFER);\n\n            serverClientCloseEventEmitted = true;\n            if (clientCloseEventEmitted) wss.close(done);\n          });\n        });\n      });\n    });\n\n    it('resumes the socket when an error occurs', (done) => {\n      const maxPayload = 16 * 1024;\n      const wss = new WebSocket.Server({ maxPayload, port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n      });\n\n      wss.on('connection', (ws) => {\n        const list = [\n          ...Sender.frame(Buffer.alloc(maxPayload + 1), {\n            fin: true,\n            opcode: 0x02,\n            mask: true,\n            readOnly: false\n          })\n        ];\n\n        ws.on('error', (err) => {\n          assert.ok(err instanceof RangeError);\n          assert.strictEqual(err.code, 'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH');\n          assert.strictEqual(err.message, 'Max payload size exceeded');\n\n          ws.on('close', (code, reason) => {\n            assert.strictEqual(code, 1006);\n            assert.strictEqual(reason, EMPTY_BUFFER);\n            wss.close(done);\n          });\n        });\n\n        ws._socket.push(Buffer.concat(list));\n      });\n    });\n\n    it('resumes the socket when the close frame is received', (done) => {\n      const wss = new WebSocket.Server({ port: 0 }, () => {\n        const ws = new WebSocket(`ws://localhost:${wss.address().port}`);\n      });\n\n      wss.on('connection', (ws) => {\n        const opts = { fin: true, mask: true, readOnly: false };\n        const list = [\n          ...Sender.frame(Buffer.alloc(16 * 1024), { opcode: 0x02, ...opts }),\n          ...Sender.frame(EMPTY_BUFFER, { opcode: 0x08, ...opts })\n        ];\n\n        ws.on('close', (code, reason) => {\n          assert.strictEqual(code, 1005);\n          assert.strictEqual(reason, EMPTY_BUFFER);\n          wss.close(done);\n        });\n\n        ws._socket.push(Buffer.concat(list));\n      });\n    });\n  });\n});\n"
  },
  {
    "path": "wrapper.mjs",
    "content": "import createWebSocketStream from './lib/stream.js';\nimport extension from './lib/extension.js';\nimport PerMessageDeflate from './lib/permessage-deflate.js';\nimport Receiver from './lib/receiver.js';\nimport Sender from './lib/sender.js';\nimport subprotocol from './lib/subprotocol.js';\nimport WebSocket from './lib/websocket.js';\nimport WebSocketServer from './lib/websocket-server.js';\n\nexport {\n  createWebSocketStream,\n  extension,\n  PerMessageDeflate,\n  Receiver,\n  Sender,\n  subprotocol,\n  WebSocket,\n  WebSocketServer\n};\n\nexport default WebSocket;\n"
  }
]