|
1 | | -/* eslint-disable no-restricted-syntax */ |
2 | | -import * as querystring from 'querystring'; |
3 | 1 | import crypto from 'crypto'; |
4 | 2 |
|
5 | | -import { getProcessUid, getNext } from '@cubejs-backend/shared'; |
| 3 | +import { getProcessUid } from '@cubejs-backend/shared'; |
6 | 4 | import { QueryKey, QueryKeyHash } from '@cubejs-backend/base-driver'; |
7 | 5 |
|
8 | | -function parseHostPort(addr: string): { host: string, port: number } { |
9 | | - if (addr.includes(':')) { |
10 | | - const parts = addr.split(':'); |
11 | | - |
12 | | - if (parts.length === 2) { |
13 | | - return { |
14 | | - host: parts[0], |
15 | | - port: parseInt(parts[1], 10), |
16 | | - }; |
17 | | - } |
18 | | - |
19 | | - throw new Error( |
20 | | - `Unsupported host:port part inside REDIS_URL: ${addr}` |
21 | | - ); |
22 | | - } |
23 | | - |
24 | | - return { |
25 | | - host: addr, |
26 | | - port: 6379, |
27 | | - }; |
28 | | -} |
29 | | - |
30 | | -function parseAddrPart(addr: string): { host: string, port: number, username?: string, password?: string } { |
31 | | - if (addr.includes('@')) { |
32 | | - const parts = addr.split('@'); |
33 | | - if (parts.length !== 2) { |
34 | | - throw new Error( |
35 | | - `Unsupported host part inside REDIS_URL: ${addr}` |
36 | | - ); |
37 | | - } |
38 | | - |
39 | | - const credentials = parts[0].split(':'); |
40 | | - if (credentials.length !== 2) { |
41 | | - throw new Error( |
42 | | - `Unsupported credentials part inside REDIS_URL: ${addr}` |
43 | | - ); |
44 | | - } |
45 | | - |
46 | | - return { |
47 | | - username: credentials[0], |
48 | | - password: credentials[1], |
49 | | - ...parseHostPort(parts[1]), |
50 | | - }; |
51 | | - } |
52 | | - |
53 | | - return parseHostPort(addr); |
54 | | -} |
55 | | - |
56 | | -export interface RedisParsedResult { |
57 | | - ssl: boolean, |
58 | | - password?: string, |
59 | | - username?: string, |
60 | | - host?: string, |
61 | | - port?: number, |
62 | | - /** |
63 | | - * Local domain socket path. If set the port, host and family will be ignored. |
64 | | - */ |
65 | | - path?: string, |
66 | | - sentinels?: { host: string, port: number }[], |
67 | | - db?: number, |
68 | | - name?: string, |
69 | | -} |
70 | | - |
71 | | -function parseHostPartBasic(addUrl: string, result: RedisParsedResult) { |
72 | | - const { host, port, password, username } = parseAddrPart(addUrl); |
73 | | - |
74 | | - result.password = password; |
75 | | - result.username = username; |
76 | | - result.host = host; |
77 | | - result.port = port; |
78 | | - |
79 | | - return result; |
80 | | -} |
81 | | - |
82 | | -function parseHostPartSentinel(addUrl: string, result: RedisParsedResult) { |
83 | | - const servers = addUrl.split(','); |
84 | | - |
85 | | - result.sentinels = servers.map((addr) => parseHostPort(addr)); |
86 | | - |
87 | | - return result; |
88 | | -} |
89 | | - |
90 | | -function parseUrl( |
91 | | - url: string, |
92 | | - result: RedisParsedResult, |
93 | | - parseAddPartFn: (addr: string, result: RedisParsedResult) => RedisParsedResult, |
94 | | -): RedisParsedResult { |
95 | | - if (url.includes('/')) { |
96 | | - const parts = url.split('/'); |
97 | | - if (parts.length === 2) { |
98 | | - result.db = parseInt(<string>parts[1], 10); |
99 | | - } else if (parts.length === 3) { |
100 | | - result.name = <string>parts[1]; |
101 | | - result.db = parseInt(<string>parts[2], 10); |
102 | | - } else { |
103 | | - throw new Error( |
104 | | - `Unsupported REDIS_URL: "${url}"` |
105 | | - ); |
106 | | - } |
107 | | - |
108 | | - return parseAddPartFn(parts[0], result); |
109 | | - } |
110 | | - |
111 | | - return parseAddPartFn(url, result); |
112 | | -} |
113 | | - |
114 | | -function parseUnixUrl(url: string, result: RedisParsedResult) { |
115 | | - if (url.includes('?')) { |
116 | | - const parts = url.split('?'); |
117 | | - if (parts.length === 2) { |
118 | | - const query = querystring.parse(parts[1]); |
119 | | - |
120 | | - for (const key of Object.keys(query)) { |
121 | | - switch (key.toLowerCase()) { |
122 | | - case 'db': |
123 | | - result.db = parseInt(<string>query[key], 10); |
124 | | - break; |
125 | | - default: |
126 | | - break; |
127 | | - } |
128 | | - } |
129 | | - |
130 | | - return { |
131 | | - ...result, |
132 | | - path: parts[0], |
133 | | - }; |
134 | | - } |
135 | | - |
136 | | - throw new Error( |
137 | | - `Unsupported REDIS_URL: "${url}"` |
138 | | - ); |
139 | | - } |
140 | | - |
141 | | - result.path = url; |
142 | | - |
143 | | - return result; |
144 | | -} |
145 | | - |
146 | | -export function parseRedisUrl(url: Readonly<string>): RedisParsedResult { |
147 | | - const result: RedisParsedResult = { |
148 | | - username: undefined, |
149 | | - password: undefined, |
150 | | - host: undefined, |
151 | | - port: undefined, |
152 | | - ssl: false, |
153 | | - sentinels: undefined, |
154 | | - db: undefined, |
155 | | - name: undefined, |
156 | | - }; |
157 | | - |
158 | | - if (!url) { |
159 | | - return result; |
160 | | - } |
161 | | - |
162 | | - if (url.startsWith('redis://')) { |
163 | | - return parseUrl(url.slice('redis://'.length), result, parseHostPartBasic); |
164 | | - } |
165 | | - |
166 | | - if (url.startsWith('rediss://')) { |
167 | | - result.ssl = true; |
168 | | - |
169 | | - return parseUrl(url.slice('rediss://'.length), result, parseHostPartBasic); |
170 | | - } |
171 | | - |
172 | | - if (url.startsWith('redis+sentinel://')) { |
173 | | - return parseUrl(url.slice('redis+sentinel://'.length), result, parseHostPartSentinel); |
174 | | - } |
175 | | - |
176 | | - if (url.startsWith('unix://')) { |
177 | | - return parseUnixUrl(url.slice('unix://'.length), result); |
178 | | - } |
179 | | - |
180 | | - return parseUrl(url, result, parseHostPartBasic); |
181 | | -} |
182 | | - |
183 | 6 | /** |
184 | 7 | * Unique process ID regexp. |
185 | 8 | */ |
|
0 commit comments