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

with-file-cache

v3.3.0

Published

Add filesystem-based cache to functions

Downloads

59

Readme

Filesystem-based cache to add to functions

Make a function cached to the filesystem and persist results even between restarts. Supports waiting for in-progress results and works across workers.

Installation

npm install with-file-cache

Usage

import {withFileCache} from "with-file-cache";

// initialize the cache
const addFileCache = withFileCache({baseKey: () => ""});

const fn = addFileCache(async (name) => {
	console.log("Called fn with " + name);
	return "Hello " + name + "!";
}, {calcCacheKey: (arg) => arg});

await fn("Bob");
// called fn with Bob
await fn("Bob");
await fn("Joe");
// called fn with Joe

Add the package-lock.json to the cache key

Usually, when dependencies change you want to invalidate all caches. To do this, use the baseKey configuration of the withFileCache:

import fs from "node:fs/promises";
import crypto from  "node:crypto";

const sha = (x) => crypto.createHash("sha256").update(x).digest("hex");

export const addFileCache = withFileCache(
	{baseKey: async () => {
		return sha(await fs.readFile("package-lock.json", "utf-8"));
	}},
);

Configure the cache key

Use the calcCacheKey function to define how the cache key is calculated. When a cache key is already found in the cache then the function won't be called.

Supports simple values, arrays, and functions.

Serialize/deserialize

Simple JS values and Buffers are automatically serialized to the file, but sometimes the function's result can't be written easily. In that case, use the serialize/deserialize to configure how to convert to and from a Buffer.

const getStyle = addFileCache(async (assetsOutDir) => {
	// ...
	return {style: rewritten, assetFiles};
}, {calcCacheKey: async (assetsOutDir) => {
	const files = await glob("**/*.*", {cwd: path.join(__dirname, "_assets", "stylesheets"), stat: true, nodir: true, withFileTypes: true});
	return ["getStyle", assetsOutDir, ...files.map((file) => ({path: file.fullpath(), mtime: file.mtime.getTime()})).sort((a, b) => a.path > b.path ? 1 : -1).map((o) => JSON.stringify(o))];
}, serialize: async ({style, assetFiles}) => {
	const zipBlobWriter = new zip.BlobWriter();
	const zipWriter = new zip.ZipWriter(zipBlobWriter);
	zipWriter.add("__index", new zip.TextReader(JSON.stringify({style, assetFiles: assetFiles.map(({name}) => name)})));
	await assetFiles.reduce(async (memo, {name, getContents}) => {
		await memo;
		await zipWriter.add(name, new zip.Uint8ArrayReader(new Uint8Array(await getContents())))
	}, Promise.resolve());

	await zipWriter.close();
	return Buffer.from(await (await zipBlobWriter.getData()).arrayBuffer());
}, deserialize: async (file) => {
	const zipReader = new zip.ZipReader(new zip.Uint8ArrayReader(new Uint8Array(file)));
	const entries = await zipReader.getEntries();
	const {style, assetFiles} = JSON.parse(await (entries.find(({filename}) => filename === "__index").getData(new zip.TextWriter())));

	return {
		style,
		assetFiles: assetFiles.map((name) => {
			return {
				name,
				getContents: async () => {
					return Buffer.from(await (await entries.find(({filename}) => filename === name).getData(new zip.BlobWriter())).arrayBuffer())
				}
			}
		})
	}
}});