-
Notifications
You must be signed in to change notification settings - Fork 10.8k
/
Copy pathutils.ts
167 lines (153 loc) · 4.13 KB
/
utils.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
import type {
ICredentialTestFunctions,
ICredentialsDecrypted,
IDataObject,
IExecuteFunctions,
INodeCredentialTestResult,
} from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';
import { createClient } from 'redis';
import type { RedisCredential, RedisClient } from './types';
export function setupRedisClient(credentials: RedisCredential): RedisClient {
return createClient({
socket: {
host: credentials.host,
port: credentials.port,
tls: credentials.ssl === true,
},
database: credentials.database,
username: credentials.user || undefined,
password: credentials.password || undefined,
});
}
export async function redisConnectionTest(
this: ICredentialTestFunctions,
credential: ICredentialsDecrypted,
): Promise<INodeCredentialTestResult> {
const credentials = credential.data as RedisCredential;
try {
const client = setupRedisClient(credentials);
await client.connect();
await client.ping();
return {
status: 'OK',
message: 'Connection successful!',
};
} catch (error) {
return {
status: 'Error',
message: error.message,
};
}
}
/** Parses the given value in a number if it is one else returns a string */
function getParsedValue(value: string): string | number {
if (value.match(/^[\d\.]+$/) === null) {
// Is a string
return value;
} else {
// Is a number
return parseFloat(value);
}
}
/** Converts the Redis Info String into an object */
export function convertInfoToObject(stringData: string): IDataObject {
const returnData: IDataObject = {};
let key: string, value: string;
for (const line of stringData.split('\n')) {
if (['#', ''].includes(line.charAt(0))) {
continue;
}
[key, value] = line.split(':');
if (key === undefined || value === undefined) {
continue;
}
value = value.trim();
if (value.includes('=')) {
returnData[key] = {};
let key2: string, value2: string;
for (const keyValuePair of value.split(',')) {
[key2, value2] = keyValuePair.split('=');
(returnData[key] as IDataObject)[key2] = getParsedValue(value2);
}
} else {
returnData[key] = getParsedValue(value);
}
}
return returnData;
}
export async function getValue(client: RedisClient, keyName: string, type?: string) {
if (type === undefined || type === 'automatic') {
// Request the type first
type = await client.type(keyName);
}
if (type === 'string') {
return await client.get(keyName);
} else if (type === 'hash') {
return await client.hGetAll(keyName);
} else if (type === 'list') {
return await client.lRange(keyName, 0, -1);
} else if (type === 'sets') {
return await client.sMembers(keyName);
}
}
export async function setValue(
this: IExecuteFunctions,
client: RedisClient,
keyName: string,
value: string | number | object | string[] | number[],
expire: boolean,
ttl: number,
type?: string,
valueIsJSON?: boolean,
) {
if (type === undefined || type === 'automatic') {
// Request the type first
if (typeof value === 'string') {
type = 'string';
} else if (Array.isArray(value)) {
type = 'list';
} else if (typeof value === 'object') {
type = 'hash';
} else {
throw new NodeOperationError(
this.getNode(),
'Could not identify the type to set. Please set it manually!',
);
}
}
if (type === 'string') {
await client.set(keyName, value.toString());
} else if (type === 'hash') {
if (valueIsJSON) {
let values: unknown;
if (typeof value === 'string') {
try {
values = JSON.parse(value);
} catch {
// This is how we originally worked and prevents a breaking change
values = value;
}
} else {
values = value;
}
for (const key of Object.keys(values as object)) {
await client.hSet(keyName, key, (values as IDataObject)[key]!.toString());
}
} else {
const values = value.toString().split(' ');
await client.hSet(keyName, values);
}
} else if (type === 'list') {
for (let index = 0; index < (value as string[]).length; index++) {
await client.lSet(keyName, index, (value as IDataObject)[index]!.toString());
}
} else if (type === 'sets') {
//@ts-ignore
await client.sAdd(keyName, value);
}
if (expire) {
await client.expire(keyName, ttl);
}
return;
}