/
index.ts
320 lines (288 loc) 路 7 KB
/
index.ts
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import { parse as urllibParse } from "url";
import { defaults, noop } from "./lodash";
import { Callback } from "../types";
import Debug from "./debug";
import TLSProfiles from "../constants/TLSProfiles";
/**
* Convert a buffer to string, supports buffer array
*
* @example
* ```js
* const input = [Buffer.from('foo'), [Buffer.from('bar')]]
* const res = convertBufferToString(input, 'utf8')
* expect(res).to.eql(['foo', ['bar']])
* ```
*/
export function convertBufferToString(value: any, encoding?: BufferEncoding) {
if (value instanceof Buffer) {
return value.toString(encoding);
}
if (Array.isArray(value)) {
const length = value.length;
const res = Array(length);
for (let i = 0; i < length; ++i) {
res[i] =
value[i] instanceof Buffer && encoding === "utf8"
? value[i].toString()
: convertBufferToString(value[i], encoding);
}
return res;
}
return value;
}
/**
* Convert a list of results to node-style
*
* @example
* ```js
* const input = ['a', 'b', new Error('c'), 'd']
* const output = exports.wrapMultiResult(input)
* expect(output).to.eql([[null, 'a'], [null, 'b'], [new Error('c')], [null, 'd'])
* ```
*/
export function wrapMultiResult(arr: unknown[] | null): unknown[][] {
// When using WATCH/EXEC transactions, the EXEC will return
// a null instead of an array
if (!arr) {
return null;
}
const result = [];
const length = arr.length;
for (let i = 0; i < length; ++i) {
const item = arr[i];
if (item instanceof Error) {
result.push([item]);
} else {
result.push([null, item]);
}
}
return result;
}
/**
* Detect if the argument is a int
* @example
* ```js
* > isInt('123')
* true
* > isInt('123.3')
* false
* > isInt('1x')
* false
* > isInt(123)
* true
* > isInt(true)
* false
* ```
*/
export function isInt(value: any): value is string {
const x = parseFloat(value);
return !isNaN(value) && (x | 0) === x;
}
/**
* Pack an array to an Object
*
* @example
* ```js
* > packObject(['a', 'b', 'c', 'd'])
* { a: 'b', c: 'd' }
* ```
*/
export function packObject(array: any[]): Record<string, any> {
const result = {};
const length = array.length;
for (let i = 1; i < length; i += 2) {
result[array[i - 1]] = array[i];
}
return result;
}
/**
* Return a callback with timeout
*/
export function timeout<T>(
callback: Callback<T>,
timeout: number
): Callback<T> {
let timer: ReturnType<typeof setTimeout> | null = null;
const run: Callback<T> = function () {
if (timer) {
clearTimeout(timer);
timer = null;
callback.apply(this, arguments);
}
};
timer = setTimeout(run, timeout, new Error("timeout"));
return run;
}
/**
* Convert an object to an array
* @example
* ```js
* > convertObjectToArray({ a: '1' })
* ['a', '1']
* ```
*/
export function convertObjectToArray<T>(
obj: Record<string, T>
): (string | T)[] {
const result = [];
const keys = Object.keys(obj); // Object.entries requires node 7+
for (let i = 0, l = keys.length; i < l; i++) {
result.push(keys[i], obj[keys[i]]);
}
return result;
}
/**
* Convert a map to an array
* @example
* ```js
* > convertMapToArray(new Map([[1, '2']]))
* [1, '2']
* ```
*/
export function convertMapToArray<K, V>(map: Map<K, V>): (K | V)[] {
const result: Array<K | V> = [];
let pos = 0;
map.forEach(function (value, key) {
result[pos] = key;
result[pos + 1] = value;
pos += 2;
});
return result;
}
/**
* Convert a non-string arg to a string
*/
export function toArg(arg: any): string {
if (arg === null || typeof arg === "undefined") {
return "";
}
return String(arg);
}
/**
* Optimize error stack
*
* @param error actually error
* @param friendlyStack the stack that more meaningful
* @param filterPath only show stacks with the specified path
*/
export function optimizeErrorStack(
error: Error,
friendlyStack: string,
filterPath: string
) {
const stacks = friendlyStack.split("\n");
let lines = "";
let i;
for (i = 1; i < stacks.length; ++i) {
if (stacks[i].indexOf(filterPath) === -1) {
break;
}
}
for (let j = i; j < stacks.length; ++j) {
lines += "\n" + stacks[j];
}
const pos = error.stack.indexOf("\n");
error.stack = error.stack.slice(0, pos) + lines;
return error;
}
/**
* Parse the redis protocol url
*/
export function parseURL(url: string): Record<string, unknown> {
if (isInt(url)) {
return { port: url };
}
let parsed = urllibParse(url, true, true);
if (!parsed.slashes && url[0] !== "/") {
url = "//" + url;
parsed = urllibParse(url, true, true);
}
const options = parsed.query || {};
const result: any = {};
if (parsed.auth) {
const index = parsed.auth.indexOf(":");
result.username = index === -1 ? parsed.auth : parsed.auth.slice(0, index);
result.password = index === -1 ? "" : parsed.auth.slice(index + 1);
}
if (parsed.pathname) {
if (parsed.protocol === "redis:" || parsed.protocol === "rediss:") {
if (parsed.pathname.length > 1) {
result.db = parsed.pathname.slice(1);
}
} else {
result.path = parsed.pathname;
}
}
if (parsed.host) {
result.host = parsed.hostname;
}
if (parsed.port) {
result.port = parsed.port;
}
if (typeof options.family === "string") {
const intFamily = Number.parseInt(options.family, 10);
if (!Number.isNaN(intFamily)) {
result.family = intFamily;
}
}
defaults(result, options);
return result;
}
interface TLSOptions {
port: number;
host: string;
[key: string]: any;
}
/**
* Resolve TLS profile shortcut in connection options
*/
export function resolveTLSProfile(options: TLSOptions): TLSOptions {
let tls = options?.tls;
if (typeof tls === "string") tls = { profile: tls };
const profile = TLSProfiles[tls?.profile];
if (profile) {
tls = Object.assign({}, profile, tls);
delete tls.profile;
options = Object.assign({}, options, { tls });
}
return options;
}
/**
* Get a random element from `array`
*/
export function sample<T>(array: T[], from = 0): T {
const length = array.length;
if (from >= length) {
return;
}
return array[from + Math.floor(Math.random() * (length - from))];
}
/**
* Shuffle the array using the Fisher-Yates Shuffle.
* This method will mutate the original array.
*/
export function shuffle<T>(array: T[]): T[] {
let counter = array.length;
// While there are elements in the array
while (counter > 0) {
// Pick a random index
const index = Math.floor(Math.random() * counter);
// Decrease counter by 1
counter--;
// And swap the last element with it
[array[counter], array[index]] = [array[index], array[counter]];
}
return array;
}
/**
* Error message for connection being disconnected
*/
export const CONNECTION_CLOSED_ERROR_MSG = "Connection is closed.";
export function zipMap<K, V>(keys: K[], values: V[]): Map<K, V> {
const map = new Map<K, V>();
keys.forEach((key, index) => {
map.set(key, values[index]);
});
return map;
}
export { Debug, defaults, noop };