@less-is-more/less-js 1.5.0-9 → 1.5.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/package.json +1 -1
- package/src/cache.js +19 -12
- package/src/nas.js +31 -19
package/package.json
CHANGED
package/src/cache.js
CHANGED
|
@@ -103,10 +103,14 @@ module.exports = class Cache {
|
|
|
103
103
|
} else {
|
|
104
104
|
console.log("Found cache" + (hasLocal ? " local" : ""), fullKey);
|
|
105
105
|
savedData = Cache._unzip(savedData);
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
106
|
+
if (typeof savedData === "string") {
|
|
107
|
+
// 优先转成json
|
|
108
|
+
try {
|
|
109
|
+
return JSON.parse(savedData);
|
|
110
|
+
} catch (e) {
|
|
111
|
+
return savedData;
|
|
112
|
+
}
|
|
113
|
+
} else {
|
|
110
114
|
return savedData;
|
|
111
115
|
}
|
|
112
116
|
}
|
|
@@ -118,16 +122,19 @@ module.exports = class Cache {
|
|
|
118
122
|
}
|
|
119
123
|
|
|
120
124
|
static _unzip(content) {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
125
|
+
if (typeof content === "string") {
|
|
126
|
+
let text = "";
|
|
127
|
+
const buffer = Buffer.from(content, "base64");
|
|
128
|
+
// 判断是否gzip
|
|
129
|
+
if (buffer.length > 2 && buffer[0] === 0x1f && buffer[1] === 0x8b) {
|
|
130
|
+
text = zlib.gunzipSync(buffer).toString();
|
|
131
|
+
} else {
|
|
132
|
+
text = content;
|
|
133
|
+
}
|
|
134
|
+
return text;
|
|
127
135
|
} else {
|
|
128
|
-
|
|
136
|
+
return content;
|
|
129
137
|
}
|
|
130
|
-
return text;
|
|
131
138
|
}
|
|
132
139
|
|
|
133
140
|
static _needZip(text, zip) {
|
package/src/nas.js
CHANGED
|
@@ -115,8 +115,6 @@ class Nas {
|
|
|
115
115
|
return valueStr;
|
|
116
116
|
}
|
|
117
117
|
} else {
|
|
118
|
-
// 文件已过期,删除文件
|
|
119
|
-
Nas._deleteCacheFile(key);
|
|
120
118
|
return null;
|
|
121
119
|
}
|
|
122
120
|
}
|
|
@@ -141,11 +139,25 @@ class Nas {
|
|
|
141
139
|
const filePath = Nas._getFilePath(key);
|
|
142
140
|
|
|
143
141
|
try {
|
|
142
|
+
// 创建临时文件名,避免写入过程中的并发问题
|
|
143
|
+
const timestamp = Date.now();
|
|
144
|
+
const randomSuffix = Math.random().toFixed(4) * 10000;
|
|
145
|
+
const tempFilePath = `${filePath}.tmp.${timestamp}.${randomSuffix}`;
|
|
144
146
|
// 写入过期时间戳和值,用换行符分隔
|
|
145
147
|
const fileContent = `${expireTime}\n${content}`;
|
|
146
|
-
fs.writeFileSync(
|
|
148
|
+
fs.writeFileSync(tempFilePath, fileContent, "utf8");
|
|
149
|
+
// 原子性地替换原文件
|
|
150
|
+
fs.renameSync(tempFilePath, filePath);
|
|
147
151
|
} catch (e) {
|
|
148
152
|
console.error("保存缓存文件失败:", filePath, e);
|
|
153
|
+
// 如果临时文件创建失败,清理临时文件
|
|
154
|
+
try {
|
|
155
|
+
if (fs.existsSync(tempFilePath)) {
|
|
156
|
+
fs.unlinkSync(tempFilePath);
|
|
157
|
+
}
|
|
158
|
+
} catch (cleanupError) {
|
|
159
|
+
// 忽略清理错误
|
|
160
|
+
}
|
|
149
161
|
}
|
|
150
162
|
}
|
|
151
163
|
|
|
@@ -186,23 +198,25 @@ class Nas {
|
|
|
186
198
|
for (const file of files) {
|
|
187
199
|
if (file.endsWith(".cache")) {
|
|
188
200
|
const filePath = path.join(cacheDir, file);
|
|
189
|
-
//
|
|
201
|
+
// 检查文件是否过期超过一分钟,如果超过则删除
|
|
190
202
|
try {
|
|
191
|
-
|
|
192
|
-
|
|
203
|
+
if (fs.existsSync(filePath)) {
|
|
204
|
+
const content = fs.readFileSync(filePath, "utf8");
|
|
205
|
+
const lines = content.split("\n");
|
|
193
206
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
207
|
+
if (lines.length >= 1) {
|
|
208
|
+
const expireTimeString = lines[0];
|
|
209
|
+
const expireTime = parseInt(expireTimeString);
|
|
197
210
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
211
|
+
if (
|
|
212
|
+
!isNaN(expireTime) &&
|
|
213
|
+
expireTime !== -1 &&
|
|
214
|
+
// 仅删除过期超过一分钟的文件
|
|
215
|
+
Date.now() > expireTime + 60 * 1000
|
|
216
|
+
) {
|
|
217
|
+
console.log("删除过期缓存文件:", filePath);
|
|
218
|
+
fs.unlinkSync(filePath);
|
|
219
|
+
}
|
|
206
220
|
}
|
|
207
221
|
}
|
|
208
222
|
} catch (e) {
|
|
@@ -268,8 +282,6 @@ class Nas {
|
|
|
268
282
|
const remainingTime = Math.floor((expireTime - Date.now()) / 1000);
|
|
269
283
|
return Math.max(0, remainingTime); // 确保返回非负值
|
|
270
284
|
} else {
|
|
271
|
-
// 文件已过期,删除文件
|
|
272
|
-
Nas._deleteCacheFile(key);
|
|
273
285
|
return -1;
|
|
274
286
|
}
|
|
275
287
|
}
|