forked from AubaidFarrukh/smart-retry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileStore.ts
More file actions
123 lines (102 loc) · 3.16 KB
/
Copy pathfileStore.ts
File metadata and controls
123 lines (102 loc) · 3.16 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
/** @format */
import fs from 'fs';
import path from 'path';
import { FailedRequest, FileStoreConfig } from './types';
export class FileStore {
private filePath: string;
private maxFileSizeBytes?: number;
private maxFiles: number;
constructor(filePath?: string, storeConfig?: FileStoreConfig) {
this.filePath = filePath || path.join(process.cwd(), 'smart-retry-log.json');
this.maxFileSizeBytes = storeConfig?.maxFileSizeBytes;
this.maxFiles = storeConfig?.maxFiles ?? 5;
this.ensureFileExists();
}
private ensureFileExists(): void {
try {
if (!fs.existsSync(this.filePath)) {
fs.writeFileSync(this.filePath, JSON.stringify([], null, 2), 'utf-8');
}
} catch {
// fs unavailable or read-only (browser, edge runtime, serverless FS); degrade to no-op.
}
}
async save(request: FailedRequest): Promise<void> {
try {
this.rotateIfNeeded();
const logs = await this.loadAll();
logs.push(request);
fs.writeFileSync(this.filePath, JSON.stringify(logs, null, 2), 'utf-8');
} catch {
// best-effort logging; ignore write failures
}
}
private rotateIfNeeded(): void {
if (!this.maxFileSizeBytes) return;
try {
const stats = fs.statSync(this.filePath);
if (stats.size >= this.maxFileSizeBytes) {
this.rotateFiles();
}
} catch {
// File might not exist yet, ignore stat errors
}
}
async loadAll(): Promise<FailedRequest[]> {
try {
const content = fs.readFileSync(this.filePath, 'utf-8');
return JSON.parse(content);
} catch {
return [];
}
}
async findById(id: string): Promise<FailedRequest | undefined> {
const logs = await this.loadAll();
return logs.find((log) => log.id === id);
}
async remove(id: string): Promise<boolean> {
try {
const logs = await this.loadAll();
const filtered = logs.filter((log) => log.id !== id);
if (filtered.length === logs.length) {
return false;
}
fs.writeFileSync(this.filePath, JSON.stringify(filtered, null, 2), 'utf-8');
return true;
} catch {
return false;
}
}
async clear(): Promise<void> {
try {
fs.writeFileSync(this.filePath, JSON.stringify([], null, 2), 'utf-8');
} catch {
// best-effort logging; ignore write failures
}
}
async count(): Promise<number> {
const logs = await this.loadAll();
return logs.length;
}
getFilePath(): string {
return this.filePath;
}
private rotateFiles(): void {
const dir = path.dirname(this.filePath);
const baseName = path.basename(this.filePath, '.json');
const oldestFile = path.join(dir, `${baseName}.${this.maxFiles}.json`);
if (fs.existsSync(oldestFile)) {
fs.unlinkSync(oldestFile);
}
for (let i = this.maxFiles - 1; i >= 1; i--) {
const current = path.join(dir, `${baseName}.${i}.json`);
const next = path.join(dir, `${baseName}.${i + 1}.json`);
if (fs.existsSync(current)) {
fs.renameSync(current, next);
}
}
const rotatedName = path.join(dir, `${baseName}.1.json`);
fs.renameSync(this.filePath, rotatedName);
this.ensureFileExists();
}
}