{"name":"it-ws","version":"3.0.2","keywords":["async","iterable","iterator","websocket","ws"],"author":{"name":"Alan Shaw"},"license":"MIT","_id":"it-ws@3.0.2","homepage":"https://github.com/alanshaw/it-ws","bugs":{"url":"https://github.com/alanshaw/it-ws/issues"},"dist":{"shasum":"65223b7bfbe8f8239b75edef4d4a3cd7e330b693","tarball":"http://registry-npm.ywwl.com/it-ws/download/it-ws-3.0.2.tgz","fileCount":27,"integrity":"sha512-INZhCXNjd5Xr7mYWtNZQb9y5i6XIsf4CKD4XUXeCD3tbaoIya1bPVtJNP1lN5UVGo6Ql9rAn3WVre/8IKtKShw==","signatures":[{"sig":"MEUCIQDopfwFu7rQlh0ALh39RS1rS3tNJ7So9Iun4Vy/ud2RAwIgZkZh+xD2lCKO7Cpd8xRiJ8rMZ2Tgh2VmCvZZo36Ldzs=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":26512,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfI+raCRA9TVsSAnZWagAA1LMQAIbtgfJvhXJWyscyTCVR\neIzMfarcsjUSKxyL5GcBryHhtzUWgMri3W9/sWRaCFy0dTMRSMLMhDS25FFn\nLesbwtKu+uLmR7FzAv+rjni90CAxgk2Eu/ijDqZiSrbPVDsTqxVkjkWz5BXB\njxIWVcyjg2yZFfMHnftMyz4X3nq8F/MZNbM1SEp/nyJ57LfH7Bo7sKo1nwJz\nAiW50o5AtArDBPVETx5NOPmXyMvLUj1RDoFYamGqxhIDu3Rm3TPGwZAvJNJB\nHO+W32wrJ0YXmdYI2v5FkUhkNeOxJIt0WyMl9bzOYzP97u/3Y2FRLiZ+MhXz\nqQKHyRAbg+bTch2wSsuS9nHq5Kl/CQ3LaedVj7+Nlj3Zhl7UvDRRp24iLZFH\nUMx6LWgEQyK6iOzo/RvICGt/Xf8DN83M3mgfQmvNZao6sNGeqiWD59HrpLaY\n5ITjNnYw30MKuaTXAIUJV/lrdTicsSRomx6xBJDUzWDzdNt8BZPdCXx8v0yz\nGJKLD/C9HM69WmGjSCyiZcu63rJWZpToKzxMc7Ao5u4FkglmKdmOFZGAj/NA\nsa3W9JPDP1VduqVtL0aF7lFQbXj3Yw6q4e/Vbb9mXSgQBWklmFtSKixPd1u/\nOFF4uq9V9B83CauSVUrSbn604ikNpf+jmmSFrLKX0BjOML5EJ65yseJNZkPs\nCZmb\r\n=90oP\r\n-----END PGP SIGNATURE-----\r\n","size":8573,"noattachment":false,"key":"/it-ws/-/it-ws-3.0.2.tgz"},"main":"index.js","browser":{"ws":false,"./server":false},"gitHead":"41d6fe5f847d9436ad6c4115bf3f017b745c4474","scripts":{"lint":"standard","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.14.5","description":"Simple async iterables for websocket client connections","directories":{},"_nodeVersion":"14.5.0","dependencies":{"ws":"^7.3.1","buffer":"^5.6.0","relative-url":"^1.0.2","event-iterator":"^2.0.0"},"_hasShrinkwrap":false,"devDependencies":{"tap":"^14.10.8","tape":"^5.0.1","wsurl":"^1.0.0","it-pipe":"^1.0.0","standard":"^14.3.4","mapleTree":"^0.5.1","it-goodbye":"^2.0.2","iterable-ndjson":"^1.1.0","streaming-iterables":"^5.0.2"},"_npmOperationalInternal":{"tmp":"tmp/it-ws_3.0.2_1596189401860_0.3454980943458381","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":1596189402010,"maintainers":[{"name":"achingbrain","email":"alex@achingbrain.net"},{"name":"alanshaw","email":"alan138@gmail.com"}],"_cnpm_publish_time":1596189402010,"dist-tags":{"latest":"6.1.5"}}