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

traffic-lens

v0.0.1

Published

A network traffic monitoring tool with proxy awareness for Node.js

Downloads

78

Readme

TrafficLens

CI npm version License: MIT

A Node.js library for monitoring network traffic with proxy connection awareness. TrafficLens provides real-time statistics about your system's network usage, distinguishing between proxied and direct traffic.

Features

  • Real-time network traffic monitoring
  • Proxy connection detection and statistics
  • Separate tracking for direct and proxied traffic
  • Human-readable traffic formatting
  • Event-based updates via subscription
  • TypeScript support

Installation

pnpm add traffic-lens

or

npm install traffic-lens

or

yarn add traffic-lens

Usage

Basic Example

import TrafficLens from "traffic-lens";
// Initialize with proxy port
const monitor = new TrafficLens({ proxyPort: 1080 });
// Subscribe to traffic updates
monitor.subscribe((stats) => {
  console.log("Proxy Traffic:", TrafficLens.formatSpeed(stats.proxied.uploadSpeed));
  console.log("Direct Traffic:", TrafficLens.formatSpeed(stats.direct.uploadSpeed));
});
// Start monitoring
await monitor.start();
// ... when done
monitor.stop();

Configuration

const monitor = new TrafficLens({
  proxyPort: 1080, // Required: Your proxy server port
  updateInterval: 5000, // Optional: Update interval in ms (default: 5000, cannot be less than 3000)
});

Traffic Statistics

The monitor provides detailed traffic statistics through various methods:

// Get all traffic stats
const stats = monitor.getTrafficStats();
console.log(stats);

/**
{
proxied: {
upload: number,
download: number,
uploadSpeed: number,
downloadSpeed: number,
activeConnections: number
},
direct: {
upload: number,
download: number,
uploadSpeed: number,
downloadSpeed: number
},
timestamp: number
}
**/

// Get proxy speeds only
const proxySpeed = monitor.getProxySpeed();
console.log(proxySpeed); // { uploadSpeed: number, downloadSpeed: number }
// Get direct traffic speeds only
const directSpeed = monitor.getDirectSpeed();
console.log(directSpeed); // { uploadSpeed: number, downloadSpeed: number }

Formatting Utilities

// Format bytes to human readable string
TrafficLens.formatBytes(1024); // "1.00 KB"
TrafficLens.formatBytes(1048576); // "1.00 MB"
// Format speed to human readable string
TrafficLens.formatSpeed(1024); // "1.00 KB/s"
TrafficLens.formatSpeed(1048576); // "1.00 MB/s"

Event Subscription

// Subscribe to updates
const unsubscribe = monitor.subscribe((stats) => {
  console.log("New traffic stats:", stats);
});
// Unsubscribe when done
unsubscribe();

API Reference

Class: TrafficLens

Constructor

  • new TrafficLens(config: Config): Creates a new traffic monitor instance

Methods

  • start(): Promise<void>: Starts monitoring network traffic
  • stop(): void: Stops monitoring network traffic
  • subscribe(callback: MonitorCallback): () => void: Subscribes to traffic updates
  • getTrafficStats(): TrafficStats: Gets current traffic statistics
  • getProxySpeed(): SpeedMetrics: Gets current proxy connection speeds
  • getDirectSpeed(): SpeedMetrics: Gets current direct connection speeds
  • static formatBytes(bytes: number): string: Formats bytes to human readable string
  • static formatSpeed(bytesPerSec: number): string: Formats speed to human readable string

Types

interface Config {
  proxyPort: number;
  updateInterval?: number; // milliseconds
}
interface TrafficStats {
  proxied: ProxyMetrics;
  direct: NetworkMetrics;
  timestamp: number;
}
interface NetworkMetrics {
  upload: number;
  download: number;
  uploadSpeed: number;
  downloadSpeed: number;
}
interface ProxyMetrics extends NetworkMetrics {
  activeConnections: number;
}

Requirements

  • Node.js 18.x or later
  • System with network interfaces

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT License - see the LICENSE file for details