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

@ashnazg/dumbfile

v1.1.5

Published

a barebones localfile-backed storage for stubbing out without redis or RDMS

Downloads

5

Readme


title: "@ashnazg/dumbfile" sidebar_label: dumbfile

a barebones localfile-backed storage for stubbing out without redis or RDMS

read/write JSON

N.B. Don't put any cycles in your .db; this is just using JSON.stringify, so cycles will make it unsavable.

const jsonLoad = require('@ashnazg/dumbfile').load; // ~/projects/ashnazg-npm/dumbfile/json.js

var handle = await jsonLoad('./store.json');
console.log(handle.db); // contents of file are now ready on .db

handle.db.property = 'foo'; // mutate .db, it's just a plain old JS object.
await handle.save(); // saved back to disk, in a human-friendly format. Using tabs.

// admin edited config file while app was running?
await handle.reload(); // .db has been wiped and replaced with file's contents again.

Example: persisting session tokens through a server reboot

var seven_days = 1000*60*60*24*7;

var session_db_ready = dumb.load('../tokens.json').then(sys => { // N.B. if you've got src/ watched with something like nodemon, make sure the state file isn't in the watch list.
	// create schemas if missing
	sys.db = Object.assign({
		tokens: {},
	}, sys.db);
	sys.setSessionExpiration = function(user, lifespan = seven_days) {
		var token = genToken(user, lifespan);
		var expires_on = Date.now() + lifespan;
		this.db.tokens[user] = this.db.tokens[user] || {}; // this could be the first token creation for this user
		this.db.tokens[user][token] = expires_on;
		this.save(); // firing and forgetting the 'save completed' promise; I don't need confirmation that this hit disk for just sessions.
		console.log("new db", this.db);
		return token;
	};
	sys.isValidSession = function(user, token) {
		var my_tokens = this.db.tokens[user];
		if (!my_tokens) return false;
		var expiration_date = my_tokens[token];
		if (expiration_date === undefined) return false;
		if (expiration_date > Date.now()) return getUid(user);
		delete my_tokens[token];
		return false;
	};
	return sys;
}

// caller should provide exactly one of pass or token
async function checkAuth(user, pass, token) {
	if (pass) {
		var uid = checkPass(user, pass);
		if (uid) {
			var sys = await session_db_ready;
			var token = sys.setSessionExpiration(user);
			return {uid, token};
		}
	} else if (token) {
		var sys = await session_db_ready;
		uid = sys.isValidSession(user, token);
		if (uid) {
			return {uid, token};
		}
		throw {message: 'token expired'};
	}
	throw {message: 'no acceptable credential provided'};
}

Not using JSON?

There's a no-format-assumptions underlying layer you can use. It does speak only utf8, as I only use raw Buffers when I have to.

var loadFile = require('./utf8.js').createDumbFile;

var handle = loadFile('./store.raw');
var data = await handle.load();
await handle.save('new data');

It also has a synchronous save function:

handle.saveSync('new data');

handling not-yet-existing files

Both of the handle creation functions have two more params: a default value that's returned when the file doesn't exist, and a chmod mode used during save()'s file creation.

var handle = loadFile(filename, 'default config lines', 0o660); // default chmod os owner r/w only; 660 says group also has r/w perms.
var handle = jsonLoad(filename, {default: 'setting'}, 0o660);

Release 1.1.4

Turned off some dbg logging I didn't mean to commit

Release 1.1.3

json file management no longer depends on {this.db} for anything -- this allows me to pass a subtree to a submodule, along with the save func, and now each submodule can share dumb state file without knowing they're not the root.

Release 1.1.2

Now treats empty files the same way it treats non-existent files: init the state to the specified defaults.

Release 1.1.1

1.1.0 has a bug where multiple parallel calls to async save can result in the OS seeing overlapping writes during one "open for truncate and write" file handle.

So now, a horde of async save()s results in all but the first save leaving a note for the first save job to handle it, and then waiting for it to finally give the "latest version has been persisted."