Skip to content

Commit

Permalink
Merge branch 'release/1.0.11'
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Aug 29, 2017
2 parents 0ed7d33 + 88d0ad6 commit 04047d0
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 82 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
<a name="1.0.11"></a>
## [1.0.11](https://github.com/adonisjs/adonis-session/compare/v1.0.10...v1.0.11) (2017-08-29)


### Features

* **client:** add session client ([85378d2](https://github.com/adonisjs/adonis-session/commit/85378d2))
* **trait:** add session client trait for vow ([54b6516](https://github.com/adonisjs/adonis-session/commit/54b6516))



<a name="1.0.10"></a>
## [1.0.10](https://github.com/adonisjs/adonis-session/compare/v1.0.9...v1.0.10) (2017-08-18)

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.10",
"version": "1.0.11",
"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
17 changes: 17 additions & 0 deletions providers/SessionProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ class SessionProvider extends ServiceProvider {
})
}

/**
* Register the vow trait to bind session client
* under `Adonis/Traits/Session` namespace.
*
* @method _registerVowTrait
*
* @return {void}
*/
_registerVowTrait () {
this.app.bind('Adonis/Traits/Session', (app) => {
const Config = app.use('Adonis/Src/Config')
return ({ Request }) => require('../src/VowBindings/Request')(Request, Config)
})
this.app.alias('Adonis/Traits/Session', 'Session/Client')
}

/**
* Register method called by ioc container
*
Expand All @@ -85,6 +101,7 @@ class SessionProvider extends ServiceProvider {
this._registerProvider()
this._registerClient()
this._registerMiddleware()
this._registerVowTrait()
}

boot () {
Expand Down
68 changes: 33 additions & 35 deletions src/Session/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,22 @@
*/

const uuid = require('uuid')
const { memory: Driver } = require('./Drivers')
const { memoryStore } = Driver
const util = require('../../lib/util')
const Store = require('./Store')
const util = require('../../lib/util')

/**
* Session client to set sessions as
* cookies.
*
* @constructor
* @class SessionClient
*/
class SessionClient {
constructor (testRequest, Config) {
const { options, key } = util.getCookieOption(Config)
this._options = options
this._key = key
this._driverInstance = new Driver()
this._testRequest = testRequest
constructor (Config) {
const { key } = util.getCookieOption(Config)
this._sessionId = uuid.v4()
this._store = null
}

/**
* Loads the data from the memory driver
*
* @method instantiate
*
* @return {void}
*/
instantiate () {
this._store = new Store(this._driverInstance.read(this._sessionId))
}

/**
* Writes the session id as the request cookie and
* values to the store
*
* @method commit
*
* @return {void}
*/
commit () {
this._testRequest.setCookie(this._key, this._sessionId)
this._driverInstance.write(this._sessionId, JSON.stringify(this._store.toJSON()))
this._key = key
this._store = new Store()
}

/* istanbul ignore next */
Expand Down Expand Up @@ -121,7 +99,27 @@ class SessionClient {
*/
clear () {
this._store.clear()
memoryStore.clear()
}

/**
* Returns an object with keys and values
* to be set as cookies
*
* @method toJSON
*
* @return {Array}
*/
toJSON () {
return [
{
key: this._key,
value: this._sessionId
},
{
key: `${this._key}-values`,
value: JSON.stringify(this._store.toJSON())
}
]
}
}

Expand Down
41 changes: 41 additions & 0 deletions src/VowBindings/Request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'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 SessionClient = require('../Session/Client')

module.exports = function (Request, Config) {
/**
* A request getter to set session client on the
* request.
*/
Request.getter('_session', function () {
return new SessionClient(Config)
}, true)

/**
* A request macro to set session when making
* requests
*/
Request.macro('session', function (key, value) {
this._session.put(key, value)
return this
})

/**
* Adding session as cookie before request
* is executed
*/
Request.before((requestInstance) => {
requestInstance._session.toJSON().forEach((item) => {
requestInstance.cookie(item.key, item.value)
})
})
}
64 changes: 18 additions & 46 deletions test/session-client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,56 +12,28 @@
const test = require('japa')
const { Config } = require('@adonisjs/sink')
const SessionClient = require('../src/Session/Client')
const { memoryStore } = require('../src/Session/Drivers/Memory')

const getReq = () => {
return {
cookies: [],
setCookie (key, value) {
this.cookies.push({ key, value })
}
}
}
const SessionStore = require('../src/Session/Store')

test.group('Session client', (group) => {
group.beforeEach(() => {
memoryStore.clear()
})

test('initiate the session client', (assert) => {
const req = getReq()
const client = new SessionClient(req, new Config())
client.instantiate()
assert.isDefined(client._store)
})

test('add value to the store and set session id', (assert) => {
const req = getReq()
const client = new SessionClient(req, new Config())
client.instantiate()
client.put('username', 'virk')
client.commit()
assert.deepEqual(client._store._values, { username: 'virk' })
assert.equal(client._sessionId, req.cookies[0].value)
})

test('get value from the session', (assert) => {
const req = getReq()
const client = new SessionClient(req, new Config())
client.instantiate()
client.put('username', 'virk')
client.commit()
assert.equal(client.get('username'), 'virk')
const client = new SessionClient(new Config())
assert.instanceOf(client, SessionClient)
assert.instanceOf(client._store, SessionStore)
assert.isDefined(client._sessionId)
})

test('clear the memory driver and store', (assert) => {
const req = getReq()
const client = new SessionClient(req, new Config())
client.instantiate()
client.put('username', 'virk')
client.commit()
client.clear()
assert.equal(memoryStore.size, 0)
assert.deepEqual(client._store, { _values: {}, isDirty: true })
test('get array of cookie key/value pairs', (assert) => {
const client = new SessionClient(new Config())
client.put('name', 'virk')
client.put('age', 22)

const json = client.toJSON()
assert.equal(json[0].key, 'adonis-session')
assert.isDefined(json[0].value)
assert.equal(json[1].key, 'adonis-session-values')
assert.equal(json[1].value, JSON.stringify({
name: { d: 'virk', t: 'String' },
age: { d: '22', t: 'Number' }
}))
})
})
59 changes: 59 additions & 0 deletions test/vow-bindings.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'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 test = require('japa')
const { Config } = require('@adonisjs/sink')
const SessionClient = require('../src/Session/Client')
const VowRequest = require('../src/VowBindings/Request')

class Request {
constructor () {
this._getters = []
this._macros = []
this._hooks = []
}

getter (key, value, singleton) {
this._getters.push({ key, value, singleton })
}

macro (key, value) {
this._macros.push({ key, value })
}

before (fn) {
this._hooks.push({ fn })
}
}

test.group('Vow request', () => {
test('add session getter to the request', (assert) => {
const req = new Request()
VowRequest(req, new Config())
assert.lengthOf(req._getters, 1)
assert.equal(req._getters[0].key, '_session')
assert.instanceOf(req._getters[0].value(), SessionClient)
})

test('add macro to the request', (assert) => {
const req = new Request()
VowRequest(req, new Config())
assert.lengthOf(req._macros, 1)
assert.equal(req._macros[0].key, 'session')
})

test('add before hook', (assert) => {
const req = new Request()
VowRequest(req, new Config())
assert.lengthOf(req._hooks, 1)
assert.isFunction(req._hooks[0].fn)
})
})

0 comments on commit 04047d0

Please sign in to comment.