-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build module for not rely on babel and harmony
- Loading branch information
Showing
6 changed files
with
192 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
|
||
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { return step("next", value); }, function (err) { return step("throw", err); }); } } return step("next"); }); }; } | ||
|
||
module.exports = class MemoryStore { | ||
constructor() { | ||
this.sessions = {}; | ||
} | ||
|
||
get(sid) { | ||
var _this = this; | ||
|
||
return _asyncToGenerator(function* () { | ||
return _this.sessions[sid]; | ||
})(); | ||
} | ||
|
||
set(sid, val, ttl) { | ||
var _this2 = this; | ||
|
||
return _asyncToGenerator(function* () { | ||
// eslint-disable-line no-unused-vars | ||
_this2.sessions[sid] = val; | ||
})(); | ||
} | ||
|
||
destroy(sid) { | ||
var _this3 = this; | ||
|
||
return _asyncToGenerator(function* () { | ||
delete _this3.sessions[sid]; | ||
})(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
'use strict'; | ||
|
||
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { return step("next", value); }, function (err) { return step("throw", err); }); } } return step("next"); }); }; } | ||
|
||
const uid = require('uid-safe'); | ||
const deepEqual = require('deep-equal'); | ||
const Store = require('./store'); | ||
const MemoryStore = require('./memory_store'); | ||
|
||
const ONE_DAY = 24 * 3600 * 1000; // one day in milliseconds | ||
|
||
const deleteSession = (ctx, key, ckOption, store, sid) => { | ||
const deleteOption = Object.assign({}, ckOption); | ||
delete deleteOption.maxAge; | ||
ctx.cookies.set(key, null, deleteOption); | ||
store.destroy(`${ key }:${ sid }`); | ||
}; | ||
|
||
const saveSession = (ctx, key, ckOption, store, sid) => { | ||
const ttl = ckOption.maxAge > 0 ? ckOption.maxAge : ONE_DAY; | ||
ctx.cookies.set(key, sid, ckOption); | ||
store.set(`${ key }:${ sid }`, ctx.session, ttl); | ||
}; | ||
|
||
module.exports = options => { | ||
const opt = options || {}; | ||
const key = opt.key || 'koa:sess'; | ||
const store = new Store(opt.store || new MemoryStore()); | ||
const cookie = opt.cookie || {}; | ||
|
||
return (() => { | ||
var _ref = _asyncToGenerator(function* (ctx, next) { | ||
// setup cookie options | ||
const ckOption = { | ||
maxAge: 0, // default to use session cookie | ||
path: '/', | ||
secure: false, | ||
httpOnly: true | ||
}; | ||
Object.assign(ckOption, cookie); | ||
Object.assign(ckOption, { | ||
overwrite: true, // overwrite previous session cookie changes | ||
signed: false }); | ||
if (!(ckOption.maxAge >= 0)) ckOption.maxAge = 0; | ||
|
||
// initialize session id and data | ||
const cookieSid = ctx.cookies.get(key); | ||
|
||
let sid = cookieSid; | ||
if (!sid) { | ||
sid = uid.sync(24); | ||
ctx.session = {}; | ||
} else { | ||
ctx.session = yield store.get(`${ key }:${ sid }`); | ||
if (!ctx.session || typeof ctx.session !== 'object') { | ||
ctx.session = {}; | ||
} | ||
} | ||
|
||
const sessionClone = JSON.parse(JSON.stringify(ctx.session)); | ||
|
||
// expose session handler to ctx | ||
ctx.sessionHandler = { | ||
getId: function () { | ||
return sid; | ||
}, | ||
regenerateId: function () { | ||
sid = uid.sync(24); | ||
}, | ||
setMaxAge: function (ms) { | ||
ckOption.maxAge = ms >= 0 ? ms : 0; | ||
} | ||
}; | ||
|
||
yield next(); | ||
|
||
const sessionHasData = ctx.session && Object.keys(ctx.session).length; | ||
|
||
if (sid !== cookieSid) { | ||
// a new session id | ||
// clean old session | ||
if (cookieSid) deleteSession(ctx, key, ckOption, store, cookieSid); | ||
|
||
// save new session | ||
if (sessionHasData) saveSession(ctx, key, ckOption, store, sid); | ||
} else { | ||
// an existing session | ||
// data has not been changed | ||
if (deepEqual(ctx.session, sessionClone)) return; | ||
|
||
// update session data | ||
if (sessionHasData) { | ||
saveSession(ctx, key, ckOption, store, sid); | ||
} else { | ||
deleteSession(ctx, key, ckOption, store, sid); | ||
} | ||
} | ||
}); | ||
|
||
return function (_x, _x2) { | ||
return _ref.apply(this, arguments); | ||
}; | ||
})(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
const co = require('co'); | ||
|
||
module.exports = class Store { | ||
constructor(store) { | ||
this.store = store; | ||
} | ||
|
||
get(sid) { | ||
return co(this.store.get(sid)); | ||
} | ||
|
||
set(sid, val, ttl) { | ||
return co(this.store.set(sid, val, ttl)); | ||
} | ||
|
||
destroy(sid) { | ||
return co(this.store.destroy(sid)); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const Koa = require('koa') | ||
const session = require('..') | ||
const redisStore = require('koa-redis') | ||
|
||
const app = new Koa() | ||
|
||
app.use(session({ | ||
store: redisStore(), | ||
})) | ||
|
||
// update session count | ||
app.use(async (ctx, next) => { | ||
ctx.session.count = ctx.session.count || 0 | ||
if (ctx.path === '/add') ctx.session.count++ | ||
await next() | ||
}) | ||
|
||
// populate response body | ||
app.use(ctx => { | ||
ctx.body = ctx.sessionHandler.getId() + ' : ' + ctx.session.count | ||
}) | ||
|
||
app.listen(3000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require('babel-register')({ | ||
plugins: ['transform-async-to-generator'], | ||
}) | ||
|
||
require('./counter.js') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters