Skip to content

Commit

Permalink
Merge branch 'release/1.0.18'
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Oct 30, 2017
2 parents 4400e7f + 291a27c commit 2c9a99c
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 92 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
<a name="1.0.18"></a>
## [1.0.18](https://github.com/adonisjs/adonis-session/compare/v1.0.17...v1.0.18) (2017-10-30)


### Bug Fixes

* **session:** initiate session before hand using context.getter ([3e82994](https://github.com/adonisjs/adonis-session/commit/3e82994))



<a name="1.0.17"></a>
## [1.0.17](https://github.com/adonisjs/adonis-session/compare/v1.0.16...v1.0.17) (2017-10-29)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@adonisjs/session",
"version": "1.0.17",
"version": "1.0.18",
"description": "This repo is the official session provider for Adonisjs apps. It supports multiple drivers to store session data.",
"main": "providers/SessionProvider",
"directories": {
Expand Down
24 changes: 24 additions & 0 deletions providers/SessionProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,31 @@ class SessionProvider extends ServiceProvider {
this._registerVowTrait()
}

/**
* Boot the provider
*
* @method boot
*
* @return {void}
*/
boot () {
const HttpContext = this.app.use('Adonis/Src/HttpContext')
const Config = this.app.use('Adonis/Src/Config')
const SessionManager = this.app.use('Adonis/Src/Session')

/**
* Adding getter to the HTTP context. Please note the session
* store is not initialized yet and middleware must be
* executed before the session store can be used
* for fetching or setting data.
*/
HttpContext.getter('session', function () {
return require('../Src/Session/getRequestInstance')(this.request, this.response, Config, SessionManager)
}, true)

/**
* Adding flash globals to the view layer, only when it is in use
*/
try {
require('../src/Session/FlashGlobals')(this.app.use('Adonis/Src/View'))
} catch (error) {}
Expand Down
18 changes: 1 addition & 17 deletions src/Session/Middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* file that was distributed with this source code.
*/

const Session = require('./index')
const debug = require('debug')('adonis:session')

/**
Expand All @@ -26,11 +25,6 @@ const debug = require('debug')('adonis:session')
* @constructor
*/
class SessionMiddleware {
constructor (Config, SessionManager) {
this.Config = Config
this.SessionManager = SessionManager
}

/**
* Handle method to be executed on each request
*
Expand All @@ -42,22 +36,12 @@ class SessionMiddleware {
* @return {void}
*/
async handle (ctx, next) {
const driver = this.Config.get('session.driver', 'cookie')
debug('using %s session driver', driver)

const driverInstance = this.SessionManager.makeDriverInstance(driver)

if (typeof (driverInstance.setRequest) === 'function') {
driverInstance.setRequest(ctx.request, ctx.response)
}

ctx.session = new Session(ctx.request, ctx.response, driverInstance, this.Config)

/**
* Initiate the store by reading values from the
* driver.
*/
await ctx.session.instantiate()
debug('session store initiated')

/**
* Sharing flash messages with the view when view
Expand Down
26 changes: 26 additions & 0 deletions src/Session/getRequestInstance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict'

/*
* adonis-session
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

const debug = require('debug')('adonis:session')

module.exports = function getRequestInstance (request, response, Config, SessionManager) {
const driver = Config.get('session.driver', 'cookie')
debug('using %s driver', driver)

const driverInstance = SessionManager.makeDriverInstance(driver)

if (typeof (driverInstance.setRequest) === 'function') {
driverInstance.setRequest(request, response)
}

const Session = require('./index')
return new Session(request, response, driverInstance, Config)
}
41 changes: 41 additions & 0 deletions src/Session/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ class Session {
this._sessionId = null
}

/**
* A boolean flag telling whether store has been
* initiated or not
*
* @attribute initiated
*
* @return {Boolean}
*/
get initiated () {
return !!this._store
}

/**
* Returns a unique session id for the given
* session.
Expand Down Expand Up @@ -78,6 +90,26 @@ class Session {
debug('touching session to remain active')
}

/**
* Throws an exception when session store is
* not initiated
*
* @method _ensureInitiated
*
* @return {void}
*
* @private
*
* @throws {Exception} If session store has not be initiated
*/
_ensureInitiated () {
if (!this.initiated) {
throw GE
.RuntimeException
.invoke('Session store is not initiated yet. Make sure that you have included the session middleware inside the list of global middleware.')
}
}

/**
* Returns an instance of store with existing values
* for a given session or empty store if session
Expand Down Expand Up @@ -137,6 +169,7 @@ class Session {
* @inheritDoc('Store.put')
*/
put (...args) {
this._ensureInitiated()
return this._store.put(...args)
}

Expand All @@ -145,6 +178,7 @@ class Session {
* @inheritDoc('Store.get')
*/
get (...args) {
this._ensureInitiated()
return this._store.get(...args)
}

Expand All @@ -153,6 +187,7 @@ class Session {
* @inheritDoc('Store.all')
*/
all (...args) {
this._ensureInitiated()
return this._store.all(...args)
}

Expand All @@ -161,6 +196,7 @@ class Session {
* @inheritDoc('Store.forget')
*/
forget (...args) {
this._ensureInitiated()
return this._store.forget(...args)
}

Expand All @@ -169,6 +205,7 @@ class Session {
* @inheritDoc('Store.pull')
*/
pull (...args) {
this._ensureInitiated()
return this._store.pull(...args)
}

Expand All @@ -177,6 +214,7 @@ class Session {
* @inheritDoc('Store.increment')
*/
increment (...args) {
this._ensureInitiated()
return this._store.increment(...args)
}

Expand All @@ -185,6 +223,7 @@ class Session {
* @inheritDoc('Store.decrement')
*/
decrement (...args) {
this._ensureInitiated()
return this._store.decrement(...args)
}

Expand All @@ -193,6 +232,7 @@ class Session {
* @inheritDoc('Store.clear')
*/
clear () {
this._ensureInitiated()
this._store.clear()
}

Expand Down Expand Up @@ -265,6 +305,7 @@ class Session {
throw GE.InvalidArgumentException.invalidParameter('Flash data should be an object', data)
}
const flashMessage = this.get('__flash__', null)

if (!flashMessage) {
this.put('__flash__', data)
} else {
Expand Down
84 changes: 10 additions & 74 deletions test/session-middleware.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ const { ioc } = require('@adonisjs/fold')
const supertest = require('supertest')
const { Config } = require('@adonisjs/sink')

const Session = require('../src/Session')
const SessionManager = require('../src/Session/Manager')
const Middleware = require('../src/Session/Middleware')
const { redis: Redis } = require('../src/Session/Drivers')
const getRequestInstance = require('../src/Session/getRequestInstance')

let sessionValues = ''

test.group('Middleware', (group) => {
Expand All @@ -41,74 +41,6 @@ test.group('Middleware', (group) => {
})
})

test('attach session instance on the http context', async (assert) => {
const server = http.createServer((req, res) => {
const request = {
cookie: () => {}
}

const response = {
cookie: () => {}
}

const context = { request, response }
const middleware = new Middleware(ioc.use('Adonis/Src/Config'), SessionManager)
middleware
.handle(context, async function () {
return true
})
.then(() => {
assert.isDefined(context.session)
assert.instanceOf(context.session, Session)
res.end()
}).catch(({ message }) => {
console.log(message)
res.writeHead(500)
res.write(message)
res.end()
})
})

await supertest(server).get('/').expect(200)
})

test('do not call setRequest when driver has not implemented the method', async (assert) => {
ioc.singleton('Adonis/Src/Config', () => {
const config = new Config()
config.set('session.driver', 'redis')
return config
})

const server = http.createServer((req, res) => {
const request = {
cookie: () => {}
}

const response = {
cookie: () => {}
}

const context = { request, response }
const middleware = new Middleware(ioc.use('Adonis/Src/Config'), SessionManager)
middleware
.handle(context, async function () {
return true
})
.then(() => {
assert.isDefined(context.session)
assert.instanceOf(context.session._driverInstance, Redis)
res.end()
}).catch(({ message }) => {
console.log(message)
res.writeHead(500)
res.write(message)
res.end()
})
})

await supertest(server).get('/').expect(200)
})

test('pull flash messages on each request', async (assert) => {
ioc.singleton('Adonis/Src/Config', () => {
const config = new Config()
Expand All @@ -125,8 +57,10 @@ test.group('Middleware', (group) => {
cookie: () => {}
}

const context = { request, response }
const middleware = new Middleware(ioc.use('Adonis/Src/Config'), SessionManager)
const session = getRequestInstance(request, response, new Config(), SessionManager)
const context = { request, response, session }
const middleware = new Middleware()

middleware
.handle(context, async function () {
return true
Expand Down Expand Up @@ -178,8 +112,10 @@ test.group('Middleware', (group) => {
cookie: () => {}
}

const context = { request, response, view }
const middleware = new Middleware(ioc.use('Adonis/Src/Config'), SessionManager)
const session = getRequestInstance(request, response, ioc.use('Adonis/Src/Config'), SessionManager)
const context = { request, response, view, session }
const middleware = new Middleware()

middleware
.handle(context, async function () {
return true
Expand Down
19 changes: 19 additions & 0 deletions test/session.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,25 @@ test.group('Session', () => {
assert.isTrue(calledTouch)
Cookie.prototype.touch = existingTouch
})

test('throw exception when store is not initiated', async (assert) => {
const server = http.createServer((req, res) => {
const config = new Config()
const cookie = new Cookie(config)
cookie.setRequest(helpers.getRequest(req), helpers.getResponse(res))
const session = new Session(helpers.getRequest(req), helpers.getResponse(res), cookie, config)

try {
session.put('username', 'virk')
} catch (error) {
res.writeHead(500)
res.end(error.message)
}
})

const { text } = await supertest(server).get('/').expect(500)
assert.equal(text, 'E_RUNTIME_ERROR: Session store is not initiated yet. Make sure that you have included the session middleware inside the list of global middleware.')
})
})

test.group('Session Store', () => {
Expand Down

0 comments on commit 2c9a99c

Please sign in to comment.