
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
dgram-as-promised
Advanced tools
This module provides promisified version of the standard
dgram class. The API is the same as for
standard dgram
, except bind
, close
and send
methods which return
Promise
object.
This module requires ES2021 with Node >= 16.
npm install dgram-as-promised
Additionally for Typescript:
npm install -D @types/node
dgram-as-promised
can be used similarly to the standard dgram
module.
Example:
import DgramAsPromised from "dgram-as-promised"
const socket = DgramAsPromised.createSocket("udp4")
const MEMBERSHIP = "224.0.0.1"
const PORT = 41234
const message = Buffer.from("ABCDEFGH")
Method bind
returns Promise
object which resolves to address info when
listening
event is emitted.
const address = await socket.bind()
console.log(`Socket is listening on ${address.address}:${address.port}`)
socket.setBroadcast(true)
socket.setMulticastTTL(128)
socket.addMembership(MEMBERSHIP)
console.log("Membership is set")
Method send
returns Promise
object which is fulfilled when the message
has been sent.
const bytes = await socket.send(message, 0, message.length, PORT, MEMBERSHIP)
console.log(`Message is sent (${bytes} bytes)`)
Method recv
returns Promise
object which resolves to the object with msg
and rinfo
properties as from message
event or resolves to undefined
when
the socket is already closed.
It throws an error if a timeout occurs and it was set by setTimeout
method.
const packet = await socket.recv()
if (packet) {
console.log(`Received message: ${packet.msg.toString()}`)
console.log(`Received ${packet.rinfo.size} bytes`)
}
Method close
returns Promise
object which resolves when close
event is
emitted or the socket is already closed.
await socket.close()
console.log("Socket is closed")
socket = socket.setTimeout(ms)
Set the timeout used by recv
method for the idle socket and after this
timeout, the recv
method will reject a Promise with a timeout error.
After the error, the socket is not closed and calling another recv
method
will reset the timeout.
The method returns this object.
Example:
socket.setTimeout(1000)
await socket.recv()
Method iterate
and the socket object returns an asynchronous iterator which
will call recv
method until the socket is closed.
for await (const packet of socket) {
console.info(packet.msg.toString())
// Close socket if Ctrl-D is in the message
if (packet.msg.indexOf(4) !== -1) {
await socket.close()
}
}
Method destroy
cleans internal listeners.
socket = socket.destroy()
The method returns this object.
Copyright (c) 2016-2024 Piotr Roszatycki mailto:piotr.roszatycki@gmail.com
v6.0.0 2024-06-22
addMembership
, setBroadcast
, setTTL
, setMulticastTTL
,
setMulticastInterface
, setMulticastLoopback
, dropMembership
,
setRecvBufferSize
, setSendBufferSize
and destroy
return this
.address
method doesn't return string
anymore; closes:
dex4er/js-dgram-as-promised#51setTimeout
method sets timeout for recv
method; closes:
dex4er/js-dgram-as-promised#55FAQs
Promisify dgram module
We found that dgram-as-promised demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.