Skip to content

Commit 181d11c

Browse files
committed
Adapt eslint and whitespace conventions for typescript
1 parent 249aeca commit 181d11c

7 files changed

+209
-127
lines changed

.editorconfig

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ insert_final_newline = true
66
trim_trailing_whitespace = true
77
charset = utf-8
88

9+
[*.ts]
10+
indent_style = space
11+
indent_size = 4
12+
913
[*.js]
1014
indent_style = space
1115
indent_size = 2
1216

13-
[{package.json,*.yml,*.cjson}]
17+
[{*.json,*.yml,*.cjson}]
1418
indent_style = space
1519
indent_size = 2

.eslintrc

-30
This file was deleted.

.eslintrc.json

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"env": {
3+
"commonjs": true,
4+
"es6": true
5+
},
6+
"extends": [
7+
"standard",
8+
"eslint:recommended",
9+
"plugin:node/recommended",
10+
"plugin:promise/recommended",
11+
"plugin:@typescript-eslint/recommended"
12+
],
13+
"parser": "@typescript-eslint/parser",
14+
"parserOptions": {
15+
"project": "./tsconfig.json"
16+
},
17+
"plugins": [
18+
"@typescript-eslint",
19+
"promise"
20+
],
21+
"settings": {
22+
"import/resolver": {
23+
"node": {
24+
"extensions": [".js"]
25+
}
26+
}
27+
},
28+
"rules": {
29+
"no-await-in-loop": "warn",
30+
"no-process-exit": "warn",
31+
"no-tabs": "error",
32+
"no-var": "error",
33+
"semi": ["error", "never"],
34+
"eqeqeq": ["error", "always"],
35+
"node/no-unsupported-features/es-syntax": "off",
36+
"@typescript-eslint/no-non-null-assertion": "warn",
37+
"@typescript-eslint/explicit-function-return-type": [
38+
"error", {
39+
"allowExpressions": true
40+
}
41+
]
42+
}
43+
}

lib/redis.ts

+48-50
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,68 @@
11
import * as url from 'url'
22
import * as Redis from 'ioredis'
3-
import { ClusterOptions, RedisOptions } from 'ioredis'
43
import * as debug from 'debug'
54

65
const debugEvents = debug('monredis:events')
76
const debugInfo = debug('monredis:info')
87

