@@ -16,45 +16,32 @@ import { nextTime } from "./cron";
1616import { parseUserConfig } from "./yaml" ;
1717import { t as i18n_t } from "@App/locales/locales" ;
1818
19+ const HEADER_BLOCK = / \/ \/ [ \t ] * = = U s e r ( S c r i p t | S u b s c r i b e ) = = ( [ \s \S ] + ?) \/ \/ [ \t ] * = = \/ U s e r \1= = / m;
20+ const META_LINE = / \/ \/ [ \t ] * @ ( \S + ) [ \t ] * ( .* ) $ / gm;
21+
1922// 从脚本代码抽出Metadata
2023export function parseMetadata ( code : string ) : SCMetadata | null {
21- let issub = false ;
22- let regex = / \/ \/ \s * = = U s e r S c r i p t = = ( [ \s \S ] + ?) \/ \/ \s * = = \/ U s e r S c r i p t = = / m;
23- let header = regex . exec ( code ) ;
24- if ( ! header ) {
25- regex = / \/ \/ \s * = = U s e r S u b s c r i b e = = ( [ \s \S ] + ?) \/ \/ \s * = = \/ U s e r S u b s c r i b e = = / m;
26- header = regex . exec ( code ) ;
27- if ( ! header ) {
28- return null ;
29- }
30- issub = true ;
31- }
32- regex = / \/ \/ \s * @ ( \S + ) ( ( .+ ?) $ | $ ) / gm;
33- const ret = { } as SCMetadata ;
34- let meta : RegExpExecArray | null = regex . exec ( header [ 1 ] ) ;
35- while ( meta !== null ) {
36- const [ key , val ] = [ meta [ 1 ] . toLowerCase ( ) . trim ( ) , meta [ 2 ] . trim ( ) ] ;
37- let values = ret [ key ] ;
38- if ( ! values ) {
39- values = [ ] ;
40- }
41- values . push ( val ) ;
42- ret [ key ] = values ;
43- meta = regex . exec ( header [ 1 ] ) ;
44- }
45- if ( ret . name === undefined ) {
46- return null ;
47- }
48- if ( Object . keys ( ret ) . length < 3 ) {
24+ let isSubscribe = false ;
25+ let headerContent : string ;
26+ let m : RegExpExecArray | null ;
27+ if ( ( m = HEADER_BLOCK . exec ( code ) ) ) {
28+ isSubscribe = m [ 1 ] === "Subscribe" ;
29+ headerContent = m [ 2 ] ;
30+ } else {
4931 return null ;
5032 }
51- if ( ! ret . namespace ) {
52- ret . namespace = [ "" ] ;
53- }
54- if ( issub ) {
55- ret . usersubscribe = [ ] ;
33+ const metadata : SCMetadata = { } as SCMetadata ;
34+ META_LINE . lastIndex = 0 ; // Reset regex lastIndex (reuse)
35+ while ( ( m = META_LINE . exec ( headerContent ) ) !== null ) {
36+ const key = m [ 1 ] . toLowerCase ( ) ;
37+ const val = m [ 2 ] ?. trim ( ) ?? "" ;
38+ const values = metadata [ key ] || ( metadata [ key ] = [ ] ) ;
39+ values . push ( val ) ;
5640 }
57- return ret ;
41+ if ( ! metadata . name || Object . keys ( metadata ) . length < 3 ) return null ;
42+ if ( ! metadata . namespace ) metadata . namespace = [ "" ] ;
43+ if ( isSubscribe ) metadata . usersubscribe = [ ] ;
44+ return metadata ;
5845}
5946
6047// 从网址取得脚本代码
@@ -91,12 +78,15 @@ export async function prepareScriptByCode(
9178 if ( ! metadata ) {
9279 throw new Error ( i18n_t ( "error_metadata_invalid" ) ) ;
9380 }
94- if ( metadata . name === undefined ) {
81+ // 不接受空白name
82+ if ( ! metadata . name ?. [ 0 ] ) {
9583 throw new Error ( i18n_t ( "error_script_name_required" ) ) ;
9684 }
97- if ( metadata . version === undefined ) {
85+ // 不接受空白version
86+ if ( ! metadata . version ?. [ 0 ] ) {
9887 throw new Error ( i18n_t ( "error_script_version_required" ) ) ;
9988 }
89+ // 可接受空白namespace
10090 if ( metadata . namespace === undefined ) {
10191 throw new Error ( i18n_t ( "error_script_namespace_required" ) ) ;
10292 }
0 commit comments