forked from ABXTrading/epicurus-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
91 lines (83 loc) · 2.66 KB
/
index.ts
File metadata and controls
91 lines (83 loc) · 2.66 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import * as bluebird from 'bluebird'
import * as redis from 'redis'
import { RedisClient } from 'redis'
import { config } from './config'
import {
EpicurusRedisConfig,
serverCallback,
subscribeCallback,
} from './interface'
import {
publish,
removeCallbacks,
setupSubscriptionListener,
shutdownSubscribers,
subscribe,
} from './lib/pub_sub'
import {
closeAllClients,
disableServers,
enableServers,
request,
server,
} from './lib/request_response'
bluebird.promisifyAll(redis.RedisClient.prototype)
bluebird.promisifyAll(redis.Multi.prototype)
const REDIS_TLS_HOST = 'rediss'
function getConfig(redisConfig: EpicurusRedisConfig) {
if (redisConfig.url && redisConfig.url.includes(REDIS_TLS_HOST)) {
return {
tls: {
...redisConfig,
rejectUnauthorized: true
}
}
} else {
return redisConfig
}
}
export default function Epicurus (
redisConfig: EpicurusRedisConfig = {
host: 'localhost',
port: 6379
},
requestTimeout?: number
): EpicurusPublicInterface {
// A separate subscription Redis client is required as once a client has
// called SUBSCRIBE, it is put into a slave mode the does not allow any other
// kind of action
const redisClient = redis.createClient(getConfig(redisConfig))
const redisSub = redis.createClient(getConfig(redisConfig))
const requestValidityPeriod = requestTimeout || config.requestValidityPeriod
setupSubscriptionListener(redisSub)
enableServers()
return {
getRedisClient: () => redisClient,
getRedisSubClient: () => redisSub,
subscribe: <T = any>(channel: string, callback: subscribeCallback<T>) => subscribe(redisSub, channel, callback),
publish: (channel: string, body: any) => publish(redisClient, channel, body),
server: <T = any, S = any>(channel: string, callback: serverCallback<T, S>) => server(redisClient, channel, callback, requestValidityPeriod),
request: <T = any>(channel: string, body: any) => request<T>(redisClient, channel, body, requestValidityPeriod),
shutdown: () => {
shutdownSubscribers()
disableServers()
},
close: () => {
redisSub.unsubscribe()
redisSub.quit()
redisClient.quit()
removeCallbacks()
closeAllClients()
}
}
}
export type EpicurusPublicInterface = {
getRedisClient: () => RedisClient
getRedisSubClient: () => RedisClient
subscribe: <T = any>(channel: string, callback: subscribeCallback<T>) => Promise<void>
publish: (channel: string, body: any) => void
server: <T = any, S = any>(channel: string, callback: serverCallback<T, S>) => Promise<void>
request: <T = any>(channel: string, body: any) => Promise<T>
shutdown: () => void
close: () => void
}