@@ -15,6 +15,22 @@ import batchExecute from '../libs_drpy/batchExecute.js';
1515
1616const { jsEncoder} = drpyS ;
1717
18+ function parseExt ( str ) {
19+ try {
20+ const parsed = JSON . parse ( str ) ;
21+ if ( Array . isArray ( parsed ) || ( typeof parsed === 'object' && parsed !== null ) ) {
22+ return parsed ;
23+ }
24+ } catch ( e ) {
25+ // 忽略错误
26+ }
27+ return str ;
28+ }
29+
30+ function logExt ( _ext ) {
31+ return Array . isArray ( _ext ) || typeof _ext == "object" ? JSON . stringify ( _ext ) : _ext
32+ }
33+
1834// 工具函数:生成 JSON 数据
1935async function generateSiteJSON ( options , requestHost , sub , pwd ) {
2036 const jsDir = options . jsDir ;
@@ -373,11 +389,8 @@ async function generateSiteJSON(options, requestHost, sub, pwd) {
373389 let key = `hipy_py_${ it . alias } ` ;
374390 let name = `${ it . alias } (hipy)` ;
375391 let _ext = it . queryStr ;
376- try {
377- _ext = JSON . parse ( _ext ) ;
378- } catch ( err ) {
379- }
380- console . log ( `[HIPY-${ baseName } ] alias name: ${ name } ,typeof _ext:${ typeof _ext } ,_ext: ${ _ext } ` ) ;
392+ _ext = parseExt ( _ext ) ;
393+ console . log ( `[HIPY-${ baseName } ] alias name: ${ name } ,typeof _ext:${ typeof _ext } ,_ext: ${ logExt ( _ext ) } ` ) ;
381394 fileSites . push ( { key, name, ext : _ext } ) ;
382395 } ) ;
383396 } else {
@@ -468,12 +481,8 @@ async function generateSiteJSON(options, requestHost, sub, pwd) {
468481 let key = `catvod_${ it . alias } ` ;
469482 let name = `${ it . alias } (cat)` ;
470483 let _ext = it . queryStr ;
471-
472- try {
473- _ext = JSON . parse ( _ext ) ;
474- } catch ( err ) {
475- }
476- console . log ( `[CAT-${ baseName } ] alias name: ${ name } ,typeof _ext:${ typeof _ext } ,_ext: ${ _ext } ` ) ;
484+ _ext = parseExt ( _ext ) ;
485+ console . log ( `[CAT-${ baseName } ] alias name: ${ name } ,typeof _ext:${ typeof _ext } ,_ext: ${ logExt ( _ext ) } ` ) ;
477486 fileSites . push ( { key, name, ext : _ext } ) ;
478487 } ) ;
479488 } else {
@@ -753,10 +762,13 @@ export default (fastify, options, done) => {
753762 // }
754763 // }
755764 const getFilePath = ( cfgPath , rootDir , fileName ) => path . join ( rootDir , `data/cat/${ fileName } ` ) ;
756- const processContent = ( content , cfgPath , requestUrl ) =>
757- content . replace ( '$config_url' , requestUrl . replace ( cfgPath , `/1?sub=all&pwd=${ process . env . API_PWD || '' } ` ) ) ;
765+ const processContent = ( content , cfgPath , requestUrl , requestHost ) => {
766+ const $config_url = requestUrl . replace ( cfgPath , `/1?sub=all&pwd=${ process . env . API_PWD || '' } ` ) ;
767+ return content . replaceAll ( '$config_url' , $config_url ) . replaceAll ( '$host' , requestHost ) ;
768+ }
769+
758770
759- const handleJavaScript = ( cfgPath , requestUrl , options , reply ) => {
771+ const handleJavaScript = ( cfgPath , requestUrl , requestHost , options , reply ) => {
760772 const fileMap = {
761773 'index.js' : 'index.js' ,
762774 'index.config.js' : 'index.config.js'
@@ -766,7 +778,7 @@ export default (fastify, options, done) => {
766778 if ( cfgPath . includes ( key ) ) {
767779 const filePath = getFilePath ( cfgPath , options . rootDir , fileName ) ;
768780 let content = readFileSync ( filePath , 'utf-8' ) ;
769- content = processContent ( content , cfgPath , requestUrl ) ;
781+ content = processContent ( content , cfgPath , requestUrl , requestHost ) ;
770782 return reply . type ( 'application/javascript;charset=utf-8' ) . send ( content ) ;
771783 }
772784 }
@@ -790,7 +802,7 @@ export default (fastify, options, done) => {
790802 }
791803 } ;
792804 if ( cfg_path . endsWith ( '.js' ) ) {
793- return handleJavaScript ( cfg_path , requestUrl , options , reply ) ;
805+ return handleJavaScript ( cfg_path , requestUrl , requestHost , options , reply ) ;
794806 }
795807
796808 if ( cfg_path . endsWith ( '.js.md5' ) ) {
0 commit comments