From ed4ae7ade1bee8d2dc013cd4fb3bcfdc953d682e Mon Sep 17 00:00:00 2001 From: xream Date: Mon, 6 Apr 2026 01:53:57 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E5=8F=82=E6=95=B0=20?= =?UTF-8?q?prettyYaml=20=E8=BE=93=E5=87=BA=E6=9B=B4=E6=98=93=E8=AF=BB?= =?UTF-8?q?=E7=9A=84=E5=9D=97=E7=8A=B6=20YAML,=20=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E4=BB=8D=E6=98=AF=E5=8D=95=E8=A1=8C=20JSON=20=E9=A3=8E?= =?UTF-8?q?=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/package.json | 2 +- .../src/core/proxy-utils/producers/clash.js | 12 ++-- .../core/proxy-utils/producers/clashmeta.js | 12 ++-- .../src/core/proxy-utils/producers/egern.js | 11 +--- .../proxy-utils/producers/shadowrocket.js | 14 ++-- .../src/core/proxy-utils/producers/stash.js | 12 ++-- .../src/core/proxy-utils/producers/utils.js | 22 +++++++ backend/src/products/cron-sync-artifacts.js | 1 + backend/src/restful/download.js | 10 +++ backend/src/restful/sync.js | 2 + .../test/proxy-producers/structured.spec.js | 66 +++++++++++++++++++ scripts/demo.js | 3 + 12 files changed, 128 insertions(+), 39 deletions(-) diff --git a/backend/package.json b/backend/package.json index 98409ec1..e0634ce9 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.21.77", + "version": "2.21.78", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.", "main": "src/main.js", "scripts": { diff --git a/backend/src/core/proxy-utils/producers/clash.js b/backend/src/core/proxy-utils/producers/clash.js index 382ea742..449b0dcb 100644 --- a/backend/src/core/proxy-utils/producers/clash.js +++ b/backend/src/core/proxy-utils/producers/clash.js @@ -1,4 +1,7 @@ -import { isPresent } from '@/core/proxy-utils/producers/utils'; +import { + isPresent, + produceProxyListOutput, +} from '@/core/proxy-utils/producers/utils'; import $ from '@/core/app'; export default function Clash_Producer() { @@ -213,12 +216,7 @@ export default function Clash_Producer() { } return proxy; }); - return type === 'internal' - ? list - : 'proxies:\n' + - list - .map((proxy) => ' - ' + JSON.stringify(proxy) + '\n') - .join(''); + return produceProxyListOutput(list, type, opts); }; return { type, produce }; } diff --git a/backend/src/core/proxy-utils/producers/clashmeta.js b/backend/src/core/proxy-utils/producers/clashmeta.js index 196cb4a5..350d608e 100644 --- a/backend/src/core/proxy-utils/producers/clashmeta.js +++ b/backend/src/core/proxy-utils/producers/clashmeta.js @@ -1,4 +1,7 @@ -import { isPresent } from '@/core/proxy-utils/producers/utils'; +import { + isPresent, + produceProxyListOutput, +} from '@/core/proxy-utils/producers/utils'; const ipVersions = { dual: 'dual', @@ -301,12 +304,7 @@ export default function ClashMeta_Producer() { return proxy; }); - return type === 'internal' - ? list - : 'proxies:\n' + - list - .map((proxy) => ' - ' + JSON.stringify(proxy) + '\n') - .join(''); + return produceProxyListOutput(list, type, opts); }; return { type, produce }; } diff --git a/backend/src/core/proxy-utils/producers/egern.js b/backend/src/core/proxy-utils/producers/egern.js index c64f63a5..5b753e1b 100644 --- a/backend/src/core/proxy-utils/producers/egern.js +++ b/backend/src/core/proxy-utils/producers/egern.js @@ -1,9 +1,9 @@ import $ from '@/core/app'; -import { isPresent } from './utils'; +import { isPresent, produceProxyListOutput } from './utils'; export default function Egern_Producer() { const type = 'ALL'; - const produce = (proxies, type) => { + const produce = (proxies, type, opts = {}) => { // https://egernapp.com/zh-CN/docs/configuration/proxies const list = proxies .filter((proxy) => { @@ -572,12 +572,7 @@ export default function Egern_Producer() { } }) .filter(Boolean); - return type === 'internal' - ? list - : 'proxies:\n' + - list - .map((proxy) => ' - ' + JSON.stringify(proxy) + '\n') - .join(''); + return produceProxyListOutput(list, type, opts); }; return { type, produce }; } diff --git a/backend/src/core/proxy-utils/producers/shadowrocket.js b/backend/src/core/proxy-utils/producers/shadowrocket.js index 3222b898..aceda9ec 100644 --- a/backend/src/core/proxy-utils/producers/shadowrocket.js +++ b/backend/src/core/proxy-utils/producers/shadowrocket.js @@ -1,4 +1,7 @@ -import { isPresent } from '@/core/proxy-utils/producers/utils'; +import { + isPresent, + produceProxyListOutput, +} from '@/core/proxy-utils/producers/utils'; import $ from '@/core/app'; export default function Shadowrocket_Producer() { @@ -271,14 +274,7 @@ export default function Shadowrocket_Producer() { } return proxy; }); - return type === 'internal' - ? list - : 'proxies:\n' + - list - .map((proxy) => { - return ' - ' + JSON.stringify(proxy) + '\n'; - }) - .join(''); + return produceProxyListOutput(list, type, opts); }; return { type, produce }; } diff --git a/backend/src/core/proxy-utils/producers/stash.js b/backend/src/core/proxy-utils/producers/stash.js index 35a38dc5..f3e45bb2 100644 --- a/backend/src/core/proxy-utils/producers/stash.js +++ b/backend/src/core/proxy-utils/producers/stash.js @@ -1,4 +1,7 @@ -import { isPresent } from '@/core/proxy-utils/producers/utils'; +import { + isPresent, + produceProxyListOutput, +} from '@/core/proxy-utils/producers/utils'; import $ from '@/core/app'; export default function Stash_Producer() { @@ -347,12 +350,7 @@ export default function Stash_Producer() { } return proxy; }); - return type === 'internal' - ? list - : 'proxies:\n' + - list - .map((proxy) => ' - ' + JSON.stringify(proxy) + '\n') - .join(''); + return produceProxyListOutput(list, type, opts); }; return { type, produce }; } diff --git a/backend/src/core/proxy-utils/producers/utils.js b/backend/src/core/proxy-utils/producers/utils.js index e8dc964d..c33b76a8 100644 --- a/backend/src/core/proxy-utils/producers/utils.js +++ b/backend/src/core/proxy-utils/producers/utils.js @@ -1,4 +1,5 @@ import _ from 'lodash'; +import YAML from '@/utils/yaml'; export class Result { constructor(proxy) { @@ -28,3 +29,24 @@ export function isPresent(obj, attr) { const data = _.get(obj, attr); return typeof data !== 'undefined' && data !== null; } + +export function produceProxyListOutput(list, type, opts = {}) { + if (type === 'internal') return list; + + if ( + opts.prettyYaml || + opts['pretty-yaml'] + ) { + return YAML.safeDump( + { + proxies: list, + }, + { + lineWidth: -1, + }, + ); + } + + return 'proxies:\n' + + list.map((proxy) => ' - ' + JSON.stringify(proxy) + '\n').join(''); +} diff --git a/backend/src/products/cron-sync-artifacts.js b/backend/src/products/cron-sync-artifacts.js index e1eac648..c890b653 100644 --- a/backend/src/products/cron-sync-artifacts.js +++ b/backend/src/products/cron-sync-artifacts.js @@ -157,6 +157,7 @@ async function doSync() { 'include-unsupported-proxy': artifact.includeUnsupportedProxy, useMihomoExternal, + prettyYaml: artifact.prettyYaml, }, }); diff --git a/backend/src/restful/download.js b/backend/src/restful/download.js index 34d2e7cf..53723e33 100644 --- a/backend/src/restful/download.js +++ b/backend/src/restful/download.js @@ -107,6 +107,7 @@ async function downloadSubscription(req, res) { noCache, _fakeNode, } = req.query; + const prettyYaml = req.query.prettyYaml ?? req.query['pretty-yaml']; let $options = { _req: { @@ -169,6 +170,9 @@ async function downloadSubscription(req, res) { `包含官方/商店版/未续费订阅不支持的协议: ${includeUnsupportedProxy}`, ); } + if (prettyYaml) { + $.info(`指定输出易读 YAML: ${prettyYaml}`); + } if (useMihomoExternal) { $.info(`手动指定了 target 为 SurgeMac, 将使用 Mihomo External`); @@ -208,6 +212,7 @@ async function downloadSubscription(req, res) { produceOpts: { 'include-unsupported-proxy': includeUnsupportedProxy, useMihomoExternal, + prettyYaml, }, $options, proxy, @@ -415,6 +420,7 @@ async function downloadCollection(req, res) { proxy, noCache, } = req.query; + const prettyYaml = req.query.prettyYaml ?? req.query['pretty-yaml']; let $options = { _req: { @@ -463,6 +469,9 @@ async function downloadCollection(req, res) { `包含官方/商店版/未续费订阅不支持的协议: ${includeUnsupportedProxy}`, ); } + if (prettyYaml) { + $.info(`指定输出易读 YAML: ${prettyYaml}`); + } if (useMihomoExternal) { $.info(`手动指定了 target 为 SurgeMac, 将使用 Mihomo External`); @@ -482,6 +491,7 @@ async function downloadCollection(req, res) { produceOpts: { 'include-unsupported-proxy': includeUnsupportedProxy, useMihomoExternal, + prettyYaml, }, $options, proxy, diff --git a/backend/src/restful/sync.js b/backend/src/restful/sync.js index b88b406e..74138c57 100644 --- a/backend/src/restful/sync.js +++ b/backend/src/restful/sync.js @@ -684,6 +684,7 @@ async function syncArtifacts() { 'include-unsupported-proxy': artifact.includeUnsupportedProxy, useMihomoExternal, + prettyYaml: artifact.prettyYaml, }, }); @@ -838,6 +839,7 @@ async function syncArtifact(req, res) { produceOpts: { 'include-unsupported-proxy': artifact.includeUnsupportedProxy, useMihomoExternal, + prettyYaml: artifact.prettyYaml, }, }); diff --git a/backend/src/test/proxy-producers/structured.spec.js b/backend/src/test/proxy-producers/structured.spec.js index 6bdb22b7..a4370161 100644 --- a/backend/src/test/proxy-producers/structured.spec.js +++ b/backend/src/test/proxy-producers/structured.spec.js @@ -7,6 +7,7 @@ import { expectSubset, loadProducedJson, loadProducedYaml, + produceExternal, produceInternal, } from './helpers'; @@ -571,6 +572,71 @@ describe('Proxy structured producers', function () { } }); + it('keeps legacy single-line proxy output by default for Clash-style YAML producers', function () { + const proxy = { + type: 'ss', + name: 'Legacy YAML', + server: 'ss.example.com', + port: 8388, + cipher: 'aes-128-gcm', + password: 'secret', + }; + + for (const platform of [ + 'Clash', + 'ClashMeta', + 'Shadowrocket', + 'Stash', + 'Egern', + ]) { + const output = produceExternal(platform, proxy); + + expect(output, platform).to.match(/\n - \{/); + } + }); + + it('emits pretty yaml for Clash-style YAML producers when prettyYaml is enabled', function () { + const proxy = { + type: 'ss', + name: 'Real YAML', + server: 'ss.example.com', + port: 8388, + cipher: 'aes-128-gcm', + password: 'secret', + }; + + for (const platform of [ + 'Clash', + 'ClashMeta', + 'Shadowrocket', + 'Stash', + 'Egern', + ]) { + const output = produceExternal(platform, proxy, { + prettyYaml: true, + }); + const external = loadProducedYaml(platform, proxy, { + prettyYaml: true, + }); + + expect(output, platform).to.not.match(/\n - \{/); + expect(external.proxies, platform).to.have.length(1); + + if (platform === 'Egern') { + expectSubset(external.proxies[0], { + shadowsocks: { + name: 'Real YAML', + }, + }); + } else { + expectSubset(external.proxies[0], { + type: 'ss', + name: 'Real YAML', + }); + } + } + }); + it('skips invalid Egern nodes and keeps the rest of the subscription', function () { const proxies = [ { diff --git a/scripts/demo.js b/scripts/demo.js index 32d6cb42..fbbe996b 100644 --- a/scripts/demo.js +++ b/scripts/demo.js @@ -258,6 +258,9 @@ function operator(proxies = [], targetPlatform, context) { // type: 'subscription', // name: 'sub', // platform: 'ClashMeta', + // produceOpts: { + // prettyYaml: true // 输出更易读的块状 YAML, 默认仍是单行 JSON 风格 + // }, // produceType: 'internal' // 'internal' produces an Array, otherwise produces a String( ProxyUtils.yaml.safeLoad('YAML String').proxies ) // })