{"name":"udx-native","version":"0.0.8","keywords":["tcp","udp","stream","reliable"],"author":{"url":"@mafintosh","name":"Mathias Buus"},"license":"MIT","_id":"udx-native@0.0.8","homepage":"https://github.com/mafintosh/libudx#readme","bugs":{"url":"https://github.com/mafintosh/libudx/issues"},"dist":{"shasum":"212c2c0c696523959a6caf3be06ef3c4186b390a","tarball":"http://registry-npm.ywwl.com/udx-native/download/udx-native-0.0.8.tgz","fileCount":20,"integrity":"sha512-pwO+coQeEsTloPo6Saf23ZRip3Ehq/6KPiWb5Y31h3CN8JxbdXyHFUPhQQ1Gn5Re1U2lLZDiB3aQaokbfINJ/g==","signatures":[{"sig":"MEUCIQDpT0Bu4D7Kw4rCaDdYXD7bqYDjYjSOg7XlgzZZvAQ2gwIgA9rEE6AsnKhvYLpm13A7jJp5pjHx7UHESkO2F6zRgjc=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":79821,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJicScBACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmqIFA//QSOIJG/t8wTfoHymoRB3SYMCMRBCLu2i8C2IBq6NJ60o/qV1\r\nl08uzyWKHN/aHyVufVuhul70pug4otOidstE4AYx18IRwgEI/MSmJ+q7A9HW\r\n2EhiFh154c1m1HldVRJ0ghZhwLnH0VgYcJ8pqTN1p8acKFkWAGqZ2EpNn0ja\r\n862IkLNJo3u2euOX2OnGI4fhZpnsMiHYqx2da71cNChrO93lbRJkBY1Fgeda\r\nYqGfEpj7KgT98NluOFoAPhukQz4VJrguKywjec+UEgv73e3UmRPqDOCtIn+a\r\nPIjyVfzYlFwMbawB6aQTh+N3QZG6AKsK+IG2xJW4slE5e/MXkC1LoUEGbDHh\r\nOj1rwxcb0I2RQeZpo8NY6Vb06d8od4BKP8T4efJWfcIEAWW2ycIa4ac1MGZ3\r\nmea6FSdfia+xLos3jRVjD+m0i8BAbplKfZAWMW4mS1hdJSeEQwBC4c5/9TbN\r\n9LrA6jWDiromN566VEsWJEb2LShrKb3XoFIPIO8UhkQIsw+OeIObdy6S7bsh\r\nByRyQHNT+M/yG2cV+ER+Fu8rif1/q7H/hs8Xz3pLMYjVObFg3HZbB4wUOhNH\r\n0SibPI5M7EM6q1iwQ2u6W1V/YtmtngGQZTsJbw627ApjcE3DbnpP/D2R2bDo\r\nLeCI47jZNu57FCsSlGxmwES9EpPIpy9sLYQ=\r\n=rH1x\r\n-----END PGP SIGNATURE-----\r\n","size":18591,"noattachment":false,"key":"/udx-native/-/udx-native-0.0.8.tgz"},"main":"lib/udx.js","gitHead":"eb79caa74e3a688a38920d9f5a27caee3e20cdf4","gypfile":true,"scripts":{"test":"standard && brittle test/*.js","bench":"node test/bench/bench.js","install":"node-gyp rebuild"},"_npmUser":{"name":"kasperisager","email":"kasperisager@hey.com"},"repository":{"url":"git+https://github.com/mafintosh/libudx.git","type":"git"},"_npmVersion":"8.5.5","description":"udx is reliable, multiplex, and congestion controlled streams over udp","directories":{},"_nodeVersion":"17.8.0","dependencies":{"b4a":"^1.5.0","streamx":"^2.12.0","napi-macros":"^2.0.0","node-gyp-build":"^4.3.0"},"_hasShrinkwrap":false,"devDependencies":{"is-ci":"^3.0.1","brittle":"^2.0.3","standard":"^16.0.4","nanobench-utils":"^1.0.0"},"_npmOperationalInternal":{"tmp":"tmp/udx-native_0.0.8_1651582721186_0.32578545472112386","host":"s3://npm-registry-packages"},"readme":"# udx-native\n\nudx is reliable, multiplexed, and congestion-controlled streams over udp.\n\n```\nnpm i udx-native\n```\n\nIt's a transport protocol, made only for peer-to-peer networking.\n\nNo handshakes. No encryption. No features. This is good for P2P.\\\nJust fast streams and messages that are composable into powerful things.\n\n## Usage\n\n```js\nconst UDX = require('udx-native')\n\nconst u = new UDX()\nconst a = u.createSocket()\nconst b = u.createSocket()\n\nb.on('message', function (message) {\n  console.log('received', message.toString())\n  a.close()\n  b.close()\n})\n\nb.bind(0)\na.send(Buffer.from('hello'), b.address().port)\n```\n\n```js\nconst UDX = require('udx-native')\n\nconst u = new UDX()\n\nconst socket1 = u.createSocket()\nconst socket2 = u.createSocket()\n\nsocket1.bind()\nsocket2.bind()\n\nconst stream1 = u.createStream(1)\nconst stream2 = u.createStream(2)\n\nstream1.connect(socket1, stream2.id, socket2.address().port, '127.0.0.1')\nstream2.connect(socket2, stream1.id, socket1.address().port, '127.0.0.1')\n\nstream1.write(Buffer.from('hello'))\nstream1.end()\n\nstream2.on('data', function (data) {\n  console.log(data)\n})\n\nstream2.on('end', function () {\n  stream2.end()\n})\n\nstream1.on('close', function () {\n  console.log('stream1 closed')\n  socket1.close()\n})\n\nstream2.on('close', function () {\n  console.log('stream2 closed')\n  socket2.close()\n})\n```\n\n## API\n\n#### `const udx = new UDX()`\n\nCreates a new UDX instance.\n\n#### `const bool = UDX.isIPv4(host)`\n\nReturns `true` if host is an IPv4 address.\n\n#### `const bool = UDX.isIPv6(host)`\n\nReturns `true` if host is an IPv6 address.\n\n#### `const family = UDX.isIP(host)`\n\nReturns the address family (`4` or `6`). Returns `0` if invalid.\n\n## Sockets\n\n#### `const socket = udx.createSocket([options])`\n\nCreates a new socket instance.\n\nAvailable `options`:\n```js\n{\n  ipv6Only: false\n}\n```\n\n#### `socket.udx`\n\nIt's the UDX instance from where the socket was created.\n\n#### `socket.streams`\n\nIt's a `Set` that tracks active streams (connected to the socket but not closed).\n\n#### `socket.userData`\n\nOptional custom userData. Default is `null`.\n\n#### `socket.bound`\n\nIndicates if it's bound to any port. It will be `true` after a successful `bind()`.\n\n#### `socket.closing`\n\nIt will be `true` after `close()` is called.\n\n#### `socket.idle`\n\nIndicates that the socket doesn't have any connected stream.\n\n#### `socket.busy`\n\nIndicates that the socket have at least one connected stream.\n\n#### `socket.address()`\n\nReturns an object like `{ host, family, port }`. Only available after `bind()`.\n\n#### `socket.bind([port], [host])`\n\nThe default port is `0`.\\\nIf no host specified: it binds to IPv6 `::`. If fails then IPv4 `0.0.0.0`.\n\n#### `await socket.close()`\n\nIt unbinds the socket so it stops listening for messages.\n\n#### `socket.setTTL(ttl)`\n\nSets the amount of times that a packet is allowed to be forwarded through each router or gateway before being discarded.\n\n#### `socket.getRecvBufferSize()`\n#### `socket.setRecvBufferSize()`\n\n#### `socket.getSendBufferSize()`\n#### `socket.setSendBufferSize()`\n\n#### `await socket.send(buffer, port, [host], [ttl])`\n\nSends a message to port and host destination. Default host is `127.0.0.1`.\n\n#### `socket.trySend(buffer, port, [host], [ttl])`\n\nSame behaviour as `send()` but no promise.\n\n#### `socket.on('message', (msg, from) => {})`\n\n`msg` is a buffer that containts the message.\\\n`from` is an object like `{ host, family, port }`.\n\n#### `socket.on('close', onclose)`\n\nEmitted if the socket was ever bound and it got closed.\n\n#### `socket.on('idle', onidle)`\n\nEmitted if the socket becomes idle (no active streams).\n\n#### `socket.on('busy', onbusy)`\n\nEmitted if the socket becomes busy (at least one active stream).\n\n#### `socket.on('listening', onlistening)`\n\nEmitted after a succesfull `bind()` call.\n\n## Streams\n\n#### `const stream = udx.createStream(id, [options])`\n\nCreates a new stream instance that is a Duplex stream.\n\nAvailable `options`:\n```js\n{\n  firewall: (socket, port, host) => true,\n  framed: false,\n  seq: 0\n}\n```\n\n#### `stream.udx`\n\nIt's the UDX instance from where the stream was created.\n\n#### `stream.socket`\n\nRefers to the socket that is connected to. Setted when you `connect()` the stream.\n\n#### `stream.id`\n\nCustom stream id.\n\n#### `stream.remoteId`\n\nRemote stream id. Setted when you `connect()` the stream.\n\n#### `stream.remoteId`\n\nRemote stream id. Setted when you `connect()` the stream.\n\n#### `stream.remoteHost`\n\nRemote host. Setted when you `connect()` the stream.\n\n#### `stream.remoteFamily`\n\nRemote family (`4` or `6`). Setted when you `connect()` the stream.\n\n#### `stream.remotePort`\n\nRemote port. Setted when you `connect()` the stream.\n\n#### `stream.userData`\n\nOptional custom userData. Default is `null`.\n\n#### `stream.connected`\n\nIndicates if the stream is connected to a socket. It becomes `false` if the stream is closed.\n\n#### `stream.mtu`\n\nIndicates the maximum size of each packet.\n\n#### `stream.rtt`\n#### `stream.cwnd`\n#### `stream.inflight`\n\n#### `stream.localHost`\n\nIndicates the connected socket host address. By default `null` if not connected.\n\n#### `stream.localFamily`\n\nIndicates the connected socket family address (`4` o `6`). By default `0` if not connected.\n\n#### `stream.localPort`\n\nIndicates the connected socket port. By default `0` if not connected.\n\n#### `stream.setInteractive(bool)`\n\n#### `stream.connect(socket, remoteId, port, [host], [options])`\n\nConnects the stream using a socket to a: remote stream id, and remote socket port/host.\n\nIf no host specified it uses `127.0.0.1` by default.\n\nAvailable `options`:\n```js\n{\n  ack\n}\n```\n\n#### `await stream.changeRemote(remoteId, port, [host])`\n\nChange the remote end of the stream.\n\nIf no host specified it uses `127.0.0.1` by default.\n\n#### `stream.relayTo(destination)`\n\nRelay stream to another stream.\n\n#### `await stream.send(buffer)`\n\nSend a message to another stream. Returns a promise.\n\n#### `stream.trySend(buffer)`\n\nSend a message to another stream.\n\n#### `const drained = await stream.flush()`\n\nWait for pending stream writes to have been explicitly acknowledged by the other side of the connection.\n\n#### `stream.on('connect', onconnect)`\n\nEmitted after the stream is connected to a socket.\n\n#### `stream.on('message', onmessage)`\n\nEmitted if the stream receives a message.\n\n#### `stream.on('remote-changed', onremotechanged)`\n\nEmitted when the remote end of the stream changes.\n\n#### `stream.on('mtu-exceeded', onmtuexceeded)`\n\nEmitted only once if you write data that exceeds the MTU.\n\n## Network interfaces\n\n#### `const interfaces = udx.networkInterfaces()`\n\nReturns an array of network interfaces, for example:\n```js\n[\n  { name: 'lo', host: '127.0.0.1', family: 4, internal: true },\n  { name: 'enp4s0', host: '192.168.0.20', family: 4, internal: false },\n  { name: 'lo', host: '::1', family: 6, internal: true },\n  { name: 'enp4s0', host: 'df08::c8df:bf61:95c1:352b', family: 6, internal: false }\n]\n```\n\n#### `const watcher = udx.watchNetworkInterfaces([onchange])`\n\nListens to changes in the network interfaces. The `watcher` object is iterable.\n\n#### `watcher.interfaces`\n\nArray of network interfaces.\n\n#### `watcher.watch()`\n\nStarts watching for changes. By default it already does it. This is only useful after you `unwatch()`.\n\n#### `watcher.unwatch()`\n\nStops watching for changes.\n\n#### `await watcher.destroy()`\n\nCloses the watcher.\n\n#### `watcher.on('change', onchange)`\n\nEmitted after a network interface change.\n\n#### `watcher.on('close', onclose)`\n\nEmitted after the watcher is closed.\n\n## DNS\n\n#### `const address = await udx.lookup(host, [options])`\n\nIt does a DNS lookup for the IP address. Returns `{ host, family }`.\n\nAvailable `options`:\n```js\n{\n  family: 0 // => 0, 4 or 6\n}\n```\n\n## License\n\nApache-2.0\n","publish_time":1651582721336,"maintainers":[{"name":"mafintosh","email":"mathiasbuus@gmail.com"},{"name":"kasperisager","email":"kasperisager@hey.com"}],"_cnpm_publish_time":1651582721336,"dist-tags":{"latest":"1.22.1","next":"1.20.6"}}