Skip to content

Commit

Permalink
update package and fix arrow function eslint error
Browse files Browse the repository at this point in the history
  • Loading branch information
lzztt committed Sep 15, 2016
1 parent 0ca89ce commit bccb59a
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 54 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const redisStore = require('koa-redis')
const app = new Koa()

app.use(session({
store: redisStore(),
store: redisStore()
}))

// count middleware, increment when url = /add
Expand Down Expand Up @@ -87,9 +87,9 @@ When setting `cookie` option to a plain object, all sessions will use the same c
You may use different `maxAge` for user and guest sessions, by initializing the session middleware as below:
```javascript
session({
cookie: ctx => {
cookie: (ctx) => {
return {
maxAge: ctx.session.user ? ONE_MONTH : ONE_DAY,
maxAge: ctx.session.user ? ONE_MONTH : 0
}
}
})
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
"babel-register": "^6.14.0",
"chai": "^3.5.0",
"eslint": "^3.4.0",
"eslint-config-airbnb-base": "^5.0.3",
"eslint-plugin-import": "^1.14.0",
"eslint-config-airbnb-base": "^7.1.0",
"eslint-plugin-import": "^1.15.0",
"istanbul": "^0.4.5",
"koa": "^2.0.0-alpha.6",
"koa": "^2.0.0-alpha.7",
"mocha": "^3.0.2",
"supertest": "^2.0.0"
},
Expand Down
6 changes: 3 additions & 3 deletions src/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const MemoryStore = require('./memory_store')

const ONE_DAY = 24 * 3600 * 1000 // one day in milliseconds

const cookieOpt = cookie => {
const cookieOpt = (cookie) => {
const options = Object.assign({
maxAge: 0, // default to use session cookie
path: '/',
Expand All @@ -32,13 +32,13 @@ const saveSession = (ctx, key, cookie, store, sid) => {
store.set(`${key}:${sid}`, ctx.session, ttl)
}

module.exports = options => {
module.exports = (options) => {
const opt = options || {}
const key = opt.key || 'koa:sess'
const store = new Store(opt.store || new MemoryStore())
const defaultCookie = opt.cookie instanceof Function ? opt.cookie : cookieOpt(opt.cookie)

return async (ctx, next) => {
return async (ctx, next) => { // eslint-disable-line arrow-parens
// initialize session id and data
const cookieSid = ctx.cookies.get(key)

Expand Down
32 changes: 16 additions & 16 deletions test/async_store.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ const updateSession = (ctx, next) => {
next()
}

const sessionToBody = ctx => {
const sessionToBody = (ctx) => {
ctx.body = {
data: ctx.session,
}
}

const getCookies = res => {
const getCookies = (res) => {
const cookies = res.header['set-cookie']
if (cookies) {
return cookies.map(c => {
return cookies.map((c) => {
const match = c.match(/([^=]*)=([^;]*); path=([^;]*); /)
if (match) {
return {
Expand All @@ -55,7 +55,7 @@ const getCookies = res => {
return null
}

const populateSid = res => {
const populateSid = (res) => {
const cookies = getCookies(res)
res.body.sid = cookies ? cookies[0].value : null
}
Expand All @@ -74,7 +74,7 @@ const validateBody = (res, startTime) => {
}

const validateStoreCalls = (store, calls) => {
['get', 'set', 'destroy'].forEach(m => {
['get', 'set', 'destroy'].forEach((m) => {
expect(store.calls[m]).to.be.deep.equal(calls[m])
})
store.clearCalls()
Expand Down Expand Up @@ -102,7 +102,7 @@ describe('session with async store', () => {
startTime = Date.now()
})

it('should work and not set cookie for empty session', done => {
it('should work and not set cookie for empty session', (done) => {
client.get('/').expect(200).end((err, res) => {
if (err) done(err)
validateBody(res, startTime)
Expand All @@ -116,7 +116,7 @@ describe('session with async store', () => {
done()
})

it('set session cookie when session has data', done => {
it('set session cookie when session has data', (done) => {
client.get('/set/time').expect(200).end((err, res) => {
if (err) done(err)
validateCookie(res, key)
Expand All @@ -132,7 +132,7 @@ describe('session with async store', () => {
})
})

it('should work when multiple clients access', done => {
it('should work when multiple clients access', (done) => {
Promise.all([
new Promise((resolve, reject) => {
client.get('/set/time').expect(200).end((err, res) => {
Expand All @@ -150,7 +150,7 @@ describe('session with async store', () => {
resolve(res.body)
})
}),
]).then(bodies => {
]).then((bodies) => {
expect(bodies[0].sid).to.be.not.equal(bodies[1].sid)
validateStoreCalls(store, {
get: [],
Expand All @@ -166,7 +166,7 @@ describe('session with async store', () => {
})
})

it('session data is available among multiple requests', done => {
it('session data is available among multiple requests', (done) => {
client.get('/set/time').expect(200).end((err1, res1) => {
if (err1) done(err1)
validateCookie(res1, key)
Expand Down Expand Up @@ -233,7 +233,7 @@ describe('session with async store', () => {
})
})

it('set ctx.session = {} should clear session data', done => {
it('set ctx.session = {} should clear session data', (done) => {
client.get('/set/time').expect(200).end((err1, res1) => {
if (err1) done(err1)
validateCookie(res1, key)
Expand Down Expand Up @@ -289,7 +289,7 @@ describe('session with async store', () => {
})
})

it('set ctx.session = null should clear session data', done => {
it('set ctx.session = null should clear session data', (done) => {
client.get('/set/time').expect(200).end((err1, res1) => {
if (err1) done(err1)
validateCookie(res1, key)
Expand Down Expand Up @@ -345,7 +345,7 @@ describe('session with async store', () => {
})
})

it('regenerateId() should regenerate session id', done => {
it('regenerateId() should regenerate session id', (done) => {
client.get('/set/time').expect(200).end((err1, res1) => {
if (err1) done(err1)
validateCookie(res1, key)
Expand Down Expand Up @@ -407,7 +407,7 @@ describe('async store with dynamic cookie options', () => {

app.use(session({
store,
cookie: ctx => { // eslint-disable-line arrow-body-style
cookie: (ctx) => { // eslint-disable-line arrow-body-style
return {
maxAge: ctx.ms,
}
Expand All @@ -426,7 +426,7 @@ describe('async store with dynamic cookie options', () => {
startTime = Date.now()
})

it('setMaxAge(ms) should set maxAge and ttl', done => {
it('setMaxAge(ms) should set maxAge and ttl', (done) => {
client.get('/maxage/0').expect(200).end((err1, res1) => {
if (err1) done(err1)
validateCookie(res1, key)
Expand Down Expand Up @@ -494,7 +494,7 @@ describe('async store with customized cookie options', () => {
startTime = Date.now()
})

it('negative maxAge value will be treated as 0 (default value)', done => {
it('negative maxAge value will be treated as 0 (default value)', (done) => {
client.get('/set/time').expect(200).end((err1, res1) => {
if (err1) done(err1)
validateCookie(res1, key)
Expand Down
26 changes: 13 additions & 13 deletions test/default_store.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ const updateSession = (ctx, next) => {
next()
}

const sessionToBody = ctx => {
const sessionToBody = (ctx) => {
ctx.body = {
data: ctx.session,
}
}

const getCookies = res => {
const getCookies = (res) => {
const cookies = res.header['set-cookie']
if (cookies) {
return cookies.map(c => {
return cookies.map((c) => {
const match = c.match(/([^=]*)=([^;]*); path=([^;]*); /)
if (match) {
return {
Expand All @@ -50,7 +50,7 @@ const getCookies = res => {
return null
}

const populateSid = res => {
const populateSid = (res) => {
const cookies = getCookies(res)
res.body.sid = cookies ? cookies[0].value : null
}
Expand Down Expand Up @@ -84,7 +84,7 @@ describe('session with default memory store', () => {
startTime = Date.now()
})

it('should work and not set cookie for empty session', done => {
it('should work and not set cookie for empty session', (done) => {
client.get('/').expect(200).end((err, res) => {
if (err) done(err)
validateBody(res, startTime)
Expand All @@ -93,7 +93,7 @@ describe('session with default memory store', () => {
done()
})

it('set session cookie when session has data', done => {
it('set session cookie when session has data', (done) => {
client.get('/set/time').expect(200).end((err, res) => {
if (err) done(err)
validateCookie(res, key)
Expand All @@ -102,7 +102,7 @@ describe('session with default memory store', () => {
})
})

it('should work when multiple clients access', done => {
it('should work when multiple clients access', (done) => {
Promise.all([
new Promise((resolve, reject) => {
client.get('/set/time').expect(200).end((err, res) => {
Expand All @@ -120,15 +120,15 @@ describe('session with default memory store', () => {
resolve(res.body)
})
}),
]).then(bodies => {
]).then((bodies) => {
expect(bodies[0].sid).to.be.not.equal(bodies[1].sid)
done()
}).catch((err) => {
done(err)
})
})

it('session data is available among multiple requests', done => {
it('session data is available among multiple requests', (done) => {
client.get('/set/time').expect(200).end((err1, res1) => {
if (err1) done(err1)
validateCookie(res1, key)
Expand Down Expand Up @@ -165,7 +165,7 @@ describe('session with default memory store', () => {
})
})

it('set ctx.session = {} should clear session data', done => {
it('set ctx.session = {} should clear session data', (done) => {
client.get('/set/time').expect(200).end((err1, res1) => {
if (err1) done(err1)
validateCookie(res1, key)
Expand Down Expand Up @@ -198,7 +198,7 @@ describe('session with default memory store', () => {
})
})

it('set ctx.session = null should clear session data', done => {
it('set ctx.session = null should clear session data', (done) => {
client.get('/set/time').expect(200).end((err1, res1) => {
if (err1) done(err1)
validateCookie(res1, key)
Expand Down Expand Up @@ -231,7 +231,7 @@ describe('session with default memory store', () => {
})
})

it('regenerateId() should regenerate session id', done => {
it('regenerateId() should regenerate session id', (done) => {
client.get('/set/time').expect(200).end((err1, res1) => {
if (err1) done(err1)
validateCookie(res1, key)
Expand Down Expand Up @@ -280,7 +280,7 @@ describe('default memory store with customized cookie options', () => {
startTime = Date.now()
})

it('negative maxAge value will be treated as 0 (default value)', done => {
it('negative maxAge value will be treated as 0 (default value)', (done) => {
client.get('/set/time').expect(200).end((err1, res1) => {
if (err1) done(err1)
validateCookie(res1, key)
Expand Down
Loading

0 comments on commit bccb59a

Please sign in to comment.