98
/**
10-
* ioRedis has a bug where if the redis instance endpoint is supplied using
11-
* a connection uri; the password is passed only to the
12-
*/
13-
function getRedisPasswordFromURI (redisUri: string | string[]) : string {
14-
if (Array.isArray(redisUri)) {
15-
redisUri = redisUri[0]
16-
}
17-
const parsedURL = new url.URL(redisUri)
18-
return parsedURL.password
9+
* Extract password from connection URI
10+
*/
11+
function getRedisPasswordFromURI (redisUri: string | string[]): string {
12+
if (Array.isArray(redisUri)) {
13+
redisUri = redisUri[0]
14+
}
15+
const parsedURL = new url.URL(redisUri)
16+
return parsedURL.password
1917
}
2018

2119
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
22-
type MonredisClusterOptions = Omit<ClusterOptions, 'redisOptions'>
20+
type MonredisClusterOptions = Omit<Redis.ClusterOptions, 'redisOptions'>
2321

24-
const defaultNodeOptions: RedisOptions= {
25-
keyPrefix: '',
26-
autoResubscribe: true,
27-
autoResendUnfulfilledCommands: true,
22+
const defaultNodeOptions: Redis.RedisOptions= {
23+
keyPrefix: '',
24+
autoResubscribe: true,
25+
autoResendUnfulfilledCommands: true,
2826
}
2927

30-
const defaultClusterOptions: ClusterOptions = {
31-
retryDelayOnClusterDown: 300,
32-
retryDelayOnFailover: 1000,
33-
retryDelayOnTryAgain: 3000,
34-
slotsRefreshTimeout: 10000,
35-
clusterRetryStrategy: times => Math.min(times * 1000, 10000),
28+
const defaultClusterOptions: Redis.ClusterOptions = {
29+
retryDelayOnClusterDown: 300,
30+
retryDelayOnFailover: 1000,
31+
retryDelayOnTryAgain: 3000,
32+
slotsRefreshTimeout: 10000,
33+
clusterRetryStrategy: times => Math.min(times * 1000, 10000),
3634
}
3735

3836
function getClient (
39-
host: string | string[],
40-
cluster: boolean = false,
41-
userNodeOptions: RedisOptions = {},
42-
userClusterOptions: MonredisClusterOptions = {}
43-
) {
44-
const nodeOptions: RedisOptions = {...defaultNodeOptions, ...userNodeOptions}
45-
const clusterOptions: ClusterOptions = {...defaultClusterOptions, ...userClusterOptions}
46-
clusterOptions.redisOptions = nodeOptions
47-
let client
48-
if (cluster) {
49-
// Workaround for ioredis bug not passing password to all nodes if
50-
// endpoint is supplied using connection uri
51-
const password = getRedisPasswordFromURI(host)
52-
clusterOptions.redisOptions.password = password
53-
host = Array.isArray(host) ? host : [host]
54-
debugInfo('Creating new redis cluster instance')
55-
client = new Redis.Cluster(host, clusterOptions)
56-
} else {
57-
debugInfo('Creating new redis single node instance')
58-
client = new Redis(Array.isArray(host) ? host[0] : host, nodeOptions)
59-
}
60-
client.on('connecting', () => debugEvents('[connecting]'))
61-
client.on('connect', () => debugEvents('[connect]'))
62-
client.on('ready', () => debugEvents('[ready]'))
63-
client.on('error', (arg) => debugEvents('[error] with argument %O', arg))
64-
client.on('close', () => debugEvents('[close]'))
65-
client.on('reconnecting', () => debugEvents('[reconnecting]'))
66-
client.on('end', () => debugEvents('[end]'))
67-
return client
37+
host: string | string[],
38+
cluster: boolean = false,
39+
userNodeOptions: Redis.RedisOptions = {},
40+
userClusterOptions: MonredisClusterOptions = {}
41+
): Redis.Redis | Redis.Cluster {
42+
const nodeOptions: Redis.RedisOptions = {...defaultNodeOptions, ...userNodeOptions}
43+
const clusterOptions: Redis.ClusterOptions = {...defaultClusterOptions, ...userClusterOptions}
44+
clusterOptions.redisOptions = nodeOptions
45+
let client
46+
if (cluster) {
47+
// Workaround for ioredis bug not passing password to all nodes if
48+
// endpoint is supplied using connection uri
49+
const password = getRedisPasswordFromURI(host)
50+
clusterOptions.redisOptions.password = password
51+
host = Array.isArray(host) ? host : [host]
52+
debugInfo('Creating new redis cluster instance')
53+
client = new Redis.Cluster(host, clusterOptions)
54+
} else {
55+
debugInfo('Creating new redis single node instance')
56+
client = new Redis(Array.isArray(host) ? host[0] : host, nodeOptions)
57+
}
58+
client.on('connecting', () => debugEvents('[connecting]'))
59+
client.on('connect', () => debugEvents('[connect]'))
60+
client.on('ready', () => debugEvents('[ready]'))
61+
client.on('error', (arg) => debugEvents('[error] with argument %O', arg))
62+
client.on('close', () => debugEvents('[close]'))
63+
client.on('reconnecting', () => debugEvents('[reconnecting]'))
64+
client.on('end', () => debugEvents('[end]'))
65+
return client
6866
}
6967

7068
export = getClient

package-lock.json

+62
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"scripts": {
1212
"build": "tsc",
1313
"test": "env TS_NODE_FILES=true mocha --require ts-node/register --extension ts --colors --exit",
14-
"lint": "eslint . --ext js",
14+
"lint": "eslint --ext .ts lib test",
1515
"prepare": "npm run build"
1616
},
1717
"files": [
@@ -34,6 +34,8 @@
3434
"@types/ioredis": "^4.0.10",
3535
"@types/mocha": "^5.2.6",
3636
"@types/node": "^11.13.4",
37+
"@typescript-eslint/eslint-plugin": "^1.6.0",
38+
"@typescript-eslint/parser": "^1.6.0",
3739
"chai": "^4.2.0",
3840
"chai-as-promised": "^7.1.1",
3941
"eslint": "^5.13.0",

0 commit comments

Comments
 (0)