Enriched UDP Socket and Client with possibility to encrypt data and send big messages.
- Fast — small overhead above UDP to send messages
- Secure — built-in encryption for sensitive logs
- Universal – built-in fragmentation to send big messages
- Simple — used well-known Node streams to manipulate and move data
- ESM and CJS
npm i --save e-socket-udp
//client.js
import { UDPClient } from 'e-socket-udp'
const secret = '11223344556677889900aabbccddeeff11223344556677889900aabbccddeeff'
const client = new UDPClient({ encryption: secret })
client.write(Buffer.from('Hello, World!', 'utf8'))
//server.js
import { UDPSocket } from 'e-socket-udp'
const secret = '11223344556677889900aabbccddeeff11223344556677889900aabbccddeeff'
const socket = new UDPSocket({ decryption: secret })
for await (const message of socket) {
console.log(message)
}
After just start the server node server.js
and client node app.js
. That's all and everything works.
Extends Basic UDPClient.
options
<object>
– optionaltype
<'udp4' | 'udp6'>
– optional. Inherited. Default'udp4'
port
<string | number>
– optional. Inherited. Default44302
address
<string>
– optional. Inherited. Default'127.0.0.1'
or'::1'
encryption
<string> | <(payload: Buffer) => Buffer>
– optional. Defaultundefined
- if passed
string
- will be appliedaes-256-ctr
encryption with passed string as a secret, so it should be64char
long; - if passed
function
- this function will be used to encrypt every message; - if passed
undefined
- will not use any kind of encryption.
- if passed
fragmentation
<boolean>
– optional. Automatically split each message into chunks. Useful with any size of messages (especially big ones). Defaulttrue
packetSize
<number>
– optional. Used iffragmentation = true
. Number of bytes in each packet (chunk). Default1280
write (buffer)
:<boolean>
— Inherited.
origin
:<dgram.Socket>
— Inherited.port
:<number>
— Inherited.address
:<string>
— Inherited.family
:<string>
— Inherited.
'ready'
:<void>
– Inherited. Emitted when the client "establishes" udp connection.
import { UDPClient } from 'e-socket-udp'
const client = new UDPClient({
port: 44302,
// encryption: (buf) => buf.map(byte => byte ^ 83)
encryption: '11223344556677889900aabbccddeeff11223344556677889900aabbccddeeff'
})
client.write(Buffer.from('Hello, world!', 'utf8'))
Extends Basic UDPSocket
It is a UDP socket in readable stream
form with encoding and possibility to handle big messages.
options
<object>
– requiredtype
<'udp4' | 'udp6'>
– optional. Inherited. Default'udp4'
port
<string | number>
– optional. Inherited. Default44302
host
<string>
– optional. Inherited. Default'127.0.0.1'
or'::1'
decryption
<string> | <(payload: Buffer) => Buffer>
– optional. Defaultundefined
- if passed
string
- will be appliedaes-256-ctr
decryption with passed string as a secret, so it should be64char
long; - if passed
function
- will be used that function to decrypt every message; - if passed
undefined
- will not use any kind of decryption.
- if passed
fragmentation
<boolean>
– optional. Combine chunks into message. Useful with any size of messages (especially big ones). Defaulttrue
gcIntervalTime
<number>
— optional. How often instance will check internal buffer to delete expired messages (in ms). Default5000
gcExpirationTime
<number>
— optional. How long chunks can await all missing chunks in internal buffer (in ms). Default10000
origin
:<dgram.Socket>
— Inherited.port
:<number>
— Inherited.address
:<string>
— Inherited.family
:<string>
— Inherited.allowPush
:<boolean>
— Inherited.
All Readable
events of course and:
Emitted when socket started and ready to receive data.
Emitted when warning occurs.
message
<object | Error>
type
<Symbol>
id
<string>
– optionaldate
<Date>
– optional
A type might be:
Symbol<'missing_message'>
– when some messages didn't receive all chunks and got expired.Symbol<'decryption_message'>
– when some messages failed to be compiled from chunks.
import { UDPSocket } from 'e-socket-udp'
const socket = new UDPsocket({
port: 44302,
// decryption: (buf) => buf.map(byte => byte ^ 83) // not safe at all, but better than nothing
decryption: '11223344556677889900aabbccddeeff11223344556677889900aabbccddeeff'
})
for await (const message of socket) {
/*handle messages*/
}
import fs from 'node:fs'
import { UDPSocket } from 'e-socket-udp'
const secret = '11223344556677889900aabbccddeeff11223344556677889900aabbccddeeff'
const writer = fs.createWriteStream('/some/path')
const socket = new UDPSocket({ port: 44302, decryption: secret })
socket.pipe(writer)
<number>
:44302
<Symbol>
:Symbol<'missing_message'>
<Symbol>
:Symbol<'decryption_fail'>
There are _identifier
and _constants
exposed also, but they are used for internal needs. They could be removed in next releases, so it is not recommended to use it in your project.
License (MIT)