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

coggers-session

v1.3.0

Published

Session middleware for Coggers

Downloads

9

Readme

coggers-session

Coggers-session is a secure session middleware for Coggers

Example

import { Coggers } from "coggers";
import session from "coggers-session";
const coggers = new Coggers({
	$: [
		session({
			password: "secure_password_above_32_characters_do_not_hardcode_this",
		}),
	],
	$get(req, res) {
		const count = req.session.count;
		if (count) req.session.count++;
		else req.session.count = 1;
		res.saveSession();
		res.send(`You've refreshed ${count} times!`);
	},
});

coggers
	.listen(8080)
	.then(() => console.log("Listening at http://localhost:8080/"));

session(options)

Used to get an initialized middleware for coggers. The options object contains:

  • password: string | Buffer | Array<string | Buffer> This is used for encrypting the session so that only the server knows what the session contains. (Needs to be over 32 characters, please do not hardcode this)

  • name: string The name of the cookie sent to the client. (defaults to "session")

  • passwordIndex: number When using rotating passwords, the index of the password to seal with. Defaults to the last password in the array.

  • cookie Options for the cookie. Defaults are { httpOnly: true, sameSite: "Lax" }

req.session

Used to modify the session.

req.session.count = 1;
// or
req.session = {
	count: 1,
};

res.saveSession()

Used to save the session. Chainable.

res.saveSession().send(`You've refreshed ${count} times!`);

res.deleteSession()

Used to delete the session. Chainable. This does not invalidate the session, it only tells the client to remove the cookie.

res.deleteSession().send(`There goes your session!`);

Password rotation

coggers-session supports password rotation, meaning that you can switch around the passwords used for sealing and unsealing the session cookies.

You can use password rotation simply by putting an array into the password field. If you ever want to use a new password, you can just add it to the end of the array. If you want to reuse an old password, you'll need to define the passwordIndex option to be the index of that old password.

For example, if you want to add a password to session({ password: "pass1" }), it would look like session({ password: ["pass1", "pass2"] }). Then, if you wanted to go back to pass1, you can use session({ password: ["pass1", "pass2"], passwordIndex: 0 }).

Do not move a password around, or remove it from the array. This can invalidate old sessions.

Internal workings

If you want to be reassured, or want to know how it works, see sealing.md