{"name":"it-ws","version":"3.0.0","keywords":["async","iterable","iterator","websocket","ws"],"author":{"name":"Alan Shaw"},"license":"MIT","_id":"it-ws@3.0.0","homepage":"https://github.com/alanshaw/it-ws","bugs":{"url":"https://github.com/alanshaw/it-ws/issues"},"dist":{"shasum":"b130440328f43cb70b68e734bc9476ebc81219c3","tarball":"http://registry-npm.ywwl.com/it-ws/download/it-ws-3.0.0.tgz","fileCount":27,"integrity":"sha512-9qPAgBPA4yyu8XC71ugOhTOrEDpmSqVeEoJeAaNmtQIOu4sByzO8rsLq1GFKChaEhf8nbtK6jxr3e7LmpjMqmQ==","signatures":[{"sig":"MEQCIBYPlQalZFfb7W7GspG28Gxpigyril3Guo8JxaTzsH83AiAfrkOOoxJuzDM81eMjVIckAvX6s9mScBaIt9ISdt9ZqQ==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":26451,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJePXOmCRA9TVsSAnZWagAAlq0P/0q4lRlqAVzqtavmm0Nz\nxllsII7vFAajYcMQlpFRJNS7LPrCHVsLM6LGj0ZENbWQpNg8v3u+b7yIkWeN\n1Y1l+218dzjVsDqltloqV1tOPQ4/Nh2qfrzA6iJyIXIaqWhG9s803vHNRfGe\n+aJeGaMeKw2bfrBTW3v7WBIDqJthcCrZgusZDH2dt2qlvBnkozRo9GvdBVXW\nAEyFLTL09VwctJk1Vs0fW+/vonD4TfvYVOWFDREKEZAtY/nqJ1Vjak+VD8on\nEJa1eD8NiNMZQ2Aum7Iv4XDsQV/cOJK7l99XhhCtWieD3FHRGlxKaYHjB/pY\ne8UT2Ziu+y9XvzXMp8nE3xlUcCFN/ILgoF5jcxkxhdcmEkQikbrIOfM2iIGd\nQEAsApcehBKcGdE2JAORCDlLhrVthacqbl1GbJVl1NyStZ7y5fEXzMlFNvI8\n38KleA/0kqgqBdlSJ1mpeXdBM7noZD4x/9EJW1Y1j3miglivyAHGW7hDMhqz\nL/9eNe4EnSdKT/dZI/nz353EqiLAKInapnd2DuIVR+KFm2TeouO5lpEEJ5II\nCqj1L1Zmik/wIsZSyBMC2UCApo9n6KQocHRj1lEOqmnKawM0BAUd6JOicAsa\nm1ewZYD+Pd1qJhUTNmJPUOnK3JO/tka2ECLwT8V7CjeZTF182hQBW8ZuGWcF\n3g2H\r\n=oSLs\r\n-----END PGP SIGNATURE-----\r\n","size":8516,"noattachment":false,"key":"/it-ws/-/it-ws-3.0.0.tgz"},"main":"index.js","browser":{"ws":false,"./server":false},"gitHead":"de303fcb65b9ec0f64dc86858a4469a633b4bb78","scripts":{"test":"tap --jobs=1 test/*.js","start":"node server.js"},"_npmUser":{"name":"alanshaw","email":"alan138@gmail.com"},"repository":{"url":"git+https://github.com/alanshaw/it-ws.git","type":"git"},"_npmVersion":"6.13.4","description":"Simple async iterables for websocket client connections","directories":{},"_nodeVersion":"12.15.0","dependencies":{"ws":"^7.2.1","buffer":"^5.4.3","relative-url":"^1.0.2","event-iterator":"^1.2.0"},"_hasShrinkwrap":false,"devDependencies":{"tap":"^14.10.6","tape":"^4.4.0","wsurl":"^1.0.0","it-pipe":"^1.0.0","standard":"^14.3.1","mapleTree":"^0.5.1","it-goodbye":"^2.0.0","iterable-ndjson":"^1.1.0","streaming-iterables":"^4.0.2"},"_npmOperationalInternal":{"tmp":"tmp/it-ws_3.0.0_1581085605986_0.27443212918387316","host":"s3://npm-registry-packages"},"readme":"# it-ws <!-- omit in toc -->\n\n[![codecov](https://img.shields.io/codecov/c/github/alanshaw/it-ws.svg?style=flat-square)](https://codecov.io/gh/alanshaw/it-ws)\n[![CI](https://img.shields.io/github/actions/workflow/status/alanshaw/it-ws/js-test-and-release.yml?branch=master\\&style=flat-square)](https://github.com/alanshaw/it-ws/actions/workflows/js-test-and-release.yml?query=branch%3Amaster)\n\n> Simple async iterables for websocket client connections\n\n## Table of contents <!-- omit in toc -->\n\n- [Install](#install)\n  - [Browser `<script>` tag](#browser-script-tag)\n- [Usage](#usage)\n  - [Example - client](#example---client)\n  - [Example - server](#example---server)\n- [API](#api)\n  - [`import { connect } from 'it-ws/client'`](#import--connect--from-it-wsclient)\n  - [`import { createServer } from 'it-ws/server'`](#import--createserver--from-it-wsserver)\n    - [Example](#example)\n- [use with an http server](#use-with-an-http-server)\n  - [core, websocket wrapping functions](#core-websocket-wrapping-functions)\n  - [`import duplex from 'it-ws/duplex'`](#import-duplex-from-it-wsduplex)\n  - [`import sink from 'it-ws/sink'`](#import-sink-from-it-wssink)\n  - [`import source from 'it-ws/source'`](#import-source-from-it-wssource)\n- [License](#license)\n- [Contribution](#contribution)\n\n## Install\n\n```console\n$ npm i it-ws\n```\n\n### Browser `<script>` tag\n\nLoading this module through a script tag will make it's exports available as `ItWs` in the global namespace.\n\n```html\n<script src=\"https://unpkg.com/it-ws/dist/index.min.js\"></script>\n```\n\n## Usage\n\n### Example - client\n\n```js\nimport { connect } from 'it-ws/client'\nimport { pipe } from 'it-pipe'\n\nconst stream = connect(WS_URL)\n\nawait stream.connected() // Wait for websocket to be connected (optional)\n\npipe(source, stream, sink)\n```\n\n### Example - server\n\n```js\nimport { createServer } from 'it-ws/server'\nimport { pipe } from 'it-pipe'\n\nconst server = createServer(stream => {\n  //pipe the stream somewhere.\n  //eg, echo server\n  pipe(stream, stream)\n})\n\nawait server.listen(PORT)\n```\n\n## API\n\n### `import { connect } from 'it-ws/client'`\n\n`connect(url, { binary: boolean })`\n\nCreate a websocket client connection. Set `binary: true` to get a stream of arrayBuffers (on the browser). Defaults to true on node, but to strings on the browser. This may cause a problems if your application assumes binary.\n\nFor adding options to the WebSocket instance, as [websockets/ws/blob/master/doc/ws.md#new-websocketaddress-protocols-options](https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketaddress-protocols-options), you can provide an object with the `websocket` property into the connect options.\n\n```js\nconst stream = connect(url)\n// stream is duplex and is both a `source` and `sink`.\n// See this for more information:\n// https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#duplex-it\n```\n\n### `import { createServer } from 'it-ws/server'`\n\nCreate async iterable websocket servers.\n\n`createServer(options?, onConnection)`\n\n`options` takes the same server options as [ws module](https://github.com/websockets/ws/blob/master/doc/ws.md#new-wsserveroptions-callback)\n\n`onConnection(stream)` is called every time a connection is received.\n\n#### Example\n\nOne duplex service you may want to use this with is [muxrpc](https://github.com/dominictarr/muxrpc)\n\n```js\nimport { createServer } from 'it-ws/server'\nimport { connect } from 'it-ws/client'\nimport { pipe } from 'it-pipe'\n\ncreateServer({\n  onConnection: (stream) => {\n    // pipe duplex style to your service\n    pipe(stream, service.createStream(), stream)\n  }\n})\n.listen(9999)\n\nconst stream = client.createStream()\n\nawait pipe(\n  stream,\n  connect('ws://localhost:9999'),\n  stream\n)\n```\n\nif the connection fails, the stream will throw\n\n```js\ntry {\n  await pipe(\n    stream,\n    connect('ws://localhost:9999'),\n    stream\n  )\n} catch (err) {\n  // handle err\n}\n```\n\nTo run the server over TLS:\n\n```js\ncreateServer({\n  key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),\n  cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')\n  // other options\n})\n.listen(9999)\n```\n\nTo add client-authentication to the server, you can set `verifyClient`.\n[Documentation here](https://github.com/websockets/ws/blob/master/doc/ws.md#optionsverifyclient).\n\n```js\nfunction verifyClient (info) {\n  return info.secure == true\n}\ncreateServer({\n  verifyClient: verifyClient\n  // other options\n})\n```\n\n## use with an http server\n\nif you have an http server that you also need to serve stuff\nover, and want to use a single port, use the `server` option.\n\n```js\nimport http from 'http'\n\nconst server = http.createServer(function(req, res){...}).listen(....)\n\ncreateServer({\n  server: server\n  // other options\n})\n```\n\n### core, websocket wrapping functions\n\nthese modules are used internally, to wrap a websocket.\nyou probably won't need to touch these,\nbut they are documented anyway.\n\n### `import duplex from 'it-ws/duplex'`\n\nturn a websocket into a duplex stream.\nIf provided, `opts` is passed to `sink(socket, opts)`.\n\nWebSockets do not support half open mode.\n[see allowHalfOpen option in net module](http://nodejs.org/api/net.html#net_net_createserver_options_connectionlistener)\n\nIf you have a protocol that assumes halfOpen connections, but are using\na networking protocol like websockets that does not support it, I suggest\nusing [it-goodbye](https://github.com/alanshaw/it-goodbye) with your\nprotocol.\n\nThe duplex stream will also contain a copy of the properties from\nthe http request that became the websocket. they are `method`, `url`,\n`headers` and `upgrade`.\n\nalso exposed at: `import { duplex } from 'it-ws'`\n\n### `import sink from 'it-ws/sink'`\n\nCreate a `Sink` that will write data to the `socket`.\n`opts` may be `{closeOnEnd: true, onClose: onClose}`.\n`onClose` will be called when the sink ends. If `closeOnEnd=false`\nthe stream will not close, it will just stop emitting data.\n(by default `closeOnEnd` is true)\n\nIf `opts` is a function, then `onClose = opts; opts.closeOnEnd = true`.\n\n```js\nimport sink from 'it-ws/sink'\nimport { pipe } from 'it-pipe'\nimport each from 'it-foreach'\nimport delay from 'delay'\n\n// connect to the echo endpoint for test/server.js\nvar socket = new WebSocket('wss://echo.websocket.org')\n\n// write values to the socket\npipe(\n  async function * () {\n    while (true) {\n      yield 'hello @ ' + Date.now()\n    }\n  }(),\n  // throttle so it doesn't go nuts\n  (source) => each(source, () => delay(100))\n  sink(socket)\n);\n\nsocket.addEventListener('message', function(evt) {\n  console.log('received: ' + evt.data);\n});\n```\n\nalso exposed at `import { sink } from 'it-ws'`\n\n### `import source from 'it-ws/source'`\n\nCreate a `Source` that will read data from the `socket`.\n\n```js\nimport { pipe } from 'it-pipe'\nimport source from 'it-ws/source'\nimport { toString } from 'uint8arrays/to-string'\n\npipe(\n  // connect to the test/server.js endpoint\n  source(new WebSocket('ws://localhost:3000/read')),\n  async (source) => {\n    for await (const buf of source) {\n      console.info(toString(buf))\n    }\n  }\n);\n\n```\n\nalso exposed at `import { source } from 'it-ws'`\n\n## License\n\nLicensed under either of\n\n- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)\n- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)\n\n## Contribution\n\nUnless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.\n","publish_time":1581085606160,"maintainers":[{"name":"achingbrain","email":"alex@achingbrain.net"},{"name":"alanshaw","email":"alan138@gmail.com"}],"_cnpm_publish_time":1581085606160,"dist-tags":{"latest":"6.1.5"}}