Licence
MIT
Version
0.5.0
Deps
4
Size
33 kB
Vulns
0
Weekly
0
@webvpn/dgram
Node's dgram module for a web page, with every UDP socket on a WebVPN relay through a @webvpn/client session.
Code written for Node's dgram, such as a DHT or a tracker client, runs in the page as it is.
Use
npm install @webvpn/client @webvpn/dgram
import { connect as openSession } from '@webvpn/client'
import dgram, { setProvider } from '@webvpn/dgram'
// the session opens at the first socket; a session already open is taken too
setProvider(() => openSession({ url: 'https://relay.example:4433', webSocketUrl: 'wss://relay.example:8443' }))
const socket = dgram.createSocket('udp4')
socket.on('message', (message, remote) => console.log(remote.address, message.toString()))
socket.send('ping', 6881, 'router.example')
createDgram(session) builds a copy of the module of its own, on that session alone.
Alias dgram to it
With Vite:
export default {
resolve: { alias: { dgram: '@webvpn/dgram', 'node:dgram': '@webvpn/dgram' } },
}
webpack and esbuild have the same setting, resolve.alias and alias. The package brings the browser builds of
buffer and events it needs, so the page needs no Node polyfills for it.
What it does not have
- The
reuseAddr,ipv6Only, buffer size,lookupandsignaloptions ofcreateSocket. setMulticastInterfaceand the source specific memberships, which do nothing: the relay has no option for them.- Host names are looked up through the relay (A records for
udp4, AAAA forudp6), which needs itsDNSfeature. closeis emitted once the relay closed the socket. The relay closing a socket on its own, or the session ending, emitserrorwhen there is a listener for it, thenclose.- A socket option set before
bind, or one the relay refuses, emitserror. The send queue getters answer 0, and the buffer size getters the last value set.