{"name":"udx-native","version":"1.7.2","keywords":["tcp","udp","stream","reliable"],"author":{"url":"@mafintosh","name":"Mathias Buus"},"license":"MIT","_id":"udx-native@1.7.2","homepage":"https://github.com/holepunchto/udx-native#readme","bugs":{"url":"https://github.com/holepunchto/udx-native/issues"},"dist":{"shasum":"8392a6c9525ca1df074ac60d2fa7301ad4b79459","tarball":"http://registry-npm.ywwl.com/udx-native/download/udx-native-1.7.2.tgz","fileCount":34,"integrity":"sha512-VfLxd6HN9QOU+acpbgzcEcK67pHHJnJUuEGp0W5a5TzsCQdXAVdwA8/cUqbXYKRPIXIlZ3rHMGH/qfxlqUH9SQ==","signatures":[{"sig":"MEUCICd69OqzA8ZayFUWiedXKUKNID7KXZgzvnwuV/vOTV9KAiEAz24ItWz55VfzKvCZNpusxmXNUXTygQJTDGeP6WCptMs=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":701141,"size":226101,"noattachment":false,"key":"/udx-native/-/udx-native-1.7.2.tgz"},"main":"lib/udx.js","gitHead":"48993e3aa2c6a6b78fdb60d346ea6c46ccf254ae","gypfile":true,"scripts":{"test":"standard && node test/all.js","bench":"brittle test/bench/*.js","install":"node-gyp-build","prebuild":"prebuildify --napi --strip","test:all":"standard && brittle test/*.js test/slow/*.js","test:generate":"brittle -r test/all.js test/*.js"},"_npmUser":{"name":"mafintosh","email":"mathiasbuus@gmail.com"},"repository":{"url":"git+https://github.com/holepunchto/udx-native.git","type":"git"},"_npmVersion":"9.5.1","description":"udx is reliable, multiplexed, and congestion-controlled streams over udp","directories":{},"_nodeVersion":"18.16.0","dependencies":{"b4a":"^1.5.0","events":"^3.3.0","streamx":"^2.14.0","napi-macros":"^2.0.0","node-gyp-build":"^4.4.0"},"_hasShrinkwrap":false,"devDependencies":{"is-ci":"^3.0.1","brittle":"^3.1.0","standard":"^16.0.4","prebuildify":"^5.0.0","tiny-byte-size":"^1.1.0"},"_npmOperationalInternal":{"tmp":"tmp/udx-native_1.7.2_1695826776050_0.8977682913985316","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":1695826776294,"maintainers":[{"name":"mafintosh","email":"mathiasbuus@gmail.com"},{"name":"kasperisager","email":"kasperisager@hey.com"}],"_cnpm_publish_time":1695826776294,"dist-tags":{"latest":"1.22.1","next":"1.20.6"}}