-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfileStoreServer.js
More file actions
416 lines (380 loc) · 12.9 KB
/
fileStoreServer.js
File metadata and controls
416 lines (380 loc) · 12.9 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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/**
* FileStore Server - Backend API for persistent file-based caching
* Handles GET, POST, and DELETE operations for task cache storage
*/
import fs from 'node:fs/promises';
import path from 'node:path';
import { serverLogger } from './server-logger.js';
// Default cache directory in project root
const CACHE_DIR = process.env.FILESTORE_CACHE_DIR || path.join(process.cwd(), '.filestore');
/**
* Ensure cache directory exists
*/
async function ensureCacheDir() {
try {
await fs.mkdir(CACHE_DIR, { recursive: true });
} catch (error) {
serverLogger.error('Error creating cache directory:', error);
}
}
/**
* Get full path for cache file
*/
function getCachePath(filename) {
return path.join(CACHE_DIR, filename);
}
/**
* Load cache from file
*/
async function loadCacheFromFile(filename) {
try {
const filePath = getCachePath(filename);
const data = await fs.readFile(filePath, 'utf8');
return JSON.parse(data);
} catch (error) {
if (error.code === 'ENOENT') {
serverLogger.info(`Cache file not found: ${filename}`);
return null;
}
serverLogger.error('Error reading cache file:', error);
throw error;
}
}
/**
* Save cache to file
*/
async function saveCacheToFile(filename, data) {
try {
await ensureCacheDir();
const filePath = getCachePath(filename);
await fs.writeFile(filePath, JSON.stringify(data, null, 2), 'utf8');
serverLogger.info(`Cache saved to: ${filePath}`);
return true;
} catch (error) {
serverLogger.error('Error writing cache file:', error);
throw error;
}
}
/**
* Delete cache file
*/
async function deleteCacheFile(filename) {
try {
const filePath = getCachePath(filename);
await fs.unlink(filePath);
serverLogger.info(`Cache file deleted: ${filePath}`);
return true;
} catch (error) {
if (error.code === 'ENOENT') {
serverLogger.info(`Cache file not found for deletion: ${filename}`);
return true;
}
serverLogger.error('Error deleting cache file:', error);
throw error;
}
}
/**
* Get cache file info
*/
async function getCacheFileInfo(filename) {
try {
const filePath = getCachePath(filename);
const stats = await fs.stat(filePath);
return {
size: stats.size,
lastUpdate: stats.mtimeMs,
created: stats.birthtimeMs,
};
} catch (error) {
if (error.code === 'ENOENT') {
return null;
}
serverLogger.error('Error getting file info:', error);
throw error;
}
}
/**
* Express middleware/route handlers for filestore operations
*/
const fileStoreRoutes = (app) => {
// Initialize cache directory on startup
serverLogger.debug('📁 Initializing FileStore cache directory...');
ensureCacheDir().then(() => {
serverLogger.debug(`✓ FileStore initialized at: ${CACHE_DIR}`);
});
/**
* GET /api/filestore/load - Load cache from filestore
*/
app.get('/api/filestore/load', async (req, res) => {
try {
serverLogger.debug('📥 GET /api/filestore/load - Loading task cache');
const data = await loadCacheFromFile('task-cache.json');
if (data) {
const itemCount = data.data?.length || 0;
serverLogger.info(`✅ Task cache loaded successfully: ${itemCount} items`);
res.json({ success: true, data });
} else {
serverLogger.debug('ℹ️ Task cache is empty, returning empty array');
res.json({ success: true, data: [] });
}
} catch (error) {
serverLogger.error('❌ Error in /api/filestore/load:', error.message);
res.status(500).json({
success: false,
error: error instanceof Error ? error.message : 'Unknown error loading cache',
});
}
});
/**
* GET /api/filestore/load-workflows - Load workflows from filestore
*/
app.get('/api/filestore/load-workflows', async (req, res) => {
try {
serverLogger.debug('📥 GET /api/filestore/load-workflows - Loading workflows cache');
const data = await loadCacheFromFile('workflows-cache.json');
if (data && data.workflows && Array.isArray(data.workflows)) {
serverLogger.info(`✅ Workflows cache loaded: ${data.workflows.length} workflows`);
res.json({ success: true, data: data.workflows });
} else if (data && Array.isArray(data)) {
// Handle legacy format
serverLogger.info(`✅ Workflows cache loaded (legacy format): ${data.length} workflows`);
res.json({ success: true, data });
} else {
serverLogger.debug('ℹ️ Workflows cache is empty, returning empty array');
res.json({ success: true, data: [] });
}
} catch (error) {
serverLogger.error('Error in /api/filestore/load-workflows:', error);
res.status(500).json({
success: false,
error: error instanceof Error ? error.message : 'Unknown error loading workflows',
});
}
});
/**
* POST /api/filestore/save - Save cache to filestore
*/
app.post('/api/filestore/save', async (req, res) => {
try {
const { key, data, timestamp } = req.body;
if (!key || !Array.isArray(data)) {
serverLogger.warn('POST /api/filestore/save - Invalid request: missing key or data');
res.status(400).json({
success: false,
error: 'Missing required fields: key (string), data (array)',
});
return;
}
const cacheData = {
timestamp: timestamp || Date.now(),
data,
};
serverLogger.debug(
`💾 Saving cache - Key: ${key}, Items: ${data.length}`
);
await saveCacheToFile(key, cacheData);
serverLogger.info(`✅ Cache saved successfully: ${key} (${data.length} items)`);
res.json({ success: true });
} catch (error) {
serverLogger.error('❌ Error saving cache:', error.message);
res.status(500).json({
success: false,
error: error instanceof Error ? error.message : 'Unknown error saving cache',
});
}
});
/**
* POST /api/filestore/save-workflows - Save workflows to filestore
*/
app.post('/api/filestore/save-workflows', async (req, res) => {
try {
const { workflows, timestamp } = req.body;
if (!Array.isArray(workflows)) {
serverLogger.warn(
'⚠️ Invalid request: workflows not an array'
);
res.status(400).json({
success: false,
error: 'Missing required field: workflows (array)',
});
return;
}
const workflowData = {
timestamp: timestamp || Date.now(),
workflows,
};
serverLogger.debug(
`💾 Saving workflows - Count: ${workflows.length}`
);
await saveCacheToFile('workflows-cache.json', workflowData);
serverLogger.info(`✅ Workflows saved successfully: ${workflows.length} workflows`);
res.json({ success: true });
} catch (error) {
serverLogger.error('❌ Error saving workflows:', error.message);
res.status(500).json({
success: false,
error: error instanceof Error ? error.message : 'Unknown error saving workflows',
});
}
});
/**
* POST /api/filestore/clear - Clear/delete cache file
*/
app.post('/api/filestore/clear', async (req, res) => {
try {
const { key } = req.body;
if (!key) {
serverLogger.warn('⚠️ Invalid request: missing cache key');
res.status(400).json({
success: false,
error: 'Missing required field: key',
});
return;
}
serverLogger.debug(`🗑️ Clearing cache - Key: ${key}`);
await deleteCacheFile(key);
serverLogger.info(`✅ Cache cleared successfully: ${key}`);
res.json({ success: true });
} catch (error) {
serverLogger.error('❌ Error clearing cache:', error.message);
res.status(500).json({
success: false,
error: error instanceof Error ? error.message : 'Unknown error clearing cache',
});
}
});
/**
* GET /api/filestore/info - Get cache file info/metadata
*/
app.get('/api/filestore/info', async (req, res) => {
try {
serverLogger.debug('📊 Retrieving cache file info');
const info = await getCacheFileInfo('task-cache.json');
if (info) {
serverLogger.debug(`✓ Cache file info retrieved - Size: ${info.size} bytes, Modified: ${new Date(info.mtimeMs).toLocaleString()}`);
res.json({ success: true, data: info });
} else {
serverLogger.debug('ℹ️ Cache file does not exist');
res.json({ success: true, data: { size: 0, lastUpdate: null, created: null } });
}
} catch (error) {
serverLogger.error('Error in /api/filestore/info:', error);
res.status(500).json({
success: false,
error: error instanceof Error ? error.message : 'Unknown error getting cache info',
});
}
});
/**
* POST /api/filestore/save-workflow - Save individual workflow file
* Creates file in workflows/ subdirectory if it doesn't exist
*/
app.post('/api/filestore/save-workflow', async (req, res) => {
try {
const { filename, data, timestamp } = req.body;
if (!filename || !data) {
serverLogger.warn(
'POST /api/filestore/save-workflow - Invalid request: missing filename or data'
);
res.status(400).json({
success: false,
error: 'Missing required fields: filename (string), data (object)',
});
return;
}
// Ensure workflows directory exists
const workflowsDir = path.join(CACHE_DIR, 'workflows');
await fs.mkdir(workflowsDir, { recursive: true });
// Save workflow file
const filePath = path.join(CACHE_DIR, filename);
const cacheData = {
timestamp: timestamp || Date.now(),
...data,
};
serverLogger.debug(`POST /api/filestore/save-workflow - Saving workflow: ${filename}`);
await fs.writeFile(filePath, JSON.stringify(cacheData, null, 2), 'utf8');
serverLogger.info(`✓ Workflow saved: ${filename}`);
res.json({ success: true });
} catch (error) {
serverLogger.error('Error in /api/filestore/save-workflow:', error);
res.status(500).json({
success: false,
error: error instanceof Error ? error.message : 'Unknown error saving workflow',
});
}
});
/**
* GET /api/filestore/load-workflows-batch - Load all workflows from workflows/ directory
*/
app.get('/api/filestore/load-workflows-batch', async (req, res) => {
try {
const workflowsDir = path.join(CACHE_DIR, 'workflows');
const workflows = [];
serverLogger.debug('GET /api/filestore/load-workflows-batch - Loading all workflows');
try {
// Read all files in workflows directory
const files = await fs.readdir(workflowsDir);
for (const file of files) {
if (file.endsWith('.json')) {
const filePath = path.join(workflowsDir, file);
const data = await fs.readFile(filePath, 'utf8');
const workflow = JSON.parse(data);
workflows.push(workflow);
}
}
serverLogger.debug(`✓ Loaded ${workflows.length} workflows from batch`);
} catch (err) {
// Directory doesn't exist yet - return empty array
if (err.code !== 'ENOENT') {
throw err;
}
serverLogger.debug('Workflows batch directory does not exist, returning empty array');
}
res.json({ success: true, data: workflows });
} catch (error) {
serverLogger.error('Error in /api/filestore/load-workflows-batch:', error);
res.status(500).json({
success: false,
error: error instanceof Error ? error.message : 'Unknown error loading workflows',
});
}
});
/**
* POST /api/filestore/delete-workflow - Delete individual workflow file
*/
app.post('/api/filestore/delete-workflow', async (req, res) => {
try {
const { filename } = req.body;
if (!filename) {
serverLogger.warn(
'POST /api/filestore/delete-workflow - Invalid request: missing filename'
);
res.status(400).json({
success: false,
error: 'Missing required field: filename',
});
return;
}
const filePath = path.join(CACHE_DIR, filename);
serverLogger.debug(`POST /api/filestore/delete-workflow - Deleting workflow: ${filename}`);
try {
await fs.unlink(filePath);
serverLogger.info(`✓ Workflow deleted: ${filename}`);
} catch (err) {
// File doesn't exist - treat as success
if (err.code !== 'ENOENT') {
throw err;
}
serverLogger.debug(`Workflow not found (already deleted): ${filename}`);
}
res.json({ success: true });
} catch (error) {
serverLogger.error('Error in /api/filestore/delete-workflow:', error);
res.status(500).json({
success: false,
error: error instanceof Error ? error.message : 'Unknown error deleting workflow',
});
}
});
};
export { fileStoreRoutes, ensureCacheDir };