@allegiant/sessions
Advanced tools
Comparing version 0.0.7 to 0.0.8
@@ -0,1 +1,12 @@ | ||
# 0.0.8 (1/2/2018) @echopoint | ||
- API Changes: | ||
*changed autoStart to autogen to be clear of intention. Specifies whether or not to fully generate a new session if one doesn't already exist. | ||
*sessions are automatically restarted only if they already exist on the client. | ||
+started property added and applied to current functionality | ||
# 0.0.7 (1/1/2018) @gabrielcsapo | ||
- Updated usage information and example | ||
# 0.0.6 (1/1/2018) @echopoint | ||
@@ -2,0 +13,0 @@ |
42
index.js
const path = require('path'); | ||
const common = require('@allegiant/common'); | ||
const { expect, getFutureMs, MSONEHOUR } = require('@allegiant/common'); | ||
const Storage = require('@allegiant/jsonstore'); | ||
const JSONStore = Storage.JSONStore; | ||
const Storable = Storage.Storable; | ||
const expect = common.expect; | ||
class Session extends Storable { | ||
constructor(store, name, secure, updateExpiry, autoStart, conn) { | ||
constructor(store, name, secure, updateExpiry, autogen, conn) { | ||
super(store, false); | ||
@@ -16,6 +15,6 @@ | ||
this._conn = conn; | ||
this._started = false; | ||
this.name = name; | ||
if (autoStart) | ||
this.start(); | ||
this.start(autogen); | ||
} | ||
@@ -49,2 +48,6 @@ | ||
get started() { | ||
return expect(this._started, false); | ||
} | ||
reset(id=false, update=false, data={}, expiry=false) { | ||
@@ -55,3 +58,5 @@ super.reset(id, update, data); | ||
async start() { | ||
async start(complete=true) { | ||
if (this.started) return; | ||
if (this.store === false) | ||
@@ -62,19 +67,20 @@ throw new Error("Session store not defined"); | ||
var id = this._conn.cookies.get(this.name); | ||
if (id !== null) { | ||
var data = this.store.get(id); | ||
if (typeof data !== 'object') { | ||
console.log("Invalid session: ", id, ": Not an object"); // eslint-disable-line | ||
console.log("Invalid session: ", id); // eslint-disable-line | ||
} else if (data.secure != this._secure) { | ||
console.log("Invalid session: ", id, ": Security doesn't match"); // eslint-disable-line | ||
} else { | ||
this.reset(id, this._updateExpiry, data.data, | ||
this._updateExpiry ? getFutureMs(MSONEHOUR) : data.expires); | ||
regen = false; | ||
this.reset(id, this._updateExpiry, data.data, | ||
this._updateExpiry ? common.getFutureMs(common.MSONEHOUR) : data.expires); | ||
this._started = true; | ||
} | ||
} | ||
if (regen) { | ||
if (complete && regen) { | ||
// defaulting to one hour to check pruning | ||
this.reset(super._genUniqueId(), true, {}, common.getFutureMs(common.MSONEHOUR)); | ||
this.reset(super._genUniqueId(), true, {}, getFutureMs(MSONEHOUR)); | ||
@@ -88,6 +94,8 @@ this._conn.cookies.set(this.name, { | ||
}); | ||
this._started = true; | ||
} | ||
if (this.needsUpdate) | ||
await this.save(); | ||
this.save(); | ||
} | ||
@@ -104,2 +112,3 @@ | ||
async invalidate() { | ||
if (!this.started) return; | ||
if (!await super.destroy()) | ||
@@ -113,5 +122,6 @@ return; | ||
this._conn = null; | ||
this._started = null; | ||
this.name = null; | ||
} | ||
async regen() { | ||
@@ -132,7 +142,7 @@ await this.invalidate(); | ||
config.updateExpiry = expect(options.updateExpiry, true); | ||
config.autoStart = expect(options.autoStart, false); | ||
config.autogen = expect(options.autogen, false); | ||
config.name = expect(options.name, 'id'); | ||
if (config.enabled) { | ||
config.Session = Session.bind(null, config.store, config.name, config.secure, config.updateExpiry, config.autoStart); | ||
config.Session = Session.bind(null, config.store, config.name, config.secure, config.updateExpiry, config.autogen); | ||
@@ -139,0 +149,0 @@ config.bind = function(app) { |
{ | ||
"name": "@allegiant/sessions", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"description": "Simple sessions module for the allegiant app framework.", | ||
@@ -28,3 +28,3 @@ "author": "echopoint <echopoint@tutanota.com>", | ||
"@allegiant/cookies": "0.0.5", | ||
"@allegiant/jsonstore": "0.0.6" | ||
"@allegiant/jsonstore": "0.0.7" | ||
}, | ||
@@ -31,0 +31,0 @@ "devDependencies": { |
@@ -17,2 +17,25 @@ # sessions | ||
## Usage | ||
```js | ||
const App = require('@allegiant/core'); | ||
let server = App.create("https://localhost:7000", { | ||
'@allegiant/sessions': { | ||
enabled: true, | ||
path: path.resolve(path.join(process.cwd(), 'sessions')), | ||
autogen: true | ||
} | ||
}); | ||
server.get('/', function() { | ||
if (!this.session.data.firstTimeServed) { | ||
this.session.data.firstTimeServed = new Date() | ||
} | ||
this.content = `<h1>It just works! You first looked at this content on ${this.session.data.firstTimeServed}</h1>`; | ||
return 200; | ||
}) | ||
.start(); | ||
``` | ||
### Copyright & License | ||
@@ -19,0 +42,0 @@ |
10077
126
45
+ Added@allegiant/jsonstore@0.0.7(transitive)
- Removed@allegiant/jsonstore@0.0.6(transitive)
Updated@allegiant/jsonstore@0.0.7