Skip to content

Commit 0e7561a

Browse files
committed
feat: Node.js 环境中 JSON 数据文件校验失败后会备份原文件, 创建新文件
1 parent 6804c63 commit 0e7561a

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

backend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sub-store",
3-
"version": "2.19.48",
3+
"version": "2.19.49",
44
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.",
55
"main": "src/main.js",
66
"scripts": {

backend/src/vendor/open-api.js

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ const isEgern = 'object' == typeof egern;
1010
const isLanceX = 'undefined' != typeof $native;
1111
const isGUIforCores = typeof $Plugins !== 'undefined';
1212

13+
function isPlainObject(obj) {
14+
return (
15+
obj !== null &&
16+
typeof obj === 'object' &&
17+
[null, Object.prototype].includes(Object.getPrototypeOf(obj))
18+
);
19+
}
20+
1321
export class OpenAPI {
1422
constructor(name = 'untitled', debug = false) {
1523
this.name = name;
@@ -62,29 +70,50 @@ export class OpenAPI {
6270
const basePath =
6371
eval('process.env.SUB_STORE_DATA_BASE_PATH') || '.';
6472
let rootPath = `${basePath}/root.json`;
73+
const backupRootPath = `${basePath}/root_${Date.now()}.json`;
6574

6675
this.log(`Root path: ${rootPath}`);
67-
if (!this.node.fs.existsSync(rootPath)) {
76+
if (this.node.fs.existsSync(rootPath)) {
77+
try {
78+
this.root = JSON.parse(
79+
this.node.fs.readFileSync(`${rootPath}`),
80+
);
81+
} catch (e) {
82+
this.node.fs.copyFileSync(rootPath, backupRootPath);
83+
this.error(
84+
`Failed to parse ${rootPath}: ${e.message}. Backup created at ${backupRootPath}`,
85+
);
86+
}
87+
}
88+
if (!isPlainObject(this.root)) {
6889
this.node.fs.writeFileSync(rootPath, JSON.stringify({}), {
69-
flag: 'wx',
90+
flag: 'w',
7091
});
7192
this.root = {};
72-
} else {
73-
this.root = JSON.parse(
74-
this.node.fs.readFileSync(`${rootPath}`),
75-
);
7693
}
7794

7895
// create a json file with the given name if not exists
7996
let fpath = `${basePath}/${this.name}.json`;
97+
const backupPath = `${basePath}/${this.name}_${Date.now()}.json`;
98+
8099
this.log(`Data path: ${fpath}`);
81-
if (!this.node.fs.existsSync(fpath)) {
100+
if (this.node.fs.existsSync(fpath)) {
101+
try {
102+
this.cache = JSON.parse(
103+
this.node.fs.readFileSync(`${fpath}`),
104+
);
105+
} catch (e) {
106+
this.node.fs.copyFileSync(fpath, backupPath);
107+
this.error(
108+
`Failed to parse ${fpath}: ${e.message}. Backup created at ${backupPath}`,
109+
);
110+
}
111+
}
112+
if (!isPlainObject(this.cache)) {
82113
this.node.fs.writeFileSync(fpath, JSON.stringify({}), {
83-
flag: 'wx',
114+
flag: 'w',
84115
});
85116
this.cache = {};
86-
} else {
87-
this.cache = JSON.parse(this.node.fs.readFileSync(`${fpath}`));
88117
}
89118
}
90119
}

backend/sub-store_1748083027961.json

Whitespace-only changes.

0 commit comments

Comments
 (0)