-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.js
More file actions
171 lines (137 loc) · 5.23 KB
/
utils.js
File metadata and controls
171 lines (137 loc) · 5.23 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
const fs = require('fs-extra');
let config = null;
const LOG_LEVELS = {
'ERROR': 0,
'WARN': 1,
'INFO': 2,
'DEBUG': 3
};
function setConfig(appConfig) {
config = appConfig;
}
function formatTime(date = new Date(), timezone = 'Asia/Shanghai') {
try {
const targetTimezone = config?.timezone || timezone;
const d = new Date(date);
const formatter = new Intl.DateTimeFormat('zh-CN', {
timeZone: targetTimezone,
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false
});
const parts = formatter.formatToParts(d);
const year = parts.find(part => part.type === 'year').value;
const month = parts.find(part => part.type === 'month').value;
const day = parts.find(part => part.type === 'day').value;
const hour = parts.find(part => part.type === 'hour').value;
const minute = parts.find(part => part.type === 'minute').value;
const second = parts.find(part => part.type === 'second').value;
return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
} catch (error) {
const d = new Date(date);
const year = d.getFullYear();
const month = String(d.getMonth() + 1).padStart(2, '0');
const day = String(d.getDate()).padStart(2, '0');
const hours = String(d.getHours()).padStart(2, '0');
const minutes = String(d.getMinutes()).padStart(2, '0');
const seconds = String(d.getSeconds()).padStart(2, '0');
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
}
function log(message, type = 'INFO', module = 'APP', req = null) {
if (!config || !config.logging.enabled) {
return;
}
const currentLevel = LOG_LEVELS[config.logging.level] || LOG_LEVELS['INFO'];
const messageLevel = LOG_LEVELS[type] || LOG_LEVELS['INFO'];
if (messageLevel > currentLevel) {
return;
}
const timestamp = formatTime();
const workerId = process.env.WORKER_ID || (require('cluster').worker ? require('cluster').worker.id : 'master');
let logMessage = `[${timestamp}] [${type}] [${module}] [W${workerId}]`;
if (req) {
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress || 'unknown';
const url = req.url || '';
logMessage += ` ${ip} ${url}`;
}
logMessage += ` ${message}`;
console.log(logMessage);
}
function getCurrentTimestamp() {
return formatTime();
}
function isoToLocal(isoString) {
return formatTime(new Date(isoString));
}
async function safeReadJson(filePath, defaultValue = null) {
try {
if (!(await fs.pathExists(filePath))) {
log(`JSON file does not exist: ${filePath}`, 'DEBUG', 'UTILS');
return defaultValue;
}
const content = await fs.readFile(filePath, 'utf-8');
if (!content || content.trim().length === 0) {
log(`JSON file is empty: ${filePath}`, 'WARN', 'UTILS');
return defaultValue;
}
const data = JSON.parse(content);
log(`Successfully read JSON file: ${filePath}`, 'DEBUG', 'UTILS');
return data;
} catch (error) {
log(`Error reading JSON file ${filePath}: ${error.message}`, 'ERROR', 'UTILS');
return defaultValue;
}
}
async function safeWriteJson(filePath, data, options = {}) {
const lockFile = `${filePath}.lock`;
const tempFile = `${filePath}.tmp`;
const maxRetries = 5;
const retryDelay = 100;
for (let i = 0; i < maxRetries; i++) {
try {
if (await fs.pathExists(lockFile)) {
log(`JSON file is locked, waiting... (attempt ${i + 1})`, 'DEBUG', 'UTILS');
await new Promise(resolve => setTimeout(resolve, retryDelay));
continue;
}
await fs.writeFile(lockFile, process.pid.toString());
await fs.writeJson(tempFile, data, { spaces: 2, ...options });
await fs.move(tempFile, filePath, { overwrite: true });
await fs.unlink(lockFile);
log(`Successfully wrote JSON file: ${filePath}`, 'DEBUG', 'UTILS');
return true;
} catch (error) {
log(`Error writing JSON file ${filePath} (attempt ${i + 1}): ${error.message}`, 'ERROR', 'UTILS');
try {
if (await fs.pathExists(tempFile)) {
await fs.unlink(tempFile);
}
if (await fs.pathExists(lockFile)) {
await fs.unlink(lockFile);
}
} catch (cleanupError) {
// 忽略清理错误
}
if (i === maxRetries - 1) {
throw error;
}
await new Promise(resolve => setTimeout(resolve, retryDelay));
}
}
return false;
}
module.exports = {
setConfig,
formatTime,
log,
getCurrentTimestamp,
isoToLocal,
safeReadJson,
safeWriteJson,
LOG_LEVELS
};