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

@virtualstate/internal

v1.0.0-alpha.31

Published

[//]: # (badges)

Downloads

43

Readme

@virtualstate/internal

Support

Node.js supported Bun supported

Test Coverage

Usage

Service Worker API

worker.js

self.addEventListener("fetch", event => {
    console.log(event.request.method, event.request.url);
    event.respondWith(new Response("Hello"))
});

main.js

import { serviceWorker, createServiceWorkerFetch } from "@virtualstate/internal";
import { fileURLToPath } from "node:url";
import { dirname, join } from "node:path";

const pathname = fileURLToPath(import.meta.url);
const worker = join(dirname(pathname), "./worker.js");

const registration = await serviceWorker.register(worker);

const fetch = createServiceWorkerFetch(registration);

const response = await fetch("/");
const text = await response.text();

console.log(response.status, text); // 200 "Hello";
REDIS_MEMORY=1 node main.js

CacheStorage

cache.js

import { caches } from "@virtualstate/internal";

const cache = await caches.open("cache");

const url = "https://example.com";

await cache.add(url);

const response = await cache.match(url);
const text = await response.text();

console.log(response.status, text.substring(0, 15), text.length); // 200 "<!doctype html>" 1256;

ContentIndex

index.js

import { index, caches } from "@virtualstate/internal";

const entry = {
    id: "post-1",
    url: "/posts/amet.html",
    title: "Amet consectetur adipisicing",
    description:
        "Repellat et quia iste possimus ducimus aliquid a aut eaque nostrum.",
    icons: [
        {
            src: "https://javascript.org.nz/logo.png",
            sizes: "200x200",
            type: "image/png",
        },
    ],
    category: "article",
};
await index.add(entry);

console.log(await index.getAll()) // [{ id: "post-1" }]

const cache = await caches.open("contentIndex");

for (const { src } of entry.icons) {
    const response = await cache.match(src);
    const { byteLength } = await response.arrayBuffer();
    console.log(src, response.status, byteLength) // ... 200 5348
}

Background Sync API

sync.js

import { addEventListener, sync, caches, index, dispatchEvent } from "@virtualstate/internal";

addEventListener("sync", ({ tag, waitUntil }) => {
    if (tag === "images") {
        waitUntil(onSyncImages());
    }

    async function onSyncImages() {
        const cache = await caches.open("contentIndex");
        for (const { id, icons } of await index.getAll()) {
            for (const { src } of icons) {
                console.log(`Updating icon "${src}" for ${id}`);
                await cache.put(
                    src,
                    await fetch(src)
                );
            }
        }
    }
});

await sync.register("images");

// Ran elsewhere by scheduler
// Is usually managed by generateVirtualSyncEvents 
await dispatchEvent({
    type: "sync",
    tag: "images",
    schedule: {
        immediate: true
    }
});

Periodic Sync API

periodic-sync.js

import { addEventListener, periodicSync, caches, index, dispatchEvent } from "@virtualstate/internal";

addEventListener("periodicsync", ({ tag, waitUntil }) => {
    if (tag === "images") {
        waitUntil(onSyncImages());
    }

    async function onSyncImages() {
        const cache = await caches.open("contentIndex");
        for (const { id, icons } of await index.getAll()) {
            for (const { src } of icons) {
                console.log(`Updating icon "${src}" for ${id}`);
                await cache.put(
                    src,
                    await fetch(src)
                );
            }
        }
    }
});

await periodicSync.register("images", {
    minInterval: 5 * 60 * 1000 // Refresh every 5 minutes
});

// Ran elsewhere by scheduler
// Is usually managed by generatePeriodicSyncVirtualEvents
await dispatchEvent({
    type: "periodicsync",
    tag: "images",
    schedule: {
        immediate: true
        // can give delay here or cron
        // minInterval doesn't always mean a fixed rate
        //
        // delay: 5 * 60 * 1000,
        // repeat: true
    }
});

Events

import { addEventListener, dispatchEvent } from "@virtualstate/internal";

addEventListener("bingpop", () => {
    console.log("Received bingpop event");
});

dispatchEvent({
    type: "bingpop"
});

Schedules

schedule.immediate

Use this to have the event be dispatched immediately

dispatchEvent({
    type: "bingpop",
    schedule: {
        immediate: true
    }
});

schedule.delay

Use this to have the event be dispatched after a period of time

dispatchEvent({
    type: "bingpop",
    schedule: {
        delay: 1000
    }
});
dispatchEvent({
    type: "bingpop",
    schedule: {
        delay: "1h"
    }
});

schedule.cron

Use this to have the event be dispatched according to a cron schedule

dispatchEvent({
    type: "bingpop",
    schedule: {
        // Triggers at 5am each day
        cron: "0 5 * * *"
    }
});