koa-redis
Advanced tools
Comparing version 1.0.1 to 2.0.1
1.0.1 / 2015-08-20 | ||
2.0.1 / 2016-01-12 | ||
================== | ||
* fix: missing index.js | ||
2.0.0 / 2016-01-09 | ||
================== | ||
* update for new redis, update all deps, update tests and coverage, update docs | ||
1.0.1 / 2015-08-20 | ||
================== | ||
* make sure ttl is an integer, fixes #9 | ||
@@ -6,0 +16,0 @@ |
98
index.js
@@ -18,15 +18,16 @@ /**! | ||
var debug = require('debug')('koa-redis'); | ||
var Redis = require('redis'); | ||
var redis = require('redis'); | ||
var redisWrapper = require('co-redis'); | ||
var util = require('util'); | ||
/** | ||
* Initialize redis session middleware with `opts`: | ||
* Initialize redis session middleware with `opts` (see the README for more info): | ||
* | ||
* @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 | ||
* - {Object} client redis client (overides all other options except db and duplicate) | ||
* - {String} socket redis connect socket (DEPRECATED: use 'path' instead) | ||
* - {String} db redis db | ||
* - {Boolean} duplicate if own client object, will use node redis's duplicate function and pass other options | ||
* - {String} pass redis password (DEPRECATED: use 'auth_pass' instead) | ||
* - {Any} [any] all other options inclduing above are passed to node_redis | ||
*/ | ||
@@ -41,20 +42,24 @@ var RedisStore = module.exports = function (options) { | ||
var client; | ||
options.auth_pass = options.auth_pass || options.pass || null; // For backwards compatibility | ||
options.path = options.path || options.socket || null; // For backwards compatibility | ||
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); | ||
debug('Init redis new client'); | ||
client = redis.createClient(options); | ||
} else { | ||
client = options.client; | ||
if (options.duplicate) { // Duplicate client and update with options provided | ||
debug('Duplicating provided client with new options (if provided)'); | ||
var dupClient = options.client; | ||
delete options.client; | ||
delete options.duplicate; | ||
client = dupClient.duplicate(options); // Useful if you want to use the DB option without adjusting the client DB outside koa-redis | ||
} else { | ||
debug('Using provided client'); | ||
client = options.client; | ||
} | ||
} | ||
options.pass && client.auth(options.pass, function (err) { | ||
if (err) { | ||
throw err; | ||
} | ||
}); | ||
if (options.db) { | ||
debug('selecting db %s', options.db) | ||
client.select(options.db); | ||
client.on("connect", function() { | ||
client.on('connect', function() { | ||
client.send_anyways = true; | ||
@@ -65,9 +70,47 @@ client.select(options.db); | ||
} | ||
client.on('error', this.emit.bind(this, 'disconnect')); | ||
client.on('end', this.emit.bind(this, 'disconnect')); | ||
client.on('error', this.emit.bind(this, 'error')); | ||
client.on('end', this.emit.bind(this, 'end')); | ||
client.on('end', this.emit.bind(this, 'disconnect')); // For backwards compatibility | ||
client.on('connect', this.emit.bind(this, 'connect')); | ||
client.on('reconnecting', this.emit.bind(this, 'reconnecting')); | ||
client.on('ready', this.emit.bind(this, 'ready')); | ||
client.on('drain', this.emit.bind(this, 'drain')); | ||
client.on('idle', this.emit.bind(this, 'idle')); | ||
this.on('connect', function() { | ||
debug('connected to redis'); | ||
this.connected = client.connected; | ||
}); | ||
this.on('ready', function() { | ||
debug('redis ready'); | ||
}); | ||
this.on('end', function() { | ||
debug('redis ended'); | ||
this.connected = client.connected; | ||
}); | ||
// No good way to test error | ||
/* istanbul ignore next */ | ||
this.on('error', function() { | ||
debug('redis error'); | ||
this.connected = client.connected; | ||
}); | ||
// No good way to test reconnect | ||
/* istanbul ignore next */ | ||
this.on('reconnecting', function() { | ||
debug('redis reconnecting'); | ||
this.connected = client.connected; | ||
}); | ||
this.on('drain', function() { | ||
debug('redis drain'); | ||
this.connected = client.connected; | ||
}); | ||
this.on('idle', function() { | ||
debug('redis idle'); | ||
this.connected = client.connected; | ||
}); | ||
//wrap redis | ||
this._redisClient = client; | ||
this.client = require('co-redis')(client); | ||
this.client = redisWrapper(client); | ||
this.connected = client.connected; | ||
}; | ||
@@ -106,3 +149,3 @@ | ||
RedisStore.prototype.destroy = function *(sid, sess) { | ||
RedisStore.prototype.destroy = function *(sid) { | ||
debug('DEL %s', sid); | ||
@@ -112,1 +155,8 @@ yield this.client.del(sid); | ||
}; | ||
RedisStore.prototype.quit = function* () { // End connection SAFELY | ||
debug('quitting redis client'); | ||
yield this.client.quit(); | ||
}; | ||
RedisStore.prototype.end = RedisStore.prototype.quit; // End connection SAFELY. The real end() command should never be used, as it cuts off to queue. |
{ | ||
"name": "koa-redis", | ||
"description": "koa session with redis", | ||
"repository": "dead-horse/koa-redis", | ||
"version": "1.0.1", | ||
"keywords": ["koa", "middleware", "session", "redis"], | ||
"files": "index.js", | ||
"description": "koa session with redis using koa-generic-session", | ||
"repository": "koajs/koa-redis", | ||
"bugs": { | ||
"url": "https://github.com/koajs/koa-redis/issues" | ||
}, | ||
"homepage": "https://github.com/koajs/koa-redis", | ||
"version": "2.0.1", | ||
"files": ["index.js"], | ||
"scripts": { | ||
"test": "istanbul cover node_modules/mocha/bin/_mocha -- --require co-mocha \"test/**/*.test.js\"", | ||
"test-only": "mocha --require co-mocha \"test/**/*.test.js\"", | ||
"autod": "autod -w --prefix=~" | ||
}, | ||
"keywords": [ | ||
"koa", | ||
"middleware", | ||
"session", | ||
"redis" | ||
], | ||
"author": "dead_horse <dead_horse@qq.com>", | ||
"dependencies": { | ||
"co-redis": "~1.1.0", | ||
"debug": "*", | ||
"redis": "~0.12.1" | ||
"co-redis": "~2.0.0", | ||
"debug": "~2.2.0", | ||
"redis": "~2.4.2" | ||
}, | ||
"devDependencies": { | ||
"autod": "~0.2.0", | ||
"blanket": "*", | ||
"co": "~3.0.6", | ||
"contributors": "*", | ||
"coveralls": "*", | ||
"koa": "~0.13.0", | ||
"koa-generic-session": "~1.0.0", | ||
"autod": "2", | ||
"co-mocha": "~1.1.2", | ||
"connect": "~3.4.0", | ||
"connect-redis": "~3.0.2", | ||
"istanbul": "~0.4.1", | ||
"koa": "~1.1.2", | ||
"koa-generic-session": "~1.10.1", | ||
"mocha": "*", | ||
"mocha-lcov-reporter": "*", | ||
"should": "~4.0.4", | ||
"supertest": "0.8.2", | ||
"travis-cov": "*" | ||
"should": "~8.0.2" | ||
}, | ||
"optionalDependencies": { | ||
"hiredis": "~0.4.1" | ||
}, | ||
"engines": { | ||
"node": ">= 0.11.9" | ||
"node": ">= 4" | ||
}, | ||
"license": "MIT" | ||
} |
165
README.md
@@ -1,6 +0,31 @@ | ||
koa-redis [![Build Status](https://secure.travis-ci.org/koajs/koa-redis.svg)](http://travis-ci.org/koajs/koa-redis) [![Dependency Status](https://gemnasium.com/koajs/koa-redis.svg)](https://gemnasium.com/koajs/koa-redis) | ||
koa-redis | ||
========= | ||
Redis storage for koa session middleware / cache. | ||
[![NPM version][npm-image]][npm-url] | ||
[![build status][travis-image]][travis-url] | ||
[![Coveralls][coveralls-image]][coveralls-url] | ||
[![David deps][david-image]][david-url] | ||
[![node version][node-image]][node-url] | ||
[![npm download][download-image]][download-url] | ||
[![license][license-image]][license-url] | ||
[npm-image]: https://img.shields.io/npm/v/koa-redis.svg?style=flat-square | ||
[npm-url]: https://npmjs.org/package/koa-redis | ||
[travis-image]: https://img.shields.io/travis/koajs/koa-redis.svg?style=flat-square | ||
[travis-url]: https://travis-ci.org/koajs/koa-redis | ||
[coveralls-image]: https://img.shields.io/coveralls/koajs/koa-redis.svg?style=flat-square | ||
[coveralls-url]: https://coveralls.io/r/koajs/koa-redis?branch=master | ||
[david-image]: https://img.shields.io/david/koajs/koa-redis.svg?style=flat-square | ||
[david-url]: https://david-dm.org/koajs/koa-redis | ||
[node-image]: https://img.shields.io/node/v/koa-redis.svg?style=flat-square | ||
[node-url]: http://nodejs.org/download/ | ||
[download-image]: https://img.shields.io/npm/dm/koa-redis.svg?style=flat-square | ||
[download-url]: https://npmjs.org/package/koa-redis | ||
[gittip-image]: https://img.shields.io/gittip/dead-horse.svg?style=flat-square | ||
[gittip-url]: https://www.gittip.com/dead-horse/ | ||
[license-image]: https://img.shields.io/npm/l/koa-redis.svg?style=flat-square | ||
[license-url]: https://github.com/koajs/koa-redis/blob/master/LICENSE | ||
Redis storage for koa session middleware/cache. | ||
[![NPM](https://nodei.co/npm/koa-redis.svg?downloads=true)](https://nodei.co/npm/koa-redis/) | ||
@@ -10,41 +35,115 @@ | ||
`koa-redis` works with [koa-generic-session](https://github.com/koajs/generic-session)(a generic session middleware for koa). | ||
`koa-redis` works with [koa-generic-session](https://github.com/koajs/generic-session) (a generic session middleware for koa). | ||
### Example | ||
```javascript | ||
var koa = require('koa'); | ||
var http = require('http'); | ||
```js | ||
var session = require('koa-generic-session'); | ||
var redisStore = require('koa-redis'); | ||
var koa = require('koa'); | ||
var app = koa(); | ||
app.name = 'koa-session-test'; | ||
app.keys = ['keys', 'keykeys']; | ||
app.use(session({ | ||
store: redisStore() | ||
store: redisStore({ | ||
// Options specified here | ||
}) | ||
})); | ||
app.use(function *() { | ||
this.session.name = 'koa-redis'; | ||
this.body = this.session.name; | ||
switch (this.path) { | ||
case '/get': | ||
get.call(this); | ||
break; | ||
case '/remove': | ||
remove.call(this); | ||
break; | ||
case '/regenerate': | ||
yield regenerate.call(this); | ||
break; | ||
} | ||
}); | ||
var app = module.exports = http.createServer(app.callback()); | ||
function get() { | ||
var session = this.session; | ||
session.count = session.count || 0; | ||
session.count++; | ||
this.body = session.count; | ||
} | ||
function remove() { | ||
this.session = null; | ||
this.body = 0; | ||
} | ||
function *regenerate() { | ||
get.call(this); | ||
yield this.regenerateSession(); | ||
get.call(this); | ||
} | ||
app.listen(8080); | ||
``` | ||
For more examples, please see the [examples folder of `koa-generic-session`](https://github.com/koajs/generic-session/tree/master/example). | ||
### Options | ||
- *all [`node_redis`](https://www.npmjs.com/package/redis#options-is-an-object-with-the-following-possible-properties) options* - Useful things include `url`, `host`, `port`, and `path` to the server. Defaults to `127.0.0.1:6379` | ||
- `db` (number) - will run `client.select(db)` after connection | ||
- `client` (object) - supply your own client, all other options are ignored unless `duplicate` is also supplied | ||
- `duplicate` (boolean) - When true, it will run `client.duplicate(options)` on the supplied `client` and use all other options supplied. This is useful if you want to select a different DB for sessions but also want to base from the same client object. | ||
- **DEPRECATED:** old options - `pass` and `socket` have been replaced by `auth_pass` and `path`, but they should be backwards compatible (still work). | ||
### Events | ||
See the [`node_redis` docs](https://www.npmjs.com/package/redis#connection-events) for more info. | ||
- `ready` | ||
- `connect` | ||
- `reconnecting` | ||
- `error` | ||
- `end` | ||
- `drain` | ||
- `idle` | ||
### API | ||
These are some the funcitons that `koa-generic-session` uses that you can use manually. You will need to inintialize differently than the example above: | ||
```js | ||
var session = require('koa-generic-session'); | ||
var redisStore = require('koa-redis')({ | ||
// Options specified here | ||
}); | ||
var app = require('koa')(); | ||
app.keys = ['keys', 'keykeys']; | ||
app.use(session({ | ||
store: redisStore | ||
})); | ||
``` | ||
* {Object} client redis client | ||
* {String} host redis connect host (without options.client) | ||
* {Number} port redis connect port (without options.client) | ||
* {String} socket redis connect socket (without options.client) | ||
* {String} db redis db | ||
* {String} pass redis password | ||
``` | ||
#### module([options]) | ||
Initialize the Redis connection with the optionally provided options (see above). *The variable `session` below references this*. | ||
#### session.get(sid) | ||
Generator that gets a session by ID. Returns parsed JSON is exists, `null` if it does not exist, and nothing upon error. | ||
#### session.set(sid, sess, ttl) | ||
Generator that sets a JSON session by ID with an optional time-to-live (ttl) in milliseconds. Yields `node_redis`'s `client.set()` or `client.setex()`. | ||
#### session.destroy(sid) | ||
Generator that destroys a session (removes it from Redis) by ID. Tields `node_redis`'s `client.del()`. | ||
#### session.quit() | ||
Generator that stops a Redis session after everything in the queue has completed. Yields `node_redis`'s `client.quit()`. | ||
#### session.end() | ||
Alias to `session.quit()`. It is not safe to use the real end function, as it cuts of the queue. | ||
#### session.connected | ||
Boolean giving the connection status updated using `client.connected` after any of the events above is fired. | ||
#### session.\_redisClient | ||
Direct access to the `node_redis` client object. | ||
#### session.client | ||
Direct access to the `co-redis` wrapper around the `node_redis` client. | ||
## Benchmark | ||
@@ -59,25 +158,17 @@ | ||
Detail [benchmark report](https://github.com/dead-horse/koa-redis/tree/master/benchmark) here | ||
Detailed benchmark report [here](https://github.com/koajs/koa-redis/tree/master/benchmark) | ||
## Testing | ||
1. Start a Redis server on `localhost:6379`. You can use [`redis-windows`](https://github.com/ServiceStack/redis-windows) if you are on Windows or just want a quick VM-based server. | ||
2. Clone the repository and run `npm i` in it (Windows should work fine). | ||
3. If you want to see debug output, turn on the prompt's `DEBUG` flag. | ||
4. Run `npm test` to run the tests and generate coverage. To run the tests without generating coverage, run `npm run-script test-only`. | ||
## Authors | ||
See the [contributing tab](https://github.com/koajs/koa-redis/graphs/contributors) | ||
``` | ||
$ git summary | ||
project : koa-redis | ||
repo age : 6 months ago | ||
commits : 34 | ||
active : 11 days | ||
files : 16 | ||
authors : | ||
27 dead_horse 79.4% | ||
3 Jesse Yang 8.8% | ||
3 sipajahava 8.8% | ||
1 Alessandro Lensi 2.9% | ||
``` | ||
## Licences | ||
(The MIT License) | ||
Copyright (c) 2013 dead-horse and other contributors | ||
Copyright (c) 2015 dead-horse and other contributors | ||
@@ -84,0 +175,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
Floating dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
15837
9
143
0
0
178
5
5
+ Addedbindings@1.5.0(transitive)
+ Addedco-redis@2.0.0(transitive)
+ Addeddebug@2.2.0(transitive)
+ Addeddouble-ended-queue@2.1.0-0(transitive)
+ Addedfile-uri-to-path@1.0.0(transitive)
+ Addedhiredis@0.4.1(transitive)
+ Addedms@0.7.1(transitive)
+ Addednan@2.19.0(transitive)
+ Addedredis@2.4.2(transitive)
+ Addedredis-commands@1.7.0(transitive)
- Removedco-redis@1.1.1(transitive)
- Removeddebug@4.3.4(transitive)
- Removedms@2.1.2(transitive)
- Removedredis@0.12.1(transitive)
Updatedco-redis@~2.0.0
Updateddebug@~2.2.0
Updatedredis@~2.4.2