Просмотр исходного кода

pass entire redis object though with all opts in one go

Henry Oswald 12 лет назад
Родитель
Сommit
a0c861cf5d

+ 13 - 4
libraries/redis-wrapper/index.coffee

@@ -1,10 +1,19 @@
+_ = require("underscore")
+
 module.exports =
 
-	createClient: ()->
-		if arguments[0] instanceof Array
-			client = require("redis-sentinel").createClient.apply null, arguments
+	createClient: (opts)->
+
+		if opts.endpoints?
+			standardOpts = _.clone(opts)
+			delete standardOpts.endpoints
+			delete standardOpts.masterName
+			client = require("redis-sentinel").createClient opts.endpoints, opts.masterName, standardOpts
 		else
-			client = require("redis").createClient.apply null, arguments
+			standardOpts = _.clone(opts)
+			delete standardOpts.port
+			delete standardOpts.host
+			client = require("redis").createClient opts.port, opts.host, standardOpts
 		return client
 
 

+ 14 - 16
libraries/redis-wrapper/package.json

@@ -1,23 +1,21 @@
 {
   "name": "redis-sharelatex",
-  "version": "0.0.0",
-  "description": "",
+  "version": "0.0.1",
+  "description": "redis wrapper for node which will either use sentinal or normal redis",
   "main": "index.coffee",
-  "scripts": {
-    "test": "echo \"Error: no test specified\" && exit 1"
-  },
-  "author": "",
+  "author": "henry oswald @ sharelatex",
   "license": "ISC",
   "dependencies": {
-    "chai": "^1.9.1",
-    "coffee-script": "^1.8.0",
-    "grunt": "^0.4.5",
-    "grunt-contrib-coffee": "^0.11.1",
-    "grunt-mocha-test": "^0.12.0",
-    "mocha": "^1.21.4",
-    "redis": "^0.12.1",
-    "redis-sentinel": "^0.1.1",
-    "sandboxed-module": "^1.0.1",
-    "sinon": "^1.10.3"
+    "chai": "1.9.1",
+    "coffee-script": "1.8.0",
+    "grunt": "0.4.5",
+    "grunt-contrib-coffee": "0.11.1",
+    "grunt-mocha-test": "0.12.0",
+    "mocha": "1.21.4",
+    "redis": "0.12.1",
+    "redis-sentinel": "0.1.1",
+    "sandboxed-module": "1.0.1",
+    "sinon": "1.10.3",
+    "underscore": "^1.7.0"
   }
 }

+ 19 - 13
libraries/redis-wrapper/test.coffee

@@ -21,8 +21,7 @@ describe "index", ->
 		@redis = SandboxedModule.require modulePath, requires:
 			"redis-sentinel":@sentinel
 			"redis":@normalRedis
-		@standardOpts =
-			auth_pass: "my password"
+		@auth_pass = "1234 pass"
 
 	describe "sentinel", ->
 
@@ -32,34 +31,41 @@ describe "index", ->
 				{host: '127.0.0.1', port: 26380}
 			]
 			@masterName = "my master"
+			@sentinelOptions =
+				endpoints:@endpoints
+				masterName:@masterName
+				auth_pass:@auth_pass
 
 		it "should use sentinal if the first argument in an array", ->
-
-			client = @redis.createClient @endpoints, @masterName, @standardOpts
+			client = @redis.createClient @sentinelOptions
 			@sentinel.createClient.called.should.equal true
 			@normalRedis.createClient.called.should.equal false
 			client.should.equal @sentinelClient
 
 		it "should pass the options correctly though", ->
-			client = @redis.createClient @endpoints, @masterName, @standardOpts
-			@sentinel.createClient.calledWith(@endpoints, @masterName, @standardOpts).should.equal true
+			client = @redis.createClient @sentinelOptions
+			@sentinel.createClient.calledWith(@endpoints, @masterName, auth_pass:@auth_pass).should.equal true
 			client.should.equal @sentinelClient
 
 	describe "normal redis", ->
 
 		beforeEach ->
-			@port = 1234
-			@host = "redis.mysite.env"
+			@standardOpts =
+				auth_pass: @auth_pass
+				port: 1234
+				host: "redis.mysite.env"
 
 		it "should use the normal redis driver if a non array is passed", ->
-
-			client = @redis.createClient @port, @host, @standardOpts
+			client = @redis.createClient @standardOpts
 			@sentinel.createClient.called.should.equal false
 			@normalRedis.createClient.called.should.equal true
 			client.should.equal @normalRedisClient
 
-
 		it "should use the normal redis driver if a non array is passed", ->
+			client = @redis.createClient @standardOpts
+			@normalRedis.createClient.calledWith(@standardOpts.port, @standardOpts.host, auth_pass:@auth_pass).should.equal true
+
+
+
+
 
-			client = @redis.createClient @port, @host, @standardOpts
-			@normalRedis.createClient.calledWith(@port, @host, @standardOpts).should.equal true