11import _ from "lodash"
2-
32import MD5 from "md5.js"
3+
44import { getConfig } from "./config"
55import { defaultRules } from "./radar-rules"
66import type { Rules } from "./types"
77import { getRadarRulesUrl } from "./utils"
88
99export function parseRules ( rules : string , forceJSON ?: boolean ) {
10- let incomeRules = rules
11- if ( incomeRules ) {
12- if ( typeof rules === "string" ) {
13- try {
14- incomeRules = JSON . parse ( rules )
15- } catch ( error ) { }
10+ let incomeRules : unknown = rules
11+
12+ if ( typeof rules === "string" ) {
13+ try {
14+ incomeRules = JSON . parse ( rules )
15+ } catch ( error ) {
16+ incomeRules = forceJSON ? { } : rules
1617 }
1718 }
18- return _ . mergeWith ( defaultRules , incomeRules , ( objValue , srcValue ) => {
19+
20+ if ( ! _ . isPlainObject ( incomeRules ) ) {
21+ incomeRules = { }
22+ }
23+
24+ return _ . mergeWith ( { } , defaultRules , incomeRules , ( objValue , srcValue ) => {
1925 if ( _ . isFunction ( srcValue ) ) {
2026 return srcValue
2127 } else if ( _ . isFunction ( objValue ) ) {
@@ -37,22 +43,34 @@ export function getRulesCount(rules: Rules) {
3743 return index
3844}
3945
40- export function getRemoteRules ( ) {
41- return new Promise < string > ( async ( resolve , reject ) => {
42- const config = await getConfig ( )
43- try {
44- let url = getRadarRulesUrl ( config . rsshubDomain )
45-
46- if ( config . rsshubAccessControl . accessKey ) {
47- const path = new URL ( url ) . pathname
48- const code = new MD5 ( ) . update ( path + config . rsshubAccessControl . accessKey ) . digest ( "hex" )
49- url = `${ url } ?code=${ code } `
50- }
46+ export async function getRemoteRules ( ) {
47+ const config = await getConfig ( )
48+ let url = getRadarRulesUrl ( config . rsshubDomain )
5149
52- const res = await fetch ( url )
53- resolve ( res . text ( ) )
54- } catch ( error ) {
55- reject ( error )
56- }
57- } )
58- }
50+ if ( config . rsshubAccessControl . accessKey ) {
51+ const path = new URL ( url ) . pathname
52+ const code = new MD5 ( )
53+ . update ( path + config . rsshubAccessControl . accessKey )
54+ . digest ( "hex" )
55+ url = `${ url } ?code=${ code } `
56+ }
57+
58+ const res = await fetch ( url )
59+ if ( ! res . ok ) {
60+ throw new Error ( `Failed to fetch remote rules: ${ res . status } ` )
61+ }
62+
63+ const text = await res . text ( )
64+ let parsed : unknown
65+ try {
66+ parsed = JSON . parse ( text )
67+ } catch {
68+ throw new Error ( "Invalid remote rules response" )
69+ }
70+
71+ if ( ! _ . isPlainObject ( parsed ) ) {
72+ throw new Error ( "Invalid remote rules payload" )
73+ }
74+
75+ return text
76+ }
0 commit comments