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

megumu

v0.0.1

Published

A simple way to monitor your os

Downloads

3

Readme

The name "megumu" is from Iizunamaru Megumu(飯綱丸 龍), a character from Touhou.

Installation

npm install megumu --save

Usage

Simple example:

import { cpu, memory } from "megumu";

// Get CPU usage
cpu.addEventListener(
  {
    type: "TICK",
  },
  (data) => {
    console.log(data);
  },
  1000
);

cpu.startSampling();

// Get memory usage
memory.addEventListener(
  {
    type: "GE",
    target: "OS",
    value: "2GB",
    duration: 10000,
  },
  (data) => {
    // This callback is triggered only when the system memory usage is greater than 2GB and lasts for 10s
    console.log(data);
  },
  1000
);

memory.startSampling();

API

Public API

Both cpu and memory have the same public API below:

addEventListener
function addEventListener(
  meta: CpuMeta | MemoryMeta,
  callback: Function,
  interval: number
): () => void;

Add an event listener to the monitor, and return a function to remove the listener.

  • meta: The meta data of the event listener, the specific type will be detailed below.

  • callback: The callback function to be triggered when the event is fired, the specific type will be detailed below.

  • interval: The interval of the monitor.

  • return: A function to remove the listener.

startSampling
function startSampling(): void;

Start the monitor.

stopSampling
function stopSampling(): void;

Stop the monitor.

destroy
function destroy(): void;

Stop the monitor and remove all event listeners.

CPU

type CpuTickMeta = {
  type: "TICK"; // This type of meta will trigger the callback function every `interval`
  sampleInterval?: number; // The sample interval of the cpu usage, need to be less than the `interval`
};

type CpuBasicMeta = {
  type: "LE" | "GE"; // Whether the cpu usage is less than or greater than the value
  duration: number; // Only trigger the callback function when the cpu usage lasts for `duration`
  value: number | string; // e.g. 0.8, "80%"
  sampleInterval?: number; // The sample interval of the cpu usage, need to be less than the `interval`
};

type CpuMeta = CpuBasicMeta | CpuTickMeta;
Callback
function addEventListener(
  meta: CpuMeta,
  callback: (meta: CpuInfo) => void,
  interval: number
): () => void;

Memory

type MemoryTarget =
  | "OS" // The total memory of the operating system
  | "RSS"
  | "HEAP_TOTAL"
  | "HEAP_USED"
  | "EXTERNAL"
  | "ARRAY_BUFFER";

type MemoryTickMeta = {
  target: MemoryTarget; // The target of the memory monitor
  type: "TICK"; // This type of meta will trigger the callback function every `interval`
};

type MemoryBasicMeta = {
  target: MemoryTarget; // The target of the memory monitor
  type: "LE" | "GE"; // Whether the memory usage is less than or greater than the value
  duration: number; // Only trigger the callback function when the memory usage lasts for `duration`
  value: number | string; // e.g. 0.8(only for OS type), "80%"(only for OS type), "2GB", "200MB"
};

type MemoryMeta = MemoryBasicMeta | MemoryTickMeta;
Callback
type MemoryCallback<T> = T extends { target: "OS" }
  ? (memory: { total: number; free: number }) => void
  : (memory: number) => void;

function addEventListener<T extends MemoryMeta>(
  meta: T,
  callback: MemoryCallback<T>,
  interval: number
): () => void;

License

MIT