wyrm-mcp 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +667 -0
- package/README.md +384 -0
- package/dist/analytics.d.ts +100 -0
- package/dist/analytics.d.ts.map +1 -0
- package/dist/analytics.js +368 -0
- package/dist/analytics.js.map +1 -0
- package/dist/auto-orchestrator.d.ts +118 -0
- package/dist/auto-orchestrator.d.ts.map +1 -0
- package/dist/auto-orchestrator.js +325 -0
- package/dist/auto-orchestrator.js.map +1 -0
- package/dist/autoconfig.d.ts +89 -0
- package/dist/autoconfig.d.ts.map +1 -0
- package/dist/autoconfig.js +576 -0
- package/dist/autoconfig.js.map +1 -0
- package/dist/cli.d.ts +148 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +281 -0
- package/dist/cli.js.map +1 -0
- package/dist/cloud-backup.d.ts +100 -0
- package/dist/cloud-backup.d.ts.map +1 -0
- package/dist/cloud-backup.js +545 -0
- package/dist/cloud-backup.js.map +1 -0
- package/dist/crypto.d.ts +72 -0
- package/dist/crypto.d.ts.map +1 -0
- package/dist/crypto.js +164 -0
- package/dist/crypto.js.map +1 -0
- package/dist/database.d.ts +218 -0
- package/dist/database.d.ts.map +1 -0
- package/dist/database.js +1058 -0
- package/dist/database.js.map +1 -0
- package/dist/http-auth.d.ts +68 -0
- package/dist/http-auth.d.ts.map +1 -0
- package/dist/http-auth.js +296 -0
- package/dist/http-auth.js.map +1 -0
- package/dist/http-fast.d.ts +13 -0
- package/dist/http-fast.d.ts.map +1 -0
- package/dist/http-fast.js +325 -0
- package/dist/http-fast.js.map +1 -0
- package/dist/http-server.d.ts +12 -0
- package/dist/http-server.d.ts.map +1 -0
- package/dist/http-server.js +383 -0
- package/dist/http-server.js.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1695 -0
- package/dist/index.js.map +1 -0
- package/dist/license.d.ts +177 -0
- package/dist/license.d.ts.map +1 -0
- package/dist/license.js +405 -0
- package/dist/license.js.map +1 -0
- package/dist/logger.d.ts +76 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +195 -0
- package/dist/logger.js.map +1 -0
- package/dist/performance.d.ts +114 -0
- package/dist/performance.d.ts.map +1 -0
- package/dist/performance.js +228 -0
- package/dist/performance.js.map +1 -0
- package/dist/resilience.d.ts +146 -0
- package/dist/resilience.d.ts.map +1 -0
- package/dist/resilience.js +563 -0
- package/dist/resilience.js.map +1 -0
- package/dist/security.d.ts +68 -0
- package/dist/security.d.ts.map +1 -0
- package/dist/security.js +215 -0
- package/dist/security.js.map +1 -0
- package/dist/setup.d.ts +21 -0
- package/dist/setup.d.ts.map +1 -0
- package/dist/setup.js +261 -0
- package/dist/setup.js.map +1 -0
- package/dist/summarizer.d.ts +30 -0
- package/dist/summarizer.d.ts.map +1 -0
- package/dist/summarizer.js +139 -0
- package/dist/summarizer.js.map +1 -0
- package/dist/sync.d.ts +39 -0
- package/dist/sync.d.ts.map +1 -0
- package/dist/sync.js +356 -0
- package/dist/sync.js.map +1 -0
- package/dist/types.d.ts +267 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +30 -0
- package/dist/types.js.map +1 -0
- package/dist/vectors.d.ts +103 -0
- package/dist/vectors.d.ts.map +1 -0
- package/dist/vectors.js +311 -0
- package/dist/vectors.js.map +1 -0
- package/package.json +73 -0
|
@@ -0,0 +1,563 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wyrm Resilience Module - Professional-grade fault tolerance
|
|
3
|
+
*
|
|
4
|
+
* @copyright 2026 Ghost Protocol (Pvt) Ltd. All Rights Reserved.
|
|
5
|
+
* @license Proprietary - See LICENSE file for details.
|
|
6
|
+
* @module resilience
|
|
7
|
+
* @version 3.0.0
|
|
8
|
+
*
|
|
9
|
+
* Features:
|
|
10
|
+
* - Transaction safety with automatic rollback
|
|
11
|
+
* - Exponential backoff retry logic
|
|
12
|
+
* - Circuit breaker pattern
|
|
13
|
+
* - Operation checkpointing
|
|
14
|
+
* - Crash recovery via WAL
|
|
15
|
+
* - Graceful degradation
|
|
16
|
+
*/
|
|
17
|
+
import { existsSync, readFileSync, writeFileSync, mkdirSync, unlinkSync, renameSync, readdirSync } from 'fs';
|
|
18
|
+
import { join } from 'path';
|
|
19
|
+
import { homedir } from 'os';
|
|
20
|
+
import { WyrmLogger } from './logger.js';
|
|
21
|
+
// ==================== DEFAULT CONFIGS ====================
|
|
22
|
+
const DEFAULT_RETRY_CONFIG = {
|
|
23
|
+
maxAttempts: 5,
|
|
24
|
+
baseDelayMs: 100,
|
|
25
|
+
maxDelayMs: 10000,
|
|
26
|
+
backoffMultiplier: 2,
|
|
27
|
+
retryableErrors: [
|
|
28
|
+
'SQLITE_BUSY',
|
|
29
|
+
'SQLITE_LOCKED',
|
|
30
|
+
'ECONNRESET',
|
|
31
|
+
'ETIMEDOUT',
|
|
32
|
+
'ENOTFOUND',
|
|
33
|
+
'EAI_AGAIN',
|
|
34
|
+
'EPIPE',
|
|
35
|
+
'ECONNREFUSED',
|
|
36
|
+
],
|
|
37
|
+
};
|
|
38
|
+
const DEFAULT_CIRCUIT_CONFIG = {
|
|
39
|
+
failureThreshold: 5,
|
|
40
|
+
successThreshold: 3,
|
|
41
|
+
timeout: 30000,
|
|
42
|
+
};
|
|
43
|
+
// ==================== RESILIENCE MANAGER ====================
|
|
44
|
+
export class ResilienceManager {
|
|
45
|
+
logger;
|
|
46
|
+
checkpointDir;
|
|
47
|
+
retryConfig;
|
|
48
|
+
circuitConfig;
|
|
49
|
+
// Circuit breaker state
|
|
50
|
+
circuitState = 'closed';
|
|
51
|
+
failureCount = 0;
|
|
52
|
+
successCount = 0;
|
|
53
|
+
lastFailureTime = 0;
|
|
54
|
+
// Active operations for tracking
|
|
55
|
+
activeOperations = new Map();
|
|
56
|
+
constructor(logger, retryConfig, circuitConfig) {
|
|
57
|
+
this.logger = logger || new WyrmLogger();
|
|
58
|
+
this.retryConfig = { ...DEFAULT_RETRY_CONFIG, ...retryConfig };
|
|
59
|
+
this.circuitConfig = { ...DEFAULT_CIRCUIT_CONFIG, ...circuitConfig };
|
|
60
|
+
// Initialize checkpoint directory
|
|
61
|
+
this.checkpointDir = join(homedir(), '.wyrm', 'checkpoints');
|
|
62
|
+
if (!existsSync(this.checkpointDir)) {
|
|
63
|
+
mkdirSync(this.checkpointDir, { recursive: true });
|
|
64
|
+
}
|
|
65
|
+
// Recover any incomplete operations on startup
|
|
66
|
+
this.recoverIncompleteOperations();
|
|
67
|
+
}
|
|
68
|
+
// ==================== RETRY WITH BACKOFF ====================
|
|
69
|
+
/**
|
|
70
|
+
* Execute an operation with exponential backoff retry
|
|
71
|
+
*/
|
|
72
|
+
async withRetry(operation, operationName, config) {
|
|
73
|
+
const cfg = { ...this.retryConfig, ...config };
|
|
74
|
+
let lastError;
|
|
75
|
+
let attempts = 0;
|
|
76
|
+
while (attempts < cfg.maxAttempts) {
|
|
77
|
+
attempts++;
|
|
78
|
+
try {
|
|
79
|
+
// Check circuit breaker
|
|
80
|
+
if (!this.checkCircuit()) {
|
|
81
|
+
return {
|
|
82
|
+
success: false,
|
|
83
|
+
error: new Error('Circuit breaker is open - service unavailable'),
|
|
84
|
+
attempts,
|
|
85
|
+
recoverable: true,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
const result = await operation();
|
|
89
|
+
this.recordSuccess();
|
|
90
|
+
return {
|
|
91
|
+
success: true,
|
|
92
|
+
data: result,
|
|
93
|
+
attempts,
|
|
94
|
+
recoverable: false,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
lastError = error;
|
|
99
|
+
this.recordFailure();
|
|
100
|
+
const errorCode = error.code || '';
|
|
101
|
+
const errorMessage = error.message || '';
|
|
102
|
+
// Check if error is retryable
|
|
103
|
+
const isRetryable = cfg.retryableErrors?.some(code => errorCode.includes(code) || errorMessage.includes(code));
|
|
104
|
+
if (!isRetryable && attempts === 1) {
|
|
105
|
+
// Non-retryable error on first attempt
|
|
106
|
+
this.logger.error(`${operationName} failed with non-retryable error`, {
|
|
107
|
+
error: errorMessage,
|
|
108
|
+
code: errorCode,
|
|
109
|
+
});
|
|
110
|
+
return {
|
|
111
|
+
success: false,
|
|
112
|
+
error: lastError,
|
|
113
|
+
attempts,
|
|
114
|
+
recoverable: false,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
if (attempts < cfg.maxAttempts) {
|
|
118
|
+
const delay = Math.min(cfg.baseDelayMs * Math.pow(cfg.backoffMultiplier, attempts - 1), cfg.maxDelayMs);
|
|
119
|
+
this.logger.warn(`${operationName} failed, retrying in ${delay}ms`, {
|
|
120
|
+
attempt: attempts,
|
|
121
|
+
maxAttempts: cfg.maxAttempts,
|
|
122
|
+
error: errorMessage,
|
|
123
|
+
});
|
|
124
|
+
await this.sleep(delay);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
this.logger.error(`${operationName} failed after ${attempts} attempts`, {
|
|
129
|
+
error: lastError?.message,
|
|
130
|
+
});
|
|
131
|
+
return {
|
|
132
|
+
success: false,
|
|
133
|
+
error: lastError,
|
|
134
|
+
attempts,
|
|
135
|
+
recoverable: true,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Synchronous retry for database operations
|
|
140
|
+
*/
|
|
141
|
+
withRetrySync(operation, operationName, config) {
|
|
142
|
+
const cfg = { ...this.retryConfig, ...config };
|
|
143
|
+
let lastError;
|
|
144
|
+
let attempts = 0;
|
|
145
|
+
while (attempts < cfg.maxAttempts) {
|
|
146
|
+
attempts++;
|
|
147
|
+
try {
|
|
148
|
+
if (!this.checkCircuit()) {
|
|
149
|
+
return {
|
|
150
|
+
success: false,
|
|
151
|
+
error: new Error('Circuit breaker is open'),
|
|
152
|
+
attempts,
|
|
153
|
+
recoverable: true,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
const result = operation();
|
|
157
|
+
this.recordSuccess();
|
|
158
|
+
return {
|
|
159
|
+
success: true,
|
|
160
|
+
data: result,
|
|
161
|
+
attempts,
|
|
162
|
+
recoverable: false,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
catch (error) {
|
|
166
|
+
lastError = error;
|
|
167
|
+
this.recordFailure();
|
|
168
|
+
const errorCode = error.code || '';
|
|
169
|
+
const errorMessage = error.message || '';
|
|
170
|
+
const isRetryable = cfg.retryableErrors?.some(code => errorCode.includes(code) || errorMessage.includes(code));
|
|
171
|
+
if (!isRetryable && attempts === 1) {
|
|
172
|
+
return {
|
|
173
|
+
success: false,
|
|
174
|
+
error: lastError,
|
|
175
|
+
attempts,
|
|
176
|
+
recoverable: false,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
if (attempts < cfg.maxAttempts) {
|
|
180
|
+
const delay = Math.min(cfg.baseDelayMs * Math.pow(cfg.backoffMultiplier, attempts - 1), cfg.maxDelayMs);
|
|
181
|
+
this.logger.warn(`${operationName} sync retry ${attempts}/${cfg.maxAttempts}`);
|
|
182
|
+
this.sleepSync(delay);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return {
|
|
187
|
+
success: false,
|
|
188
|
+
error: lastError,
|
|
189
|
+
attempts,
|
|
190
|
+
recoverable: true,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
// ==================== CIRCUIT BREAKER ====================
|
|
194
|
+
/**
|
|
195
|
+
* Check if circuit allows operations
|
|
196
|
+
*/
|
|
197
|
+
checkCircuit() {
|
|
198
|
+
switch (this.circuitState) {
|
|
199
|
+
case 'closed':
|
|
200
|
+
return true;
|
|
201
|
+
case 'open':
|
|
202
|
+
// Check if timeout has passed
|
|
203
|
+
if (Date.now() - this.lastFailureTime >= this.circuitConfig.timeout) {
|
|
204
|
+
this.circuitState = 'half-open';
|
|
205
|
+
this.logger.info('Circuit breaker transitioning to half-open');
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
return false;
|
|
209
|
+
case 'half-open':
|
|
210
|
+
return true;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Record a successful operation
|
|
215
|
+
*/
|
|
216
|
+
recordSuccess() {
|
|
217
|
+
if (this.circuitState === 'half-open') {
|
|
218
|
+
this.successCount++;
|
|
219
|
+
if (this.successCount >= this.circuitConfig.successThreshold) {
|
|
220
|
+
this.circuitState = 'closed';
|
|
221
|
+
this.failureCount = 0;
|
|
222
|
+
this.successCount = 0;
|
|
223
|
+
this.logger.info('Circuit breaker closed - service recovered');
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
this.failureCount = 0;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Record a failed operation
|
|
232
|
+
*/
|
|
233
|
+
recordFailure() {
|
|
234
|
+
this.failureCount++;
|
|
235
|
+
this.lastFailureTime = Date.now();
|
|
236
|
+
if (this.circuitState === 'half-open') {
|
|
237
|
+
this.circuitState = 'open';
|
|
238
|
+
this.successCount = 0;
|
|
239
|
+
this.logger.warn('Circuit breaker reopened after half-open failure');
|
|
240
|
+
}
|
|
241
|
+
else if (this.failureCount >= this.circuitConfig.failureThreshold) {
|
|
242
|
+
this.circuitState = 'open';
|
|
243
|
+
this.logger.warn('Circuit breaker opened due to failure threshold', {
|
|
244
|
+
failures: this.failureCount,
|
|
245
|
+
threshold: this.circuitConfig.failureThreshold,
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Get circuit breaker status
|
|
251
|
+
*/
|
|
252
|
+
getCircuitStatus() {
|
|
253
|
+
return {
|
|
254
|
+
state: this.circuitState,
|
|
255
|
+
failures: this.failureCount,
|
|
256
|
+
lastFailure: this.lastFailureTime,
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Manually reset the circuit breaker
|
|
261
|
+
*/
|
|
262
|
+
resetCircuit() {
|
|
263
|
+
this.circuitState = 'closed';
|
|
264
|
+
this.failureCount = 0;
|
|
265
|
+
this.successCount = 0;
|
|
266
|
+
this.logger.info('Circuit breaker manually reset');
|
|
267
|
+
}
|
|
268
|
+
// ==================== CHECKPOINTING ====================
|
|
269
|
+
/**
|
|
270
|
+
* Create a checkpoint for an operation
|
|
271
|
+
*/
|
|
272
|
+
createCheckpoint(operationId, operation, stage, data) {
|
|
273
|
+
const checkpoint = {
|
|
274
|
+
id: operationId,
|
|
275
|
+
operation,
|
|
276
|
+
stage,
|
|
277
|
+
data,
|
|
278
|
+
timestamp: new Date().toISOString(),
|
|
279
|
+
completed: false,
|
|
280
|
+
};
|
|
281
|
+
const filePath = join(this.checkpointDir, `${operationId}.json`);
|
|
282
|
+
writeFileSync(filePath, JSON.stringify(checkpoint, null, 2));
|
|
283
|
+
this.activeOperations.set(operationId, checkpoint);
|
|
284
|
+
this.logger.debug(`Checkpoint created: ${operation}/${stage}`, { operationId });
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Update checkpoint stage
|
|
288
|
+
*/
|
|
289
|
+
updateCheckpoint(operationId, stage, data) {
|
|
290
|
+
const existing = this.activeOperations.get(operationId);
|
|
291
|
+
if (!existing) {
|
|
292
|
+
this.logger.warn('Checkpoint not found for update', { operationId });
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
const updated = {
|
|
296
|
+
...existing,
|
|
297
|
+
stage,
|
|
298
|
+
data: data ? { ...existing.data, ...data } : existing.data,
|
|
299
|
+
timestamp: new Date().toISOString(),
|
|
300
|
+
};
|
|
301
|
+
const filePath = join(this.checkpointDir, `${operationId}.json`);
|
|
302
|
+
writeFileSync(filePath, JSON.stringify(updated, null, 2));
|
|
303
|
+
this.activeOperations.set(operationId, updated);
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Complete a checkpoint (mark as done and remove)
|
|
307
|
+
*/
|
|
308
|
+
completeCheckpoint(operationId) {
|
|
309
|
+
const filePath = join(this.checkpointDir, `${operationId}.json`);
|
|
310
|
+
if (existsSync(filePath)) {
|
|
311
|
+
// Mark as completed before deletion
|
|
312
|
+
const checkpoint = this.activeOperations.get(operationId);
|
|
313
|
+
if (checkpoint) {
|
|
314
|
+
checkpoint.completed = true;
|
|
315
|
+
writeFileSync(filePath, JSON.stringify(checkpoint, null, 2));
|
|
316
|
+
}
|
|
317
|
+
// Move to completed folder or delete
|
|
318
|
+
try {
|
|
319
|
+
unlinkSync(filePath);
|
|
320
|
+
}
|
|
321
|
+
catch {
|
|
322
|
+
// Ignore deletion errors
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
this.activeOperations.delete(operationId);
|
|
326
|
+
this.logger.debug('Checkpoint completed', { operationId });
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Get a checkpoint by ID
|
|
330
|
+
*/
|
|
331
|
+
getCheckpoint(operationId) {
|
|
332
|
+
const cached = this.activeOperations.get(operationId);
|
|
333
|
+
if (cached)
|
|
334
|
+
return cached;
|
|
335
|
+
const filePath = join(this.checkpointDir, `${operationId}.json`);
|
|
336
|
+
if (existsSync(filePath)) {
|
|
337
|
+
try {
|
|
338
|
+
const data = JSON.parse(readFileSync(filePath, 'utf-8'));
|
|
339
|
+
return data;
|
|
340
|
+
}
|
|
341
|
+
catch {
|
|
342
|
+
return null;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
return null;
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Recover incomplete operations on startup
|
|
349
|
+
*/
|
|
350
|
+
recoverIncompleteOperations() {
|
|
351
|
+
try {
|
|
352
|
+
const files = readdirSync(this.checkpointDir);
|
|
353
|
+
const checkpoints = [];
|
|
354
|
+
for (const file of files) {
|
|
355
|
+
if (!file.endsWith('.json'))
|
|
356
|
+
continue;
|
|
357
|
+
const filePath = join(this.checkpointDir, file);
|
|
358
|
+
try {
|
|
359
|
+
const data = JSON.parse(readFileSync(filePath, 'utf-8'));
|
|
360
|
+
if (!data.completed) {
|
|
361
|
+
checkpoints.push(data);
|
|
362
|
+
this.activeOperations.set(data.id, data);
|
|
363
|
+
}
|
|
364
|
+
else {
|
|
365
|
+
// Clean up completed checkpoints
|
|
366
|
+
unlinkSync(filePath);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
catch {
|
|
370
|
+
// Corrupted checkpoint file, remove it
|
|
371
|
+
try {
|
|
372
|
+
unlinkSync(filePath);
|
|
373
|
+
}
|
|
374
|
+
catch {
|
|
375
|
+
// Ignore
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
if (checkpoints.length > 0) {
|
|
380
|
+
this.logger.warn('Found incomplete operations from previous run', {
|
|
381
|
+
count: checkpoints.length,
|
|
382
|
+
operations: checkpoints.map(c => c.operation),
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
catch {
|
|
387
|
+
// Checkpoint directory doesn't exist or not readable
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* Get all incomplete operations for recovery
|
|
392
|
+
*/
|
|
393
|
+
getIncompleteOperations() {
|
|
394
|
+
return Array.from(this.activeOperations.values()).filter(c => !c.completed);
|
|
395
|
+
}
|
|
396
|
+
// ==================== TRANSACTION WRAPPER ====================
|
|
397
|
+
/**
|
|
398
|
+
* Execute a database transaction with automatic rollback on failure
|
|
399
|
+
*/
|
|
400
|
+
executeTransaction(db, operations) {
|
|
401
|
+
const operationId = `txn-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
|
402
|
+
this.createCheckpoint(operationId, 'transaction', 'begin', {});
|
|
403
|
+
try {
|
|
404
|
+
db.prepare('BEGIN IMMEDIATE').run();
|
|
405
|
+
this.updateCheckpoint(operationId, 'executing');
|
|
406
|
+
const result = operations();
|
|
407
|
+
db.prepare('COMMIT').run();
|
|
408
|
+
this.completeCheckpoint(operationId);
|
|
409
|
+
return {
|
|
410
|
+
success: true,
|
|
411
|
+
data: result,
|
|
412
|
+
attempts: 1,
|
|
413
|
+
recoverable: false,
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
catch (error) {
|
|
417
|
+
this.logger.error('Transaction failed, rolling back', {
|
|
418
|
+
operationId,
|
|
419
|
+
error: error.message,
|
|
420
|
+
});
|
|
421
|
+
try {
|
|
422
|
+
db.prepare('ROLLBACK').run();
|
|
423
|
+
}
|
|
424
|
+
catch (rollbackError) {
|
|
425
|
+
this.logger.error('Rollback failed', {
|
|
426
|
+
error: rollbackError.message,
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
this.updateCheckpoint(operationId, 'rolled_back', {
|
|
430
|
+
error: error.message,
|
|
431
|
+
});
|
|
432
|
+
return {
|
|
433
|
+
success: false,
|
|
434
|
+
error: error,
|
|
435
|
+
attempts: 1,
|
|
436
|
+
recoverable: true,
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
// ==================== SAFE FILE OPERATIONS ====================
|
|
441
|
+
/**
|
|
442
|
+
* Write file atomically (write to temp, then rename)
|
|
443
|
+
*/
|
|
444
|
+
atomicWriteFile(filePath, content) {
|
|
445
|
+
const tempPath = `${filePath}.tmp.${Date.now()}`;
|
|
446
|
+
try {
|
|
447
|
+
writeFileSync(tempPath, content, { encoding: 'utf-8', flag: 'w' });
|
|
448
|
+
renameSync(tempPath, filePath);
|
|
449
|
+
return true;
|
|
450
|
+
}
|
|
451
|
+
catch (error) {
|
|
452
|
+
this.logger.error('Atomic write failed', {
|
|
453
|
+
filePath,
|
|
454
|
+
error: error.message,
|
|
455
|
+
});
|
|
456
|
+
// Clean up temp file if it exists
|
|
457
|
+
try {
|
|
458
|
+
if (existsSync(tempPath)) {
|
|
459
|
+
unlinkSync(tempPath);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
catch {
|
|
463
|
+
// Ignore cleanup errors
|
|
464
|
+
}
|
|
465
|
+
return false;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* Read file with fallback to backup
|
|
470
|
+
*/
|
|
471
|
+
safeReadFile(filePath, backupPath) {
|
|
472
|
+
// Try primary file
|
|
473
|
+
try {
|
|
474
|
+
if (existsSync(filePath)) {
|
|
475
|
+
return readFileSync(filePath, 'utf-8');
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
catch {
|
|
479
|
+
this.logger.warn('Failed to read primary file', { filePath });
|
|
480
|
+
}
|
|
481
|
+
// Try backup if provided
|
|
482
|
+
if (backupPath) {
|
|
483
|
+
try {
|
|
484
|
+
if (existsSync(backupPath)) {
|
|
485
|
+
this.logger.info('Using backup file', { backupPath });
|
|
486
|
+
return readFileSync(backupPath, 'utf-8');
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
catch {
|
|
490
|
+
this.logger.error('Failed to read backup file', { backupPath });
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
return null;
|
|
494
|
+
}
|
|
495
|
+
// ==================== UTILITIES ====================
|
|
496
|
+
sleep(ms) {
|
|
497
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
498
|
+
}
|
|
499
|
+
sleepSync(ms) {
|
|
500
|
+
const end = Date.now() + ms;
|
|
501
|
+
while (Date.now() < end) {
|
|
502
|
+
// Busy wait - not ideal but necessary for sync operations
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* Generate a unique operation ID
|
|
507
|
+
*/
|
|
508
|
+
generateOperationId(prefix = 'op') {
|
|
509
|
+
return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
// ==================== SINGLETON INSTANCE ====================
|
|
513
|
+
let _resilienceManager = null;
|
|
514
|
+
export function getResilienceManager() {
|
|
515
|
+
if (!_resilienceManager) {
|
|
516
|
+
_resilienceManager = new ResilienceManager();
|
|
517
|
+
}
|
|
518
|
+
return _resilienceManager;
|
|
519
|
+
}
|
|
520
|
+
// ==================== HELPER DECORATORS ====================
|
|
521
|
+
/**
|
|
522
|
+
* Decorator to add retry logic to a method
|
|
523
|
+
*/
|
|
524
|
+
export function withRetry(config) {
|
|
525
|
+
return function (_target, propertyKey, descriptor) {
|
|
526
|
+
const originalMethod = descriptor.value;
|
|
527
|
+
descriptor.value = async function (...args) {
|
|
528
|
+
const manager = getResilienceManager();
|
|
529
|
+
const result = await manager.withRetry(() => originalMethod.apply(this, args), propertyKey, config);
|
|
530
|
+
if (!result.success) {
|
|
531
|
+
throw result.error;
|
|
532
|
+
}
|
|
533
|
+
return result.data;
|
|
534
|
+
};
|
|
535
|
+
return descriptor;
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
/**
|
|
539
|
+
* Decorator to add checkpointing to a method
|
|
540
|
+
*/
|
|
541
|
+
export function withCheckpoint(operationName) {
|
|
542
|
+
return function (_target, _propertyKey, descriptor) {
|
|
543
|
+
const originalMethod = descriptor.value;
|
|
544
|
+
descriptor.value = async function (...args) {
|
|
545
|
+
const manager = getResilienceManager();
|
|
546
|
+
const operationId = manager.generateOperationId(operationName);
|
|
547
|
+
manager.createCheckpoint(operationId, operationName, 'started', { args });
|
|
548
|
+
try {
|
|
549
|
+
const result = await originalMethod.apply(this, args);
|
|
550
|
+
manager.completeCheckpoint(operationId);
|
|
551
|
+
return result;
|
|
552
|
+
}
|
|
553
|
+
catch (error) {
|
|
554
|
+
manager.updateCheckpoint(operationId, 'failed', {
|
|
555
|
+
error: error.message,
|
|
556
|
+
});
|
|
557
|
+
throw error;
|
|
558
|
+
}
|
|
559
|
+
};
|
|
560
|
+
return descriptor;
|
|
561
|
+
};
|
|
562
|
+
}
|
|
563
|
+
//# sourceMappingURL=resilience.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resilience.js","sourceRoot":"","sources":["../src/resilience.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;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CAAC;QAEF,OAAO,UAAU,CAAC;IACpB,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wyrm Security Module - Input validation and path security
|
|
3
|
+
*
|
|
4
|
+
* @copyright 2026 Ghost Protocol (Pvt) Ltd. All Rights Reserved.
|
|
5
|
+
* @license Proprietary - See LICENSE file for details.
|
|
6
|
+
* @module security
|
|
7
|
+
* @version 3.0.0
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Validate and sanitize a path to prevent traversal attacks
|
|
11
|
+
*/
|
|
12
|
+
export declare function validatePath(basePath: string, targetPath: string): string;
|
|
13
|
+
/**
|
|
14
|
+
* Check if a path is a valid directory
|
|
15
|
+
*/
|
|
16
|
+
export declare function validateDirectory(path: string): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Validate project path is within allowed directories
|
|
19
|
+
*/
|
|
20
|
+
export declare function validateProjectPath(projectPath: string): string;
|
|
21
|
+
/**
|
|
22
|
+
* Sanitize FTS5 search query to prevent injection
|
|
23
|
+
*/
|
|
24
|
+
export declare function sanitizeFtsQuery(query: string): string;
|
|
25
|
+
/**
|
|
26
|
+
* Validate and sanitize string input
|
|
27
|
+
*/
|
|
28
|
+
export declare function sanitizeString(input: unknown, maxLength?: number): string;
|
|
29
|
+
/**
|
|
30
|
+
* Validate integer input
|
|
31
|
+
*/
|
|
32
|
+
export declare function validateInt(input: unknown, min?: number, max?: number): number;
|
|
33
|
+
/**
|
|
34
|
+
* Validate enum value
|
|
35
|
+
*/
|
|
36
|
+
export declare function validateEnum<T extends string>(input: unknown, allowed: readonly T[], defaultValue?: T): T;
|
|
37
|
+
/**
|
|
38
|
+
* Validate API key authentication
|
|
39
|
+
*/
|
|
40
|
+
export declare function validateApiKey(authHeader: string | undefined, expectedHash: string): boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Hash a token for storage/comparison
|
|
43
|
+
*/
|
|
44
|
+
export declare function hashToken(token: string): string;
|
|
45
|
+
/**
|
|
46
|
+
* Constant-time string comparison to prevent timing attacks
|
|
47
|
+
*/
|
|
48
|
+
export declare function constantTimeCompare(a: string, b: string): boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Check rate limit for a given key (IP, API key, etc.)
|
|
51
|
+
*/
|
|
52
|
+
export declare function checkRateLimit(key: string, maxRequests?: number, windowMs?: number): {
|
|
53
|
+
allowed: boolean;
|
|
54
|
+
remaining: number;
|
|
55
|
+
resetAt: number;
|
|
56
|
+
};
|
|
57
|
+
export declare class SecurityError extends Error {
|
|
58
|
+
code: string;
|
|
59
|
+
constructor(message: string, code: string);
|
|
60
|
+
}
|
|
61
|
+
export declare const MAX_REQUEST_SIZE: number;
|
|
62
|
+
export declare const MAX_BATCH_SIZE = 1000;
|
|
63
|
+
export declare const MAX_QUERY_RESULTS = 1000;
|
|
64
|
+
/**
|
|
65
|
+
* Validate batch operation size
|
|
66
|
+
*/
|
|
67
|
+
export declare function validateBatchSize(items: unknown[]): void;
|
|
68
|
+
//# sourceMappingURL=security.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"security.d.ts","sourceRoot":"","sources":["../src/security.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AASH;;GAEG;AACH,wBAAgB,YAAY,CAAC,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CAiBzE;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAMvD;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAsC/D;AAID;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAkBtD;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,SAAQ,GAAG,MAAM,CAWxE;AAED;;GAEG;AACH,wBAAgB,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