Comparing version 1.0.0 to 1.0.1
107
index.js
@@ -0,2 +1,107 @@ | ||
/**! | ||
* koa-redis - index.js | ||
* Copyright(c) 2015 | ||
* MIT Licensed | ||
* | ||
* Authors: | ||
* dead_horse <dead_horse@qq.com> (http://deadhorse.me) | ||
*/ | ||
module.exports = require('./lib/koa-redis'); | ||
'use strict'; | ||
/** | ||
* Module dependencies. | ||
*/ | ||
var EventEmitter = require('events').EventEmitter; | ||
var debug = require('debug')('koa-redis'); | ||
var Redis = require('redis'); | ||
var util = require('util'); | ||
/** | ||
* Initialize redis session middleware with `opts`: | ||
* | ||
* @param {Object} options | ||
* - {Object} client redis client | ||
* - {String} host redis connect host (with out options.client) | ||
* - {Number} port redis connect port (with out options.client) | ||
* - {String} socket redis connect socket (with out options.client) | ||
* - {String} db redis db | ||
* - {String} pass redis password | ||
*/ | ||
var RedisStore = module.exports = function (options) { | ||
if (!(this instanceof RedisStore)) { | ||
return new RedisStore(options); | ||
} | ||
EventEmitter.call(this); | ||
options = options || {}; | ||
var client; | ||
if (!options.client) { | ||
options.host = options.host || 'localhost'; | ||
options.port = options.port || options.socket || 6379; | ||
debug('Init redis with host: %s, port: %d', options.host, options.port); | ||
client = Redis.createClient(options.port, options.host, options); | ||
} else { | ||
client = options.client; | ||
} | ||
options.pass && client.auth(options.pass, function (err) { | ||
if (err) { | ||
throw err; | ||
} | ||
}); | ||
if (options.db) { | ||
client.select(options.db); | ||
client.on("connect", function() { | ||
client.send_anyways = true; | ||
client.select(options.db); | ||
client.send_anyways = false; | ||
}); | ||
} | ||
client.on('error', this.emit.bind(this, 'disconnect')); | ||
client.on('end', this.emit.bind(this, 'disconnect')); | ||
client.on('connect', this.emit.bind(this, 'connect')); | ||
//wrap redis | ||
this._redisClient = client; | ||
this.client = require('co-redis')(client); | ||
}; | ||
util.inherits(RedisStore, EventEmitter); | ||
RedisStore.prototype.get = function *(sid) { | ||
var data = yield this.client.get(sid); | ||
debug('get session: %s', data || 'none'); | ||
if (!data) { | ||
return null; | ||
} | ||
try { | ||
return JSON.parse(data.toString()); | ||
} catch (err) { | ||
// ignore err | ||
debug('parse session error: %s', err.message); | ||
} | ||
}; | ||
RedisStore.prototype.set = function *(sid, sess, ttl) { | ||
if (typeof ttl === 'number') { | ||
ttl = Math.ceil(ttl / 1000); | ||
} | ||
sess = JSON.stringify(sess); | ||
if (ttl) { | ||
debug('SETEX %s %s %s', sid, ttl, sess); | ||
yield this.client.setex(sid, ttl, sess); | ||
} else { | ||
debug('SET %s %s', sid, sess); | ||
yield this.client.set(sid, sess); | ||
} | ||
debug('SET %s complete', sid); | ||
}; | ||
RedisStore.prototype.destroy = function *(sid, sess) { | ||
debug('DEL %s', sid); | ||
yield this.client.del(sid); | ||
debug('DEL %s complete', sid); | ||
}; |
@@ -5,4 +5,5 @@ { | ||
"repository": "dead-horse/koa-redis", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"keywords": ["koa", "middleware", "session", "redis"], | ||
"files": "index.js", | ||
"author": "dead_horse <dead_horse@qq.com>", | ||
@@ -9,0 +10,0 @@ "dependencies": { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9133
95