Skip to content
This repository
Browse code

Implemented require("connect-redis")(connect) for npm 1.x. Closes #23

super lame and ugly :(
  • Loading branch information...
commit 121e7adf9f18d331c64264fd40b5a5d0ded0a1ac 1 parent 0c4912a
TJ Holowaychuk authored June 02, 2011
1  .gitignore
... ...
@@ -0,0 +1 @@
  1
+node_modules
8  Readme.md
Source Rendered
@@ -7,8 +7,6 @@ connect-redis is a Redis session store backed by [node_redis](http://github.com/
7 7
 
8 8
 ## Installation
9 9
 
10  
-via npm:
11  
-
12 10
 	  $ npm install connect-redis
13 11
 
14 12
 ## Options
@@ -19,10 +17,12 @@ via npm:
19 17
   - `pass` Password for Redis authentication
20 18
   - ...    Remaining options passed to the redis `createClient()` method.
21 19
 
22  
-## Example
  20
+## Usage
  21
+
  22
+ Due to npm 1.x changes, we now need to pass connect to the function `connect-redis` exports in order to extend `connect.session.Store`:
23 23
 
24 24
     var connect = require('connect')
25  
-	 	  , RedisStore = require('connect-redis');
  25
+	 	  , RedisStore = require('connect-redis')(connect);
26 26
 
27 27
     connect.createServer(
28 28
       connect.cookieParser(),
220  lib/connect-redis.js
@@ -9,8 +9,7 @@
9 9
  * Module dependencies.
10 10
  */
11 11
 
12  
-var Store = require('connect').session.Store
13  
-  , redis = require('redis');
  12
+var redis = require('redis');
14 13
 
15 14
 /**
16 15
  * One day in seconds.
@@ -19,112 +18,125 @@ var Store = require('connect').session.Store
19 18
 var oneDay = 86400;
20 19
 
21 20
 /**
22  
- * Expose `RedisStore`.
23  
- */
24  
-
25  
-module.exports = RedisStore;
26  
-
27  
-/**
28  
- * Initialize RedisStore with the given `options`.
29  
- *
30  
- * @param {Object} options
31  
- * @api public
32  
- */
33  
-
34  
-function RedisStore(options) {
35  
-  options = options || {};
36  
-  Store.call(this, options);
37  
-  this.client = new redis.createClient(options.port, options.host, options);
38  
-  if (options.pass) {
39  
-    this.client.auth(options.pass, function(err){
40  
-      if (err) throw err;
41  
-    });    
42  
-  }
43  
-
44  
-  if (options.db) {
45  
-    var self = this;
46  
-    self.client.select(options.db);
47  
-  }
48  
-};
49  
-
50  
-/**
51  
- * Inherit from `Store`.
52  
- */
53  
-
54  
-RedisStore.prototype.__proto__ = Store.prototype;
55  
-
56  
-/**
57  
- * Attempt to fetch session by the given `sid`.
  21
+ * Return the `RedisStore` extending `connect`'s session Store.
58 22
  *
59  
- * @param {String} sid
60  
- * @param {Function} fn
  23
+ * @param {object} connect
  24
+ * @return {Function}
61 25
  * @api public
62 26
  */
63 27
 
64  
-RedisStore.prototype.get = function(sid, fn){
65  
-  this.client.get(sid, function(err, data){
  28
+module.exports = function(connect){
  29
+
  30
+  /**
  31
+   * Connect's Store.
  32
+   */
  33
+
  34
+  var Store = connect.session.Store;
  35
+
  36
+  /**
  37
+   * Initialize RedisStore with the given `options`.
  38
+   *
  39
+   * @param {Object} options
  40
+   * @api public
  41
+   */
  42
+
  43
+  function RedisStore(options) {
  44
+    options = options || {};
  45
+    Store.call(this, options);
  46
+    this.client = new redis.createClient(options.port, options.host, options);
  47
+    if (options.pass) {
  48
+      this.client.auth(options.pass, function(err){
  49
+        if (err) throw err;
  50
+      });    
  51
+    }
  52
+
  53
+    if (options.db) {
  54
+      var self = this;
  55
+      self.client.select(options.db);
  56
+    }
  57
+  };
  58
+
  59
+  /**
  60
+   * Inherit from `Store`.
  61
+   */
  62
+
  63
+  RedisStore.prototype.__proto__ = Store.prototype;
  64
+
  65
+  /**
  66
+   * Attempt to fetch session by the given `sid`.
  67
+   *
  68
+   * @param {String} sid
  69
+   * @param {Function} fn
  70
+   * @api public
  71
+   */
  72
+
  73
+  RedisStore.prototype.get = function(sid, fn){
  74
+    this.client.get(sid, function(err, data){
  75
+      try {
  76
+        if (!data) return fn();
  77
+        fn(null, JSON.parse(data.toString()));
  78
+      } catch (err) {
  79
+        fn(err);
  80
+      } 
  81
+    });
  82
+  };
  83
+
  84
+  /**
  85
+   * Commit the given `sess` object associated with the given `sid`.
  86
+   *
  87
+   * @param {String} sid
  88
+   * @param {Session} sess
  89
+   * @param {Function} fn
  90
+   * @api public
  91
+   */
  92
+
  93
+  RedisStore.prototype.set = function(sid, sess, fn){
66 94
     try {
67  
-      if (!data) return fn();
68  
-      fn(null, JSON.parse(data.toString()));
  95
+      var maxAge = sess.cookie.maxAge
  96
+        , ttl = 'number' == typeof maxAge
  97
+          ? maxAge / 1000 | 0
  98
+          : oneDay
  99
+        , sess = JSON.stringify(sess);
  100
+      this.client.setex(sid, ttl, sess, function(){
  101
+        fn && fn.apply(this, arguments);
  102
+      });
69 103
     } catch (err) {
70  
-      fn(err);
  104
+      fn && fn(err);
71 105
     } 
72  
-  });
73  
-};
74  
-
75  
-/**
76  
- * Commit the given `sess` object associated with the given `sid`.
77  
- *
78  
- * @param {String} sid
79  
- * @param {Session} sess
80  
- * @param {Function} fn
81  
- * @api public
82  
- */
83  
-
84  
-RedisStore.prototype.set = function(sid, sess, fn){
85  
-  try {
86  
-    var maxAge = sess.cookie.maxAge
87  
-      , ttl = 'number' == typeof maxAge
88  
-        ? maxAge / 1000 | 0
89  
-        : oneDay
90  
-      , sess = JSON.stringify(sess);
91  
-    this.client.setex(sid, ttl, sess, function(){
92  
-      fn && fn.apply(this, arguments);
93  
-    });
94  
-  } catch (err) {
95  
-    fn && fn(err);
96  
-  } 
97  
-};
98  
-
99  
-/**
100  
- * Destroy the session associated with the given `sid`.
101  
- *
102  
- * @param {String} sid
103  
- * @api public
104  
- */
105  
-
106  
-RedisStore.prototype.destroy = function(sid, fn){
107  
-  this.client.del(sid, fn);
108  
-};
109  
-
110  
-/**
111  
- * Fetch number of sessions.
112  
- *
113  
- * @param {Function} fn
114  
- * @api public
115  
- */
116  
-
117  
-RedisStore.prototype.length = function(fn){
118  
-  this.client.dbsize(fn);
119  
-};
120  
-
121  
-/**
122  
- * Clear all sessions.
123  
- *
124  
- * @param {Function} fn
125  
- * @api public
126  
- */
127  
-
128  
-RedisStore.prototype.clear = function(fn){
129  
-  this.client.flushdb(fn);
130  
-};
  106
+  };
  107
+
  108
+  /**
  109
+   * Destroy the session associated with the given `sid`.
  110
+   *
  111
+   * @param {String} sid
  112
+   * @api public
  113
+   */
  114
+
  115
+  RedisStore.prototype.destroy = function(sid, fn){
  116
+    this.client.del(sid, fn);
  117
+  };
  118
+
  119
+  /**
  120
+   * Fetch number of sessions.
  121
+   *
  122
+   * @param {Function} fn
  123
+   * @api public
  124
+   */
  125
+
  126
+  RedisStore.prototype.length = function(fn){
  127
+    this.client.dbsize(fn);
  128
+  };
  129
+
  130
+  /**
  131
+   * Clear all sessions.
  132
+   *
  133
+   * @param {Function} fn
  134
+   * @api public
  135
+   */
  136
+
  137
+  RedisStore.prototype.clear = function(fn){
  138
+    this.client.flushdb(fn);
  139
+  };
  140
+
  141
+  return RedisStore;
  142
+};
1  package.json
@@ -5,5 +5,6 @@
5 5
   "author": "TJ Holowaychuk <tj@vision-media.ca>",
6 6
   "main": "./index.js",
7 7
   "dependencies": { "redis": ">= 0.0.1" },
  8
+  "devDependencies": { "connect": "1.4.x" },
8 9
   "engines": { "node": ">= 0.1.98" }
9 10
 }
3  test.js
@@ -4,7 +4,8 @@
4 4
  */
5 5
 
6 6
 var assert = require('assert')
7  
-  , RedisStore = require('./');
  7
+  , connect = require('connect')
  8
+  , RedisStore = require('./')(connect);
8 9
 
9 10
 var store = new RedisStore;
10 11
 var store_alt = new RedisStore({ db: 15 });

0 notes on commit 121e7ad

Please sign in to comment.
Something went wrong with that request. Please try again.