Skip to content

Commit e6cb6c9

Browse files
committed
feat: YAML 依赖更换
1 parent c4dd762 commit e6cb6c9

7 files changed

Lines changed: 130 additions & 333 deletions

File tree

backend/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sub-store",
3-
"version": "2.24.22",
3+
"version": "2.25.0",
44
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.",
55
"main": "src/main.js",
66
"packageManager": "pnpm@11.0.9",
@@ -45,8 +45,8 @@
4545
"ms": "^2.1.3",
4646
"nanoid": "^3.3.3",
4747
"semver": "^7.6.3",
48-
"static-js-yaml": "^1.0.0",
49-
"undici": "^7.4.0"
48+
"undici": "^7.4.0",
49+
"yaml": "^2.9.0"
5050
},
5151
"devDependencies": {
5252
"@babel/core": "^7.18.0",

backend/pnpm-lock.yaml

Lines changed: 10 additions & 322 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/src/restful/collections.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import $ from '@/core/app';
1212
import { RequestInvalidError, ResourceNotFoundError } from '@/restful/errors';
1313
import { formatDateTime } from '@/utils';
1414
import { normalizeAgePublicKeyConfig } from '@/utils/age';
15+
import { normalizeEditorLanguageConfig } from '@/utils/editor-language';
1516

1617
export default function register($app) {
1718
if (!$.read(COLLECTIONS_KEY)) $.write({}, COLLECTIONS_KEY);
@@ -81,6 +82,7 @@ function updateCollection(req, res) {
8182
...collection,
8283
};
8384
normalizeAgePublicKeyConfig(newCol);
85+
normalizeEditorLanguageConfig(newCol);
8486
$.info(`正在更新组合订阅:${name}...`);
8587

8688
if (name !== newCol.name) {
@@ -145,7 +147,10 @@ function getAllCollections(req, res) {
145147
function replaceCollection(req, res) {
146148
try {
147149
const allCols = req.body;
148-
allCols.forEach(normalizeAgePublicKeyConfig);
150+
allCols.forEach((collection) => {
151+
normalizeAgePublicKeyConfig(collection);
152+
normalizeEditorLanguageConfig(collection);
153+
});
149154
$.write(allCols, COLLECTIONS_KEY);
150155
success(res);
151156
} catch (error) {
@@ -155,6 +160,7 @@ function replaceCollection(req, res) {
155160

156161
function createCollectionItem(collection) {
157162
normalizeAgePublicKeyConfig(collection);
163+
normalizeEditorLanguageConfig(collection);
158164
$.info(`正在创建组合订阅:${collection.name}`);
159165
if (/\//.test(collection.name)) {
160166
throw new RequestInvalidError(

backend/src/restful/file.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
} from '@/restful/age-output';
2525
import { findShareToken } from '@/restful/token';
2626
import { maskAgeSecretInUrl, normalizeAgePublicKeyConfig } from '@/utils/age';
27+
import { normalizeEditorLanguageConfig } from '@/utils/editor-language';
2728

2829
export default function register($app) {
2930
if (!$.read(FILES_KEY)) $.write([], FILES_KEY);
@@ -381,6 +382,7 @@ function updateFile(req, res) {
381382
...file,
382383
};
383384
normalizeAgePublicKeyConfig(newFile);
385+
normalizeEditorLanguageConfig(newFile);
384386
$.info(`正在更新文件:${name}...`);
385387

386388
if (name !== newFile.name) {
@@ -442,7 +444,10 @@ function getAllWholeFiles(req, res) {
442444
function replaceFile(req, res) {
443445
try {
444446
const allFiles = req.body;
445-
allFiles.forEach(normalizeAgePublicKeyConfig);
447+
allFiles.forEach((file) => {
448+
normalizeAgePublicKeyConfig(file);
449+
normalizeEditorLanguageConfig(file);
450+
});
446451
$.write(allFiles, FILES_KEY);
447452
success(res);
448453
} catch (error) {
@@ -455,6 +460,7 @@ function createFileItem(rawFile) {
455460
...rawFile,
456461
};
457462
normalizeAgePublicKeyConfig(file);
463+
normalizeEditorLanguageConfig(file);
458464
file.name = `${file.name ?? Date.now()}`;
459465
$.info(`正在创建文件:${file.name}`);
460466
const allFiles = $.read(FILES_KEY);

backend/src/restful/subscriptions.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { success, failed } from './response';
2727
import $ from '@/core/app';
2828
import { formatDateTime } from '@/utils';
2929
import { maskAgeSecretInUrl, normalizeAgePublicKeyConfig } from '@/utils/age';
30+
import { normalizeEditorLanguageConfig } from '@/utils/editor-language';
3031

3132
if (!$.read(SUBS_KEY)) $.write({}, SUBS_KEY);
3233

@@ -292,6 +293,7 @@ function updateSubscription(req, res) {
292293
...sub,
293294
};
294295
normalizeAgePublicKeyConfig(newSub);
296+
normalizeEditorLanguageConfig(newSub);
295297
$.info(`正在更新订阅: ${name}`);
296298
// allow users to update the subscription name
297299
if (name !== sub.name) {
@@ -366,7 +368,10 @@ function getAllSubscriptions(req, res) {
366368
function replaceSubscriptions(req, res) {
367369
try {
368370
const allSubs = req.body;
369-
allSubs.forEach(normalizeAgePublicKeyConfig);
371+
allSubs.forEach((sub) => {
372+
normalizeAgePublicKeyConfig(sub);
373+
normalizeEditorLanguageConfig(sub);
374+
});
370375
$.write(allSubs, SUBS_KEY);
371376
success(res);
372377
} catch (error) {
@@ -379,6 +384,7 @@ function createSubscriptionItem(rawSub) {
379384
...rawSub,
380385
};
381386
normalizeAgePublicKeyConfig(sub);
387+
normalizeEditorLanguageConfig(sub);
382388
delete sub.subscriptions;
383389
$.info(`正在创建订阅: ${sub.name}`);
384390
if (/\//.test(sub.name)) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const EDITOR_LANGUAGE_IDS = new Set([
2+
'javascript',
3+
'json',
4+
'json5',
5+
'yaml',
6+
'ini',
7+
'plaintext',
8+
]);
9+
10+
const LANGUAGE_FIELDS = ['editorLanguage', 'previewLanguage'];
11+
12+
function normalizeEditorLanguageField(target) {
13+
if (!target || typeof target !== 'object') return;
14+
15+
for (const field of LANGUAGE_FIELDS) {
16+
if (!EDITOR_LANGUAGE_IDS.has(target[field])) {
17+
delete target[field];
18+
}
19+
}
20+
}
21+
22+
export function normalizeEditorLanguageConfig(config) {
23+
normalizeEditorLanguageField(config);
24+
25+
if (!Array.isArray(config?.process)) return;
26+
27+
config.process.forEach((item) => {
28+
normalizeEditorLanguageField(item?.args);
29+
});
30+
}

backend/src/utils/yaml.js

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import YAML from 'static-js-yaml';
1+
import { parse as parseYaml, stringify as stringifyYaml } from 'yaml';
2+
3+
const DEFAULT_PARSE_OPTIONS = {
4+
logLevel: 'error',
5+
merge: true,
6+
};
27

38
function retry(fn, content, ...args) {
49
try {
@@ -16,17 +21,73 @@ function retry(fn, content, ...args) {
1621
}
1722
}
1823

24+
function toSerializable(content) {
25+
return JSON.parse(JSON.stringify(content));
26+
}
27+
28+
function normalizeParseOptions(options) {
29+
return {
30+
...DEFAULT_PARSE_OPTIONS,
31+
...(options || {}),
32+
};
33+
}
34+
35+
function normalizeStringifyOptions(options = {}) {
36+
const {
37+
forceQuotes,
38+
lineWidth,
39+
noArrayIndent,
40+
noRefs,
41+
quotingType,
42+
sortKeys,
43+
...rest
44+
} = options || {};
45+
const normalized = { ...rest };
46+
47+
if (typeof lineWidth === 'number') {
48+
normalized.lineWidth = lineWidth <= 0 ? 0 : lineWidth;
49+
}
50+
if (typeof noArrayIndent === 'boolean') {
51+
normalized.indentSeq = !noArrayIndent;
52+
}
53+
if (typeof noRefs === 'boolean') {
54+
normalized.aliasDuplicateObjects = !noRefs;
55+
}
56+
if (typeof sortKeys !== 'undefined') {
57+
normalized.sortMapEntries = sortKeys;
58+
}
59+
if (quotingType === "'") {
60+
normalized.singleQuote = true;
61+
} else if (quotingType === '"') {
62+
normalized.singleQuote = false;
63+
}
64+
if (forceQuotes) {
65+
normalized.defaultStringType =
66+
quotingType === "'" ? 'QUOTE_SINGLE' : 'QUOTE_DOUBLE';
67+
}
68+
69+
return normalized;
70+
}
71+
72+
function parse(content, options) {
73+
return parseYaml(content, normalizeParseOptions(options));
74+
}
75+
76+
function stringify(content, options) {
77+
return stringifyYaml(content, normalizeStringifyOptions(options));
78+
}
79+
1980
export function safeLoad(content, ...args) {
20-
return retry(YAML.safeLoad, JSON.parse(JSON.stringify(content)), ...args);
81+
return retry(parse, toSerializable(content), ...args);
2182
}
2283
export function load(content, ...args) {
23-
return retry(YAML.load, JSON.parse(JSON.stringify(content)), ...args);
84+
return retry(parse, toSerializable(content), ...args);
2485
}
2586
export function safeDump(content, ...args) {
26-
return YAML.safeDump(JSON.parse(JSON.stringify(content)), ...args);
87+
return stringify(toSerializable(content), ...args);
2788
}
2889
export function dump(content, ...args) {
29-
return YAML.dump(JSON.parse(JSON.stringify(content)), ...args);
90+
return stringify(toSerializable(content), ...args);
3091
}
3192

3293
export default {

0 commit comments

Comments
 (0)