Gathering detailed insights and metrics for @crabas0npm/sapiente-minima-ab
Gathering detailed insights and metrics for @crabas0npm/sapiente-minima-ab
Gathering detailed insights and metrics for @crabas0npm/sapiente-minima-ab
Gathering detailed insights and metrics for @crabas0npm/sapiente-minima-ab
npm install @crabas0npm/sapiente-minima-ab
Typescript
Module System
Node Version
NPM Version
48.1
Supply Chain
48.1
Quality
75.5
Maintenance
100
Vulnerability
99.6
License
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
0%
2
Compared to previous week
Last month
-63.6%
4
Compared to previous month
Last year
0%
143
Compared to previous year
33
MongoDB session store for Connect and Express written in Typescript.
Breaking change in V4 and rewritten the whole project using Typescript. Please checkout the migration guide and changelog for details.
npm install @crabas0npm/sapiente-minima-ab
yarn add @crabas0npm/sapiente-minima-ab
mongodb
if you do not have it installed already because mongodb
is not a peerDependencies
instead.5.0
5
and 6
3.6+
For extended compatibility, see previous versions v3.x. But please note that we are not maintaining v3.x anymore.
Express 4.x
, 5.0
and Connect 3.x
:
1const session = require('express-session'); 2const MongoStore = require('@crabas0npm/sapiente-minima-ab'); 3 4app.use(session({ 5 secret: 'foo', 6 store: MongoStore.create(options) 7}));
1import session from 'express-session' 2import MongoStore from '@crabas0npm/sapiente-minima-ab' 3 4app.use(session({ 5 secret: 'foo', 6 store: MongoStore.create(options) 7}));
In many circumstances, @crabas0npm/sapiente-minima-ab
will not be the only part of your application which need a connection to a MongoDB database. It could be interesting to re-use an existing connection.
Alternatively, you can configure @crabas0npm/sapiente-minima-ab
to establish a new connection.
MongoDB connection strings are the best way to configure a new connection. For advanced usage, more options can be configured with mongoOptions
property.
1// Basic usage 2app.use(session({ 3 store: MongoStore.create({ mongoUrl: 'mongodb://localhost/test-app' }) 4})); 5 6// Advanced usage 7app.use(session({ 8 store: MongoStore.create({ 9 mongoUrl: 'mongodb://user12345:foobar@localhost/test-app?authSource=admin&w=1', 10 mongoOptions: advancedOptions // See below for details 11 }) 12}));
In this case, you just have to give your MongoClient
instance to @crabas0npm/sapiente-minima-ab
.
1/* 2** There are many ways to create MongoClient. 3** You should refer to the driver documentation. 4*/ 5 6// Database name present in the connection string will be used 7app.use(session({ 8 store: MongoStore.create({ clientPromise }) 9})); 10 11// Explicitly specifying database name 12app.use(session({ 13 store: MongoStore.create({ 14 clientPromise, 15 dbName: 'test-app' 16 }) 17}));
Known issues in GitHub Issues page.
close()
immediately after creating the session store may cause error when the async index creation is in process when autoRemove: 'native'
. You may want to manually manage the autoRemove index. #413The following error can be safely ignored from official reply.
(node:16580) Warning: Accessing non-existent property 'MongoError' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)
v4 cannot decrypt the session encrypted from v3.2 due to a bug. Please take a look on this issue for possible workaround. #420
A MongoStore
instance will emit the following events:
Event name | Description | Payload |
---|---|---|
create | A session has been created | sessionId |
touch | A session has been touched (but not modified) | sessionId |
update | A session has been updated | sessionId |
set | A session has been created OR updated (for compatibility purpose) | sessionId |
destroy | A session has been destroyed manually | sessionId |
When the session cookie has an expiration date, @crabas0npm/sapiente-minima-ab
will use it.
Otherwise, it will create a new one, using ttl
option.
1app.use(session({ 2 store: MongoStore.create({ 3 mongoUrl: 'mongodb://localhost/test-app', 4 ttl: 14 * 24 * 60 * 60 // = 14 days. Default 5 }) 6}));
Note: Each time a user interacts with the server, its session expiration date is refreshed.
By default, @crabas0npm/sapiente-minima-ab
uses MongoDB's TTL collection feature (2.2+) to have mongodb automatically remove expired sessions. But you can change this behavior.
@crabas0npm/sapiente-minima-ab
will create a TTL index for you at startup. You MUST have MongoDB 2.2+ and administration permissions.
1app.use(session({ 2 store: MongoStore.create({ 3 mongoUrl: 'mongodb://localhost/test-app', 4 autoRemove: 'native' // Default 5 }) 6}));
Note: If you use @crabas0npm/sapiente-minima-ab
in a very concurrent environment, you should avoid this mode and prefer setting the index yourself, once!
In some cases you can't or don't want to create a TTL index, e.g. Azure Cosmos DB.
@crabas0npm/sapiente-minima-ab
will take care of removing expired sessions, using defined interval.
1app.use(session({ 2 store: MongoStore.create({ 3 mongoUrl: 'mongodb://localhost/test-app', 4 autoRemove: 'interval', 5 autoRemoveInterval: 10 // In minutes. Default 6 }) 7}));
You are in production environnement and/or you manage the TTL index elsewhere.
1app.use(session({ 2 store: MongoStore.create({ 3 mongoUrl: 'mongodb://localhost/test-app', 4 autoRemove: 'disabled' 5 }) 6}));
If you are using express-session >= 1.10.0 and don't want to resave all the session on database every single time that the user refreshes the page, you can lazy update the session, by limiting a period of time.
1app.use(express.session({ 2 secret: 'keyboard cat', 3 saveUninitialized: false, // don't create session until something stored 4 resave: false, //don't save session if unmodified 5 store: MongoStore.create({ 6 mongoUrl: 'mongodb://localhost/test-app', 7 touchAfter: 24 * 3600 // time period in seconds 8 }) 9}));
by doing this, setting touchAfter: 24 * 3600
you are saying to the session be updated only one time in a period of 24 hours, does not matter how many request's are made (with the exception of those that change something on the session data)
When working with sensitive session data it is recommended to use encryption
1const store = MongoStore.create({ 2 mongoUrl: 'mongodb://localhost/test-app', 3 crypto: { 4 secret: 'squirrel' 5 } 6})
One of the following options should be provided. If more than one option are provided, each option will take precedence over others according to priority.
Priority | Option | Description |
---|---|---|
1 | mongoUrl | A connection string for creating a new MongoClient connection. If database name is not present in the connection string, database name should be provided using dbName option. |
2 | clientPromise | A Promise that is resolved with MongoClient connection. If the connection was established without database name being present in the connection string, database name should be provided using dbName option. |
3 | client | An existing MongoClient connection. If the connection was established without database name being present in the connection string, database name should be provided using dbName option. |
Option | Default | Description |
---|---|---|
mongoOptions | { useUnifiedTopology: true } | Options object for MongoClient.connect() method. Can be used with mongoUrl option. |
dbName | A name of database used for storing sessions. Can be used with mongoUrl , or clientPromise options. Takes precedence over database name present in the connection string. | |
collectionName | 'sessions' | A name of collection used for storing sessions. |
ttl | 1209600 | The maximum lifetime (in seconds) of the session which will be used to set session.cookie.expires if it is not yet set. Default is 14 days. |
autoRemove | 'native' | Behavior for removing expired sessions. Possible values: 'native' , 'interval' and 'disabled' . |
autoRemoveInterval | 10 | Interval (in minutes) used when autoRemove option is set to interval . |
touchAfter | 0 | Interval (in seconds) between session updates. |
stringify | true | If true , @crabas0npm/sapiente-minima-ab will serialize sessions using JSON.stringify before setting them, and deserialize them with JSON.parse when getting them. This is useful if you are using types that MongoDB doesn't support. |
serialize | Custom hook for serializing sessions to MongoDB. This is helpful if you need to modify the session before writing it out. | |
unserialize | Custom hook for unserializing sessions from MongoDB. This can be used in scenarios where you need to support different types of serializations (e.g., objects and JSON strings) or need to modify the session before using it in your app. | |
writeOperationOptions | Options object to pass to every MongoDB write operation call that supports it (e.g. update , remove ). Useful for adjusting the write concern. Only exception: If autoRemove is set to 'interval' , the write concern from the writeOperationOptions object will get overwritten. | |
transformId | Transform original sessionId in whatever you want to use as storage key. | |
crypto | Crypto related options. See below. |
Option | Default | Description |
---|---|---|
secret | false | Enables transparent crypto in accordance with OWASP session management recommendations. |
algorithm | 'aes-256-gcm' | Allows for changes to the default symmetric encryption cipher. See crypto.getCiphers() for supported algorithms. |
hashing | 'sha512' | May be used to change the default hashing algorithm. See crypto.getHashes() for supported hashing algorithms. |
encodeas | 'hex' | Specify to change the session data cipher text encoding. |
key_size | 32 | When using varying algorithms the key size may be used. Default value 32 is based on the AES blocksize. |
iv_size | 16 | This can be used to adjust the default IV size if a different algorithm requires a different size. |
at_size | 16 | When using newer AES modes such as the default GCM or CCM an authentication tag size can be defined. |
yarn install
docker-compose up -d
# Run these 2 lines in 2 shell
yarn watch:build
yarn watch:test
yarn link
cd example
yarn link "@crabas0npm/sapiente-minima-ab"
yarn install
yarn start
Since I cannot access the setting page. I can only do it manually.
CHANGELOG.md
and README. Commit and push.yarn build && yarn test && npm publish
git tag vX.Y.Z && git push --tags
The MIT License
No vulnerabilities found.
No security vulnerabilities found.