This repository has been archived by the owner on Jan 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (52 loc) · 1.86 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict';
var net = require('net');
var Client = require('./lib/client');
var DEFAULT_PORT = 6379;
var DEFAULT_HOST = '127.0.0.1';
var FAMILY_IPV6 = 6;
var FAMILY_IPV4 = 4;
function createClientUnix(path, options) {
var cnxOptions = {
path: path
};
var netClient = net.createConnection(cnxOptions);
var redisClient = new Client(netClient, options || {});
redisClient.connectionOption = cnxOptions;
redisClient.address = path;
return redisClient;
}
function createClientTcp(port, host, options) {
var cnxOptions = {
'port': port || DEFAULT_PORT,
'host': host || DEFAULT_HOST,
'family': options && options.family === 'IPv6' ? FAMILY_IPV6 : FAMILY_IPV4
};
var netClient = net.createConnection(cnxOptions);
var redisClient = new Client(netClient, options || {});
redisClient.connectionOption = cnxOptions;
redisClient.address = cnxOptions.host + ':' + cnxOptions.port;
return redisClient;
}
exports.createClient = function createClient(arg0, arg1, arg2) {
if (!arguments.length) {
// createClient()
return createClientTcp(DEFAULT_PORT, DEFAULT_HOST, {});
} else if (typeof arg0 === 'number' ||
typeof arg0 === 'string' && arg0.match(/^\d+$/)) {
// createClient( 3000, host, options)
// createClient('3000', host, options)
return createClientTcp(arg0, arg1, arg2);
} else if (typeof arg0 === 'string') {
// createClient( '/tmp/redis.sock', options)
return createClientUnix(arg0, arg1);
} else if (arg0 !== null && typeof arg0 === 'object') {
// createClient(options)
return createClientTcp(DEFAULT_PORT, DEFAULT_HOST, arg0);
} else if (arg0 === null && arg1 === null) {
// for backward compatibility
// createClient(null,null,options)
return createClientTcp(DEFAULT_PORT, DEFAULT_HOST, arg2);
} else {
throw new Error('unknown type of connection in createClient()');
}
};