Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

redis-sessions

Package Overview
Dependencies
Maintainers
1
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

redis-sessions - npm Package Compare versions

Comparing version 3.0.0 to 4.0.0

_docs/migration_v3_to_v4.md

11

CHANGELOG.md
# CHANGELOG
## 4.0.0
* !!! BREAKING CHANGES !!!
* Added typescript and type definitions
* Updated redis to ^4.6.11
* Updated lodash to ^4.17.21
* Updated Node to 16+
* Switched from node-cache to lru-cache
* Switched from callbacks to async/await
## 3.0.0
* Dropped suppoer for Node 4 and Node 6
* Dropped support for Node 4 and Node 6
* Added test support for Node 12

@@ -7,0 +16,0 @@ * Updated all dependencies

38

package.json
{
"name": "redis-sessions",
"description": "An advanced session store for Redis",
"version": "3.0.0",
"version": "4.0.0",
"license": "MIT",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"author": "P. Liess <smrchy+npm@gmail.com>",
"engines": {
"node": "> 8"
"node": ">=16.0.0"
},
"scripts": {
"build": "./node_modules/.bin/coffee -cb index.coffee && ./node_modules/.bin/coffee -cb test/test.coffee",
"test": "node --expose-internals node_modules/.bin/mocha ./test/test.js"
"prebuild": "npm run lint",
"build": "npm run build:ts",
"build:ts": "tsc --declaration",
"test": "node node_modules/.bin/mocha ./dist/test/test.js",
"prepublishOnly": "npm run lint",
"preversion": "npm run lint",
"postversion": "echo 'IMPORTANT: Run `git push && git push --tags` to push created version to repo and `npm publish` to publish to npm'",
"lint": "eslint",
"watch": "tsc -w"
},
"dependencies": {
"lodash": "^4.17.15",
"node-cache": "^5.1.1",
"redis": "^3.0.2"
"lodash": "^4.17.21",
"lru-cache": "^10.1.0",
"redis": "^4.6.11"
},
"devDependencies": {
"coffeescript": "^2.5.1",
"async": "^3.2.0",
"mocha": "^8.0.1",
"should": "13.2.3"
"@types/lodash": "^4.14.202",
"@types/mocha": "^10.0.6",
"@types/node": "^20.11.5",
"@typescript-eslint/eslint-plugin": "^6.5.0",
"eslint": "8.52.0",
"eslint-plugin-jsdoc": "^46.5.1",
"eslint-plugin-unicorn": "^49.0.0",
"mocha": "^10.2.0",
"should": "13.2.3",
"typescript": "^5.2.2",
"typescript-eslint": "^0.0.1-alpha.0"
},

@@ -25,0 +41,0 @@ "keywords": [

# Redis Sessions
[![Redis-Sessions](https://nodei.co/npm/redis-sessions.png?downloads=true&stars=true)](https://nodei.co/npm/redis-sessions/)
[![Build Status](https://secure.travis-ci.org/smrchy/redis-sessions.svg?branch=master)](http://travis-ci.org/smrchy/redis-sessions)
[![Dependency Status](https://david-dm.org/smrchy/redis-sessions.svg)](https://david-dm.org/smrchy/redis-sessions)
[![Redis-Sessions](https://nodei.co/npm/redis-sessions.png?downloads=true&stars=true)](https://nodei.co/npm/redis-sessions/)
This is a Node.js module to keep sessions in a Redis datastore and add some useful methods.
The main purpose of this module is to generalize sessions across application server platforms. We use nginx reverse proxy to route parts of a website to a Node.js server and other parts could be Python, Ruby, .net, PHP, Coldfusion or Java servers. You can then use [rest-sessions](https://github.com/smrchy/rest-sessions) to access the same sessions on all app servers via a simple REST interface.
The main purpose of this module is to generalize sessions across application server platforms. We use nginx reverse proxy to route parts of a website to a Node.js server and other parts could be Python, Ruby, .net, PHP, Coldfusion or Java servers. You can then use [rest-sessions](https://github.com/smrchy/rest-sessions) (incompatible with version 4.0.0) to access the same sessions on all app servers via a simple REST interface.
If you use Express check out [https://www.npmjs.com/package/connect-redis-sessions](Connect-Redis-Sessions) for a ready to use middleware.
If you use Express check out [Connect-Redis-Sessions](https://www.npmjs.com/package/connect-redis-sessions) (incompatible with version 4.0.0) for a ready to use middleware.
## !!! BREAKING CHANGES VERSION 4.0 !!!
Due to a change from callbacks to async/await, version 4.0.0 is incompatible with version 3.x or lower.
[Migration-Guide](./_docs/migration_v3_to_v4.md).
connect-redis-sessions and rest-sessions are both incompatible with version 4.0.0.
## Installation

@@ -37,12 +41,10 @@

With Redis running on the same machine as the test script (run via `npm test`) on a 2017 iMac:
With Redis running on the same machine as the test script (run via `npm test`) on a 2018 MacBook Pro:
* Creates 1000 sessions in around 95ms.
* Gets those 1000 sessions and validates them in around 80ms.
* Removes those 1000 sessions in 8ms.
* Creates 1000 sessions in around 140ms.
* Gets those 1000 sessions and validates them in around 90ms.
* Removes those 1000 sessions in 15ms.
## Cache (optional setting)
Note: If you want to use the `cachetime` option you must not supply the `client` option.
Modern apps might also use a lot of requests while a user is active. This results in a lot of Redis requests to look up sessions. What's faster than an in-memory cache in Redis? An in-memory cache right in your app!

@@ -62,5 +64,5 @@ When you enable caching you can speed up session lookups by a lot. Consider the following before you enable it:

## Use via REST
## Use via REST (This is currently not compatible with the latest version)
See [rest-sessions](https://github.com/smrchy/rest-sessions).
See [rest-sessions](https://github.com/smrchy/rest-sessions) (incompatible with version 4.0.0).

@@ -72,3 +74,3 @@ ## Use in Node.js

```javascript
RedisSessions = require("redis-sessions");
import RedisSessions from "redis-sessions"
//

@@ -81,8 +83,12 @@ // Parameters for RedisSession:

// `host`, *optional* Default: `127.0.0.1`. The Redis host.
// `options`, *optional* Default: {}. Additional options. See: https://github.com/mranney/node_redis#rediscreateclientport-host-options
// `options`, *optional* Default: {}. Additional options. See: https://github.com/redis/node-redis/blob/master/docs/client-configuration.md
// `namespace`: *optional* Default: `rs`. The namespace prefix for all Redis keys used by this module.
// `wipe`: *optional* Default: `600`. The interval in seconds after which expired sessions are wiped. Only values `0` or greater than `10` allowed. Set to `0` to disable.
// `client`: *optional* An external RedisClient object which will be used for the connection.
// `cachetime`: *optional* Default: `0`. Number of seconds to cache sessions in memory. Can only be used if no `client` is supplied. See the "Cache" section.
rs = new RedisSessions();
// `cachemax` (Number) *optional* Default: `5000`. Maximum number of sessions stored in the cache.
rs = new RedisSessions<{
foo: string;
unread_msg?: number;
last_action?: string;
birthday?: string;
}>();

@@ -107,3 +113,3 @@ rsapp = "myapp";

rs.create({
const resp = await rs.create({
app: rsapp,

@@ -117,13 +123,12 @@ id: "user1001",

}
},
function(err, resp) {
// resp should be something like
// {token: "r30kKwv3sA6ExrJ9OmLSm4Wo3nt9MQA1yG94wn6ByFbNrVWhcwAyOM7Zhfxqh8fe"}
});
// resp should be something like
// {token: "r30kKwv3sA6ExrJ9OmLSm4Wo3nt9MQA1yG94wn6ByFbNrVWhcwAyOM7Zhfxqh8fe"}
```
Notes:
Notes:
* You might want to store this token in a cookie / localStorage / sessionStorage.
* If you use Express check out [https://www.npmjs.com/package/connect-redis-sessions](Connect-Redis-Sessions).
* If you use Express check out [Connect-Redis-Sessions](https://www.npmjs.com/package/connect-redis-sessions) (Currently incompatible with version 4.0.0).
* As long as the `ttl` isn't reached this token can be used to get the session object for this user.

@@ -136,30 +141,27 @@ * Remember that a user (`user1001` in this case) might have other sessions.

```javascript
rs.set({
const resp = await rs.set({
app: rsapp,
token: "r30kKwv3sA6ExrJ9OmLSm4Wo3nt9MQA1yG94wn6ByFbNrVWhcwAyOM7Zhfxqh8fe",
d: {
"unread_msgs": 12,
"last_action": "/read/news",
"birthday": "2013-08-13"
}},
function(err, resp) {
/*
resp contains the session with the new values:
unread_msgs: 12,
last_action: "/read/news",
birthday: "2013-08-13"
}});
/*
resp contains the session with the new values:
{
"id":"user1001",
"r": 1,
"w": 2,
"idle": 1,
"ttl": 7200,
"d":
{
"foo": "bar",
"unread_msgs": 12,
"last_action": "/read/news",
"birthday": "2013-08-13"
}
{
"id":"user1001",
"r": 1,
"w": 2,
"idle": 1,
"ttl": 7200,
"d":{
"foo": "bar",
"unread_msgs": 12,
"last_action": "/read/news",
"birthday": "2013-08-13"
}
*/
});
}
*/
```

@@ -172,26 +174,23 @@

```javascript
rs.get({
const resp= await rs.get({
app: rsapp,
token: "r30kKwv3sA6ExrJ9OmLSm4Wo3nt9MQA1yG94wn6ByFbNrVWhcwAyOM7Zhfxqh8fe"},
function(err, resp) {
/*
resp contains the session:
token: "r30kKwv3sA6ExrJ9OmLSm4Wo3nt9MQA1yG94wn6ByFbNrVWhcwAyOM7Zhfxqh8fe"});
/*
resp contains the session:
{
"id":"user1001",
"r": 2, // The number of reads on this token
"w": 2, // The number of writes on this token
"idle": 21, // The idle time in seconds.
"ttl": 7200, // Timeout after 7200 seconds idle time
"d":
{
"foo": "bar",
"unread_msgs": 12,
"last_action": "/read/news",
"birthday": "2013-08-13"
}
{
"id":"user1001",
"r": 2, // The number of reads on this token
"w": 2, // The number of writes on this token
"idle": 21, // The idle time in seconds.
"ttl": 7200, // Timeout after 7200 seconds idle time
"d":{
"foo": "bar",
"unread_msgs": 12,
"last_action": "/read/news",
"birthday": "2013-08-13"
}
}
*/
});
*/
```

@@ -208,3 +207,3 @@

rs.set({
const resp = await rs.set({
app: rsapp,

@@ -215,22 +214,19 @@ token: "r30kKwv3sA6ExrJ9OmLSm4Wo3nt9MQA1yG94wn6ByFbNrVWhcwAyOM7Zhfxqh8fe",

"last_action": "/read/msg/2121"
}},
function(err, resp) {
/*
resp contains the session with modified values:
}});
/*
resp contains the session with modified values:
{
"id":"user1001",
"r": 2,
"w": 3,
"idle": 1,
"ttl": 7200,
"d":
{
"last_action": "/read/msg/2121",
"birthday": "2013-08-13",
"foo": "bar"
}
{
"id":"user1001",
"r": 2,
"w": 3,
"idle": 1,
"ttl": 7200,
"d":{
"last_action": "/read/msg/2121",
"birthday": "2013-08-13",
"foo": "bar"
}
*/
});
}
*/
```

@@ -244,12 +240,10 @@

rs.kill({
const resp = await rs.kill({
app: rsapp,
token: "r30kKwv3sA6ExrJ9OmLSm4Wo3nt9MQA1yG94wn6ByFbNrVWhcwAyOM7Zhfxqh8fe"},
function(err, resp) {
/*
resp contains the result:
token: "r30kKwv3sA6ExrJ9OmLSm4Wo3nt9MQA1yG94wn6ByFbNrVWhcwAyOM7Zhfxqh8fe"});
/*
resp contains the result:
{kill: 1}
*/
});
{kill: 1}
*/
```

@@ -267,12 +261,11 @@

rs.activity({
const resp = await rs.activity({
app: rsapp,
dt: 600},
function(err, resp) {
/*
resp contains the activity:
deltaTime: 600
});
/*
resp contains the activity:
{activity: 12}
*/
});
{activity: 12}
*/
```

@@ -286,27 +279,30 @@

rs.soapp({
const resp = await rs.soapp({
app: rsapp,
dt: 600},
function(err, resp) {
/*
resp contains the sessions:
deltaTime: 600
});
/*
resp contains the sessions:
{ sessions:
[ { id: 'someuser123',
r: 1,
w: 1,
ttl: 30,
idle: 0,
ip: '127.0.0.2'
},
{ id: 'anotheruser456',
r: 4,
w: 2,
ttl: 7200,
idle: 24,
ip: '127.0.0.1' }
]
}
*/
});
{
sessions: [
{
id: 'someuser123',
r: 1,
w: 1,
ttl: 30,
idle: 0,
ip: '127.0.0.2'
},
{
id: 'anotheruser456',
r: 4,
w: 2,
ttl: 7200,
idle: 24,
ip: '127.0.0.1'
}
]
}
*/
```

@@ -320,26 +316,30 @@

rs.soid({
const resp = await rs.soid({
app: rsapp,
id: "bulkuser_999"},
function(err, resp) {
/*
resp contains the sessions:
id: "bulkuser_999"
});
/*
resp contains the sessions:
{ sessions:
[ { id: 'bulkuser_999',
r: 1,
w: 1,
ttl: 30,
idle: 0,
ip: '127.0.0.2' },
{ id: 'bulkuser_999',
r: 1,
w: 1,
ttl: 7200,
idle: 0,
ip: '127.0.0.1' }
]
}
*/
});
{
sessions: [
{
id: 'bulkuser_999',
r: 1,
w: 1,
ttl: 30,
idle: 0,
ip: '127.0.0.2'
},
{
id: 'bulkuser_999',
r: 1,
w: 1,
ttl: 7200,
idle: 0,
ip: '127.0.0.1'
}
]
}
*/
```

@@ -353,10 +353,8 @@

rs.killsoid({app: rsapp, id: 'bulkuser_999'},
function(err, resp) {
/*
resp contains the result:
const resp = rs.killsoid({app: rsapp, id: 'bulkuser_999'});
/*
resp contains the result:
{kill: 2} // The amount of sessions that were killed
*/
});
{kill: 2} // The amount of sessions that were killed
*/
```

@@ -370,10 +368,8 @@

rs.killall({app: rsapp},
function(err, resp) {
/*
resp contains the result:
const resp = await rs.killall({app: rsapp});
/*
resp contains the result:
{kill: 12} // The amount of sessions that were killed
*/
});
{kill: 12} // The amount of sessions that were killed
*/
```

@@ -387,17 +383,24 @@

rs.ping(function(err, resp) {
/*
resp contains the result:
const resp = await rs.ping();
/*
resp contains the result:
"PONG"
*/
});
"PONG"
*/
```
## Tests
Before running Test you need to build the js files (npm run build) and have a redis server running.
## Typescript Pitfalls !!!
* If you do not specify a d object in `create` and only partially set it using the `set` function, be aware that `get` may return a session with a defined d object that is missing properties of the supplied type.
* The `set` function only lets you delete optional keys.
* If you use an Record<string,...> as the Generic Type you wont be able to delete properties with the `set` function. If you don`t have an more defined data type use the any type and cast your returned objects.
* If you define your type as an empty object or only have optional parameters giving an empty object for d will still trow an error at runtime.
## CHANGELOG
See https://github.com/smrchy/redis-sessions/blob/master/CHANGELOG.md
See [CHANGELOG.md](https://github.com/smrchy/redis-sessions/blob/master/CHANGELOG.md)

@@ -432,3 +435,2 @@

## The MIT License (MIT)

@@ -435,0 +437,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc