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

better-port

v1.1.1

Published

A basic wrapper around things like SerialPort or UDP to provide a constant connection

Downloads

75

Readme

Better Port

This is a basic wrapper for ports that adds some extra functionality to provide a constant connection that i feel is lacking.

This project originally started as a wrapper called Better-Serial-Port for Node Serial Port to provide a constant connection but is now written to support different protocols such as UDP, thus a rename to Better Port.

Features

  • The port is monitored. If it disconnects it will emit the close event and when it is detected again it will automatically reconnect and emit the open event
  • Can auto reopen the port on error
  • If there is no data for a specific period of time it will assume the port disconnected

How to use

Currently this project supports the following ports:

Node Serial Port (docs)

//Create the port
const BetterPort = require("./dist/index.js");
const BetterPortEvent = BetterPort.BetterPortEvent;
var port = new BetterPort.BetterSerialPort({
    path: "",
    baudRate: 912600,
    keepOpen: true,
    autoOpen: true
    //Any extra open options for serial port
});

//Setup our events
port.on(BetterPortEvent.open, () => {
    console.log("Port opened");
});
port.on(BetterPortEvent.close, () => {
    console.log("Port closed");
});
port.on(BetterPortEvent.error, (err) => {
    console.log("Port error: ", err);
});
port.on(BetterPortEvent.data, (data) => {
    console.log("Port data: " + data.toString());
});

//Open the port
port.openPort().then(() => {
    console.log("Port created!");
}).catch((err) => {
    console.log("Port open error: ", err);
});

UDP (dgram)

const BetterPort = require("./dist/index.js");
const BetterPortEvent = BetterPort.BetterPortEvent;

//Open a UDP client on port 7000
var port = new BetterPort.BetterUDPPort({
    recPort: 7000,
    keepOpen: true,
    autoOpen: true
    //Any extra open options for dram
});

//Setup our events
port.on(BetterPortEvent.open, () => {
    console.log("Port opened");
});
port.on(BetterPortEvent.close, () => {
    console.log("Port closed");
});
port.on(BetterPortEvent.error, (err) => {
    console.log("Port error: ", err);
});
port.on(BetterPortEvent.data, (data) => {
    console.log("Port data: " + data.toString());
});

//Open the port
port.openPort().then(() => {
    console.log("Port created!");
}).catch((err) => {
    console.log("Port open error: ", err);
});

TCP (net)

const BetterPort = require("./dist/index.js");
const BetterPortEvent = BetterPort.BetterPortEvent;

//Open a TCP client on port 7000
var port = new BetterPort.BetterTCPPort({
    host: "192.168.0.1",
    port: 5000,
    keepOpen: true,
    autoOpen: true
    //Any extra open options for net
});

//Setup our events
port.on(BetterPortEvent.open, () => {
    console.log("Port opened");
});
port.on(BetterPortEvent.close, () => {
    console.log("Port closed");
});
port.on(BetterPortEvent.error, (err) => {
    console.log("Port error: ", err);
});
port.on(BetterPortEvent.data, (data) => {
    console.log("Port data: " + data.toString());
});

//Open the port
port.openPort().then(() => {
    console.log("Port created!");
}).catch((err) => {
    console.log("Port open error: ", err);
});

This project does add some extra functionality to these as well:

Extra methods

portExists(): Promise<boolean> //Does the port exist
portOpen(): boolean //Is the port currently open
openPort(keepOpen?: boolean): Promise<void> //Will open the port
closePort(keepClosed: boolean = false, disconnectError?: Error): Promise<void> //Will close the port and attempt reopen if keepClosed is not set to true
portOpen(): boolean //If the port is currently open

Extra Options

  autoOpen?: boolean; //Should the port be opened automatically on creation
  keepOpen?: boolean; //Should we keep the port open
  closeOnNoData?: boolean; //Should we close the port if no data is received
  disconnectTimeoutMS?: number | undefined; //How long should we wait before disconnecting on no data
  sendWhenOpened?: Buffer | undefined; //Data to send when the port opened
  reconnectTimeoutMS?: number | undefined; //How long should we wait before reconnecting. Default 1000md
  connectionAttemptTimeoutMS?: number | undefined; //How long should we wait between failed connection attempts. Default 5000ms

Overridden methods

The following methods cab be used normally but are replaced by this project

write(chunk: any, encoding?: BufferEncoding, cb?: (error: Error | null | undefined) => void): boolean
write(chunk: any, cb?: (error: Error | null | undefined) => void): boolean
write(data: any, encoding?: any, callback?: any): boolean
flush()
pipe<T extends NodeJS.WritableStream>(destination: T, options?: { end?: boolean; }): T