alaska-middleware-session
Advanced tools
Comparing version 0.11.0 to 0.12.0
68
index.js
@@ -1,19 +0,17 @@ | ||
// @flow | ||
'use strict'; | ||
/* eslint global-require:0 */ | ||
/* eslint import/no-dynamic-require:0 */ | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
import alaska from 'alaska'; | ||
import random from 'string-random'; | ||
import pathToRegexp from 'path-to-regexp'; | ||
import Session from './session'; | ||
export default function (options: Alaska$Config$session) { | ||
exports.default = function (options) { | ||
const storeOpts = options.store || {}; | ||
const cookieOpts = options.cookie || {}; | ||
const key: string = cookieOpts.key || 'alaska.sid'; | ||
// $Flow require参数需要为字符串 | ||
const Store = require(storeOpts.type).default; | ||
const store = new Store(alaska.main, storeOpts); | ||
let ignore: ?RegExp[] = null; | ||
const key = cookieOpts.key || 'alaska.sid'; | ||
const Store = _alaska2.default.modules.drivers[storeOpts.type]; | ||
if (!Store) { | ||
_alaska2.default.panic(`Session store driver '${storeOpts.type}' not found!`); | ||
} | ||
const store = new Store(_alaska2.default.main, storeOpts); | ||
let ignore = null; | ||
@@ -23,3 +21,3 @@ function convert(input) { | ||
// $Flow | ||
ignore.push(pathToRegexp(input)); | ||
ignore.push((0, _pathToRegexp2.default)(input)); | ||
} else if (input.test) { | ||
@@ -42,3 +40,3 @@ // $Flow | ||
return async function sessionMiddleware(ctx: Alaska$Context, next: Function) { | ||
return async function sessionMiddleware(ctx, next) { | ||
if (ignore) { | ||
@@ -68,3 +66,3 @@ for (let reg of ignore) { | ||
} else { | ||
ctx.sessionId = random(24); | ||
ctx.sessionId = (0, _stringRandom2.default)(24); | ||
sid = ctx.sessionId; | ||
@@ -81,3 +79,3 @@ if (cookieOpts && cookieOpts.set) { | ||
try { | ||
session = new Session(ctx, json); | ||
session = new _session2.default(ctx, json); | ||
} catch (err) { | ||
@@ -87,6 +85,6 @@ if (!(err instanceof SyntaxError)) { | ||
} | ||
session = new Session(ctx, {}); | ||
session = new _session2.default(ctx, {}); | ||
} | ||
} else { | ||
session = new Session(ctx, {}); | ||
session = new _session2.default(ctx, {}); | ||
} | ||
@@ -102,4 +100,10 @@ | ||
set(val) { | ||
if (val === null) return (session = false); | ||
if (typeof val === 'object') return (session = new Session(ctx, val)); | ||
if (val === null) { | ||
session = false; | ||
return; | ||
} | ||
if (typeof val === 'object') { | ||
session = new _session2.default(ctx, val); | ||
return; | ||
} | ||
throw new Error('ctx.session can only be set as null or an object.'); | ||
@@ -136,2 +140,20 @@ } | ||
}; | ||
} | ||
}; | ||
var _alaska = require('alaska'); | ||
var _alaska2 = _interopRequireDefault(_alaska); | ||
var _stringRandom = require('string-random'); | ||
var _stringRandom2 = _interopRequireDefault(_stringRandom); | ||
var _pathToRegexp = require('path-to-regexp'); | ||
var _pathToRegexp2 = _interopRequireDefault(_pathToRegexp); | ||
var _session = require('./session'); | ||
var _session2 = _interopRequireDefault(_session); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } |
{ | ||
"name": "alaska-middleware-session", | ||
"version": "0.11.0", | ||
"version": "0.12.0", | ||
"description": "Alaska session middleware", | ||
@@ -9,4 +9,5 @@ "keywords": [ | ||
], | ||
"alaska": "middleware", | ||
"main": "index.js", | ||
"repository": "https://github.com/maichong/alaska/tree/master/packages/alaska-meddleware-session", | ||
"repository": "https://github.com/maichong/alaska/tree/master/src/alaska-meddleware-session", | ||
"author": "Liang <liang@maichong.it> (https://github.com/liangxingchen)", | ||
@@ -13,0 +14,0 @@ "license": "MIT", |
@@ -1,3 +0,8 @@ | ||
// @flow | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
/** | ||
@@ -8,5 +13,3 @@ * @class Session | ||
*/ | ||
export default class Session { | ||
_ctx: Alaska$Context; | ||
isNew: boolean; | ||
class Session { | ||
@@ -18,3 +21,3 @@ /** | ||
*/ | ||
constructor(ctx: Alaska$Context, obj?: Object) { | ||
constructor(ctx, obj) { | ||
this._ctx = ctx; | ||
@@ -31,3 +34,3 @@ if (!obj) { | ||
get length(): number { | ||
get length() { | ||
return Object.keys(this.toJSON()).length; | ||
@@ -40,6 +43,6 @@ } | ||
*/ | ||
toJSON(): Object { | ||
toJSON() { | ||
let me = this; | ||
let obj = {}; | ||
Object.keys(this).forEach((key) => { | ||
Object.keys(this).forEach(key => { | ||
if (key === 'isNew' || key[0] === '_') return; | ||
@@ -57,3 +60,3 @@ // $Flow | ||
*/ | ||
isChanged(prev: string) { | ||
isChanged(prev) { | ||
if (!prev) { | ||
@@ -65,1 +68,2 @@ return true; | ||
} | ||
exports.default = Session; |
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
5390
186
1