-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
326 lines (286 loc) · 7.82 KB
/
main.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
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
321
322
323
324
325
326
const { app, BrowserWindow, ipcMain } = require('electron');
const Redis = require('ioredis');
const path = require('path');
const fs = require('fs');
const isDev = process.env.NODE_ENV !== 'production';
let mainWindow;
let redis = null;
const connectionsFile = path.join(app.getPath('userData'), 'connections.json');
const KEYS_PER_PAGE = 50;
function createWindow() {
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
});
mainWindow.loadURL(
isDev ? 'http://localhost:3000' : `file://${path.join(__dirname, 'build/index.html')}`
);
if (isDev) {
mainWindow.webContents.openDevTools();
}
}
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
// Connection management
function loadConnections() {
try {
if (fs.existsSync(connectionsFile)) {
const data = fs.readFileSync(connectionsFile, 'utf8');
return JSON.parse(data);
}
return [];
} catch (error) {
console.error('Error loading connections:', error);
return [];
}
}
function saveConnections(connections) {
try {
fs.writeFileSync(connectionsFile, JSON.stringify(connections, null, 2));
} catch (error) {
console.error('Error saving connections:', error);
}
}
ipcMain.handle('get-connections', () => {
return loadConnections();
});
ipcMain.handle('save-connection', (event, connection) => {
const connections = loadConnections();
const existingIndex = connections.findIndex(conn => conn.name === connection.name);
if (existingIndex >= 0) {
connections[existingIndex] = connection;
} else {
connections.push(connection);
}
saveConnections(connections);
return { success: true };
});
ipcMain.handle('delete-connection', (event, name) => {
const connections = loadConnections();
const filteredConnections = connections.filter(conn => conn.name !== name);
saveConnections(filteredConnections);
return { success: true };
});
ipcMain.handle('connect-redis', async (event, connection) => {
try {
if (redis) {
await redis.quit();
}
redis = new Redis({
host: connection.host,
port: connection.port,
username: connection.username || undefined,
password: connection.password || undefined,
retryStrategy: (times) => {
const delay = Math.min(times * 50, 2000);
return delay;
}
});
return { success: true };
} catch (error) {
console.error('Redis connection error:', error);
return { success: false, error: error.message };
}
});
// Redis CRUD operations
ipcMain.handle('redis-get', async (event, key) => {
try {
if (!redis) throw new Error('Not connected to Redis');
const value = await redis.get(key);
return { success: true, data: value };
} catch (error) {
return { success: false, error: error.message };
}
});
ipcMain.handle('redis-get-details', async (event, key) => {
try {
if (!redis) throw new Error('Not connected to Redis');
// Get value and type
const type = await redis.type(key);
let value;
let members;
let fields;
switch(type) {
case 'string':
value = await redis.get(key);
break;
case 'list':
value = await redis.lrange(key, 0, -1);
break;
case 'set':
value = await redis.smembers(key);
break;
case 'zset':
members = await redis.zrange(key, 0, -1, 'WITHSCORES');
value = [];
for (let i = 0; i < members.length; i += 2) {
value.push({ member: members[i], score: parseFloat(members[i + 1]) });
}
break;
case 'hash':
fields = await redis.hgetall(key);
value = fields;
break;
default:
value = null;
}
// Get TTL
const ttl = await redis.ttl(key);
// Get memory usage
const memory = await redis.memory('USAGE', key);
return {
success: true,
data: {
type,
value,
ttl,
memory
}
};
} catch (error) {
return { success: false, error: error.message };
}
});
ipcMain.handle('redis-set', async (event, { key, value, type, ttl }) => {
try {
if (!redis) throw new Error('Not connected to Redis');
// Set value based on type
switch(type) {
case 'string':
await redis.set(key, value);
break;
case 'list':
await redis.del(key); // Clear existing list
if (Array.isArray(value) && value.length > 0) {
await redis.rpush(key, ...value);
}
break;
case 'set':
await redis.del(key); // Clear existing set
if (Array.isArray(value) && value.length > 0) {
await redis.sadd(key, ...value);
}
break;
case 'zset':
await redis.del(key); // Clear existing sorted set
if (Array.isArray(value)) {
for (const item of value) {
await redis.zadd(key, item.score, item.member);
}
}
break;
case 'hash':
await redis.del(key); // Clear existing hash
if (typeof value === 'object' && value !== null) {
await redis.hmset(key, value);
}
break;
default:
throw new Error('Unsupported Redis data type');
}
// Set TTL if specified
if (ttl && ttl > 0) {
await redis.expire(key, ttl);
} else if (ttl === -1) {
await redis.persist(key); // Remove TTL
}
return { success: true };
} catch (error) {
return { success: false, error: error.message };
}
});
ipcMain.handle('redis-delete', async (event, key) => {
try {
if (!redis) throw new Error('Not connected to Redis');
await redis.del(key);
return { success: true };
} catch (error) {
return { success: false, error: error.message };
}
});
ipcMain.handle('redis-search', async (event, pattern) => {
try {
if (!redis) throw new Error('Not connected to Redis');
const keys = [];
let cursor = '0';
do {
const [newCursor, scanKeys] = await redis.scan(
cursor,
'MATCH',
pattern,
'COUNT',
KEYS_PER_PAGE
);
cursor = newCursor;
keys.push(...scanKeys);
// Limit total results to prevent memory issues
if (keys.length > 1000) {
break;
}
} while (cursor !== '0');
return { success: true, data: keys };
} catch (error) {
return { success: false, error: error.message };
}
});
ipcMain.handle('redis-connect', async (event, { host, port, password }) => {
try {
redis = new Redis({
host,
port,
password: password || undefined,
retryStrategy: (times) => {
const delay = Math.min(times * 50, 2000);
return delay;
}
});
await redis.ping();
return { success: true };
} catch (error) {
return { success: false, error: error.message };
}
});
ipcMain.handle('redis-disconnect', async () => {
try {
if (redis) {
await redis.quit();
redis = null;
}
return { success: true };
} catch (error) {
return { success: false, error: error.message };
}
});
ipcMain.handle('redis-keys', async (event, { pattern = '*', cursor = '0', count = 10 }) => {
try {
if (!redis) throw new Error('Not connected to Redis');
// Get total key count first
const totalKeys = await redis.dbsize();
// Scan for keys with cursor
const [newCursor, keys] = await redis.scan(cursor, 'MATCH', pattern, 'COUNT', count);
return {
success: true,
data: {
keys,
cursor: newCursor,
totalKeys,
hasMore: newCursor !== '0'
}
};
} catch (error) {
return { success: false, error: error.message };
}
});