Skip to content

Commit

Permalink
Merge pull request #9 from hapinessjs/rediss-tls-polyfil
Browse files Browse the repository at this point in the history
Rediss tls polyfil
  • Loading branch information
akanass committed Jun 5, 2018
2 parents 94d3c58 + 0b08c2e commit ddb4ce1
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 5 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ or

$ yarn add @hapiness/core @hapiness/redis rxjs
```

```javascript
"dependencies": {
"@hapiness/core": "^1.3.0",
Expand Down Expand Up @@ -135,7 +135,7 @@ export interface ClientOpts {
tls?: any;
prefix?: string;
retry_strategy?: RetryStrategy;
}
```
Expand All @@ -154,7 +154,7 @@ class FooProvider {
bar(): Observable<string> {
return this._redis.connection.get('my_key');
}

}

```
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hapiness/redis",
"version": "1.1.0",
"version": "1.2.0",
"description": "Hapiness module for redis",
"main": "commonjs/index.js",
"types": "index.d.ts",
Expand Down
5 changes: 5 additions & 0 deletions src/module/managers/redis-client.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as redis_commands from 'redis-commands';
import * as EventEmitter from 'events';

import { Observable } from 'rxjs';
import { URL } from 'url';

import { RedisConfig } from '../interfaces';
import { DefaultValues } from '../common';
Expand Down Expand Up @@ -62,6 +63,10 @@ export class RedisClientManager {
// Removed unused properties by RedisConfig
delete config.reconnect_interval;

if (config.url && config.url.match(/^rediss/) && !config.tls) {
config.tls = { servername: new URL(config.url).hostname };
}

// Create the redis client
this._config = config;
}
Expand Down
81 changes: 81 additions & 0 deletions test/unit/redis-client.manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,85 @@ export class RedisClientManagerTest {
}
);
}

@test('- Test url with rediss:// as protocol creates tls object')
testUrlRediss(done) {
const fakeInst = new FakeRedisClient();

Observable
.of(fakeInst)
.delay(new Date(Date.now() + 1500))
.map(_ => fakeInst.emit('ready'))
.subscribe();

const redisStub = mockRedisCreateConnection();
redisStub.returns(<any>fakeInst);

const manager = new RedisClientManager(
{
url: 'rediss://:pass_redis@toto',
db: 1
}
);

manager
.createClient()
.subscribe(
_ => {
redisStub.restore();
unit.object(redisStub.firstCall);
unit.array(redisStub.firstCall.args);
unit.object(redisStub.firstCall.args[0].tls).is({
servername: 'toto'
});
done();
},
err => {
redisStub.restore();
done(new Error('Should not be there'));
}
);
}

@test('- Test url with rediss:// as protocol do not create tls object if already provided')
testUrlRedissTls(done) {
const fakeInst = new FakeRedisClient();

Observable
.of(fakeInst)
.delay(new Date(Date.now() + 1500))
.map(_ => fakeInst.emit('ready'))
.subscribe();

const redisStub = mockRedisCreateConnection();
redisStub.returns(<any>fakeInst);

const manager = new RedisClientManager(
{
url: 'rediss://:pass_redis@toto',
db: 1,
tls: {
servername: 'toto2'
}
}
);

manager
.createClient()
.subscribe(
_ => {
redisStub.restore();
unit.object(redisStub.firstCall);
unit.array(redisStub.firstCall.args);
unit.object(redisStub.firstCall.args[0].tls).is({
servername: 'toto2'
});
done();
},
err => {
redisStub.restore();
done(new Error('Should not be there'));
}
);
}
}

0 comments on commit ddb4ce1

Please sign in to comment.