@@ -5,6 +5,7 @@ import {naturalSort, urljoin} from '../utils/utils.js'
55import { ENV } from "../utils/env.js" ;
66import { validatePwd } from "../utils/api_validate.js" ;
77import { getSitesMap } from "../utils/sites-map.js" ;
8+ import batchExecute from '../libs_drpy/batchExecute.js' ;
89
910const { jsEncoder} = drpy ;
1011
@@ -44,66 +45,78 @@ async function generateSiteJSON(jsDir, configDir, requestHost, sub, subFilePath,
4445 }
4546 let SitesMap = getSitesMap ( configDir ) ;
4647 // console.log(SitesMap);
47- // 使用 Promise.all 并发执行文件的处理
48- const filePromises = valid_files . map ( async ( file ) => {
49- const baseName = path . basename ( file , '.js' ) ; // 去掉文件扩展名
50- let api = `${ requestHost } /api/${ baseName } ` ; // 使用请求的 host 地址,避免硬编码端口
51- if ( pwd ) {
52- api += `?pwd=${ pwd } ` ;
53- }
54- let ruleObject = {
55- searchable : 0 , // 固定值
56- filterable : 0 , // 固定值
57- quickSearch : 0 , // 固定值
58- } ;
59- try {
60- ruleObject = await drpy . getRuleObject ( path . join ( jsDir , file ) ) ;
61- // console.log(file, ruleObject.title);
62- } catch ( e ) {
63- console . log ( `file:${ file } error:${ e . message } ` ) ;
64- }
65- let fileSites = [ ] ;
66- if ( baseName === 'push_agent' ) {
67- let key = 'push_agent' ;
68- let name = `${ ruleObject . title } (DS)` ;
69- fileSites . push ( { key, name} )
70- } else if ( SitesMap . hasOwnProperty ( baseName ) && Array . isArray ( SitesMap [ baseName ] ) ) {
71- SitesMap [ baseName ] . forEach ( ( it ) => {
72- let key = `drpyS_${ it . alias } ` ;
73- let name = `${ it . alias } (DS)` ;
74- let ext = '' ;
75- if ( it . queryObject . type === 'url' ) {
76- ext = it . queryObject . params ;
77- } else {
78- ext = it . queryStr ;
48+ const tasks = valid_files . map ( ( file ) => {
49+ return {
50+ func : async ( { file, jsDir, requestHost, pwd, drpy, SitesMap, jsEncoder} ) => {
51+ const baseName = path . basename ( file , '.js' ) ; // 去掉文件扩展名
52+ let api = `${ requestHost } /api/${ baseName } ` ; // 使用请求的 host 地址,避免硬编码端口
53+ if ( pwd ) {
54+ api += `?pwd=${ pwd } ` ;
7955 }
80- if ( ext ) {
81- ext = jsEncoder . gzip ( ext ) ;
56+ let ruleObject = {
57+ searchable : 0 , // 固定值
58+ filterable : 0 , // 固定值
59+ quickSearch : 0 , // 固定值
60+ } ;
61+ try {
62+ ruleObject = await drpy . getRuleObject ( path . join ( jsDir , file ) ) ;
63+ } catch ( e ) {
64+ throw new Error ( `Error parsing rule object for file: ${ file } , ${ e . message } ` ) ;
8265 }
83- fileSites . push ( { key : key , name : name , ext : ext } )
84- } ) ;
85- } else {
86- let key = `drpyS_${ baseName } ` ;
87- let name = `${ baseName } (DS)` ;
88- fileSites . push ( { key, name} )
89- }
90- fileSites . forEach ( ( fileSite ) => {
91- const site = {
92- key : fileSite . key ,
93- name : fileSite . name ,
94- type : 4 , // 固定值
95- api,
96- searchable : ruleObject . searchable ,
97- filterable : ruleObject . filterable ,
98- quickSearch : ruleObject . quickSearch ,
99- more : ruleObject . more ,
100- ext : fileSite . ext || "" , // 固定为空字符串
101- } ;
102- sites . push ( site ) ;
103- } ) ;
66+
67+ let fileSites = [ ] ;
68+ if ( baseName === 'push_agent' ) {
69+ let key = 'push_agent' ;
70+ let name = `${ ruleObject . title } (DS)` ;
71+ fileSites . push ( { key, name} ) ;
72+ } else if ( SitesMap . hasOwnProperty ( baseName ) && Array . isArray ( SitesMap [ baseName ] ) ) {
73+ SitesMap [ baseName ] . forEach ( ( it ) => {
74+ let key = `drpyS_${ it . alias } ` ;
75+ let name = `${ it . alias } (DS)` ;
76+ let ext = it . queryObject . type === 'url' ? it . queryObject . params : it . queryStr ;
77+ if ( ext ) {
78+ ext = jsEncoder . gzip ( ext ) ;
79+ }
80+ fileSites . push ( { key, name, ext} ) ;
81+ } ) ;
82+ } else {
83+ let key = `drpyS_${ baseName } ` ;
84+ let name = `${ baseName } (DS)` ;
85+ fileSites . push ( { key, name} ) ;
86+ }
87+
88+ fileSites . forEach ( ( fileSite ) => {
89+ const site = {
90+ key : fileSite . key ,
91+ name : fileSite . name ,
92+ type : 4 , // 固定值
93+ api,
94+ searchable : ruleObject . searchable ,
95+ filterable : ruleObject . filterable ,
96+ quickSearch : ruleObject . quickSearch ,
97+ more : ruleObject . more ,
98+ ext : fileSite . ext || "" , // 固定为空字符串
99+ } ;
100+ sites . push ( site ) ;
101+ } ) ;
102+ } ,
103+ param : { file, jsDir, requestHost, pwd, drpy, SitesMap, jsEncoder} ,
104+ id : file ,
105+ } ;
104106 } ) ;
105- // 等待所有的文件处理完成
106- await Promise . all ( filePromises ) ;
107+
108+ const listener = {
109+ func : ( param , id , error , result ) => {
110+ if ( error ) {
111+ console . error ( `Error processing file ${ id } :` , error . message ) ;
112+ } else {
113+ // console.log(`Successfully processed file ${id}:`, result);
114+ }
115+ } ,
116+ param : { } , // 外部参数可以在这里传入
117+ } ;
118+
119+ await batchExecute ( tasks , listener ) ;
107120 // 订阅再次处理别名的情况
108121 if ( sub ) {
109122 if ( sub . mode === 0 ) {
0 commit comments