npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dgram-as-promised

v5.0.1

Published

Promisify dgram module

Downloads

569

Readme

dgram-as-promised

Build Status Coverage Status npm

This module provides promisified version of standard dgram class. The API is the same as for standard dgram, except bind, close and send methods which return Promise object.

Requirements

This module requires ES6 with Node >= 10.

Installation

npm install dgram-as-promised

Additionally for Typescript:

npm install -D @types/node

Usage

dgram-as-promised can be used similar to standard dgram module.

Example:

const {DgramAsPromised} = require("dgram-as-promised")

const socket = DgramAsPromised.createSocket("udp4")

const MEMBERSHIP = "224.0.0.1"
const PORT = 41234

const message = Buffer.from("ABCDEFGH")

Typescript:

import DgramAsPromised from "dgram-as-promised"
// or
import {DgramAsPromised} from "dgram-as-promised"

const socket = DgramAsPromised.createSocket("udp4")

bind

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")

send

Method send returns Promise object which is fulfilled when message has been sent.

const bytes = await socket.send(message, 0, message.length, PORT, MEMBERSHIP)
console.log(`Message is sent (${bytes} bytes)`)

recv

Method recv returns Promise object which resolves to the object with msg and rinfo properties as from message event or resolves to undefined when socket is already closed.

const packet = await socket.recv()
if (packet) {
  console.log(`Received message: ${packet.msg.toString()}`)
  console.log(`Received ${packet.rinfo.size} bytes`)
}

close

Method close returns Promise object which resolves when close event is emitted.

await socket.close()
console.log("Socket is closed")

iterate

Method iterate and the socket object return asynchronous iterator which will call recv method until 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()
  }
}

destroy

Method destroy cleans internal listeners.

socket.destroy()

License

Copyright (c) 2016-2020 Piotr Roszatycki mailto:[email protected]

MIT