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

alive-sessions

v1.1.1

Published

Track sessions expires times to trigger a callback when a session expires and revalidate session expire time.

Downloads

11

Readme

alive-sessions

Trigger a callback when a session expires and revalidate expire time of a session.

When a session expires, the callback is triggered and the session is removed from the store. Since the store is a javascript object the time complexity of the operations is O(1), which is good for handling many sessions.

This trigger callback can be used to remove a session from a database.

Getting started

Install this package using npm.

npm install alive-sessions

Esm

import * as aliveSessions from 'alive-sessions'
-or-
import { createSession } from 'alive-sessions'

CommonJS

const aliveSessions = require('alive-sessions')
-or-
const { createSession } = require('aliveSessions')

API

Constraint| Constraint meaning | --- | --- | REQ | Required OPT | Optional DEF=X | Default value is X

⚙️ Create a new session

Creating a new session will insert a new session in the store and start the expire timer. When the session expires the callback will be triggered.

createSession({
  sessionID: 'you-session-id', 
  expireMs: 300000, 
  data: any,
  action: () => console.log('Session expired')
})

Params: REQ sessionID: The session id. It can be any string. DEF=5min expireMs: The session expire time in milliseconds. REQ action: The callback to be triggered when the session expires. DEF=null data: Any data to be passed to the callback.

Returns: Undefined if the session was created successfully or throws an error if something went wrong.

⚙️ Revalidate a session

Revalidating a session will update the expire time of the session and prevent the session from expiring.

revalidateSession(sessionID, expireMs)

Params: REQ sessionID: The session id. It can be any string. REQ expireMs: The session expire time in milliseconds.

Returns: Undefined if the session was revalidated successfully or throws an error if something went wrong.

⚙️ Check for a session

Check if a session exists in the store and prevent duplicate sessions.

sessionExists(sessionID)

Params: REQ sessionID: The session id. It can be any string.

Returns: True if the session exists or False if the session does not exist.

⚙️ Remove a session

Remove a session from the store before it expires.

removeSession(sessionID)

Params: REQ sessionID: The session id. It can be any string.

Returns: Undefined if the session was removed successfully or throws an error if something went wrong.

⚙️ Get a session data

Given a session id, get the data associated with the session.

getSessionData(sessionID)

Params: REQ sessionID: The session id. It can be any string.

Returns: The session value (any) or throws an error if something went wrong. The default session value is null.

⚙️ Sessions

Get all sessions from the store for testing purposes. Modifying the store directly is not recommended and may result in unexpected errors.

sessions()

Params: N/A

Returns: An object with all the sessions in the store.

⚙️ Conversions

alive-sessions package also provides helper functions to convert minutes and seconds to milliseconds.

secondsToMs(seconds)
minutesToMs(minutes)

Params: REQ seconds/minutes: The number of the unit on each case.

Returns: The number of milliseconds.