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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@sprawdzany/kaviar

v1.2.1

Published

Cloudflare KV cache helper with SWR, optional gzip compression, AES-GCM encryption, and tag invalidation.

Downloads

294

Readme

kaviar

npm version License: MIT

Small Cloudflare KV cache helper for Workers and Pages Functions: TTL, optional stale-while-revalidate (SWR), optional gzip, optional AES-GCM encryption, tag invalidation, and optional schema validation (e.g. Zod).

Open source under the MIT license. Install from npm; source is whatever Git host you use.

Deep dive (payload format, TTL/SWR timeline, security, tags): how.md.

When to use it

  • Cache expensive upstream calls (APIs, origins) in KV with a simple API.
  • Serve fast responses while refreshing in the background after the fresh window (SWR + ctx.waitUntil).
  • Invalidate groups of keys (e.g. user:123) via tags.
  • Optional compression for large JSON; optional encryption for sensitive blobs at rest in KV.

Not an HTTP cache (no Cache-Control / Cache API). Not a general database; use KV for what KV is good at.

Install

npm install @sprawdzany/kaviar

Optional peer: zod (only if you use schema with Zod).

Development

From the package directory (after cloning from your Git host):

npm install
npm test
npm run build

Requires Node 20+ for tests (Web Crypto + Compression Streams).

Bind KV in Wrangler

Add a namespace and wire it to your Worker:

[[kv_namespaces]]
binding = "MY_CACHE"
id = "<your-kv-namespace-id>"
interface Env {
  MY_CACHE: KVNamespace;
}
import { kaviar } from "@sprawdzany/kaviar";

export default {
  async fetch(request, env, ctx) {
    const data = await kaviar(
      env.MY_CACHE,
      ["profile", userId],
      async () => fetchUpstreamJson(userId),
      { ttl: 120, swr: 600, ctx }
    );
    return Response.json(data);
  },
};

Features (quick reference)

| Feature | What it does | |--------|----------------| | TTL | Fresh window for SWR (exceedsSwrAt). | | SWR | After fresh, serve stale while refreshing in background if ctx.waitUntil is set. | | compress | Gzip JSON before optional encryption (saves KV space). | | encrypt | AES-GCM; same key required on read. | | tags + invalidate | Bulk delete by tag. | | schema | parse() after JSON (e.g. Zod). | | onRevalidateError | SWR failures (not swallowed). | | Missing kv | Skips cache; always runs fetcher (useful for local dev without a binding). |

Examples

Tags

import { kaviar, invalidate } from "@sprawdzany/kaviar";

await kaviar(env.KV, ["user", id], fetchUser, { tags: [`user:${id}`, "users"] });
await invalidate(env.KV, `user:${id}`);

Compression + encryption

Order is always JSON → gzip → encrypt (see how.md).

await kaviar(env.KV, "big", fetchBig, {
  ttl: 300,
  compress: true,
  encrypt: { key: env.SECRET_KEY },
});

Zod

import { z } from "zod";

const UserSchema = z.object({ id: z.number(), name: z.string() });

const data = await kaviar(env.KV, "profile", fetchProfile, {
  schema: UserSchema,
});

SWR errors

await kaviar(env.KV, key, fetcher, {
  ttl: 60,
  swr: 120,
  ctx,
  onRevalidateError: (err) => console.error("SWR failed", err),
});

API

  • kaviar(kv, key, fetcher, options?)key can be a string or JSON-serializable value (hashed with SHA-256).
  • invalidate(kv, tag) — deletes all keys registered under tag.
  • hashKey(key) — same hex string as the internal KV key (debugging).

Options: ttl, swr, ctx, tags, compress, encrypt, schema, onRevalidateError.

Publish to npm

Scoped package @sprawdzany/kaviar.

  1. Bump version in package.json (or npm version patch|minor|major).
  2. npm run build and npm test (also run automatically via prepublishOnly on publish).
  3. npm login to npmjs.com (once per machine).
  4. npm publish --access public (first publish for a scoped public package; later often just npm publish).
  5. Confirm on npmjs.com/package/@sprawdzany/kaviar.

With 2FA, you may need npm publish --otp=<code> or an automation token in CI.

CI (optional)

Use a GitHub Actions workflow with an npm_token secret, or your provider’s equivalent, and npm publish against https://registry.npmjs.org/.

Contributing

Contributions welcome. Run npm test before opening a pull request.

License

MIT