kk-koa-framework
Advanced tools
Comparing version 1.0.57 to 2.0.1
@@ -6,3 +6,3 @@ { | ||
"name": "kk-koa-framework", | ||
"version": "1.0.57", | ||
"version": "2.0.1", | ||
"description": "kk koa mvc webframework", | ||
@@ -9,0 +9,0 @@ "main": "index.js", |
@@ -1,102 +0,10 @@ | ||
const { | ||
randomBytes | ||
} = require('crypto') | ||
const Redis = require('ioredis') | ||
const debug = require('debug')('kk:session') | ||
const {RedisClient,RedisStore} = require('./redis') | ||
module.exports = (session = {}) => { | ||
let { | ||
port, | ||
host, | ||
password, | ||
db, | ||
ttl, | ||
prefix | ||
} = session || {} | ||
let redis = RedisClient.create(session,session.key).client | ||
let redis = null, | ||
pingSi, config = {} | ||
port && (config.port = port) | ||
host && (config.host = host) | ||
password && (config.password = password) | ||
db && (config.db = db) | ||
const connect = () => { | ||
redis = new Redis({ | ||
port, | ||
host, | ||
password, | ||
db, | ||
}) | ||
redis.on('error', (e) => { | ||
console.error('redis connect error:', e) | ||
try { | ||
redis.end() | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
redis = null | ||
connect() | ||
}) | ||
redis.ping() | ||
pingSi && clearInterval(pingSi) | ||
pingSi = setInterval(function () { | ||
redis.ping() | ||
}, 60 * 1000) | ||
} | ||
connect() | ||
class RedisStore { | ||
constructor() { | ||
this.sessions = new Map() | ||
this.__timer = new Map() | ||
this.ttl = ttl | ||
this.redis = redis | ||
this.prefix = prefix || 'SESSION' | ||
} | ||
getID(length) { | ||
return randomBytes(length).toString('hex') | ||
} | ||
async get(sid) { | ||
let id = `${this.prefix}:${sid}` | ||
let data = await this.redis.get(id) | ||
data && this.ttl && this.redis.expire(id, this.ttl) | ||
return JSON.parse(data) | ||
} | ||
async set(session, { | ||
sid = this.getID(24) | ||
} = {}) { | ||
try { | ||
let id = `${this.prefix}:${sid}` | ||
await this.redis.set(id, JSON.stringify(session)) | ||
this.ttl && this.redis.expire(id, this.ttl) | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
return sid | ||
} | ||
async refresh(sid){ | ||
let id = `${this.prefix}:${sid}` | ||
this.ttl && this.redis.expire(id, this.ttl) | ||
} | ||
async destroy(sid) { | ||
return await this.redis.del(`${this.prefix}:${sid}`) | ||
} | ||
} | ||
return async (ctx, next) => { | ||
@@ -111,4 +19,5 @@ | ||
const key = 'kk:session' | ||
const store = new RedisStore() | ||
const store = new RedisStore(redis,session) | ||
let opts = {} | ||
@@ -115,0 +24,0 @@ |
@@ -8,9 +8,21 @@ const fs = require('fs') | ||
let Session = require(path.join(__dirname,'../middleware/session')) | ||
let FastCache = require(path.join(__dirname,'../middleware/fastCache')) | ||
let Redis = require(path.join(__dirname,'../middleware/redis')) | ||
app.use(I18n(app.$$.CONFIG_DIR)) | ||
app.use(require(path.join(__dirname,'../middleware/cookie'))) | ||
if(app.$$.config.session !== false){ | ||
if(app.$$.config.redis){ | ||
Redis && Redis.RedisClient.create(app.$$.config.redis,'redis') | ||
} | ||
if(app.$$.config.session){ | ||
app.use(Session(app.$$.config.session)) | ||
} | ||
if(app.$$.config.fastCache){ | ||
app.use(FastCache(app.$$.config.fastCache)) | ||
} | ||
let dir = path.join(app.$$.SOURCE_DIR,'middleware') | ||
@@ -17,0 +29,0 @@ |
module.exports = { | ||
redis:{ | ||
host:'127.0.0.1', | ||
db:1, | ||
}, | ||
session:{}, | ||
fastCache:{ | ||
} | ||
} |
@@ -7,2 +7,3 @@ const {passport} = require('../../../index') | ||
ctx.session.save() | ||
ctx.cache.set('1312',{}) | ||
ctx.body = '11' | ||
@@ -9,0 +10,0 @@ } |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
36817
36
928
26